图片预测错误 [英] Wrong prediction on images

查看:92
本文介绍了图片预测错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之 我正在使用我的数据集与Keras一起迈出第一步. 当我尝试加载模型并在测试图像上进行测试时,尽管先前的训练状态输出仍然只有77个类别,但我仅获得一个类别.您能否指出,预测代码中配置错误的内容是什么?

In short I'm going my first steps with Keras using my dataset. When I try to load a model and test on test-images - I obtain only one class despite the previous output on training states the accuracy was 77%. Could you point out, what is misconfigured in the prediction code?

详细说明: 该数据集是标记的生物细胞,灰度为64x64.有两种类型的单元格p和c. 我按照cat/dogs示例中的建议创建了数据集目录. 分别使用panca和canca进行培训,测试和val目录. canca,canca目录包含图像. 例如:

Detailed description: The dataset are marked biological cells in gray-scale, size 64x64. There are 2 types of cells p and c. I created the dataset directories as proposed in cat/dogs example. train, test, val irectories with panca, canca in each. canca, canca directories contain images. e.g.:

train-panca-<images>
train-canca-<images>

我主要还是从cat/dog的示例中得出了代码,并更改了图像大小.

I've derived the code mainly also from cat/dog example and changed the image size.

运行代码后,我得到输出:

After running the code, I obtain the output:

...
80/80 [==============================] - 8s 101ms/step - loss: 0.2995 - acc: 0.8910 - val_loss: 0.5150 - val_acc: 0.7560

Using TensorFlow backend.
Finished saving
Test loss: 0.46428223699331284
Test accuracy: 0.7759999990463257

这看起来是合理且有希望的-数据非常少,培训时间短(约3分钟),准确率达到77%令人难以置信. 我还测试了用火车目录替换测试目录,并获得了> 90%的准确性.因此该模型看起来很实用.

This looks reasonable and promising - Accuracy of 77% with very litle data and short training time (ca. 3 minutes) is incredible. I also tested replacing the test directory by train directory and obtain the accuracy of > 90%. So the model looks functional.

但是,当我尝试加载它并在测试图像上进行测试时,我只能获得一个类.

However, when I try to load it and test on test-images - I obtain only one class.

train.py

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K


# dimensions of the images.
img_width, img_height = 64, 64

train_dir = 'c:/tmp/anca_pc/train'
val_dir = 'c:/tmp/anca_pc/val'
test_dir = 'c:/tmp/anca_pc/test'

nb_train_samples = 2000
nb_validation_samples = 500
nb_test_samples = 500

epochs = 5
batch_size = 25

#Is right got gray-scale images? Shouldn't be 1 instead of 3
if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

#Define model as proposed in keras tutorials
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

datagen = ImageDataGenerator( rescale=1. / 255 )

train_generator = datagen.flow_from_directory(
    train_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

validation_generator = datagen.flow_from_directory(
    val_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

test_generator = datagen.flow_from_directory(
    test_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size)

#Save model architecture
model_json = model.to_json()
json_file = open("anca_pc.json", "w")
json_file.write(model_json)
json_file.close()

#Save model weights
model.save_weights("anca_pc.h5")
print("Finished saving")

score = model.evaluate_generator(test_generator, nb_test_samples // batch_size)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
print('Scode', score)

predict.py

import numpy as np
from keras.models import model_from_json
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import matplotlib.pyplot as plt
from scipy.misc import toimage
import os


classes = ['panca', 'canca']
directory = 'C:/tmp/anca_pc/test/'

json_file = open("anca_pc.json", "r")
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("anca_pc.h5")

loaded_model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])

for c in classes:
    aktdir=directory+c
    print(aktdir)
    for filename in os.listdir(aktdir):
        fn=os.path.join(aktdir, filename)
        #print(fn)
        img = image.load_img(fn, target_size=(64, 64))
        #plt.imshow(img)
        #plt.show()

        x = image.img_to_array(img)
        x = x.astype('float32')
        x /= 255
        x = np.expand_dims(x, axis=0)
        #x = preprocess_input(x)

        prediction = loaded_model.predict(x)

        #print(prediction)
        print(c + " : " + classes[np.argmax(prediction)], " : ", prediction)

编辑 model.predict_generator(test_generator, nb_test_samples // batch_size)的分数输出运行3个历元

EDIT Score output of model.predict_generator(test_generator, nb_test_samples // batch_size) run with 3 epochs

Test loss: 0.5996998563408852
Test accuracy: 0.7060000032186509
Score [[0.72468185]
 [0.07171335]
 [0.06702321]
 [0.04176971]
 [0.76247555]
 [0.07743845]
 [0.07435916]
 [0.9960306 ]
 [0.9270018 ]
 [0.04746262]
 [0.05305129]
 [0.9993339 ]
 [0.9986149 ]
 [0.63527316]
 [0.08033804]
 [0.3816172 ]
 [0.97601706]
 [0.83666223]
 [0.7226989 ]
 [0.5351326 ]
 [0.8407803 ]
 [0.6953097 ]
 [0.89651984]
 [0.44985726]
 [0.30889446]
 [0.16406931]
 [0.6346773 ]
 [0.13678996]
 [0.51343983]
 [0.97438985]
 [0.9981396 ]
 [0.5485193 ]
 [0.05270131]
 [0.8029713 ]
 [0.3295382 ]
 [0.1865853 ]
 [0.94497275]
 [0.07609159]
 [0.67434824]
 [0.18562992]
 [0.53442085]
 [0.06662691]
 [0.0388172 ]
 [0.8763066 ]
 [0.9875164 ]
 ...

推荐答案

np.argmax(prediction)将始终返回0,因为预测仅包含一个值.

np.argmax(prediction) will always return 0, because prediction contains only one value.

由于具有二进制输出,因此需要将np.argmax替换为以下内容:

Since you have a binary output, you need to replace np.argmax with something like this:

def get_class(prediction):
  return 1 if prediction > 0.5 else 0

这篇关于图片预测错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆