七、VGG16实现鸟类数据库分类

前文

加利福尼亚理工学院鸟类数据库分类

数据生成器

from keras.preprocessing.image import ImageDataGenerator

IMSIZE = 224
train_generator = ImageDataGenerator(rescale=1. / 255).flow_from_directory('../../data/data_vgg/train',
                                                                           target_size=(IMSIZE, IMSIZE),
                                                                           batch_size=20,
                                                                           class_mode='categorical'
                                                                           )

validation_generator = ImageDataGenerator(rescale=1. / 255).flow_from_directory('../../data/data_vgg/test',
                                                                                target_size=(IMSIZE, IMSIZE),
                                                                                batch_size=20,
                                                                                class_mode='categorical'
                                                                                )
                                                                           )

图像显示

from matplotlib import pyplot as plt

plt.figure()
fig, ax = plt.subplots(2, 5)
fig.set_figheight(6)
fig.set_figwidth(15)
ax = ax.flatten()
X, Y = next(validation_generator)
for i in range(15): ax[i].imshow(X[i, :, :, ])

VGG模型构建

from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Flatten, Dense, Input, Activation
from keras import Model
from keras.layers import GlobalAveragePooling2D

input_shape = (IMSIZE, IMSIZE, 3)
input_layer = Input(input_shape)
x = input_layer

x = Conv2D(64, [3, 3], padding="same", activation='relu')(x)
x = Conv2D(64, [3, 3], padding="same", activation='relu')(x)
x = MaxPooling2D((2, 2))(x)

x = Conv2D(128, [3, 3], padding="same", activation='relu')(x)
x = Conv2D(128, [3, 3], padding="same", activation='relu')(x)
x = Conv2D(128, [3, 3], padding="same", activation='relu')(x)
x = MaxPooling2D((2, 2))(x)

x = Conv2D(256, [3, 3], padding="same", activation='relu')(x)
x = Conv2D(256, [3, 3], padding="same", activation='relu')(x)
x = Conv2D(256, [3, 3], padding="same", activation='relu')(x)
x = MaxPooling2D((2, 2))(x)

x = Conv2D(512, [3, 3], padding="same", activation='relu')(x)
x = Conv2D(512, [3, 3], padding="same", activation='relu')(x)
x = Conv2D(512, [3, 3], padding="same", activation='relu')(x)
x = MaxPooling2D((2, 2))(x)

x = Conv2D(512, [3, 3], padding="same", activation='relu')(x)
x = Conv2D(512, [3, 3], padding="same", activation='relu')(x)
x = Conv2D(512, [3, 3], padding="same", activation='relu')(x)
x = MaxPooling2D((2, 2))(x)

x = GlobalAveragePooling2D()(x)

x = Dense(315)(x)
x = Activation('softmax')(x)
output_layer = x
model_vgg16 = Model(input_layer, output_layer)
model_vgg16.summary()

VGG模型编译与拟合

from keras.optimizers import Adam

model_vgg16.compile(loss='categorical_crossentropy',
                    optimizer=Adam(lr=0.001),
                    metrics=['accuracy'])
model_vgg16.fit_generator(train_generator,
                          epochs=20,
                          validation_data=validation_generator)

注意:

因为自己是使用tensorflow-GPU版本,自己电脑是1050Ti,4G显存。实际运行时候batch_size设置不到15大小,太大了就显存资源不足。
但是batch_size太小,总的数据集较大较多,所以最后消耗时间就较长。
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:7576b1d0-7718-434f-977c-32af2c49595c

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:7b787ff2-2365-4902-a514-a19686bed26a

数据集来源:kaggle平台315种鸟类:315 Bird Species – Classification | Kaggle

GitHub下载地址:

Tensorflow1.15深度学习

Original: https://www.cnblogs.com/lehoso/p/15614793.html
Author: 李好秀
Title: 七、VGG16实现鸟类数据库分类

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/563747/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球