【问题标题】:Tensorflow Image Classification Python Always Saying Same AnswerTensorFlow 图像分类 Python 总是说相同的答案
【发布时间】:2019-02-22 05:16:45
【问题描述】:

我为我的一个课程编写了一个图像识别代码。我正在对“好”和“坏”的心脏超声图像进行分类。我遇到的问题是分类器总是预测图像是“好”的。目前我没有太多图片需要整理,所以准确率只有50%左右,但是我不确定为什么机器总是认为图片很好。

图片示例:

Here is a bad image

Here is a good image

有什么建议吗?我提供了以下代码:

#required imports

#using sequential from tensorflow 

from keras.models import Sequential

from keras.layers import Conv2D

from keras.layers import MaxPooling2D

from keras.layers import Flatten

from keras.layers import Dense


#classification model to be sequential
classifier = Sequential()

classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Flatten())
classifier.add(Dense(units = 128, activation = 'relu'))

#output layer
classifier.add(Dense(units = 1, activation = 'sigmoid'))

#compilation
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

#training
from 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("/home/jovyan/dataset/training_set/", target_size = (64, 64), batch_size = 32, class_mode = 'binary')

test_set = test_datagen.flow_from_directory("/home/jovyan/dataset/test_set/", target_size = (64, 64), batch_size = 32, class_mode = 'binary')

classifier.fit_generator(training_set, steps_per_epoch = 85, epochs = 25, validation_data=test_set, validation_steps=2000)

#predictions
import numpy as np
from keras.preprocessing import image
test_image=image.load_img("/home/jovyan/dataset/test_set/test_bad_1.jpg", target_size=(64, 64))
test_image=image.img_to_array(test_image)
test_image=np.expand_dims(test_image, axis=0)
result=classifier.predict(test_image)
training_set.class_indices

if result[0][0]==1:
    prediction='good'
else:
    prediction='bad'

print(prediction)`

【问题讨论】:

  • 欢迎来到 StackOverflow!可以添加带有预期结果描述的图像示例吗?例如。 “imageA” - 预期好,“imageB” - 坏
  • 谢谢!是的
  • 我认为架构是不够的,仅仅使用一个conv层可能无法检测到模型学习能够很好地执行分类所需的复杂特征。

标签: python tensorflow machine-learning keras


【解决方案1】:

test_image 需要重新缩放 1./255,与 train_datagentest_datagen 相同。由于“好”似乎是当有更多的白色区域时,test_image 中的较高值可能会使输出激活饱和,始终为“好”。

【讨论】:

    【解决方案2】:

    我对 Keras 不熟悉,所以我只是假设您的代码没有问题,并尝试在您的问题上给我两分钱。

    首先要检查的是数据集是否平衡。如果数据中有相对过多的“好”图像,该模型将只输出“好”,因为它产生的性能比实际预测的“正常”二元交叉熵损失更好。

    因此,您可以尝试的一件事是为两个标签赋予不同的权重(我们将其称为加权交叉熵损失):惩罚被标记为“好”的错误答案,这样模型现在将继续尝试预测“坏”图像。

    如果这不起作用,您可以尝试other ways 来处理这种不平衡数据集的情况。

    【讨论】:

    • 嗯,我的数据集是均分的,85 个“好”的图像和 85 个“坏”的图像。您是否建议添加更多“坏”图像?
    • 嗯,那就平衡了。您是否检查过训练集的准确率是否达到 ~1.0 过度训练(过度拟合)?
    • 好吧,模型肯定需要更多的图像,因为训练集的准确率仅略高于 50%。话虽如此,我从来没有得到一个“坏”的预测器,所以我认为它一定是别的东西?
    • 我指的是this post中的清单1,“在小数据集上过拟合”。
    猜你喜欢
    • 2022-07-26
    • 1970-01-01
    • 2011-09-15
    • 2016-08-23
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2016-08-11
    相关资源
    最近更新 更多