【问题标题】:CNN accuracy on binary classification of cat/dog images no better than randomCNN 对猫/狗图像二元分类的准确率不比随机数好
【发布时间】:2019-11-20 06:32:58
【问题描述】:

我根据Analytics Vidhya 上的教程改编了一个简单的 CNN。

问题是我在保持集上的准确性并不比随机好。我正在对大约 8600 张猫和狗的图像进行训练,这对于体面的模型来说应该是足够的数据,但测试集的准确率是 49%。我的代码中是否有明显的遗漏?

import os
import numpy as np
import keras
from keras.models import Sequential
from sklearn.model_selection import train_test_split
from datetime import datetime
from PIL import Image
from keras.utils.np_utils import to_categorical
from sklearn.utils import shuffle


def main():

    cat=os.listdir("train/cats")
    dog=os.listdir("train/dogs")
    filepath="train/cats/"
    filepath2="train/dogs/"

    print("[INFO] Loading images of cats and dogs each...", datetime.now().time())
    #print("[INFO] Loading {} images of cats and dogs each...".format(num_images), datetime.now().time())
    images=[]
    label = []
    for i in cat:
        image = Image.open(filepath+i)
        image_resized = image.resize((300,300))
        images.append(image_resized)
        label.append(0) #for cat images

    for i in dog:
        image = Image.open(filepath2+i)
        image_resized = image.resize((300,300))
        images.append(image_resized)
        label.append(1) #for dog images

    images_full = np.array([np.array(x) for x in images])

    label = np.array(label)
    label = to_categorical(label)

    images_full, label = shuffle(images_full, label)

    print("[INFO] Splitting into train and test", datetime.now().time())
    (trainX, testX, trainY, testY) = train_test_split(images_full, label, test_size=0.25)


    filters = 10
    filtersize = (5, 5)

    epochs = 5
    batchsize = 32

    input_shape=(300,300,3)
    #input_shape = (30, 30, 3)

    print("[INFO] Designing model architecture...", datetime.now().time())
    model = Sequential()
    model.add(keras.layers.InputLayer(input_shape=input_shape))
    model.add(keras.layers.convolutional.Conv2D(filters, filtersize, strides=(1, 1), padding='same',
                                                data_format="channels_last", activation='relu'))
    model.add(keras.layers.MaxPooling2D(pool_size=(2, 2)))
    model.add(keras.layers.Flatten())

    model.add(keras.layers.Dense(units=2, input_dim=50,activation='softmax'))
    #model.add(keras.layers.Dense(units=2, input_dim=5, activation='softmax'))

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

    print("[INFO] Fitting model...", datetime.now().time())
    model.fit(trainX, trainY, epochs=epochs, batch_size=batchsize, validation_split=0.3)

    model.summary()

    print("[INFO] Evaluating on test set...", datetime.now().time())
    eval_res = model.evaluate(testX, testY)
    print(eval_res)

if __name__== "__main__":
    main()

【问题讨论】:

  • 是二分类还是多分类?
  • 我确实看到您没有对数据进行洗牌。由于您首先为猫加载数据,然后为狗加载数据,因此很可能在训练期间,网络最初学习的是 Cats 的表示,然后是 Dogs 的表示。但是在学习 Dogs 的表示时,网络得到了优化,因此没有 Cats。
  • 你是对的。我打乱了数据并重新运行。但准确度还是一样的。相应地更新了上面的原始代码
  • 你只做了 5 个 epochs...如果这个数字是 500 会发生什么?通常,您会一直运行直到您的错误率达到可接受的水平或 epoch 数很大。你不能指望它只经过 5 次迭代就能学到任何东西。
  • epoch 数一定不是这里的问题。即 5 次通过约 16K 图像,每 32 图像调整权重。这对于网络来说应该是足够的数据了。

标签: python machine-learning keras conv-neural-network


【解决方案1】:

对我来说,问题在于您的网络规模,您只有一个过滤器大小为 10 的 Conv2D。这对于学习图像的深度表示来说太小了。

尝试通过使用 VGGnet 等常见架构块来大幅增加这一点!
区块示例:

x = Conv2D(32, (3, 3) , padding='SAME')(model_input)
x = LeakyReLU(alpha=0.3)(x)
x = BatchNormalization()(x)
x = Conv2D(32, (3, 3) , padding='SAME')(x)
x = LeakyReLU(alpha=0.3)(x)
x = BatchNormalization()(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)

您需要尝试多个这样的块,并增加过滤器大小以捕获更深层次的特征。

另外,你不需要指定密集层的 input_dim,keras 会自动处理!

最后但同样重要的是,您需要完全连接的网络才能正确分类您的图像,而不仅仅是单层。

例如:

x = Flatten()(x)
x = Dense(256)(x)
x = LeakyReLU(alpha=0.3)(x)
x = Dense(128)(x)
x = LeakyReLU(alpha=0.3)(x)
x = Dense(2)(x)
x = Activation('softmax')(x)

尝试这些更改并与我保持联系!

在操作问题后更新

图像很复杂,它们包含很多信息,例如形状、边缘、颜色等

为了捕获最大量的信息,您需要通过多个卷积来学习图像的不同方面。 想象一下,例如第一个卷积将学习识别正方形,第二个卷积将学习识别圆形,第三个卷积识别边缘,等等。

对于我的第二点,最终的全连接就像一个分类器,conv 网络将输出一个“代表”狗或猫的向量,现在你需要了解这种向量是一个类还是另一个。
而在最后一层直接输入该向量并不足以学习这种表示。

这样更清楚吗?

op 的第二条评论的最后更新

这里有两种定义 Keras 模型的方法,都输出相同的东西!

model_input = Input(shape=(200, 1))
x = Dense(32)(model_input)
x = Dense(16)(x)
x = Activation('relu')(x)
model = Model(inputs=model_input, outputs=x)




model = Sequential()
model.add(Dense(32, input_shape=(200, 1)))
model.add(Dense(16, activation = 'relu'))

架构示例

model = Sequential()
model.add(keras.layers.InputLayer(input_shape=input_shape))
model.add(keras.layers.convolutional.Conv2D(32, (3,3), strides=(2, 2), padding='same', activation='relu'))
model.add(keras.layers.convolutional.Conv2D(32, (3,3), strides=(2, 2), padding='same', activation='relu'))
model.add(keras.layers.MaxPooling2D(pool_size=(2, 2)))

model.add(keras.layers.convolutional.Conv2D(64, (3,3), strides=(2, 2), padding='same', activation='relu'))
model.add(keras.layers.convolutional.Conv2D(64, (3,3), strides=(2, 2), padding='same', activation='relu'))
model.add(keras.layers.MaxPooling2D(pool_size=(2, 2)))

model.add(keras.layers.Flatten())

model.add(keras.layers.Dense(128, activation='relu'))
model.add(keras.layers.Dense(2, activation='softmax'))

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

不要忘记在输入网络之前对数据进行规范化。

对您的数据使用简单的images_full = images_full / 255.0 可以大大提高您的准确性。
也可以尝试使用灰度图像,它的计算效率更高。

【讨论】:

  • 我明白你的第一点。然而,我非常怀疑即使是 1 个带有 10 个过滤器的 conv2d 层也不比随机猜测更好。记住。该模型看到 16K 的猫/狗 5 次。似乎有足够的数据来推断至少一些模式。我的预感是某些东西被错误地指定了。我不愿意在解决这个问题之前实施你的建议,因为我在我的机器上本地运行它。关于您的第二点,为什么在具有 2 个节点的最后一层之前需要额外增加 2 个密集层?
  • 我更新了答案,因为评论太长了!
  • 谢谢。为什么行尾有 x?这是另一种指定附加层的方法吗?我只熟悉model.add语法
  • 是的,您可以使用 model.add 和我定义的方式指定 keras 层。我会用一个天真的例子来更新我的答案来向你展示!
  • 根据您的建议修改了脚本(开始时仅添加了 1 个卷积层和 2 个密集层)。测试集的准确率仍为 50%。我不知道发生了什么。
猜你喜欢
  • 2020-05-07
  • 1970-01-01
  • 2017-12-05
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 2012-04-18
  • 1970-01-01
相关资源
最近更新 更多