tensorflow.python.framework.errors_impl.AlreadyExistsError



我使用teatable Machine训练了一个imagecclassifier模型,并尝试在python 3.8的VScode上运行以下代码

from keras.models import load_model
from PIL import Image, ImageOps
import numpy as np
# Load the model
model = load_model('keras_model.h5')
# Create the array of the right shape to feed into the keras model
# The 'length' or number of images you can put into the array is
# determined by the first position in the shape tuple, in this case 1.
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
# Replace this with the path to your image
image = Image.open('1.jpeg')
#resize the image to a 224x224 with the same strategy as in TM2:
#resizing the image to be at least 224x224 and then cropping from the center
size = (224, 224)
image = ImageOps.fit(image, size, Image.ANTIALIAS)
#turn the image into a numpy array
image_array = np.asarray(image)
# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
# Load the image into the array
data[0] = normalized_image_array
# run the inference
prediction = model.predict(data)
print(prediction)

,我得到以下错误
2021-09-29 11:37:52.587380: E tensorflow/core/lib/monitoring/collection_registry.cc:77] Cannot register 2 metrics with the same name: /tensorflow/api/keras/dropout/temp_rate_is_zero
Traceback (most recent call last):
File "c:/Users/sumuk/OneDrive/Documents/ML/converted_keras/1.py", line 1, in <module>
from keras.models import load_model
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packageskeras__init__.py", line 25, in <module>
from keras import models
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packageskerasmodels.py", line 20, in <module>
from keras import metrics as metrics_module
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packageskerasmetrics.py", line 26, in <module>
from keras import activations
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packageskerasactivations.py", line 20, in <module>
from keras.layers import advanced_activations
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packageskeraslayers__init__.py", line 31, in <module>     
from keras.layers.preprocessing.image_preprocessing import CenterCrop
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packageskeraslayerspreprocessingimage_preprocessing.py", 
line 24, in <module>
from keras.preprocessing import image as image_preprocessing
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packageskeraspreprocessing__init__.py", line 26, in <module>
from keras.utils import all_utils as utils
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packageskerasutilsall_utils.py", line 34, in <module>     
from keras.utils.multi_gpu_utils import multi_gpu_model
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packageskerasutilsmulti_gpu_utils.py", line 20, in <module>
from keras.layers.core import Lambda
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packageskeraslayerscore__init__.py", line 20, in <module>    from keras.layers.core.dropout import Dropout
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packageskeraslayerscoredropout.py", line 26, in <module> 
keras_temporary_dropout_rate = tf.__internal__.monitoring.BoolGauge(
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythoneagermonitoring.py", line 360, in __init__
super(BoolGauge, self).__init__('BoolGauge', _bool_gauge_methods,
File "C:UserssumukAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythoneagermonitoring.py", line 135, in __init__
self._metric = self._metric_methods[self._label_length].create(*args)
tensorflow.python.framework.errors_impl.AlreadyExistsError: Another metric with the same name already exists.

这是模型,我在网上找不到相关的解决方案,应该怎么做?谢谢你

要运行此代码,需要使用

from tensorflow.keras.models import load_model

代替

from keras.models import load_model

此问题是由于系统中可用的tensorflowkeras版本不匹配造成的。确保您使用相同版本的tensorflowkeras或至少最新的tensorflow 2.7,并再次尝试执行相同的代码。如果问题仍然存在,请告诉我们。

最新更新