📅  最后修改于: 2022-03-11 14:47:00.159000             🧑  作者: Mango
import numpy as np
import tensorflow as tf
from keras.models import Model
from keras.layers import Input, Activation, Dense, Conv2D, MaxPooling2D, Flatten
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from keras.callbacks import TensorBoard
# Images Dimensions
img_width, img_height = 200, 200
# Data Path
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
# Parameters
nb_train_samples = 100
nb_validation_samples = 50
epochs = 50
batch_size = 10
# TensorBoard Callbacks
callbacks = TensorBoard(log_dir='./Graph')
# Training Data Augmentation
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
# Rescale Testing Data
test_datagen = ImageDataGenerator(rescale=1. / 255)
# Train Data Generator
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')
# Testing Data Generator
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')
# Feature Extraction Layer KorNet
inputs = Input(shape=(img_width, img_height, 3))
conv_layer = Conv2D(16, (5, 5), strides=(3,3), activation='relu')(inputs)
conv_layer = MaxPooling2D((2, 2))(conv_layer)
conv_layer = Conv2D(32, (5, 5), strides=(3,3), activation='relu')(conv_layer)
conv_layer = MaxPooling2D((2, 2))(conv_layer)
# Flatten Layer
flatten = Flatten()(conv_layer)
# Fully Connected Layer
fc_layer = Dense(32, activation='relu')(flatten)
outputs = Dense(3, activation='softmax')(fc_layer)
model = Model(inputs=inputs, outputs=outputs)
# Adam Optimizer and Cross Entropy Loss
adam = Adam(lr=0.0001)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])
# Print Model Summary
print(model.summary())
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size,
callbacks=[callbacks])
model.save('./models/model.h5')
model.save_weights('./models/weights.h5')