MLOPS TASK 4

MLOPS TASK 4

Transfer Learning with VGG16 model

Transfer Learning

Transfer learning (TL) is a research problem in machine learning that focuses on storing knowledge gained while solving one problem and applying it to a different but related problem. For example, knowledge gained while learning to recognize cars could apply when trying to recognize trucks. This area of research bears some relation to the long history of psychological literature on transfer of learning, although formal ties between the two fields are limited. From the practical standpoint, reusing or transferring information from previously learned tasks for the learning of new tasks has the potential to significantly improve the sample efficiency of a reinforcement learning agent.

No alt text provided for this image

ABOUT VGG16 MODEL

VGG16 is a convolutional neural network model proposed by K. Simonyan and A. Zisserman from the University of Oxford in the paper “Very Deep Convolutional Networks for Large-Scale Image Recognition”. The model achieves 92.7% top-5 test accuracy in ImageNet, which is a dataset of over 14 million images belonging to 1000 classes. It was one of the famous model submitted to ILSVRC-2014. It makes the improvement over AlexNet by replacing large kernel-sized filters (11 and 5 in the first and second convolutional layer, respectively) with multiple 3×3 kernel-sized filters one after another. VGG16 was trained for weeks and was using NVIDIA Titan Black GPU’s.

ARCHITECTURE OF VGG16 MODEL


No alt text provided for this image

FILE STRUCTURE FOR STORING DATA


No alt text provided for this image


DATA COLLECTION USING DATACOLLECTOR.PY CODE

import os 
import cv2

def photo(maxcount):
  #maxcount means how many photos you want to click

    folder​=r"foldername" 

    #for example : folder for testing:
    # "D:/data/train/foldername/"
    # "D:/data/test/foldername/"
    
    
    cap=cv2.VideoCapture(0)
    count=0
    while True:
        status,image=cap.read()
        if status:
            count=count+1
            image=image[100:500,200:700]
            file="file{0}".format(count) + ".jpg"
            file=os.path.join(folder,file)
            cv2.imwrite(file,image)
            cv2.putText(image,str(count),(150,150),cv2.FONT_HERSHEY_SIMPLEX,1,(255,234,0),2)
            cv2.imshow("photo",image)

        
        
            if cv2.waitKey(100) == ord('q') or count == int(max_count): 
                break
    cv2.destroyAllWindows()
    print('Total photo clicked {0}'.format(count))
    cap.release()

FACE RECOGNIZATION AND DATA AUGMENTATION

Data augmentation is a strategy that enables practitioners to significantly increase the diversity of data available for training models, without actually collecting new dataData augmentation techniques such as cropping, padding, and horizontal flipping are commonly used to train large neural networks.

No alt text provided for this image
from keras.applications import vgg16

model=vgg16.VGG16(weights="imagenet",include_top=False,input_shape=(224,224,3))

for layer in model.layers:
  layer.trainable=False

model.summary()

from keras.layers import Dense,Flatten,Dropout
def addTopModel(bottom_model, num_classes, D=256):
    """creates the top or head of the model that will be 
    placed ontop of the bottom layers"""
    top_model = bottom_model.output
    top_model = Flatten(name = "flatten")(top_model)
    top_model = Dense(D, activation = "relu")(top_model)
    top_model = Dropout(0.3)(top_model)
    top_model = Dense(num_classes, activation = "softmax")(top_model)
    return top_model
    
model.input
model.layers

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
from keras.layers.normalization import BatchNormalization
from keras.models import Model

num_classes = 2

FC_Head = addTopModel(model, num_classes)

modelnew = Model(inputs=model.input, outputs=FC_Head)

print(modelnew.summary())

from keras.preprocessing.image import ImageDataGenerator

train_data_dir=r"drive/My Drive/data/train"
validation_data_dir=r"drive/My Drive/data/test"

train_datagen=ImageDataGenerator(
      rescale=1./255,
      rotation_range=20,
      width_shift_range=0.2,
      height_shift_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest')
 
validation_datagen = ImageDataGenerator(rescale=1./255)
img_rows=224
img_cols=224

train_batchsize = 64
val_batchsize = 64
 
train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_rows, img_cols),
        batch_size=train_batchsize,
        class_mode='categorical')
 
validation_generator = validation_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_rows, img_cols),
        batch_size=val_batchsize,
        class_mode='categorical',
        shuffle=False)

from keras.optimizers import RMSprop
from keras.callbacks import ModelCheckpoint, EarlyStopping
                   
checkpoint = ModelCheckpoint("facemodel.h5",
                             monitor="val_loss",
                             mode="min",
                             save_best_only = True,
                             verbose=1)

earlystop = EarlyStopping(monitor = 'val_loss', 
                          min_delta = 0, 
                          patience = 3,
                          verbose = 1,
                          restore_best_weights = True)

# we put our call backs into a callback list
callbacks = [earlystop, checkpoint]

# Note we use a very small learning rate 
modelnew.compile(loss = 'categorical_crossentropy',
              optimizer = RMSprop(lr = 0.001),
              metrics = ['accuracy'])

nb_train_samples = 544
nb_validation_samples = 200
epochs = 3
batch_size = 64

history = modelnew.fit_generator(
    train_generator,
    steps_per_epoch = nb_train_samples // batch_size,
    epochs = epochs,
    callbacks = callbacks,
    validation_data = validation_generator,
    validation_steps = nb_validation_samples // batch_size)


modelnew.save("facemodel.h5")
No alt text provided for this image

model.summary()

No alt text provided for this image


TESTING:

from keras.applications.vgg16 import preprocess_input
from keras.preprocessing import image
from keras.models import load_model
modelnew=load_model("facemodel.h5")


oimg=image.load_img(r"testingfoldername",target_size=(224,224))
oimg.size

img=image.img_to_array(oimg)
import numpy as np
img.shape

img=np.expand_dims(img,axis=0)
img.shape
img=modelnew.predict(img)

# from keras.applications.vgg16 import decode_predictions
# decode_predictions(img)

img.shape
img

if img[0][0]==1 and img[0][1]==0:
  char="OBJECT1"
elif img[0][0]==0 and img[0][1]==1:
  char="OBJECT2"
else:
  char="DONT KNOW"
# from keras.applications.vgg16 import decode_predictions
# decode_predictions(img)

print(char)

image.array_to_img(oimg)




To view or add a comment, sign in

More articles by Raghav Gupta

  • What actually Containers are?

    If you are familiar with containers, you might know Docker, What actually Docker is? Is it a Container Engine or a…

    2 Comments
  • Provisioning and Configuring Jenkins Dynamic Slaves on AWS EC2 Using Ansible

    Jenkins: Jenkins is a free and open-source automation server. It helps automate the parts of software development…

    2 Comments
  • Kubernetes Industry Use Case- OpenAI

    OpenAI is an artificial intelligence research laboratory consisting of the for-profit corporation OpenAI LP and its…

  • BOSCH And Azure AKS

    PROBLEM: THE WRONG WAY DRIVING PROBLEM The problem of drivers going the wrong way on highways, the goal was to save…

  • ARTH TASK 3

    TASK DESCRIPTION: 🔅 Create a key pair 🔅 Create a security group 🔅 Launch an instance using the above created key…

    2 Comments
  • Slack Case Study - AWS

    Slack provides a messaging platform that integrates with and unifies a wide range of communications services such as…

    2 Comments
  • INTRO TO BIG DATA AND DISTRIBUTED STORAGE CLUSTERS

    What is Data? The quantities, characters, or symbols on which operations are performed by a computer, which may be…

    2 Comments
  • HYBRID MULTI CLOUD TASK 6

    TASK DESCRIPTION: Deploy the Wordpress application on Kubernetes and AWS using terraform including the following steps;…

  • HYBRID MULTI CLOUD TASK 2

    Task-Description: Create/launch Application using Terraform 1. Create a Security group that allows the port 80.

  • Ansible Task 3

    𝗧𝗮𝘀𝗸 3:- ♦️ Provision EC2 Instances Through Ansible. ♦️ Retrieve The IP Address of Instances Using The Dynamic…

    1 Comment

Others also viewed

Explore content categories