如何使用Alexnet模型构建CNN?

时间:2019-03-08 16:00:20

标签: python-3.x tensorflow keras

如何使用keras和Tensorflow在python中使用Alexnet构建基于计算机视觉的对象识别系统?

有没有熟悉 Alexnet 的人,请帮助我使用Alexnet CNN模型使用我的自定义图像目录数据集建立图像分类。

1 个答案:

答案 0 :(得分:1)

您可以从Squeeze Net(AlexNet的简化版本)开始:

SqueezeNet:AlexNet级精度,参数减少50倍,模型尺寸小于0.5MB:

https://arxiv.org/pdf/1602.07360.pdf

代码:

import numpy as np
from keras.layers import Input, Dense, Lambda
from keras.models import Model
from keras import backend as K
from keras import objectives
from keras.datasets import mnist
from keras.layers.core import Reshape
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, Concatenate
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers.convolutional import Conv2D, MaxPooling2D, ZeroPadding2D, UpSampling2D
from keras.utils import np_utils
from keras.layers.normalization import BatchNormalization
from keras.optimizers import SGD
from keras.layers.advanced_activations import ELU
from keras.layers.pooling import GlobalAveragePooling2D
import pandas as pd
import matplotlib.pyplot as plt

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))

x_train_CNN=x_train.reshape(60000,28,28,1)

y_train2=pd.get_dummies(y_train)

epochs=3
learning_rate = 0.07
decay_rate = 5e-5
momentum = 0.6

sgd = SGD(lr=learning_rate,momentum=momentum, decay=decay_rate, nesterov=False)

input_shape=(28,28,1)

input_img = Input(batch_shape=(None, 28,28,1))
squeeze=Lambda(lambda x: x ** 2,input_shape=(784,),output_shape=(1,784))(input_img)
squeeze=Reshape((28,28,1))(squeeze)
squeeze=Conv2D(64, 3,3,
                          border_mode='valid',
                        input_shape=input_shape)(squeeze)
squeeze=BatchNormalization()(squeeze)
squeeze=ELU(alpha=1.0)(squeeze)
squeeze=MaxPooling2D(pool_size=(2,2))(squeeze)
squeeze=Conv2D(32, 1, 1,
                            init='glorot_uniform')(squeeze)
squeeze=BatchNormalization()(squeeze)
squeeze=ELU(alpha=1.0)(squeeze)

squeeze_left=squeeze
squeeze_left=Conv2D(64, 3,3,
                          border_mode='valid',
                        input_shape=input_shape)(squeeze_left)
squeeze_left=ELU(alpha=1.0)(squeeze_left)

squeeze_right=squeeze
squeeze_right=Conv2D(64, 3,3,
                          border_mode='valid',
                        input_shape=input_shape)(squeeze_right)
squeeze_right=ELU(alpha=1.0)(squeeze_right)

squeeze0=Concatenate()([squeeze_left,squeeze_right])
squeeze0=Dropout(0.2)(squeeze0)
squeeze0=GlobalAveragePooling2D()(squeeze0)
squeeze0=Dense(10)(squeeze0)
squeeze0=Activation('sigmoid')(squeeze0)

model = Model(inputs = input_img, outputs = squeeze0)

model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics = ['accuracy'])
model.summary()

model.fit(x_train_CNN,np.array(y_train2),
                nb_epoch=15,
                batch_size=30,verbose=1)

predictions=np.argmax(model.predict(x_train_CNN,verbose=1),axis=1)

神经网络架构:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_22 (InputLayer)           (None, 28, 28, 1)    0                                            
__________________________________________________________________________________________________
lambda_39 (Lambda)              (None, 1, 784)       0           input_22[0][0]                   
__________________________________________________________________________________________________
reshape_39 (Reshape)            (None, 28, 28, 1)    0           lambda_39[0][0]                  
__________________________________________________________________________________________________
conv2d_144 (Conv2D)             (None, 26, 26, 64)   640         reshape_39[0][0]                 
__________________________________________________________________________________________________
batch_normalization_73 (BatchNo (None, 26, 26, 64)   256         conv2d_144[0][0]                 
__________________________________________________________________________________________________
elu_143 (ELU)                   (None, 26, 26, 64)   0           batch_normalization_73[0][0]     
__________________________________________________________________________________________________
max_pooling2d_37 (MaxPooling2D) (None, 13, 13, 64)   0           elu_143[0][0]                    
__________________________________________________________________________________________________
conv2d_145 (Conv2D)             (None, 13, 13, 32)   2080        max_pooling2d_37[0][0]           
__________________________________________________________________________________________________
batch_normalization_74 (BatchNo (None, 13, 13, 32)   128         conv2d_145[0][0]                 
__________________________________________________________________________________________________
elu_144 (ELU)                   (None, 13, 13, 32)   0           batch_normalization_74[0][0]     
__________________________________________________________________________________________________
conv2d_146 (Conv2D)             (None, 11, 11, 64)   18496       elu_144[0][0]                    
__________________________________________________________________________________________________
conv2d_147 (Conv2D)             (None, 11, 11, 64)   18496       elu_144[0][0]                    
__________________________________________________________________________________________________
elu_145 (ELU)                   (None, 11, 11, 64)   0           conv2d_146[0][0]                 
__________________________________________________________________________________________________
elu_146 (ELU)                   (None, 11, 11, 64)   0           conv2d_147[0][0]                 
__________________________________________________________________________________________________
concatenate_34 (Concatenate)    (None, 11, 11, 128)  0           elu_145[0][0]                    
                                                                 elu_146[0][0]                    
__________________________________________________________________________________________________
dropout_28 (Dropout)            (None, 11, 11, 128)  0           concatenate_34[0][0]             
__________________________________________________________________________________________________
global_average_pooling2d_21 (Gl (None, 128)          0           dropout_28[0][0]                 
__________________________________________________________________________________________________
dense_15 (Dense)                (None, 10)           1290        global_average_pooling2d_21[0][0]
__________________________________________________________________________________________________
activation_15 (Activation)      (None, 10)           0           dense_15[0][0]                   
==================================================================================================
Total params: 41,386
Trainable params: 41,194
Non-trainable params: 192
__________________________________________________________________________________________________