卷积神经网络中的图像预处理在Keras与Tflearn中产生较低的精度

我正在尝试将此tflearn DCNN示例(使用图像预处理和augmemtation)转换为keras:


Tflearn示例:


import tflearn

from tflearn.data_utils import shuffle, to_categorical

from tflearn.layers.core import input_data, dropout, fully_connected

from tflearn.layers.conv import conv_2d, max_pool_2d

from tflearn.layers.estimator import regression

from tflearn.data_preprocessing import ImagePreprocessing

from tflearn.data_augmentation import ImageAugmentation


# Data loading and preprocessing

from tflearn.datasets import cifar10

(X, Y), (X_test, Y_test) = cifar10.load_data()

X, Y = shuffle(X, Y)

Y = to_categorical(Y, 10)

Y_test = to_categorical(Y_test, 10)


# Real-time data preprocessing

img_prep = ImagePreprocessing()

img_prep.add_featurewise_zero_center()

img_prep.add_featurewise_stdnorm()


# Real-time data augmentation

img_aug = ImageAugmentation()

img_aug.add_random_flip_leftright()

img_aug.add_random_rotation(max_angle=25.)


# Convolutional network building

network = input_data(shape=[None, 32, 32, 3],

                     data_preprocessing=img_prep,

                     data_augmentation=img_aug)

network = conv_2d(network, 32, 3, activation='relu')

network = max_pool_2d(network, 2)

network = conv_2d(network, 64, 3, activation='relu')

network = conv_2d(network, 64, 3, activation='relu')

network = max_pool_2d(network, 2)

network = fully_connected(network, 512, activation='relu')

network = dropout(network, 0.5)


50个纪元后产生了以下结果:


Training Step: 26050  | total loss: 0.35260 | time: 144.306s

| Adam | epoch: 050 | loss: 0.35260 - acc: 0.8785 | val_loss: 0.64622 - val_acc: 0.8212 -- iter: 50000/50000

然后,我尝试使用相同的DCNN图层,参数和图像预处理/增强功能将其转换为Keras

这会产生差得多的验证准确性结果:


Epoch 50/50

521/521 [==============================] - 84s 162ms/step - loss: 0.4723 - acc: 0.8340 - val_loss: 3.2970 - val_acc: 0.2729

Test score: 3.2969648239135743

Accuracy: 27.29%

谁能帮我理解原因?我在Keras中是否误用了/误解了图像预处理/增强功能?


白衣非少年
浏览 264回答 1
1回答

桃花长相依

在Keras模型中,您也忘记了标准化验证数据。您可以通过使用训练数据datagen.mean并对其进行datagen.std计算来做到这一点:# normalize test data; add a small constant to avoid division by zero,# you can alternatively use `keras.backend.epsilon()`X_test = (X_test - datagen.mean) / (datagen.std + 1e-8) 或者您可以使用该datagen.standardize()方法对测试数据进行规范化:X_test = datagen.standardize(X_test)有关更多信息,请参见SO上的这个问题:预测期间,数据标准化如何在keras中起作用?别忘了您应该通过对训练数据计算出的统计数据对测试数据进行归一化。永远不要通过自己的统计数据对测试数据进行归一化。警告:似乎也standardize 改变了它的参数...是的,您可以在源代码中确认这一点。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python