Implementation of VGG16 using Keras created without using a trained model

Read this article about the readership

Those who have basic knowledge of Deep Learning convolutional neural networks (CNN) and understand the meanings of the following words Example) --Convolution

What is VGG16

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.

Operating environment

GoogleColaboratory

Sample program

#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])

References

Intuition Deep Learning

Recommended Posts

Implementation of VGG16 using Keras created without using a trained model
I tried using the trained model VGG16 of the deep learning library Keras
Diversion of layers of trained keras model
We have released a trained model of fastText
Two-dimensional visualization of document vectors using Word2Vec trained model
Building a seq2seq model using keras' Functional API Inference
Implementation of a convolutional neural network using only Numpy
Implementation of TF-IDF using gensim
I made a VGG16 model using TensorFlow (on the way)
A memorandum of using eigen3
Try to edit a new image using the trained StyleGAN2 model
Evaluate the performance of a simple regression model using LeaveOneOut cross-validation
Implementation of a simple particle filter
Implementation of a two-layer neural network 2
Implementation of desktop notifications using Python
Implementation of Light CNN (Python Keras)
Qiskit: Implementation of QAOA without Qiskit Aqua
Creating a learning model using MNIST
Image recognition of fruits using VGG16
Create a survival prediction model for Kaggle Titanic passengers without using Python
[NNabla] How to add a quantization layer to the middle layer of a trained model
Implement a model with state and behavior (3) --Example of implementation by decorator
Getting a combination of elements using itertools
A memorandum of using Python's input function
Use a scikit-learn model trained in PySpark
Implementation of dialogue system using Chainer [seq2seq]
Impressions of using Flask for a month
Sum of variables in a mathematical model
Get a reference model using Django Serializer
Implementation of "blurred" neural network using Chainer
Add a layer using the Keras backend
Benefits of using slugfield in Django's model
Python implementation of continuous hidden Markov model
Implementation of a model that predicts the exchange rate (dollar-yen rate) by machine learning