简体   繁体   English

Keras预测对于测试集中的不同类别给出相同的结果

[英]Keras Prediction gives the same result for different classes in test set

all. 所有。

I am using transfer learning to build a new model based on my own samples. 我正在使用转移学习根据自己的样本构建新模型。 The learning framework is Keras 2.0+. 学习框架是Keras 2.0+。 I modified the codes in reference to this page: Fine-tune InceptionV3 on a new set of classes https://keras.io/applications/ 我参考以下页面修改了代码: 在一组新的类 https://keras.io/applications/ 上微调InceptionV3

Nothing goes wrong in the training step. 在培训步骤中没有任何问题。 When I test the model using test sets, every pictures gives the same predicted class though they are from different classes. 当我使用测试集测试模型时,尽管每张图片都来自不同的类,但它们给出了相同的预测类。 Example: 例:

>>> print(preds)
[[0.0000000e+00 4.5558951e-38 0.0000000e+00 0.0000000e+00 6.3798614e-36
  8.4623914e-22 1.0000000e+00 1.0636564e-11]]
>>> print(pred_classes)
6

I test 10 pictures of 8 classes, all gave class 6. 我测试了8个班级的10张图片,都给了6级。

Any suggestions? 有什么建议么?

Training code: 培训代码:

from keras.applications.inception_v3 import InceptionV3, preprocess_input
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator

base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(8, activation='softmax')(x)

model = Model(inputs=base_model.input, outputs=predictions)

for layer in base_model.layers:
    layer.trainable = False

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

train_datagen=ImageDataGenerator(preprocessing_function=preprocess_input)
train_generator=train_datagen.flow_from_directory('./TranningSet',
                                                 target_size=(224,224),
                                                 color_mode='rgb',
                                                 batch_size=32,
                                                 class_mode='categorical',
                                                 shuffle=True)

step_size_train=train_generator.n//train_generator.batch_size
model.fit_generator(generator=train_generator,
                   steps_per_epoch=step_size_train,
                   epochs=100,
                   use_multiprocessing=True)

Final train accuracy is a bit low, but it's around 70% 最终火车的准确度较低,但大约为70%

50/50 [==============================] - 297s 6s/step - loss: 4.2306 - acc: 0.7040
Epoch 99/100
50/50 [==============================] - 303s 6s/step - loss: 3.7681 - acc: 0.7387
Epoch 100/100
50/50 [==============================] - 293s 6s/step - loss: 3.7569 - acc: 0.7443
<keras.callbacks.History object at 0x7fd931756bd0>
>>>

Prediction code: 预测代码:

import keras
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras.models import Model
import numpy as np
from keras.models import load_model
from keras.applications.inception_v3 import preprocess_input

model = load_model('/root/AIdetection/Keras/V6.5/20181217_V6.5.h5')


from keras.preprocessing import image
img =image.load_img('/root/AIdetection/Keras/V6.5/TestSet/Healthy50/20181026.06.JPG', target_size=(224, 224))
x = image.img_to_array(img)
x *= (255.0/x.max())
image = np.expand_dims(x, axis = 0)
image = preprocess_input(image)
preds = model.predict(image)
pred_classes = np.argmax(preds)
print(preds)
print(pred_classes)

Is your training data balanced and are you shuffling it before training? 您的训练数据是否平衡,并且在训练之前进行了改组? In other words, is it possible that most of your training data is of class 6 and it is learning to simply predict 6 every time? 换句话说,您的大多数训练数据是否有可能属于6类,并且每次都在学习简单地预测6类?

Also check that your test set is in the same format as your train set. 还要检查测试仪和火车仪的格式是否相同。 Are you doing any type of image processing before passing the train data to your model? 在将训练数据传递到模型之前,您是否要进行任何类型的图像处理?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM