ValueError:找不到可以处理输入的数据适配器:

2024-04-16 22:36:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用合并模型(GoogleNet和ResNet)应用下面的代码来预测一张癌症图像。我使用了concatenate函数来合并模型。 但是,我在model.fit中使用的行验证_steps=len(test_set)出现错误,即使测试集和训练集中都有图像。 请帮我解决这个问题

from keras.models import load_model
merged_model = load_model('googleResNet.h5')
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                               shear_range = 0.2,
                               zoom_range = 0.2,
                               horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('Images/Train',
                                             target_size = (224, 224),
                                             batch_size = 32,
                                             class_mode = 'categorical')

test_set = test_datagen.flow_from_directory('Images/Test',
                                        target_size = (224, 224),
                                        batch_size = 32,
                                        class_mode = 'categorical')

r = merged_model.fit(
[training_set,training_set2],
validation_data = [test_set,test_set2],
epochs=5,
steps_per_epoch = len(training_set),
validation_steps = len(test_set)
)

# loss
plt.plot(r.history['loss'], label='train loss')
plt.plot(r.history['val_loss'], label='val loss')
plt.legend()
plt.show()
plt.savefig('LossVal_loss')

# accuracies
plt.plot(r.history['accuracy'], label='train acc')
plt.plot(r.history['val_accuracy'], label='val acc')
plt.legend()
plt.show()
plt.savefig('AccVal_acc')


#Test the model
new_image = plt.imread('img_004.jpg') #read in the image (3,14,20)

#show the uploaded image
img = plt.imshow(new_image)

from tensorflow.keras.preprocessing import image
img = image.load_img('img_004.jpg',target_size=(224,224))
img = np.asarray(img)
plt.imshow(img)
img = np.expand_dims(img, axis=0)
predictions = model.predict(img)

list_index = [0,1]

x = predictions

for i in range (2):
   for j in range(2):
     if x[0][list_index][i] > x[0][list_index][j]:
     temp = list_index[i]
     list_index[i] = list_index[j]
     list_index[j] = temp

#Show the sorted labels in order from highesh probability to lowest
print(list_index)
print('')

classification = ['mass','calcifications']

 i = 0
 for i in range(3):
 print(classification[list_index[i]],';',round(predictions[0][list_index[i]]*100,2),'%')

请查找完整错误: 请在下面查找错误跟踪:

 ValueError                                Traceback (most recent call last)
 <ipython-input-21-1294c8191a37> in <module>()
 33   epochs=5,
 34   steps_per_epoch = len(training_set),
  ---> 35   validation_steps = len(test_set)
 36 )
 37 

 3 frames
/usr/local/lib/python3.6/dist- 
packages/tensorflow/python/keras/engine/data_adapter.py in 
select_data_adapter(x, 
y)
969         "Failed to find data adapter that can handle "
970         "input: {}, {}".format(
--> 971             _type_name(x), _type_name(y)))
972   elif len(adapter_cls) > 1:
973     raise RuntimeError(

ValueError: Failed to find data adapter that can handle input: (<class 
'list'> containing 
values of types {"<class 
'tensorflow.python.keras.preprocessing.image.DirectoryIterator'>"}), <class 
 'NoneType'>

Tags: infromtestimageimgindexmodellen