基础_cifar10_model

今天进一步在cifar10数据集上解决几个问题:
1、比较一下序贯和model,为什么要分成两块;
2、同样的条件下,我去比较一下序贯和model。这个例子作为今天的晚间运行。

1、比较一下序贯和model,为什么要分成两块;
我认为比较 能够说明问题 的是前期那个验证码的model
input_tensor = Input((height, width, 3))
x = input_tensor
for i in range(4):
    x = Convolution2D(32*2**i, 3, 3, activation='relu')(x)
    x = Convolution2D(32*2**i, 3, 3, activation='relu')(x)
    x = MaxPooling2D((2, 2))(x)

x = Flatten()(x)
x = Dropout(0.25)(x)
x = [Dense(n_class, activation='softmax', name='c%d'%(i+1))(x) for i in range(4)]
model = Model(input=input_tensor, output=x)

一个非常显著的不同,就是最后分为了4个部分(这也是这里选择model的原因),这个 4分类直接最后导致了验证码4个部分的区分。这是验证码识别最为有技术的地方。应该说,从网络结构上来看,这个分叉,在cnn网络 中,就是model和sequence最大的不同之处,也是着重要研究的地方。

model  =  Model( input = input_tensor, output = x)

model最大的不同就在于它首先定义了网络结构,然后最后才生成这个模型。从编码方式上来看,只是顺序的不同而已。

=  [Dense(n_class, activation = 'softmax' , name = 'c%d' % (i + 1 ))(x)  for  i  in   range ( 4 )]

这句之所以能够将网络1分4,是因为采用的列表的形式。然后就能够进行验证?我将这个问题留给明天。另一个方面,在这个验证码的例子中,其网络就是不断地卷积-卷积-maxpooling,非常简单,是否需要进一步优化?

2、同样的条件下,我去比较一下序贯和model
'''
同样的数据集和epochs,同样的模型构造。
一个采用序贯、一个采用model方法
对生成结果进行比较
'''

from __future__ import print_function
#!apt-get -qq install -y graphviz && pip install -q pydot
import pydot
import keras
import cv2
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Input
from keras.models import Model
from keras.utils.vis_utils import plot_model
import matplotlib.image as image # image 用于读取图片
import matplotlib.pyplot as plt
import os


batch_size = 32
num_classes = 10
#epochs = 100
epochs = 3
data_augmentation = True
num_predictions = 20
save_dir = os.path.join(os.getcwd(), 'saved_models')
model_name = 'keras_cifar10_trained_model.h5'

# The data, split between train and test sets:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print( 'x_train shape:', x_train.shape)
print(x_train.shape[ 0], 'train samples')
print(x_test.shape[ 0], 'test samples')

# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

#序贯模型
model = Sequential()
model.add(Conv2D( 32, ( 3, 3), padding= 'same',
input_shape=x_train.shape[ 1:]))
model.add(Activation( 'relu'))
model.add(Conv2D( 32, ( 3, 3)))
model.add(Activation( 'relu'))
model.add(MaxPooling2D( pool_size=( 2, 2)))
model.add(Dropout( 0.25))

model.add(Conv2D( 64, ( 3, 3), padding= 'same'))
model.add(Activation( 'relu'))
model.add(Conv2D( 64, ( 3, 3)))
model.add(Activation( 'relu'))
model.add(MaxPooling2D( pool_size=( 2, 2)))
model.add(Dropout( 0.25))

model.add(Flatten())
model.add(Dense( 512))
model.add(Activation( 'relu'))
model.add(Dropout( 0.5))
model.add(Dense(num_classes))
model.add(Activation( 'softmax'))

#model模型
inputs = Input( shape=(x_train.shape[ 1:]))
x = Conv2D( 32, ( 3, 3), activation= 'relu')(inputs)
x = Conv2D( 32, ( 3, 3), activation= 'relu')(x)
x = MaxPooling2D(( 2, 2))(x)
x = Dropout( 0.25)(x)
x = Conv2D( 64, ( 3, 3), activation= 'relu')(x)
x = Conv2D( 64, ( 3, 3), activation= 'relu')(x)
x = MaxPooling2D(( 2, 2))(x)
x = Dropout( 0.25)(x)
x = Flatten()(x)
x = Dense( 512)(x)
x = Activation( 'relu')(x)
x = Dropout( 0.5)(x)
x = Dense(num_classes)(x)
x = Activation( 'softmax')(x)
model2 = Model( inputs=inputs, outputs=x)

#显示模型
model.summary()
plot_model(model, to_file= 'model_sequence.png', show_shapes= True)
img = image.imread( 'model_sequence.png')
print(img.shape)
plt.imshow(img) # 显示图片
plt.axis( 'off') # 不显示坐标轴
plt.show()

model2.summary()
plot_model(model2, to_file= 'model_model.png', show_shapes= True)
img2 = image.imread( 'model_model.png')
print(img2.shape)
plt.imshow(img2) # 显示图片
plt.axis( 'off') # 不显示坐标轴
plt.show()
# initiate RMSprop optimizer
opt = keras.optimizers.rmsprop( lr= 0.0001, decay= 1e-6)

# Let's train the model using RMSprop
model.compile( loss= 'categorical_crossentropy',
optimizer=opt,
metrics=[ 'accuracy'])

model2.compile( loss= 'categorical_crossentropy',
optimizer=opt,
metrics=[ 'accuracy'])

x_train = x_train.astype( 'float32')
x_test = x_test.astype( 'float32')
x_train /= 255
x_test /= 255

if not data_augmentation:
print( 'Not using data augmentation.')
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(x_test, y_test),
shuffle= True)
else:
print( 'Using real-time data augmentation.')
# This will do preprocessing and realtime data augmentation:
datagen = ImageDataGenerator(
featurewise_center= False, # set input mean to 0 over the dataset
samplewise_center= False, # set each sample mean to 0
featurewise_std_normalization= False, # divide inputs by std of the dataset
samplewise_std_normalization= False, # divide each input by its std
zca_whitening= False, # apply ZCA whitening
rotation_range= 0, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range= 0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range= 0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip= True, # randomly flip images
vertical_flip= False) # randomly flip images

# Compute quantities required for feature-wise normalization
# (std, mean, and principal components if ZCA whitening is applied).
datagen.fit(x_train)

# Fit the model on the batches generated by datagen.flow().
model.fit_generator(datagen.flow(x_train, y_train,
batch_size=batch_size),
epochs=epochs,
validation_data=(x_test, y_test),
workers= 4)
model2.fit_generator(datagen.flow(x_train, y_train,
batch_size=batch_size),
epochs=epochs,
validation_data=(x_test, y_test),
workers= 4)

# Save model and weights
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
model_path = os.path.join(save_dir, model_name)
model.save(model_path)
print( 'Saved trained model at %s ' % model_path)

# Score trained model.
scores = model.evaluate(x_test, y_test, verbose= 1)
print( 'Test loss:', scores[ 0])
print( 'Test accuracy:', scores[ 1])

模型打印出来比较:
508489-20180418190103064-141102439.png
sequence
主要是由于在
x = Conv2D( 32 , ( 3 , 3 ), activation = 'relu' )(inputs)
似乎是将activation合并了,所以model_model更短一些。但是层的结果似乎也是不一样的。这个等到训练结果出来后进一步研究。
Using TensorFlow backend.
x_train shape: (50000, 32, 32, 3)
50000 train samples
10000 test samples
_________________________________________________________________
Layer (type)                 Output Shape              Param #  
=================================================================
conv2d_1 (Conv2D)            (None, 32, 32, 32)        896      
_________________________________________________________________
activation_1 (Activation)    (None, 32, 32, 32)        0        
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 30, 30, 32)        9248     
_________________________________________________________________
activation_2 (Activation)    (None, 30, 30, 32)        0        
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 15, 15, 32)        0        
_________________________________________________________________
dropout_1 (Dropout)          (None, 15, 15, 32)        0        
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 15, 15, 64)        18496    
_________________________________________________________________
activation_3 (Activation)    (None, 15, 15, 64)        0        
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 13, 13, 64)        36928    
_________________________________________________________________
activation_4 (Activation)    (None, 13, 13, 64)        0        
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 6, 6, 64)          0        
_________________________________________________________________
dropout_2 (Dropout)          (None, 6, 6, 64)          0        
_________________________________________________________________
flatten_1 (Flatten)          (None, 2304)              0        
_________________________________________________________________
dense_1 (Dense)              (None, 512)               1180160  
_________________________________________________________________
activation_5 (Activation)    (None, 512)               0        
_________________________________________________________________
dropout_3 (Dropout)          (None, 512)               0        
_________________________________________________________________
dense_2 (Dense)              (None, 10)                5130     
_________________________________________________________________
activation_6 (Activation)    (None, 10)                0        
=================================================================
Total params: 1,250,858
Trainable params: 1,250,858
Non-trainable params: 0
_________________________________________________________________
(2065, 635, 4)
_________________________________________________________________
Layer (type)                 Output Shape              Param #  
=================================================================
input_1 (InputLayer)         (None, 32, 32, 3)         0        
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 30, 30, 32)        896      
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 28, 28, 32)        9248     
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 14, 14, 32)        0        
_________________________________________________________________
dropout_4 (Dropout)          (None, 14, 14, 32)        0        
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 12, 12, 64)        18496    
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 10, 10, 64)        36928    
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 5, 5, 64)          0        
_________________________________________________________________
dropout_5 (Dropout)          (None, 5, 5, 64)          0        
_________________________________________________________________
flatten_2 (Flatten)          (None, 1600)              0        
_________________________________________________________________
dense_3 (Dense)              (None, 512)               819712   
_________________________________________________________________
activation_7 (Activation)    (None, 512)               0        
_________________________________________________________________
dropout_6 (Dropout)          (None, 512)               0        
_________________________________________________________________
dense_4 (Dense)              (None, 10)                5130     
_________________________________________________________________
activation_8 (Activation)    (None, 10)                0        
=================================================================
Total params: 890,410
Trainable params: 890,410
Non-trainable params: 0
_________________________________________________________________
(1623, 635, 4)
从最后的结果上来看,model消耗的时间更少,而且正确率更高、loss更低。那么以后我优先建立model模型!
10000/10000 [==============================] - 2s 187us/step
Test loss: 0.9320432889938355
Test accuracy: 0.682
10000/10000 [==============================] - 2s 173us/step
Test loss2: 0.8202090420722962
Test accuracy2: 0.7091






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值