为什么即使两个图像都是在相同的环境下拍摄的,我的训练准确度(98%)与验证准确度(32%)相差如此大?

时间:2019-07-04 13:33:48

标签: python tensorflow keras conv-neural-network

我正在构建一个图像分类器,根据白色塑料椅子的堆叠数量,可以将其分类为1到12(一打)类。

A photo of a 1 chair stack belonging to the 1 chair class

A photo of a stack of 12 chairs belonging to the 12 chair class

我在验证数据集中有4972张图像(每班平均350张图像),在训练数据集中有7342张图像(每班平均550张图像)。

我想知道为什么我的模型在精度上有如此大的差异。 而且,每个类别550张图像足以训练图像分类器吗?

我从头开始构建模型,也使用过转移学习(vgg16),尽管测试和验证图像是在同一环境中拍摄的,但我的验证准确性仍低于40%。

# linking my links...
data_zip = 'C:/Users/1/Desktop/data_sets'
train_dir = 'C:/Users/1/Desktop/data_sets/train/'
test_dir = 'C:/Users/1/Desktop/data_sets/test'
valid_dir = 'C:/Users/1/Desktop/data_sets/valid/New folder'
print(train_dir)
print(test_dir)
print(data_zip)


# checking connections...
train_names = os.path.exists(train_dir)
test_names = os.path.exists(test_dir)
data_path = os.path.exists(data_zip)
print(train_names)
print(test_names)
print(data_path)


print(train_dir[0:])
print(test_dir[0:])

training_list = os.listdir(train_dir)
test_list = os.listdir(test_dir)

# how many classes?
print("Training: " + str(len(training_list)) + " Classes ")
print("Testing: " + str(len(test_list)) + " Classes ")

# Introducing labels
labels = training_list[0:]
labels_uno = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]


def my_CNN(input_shape=(224,224,3), num_classes=12):

    model = tf.keras.Sequential()

    model.add(tf.keras.layers.Conv2D(5, kernel_size=(3, 3), 
                                    padding='same',
                                    name='image_array', 
                                    input_shape=input_shape,
                                    activation='relu'))

    model.add(tf.keras.layers.BatchNormalization())

    model.add(tf.keras.layers.Conv2D(10, kernel_size=(3, 3), 
                                 padding='same',
                                 strides=(2, 2),
                                 activation='relu'))

    model.add(tf.keras.layers.BatchNormalization())
    model.add(tf.keras.layers.Dropout(.25))
    model.add(tf.keras.layers.Conv2D(16, kernel_size=(3, 3),padding='same', strides=(2, 2), activation='relu'))

    model.add(tf.keras.layers.BatchNormalization())

    model.add(tf.keras.layers.Conv2D(16, kernel_size=(3, 3),padding='same',strides=(2, 2), activation='relu'))

    model.add(tf.keras.layers.BatchNormalization())

    model.add(tf.keras.layers.Dropout(.25))

    model.add(tf.keras.layers.Conv2D(32, kernel_size=(3, 3), padding='same',strides=(2, 2), activation='relu'))

    model.add(tf.keras.layers.BatchNormalization())

    model.add(tf.keras.layers.Conv2D(64, kernel_size=(3, 3),padding='same',strides=(2, 2), activation='relu'))

    model.add(tf.keras.layers.BatchNormalization())
    model.add(tf.keras.layers.Convolution2D(filters=num_classes, 
                                        kernel_size=(3, 3),
                                        strides=(2, 2),
                                        padding='same'))

    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(63, activation='relu')),
    model.add(tf.keras.layers.Dense(12,activation='softmax',name='predictions'))

    return model


train_datagen = ImageDataGenerator(rescale=1/255, featurewise_center=False,
                               featurewise_std_normalization=False,
                               rotation_range=5,
                               width_shift_range=0.1,
                               height_shift_range=0.1,
                               zoom_range=.1
                               )

train_generator = train_datagen.flow_from_directory(train_dir,
                                                batch_size=50,
                                                class_mode='categorical',
                                                target_size=(224, 224),
                                                color_mode='rgb',
                                                shuffle=True
                                                )

validation_datagen = ImageDataGenerator(rescale=1./255,
                                    featurewise_center=False,
                                    featurewise_std_normalization=False,
                                    rotation_range=5,
                                    width_shift_range=0.1,
                                    height_shift_range=0.1,
                                    zoom_range=.1)

validation_generator = validation_datagen.flow_from_directory(test_dir,batch_size=40,class_mode='categorical',target_size=(224, 224),
                                            color_mode='rgb',shuffle=True)


model = my_CNN()
model.compile(optimizer=tf.train.AdamOptimizer(learning_rate=0.005),
          loss='categorical_crossentropy',
          metrics=['accuracy'])
model.summary()

history = model.fit_generator(train_generator,
                          epochs=9,
                          verbose=1,
                          validation_data=validation_generator,
                          validation_steps=int(3000/40)
                          )


train_labels = to_categorical(labels_uno, num_classes=12, dtype='int')
print(train_labels)

# printing results
test_image = image.load_img(valid_dir + '/14test.jpg', target_size=(224, 224), color_mode='rgb')

plt.imshow(test_image)
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
result = model.predict(test_image)
print(result)
print(result[0])
print(result.argmax())

test_image2 = image.load_img(valid_dir + '/13test.jpg', target_size=(224, 224), color_mode='rgb')

plt.imshow(test_image2)
test_image2 = image.img_to_array(test_image2)
test_image2 = np.expand_dims(test_image2, axis=0)
result2 = model.predict(test_image2)
print(result2)
print(result2[0])
print(result2.argmax())

test_image3 = image.load_img(valid_dir + '/1test.jpg', target_size=(224, 224), color_mode='rgb')
plt.imshow(test_image3)
test_image3 = image.img_to_array(test_image3)
test_image3 = np.expand_dims(test_image3, axis=0)
result3 = model.predict(test_image3)
print(result3)

print(result3[0])
print(result3.argmax())

preds = np.round_(result.reshape(12, 1))
preds2 = np.round_(result2.reshape(12, 1))
preds3 = np.round_(result3.reshape(12, 1))
print('rounded test_labels:', preds)
print('rounded test_labels2:', preds2)
print('rounded test_labels3:', preds3)
print('preds_shape:', preds.shape)
print('preds2_shape:', preds2.shape)
print('preds3_shape:', preds3.shape)

classification_metrics = metrics.classification_report(labels_uno, preds, target_names=labels)
classification_metrics2 = metrics.classification_report(labels_uno, preds2, target_names=labels)
classification_metrics3 = metrics.classification_report(labels_uno, preds3, target_names=labels)
print(classification_metrics)
print(classification_metrics2)
print(classification_metrics3)

#These are the last outputs of the 10 Epoches
147/147 [==============================] - 1725s 12s/step - loss: 3.0420 - acc: 0.5726 - val_loss: 3.7351 - val_acc: 0.2750
147/147 [==============================] - 1731s 12s/step - loss: 0.6688 - acc: 0.8062 - val_loss: 4.7904 - val_acc: 0.2440
147/147 [==============================] - 1793s 12s/step - loss: 0.6961 - acc: 0.8180 - val_loss: 5.7115 - val_acc: 0.2910
147/147 [==============================] - 1793s 12s/step - loss: 0.5203 - acc: 0.8680 - val_loss: 6.8262 - val_acc: 0.2337
147/147 [==============================] - 1750s 12s/step - loss: 0.5274 - acc: 0.8697 - val_loss: 7.1189 - val_acc: 0.2803
147/147 [==============================] - 2085s 14s/step - loss: 0.7673 - acc: 0.8511 - val_loss: 7.5080 - val_acc: 0.2700
147/147 [==============================] - 1721s 12s/step - loss: 0.4953 - acc: 0.8936 - val_loss: 6.9290 - val_acc: 0.3057
147/147 [==============================] - 1981s 13s/step - loss: 0.5091 - acc: 0.8912 - val_loss: 7.7346 - val_acc: 0.2787
147/147 [==============================] - 1730s 12s/step - loss: 0.4535 - acc: 0.9042 - val_loss: 8.1851 - val_acc: 0.2883
147/147 [==============================] - 1692s 12s/step - loss: 0.4414 - acc: 0.9100 - val_loss: 10.0667 - val_acc: 0.2497

0 个答案:

没有答案