猫狗识别
#%%
from keras.applications.vgg16 import VGG16
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, load_model
from keras.layers import Dropout, Flatten, Dense
from keras.optimizers import Adam
#%%
# 载入VGG网络,不包括全连接层
model = VGG16(weights='imagenet', include_top=False)
#%%
model.summary()
#%%
# 对图片进行随机处理,以扩大数据集
datagen = ImageDataGenerator(
# 随机旋转角度
rotation_range=40,
# 随机水平平移
width_shift_range=0.2,
# 随机竖直平移
height_shift_range=0.2,
# 数值归一化
rescale=1. / 255,
# 随机裁剪
shear_range=0.2,
# 随机放大
zoom_range=0.2,
# 水平翻转
horizontal_flip=True,
# 填充方式
fill_mode='nearest'
)
#%%
batch_size = 32
# 训练集生成器
train_generator = datagen.flow_from_directory(
# 训练集路径
'image/train',
# 设置生成的图片大小
target_size=(150, 150),
# 批次大小
batch_size=batch_size,
# 不生成标签
# class_mode=None,
# 不随机打乱
shuffle=False
)
# 测试集生成器
test_generator = datagen.flow_from_directory(
# 训练集路径
'image/test',
# 设置生成的图片大小
target_size=(150, 150),
# 批次大小
batch_size=batch_size,
# 不生成标签
# class_mode=None,
# 不随机打乱
shuffle=False
)
#%%
# 得到经过卷积层和池化层后的训练集数据
# 相当于model.predict
bottleneck_features_train = model.predict_generator(
# 训练集生成器
train_generator,
# batch数量 变换1次
train_generator.samples / batch_size * 1)
# (400, 4, 4, 512)
# (image数量, row, col, 层数)
print(bottleneck_features_train.shape)
bottleneck_features_test = model.predict_generator(
# 测试集生成器
test_generator,
# batch数量 变换1次
test_generator.samples / batch_size * 1)
# (200, 4, 4, 512)
print(bottleneck_features_test.shape)
#%%
# 保存这些数据 训练集和测试集bottleneck结果
np.save(open("bottleneck_features_train.npy", "wb"), bottleneck_features_train)
np.save(open("bottleneck_features_test.npy", "wb"), bottleneck_features_test)
#%%
# 读取训练集和测试集bottleneck结果
train_data = np.load(open('bottleneck_features_train.npy', 'rb'))
test_data = np.load(open('bottleneck_features_test.npy', 'rb'))
print(train_data.shape)
print(test_data.shape)
#%%
'''labels = np.array([0] * 20 + [1] * 20)
train_labels = np.array([])
for i in range(10):
train_labels = np.concatenate((train_labels, labels))
labels = np.array([0] * 10 + [1] * 10)
test_labels = np.array([])
for i in range(10):
test_labels = np.concatenate((test_labels, labels))'''
#%%
train_labels = np.zeros((train_data.shape[0], 2))
for i in range(int(train_data.shape[0] / 2)):
train_labels[i, 0] = 1
for i in range(int(train_data.shape[0] / 2), train_data.shape[0]):
train_labels[i, 1] = 1
test_labels = np.zeros((test_data.shape[0], 2))
for i in range(int(test_data.shape[0] / 2)):
test_labels[i, 0] = 1
for i in range(int(test_data.shape[0] / 2), test_data.shape[0]):
test_labels[i, 1] = 1
#%%
model = Sequential()
model.add(Flatten(input_shape=train_data.shape[1:]))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))
adam = Adam(lr=1e-4)
#%%
model.compile(optimizer=adam, loss="categorical_crossentropy", metrics=['accuracy'])
#%%
model.summary()
#%%
model.fit(train_data, train_labels, epochs=20)
#%%
model.save("bottleneck_fc_model.h5")
#%%
model = load_model("bottleneck_fc_model.h5")
#%%
test_pred = model.predict(test_data)