SoFunction
Updated on 2024-11-20

keras implementation of VGG16 CIFAR10 dataset approach

I'm going to cut to the chase, so let's just go straight to the code!

import keras
from  import cifar10
from  import ImageDataGenerator
from  import Sequential
from  import Dense, Dropout, Activation, Flatten
from  import Conv2D, MaxPooling2D, BatchNormalization
from keras import optimizers
import numpy as np
from  import Lambda
from keras import backend as K
from  import SGD
from keras import regularizers
 
#import data
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
y_train = .to_categorical(y_train, 10)
y_test = .to_categorical(y_test, 10)
 
weight_decay = 0.0005
nb_epoch=100
batch_size=32
 
#layer1 32*32*3
model = Sequential()
(Conv2D(64, (3, 3), padding='same',
input_shape=(32,32,3),kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(Dropout(0.3))
#layer2 32*32*64
(Conv2D(64, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(MaxPooling2D(pool_size=(2, 2)))
#layer3 16*16*64
(Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(Dropout(0.4))
#layer4 16*16*128
(Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(MaxPooling2D(pool_size=(2, 2)))
#layer5 8*8*128
(Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(Dropout(0.4))
#layer6 8*8*256
(Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(Dropout(0.4))
#layer7 8*8*256
(Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(MaxPooling2D(pool_size=(2, 2)))
#layer8 4*4*256
(Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(Dropout(0.4))
#layer9 4*4*512
(Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(Dropout(0.4))
#layer10 4*4*512
(Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(MaxPooling2D(pool_size=(2, 2)))
#layer11 2*2*512
(Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(Dropout(0.4))
#layer12 2*2*512
(Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(Dropout(0.4))
#layer13 2*2*512
(Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
(MaxPooling2D(pool_size=(2, 2)))
(Dropout(0.5))
#layer14 1*1*512
(Flatten())
(Dense(512,kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
#layer15 512
(Dense(512,kernel_regularizer=regularizers.l2(weight_decay)))
(Activation('relu'))
(BatchNormalization())
#layer16 512
(Dropout(0.5))
(Dense(10))
(Activation('softmax'))
# 10
 
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
(loss='categorical_crossentropy', optimizer=sgd,metrics=['accuracy'])
 
(x_train,y_train,epochs=nb_epoch, batch_size=batch_size,
       validation_split=0.1, verbose=1)

Additional knowledge:pytorch step by step training its own dataset on VGG16

Preparing the dataset and loading it, ImageFolder

In many machine learning or deep learning tasks, often we have to provide our own images. That means our dataset is not pre-processed, like mnist, cifar10, etc. it's already processed for you, it's more raw images. For example, let's take dog and cat categorization as an example. Under data file, there are two folders which are train and val respectively. Then under train, there are two folders, cat and dog, which store their own image data, and the val folder is the same as train, so that our dataset is ready.

ImageFolder can use the directory name as the label to divide the data set, the following is the introduction of ImageFolder in pytorch Chinese document:

# Do a transformation on the training set
train_transforms = ([
  (224), #Do a zoom cut on the image size
  (), # Horizontal flip
  (),   #Transform into a tensor
  ((.5, .5, .5), (.5, .5, .5)) # Perform normalization
])
# Doing transformations on the test set
val_transforms = ([
  (256),
  (224),
  (),
  ((.5, .5, .5), (.5, .5, .5))
])

train_dir = "G:/data/train"      # Training set paths
#Define the data set
train_datasets = (train_dir, transform=train_transforms)
#Load the dataset
train_dataloader = (train_datasets, batch_size=batch_size, shuffle=True)

val_dir = "G:/datat/val" 
val_datasets = (val_dir, transform=val_transforms)
val_dataloader = (val_datasets, batch_size=batch_size, shuffle=True)

Transfer Learning Exemplified by VGG16

Here is the implementation of the migration code:

class VGGNet():
  def __init__(self, num_classes=2):  #num_classes, where the dichotomized value is 2
    super(VGGNet, self).__init__()
    net = models.vgg16(pretrained=True)  #Load VGG16 network parameters from pre-trained models
     = () # Leave the categorization layer empty, the following will change our categorization layer
     = net #Preserve the VGG16 feature layer
     = (  #Define your own classification layer
        (512 * 7 * 7, 512), #512 * 7 * 7 cannot be changed , determined by the VGG16 network, the second parameter is the number of neurons which can be fine-tuned.
        (True),
        (),
        (512, 128),
        (True),
        (),
        (128, num_classes),
    )

  def forward(self, x):
    x = (x)
    x = ((0), -1)
    x = (x)
    return x

The complete code is as follows

from __future__ import print_function, division

import torch
import  as nn
import  as F
import  as optim
from torchvision import datasets, transforms
from  import Variable
import numpy as np
from torchvision import models

batch_size = 16
learning_rate = 0.0002
epoch = 10

train_transforms = ([
  (224),
  (),
  (),
  ((.5, .5, .5), (.5, .5, .5))
])
val_transforms = ([
  (256),
  (224),
  (),
  ((.5, .5, .5), (.5, .5, .5))
])

train_dir = './VGGDataSet/train'
train_datasets = (train_dir, transform=train_transforms)
train_dataloader = (train_datasets, batch_size=batch_size, shuffle=True)

val_dir = './VGGDataSet/val'
val_datasets = (val_dir, transform=val_transforms)
val_dataloader = (val_datasets, batch_size=batch_size, shuffle=True)

class VGGNet():
  def __init__(self, num_classes=3):
    super(VGGNet, self).__init__()
    net = models.vgg16(pretrained=True)
     = ()
     = net
     = (
        (512 * 7 * 7, 512),
        (True),
        (),
        (512, 128),
        (True),
        (),
        (128, num_classes),
    )

  def forward(self, x):
    x = (x)
    x = ((0), -1)
    x = (x)
    return x

#-------------------- training process ---------------------------------
model = VGGNet()
if .is_available():
  ()
params = [{'params': ()} for md in ()
     if md in []]
optimizer = ((), lr=learning_rate)
loss_func = ()

Loss_list = []
Accuracy_list = []

for epoch in range(100):
  print('epoch {}'.format(epoch + 1))
  # training-----------------------------
  train_loss = 0.
  train_acc = 0.
  for batch_x, batch_y in train_dataloader:
    batch_x, batch_y = Variable(batch_x).cuda(), Variable(batch_y).cuda()
    out = model(batch_x)
    loss = loss_func(out, batch_y)
    train_loss += [0]
    pred = (out, 1)[1]
    train_correct = (pred == batch_y).sum()
    train_acc += train_correct.data[0]
    optimizer.zero_grad()
    ()
    ()
  print('Train Loss: {:.6f}, Acc: {:.6f}'.format(train_loss / (len(
    train_datasets)), train_acc / (len(train_datasets))))

  # evaluation--------------------------------
  ()
  eval_loss = 0.
  eval_acc = 0.
  for batch_x, batch_y in val_dataloader:
    batch_x, batch_y = Variable(batch_x, volatile=True).cuda(), Variable(batch_y, volatile=True).cuda()
    out = model(batch_x)
    loss = loss_func(out, batch_y)
    eval_loss += [0]
    pred = (out, 1)[1]
    num_correct = (pred == batch_y).sum()
    eval_acc += num_correct.data[0]
  print('Test Loss: {:.6f}, Acc: {:.6f}'.format(eval_loss / (len(
    val_datasets)), eval_acc / (len(val_datasets))))
    
	Loss_list.append(eval_loss / (len(val_datasets)))
  Accuracy_list.append(100 * eval_acc / (len(val_datasets)))

x1 = range(0, 100)
x2 = range(0, 100)
y1 = Accuracy_list
y2 = Loss_list
(2, 1, 1)
(x1, y1, 'o-')
('Test accuracy vs. epoches')
('Test accuracy')
(2, 1, 2)
(x2, y2, '.-')
('Test loss vs. epoches')
('Test loss')
()
# ("accuracy_loss.jpg")

Above this keras realize VGG16 CIFAR10 dataset way is all I share with you, I hope to give you a reference, and I hope you support me more.