Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Custom Models, Layers, and Loss Functions with TensorFlow/Week 4 - Custom Models/C1W4_Assignment.ipynb
Views: 13373
Week 4 Assignment: Create a VGG network
In this exercise, you will build a class that implements a VGG network and then train it to classify images of cats and dogs. The model will look something like this:
It is primarily made up of a series of Conv2D layers followed by a softmax activated layers to classify the image. As you can see, this will be a handful and the code will look huge if you specify each layer individually. As shown in the lectures, you can instead use model subclassing to build complex architectures. You can encapsulate repeating parts of a network then reuse that code when building the final model. You will get to practice that in this exercise. Let's get started!
Create named-variables dynamically
In this assignment, you will see the use of the Python function vars()
. This will allow you to use a for loop to define and set multiple variables with a similar name, such as var1, var2, var3.
Please go through the following examples to get familiar with vars()
, as you will use it when building the VGG model.
You'll start by defining a class
MyClass
It contains one variable
var1
.Create an object of type
MyClass
.
Python classes have an attribute called __dict__
.
__dict__
is a Python dictionary that contains the object's instance variables and values as key value pairs.
If you call vars()
and pass in an object, it will call the object's __dict__
attribute, which is a Python dictionary containing the object's instance variables and their values as ke
You may be familiar with adding new variable like this:
Here is another way that you can add an instance variable to an object, using vars()
.
Retrieve the Python dictionary
__dict__
of the object using vars(my_obj).Modify this
__dict__
dictionary using square bracket notation and passing in the variable's name as a string:['var3'] = 3
Why this is helpful!
You may be wondering why you would need another way to access an object's instance variables.
Notice that when using
vars()
, you can now pass in the name of the variablevar3
as a string.What if you plan to use several variables that are similarly named (
var4
,var5
...var9
) and wanted a convenient way to access them by incrementing a number?
Try this!
There are couple equivalent ways in Python to format a string. Here are two of those ways:
f-string: f"var{i}"
.format: "var{}".format(i)
You can access the variables of a class inside the class definition using vars(self)
You'll see this in the upcoming code. Now you'll start building the VGG network!
Create a generic VGG block (TODO)
The VGG Network has blocks of layers, where each block has a varied number of layers.
In order to create blocks of layers that have a customizable number of conv2D layers, you'll define a class
Block
, which can generate a customizable block of layers
__init__
In the constructor __init__
, store the conv2D parameters and also define the number of conv2D layers using the parameters passed into __init__
.
Store the filters, kernel_size, and repetitions as class variables so that they can be used later in the
call
function.Using a for loop, define a number of Conv2D Conv2D layers, based on the number of
repetitions
desired for this block.You can define each conv2D layer using
vars
and string formatting to create conv2D_0, conv2D_1, conv2D_3 etc.Set these four parameters of Conv2D:
filters
kernel_size
activation: set this to 'relu'
padding: set this to 'same' (default pading is 'valid').
Define the MaxPool2D layer that follows these Conv2D layers.
Set the following parameters for MaxPool2D:
pool_size: this will be a tuple with two values.
strides: this will also be a tuple with two values.
call
In call
, you will connect the layers together.
The 0-th conv2D layer,
conv2D_0
, immediately follows theinputs
.For conv2D layers 1,2 and onward, you can use a for loop to connect conv2D_1 to conv2D_0, and connect conv2D_2 to conv2D_1, and so on.
After connecting all of the conv2D_i layers, add connect the max_pool layer and return the max_pool layer.
All public tests passed
Create the Custom VGG network (TODO)
This model stack has a series of VGG blocks, which can be created using the Block
class that you defined earlier.
__init__
Recall that the
__init__
constructor ofBlock
takes several function parameters,filters, kernel_size, repetitions: you'll set these.
kernel_size and strides: you can use the default values.
For blocks a through e, build the blocks according to the following specifications:
block_a: 64 filters, kernel_size 3, repetitions 2
block_b: 128 filters, kernel_size 3, repetitions 2
block_c: 256 filters, kernel_size 3, repetitions 3
block_d: 512 filters, kernel_size 3, repetitions 3
block_e: 512 filters, kernel_size 3, repetitions 3
After block 'e', add the following layers:
flatten: use Flatten.
fc: create a fully connected layer using Dense. Give this 256 units, and a
'relu'
activation.classifier: create the classifier using a Dense layer. The number of units equals the number of classes. For multi-class classification, use a
'softmax'
activation.
call
Connect these layers together using the functional API syntax:
inputs
block_a
block_b
block_c
block_d
block_e
flatten
fc
classifier
Return the classifier layer.
All public tests passed
Load data and train the VGG network (Optional)
You can now load the dataset and proceed to train your VGG network.
This will take a few minutes to complete and is not required to complete the assignment.
You can submit your work before starting the training.