import tensorflow as tf
from keras.layers import Dense, Conv2D, Flatten, MaxPool2D, BatchNormalization, AlphaDropout
import keras
import pandas as pd
import matplotlib.pyplot as plt
train_dir = './training/training/'
valid_dir = './validation/validation/'
# tensorflow创建dataset的几种方式
# 第一种,通过tf.keras.processing.image.ImageDataGenerator
train_data_generator = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1. / 255.,
rotation_range=90,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
vertical_flip=True,
shear_range=0.2,
zoom_range=0.2,
fill_mode='nearest'
)
valid_data_generator = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1. / 255.
)
train_dataset = train_data_generator.flow_from_directory(
directory=train_dir,
class_mode='categorical',
target_size=(224, 224),
shuffle=True,
batch_size=32
)
valid_dataset = valid_data_generator.flow_from_directory(
directory=valid_dir,
class_mode='categorical',
target_size=(224, 224),
shuffle=False,
batch_size=32
)
x, y = valid_dataset.next()
print(x.shape, y.shape)
def build_cnn_net(n_height, n_width, num_classes):
input_layer1 = tf.keras.layers.Input(shape=(n_height, n_width, 3), dtype=tf.float32)
x = tf.keras.layers.ZeroPadding2D(padding=((1, 2), (1, 2)))(input_layer1)
x = tf.keras.layers.Conv2D(48, kernel_size=11, strides=4, activation='relu')(x)
x = tf.keras.layers.MaxPool2D(pool_size=3, strides=2)(x)
x = tf.keras.layers.Conv2D(128, padding='same', kernel_size=5, strides=1, activation='relu')(x)
x = tf.keras.layers.MaxPool2D(pool_size=3, strides=2)(x)
x = tf.keras.layers.Conv2D(192, kernel_size=3, activation='relu', padding='same')(x)
x = tf.keras.layers.Conv2D(192, kernel_size=3, activation='relu', padding='same')(x)
x = tf.keras.layers.Conv2D(128, kernel_size=3, activation='relu', padding='same')(x)
x = tf.keras.layers.MaxPool2D(pool_size=3, strides=2)(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dropout(0.2)(x)
x = tf.keras.layers.Dense(2048, activation='relu')(x)
x = tf.keras.layers.Dropout(0.2)(x)
x = tf.keras.layers.Dense(2048, activation='relu')(x)
prediction = tf.keras.layers.Dense(num_classes, activation='softmax')(x)
model = keras.models.Model(inputs=input_layer1, outputs=prediction)
return model
def draw_curve(history):
pd.DataFrame(history.history).plot()
plt.gca().set_ylim(0, 1)
plt.grid(True)
plt.show()
model = build_cnn_net(224, 224, 10)
model.summary()
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])
history = model.fit(train_dataset, steps_per_epoch=train_dataset.samples // 32, validation_data=valid_dataset,
validation_steps=valid_dataset.samples // 32, epochs=10)
draw_curve(history)