Those who have basic knowledge of Deep Learning convolutional neural networks (CNN) and understand the meanings of the following words Example) --Convolution
This is one of the Deep Learning methods for analyzing images. This model was announced in 2014 and has high performance so that it can still be used today. Trained models can also be called from Keras and used.
The original article is here.
The following are important in creating VGG16: --The size of the filter used for convolution is 3 x 3 --Max Pooling is performed when convolution is performed multiple times. --Double the number of filter channels after Max Pooling.
GoogleColaboratory
#Installation of required libraries
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.datasets import cifar10
#A class that takes CIFAR10 data and converts it to a vector
class CIFAR10Dataset():
def __init__(self):
self.image_shape = (32, 32, 3)
self.num_classes = 10
#Acquire training data and test data.
def get_batch(self):
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = [self.change_vec(img_data) for img_data in [x_train, x_test]]
y_train, y_test = [self.change_vec(img_data, label_data=True) for img_data in [y_train, y_test]]
return x_train, y_train, x_test, y_test
#If it is an objective variable, change it to a class vector. Explanatory variables are standardized.
def change_vec(self, img_data, label=False):
if label:
data = keras.utils.to_categorical(img_data, self.num_classes)
else:
img_data = img_data.astype("float32")
img_data /= 255
shape = (img_data.shape[0],) + self.image_shape
img_data = img_data.reshape(shape)
return img_data
#A function that sets and returns a deep learning model
def network(input_shape, num_classes):
model = Sequential()
model.add(Conv2D(32, kernel_size=3, padding="same", input_shape=input_shape, activation="relu"))
model.add(Conv2D(32, kernel_size=3, padding="same", activation="relu"))
model.add(MaxPooling2D())
model.add(Conv2D(64, kernel_size=3, padding="same", activation="relu"))
model.add(Conv2D(64, kernel_size=3, padding="same", activation="relu"))
model.add(MaxPooling2D())
model.add(Conv2D(128, kernel_size=3, padding="same", activation="relu"))
model.add(Conv2D(128, kernel_size=3, padding="same", activation="relu"))
model.add(Conv2D(128, kernel_size=3, padding="same", activation="relu"))
model.add(MaxPooling2D())
model.add(Flatten())
model.add(Dense(1024, activation="relu"))
model.add(Dense(1024, activation="relu"))
model.add(Dense(num_classes, activation="softmax"))
print(model.summary())
return model
#Class to train the model
class Trainer():
#Compile the model and set the settings for training in private properties.
def __init__(self, model, loss, optimizer):
self._model = model
self._model.compile(
loss=loss,
optimizer=optimizer,
metrics=["accuracy"]
)
self._verbose = 1
self._batch_size = 128
self._epochs = 30
#Actual learning
def fit(self, x_train, y_train, x_test, y_test):
self._model.fit(
x_train,
y_train,
batch_size=self._batch_size,
epochs=self._epochs,
verbose=self._verbose,
validation_data=(x_test, y_test)
)
return self._model
dataset = CIFAR10Dataset() #Instantiation of CIFAR10 Dataset to retrieve data
model = network(dataset.image_shape, dataset.num_classes) #Get the model
x_train, y_train, x_test, y_test = dataset.get_batch() #Acquisition of training data and test data
trainer = Trainer(model, loss="categorical_crossentropy", optimizer="adam") #Instantiation of Trainer with model, loss function, and optimization algorithm as arguments
model = trainer.fit(x_train, y_train, x_test, y_test) #Model learning
#Model evaluation
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss: ', score[0])
print('Test accuracy: ', score[1])
Recommended Posts