实验平台:谷歌Colaboratory
网络模型:VGG16
数据集:kaggle猫狗数据集(仅使用2000张,1000张猫1000张狗)
深度学习框架:Keras+Tensorflow后台
代码如下:
from PIL import Image
import numpy as np
from keras.utils import to_categorical
path="/content/drive/My Drive/Colab Notebooks/data/dog_vs_cat/"
train_X=np.empty((2000,224,224,3),dtype="float16")
train_Y=np.empty((2000,),dtype="int")
for i in range(1000):
file_path=path+"cat."+str(i)+".jpg"
image=Image.open(file_path)
resized_image = image.resize((224, 224), Image.ANTIALIAS)
img=np.array(resized_image)
train_X[i,:,:,:]=img
train_Y[i]=0
for i in range(1000):
file_path=path+"dog."+str(i)+".jpg"
image = Image.open(file_path)
resized_image = image.resize((224, 224), Image.ANTIALIAS)
img = np.array(resized_image)
train_X[i+1000, :, :, :] = img
train_Y[i+1000] = 1
train_X /= 255
train_Y = to_categorical(train_Y, 2)
index = np.arange(2000)
np.random.shuffle(index)
train_X = train_X[index, :, :, :]
train_Y = train_Y[index]
print(train_X.shape)
print(train_Y.shape)
from keras.layers import BatchNormalization, Dropout
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense,Activation
# AlexNet
model = Sequential()
model.add(Conv2D(64, (3, 3), input_shape=(224, 224, 3), padding='same', activation='relu', name='conv1_block'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same',name='conv2_block'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same',name='conv3_block'))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same',name='conv4_block'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(256, (3, 3), activation='relu', padding='same',name='conv5_block'))
model.add(Conv2D(256, (3, 3), activation='relu', padding='same',name='conv6_block'))
model.add(Conv2D(256, (3, 3), activation='relu', padding='same',name='conv7_block'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same',name='conv8_block'))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same',name='conv9_block'))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same',name='conv10_block'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same',name='conv11_block'))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same',name='conv12_block'))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same',name='conv13_block'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
batch_size = 32
epochs = 50
model.fit(train_X, train_Y,
batch_size=batch_size,
epochs=epochs)