Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
806 views
in Technique[技术] by (71.8m points)

keras - ValueError: Found input variables with inconsistent numbers of samples: [32, 624]

When plotting confusion matrix I got this error , if I set batch size is 624 and steps = 1 then code
runs fine but if I set batch size 32 and steps 20 then I got this error why ? I have total 624
test images of two classes normal and penumonia

test_batch= ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) 
.flow_from_directory(directory=test_path, target_size=(224,224), classes=['NORMAL', 'PNEUMONIA'], 
 batch_size=32, shuffle=False)


assert  train_batch.n == 3616
assert  val_batch.n == 1616
assert  test_batch.n == 624


assert train_batch.num_classes == val_batch.num_classes == test_batch.num_classes == 2


test_imgs , test_labels = next(test_batch)

test_labels = test_labels[:,0]

prediction = model.predict_generator(test_batch,steps=20)

cm = confusion_matrix (test_labels, prediction[:,0]) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This might be because you have 624 samples and with a batch size of 32, you would have 624 // 32 = 19 steps. You are trying to pass steps as 20. When you change the batch size you also need to change steps accordingly.

Also, predict_generator etc. is deprecated you can use normal predict for generators.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.7k users

...