使用带有Tensorflow的Keras作为后端,使用来自keras的vgg16.py来训练cifar10

时间:2017-02-15 07:41:28

标签: python tensorflow keras

我使用了keras提供的pre-trained model of vgg16vgg16.py
在vgg16.py中,我将最小输入大小从48更改为32,默认值从225更改为32.cifar10的维度为(nb_samples,3,32,32)。

以下是代码:

from keras.datasets import cifar10
from keras.utils import *
from keras.optimizers import SGD
nb_classes = 10
(X_train, Y_train), (X_test, Y_test) = cifar10.load_data()
print ("Train shape", X_train.shape, Y_train.shape)
print ("Train samples", X_train.shape[0])
print ("Test samples", X_test.shape[0])
Y_train = np_utils.to_categorical(Y_train, nb_classes)
Y_test = np_utils.to_categorical(Y_test, nb_classes)
print ("Train shape", X_train.shape, Y_train.shape)

from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
from keras.models import Model
import numpy as np

base_model = VGG16(weights=None, include_top=True, input_shape=X_train.shape[1:], classes=10)
base_model.compile(optimizer=SGD(lr=0.005, decay=1e-6, momentum=0.9, nesterov=True), loss='categorical_crossentropy', metrics=['accuracy'])   
base_model.fit(X_train, Y_train, nb_epoch=10, batch_size=256, verbose=1)
base_model.evaluate(X_test, Y_test, batch_size=256, verbose=1)
#The commented code gives validation accuracy but above code does not.
#base_model.fit(X_train, Y_train,batch_size=256,nb_epoch=10,validation_data=(X_test, Y_test),shuffle=True)

以上代码有效但权重随机初始化。结果如下:

Using TensorFlow backend.
('http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz', 'cifar-10-batches-py')
('Train shape', (50000, 32, 32, 3), (50000, 1))
('Train samples', 50000)
('Test samples', 10000)
('Train shape', (50000, 32, 32, 3), (50000, 10))
Train on 10000 samples, validate on 5000 samples
Epoch 1/10
50000/50000 [==============================] - 2641s - loss: 2.3138 - acc: 0.111
Epoch 2/10
50000/50000 [==============================] - 2643s - loss: 2.3027 - acc: 0.0974
Epoch 3/10
50000/50000 [==============================] - 2642 - loss: 2.3027 - acc: 0.0987
Epoch 4/10
50000/50000 [==============================] - 2643s - loss: 2.3027 - acc: 0.0986
Epoch 5/10
50000/50000 [==============================] - 2728s - loss: 2.3027 - acc: 0.0966 
Epoch 6/10
50000/50000 [==============================] - 2736s - loss: 2.3027 - acc: 0.0983
Epoch 7/10
50000/50000 [==============================] - 2681s - loss: 2.3027 - acc: 0.0971
Epoch 8/10
50000/50000 [==============================] - 2707s - loss: 2.3027 - acc: 0.0970
Epoch 9/10
50000/50000 [==============================] - 2609s - loss: 2.3027 - acc: 0.0955
Epoch 10/10
50000/50000 [==============================] - 2649s - loss: 2.3027 - acc: 0.0997

此培训似乎saturated with loss=2.3027 keras的cifar10_cnn.py代码使用实时数据增加和速度reduces the speed of above 2000s to 351s of the code.任何原因以及后期训练集的准确率上升到80%但在上述情况下,它恒定为9%?< / p>

2 个答案:

答案 0 :(得分:0)

我在尝试不同配置后发现,VGG16 architecture is too big表示大小为32x32的图片。我尝试使用VGG16直到block3_pool,然后添加了dense 512完全连接,然后是{{1对于softmax classifier。以下是修改后的代码:

10 classes

我在问题模型中发现的缺点是:(i)将批量大小从256减少到32 (ii)使用平均值对数据进行标准化 (iii)应使用base_model = VGG16(weights=None, include_top=False, input_shape=X_train.shape[1:], classes=10) x = base_model.get_layer('block3_pool').output x = Flatten(name='Flatten')(x) x = Dense(512, activation='relu', name='fc1')(x) predictions = Dense(nb_classes, activation='softmax')(x) model = Model(input=base_model.input, output=predictions) keras函数使用数据扩充 (iv)较小的架构VGG16(16层)到data_gen(3层) (v)使用上述架构将时间从2000年缩短至900秒。

block3_pool达到accuracy左右。可以75%吗? 在对大小为10000的测试示例评估模型后,得到以下结果:
further reduce time on CPU
这是['loss', 'acc'] : [2.3033507381439211, 0.10000000000000001]

答案 1 :(得分:0)

我找到了this github project。他们在cifar10上使用vgg16和keras。它们的验证准确度超过0.97。

更确切地说:

  • 他们使用vgg16直到图层'block3_pool'
  • 比添加一些完全连接的图层

完全连接层的代码:

x = Flatten()(last)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
pred = Dense(10, activation='sigmoid')(x)