1. 云栖社区>
2. 翻译小组>
3. 博客>
4. 正文

## 深度剖析卷积神经网络

【方向】 2018-05-21 19:52:49 浏览16172 评论0

Pooling

Max Pooling近年来效果最好，它以某个地区的最大像素代表该地区最重要的特征为理论基础。通常我们希望分类的物体图像可能包含许多其他物体，例如，出现在汽车图像某处的猫可能会误导分类器，Pooling有助于缓解这种影响。

Dropout

Batch Normalization

``````def Unit(x,filters):
out = BatchNormalization()(x)
out = Activation("relu")(out)
out = Conv2D(filters=filters, kernel_size=[3, 3], strides=[1, 1], padding="same")(out)
return out
``````

``````def MiniModel(input_shape):
images = Input(input_shape)
net = Unit(images,64)
net = Unit(net,64)
net = Unit(net,64)
net = MaxPooling2D(pool_size=(2,2))(net)
net = Unit(net,128)
net = Unit(net,128)
net = Unit(net,128)
net = MaxPooling2D(pool_size=(2, 2))(net)
net = Unit(net,256)
net = Unit(net,256)
net = Unit(net,256)
net = Dropout(0.5)(net)
net = AveragePooling2D(pool_size=(8,8))(net)
net = Flatten()(net)
net = Dense(units=10,activation="softmax")(net)
model = Model(inputs=images,outputs=net)
return model
``````

``````#load the cifar10 dataset
(train_x, train_y) , (test_x, test_y) = cifar10.load_data()
#normalize the data
train_x = train_x.astype('float32') / 255
test_x = test_x.astype('float32') / 255
#Subtract the mean image from both train and test set
train_x = train_x - train_x.mean()
test_x = test_x - test_x.mean()
#Divide by the standard deviation
train_x = train_x / train_x.std(axis=0)
test_x = test_x / test_x.std(axis=0)
``````

`````` datagen = ImageDataGenerator(rotation_range=10,
width_shift_range=5. / 32,
height_shift_range=5. / 32,
horizontal_flip=True)
# Compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied).
datagen.fit(train_x)
``````

``````#Encode the labels to vectors
train_y = keras.utils.to_categorical(train_y,10)
test_y = keras.utils.to_categorical(test_y,10)
``````

``````#import needed classes
import keras
from keras.datasets import cifar10
from keras.layers import Dense,Conv2D,MaxPooling2D,Flatten,AveragePooling2D,Dropout,BatchNormalization,Activation
from keras.models import Model,Input
from keras.callbacks import LearningRateScheduler
from keras.callbacks import ModelCheckpoint
from math import ceil
import os
from keras.preprocessing.image import ImageDataGenerator
def Unit(x,filters):
out = BatchNormalization()(x)
out = Activation("relu")(out)
out = Conv2D(filters=filters, kernel_size=[3, 3], strides=[1, 1], padding="same")(out)
return out
#Define the model
def MiniModel(input_shape):
images = Input(input_shape)
net = Unit(images,64)
net = Unit(net,64)
net = Unit(net,64)
net = MaxPooling2D(pool_size=(2,2))(net)
net = Unit(net,128)
net = Unit(net,128)
net = Unit(net,128)
net = MaxPooling2D(pool_size=(2, 2))(net)
net = Unit(net,256)
net = Unit(net,256)
net = Unit(net,256)
net = Dropout(0.25)(net)
net = AveragePooling2D(pool_size=(8,8))(net)
net = Flatten()(net)
net = Dense(units=10,activation="softmax")(net)
model = Model(inputs=images,outputs=net)
return model
(train_x, train_y) , (test_x, test_y) = cifar10.load_data()
#normalize the data
train_x = train_x.astype('float32') / 255
test_x = test_x.astype('float32') / 255
#Subtract the mean image from both train and test set
train_x = train_x - train_x.mean()
test_x = test_x - test_x.mean()
#Divide by the standard deviation
train_x = train_x / train_x.std(axis=0)
test_x = test_x / test_x.std(axis=0)
datagen = ImageDataGenerator(rotation_range=10,
width_shift_range=5. / 32,
height_shift_range=5. / 32,
horizontal_flip=True)
# Compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied).
datagen.fit(train_x)
#Encode the labels to vectors
train_y = keras.utils.to_categorical(train_y,10)
test_y = keras.utils.to_categorical(test_y,10)
#define a common unit
input_shape = (32,32,3)
model = MiniModel(input_shape)
#Print a Summary of the model
model.summary()
#Specify the training components
epochs = 20
steps_per_epoch = ceil(50000/128)
# Fit the model on the batches generated by datagen.flow().
model.fit_generator(datagen.flow(train_x, train_y, batch_size=128),
validation_data=[test_x,test_y],                epochs=epochs,steps_per_epoch=steps_per_epoch, verbose=1, workers=4)
#Evaluate the accuracy of the test dataset
accuracy = model.evaluate(x=test_x,y=test_y,batch_size=128)
model.save("cifar10model.h5")
``````

``````input_shape = (32,32,3)
model = MiniModel(input_shape)
#Print a Summary of the model
model.summary()
``````

``````epochs = 20
steps_per_epoch = ceil(50000/128)
# Fit the model on the batches generated by datagen.flow().
model.fit_generator(datagen.flow(train_x, train_y, batch_size=128),
validation_data=[test_x,test_y],                  epochs=epochs,steps_per_epoch=steps_per_epoch, verbose=1, workers=4)
#Evaluate the accuracy of the test dataset
accuracy = model.evaluate(x=test_x,y=test_y,batch_size=128)
model.save("cifar10model.h5")
``````

`steps_per_epoch = ceil(50000/128)`

50000是总共训练图像的数量，我们使用一个批处理大小为128。

``````Fit the model on the batches generated by datagen.flow().
model.fit_generator(datagen.flow(train_x, train_y, batch_size=128),
validation_data=[test_x,test_y],
epochs=epochs,steps_per_epoch=steps_per_epoch, verbose=1, workers=4)
``````

【云栖快讯】Apache旗下顶级开源盛会 HBasecon Asia 2018将于8月17日在京举行，现场仅600席，免费赠票领取入口  详情请点击

### 网友评论

【方向】

PostgreSQL被业界誉为“最先进的开源数据库”，面向企业复杂SQL处理的OLTP在线事... 查看详情