【问题标题】:Model.Predict is returning same values when using TF Keras ImageDataGenerator使用 TF Keras ImageDataGenerator 时,Model.Predict 返回相同的值
【发布时间】:2020-06-05 06:30:48
【问题描述】:

我正在使用Cat and Dog Dataset 使用Tensorflow Keras 训练模型,并使用ImageDataGenerator.flow_from_directory 读取文件。

训练和验证准确度不错,但在尝试对测试数据进行预测时,模型为所有图像预测相同的类别。

训练代码如下:

import os, shutil
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dropout, Dense
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.losses import binary_crossentropy
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import matplotlib.pyplot as plt

# Path to Training Directory
train_dir = 'Dogs_Vs_Cats_Small/train'

# Path to Validation Directory
validation_dir = 'Dogs_Vs_Cats_Small/validation'

#### Create the Convolutional Base

Max_Pool_Size = (2,2)
model = Sequential([
    Conv2D(input_shape = (150, 150, 3), filters = 32, kernel_size = (3,3), activation = 'relu', 
           padding = 'valid', data_format = 'channels_last'),
    MaxPooling2D(pool_size = Max_Pool_Size),
    Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu', padding = 'valid'),
    MaxPooling2D(pool_size = Max_Pool_Size),
    Conv2D(filters = 128, kernel_size = (3,3), activation = 'relu', padding = 'valid'),
    MaxPooling2D(pool_size = Max_Pool_Size),
    Conv2D(filters = 128, kernel_size = (3,3), activation = 'relu', padding = 'valid'),
    MaxPooling2D(pool_size = Max_Pool_Size)
])


#### Define the Dense Layers on Top of Convolutional Base

model.add(Flatten())
model.add(Dense(units = 512, activation = 'relu'))
model.add(Dense(units = 1, activation = 'sigmoid'))
model.summary()

model.compile(optimizer = RMSprop(learning_rate = 0.001), loss = 'binary_crossentropy', metrics = 'acc')

Train_Gen = ImageDataGenerator(1./255)
Val_Gen = ImageDataGenerator(1./255)

Train_Generator = Train_Gen.flow_from_directory(train_dir, target_size = (150,150), batch_size = 20,
                                               class_mode = 'binary')

Val_Generator = Val_Gen.flow_from_directory(validation_dir, target_size = (150, 150), class_mode = 'binary',
                                           batch_size = 20)

batch_size = 20
target_size = (150,150)
No_Of_Training_Images = Train_Generator.classes.shape[0]
No_Of_Val_Images = Val_Generator.classes.shape[0]
steps_per_epoch = No_Of_Training_Images/batch_size
validation_steps = No_Of_Val_Images/batch_size

history = model.fit(x = Train_Generator, shuffle=True, epochs = 20, 
          steps_per_epoch = steps_per_epoch,
          validation_data = Val_Generator
          , validation_steps = validation_steps
         )

现在,我在PredictTest Data如下图:

Test_Dir = 'Dogs_Vs_Cats_Very_Small/test'

Test_Generator = ImageDataGenerator(1./255).flow_from_directory(Test_Dir, 
           target_size = (150,150), batch_size = 1, 
           shuffle = False, class_mode = 'binary') # This outputs Found 17 images belonging to 2 classes.

No_Of_Samples = len(Test_Generator.filenames)

testPredictions = model.predict(Test_Generator, steps = No_Of_Samples)


predictedClassIndices=np.argmax(testPredictions,axis=1)
print(predictedClassIndices)

filenames = Test_Generator.filenames
for f in range(len(filenames)):
    print(filenames[f],":",predictedClassIndices[f])

上述Print语句的输出,即Predicted Classes如下所示:

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])


cats/cat.1546.jpg : 0
cats/cat.1547.jpg : 0
cats/cat.1548.jpg : 0
cats/cat.1549.jpg : 0
cats/cat.1550.jpg : 0
cats/cat.1566.jpg : 0
cats/cat.1593.jpg : 0
cats/cat.1594.jpg : 0
dogs/dog.1514.jpg : 0
dogs/dog.1520.jpg : 0
dogs/dog.1525.jpg : 0
dogs/dog.1551.jpg : 0
dogs/dog.1555.jpg : 0
dogs/dog.1574.jpg : 0
dogs/dog.1594.jpg : 0
dogs/dog.1597.jpg : 0
dogs/dog.1599.jpg : 0

如上所示,所有图像都预测为Class = 0,即Cats

我已经查看了这个Stack Overflow Question 并且我的数据是平衡的(1000 个猫图像和 1000 个狗图像),因此,根据我的理解,重新平衡我的数据集或调整类权重不适用。我也试过“增加训练时间”。

编辑testPredictions的内容如下所示:

[[1.0473319e-05]
 [9.8473930e-01]
 [2.9069009e-01]
 [5.0639841e-07]
 [1.8511847e-01]
 [6.0166395e-01]
 [4.2568660e-01]
 [4.6028453e-01]
 [7.8800195e-01]
 [8.5675471e-02]
 [8.2654454e-02]
 [7.2898394e-01]
 [1.5504999e-01]
 [8.2106847e-01]
 [8.7003058e-01]
 [9.9999285e-01]
 [5.1210046e-01]]

谁能帮我改正。

提前谢谢大家。

【问题讨论】:

  • 您能否为您的示例提供testPredictions 的内容?
  • @Lukasz Tracewski,当然,谢谢您的回复。我已经用testPredictions 的内容更新了我的问题。

标签: tensorflow tensorflow2.0 tf.keras


【解决方案1】:

这里的问题是在您为testPredictions 结果分配类时的最后一步。 argmax 方法“返回沿轴的最大值的索引”。在您的情况下,它始终是0,因为沿着axis=1,您只有一个元素(索引为0)。

由于您正在进行二元分类并且类是平衡的,因此应用 0.5 概率阈值来分配类是最有意义的:

predictedClassIndices = testPredictions > 0.5

for idx, filename in enumerate(filenames):
    print(filename,":",predictedClassIndices[idx])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 2018-10-31
    • 1970-01-01
    • 2019-12-08
    • 2016-09-02
    • 2018-07-15
    • 2018-09-04
    相关资源
    最近更新 更多