利用keras搭建AlexNet神经网络识别kaggle猫狗图片
程序员文章站
2024-03-14 11:23:22
...
AlexNet结构
keras代码
from PIL import Image
import numpy as np
from keras.utils import to_categorical
path="F:\\kaggle\\dog_vs_cat\\"
train_X=np.empty((2000,227,227,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((227, 227), 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((227, 227), 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(filters=96, kernel_size=(11, 11),
strides=(4, 4), padding='valid',
input_shape=(227, 227, 3),
activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(3, 3),
strides=(2, 2),
padding='valid'))
# 第二段
model.add(Conv2D(filters=256, kernel_size=(5, 5),
strides=(1, 1), padding='same',
activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(3, 3),
strides=(2, 2),
padding='valid'))
# 第三段
model.add(Conv2D(filters=384, kernel_size=(3, 3),
strides=(1, 1), padding='same',
activation='relu'))
model.add(Conv2D(filters=384, kernel_size=(3, 3),
strides=(1, 1), padding='same',
activation='relu'))
model.add(Conv2D(filters=256, kernel_size=(3, 3),
strides=(1, 1), padding='same',
activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3),
strides=(2, 2), padding='valid'))
# 第四段
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(1000, activation='relu'))
model.add(Dropout(0.5))
# Output Layer
model.add(Dense(2))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
batch_size = 32
epochs = 20
model.fit(train_X, train_Y,
batch_size=batch_size,
epochs=epochs)
其中数据集为2000张猫狗图片,1000张猫,1000张狗,图片名为cat.0.jpg,dog.1.jpg等,即cat(dog).i.jpg格式,读取图像后resize为227x227x3作为AlexNet的输入,这里用BN代替LRN,batch_size取为32,训练20轮(实在太慢,20轮就算了很久),最后得到如下结果: