Sunteți pe pagina 1din 14

Introduction

1. At the heart of many computer vision tasks like image classi cation, object detection,
segmentation, etc. is a Convolutional Neural Network (CNN).
2. In CNN, there is an arrangement of convolutional, pooling and fully connected layers that
learns the patterns or features from set of input images.
3. The result of the arrangement of these layers is what we call an model architecture. The
architecture tells us about the size and parameters associated with the model.
4. The history of classi cation model tells us that accuracy improvements in subsequent
years came at the expense of an increased number of model parameters.
5. e.g. In 2012, AlexNet won the ImageNet Competition beating the nearest competitor by
nearly 10% in top-5 accuracy on ImageNet dataset. But it used a whopping 62 million
parameters!
6. GoogleNet, the winner of ImageNet Competition 2014, used only 6.8 million parameters
while being substantially more accurate than AlexNet.
7. So how did Googlenet with almost 1/10th parameter of Alexnet, performed better than
later. One of the reason is the model architecture itself.
8. We have seen for past several years, researchers are coming with new model architecture
that tries to solve problems like huge # of parameters, large size, learning complex non-
linear features, etc. We also want our model to run on any low performing devices like
mobile phones, edge devices and personal computers.
9. So how do we now design a network that is say half the size even though it is less
accurate? One of the way is model scaling.
10. The concept of model scaling is to use a standard network like GoogleNet or ResNet and
scale it up (i.e. use larger parameters) or down (i.e. use fewer parameters) by changing the
depth and/or width of the network, or the size of the input image.
11. CNNs are commonly developed at a xed resource cost, and then scaled up in order to
achieve better accuracy when more resources are made available. For example, ResNet
can be scaled up from ResNet-18 to ResNet-200 by increasing the number of layers.

Model Scaling

Following are few different ways, scaling can be achieved:

1. Change model depth: A CNN with a larger number of layers ( i.e. a deeper network ) can
hold richer details about the image and therefore is usually more accurate than a model
with a fewer number of layers ( i.e. a shallow network).

e.g. ResNet-18 and ResNet-200 are both based on the ResNet architecture, but ResNet-200
is much deeper than ResNet-18 and is, therefore, more accurate. On the other hand,
ResNet-18 is smaller and faster to run.

But there are a few problems with using very deep networks
They are di cult to train due to the vanishing gradient problem.
The gain in accuracy saturates after a certain depth.

2. Change model width: A CNN layer also have multiple channels much like the R, G, and B
channels of an image. A network with more channels per layer is considered wider than a
network with fewer channels.

e.g. WideResNet and MobileNets are not very deep but are wide.

3. Input image resolution: CNN architectures take in images of xed size as input. It is
obvious that a 512×512 image has more information than a 256×256 image. Therefore,
one can change the architecture to take in a larger input image and improve accuracy. This
increase in accuracy but requires more processing power because a 512×512 image has
4x more pixels compared to a 256×256 image.

E cientNet: Rethinking Model Scaling for Convolutional Neural Networks

Excerpt from the paper:

1. While these methods do improve accuracy, they usually require tedious manual tuning, and
still often yield suboptimal performance. What if, instead, we could nd a more principled
method to scale up a CNN to obtain better accuracy and e ciency?
2. We propose a novel model scaling method that uses a simple yet highly effective
compound coe cient to scale up CNNs in a more structured manner.
3. Compound Model Scaling: In order to understand the effect of scaling the network, we
systematically studied the impact of scaling different dimensions of the model. While
scaling individual dimensions improves model performance, we observed that balancing
all dimensions of the network—width, depth, and image resolution—against the available
resources would best improve overall performance.
The authors found that the choice of the initial model to scale makes a difference in the nal
output. So they developed their own baseline architecture and called it E cientNet-B0. Based
on this baseline model, they developed a family of networks called E cientNet. Following is the
comparison taken from the research paper.

For this presentation, I have implemented E cientNetB2 and compared it with Resent50.

Mounting Drive

1 from google.colab import drive
2 drive.mount('/content/drive')

Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client

Enter your authorization code:


··········
Mounted at /content/drive

Installing E cientNet
1 !pip install efficientnet

Collecting efficientnet
Downloading https://files.pythonhosted.org/packages/28/91/67848a143b54c3316
Requirement already satisfied: keras-applications<=1.0.8,>=1.0.7 in /usr/loca
Requirement already satisfied: scikit-image in /usr/local/lib/python3.6/dist-
Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages
Requirement already satisfied: numpy>=1.9.1 in /usr/local/lib/python3.6/dist-
Requirement already satisfied: imageio>=2.3.0 in /usr/local/lib/python3.6/dis
Requirement already satisfied: pillow>=4.3.0 in /usr/local/lib/python3.6/dist
Requirement already satisfied: scipy>=0.19.0 in /usr/local/lib/python3.6/dist
Requirement already satisfied: matplotlib!=3.0.0,>=2.0.0 in /usr/local/lib/py
Requirement already satisfied: PyWavelets>=0.4.0 in /usr/local/lib/python3.6/
Requirement already satisfied: networkx>=2.0 in /usr/local/lib/python3.6/dist
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /u
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3
Requirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.6/d
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-pa
Installing collected packages: efficientnet
Successfully installed efficientnet-1.1.0

Dependencies

1 %tensorflow_version 1.x
2 import tensorflow as tf
3 print(tf.__version__)
4  
5 tf.keras.backend.clear_session()
6  
7 import numpy as np
8 import os
9 import matplotlib.pyplot as plt
10  
11 from tensorflow.keras.models import Sequential
12 from tensorflow.keras.layers import Dense, Dropout, GlobalAveragePooling2D
13 from tensorflow.keras.optimizers import Adam
14 from tensorflow.keras.applications.resnet50 import ResNet50
15 from tensorflow.keras.preprocessing.image import ImageDataGenerator
16 from tensorflow.keras.applications.resnet50 import preprocess_input as keras_pp
17 from tensorflow.keras.callbacks import ReduceLROnPlateau
18  
19 from efficientnet.tfkeras import EfficientNetB2
20 from efficientnet.tfkeras import preprocess_input as eff_pp

TensorFlow 1.x selected.


1.15.2

Creating models
1 # constants for both model
2 input_shape = (224, 224, 3)
3 num_classes = 3
4  
5 def getModel(baseModel):
6  
7   return Sequential([
8                   baseModel,
9                   GlobalAveragePooling2D(name="global_avg_pool"),
10                   Dense(units=512, activation='relu', name='Dense_1'),
11                   Dropout(rate=0.2, name='Dropout_1'),
12                   Dense(units=256, activation='relu', name='Dense_2'),
13                   Dropout(rate=0.2, name='Dropout_2'),
14                   Dense(units = num_classes, activation='sigmoid', name='output
15               ])
16  
17 # Resnet50 model
18 resnet_base_model = ResNet50(weights="imagenet", include_top=False, input_shape=
19  
20 resnet_model = getModel(resnet_base_model)
21  
22 print('\n[INFO] Summary of resnet50 model..\n')
23 resnet_model.summary()
24  
25  
26 # efficientnetB2 model
27 eff_base_model = EfficientNetB2(weights="imagenet", include_top=False, input_sha
28  
29 effnet_model = getModel(eff_base_model)
30  
31 print('\n[INFO] Summary of efficientnetB2 model..\n')
32 effnet_model.summary()
WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/o
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
Downloading data from https://github.com/keras-team/keras-applications/releas
94773248/94765736 [==============================] - 3s 0us/step

[INFO] Summary of resnet50 model..

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
resnet50 (Model) (None, 7, 7, 2048) 23587712
_________________________________________________________________
global_avg_pool (GlobalAvera (None, 2048) 0
_________________________________________________________________
Dense_1 (Dense) (None, 512) 1049088
_________________________________________________________________
Dropout_1 (Dropout) (None, 512) 0
_________________________________________________________________
Dense_2 (Dense) (None, 256) 131328
_________________________________________________________________
Dropout_2 (Dropout) (None, 256) 0
_________________________________________________________________
output (Dense) (None, 3) 771
=================================================================
Total params: 24,768,899
Trainable params: 24,715,779
Non-trainable params: 53,120
_________________________________________________________________
WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/u
Instructions for updating:
`normal` is a deprecated alias for `truncated_normal`
Downloading data from https://github.com/Callidior/keras-applications/release
31940608/31936256 [==============================] - 1s 0us/step

[INFO] Summary of efficientnetB2 model..

Model: "sequential_1"
_________________________________________________________________
Training
LayerResnet50
(type) Model Output Shape Param #
=================================================================
efficientnet-b2 (Model) (None, 7, 7, 1408) 7768562
_________________________________________________________________
1 epochs = 30
global_avg_pool (GlobalAvera (None, 1408)
2 learningRate = 0.001 0
_________________________________________________________________
3 dataset_path = '/content/drive/My Drive/Colab Notebooks/datasets/classification/
Dense_1 (Dense) (None, 512) 721408
4  
_________________________________________________________________
5 resnet_datagen = ImageDataGenerator(brightness_range=(0.10, 0.25),
Dropout_1 (Dropout) (None, 512) 0
6                                     rotation_range=20,
_________________________________________________________________
7                                     preprocessing_function=keras_pp,
Dense_2 (Dense) (None, 256) 131328
8                                     validation_split=0.2)
_________________________________________________________________
9   Dropout_2 (Dropout) (None, 256) 0
_________________________________________________________________
10 resnet_train_gen = resnet_datagen.flow_from_directory(dataset_path,
output (Dense) (None, 3) 771
11                                                       batch_size=32,
=================================================================
12                                                       target_size=(224, 224),
Total params: 8,622,069
13                                                       subset='training')
Trainable params: 8,554,501
14   Non-trainable params: 67,568
15 resnet_val_gen = resnet_datagen.flow_from_directory(dataset_path,
_________________________________________________________________
16                                                   batch_size=32,
17                                                   target_size=(224, 224),
18                                                   subset='validation')
19  
20 # Adam optimisers
21 opt = Adam(lr=learningRate)
22  
23 callbacks = [ReduceLROnPlateau(patience=2, verbose=1)]
24  
25 # compiling model
26 print('[INFO] Compiling Resent50 model...')
27 resnet_model.compile(loss = 'categorical_crossentropy', optimizer = opt, metrics
28  
29 # training
30 print('[INFO] Training Resnet50...')
31 history_resnet = resnet_model.fit_generator(resnet_train_gen, epochs=epochs, val
Found 2016 images belonging to 3 classes.
Found 504 images belonging to 3 classes.
[INFO] Compiling Resent50 model...
[INFO] Training Resnet50...
Epoch 1/30
WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/o
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
62/63 [============================>.] - ETA: 13s - loss: 0.2268 - acc: 0.933
63/63 [==============================] - 1087s 17s/step - loss: 0.2234 - acc:
Epoch 2/30
62/63 [============================>.] - ETA: 0s - loss: 0.0694 - acc: 0.9808
63/63 [==============================] - 41s 657ms/step - loss: 0.0683 - acc:
Epoch 3/30
62/63 [============================>.] - ETA: 0s - loss: 0.1117 - acc: 0.9682
63/63 [==============================] - 42s 661ms/step - loss: 0.1100 - acc:
Epoch 4/30
62/63 [============================>.] - ETA: 0s - loss: 0.0018 - acc: 1.0000
63/63 [==============================] - 41s 650ms/step - loss: 0.0019 - acc:
Epoch 5/30
62/63 [============================>.] - ETA: 0s - loss: 5.1695e-04 - acc: 1.
63/63 [==============================] - 41s 656ms/step - loss: 5.1181e-04 -
Epoch 6/30
62/63 [============================>.] - ETA: 0s - loss: 9.6003e-05 - acc: 1.
63/63 [==============================] - 41s 652ms/step - loss: 9.4783e-05 -
Epoch 7/30
62/63 [============================>.] - ETA: 0s - loss: 0.0033 - acc: 0.9990
63/63 [==============================] - 41s 655ms/step - loss: 0.0073 - acc:
Epoch 8/30
62/63 [============================>.] - ETA: 0s - loss: 0.0864 - acc: 0.9824
63/63 [==============================] - 41s 651ms/step - loss: 0.0850 - acc:
Epoch 9/30
62/63 [============================>.] - ETA: 0s - loss: 0.0015 - acc: 1.0000
16/63 [======>.......................] - ETA: 34s - loss: 0.6569 - acc: 0.837
Epoch 00009: ReduceLROnPlateau reducing learning rate to 0.000100000004749745
63/63 [==============================] - 42s 670ms/step - loss: 0.0015 - acc:
Epoch 10/30
62/63 [============================>.] - ETA: 0s - loss: 0.0019 - acc: 0.9995
63/63 [==============================] - 41s 650ms/step - loss: 0.0018 - acc:
Epoch 11/30
62/63 [============================>.] - ETA: 0s - loss: 5.8039e-05 - acc: 1.
63/63 [==============================] - 41s 649ms/step - loss: 5.7299e-05 -
Epoch 12/30
62/63 [============================>.] - ETA: 0s - loss: 1.1858e-04 - acc: 1.
63/63 [==============================] - 41s 655ms/step - loss: 1.1674e-04 -
Epoch 13/30
62/63 [============================>.] - ETA: 0s - loss: 9.6781e-05 - acc: 1.
63/63 [==============================] - 42s 662ms/step - loss: 9.5270e-05 -
Epoch 14/30
62/63 [============================>.] - ETA: 0s - loss: 1.6926e-05 - acc: 1.
16/63 [======>.......................] - ETA: 35s - loss: 0.0251 - acc: 0.994
Epoch 00014: ReduceLROnPlateau reducing learning rate to 1.0000000474974514e-
63/63 [==============================] - 42s 663ms/step - loss: 1.6662e-05 -
Epoch 15/30
62/63 [============================>.] - ETA: 0s - loss: 2.1698e-05 - acc: 1.
63/63 [==============================] - 43s 677ms/step - loss: 2.1356e-05 -
Epoch 16/30
62/63 [============================>.] - ETA: 0s - loss: 7.2896e-05 - acc: 1.
16/63 [======>.......................] - ETA: 35s - loss: 0.0300 - acc: 0.988
Epoch 00016: ReduceLROnPlateau reducing learning rate to 1.0000000656873453e-
63/63 [==============================] - 42s 672ms/step - loss: 7.1925e-05 -
Epoch 17/30
62/63 [============================>.] - ETA: 0s - loss: 9.0496e-05 - acc: 1.
63/63 [==============================] - 42s 672ms/step - loss: 8.9438e-05 -
Epoch 18/30
62/63 [============================>.] - ETA: 0s - loss: 3.3039e-05 - acc: 1.
16/63 [======>.......................] - ETA: 35s - loss: 0.0100 - acc: 0.998
Epoch 00018: ReduceLROnPlateau reducing learning rate to 1.0000001111620805e-
63/63 [==============================] - 42s 671ms/step - loss: 3.3038e-05 -
Epoch 19/30
62/63 [============================>.] - ETA: 0s - loss: 6.6137e-05 - acc: 1.
63/63 [==============================] - 42s 671ms/step - loss: 6.5118e-05 -
Epoch 20/30
62/63 [============================>.] - ETA: 0s - loss: 1.2834e-04 - acc: 1.
16/63 [======>.......................] - ETA: 35s - loss: 0.0171 - acc: 0.990
Epoch 00020: ReduceLROnPlateau reducing learning rate to 1.000000082740371e-0
63/63E[==============================]
Training cient Net - 42s 674ms/step - loss: 1.2642e-04 -
Epoch 21/30
62/63 [============================>.] - ETA: 0s - loss: 8.5445e-05 - acc: 1.
63/63 [==============================] - 42s 669ms/step - loss: 8.4104e-05 -
1 effnet_datagen = ImageDataGenerator(brightness_range=(0.10, 0.25),
Epoch 22/30
2                                     rotation_range=20,
62/63 [============================>.] - ETA: 0s - loss: 1.7896e-04 - acc: 1.
16/63 [======>.......................] - ETA: 35s - loss: 0.0061 - acc: 0.996
3                                     preprocessing_function=eff_pp,
Epoch 00022: ReduceLROnPlateau reducing learning rate to 1.000000082740371e-0
4                                     validation_split=0.2)
5   63/63 [==============================] - 43s 676ms/step - loss: 1.7622e-04 -
Epoch 23/30
6 effnet_train_gen = effnet_datagen.flow_from_directory(dataset_path,
62/63 [============================>.] - ETA: 0s - loss: 1.0273e-04 - acc: 1.
7                                                       batch_size=32,
63/63 [==============================] - 42s 661ms/step - loss: 1.0110e-04 -
8                                                       target_size=(224, 224),
Epoch 24/30
9                                                       subset='training')
62/63 [============================>.] - ETA: 0s - loss: 1.5341e-04 - acc: 1.
10   16/63 [======>.......................] - ETA: 35s - loss: 0.0221 - acc: 0.990
Epoch 00024: ReduceLROnPlateau reducing learning rate to 1.000000082740371e-1
11 effnet_val_gen = effnet_datagen.flow_from_directory(dataset_path,
63/63 [==============================] - 43s 677ms/step - loss: 1.5100e-04 -
12                                                     batch_size=32,
Epoch 25/30
13                                                     target_size=(224, 224),
62/63 [============================>.] - ETA: 0s - loss: 0.0030 - acc: 0.9995
14                                                     subset='validation')
63/63 [==============================] - 42s 669ms/step - loss: 0.0029 - acc:
15  
Epoch 26/30
16 # Adam optimisers
62/63 [============================>.] - ETA: 0s - loss: 3.7102e-04 - acc: 1.
17 opt = Adam(lr=learningRate)
16/63 [======>.......................] - ETA: 34s - loss: 0.0064 - acc: 0.996
18   Epoch 00026: ReduceLROnPlateau reducing learning rate to 1.000000082740371e-1
19 callbacks = [ReduceLROnPlateau(patience=2, verbose=1)]
63/63 [==============================] - 42s 668ms/step - loss: 3.6514e-04 -
20   Epoch 27/30
62/63 [============================>.] - ETA: 0s - loss: 5.4154e-05 - acc: 1.
21 # compiling model
63/63 [==============================] - 41s 657ms/step - loss: 5.3297e-05 -
22 print('[INFO] Compiling EfficientNetB2 model...')
Epoch 28/30
23 effnet_model.compile(loss = 'categorical_crossentropy', optimizer = opt, metrics
62/63 [============================>.] - ETA: 0s - loss: 2.0696e-05 - acc: 1.
24  
16/63 [======>.......................] - ETA: 33s - loss: 0.0122 - acc: 0.994
25 # training
Epoch 00028: ReduceLROnPlateau reducing learning rate to 1.000000082740371e-1
26 print('[INFO] Training EfficientNetB2...')
63/63 [==============================] - 41s 656ms/step - loss: 2.0370e-05 -
27 history_effnet = effnet_model.fit_generator(effnet_train_gen, epochs=epochs, val
Epoch 29/30
62/63 [============================>.] - ETA: 0s - loss: 5.9346e-05 - acc: 1.
63/63 [==============================] - 41s 652ms/step - loss: 5.8406e-05 -
Epoch 30/30
62/63 [============================>.] - ETA: 0s - loss: 1.0067e-04 - acc: 1.
16/63 [======>.......................] - ETA: 34s - loss: 0.0153 - acc: 0.994
Epoch 00030: ReduceLROnPlateau reducing learning rate to 1.0000001044244145e-
63/63 [==============================] - 41s 657ms/step - loss: 1.0467e-04 -
Found 2016 images belonging to 3 classes.
Found 504 images belonging to 3 classes.
[INFO] Compiling EfficientNetB2 model...
[INFO] Training EfficientNetB2...
Epoch 1/30
62/63 [============================>.] - ETA: 0s - loss: 0.1540 - acc: 0.9441
63/63 [==============================] - 70s 1s/step - loss: 0.1515 - acc: 0.
Epoch 2/30
62/63 [============================>.] - ETA: 0s - loss: 0.0679 - acc: 0.9844
63/63 [==============================] - 45s 713ms/step - loss: 0.0670 - acc:
Epoch 3/30
62/63 [============================>.] - ETA: 0s - loss: 0.0037 - acc: 0.9985
63/63 [==============================] - 45s 720ms/step - loss: 0.0036 - acc:
Epoch 4/30
62/63 [============================>.] - ETA: 0s - loss: 0.0745 - acc: 0.9879
63/63 [==============================] - 45s 713ms/step - loss: 0.0743 - acc:
Epoch 5/30
62/63 [============================>.] - ETA: 0s - loss: 0.0390 - acc: 0.9934
16/63 [======>.......................] - ETA: 33s - loss: 0.0195 - acc: 0.994
Epoch 00005: ReduceLROnPlateau reducing learning rate to 0.000100000004749745
63/63 [==============================] - 47s 740ms/step - loss: 0.0384 - acc:
Epoch 6/30
62/63 [============================>.] - ETA: 0s - loss: 0.0065 - acc: 0.9965
63/63 [==============================] - 45s 719ms/step - loss: 0.0064 - acc:
Epoch 7/30
62/63 [============================>.] - ETA: 0s - loss: 0.0025 - acc: 0.9985
16/63 [======>.......................] - ETA: 33s - loss: 9.4581e-04 - acc: 1
Epoch 00007: ReduceLROnPlateau reducing learning rate to 1.0000000474974514e-
63/63 [==============================] - 45s 716ms/step - loss: 0.0025 - acc:
Epoch 8/30
62/63 [============================>.] - ETA: 0s - loss: 0.0018 - acc: 0.9995
63/63 [==============================] - 45s 714ms/step - loss: 0.0018 - acc:
Epoch 9/30
62/63 [============================>.] - ETA: 0s - loss: 4.4878e-04 - acc: 1.
16/63 [======>.......................] - ETA: 33s - loss: 4.5299e-04 - acc: 1
Epoch 00009: ReduceLROnPlateau reducing learning rate to 1.0000000656873453e-
63/63 [==============================] - 45s 708ms/step - loss: 4.4195e-04 -
Epoch 10/30
62/63 [============================>.] - ETA: 0s - loss: 4.1796e-04 - acc: 1.
63/63 [==============================] - 45s 719ms/step - loss: 4.1178e-04 -
Epoch 11/30
62/63 [============================>.] - ETA: 0s - loss: 8.9748e-04 - acc: 1.
16/63 [======>.......................] - ETA: 34s - loss: 5.2293e-04 - acc: 1
Epoch 00011: ReduceLROnPlateau reducing learning rate to 1.0000001111620805e-
63/63 [==============================] - 45s 716ms/step - loss: 9.1884e-04 -
Epoch 12/30
62/63 [============================>.] - ETA: 0s - loss: 0.0013 - acc: 0.9995
63/63 [==============================] - 45s 708ms/step - loss: 0.0013 - acc:
Epoch 13/30
62/63 [============================>.] - ETA: 0s - loss: 0.0022 - acc: 0.9995
16/63 [======>.......................] - ETA: 34s - loss: 5.7901e-04 - acc: 1
Epoch 00013: ReduceLROnPlateau reducing learning rate to 1.000000082740371e-0
63/63 [==============================] - 45s 721ms/step - loss: 0.0021 - acc:
Epoch 14/30
62/63 [============================>.] - ETA: 0s - loss: 8.4842e-04 - acc: 1.
63/63 [==============================] - 45s 711ms/step - loss: 8.3507e-04 -
Epoch 15/30
62/63 [============================>.] - ETA: 0s - loss: 3.3399e-04 - acc: 1.
16/63 [======>.......................] - ETA: 33s - loss: 3.4342e-04 - acc: 1
Epoch 00015: ReduceLROnPlateau reducing learning rate to 1.000000082740371e-0
63/63 [==============================] - 45s 716ms/step - loss: 3.3985e-04 -
Epoch 16/30
62/63 [============================>.] - ETA: 0s - loss: 9.3819e-04 - acc: 1.
63/63 [==============================] - 45s 715ms/step - loss: 9.2356e-04 -
Epoch 17/30
62/63 [============================>.] - ETA: 0s - loss: 0.0012 - acc: 0.9995
16/63 [======>.......................] - ETA: 33s - loss: 6.7204e-04 - acc: 1
Epoch 00017: ReduceLROnPlateau reducing learning rate to 1.000000082740371e-1
63/63 [==============================] - 45s 717ms/step - loss: 0.0011 - acc:
Epoch 18/30
62/63 [============================>.] - ETA: 0s - loss: 0.0015 - acc: 0.9995
63/63 [==============================] - 45s 714ms/step - loss: 0.0015 - acc:
Epoch 19/30
62/63 [============================>.] - ETA: 0s - loss: 6.4561e-04 - acc: 1.
16/63 [======>.......................] - ETA: 33s - loss: 5.1373e-04 - acc: 1
Epoch 00019: ReduceLROnPlateau reducing learning rate to 1.000000082740371e-1
63/63 [==============================] - 45s 718ms/step - loss: 6.3557e-04 -
Epoch 20/30
62/63 [============================>.] - ETA: 0s - loss: 5.0376e-04 - acc: 1.
63/63 [==============================] - 45s 718ms/step - loss: 7.1406e-04 -
Epoch 21/30
62/63 [============================>.] - ETA: 0s - loss: 7.8183e-04 - acc: 1.
16/63 [======>.......................] - ETA: 33s - loss: 4.3897e-04 - acc: 1
Epoch 00021: ReduceLROnPlateau reducing learning rate to 1.000000082740371e-1
63/63 [==============================] - 45s 716ms/step - loss: 7.7091e-04 -
Epoch 22/30
62/63 [============================>.] - ETA: 0s - loss: 5.6395e-04 - acc: 1.
63/63 [==============================] - 45s 715ms/step - loss: 5.5596e-04 -
Epoch 23/30
62/63 [============================>.] - ETA: 0s - loss: 9.6428e-04 - acc: 1.
16/63 [======>.......................] - ETA: 33s - loss: 3.3960e-04 - acc: 1
Epoch 00023: ReduceLROnPlateau reducing learning rate to 1.0000001044244145e-
63/63 [==============================] - 45s 718ms/step - loss: 9.4954e-04 -
Epoch 24/30
62/63 [============================>.] - ETA: 0s - loss: 5.3347e-04 - acc: 1.
63/63 [==============================] - 45s 712ms/step - loss: 5.2601e-04 -
Epoch 25/30
62/63 [============================>.] - ETA: 0s - loss: 6.8272e-04 - acc: 1.
16/63 [======>.......................] - ETA: 33s - loss: 5.7269e-04 - acc: 1
Epoch 00025: ReduceLROnPlateau reducing learning rate to 1.0000001179769417e-
63/63 [==============================] - 45s 714ms/step - loss: 6.7424e-04 -
Epoch 26/30
62/63 [============================>.] - ETA: 0s - loss: 3.6536e-04 - acc: 1.
63/63 [==============================] - 45s 720ms/step - loss: 3.5980e-04 -
Epoch 27/30
62/63 [============================>.] - ETA: 0s - loss: 7.7410e-04 - acc: 1.
16/63 [======>.......................] - ETA: 33s - loss: 7.7736e-04 - acc: 1
Epoch 00027: ReduceLROnPlateau reducing learning rate to 1.0000001518582595e-
63/63 [==============================] - 45s 716ms/step - loss: 7.6979e-04 -
Epoch 28/30
62/63 [============================>.] - ETA: 0s - loss: 5.7476e-04 - acc: 1.
Saving Models
63/63 [==============================] - 45s 711ms/step - loss: 5.9135e-04 -
Epoch 29/30
62/63 [============================>.] - ETA: 0s - loss: 0.0012 - acc: 0.9995
1 resnet_model.save('/content/drive/My Drive/Colab Notebooks/resnet50.h5')
16/63 [======>.......................] - ETA: 33s - loss: 7.0590e-04 - acc: 1
2 effnet_model.save('/content/drive/My Drive/Colab Notebooks/effnetB2.h5')
Epoch 00029: ReduceLROnPlateau reducing learning rate to 1.0000001095066122e-
3   63/63 [==============================] - 45s 714ms/step - loss: 0.0012 - acc:
Epoch 30/30
4 print("Size of Resnet50 model file = {} MB".format(os.path.getsize('/content/dri
62/63 [============================>.] - ETA: 0s - loss: 6.3676e-04 - acc: 1.
5 print("Size of EfficientNetB2 model file = {} MB".format(os.path.getsize('/conte
63/63 [==============================] - 45s 713ms/step - loss: 6.3266e-04 -
Size of Resnet50 model file = 297.62772 MB
Size of EfficientNetB2 model file = 104.20259999999999 MB
Plotting Training and validation accuracy

1 # Getting range or number of epochs actually trained.
2 N = np.arange(0, len(history_effnet.history["loss"]))
3  
4 def plot_graphs(title, metric):
5  
6   plt.figure(figsize=(15, 5))
7   plt.plot(N, history_effnet.history[metric], label="efficient_net_B2")
8   plt.plot(N, history_resnet.history[metric], label="resnet_50")
9   plt.xlabel("Epoch #")
10   plt.ylabel(title)
11   plt.legend()
12   plt.show()
13  
14  
15 plot_graphs("Training Accuracy", "acc")
16 plot_graphs("Training Loss", "loss")
17 plot_graphs("Validation Accuracy", "val_acc")
18 plot_graphs("Validation Loss", "val_loss")
1 print('\n[INFO] Evaluating Resent50 Model..\n')
2 loss, accuracy = resnet_model.evaluate(resnet_val_gen)
3 print('Loss: {} and Accuracy: {}'.format(loss, accuracy))
4  
5 print('\n[INFO] Evaluating EfficientNetB2 Model..\n')
6 loss, accuracy = effnet_model.evaluate(effnet_val_gen)
7 print('Loss: {} and Accuracy: {}'.format(loss, accuracy))

[INFO] Evaluating Resent50 Model..

16/16 [==============================] - 8s 478ms/step - loss: 0.0219 - acc:


Loss: 0.021850345341590582 and Accuracy: 0.9920634627342224

[INFO] Evaluating EfficientNetB2 Model..

16/16 [==============================] - 8s 496ms/step - loss: 6.2201e-04 - a


Loss: 0.0006220091796933502 and Accuracy: 1.0

S-ar putea să vă placă și