Michael S.
Michael S.

Reputation: 195

Wrong predictions using model.predict()

I have a problem with making predictions in transfer-learned VGG16 net. I have a model trained with Adam on 7 classes. It was trained with fit_generator(), using ImageDataGenerator. I am loading model using:

# load the model we saved
model = load_model('models/vgg16_9.h5')
model.compile(loss='categorical_crossentropy',
              optimizer=optimizers.Adam(lr=1e-4),
              metrics=['acc'])

And then trying to made predictions. First, I used predict_generator() to make a .CSV file with results:

test_datagen = ImageDataGenerator(rescale = 1./255)
test_generator = test_datagen.flow_from_directory("dataset/test_set",
                                                  target_size=(227, 454),
                                                  batch_size=1,
                                                  class_mode=None,
                                                  shuffle=False,
                                                  seed=42)

test_generator.reset()
pred = model.predict_generator(test_generator, verbose = 1)
predicted_class_indices = np.argmax(pred, axis = 1)

labels = (valid_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
predictions = [labels[k] for k in predicted_class_indices]

filenames=test_generator.filenames
results=pd.DataFrame({"Filename":filenames,
                      "Predictions":predictions})
results.to_csv("results.csv",index=False)

It works ok, and I get a result like:

...
Filename,Predictions
test\green.1191.png,Green
test\green.1195.png,Green
test\green.1196.png,Green
test\green.1197.png,Green
test\green.1198.png,OK
test\green.1199.png,Green
test\green.1200.png,Green
test\green.1201.png,Green
test\green.1202.png,OK
test\green.1203.png,Green
test\green.1204.png,OK
test\green.1205.png,Green
test\green.1206.png,Green
test\green.1207.png,Green
...

But when I am trying to make a single image prediction with:

# predicting images
test_image = image.load_img('dataset/test_set/test/green.1230.png', target_size = (227, 454))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = model.predict(test_image, verbose=1)
y_pred = np.argmax(result, axis = 1)

My y_pred is [6] and if I am reading class_indices correctly [6] is totaly other class (predict_generator() is doing well).

Class indices:

class_names = (valid_generator.class_indices)

class_names = dict((v,k) for k,v in class_names.items())
class_names_list = []
temp = []

for key, value in class_names.items():
    temp = value
    class_names_list.append(temp)

Gives me:

{0: 'Green', 1: 'Half', 2: 'Moldy', 3: 'NoEmbryo', 4: 'OK', 5: 'Organic', 6: 'Stones'}

What am I doing wrong?

Upvotes: 1

Views: 2225

Answers (1)

Geeocode
Geeocode

Reputation: 5797

Your problem are rooted probably from python dict to list conversion. When your prediction goes from predict_generator(), then it chooses from a dict by key.

In the second example you convert it to a list and uses the list's indices, which will be quite other result.

I don't really get what is the purpose of:

for key, value in class_names.items():
    temp = value
    class_names_list.append(temp)

But if you get the class result from the class_names_list, you will get wrong results. So:

y_pred = np.argmax(result, axis = 1)
class_names[y_pred]

should give you the correct value.

Upvotes: 2

Related Questions