Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Introduction

Setup

ANN

Working process ANN

Propagation

Bias parameter

Activation function

Loss function

Overfitting and Underfitting

Optimization function

Chain rule

Minima

Gradient problem

Weight initialization

Dropout

ANN Regression Exercise

ANN Classification Exercise

Hyper parameter tuning

CNN

CNN basics

Convolution

Padding

Pooling

Data argumentation

Flattening

Create Custom Dataset

Binary Classification Exercise

Multiclass Classification Exercise

Transfer learning

Transfer model Basic template

RNN

How RNN works

LSTM

Bidirectional RNN

Sequence to sequence

Attention model

Transformer model

Bag of words

Tokenization & Stop words

Stemming & Lemmatization

TF-IDF

N-Gram

Word embedding

Normalization

Pos tagging

Parser

semantic analysis

Regular expression

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Here we will use VGG16 for the example

Import Libraries

import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Flatten, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.applications.vgg16 import VGG16
from glob import glob
from keras.preprocessing import image
'''
Because my those four different images folder are present in main folder name data2. So I need the path of this data folder to access those images folder. So in directory variable we have the path of the data folders.
'''

base_dir=r"D:\data2"

Data preprocessing

#Data Preprocessing for training dataset
train_datagen=tf.keras.preprocessing.image.ImageDataGenerator( rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest', rotation_range=50, validation_split=0.1 )
'''
So here we will take each image and will shear, zoom, horizontal_flip,
fill_mode
, rotate the image. So after the work we will get 5 different images from one image.
So if we have to images then after preprocessing we will have 50 images. Here I used
validation split is 0.1. It means we will take 10% data for testing. Here we rescale
each image between 0 to 1, by doing this we are performing normalization.
'''


#Data Preprocessing for test dataset
test_datagen=tf.keras.preprocessing.image.ImageDataGenerator( rescale=1./255, validation_split=0.1 )
'''
For test data we will try to keep the images raw as much as possible. Because using this raw images will we see that is our model is working or not.
'''

Let's define the directory

#At first defined the directory for train data
train_datagen=train_datagen.flow_from_directory( base_dir, target_size=(224,224), #target size means image size batch_size=64, subset="training" )

#At first defined the directory for test data
test_datagen=test_datagen.flow_from_directory( base_dir, target_size=(224,224),
batch_size=64, subset="validation" )
#target size means image size

Creating input using VGG16

In VGG16 the input shape must be 224x224

IMAGE_SIZE=[224,224]
vgg=VGG16(input_shape=IMAGE_SIZE+[3],weights="imagenet",include_top=False)
vgg.output
'''
VGG16 is trained on a huge dataset name imagenet. To work with VGG16 we will use those weights on which vgg16 was trained using imagenet dataset. That's why here I wrote weight="imagenet. By writing include_top=False we disable the final layer of VGG16 because there so many class in vgg16 but in our case we will use our custom dataset and the number of class will be different."
'''

Disabling the layer of VGG16

for layer in vgg.layers:
layer.trainable=False
'''`
Look we don't the layer of VGG16. We need only the weights. So here we will disable the layers.
'''

Let's get our classes

folders=glob(r"D:\data2\*")
print(len(folders))

Train our data using vgg16 weights

x=Flatten()(vgg.output)
#here we are flattening the vgg16 output

#Let's create dense layer
prediction=Dense(len(folders),activation="softmax")(x)
'''
In folders variable we have our the class number. len(folders) means add the number of neurons in the dense layer according to the number of class. Because we have four classes so4 neurons will be added in the dense layer.
'''


model=Model(inputs=vgg.input,outputs=prediction)
model.summary()

Let's do compiling

model.compile(loss="binary_crossentropy",optimizer="adam",metrics=["accuracy"])

Let's fit the model

model.fit(train_datagen,steps_per_epoch=len(train_datagen), epochs=20, validation_data=test_datagen, validation_steps=len(test_datagen))

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.