0% found this document useful (0 votes)
120 views8 pages

BreastCancer EXP

The document discusses several pre-trained models including ResNet50, InceptionV3, and VGG19. For ResNet50, it explains that ResNet has many variants with different numbers of layers, and ResNet50 uses 50 layers. It also provides information on preprocessing inputs for ResNet models. For InceptionV3, it discusses loading pretrained weights into the model and setting layers to non-trainable. For VGG19, it notes the model has convolutional and pooling layers and provides an example of creating a sequential model with VGG19 as the base model.

Uploaded by

Amodha InfoTech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views8 pages

BreastCancer EXP

The document discusses several pre-trained models including ResNet50, InceptionV3, and VGG19. For ResNet50, it explains that ResNet has many variants with different numbers of layers, and ResNet50 uses 50 layers. It also provides information on preprocessing inputs for ResNet models. For InceptionV3, it discusses loading pretrained weights into the model and setting layers to non-trainable. For VGG19, it notes the model has convolutional and pooling layers and provides an example of creating a sequential model with VGG19 as the base model.

Uploaded by

Amodha InfoTech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

RESNET50

ResNet has many variants that run on the same concept but have different numbers of layers.
Resnet50 is used to denote the variant that can work with 50 neural network layers.

ResNet has many variants that run on the same concept but have different numbers of layers.
Resnet50 is used to denote the variant that can work with 50 neural network layers.

Each Keras Application expects a specific kind of input preprocessing. For ResNet, call
[Link].preprocess_input on your inputs before passing them to the model.
resnet.preprocess_input will convert the input images from RGB to BGR, then will zero-
center each color channel with respect to the ImageNet dataset, without scaling.

[Link].resnet50.preprocess_input

Preprocesses a tensor or Numpy array encoding a batch of images.

from [Link] import ResNet50


Base_Model = ResNet50(input_shape=(224, 224,3),
include_top=False, weights="imagenet",
pooling='max')
for layer in Base_Model.layers:
[Link] = False

For transfer learning

Note: each Keras Application expects a specific kind of input preprocessing. For ResNet,
call [Link].preprocess_input on your inputs before passing them to the
model. resnet.preprocess_input will convert the input images from RGB to BGR, then will
zero-center each color channel with respect to the ImageNet dataset, without scaling.

Arguments

 include_top: whether to include the fully-connected layer at the top of the network.
 weights: one of None (random initialization), 'imagenet' (pre-training on ImageNet),
or the path to the weights file to be loaded.
 input_shape: optional shape tuple, only to be specified if include_top is False
(otherwise the input shape has to be (224, 224, 3) (with 'channels_last' data format)
or (3, 224, 224) (with 'channels_first' data format). It should have exactly 3 inputs
channels, and width and height should be no smaller than 32. E.g. (200, 200, 3) would
be one valid value.
 pooling: Optional pooling mode for feature extraction when include_top is False.
o None means that the output of the model will be the 4D tensor output of the
last convolutional block.
o avg means that global average pooling will be applied to the output of the last
convolutional block, and thus the output of the model will be a 2D tensor.
o max means that global max pooling will be applied.
 classes: optional number of classes to classify images into, only to be specified
if include_top is True, and if no weights argument is specified.
 classifier_activation: A str or callable. The activation function to use on the "top"
layer. Ignored unless include_top=True. Set classifier_activation=None to return the
logits of the "top" layer. When loading pretrained weights, classifier_activation can
only be None or "softmax".

 While importing the ResNet50 class, we mention include_top=False. This ensures


that we can add our own custom input and output layers according to our data.

 We mention the weights='imagenet'. This means that the Resnet50 model will use
the weights it learnt while being trained on the imagenet  data.

 Finally, we mention [Link]= False in the pretrained model. This ensures


that the model does not learn the weights again, saving us a lot of time and space
complexity.

ResNet50_Model = Sequential()
ResNet50_Model.add (Base_Model)
ResNet50_Model.add (Dense(2, activation='softmax'))
ResNet50_Model.summary() # print summary my model
Usually, we use the softmax activation function to do classification tasks, and the output
width will be the number of the categories. This means that if you want to classify one object
into two categories with the labels A,B you would need to make the Dense layer generate an
output with a shape of (None, 2). Then you can use the cross_entropy loss function to
calculate the LOSS, automatically calculate the gradient, and do the back-propagation
process.

ResNet50_Model.compile(loss='sparse_categorical_crossentropy’,optimizer='adam’,
metrics=['accuracy'])
print ("Model Compile Done")

Multiclass classification

Problems involving the prediction of more than one class uses loss functions.

Sparse Categorical Crossentropy

If you have two or more classes and the labels are integers, the
SparseCategoricalCrossentropy should be used. 

Optimizer that implements the Adam.

Adam optimization is a stochastic gradient descent method that is based on adaptive


estimation of first-order and second-order moments.
Inception V3
from [Link] import layers
from [Link].inception_v3 import InceptionV3
local_weights_file = './inception_v3.h5'
pre_trained_model = InceptionV3(input_shape = (224, 224, 3),
include_top = False,
weights = None)
pre_trained_model.load_weights(local_weights_file)
for layer in pre_trained_model.layers:
[Link] = False

Load Pretrained Model


InceptionV3 is pretrained model. InceptionV3 returns a skeleton of model
and load_weights load pretrained model weights into the skeleton.

Set pre_model architecture (also known as base_model)

Two parameters of the inception V3 model are more important. One is weights. If it is'
Imagenet ', Keras will automatically download the parameters that have been trained on
Imagenet. At present, there are only two options for this parameter.

pre_trained_model = InceptionV3(input_shape = (224, 224, 3),


include_top = False,
weights = None)

pre_trained_model.load_weights(local_weights_file)

Freeze pre_trained_model all layers, so that the skeleton model is no longer trained

for layer in pre_trained_model.layers:


[Link] = False

Because the network structure is too long, only part is shown here

pre_trained_model.summary()

Here, we do not use all levels of inception V3, but let the network take the mixed 7 layer as
the output and connect to our newly added network
from [Link] import Model

last_layer = pre_trained_model.get_layer('mixed7')

print('last layer output shape: ', last_layer.output_shape)

last_output = last_layer.output

x = [Link]()(last_output)

x = [Link](1024, activation='relu')(x)

x = [Link](0.2)(x)

x = [Link](6, activation='softmax')(x)

InceptionV3_Model = Model (pre_trained_model.input, x)

Add our own layers to the network and use dropout to reduce over fitting

InceptionV3_Model.compile(optimizer = RMSprop(learning_rate=0.0001),

The gist of RMSprop is to:

 Maintain a moving (discounted) average of the square of gradients


 Divide the gradient by the root of this average

This implementation of RMSprop uses plain momentum.

The centred version additionally maintains a moving average of the gradients, and uses that
average to estimate the variance.

Arguments

 learning_rate: A Tensor, floating point value, or a schedule that is


a [Link], or a callable that takes no
arguments and returns the actual value to use. The learning rate. Defaults to 0.0001.

VGG19

Very Deep Convolutional Networks for Large-Scale Image Recognition (VGG-16)


The VGG-16 is one of the most popular pre-trained models for image classification.

Introduced in the famous ILSVRC 2014 Conference, it was and remains THE model to beat

even today. Developed at the Visual Graphics Group at the University of Oxford, VGG-16

beat the then standard of AlexNet and was quickly adopted by researchers and the industry

for their image Classification Tasks.

Here is the architecture of VGG-16:

Here is a more intuitive layout of the VGG-16 Model.


The following are the layers of the model:

 Convolutional Layers = 13
 Pooling Layers = 5
 Dense Layers = 3

Base_Model = VGG19(input_shape=(224, 224,3),


include_top=False, weights="imagenet",
pooling='max')
Vgg19_Model = Sequential ()
Vgg19_Model.add (Base_Model)
Vgg19_Model.add (Flatten())
Vgg19_Model.add (Dense (units=64, activation="relu"))
# Add output layer
Vgg19_Model.add (Dense (units=2, activation="SoftMax"))
Vgg19_Model.summary()

The input size for this model is 224x224.


pooling="max" for global average or max pooling.
2-unit dense layer in the end with softmax activation as 2 classes to predict from in the end.
The softmax layer will output the value between 0 and 1 based on the confidence of the model
that which class the images belongs to.

After the creation of SoftMax layer the model is finally prepared.

(64, activation="relu")
The "layer call" action is like drawing an arrow from "inputs" to this layer you
created. You're "passing" the inputs to the dense layer.

You might also like