训练和测试准确性对用于图像分类的大数据集没有希望

时间:2018-10-25 02:03:43

标签: python tensorflow keras deep-learning computer-vision

我在训练集中有大约 170K 张图像,在验证集中有 27k 张图像,平均分配给猫和狗。我正在使用下面的代码对猫和狗进行分类。除了下面的架构师,我还尝试了不同的时期/批处理大小/图层组合

img_width,img_height =128,128
if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)
# Initialising the CNN
classifier = Sequential()

# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape=input_shape, activation='relu'))

# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size=(2, 2)))  # 2x2 is optimal

# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))

classifier.add(Conv2D(32, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))

classifier.add(Conv2D(64, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))

# Step 3 - Flattening
classifier.add(Flatten())

# Step 4 - Full connection

classifier.add(Dense(64, activation='relu'))
classifier.add(Dropout(0.4))
classifier.add(Dense(1, activation='sigmoid'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Part 2 - Fitting the CNN to the images
batch_size =128
train_datagen = ImageDataGenerator(rescale=1./255,
                                   shear_range=0.2,
                                   zoom_range=0.2,
                                   horizontal_flip=True,
                                   fill_mode='nearest')

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(training_set_path,
                                                 target_size=input_size,
                                                 batch_size=batch_size,
                                                 class_mode='binary',
                                                 shuffle=True)

test_set = test_datagen.flow_from_directory(test_set_path,
                                            target_size=input_size,
                                            batch_size=batch_size,
                                            class_mode='binary')


classifier.fit_generator(training_set,
                         steps_per_epoch=<170K>/batch_size,
                         epochs=128,
                         validation_data=test_set,
                         validation_steps=<27K>/batch_size,
                         use_multiprocessing=True,
                         workers=12)

输出对我来说似乎没有希望。我需要您的建议/帮助,以提高培训准确性和验证准确性。

Epoch 78: accuracy -> 0.7592, val_accuracy -> 0.6669
Epoch 79: accuracy -> 0.7602, val_accuracy -> 0.6620
Epoch 80: accuracy -> 0.7592, val_accuracy -> 0.6579
Epoch 81: accuracy -> 0.7606, val_accuracy -> 0.6636
Epoch 82: accuracy -> 0.7583, val_accuracy -> 0.6614
Epoch 83: accuracy -> 0.7614, val_accuracy -> 0.6455
Epoch 84: accuracy -> 0.7609, val_accuracy -> 0.6573
Epoch 85: accuracy -> 0.7611, val_accuracy -> 0.6653
Epoch 86: accuracy -> 0.7600, val_accuracy -> 0.6631
Epoch 87: accuracy -> 0.7597, val_accuracy -> 0.6516
Epoch 88: accuracy -> 0.7606, val_accuracy -> 0.6661
Epoch 89: accuracy -> 0.7618, val_accuracy -> 0.6639
Epoch 90: accuracy -> 0.7608, val_accuracy -> 0.6675
Epoch 91: accuracy -> 0.7622, val_accuracy -> 0.6642
Epoch 92: accuracy -> 0.7630, val_accuracy -> 0.6621
Epoch 93: accuracy -> 0.7609, val_accuracy -> 0.6557
Epoch 94: accuracy -> 0.7630, val_accuracy -> 0.6664
Epoch 95: accuracy -> 0.7620, val_accuracy -> 0.6664
Epoch 96: accuracy -> 0.7639, val_accuracy -> 0.6646
Epoch 97: accuracy -> 0.7638, val_accuracy -> 0.6697
Epoch 98: accuracy -> 0.7634, val_accuracy -> 0.6633
Epoch 99: accuracy -> 0.7629, val_accuracy -> 0.6607
Epoch 100: accuracy -> 0.7639, val_accuracy -> 0.6588
Epoch 101: accuracy -> 0.7640, val_accuracy -> 0.6616
Epoch 102: accuracy -> 0.7641, val_accuracy -> 0.6597
Epoch 103: accuracy -> 0.7656, val_accuracy -> 0.6672
Epoch 104: accuracy -> 0.7638, val_accuracy -> 0.6650
Epoch 105: accuracy -> 0.7646, val_accuracy -> 0.6572
Epoch 106: accuracy -> 0.7648, val_accuracy -> 0.6649
Epoch 107: accuracy -> 0.7645, val_accuracy -> 0.6630
Epoch 108: accuracy -> 0.7651, val_accuracy -> 0.6537
Epoch 109: accuracy -> 0.7668, val_accuracy -> 0.6623
Epoch 110: accuracy -> 0.7659, val_accuracy -> 0.6620
Epoch 111: accuracy -> 0.7648, val_accuracy -> 0.6660
Epoch 112: accuracy -> 0.7660, val_accuracy -> 0.6621
Epoch 113: accuracy -> 0.7661, val_accuracy -> 0.6714
Epoch 114: accuracy -> 0.7661, val_accuracy -> 0.6601
Epoch 115: accuracy -> 0.7639, val_accuracy -> 0.6625
Epoch 116: accuracy -> 0.7657, val_accuracy -> 0.6676
Epoch 117: accuracy -> 0.7652, val_accuracy -> 0.6700
Epoch 118: accuracy -> 0.7669, val_accuracy -> 0.6608
Epoch 119: accuracy -> 0.7668, val_accuracy -> 0.6711
Epoch 120: accuracy -> 0.7680, val_accuracy -> 0.6718
Epoch 121: accuracy -> 0.7681, val_accuracy -> 0.6676
Epoch 122: accuracy -> 0.7666, val_accuracy -> 0.6565
Epoch 123: accuracy -> 0.7673, val_accuracy -> 0.6527
Epoch 124: accuracy -> 0.7670, val_accuracy -> 0.6548
Epoch 125: accuracy -> 0.7666, val_accuracy -> 0.6602
Epoch 126: accuracy -> 0.7671, val_accuracy -> 0.6696
Epoch 127: accuracy -> 0.7680, val_accuracy -> 0.6628

请尝试您的宝贵建议/指导或摘要。

2 个答案:

答案 0 :(得分:1)

对于第一,第二和第三卷积层,您具有相同的过滤器编号。根据我的经验,最好使用卷积层来增加过滤器数量,例如32-> 64-> 128等。然后,您的模态可以获得更多功能。

答案 1 :(得分:1)

@NormanZhu给了您一个很好的提示。但是您可以尝试其他方法。就像添加BatchNormalization层并使用LeakyReLU而不是ReLU一样:

x = Conv1D(filters=64, kernel_size=1)(inputs)
x = BatchNormalization()(x)
x = LeakyReLU(x)

之后,您可以使用MaxPooling,这可能是代码的基础。堆叠其中一些,然后添加Dense层,您应该会很好。甚至可以尝试降低辍学率。

也许还可以尝试以某种方式对图像进行预处理,也许可以提高准确性。