Keras 猫狗二分类
import keras
from keras.models import Sequential
from keras.layers import Dense,MaxPooling2D,Input,Flatten,Convolution2D,Dropout,GlobalAveragePooling2D
from keras.optimizers import SGD
from keras.callbacks import TensorBoard,ModelCheckpoint
from PIL import Image
import os
import numpy as np
from scipy import misc
root_path = os.getcwd()
def load_data():
tran_imags = []
labels = []
seq_names = ['cat','dog']
for seq_name in seq_names:
frames = sorted(os.listdir(os.path.join(root_path,'data','train_data', seq_name)))
for frame in frames:
imgs = [os.path.join(root_path, 'data', 'train_data', seq_name, frame)]
imgs = np.array(Image.open(imgs[0]))
tran_imags.append(imgs)
if seq_name=='cat':
labels.append(0)
else:
labels.append(1)
return np.array(tran_imags), np.array(labels)
def vgg_cat_dog(weights_path=None,has_fc=True):
model = Sequential()
model.add(Convolution2D(64,(3,3),activation='relu',padding='same',input_shape=(224,224,3)))
model.add(Convolution2D(64,(3,3),activation='relu',padding='same'))
model.add(MaxPooling2D((2,2),strides=(2,2)))
model.add(Convolution2D(128,(3,3),activation='relu',padding='same'))
model.add(Convolution2D(128,(3,3),activation='relu',padding='same'))
model.add(MaxPooling2D((2,2),strides=(2,2)))
model.add(Convolution2D(256,(3,3),activation='relu',padding='same'))
model.add(Convolution2D(256,(3,3),activation='relu',padding='same'))
model.add(Convolution2D(256,(3,3),activation='relu',padding='same'))
model.add(MaxPooling2D((2,2),strides=(2,2)))
model.add(Convolution2D(512,(3,3),activation='relu',padding='same'))
model.add(Convolution2D(512,(3,3),activation='relu',padding='same'))
model.add(Convolution2D(512,(3,3),activation='relu',padding='same'))
model.add(MaxPooling2D((2,2),strides=(2,2)))
model.add(Convolution2D(512,(3,3),activation='relu',padding='same'))
model.add(Convolution2D(512,(3,3),activation='relu',padding='same'))
model.add(Convolution2D(512,(3,3),activation='relu',padding='same'))
model.add(MaxPooling2D((2,2),strides=(2,2)))
if weights_path:
model.load_weights(weights_path)
if has_fc:
model.add(Flatten())
model.add(Dense(500,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(100,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2,activation='softmax'))
else:
model.add(GlobalAveragePooling2D())
model.add(Dense(2,activation='softmax'))
return model
train_data,train_labs = load_data()
train = True
if train:
model = vgg_cat_dog('model/vgg_16_without_fc.h5')
sgd = SGD(lr=1e-6,momentum=0.9,decay=1e-6,nesterov=True)
model.compile(optimizer=sgd,loss='categorical_crossentropy',metrics=['accuracy'])
tensorboard = TensorBoard(log_dir='log/cat_dog',histogram_freq=10,write_images=True)
ckpt = ModelCheckpoint(os.path.join(root_path,'model','cat_dog','vgg.h5'),verbose=1,period=5)
model.fit(train_data,keras.utils.to_categorical(train_labs),batch_size=32,epochs=50,verbose=1,
callbacks=[tensorboard,ckpt],validation_split=0.1,shuffle=True)
else:
model = vgg_cat_dog()
model.load_weights('model/cat_dog/vgg.h5')
frames = sorted(os.listdir(os.path.join(root_path,'data','test', 'cat')))
for frame in frames:
imgs = [os.path.join(root_path, 'data', 'test', 'cat', frame)]
im = np.expand_dims(np.array(misc.imresize(Image.open(imgs[0]), [224, 224])), axis=0)
cal = np.argmax(model.predict(im))
if cal:
print('dog')
else:
print('cat')
使用在imagenet上的预训练好的前13层的权重进行fine-tune,训练集猫狗各420张左右,进行了50轮训练,效果还不错 ####记: 1. 开始未加dropout. 微调时,参数调的不多,第一次设置的学习率过大(0.1),损失居高不下一条直线,精度也是一条直线(0.1)。 第二次设置的1e-8,网络开始收敛,比较慢,最后选择1e-6,训练集精度达到95左右,验证集只有50左右的精度,过拟合。
- 添加dropout层,参数不变,验证集精度飙升,由上图可见40轮左右基本稳定下来了,不得不感叹,dropout真是好啊。
附:keras下:1.vgg16带全连接层权重带全连接链接 2.vgg16不带全连接权重不带全连接