Skip to content

Commit

Permalink
edits to py for only droput; add comments to md
Browse files Browse the repository at this point in the history
  • Loading branch information
erinmgraham committed Oct 11, 2023
1 parent b7ae3ab commit 90fc470
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 41 deletions.
6 changes: 3 additions & 3 deletions episodes/03-build-cnn.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ inputs_intro = keras.Input(shape=train_images.shape[1:])

# Convolutional layer with 50 filters, 3x3 kernel size, and ReLU activation
x_intro = keras.layers.Conv2D(50, (3, 3), activation='relu')(inputs_intro)
# Second Convolutional layer
# Second Convolutional layer with 50 filters, 3x3 kernel size, and ReLU activation
x_intro = keras.layers.Conv2D(50, (3, 3), activation='relu')(x_intro)
# Flatten layer to convert 2D feature maps into a 1D vector
x_intro = keras.layers.Flatten()(x_intro)
Expand Down Expand Up @@ -441,11 +441,11 @@ inputs_pool = keras.Input(shape=train_images.shape[1:])
# CNN Part 2
# Convolutional layer with 50 filters, 3x3 kernel size, and ReLU activation
x_pool = keras.layers.Conv2D(50, (3, 3), activation='relu')(inputs_pool)
# Pooling layer
# Pooling layer with input window sized 2,2
x_pool = keras.layers.MaxPooling2D((2, 2))(x_pool)
# Second Convolutional layer with 50 filters, 3x3 kernel size, and ReLU activation
x_pool = keras.layers.Conv2D(50, (3, 3), activation='relu')(x_pool)
# Second Pooling layer
# Second Pooling layer with input window sized 2,2
x_pool = keras.layers.MaxPooling2D((2, 2))(x_pool)
# Flatten layer to convert 2D feature maps into a 1D vector
x_pool = keras.layers.Flatten()(x_pool)
Expand Down
42 changes: 31 additions & 11 deletions episodes/04-fit-cnn.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exercises: 2

- Explain the difference between compiling and training a CNN
- Know how to select a loss function for your model
- Understand what an optimizer is and be familiar with advantages and disadvantages of different optimizers
- Understand what an optimizer is
- Define the terms: learning rate, batch size, epoch
- Explain overfitting

Expand All @@ -34,7 +34,8 @@ We now need to select an appropriate optimizer and loss function that we will us

Recall how we compiled our model in the introduction:
```
#model.compile(optimizer = 'adam',
# compile the pooling model
#model_pool.compile(optimizer = 'adam',
# loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True),
# metrics = ['accuracy'])
```
Expand Down Expand Up @@ -102,7 +103,8 @@ Somewhat coupled to the loss function is the optimizer. The optimizer here refer
We need to choose which optimizer to use and, if this optimizer has parameters, what values to use for those. Furthermore, we need to specify how many times to show the training samples to the optimizer.

```
#model.compile(optimizer = 'adam',
# compile the pooling model
#model_pool.compile(optimizer = 'adam',
# loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True),
# metrics = ['accuracy'])
```
Expand Down Expand Up @@ -152,7 +154,8 @@ Lastly, we can observe below that a modest learning rate will ensure that the pr
After we select the desired optimizer and loss function we want to specify the metric(s) to be evaluated by the model during training and testing. A **metric** is a function that is used to judge the performance of your model.

```
#model.compile(optimizer = adam',
# compile the pooling model
#model_pool.compile(optimizer = adam',
# loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True),
# metrics = ['accuracy'])
```
Expand All @@ -179,6 +182,7 @@ A training **epoch** means that every sample in the training data has been shown
We want to train our model for 10 epochs:

```
# fit the pooling model
#history_pool = model_pool.fit(train_images, train_labels, epochs=10, validation_data=(val_images, val_labels))
```

Expand Down Expand Up @@ -279,17 +283,35 @@ The intuition behind dropout is that it enforces redundancies in the network by
Let us add one dropout layer towards the end of the network, that randomly drops 20% of the input units.

```python
# define the inputs, layers, and outputs of a CNN model with dropout

# CNN Part 1
# Input layer of 32x32 images with three channels (RGB)
inputs_dropout = keras.Input(shape=train_images.shape[1:])

# CNN Part 2
# Convolutional layer with 50 filters, 3x3 kernel size, and ReLU activation
x_dropout = keras.layers.Conv2D(50, (3, 3), activation='relu')(inputs_dropout)
# Pooling layer with input window sized 2,2
x_dropout = keras.layers.MaxPooling2D((2, 2))(x_dropout)
# Second Convolutional layer with 50 filters, 3x3 kernel size, and ReLU activation
x_dropout = keras.layers.Conv2D(50, (3, 3), activation='relu')(x_dropout)
# Second Pooling layer with input window sized 2,2
x_dropout = keras.layers.MaxPooling2D((2, 2))(x_dropout)
# Third Convolutional layer with 50 filters, 3x3 kernel size, and ReLU activation
x_dropout = keras.layers.Conv2D(50, (3, 3), activation='relu')(x_dropout)
# Dropout layer andomly drops 20% of the input units
x_dropout = keras.layers.Dropout(0.8)(x_dropout) # This is new!
# Flatten layer to convert 2D feature maps into a 1D vector
x_dropout = keras.layers.Flatten()(x_dropout)
# Dense layer with 50 neurons and ReLU activation
x_dropout = keras.layers.Dense(50, activation='relu')(x_dropout)

# CNN Part 3
# Output layer with 10 units (one for each class)
outputs_dropout = keras.layers.Dense(10)(x_dropout)

# create the dropout model
model_dropout = keras.Model(inputs=inputs_dropout, outputs=outputs_dropout, name="cifar_model_dropout")

model_dropout.summary()
Expand Down Expand Up @@ -333,10 +355,12 @@ We can see that the dropout does not alter the dimensions of the image, and has
We again compile and train the model.

```python
# compile the dropout model
model_dropout.compile(optimizer = 'adam',
loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics = ['accuracy'])

# fit the dropout model
history_dropout = model_dropout.fit(train_images, train_labels, epochs=20,
validation_data=(val_images, val_labels))

Expand All @@ -347,8 +371,10 @@ model_dropout.save('fit_outputs/model_dropout.h5')
And inspect the training results:

```python
# convert the history to a dataframe for plotting
history_dropout_df = pd.DataFrame.from_dict(history_dropout.history)

# plot the loss and accuracy from the training process
fig, axes = plt.subplots(1, 2)
fig.suptitle('cifar_model_dropout')
sns.lineplot(ax=axes[0], data=history_dropout_df[['loss', 'val_loss']])
Expand Down Expand Up @@ -394,7 +420,7 @@ By using regularization techniques, you can improve the generalization performan

::::::::::::::::::::::::::::::::::::: challenge

## Vary dropout rate
Vary dropout rate

Q1. What do you think would happen if you lower the dropout rate? Try it out, and see how it affects the model training.

Expand Down Expand Up @@ -456,12 +482,6 @@ This is called hyperparameter tuning.

Based on our evaluation of the loss and accuracy metrics, the `model_dropout` appears to have the best performance **of the models we have examined thus far**. The next step is to use these models to predict object classes on our test dataset.

Make sure you save the model weights!

```python
# save dropout model
model_dropout.save('model_dropout.h5')
```

::::::::::::::::::::::::::::::::::::: keypoints

Expand Down
1 change: 1 addition & 0 deletions episodes/05-evaluate-predict-cnn.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exercises: 2
- Use a convolutional neural network (CNN) to make a prediction (ie classify an image)
- Explain how to measure the performance of a CNN
- Explain hyperparameter tuning
- Be familiar with advantages and disadvantages of different optimizers
- Understand what steps to take to improve model accuracy

::::::::::::::::::::::::::::::::::::::::::::::::
Expand Down
6 changes: 3 additions & 3 deletions episodes/scripts/build_ep_pool_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@
# CNN Part 2
# Convolutional layer with 50 filters, 3x3 kernel size, and ReLU activation
x_pool = keras.layers.Conv2D(50, (3, 3), activation='relu')(inputs_pool)
# Pooling layer
# Pooling layer with input window sized 2,2
x_pool = keras.layers.MaxPooling2D((2, 2))(x_pool)
# Second Convolutional layer with 50 filters, 3x3 kernel size, and ReLU activation
x_pool = keras.layers.Conv2D(50, (3, 3), activation='relu')(x_pool)
# Second Pooling layer
# Second Pooling layer with input window sized 2,2
x_pool = keras.layers.MaxPooling2D((2, 2))(x_pool)
# Flatten layer to convert 2D feature maps into a 1D vector
x_pool = keras.layers.Flatten()(x_pool)
# Dense layer
# Dense layer with 50 neurons and ReLU activation
x_pool = keras.layers.Dense(50, activation='relu')(x_pool)

# CNN Part 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,89 @@
"""

from tensorflow import keras
(train_images, train_labels), (val_images, val_labels) = keras.datasets.cifar10.load_data()

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import time

start = time.time()

#model.compile(optimizer = 'adam',
# **loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True)**,
# metrics = ['accuracy'])

# Dropout
# load the cifar dataset included with the keras packages
(train_images, train_labels), (val_images, val_labels) = keras.datasets.cifar10.load_data()

#model_ex.compile(loss = 'mse')
# Recall how we compiled our intro and pooling models:

#model_pool.compile(optimizer = 'adam',
# loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True),
# metrics = ['accuracy'])

#history = model.fit(train_images, train_labels, epochs=10, validation_data=(val_images, val_labels))
# convert the history to a dataframe for plotting

# NOTE this should already be in memory from previous episode
####
####
#### return to build_ep_pool_model.py if you need to recreate/rerun the model_pool
#### and view the training output
####

#history_pool = model_pool.fit(train_images, train_labels, epochs=10, validation_data=(val_images, val_labels))
# Dropout Layers

history_pool_df = pd.DataFrame.from_dict(history_pool.history)

# plot the loss and accuracy from the training process
fig, axes = plt.subplots(1, 2)
fig.suptitle('cifar_model_pool')
sns.lineplot(ax=axes[0], data=history_pool_df[['loss', 'val_loss']])
sns.lineplot(ax=axes[1], data=history_pool_df[['accuracy', 'val_accuracy']])

# Dropout
# define the inputs, layers, and outputs of a CNN model with dropout

# CNN Part 1
# Input layer of 32x32 images with three channels (RGB)
inputs_dropout = keras.Input(shape=train_images.shape[1:])

# CNN Part 2
# Convolutional layer with 50 filters, 3x3 kernel size, and ReLU activation
x_dropout = keras.layers.Conv2D(50, (3, 3), activation='relu')(inputs_dropout)
# Pooling layer with input window sized 2,2
x_dropout = keras.layers.MaxPooling2D((2, 2))(x_dropout)
# Second Convolutional layer with 50 filters, 3x3 kernel size, and ReLU activation
x_dropout = keras.layers.Conv2D(50, (3, 3), activation='relu')(x_dropout)
# Second Pooling layer with input window sized 2,2
x_dropout = keras.layers.MaxPooling2D((2, 2))(x_dropout)
# Third Convolutional layer with 50 filters, 3x3 kernel size, and ReLU activation
x_dropout = keras.layers.Conv2D(50, (3, 3), activation='relu')(x_dropout)
# Dropout layer andomly drops 20% of the input units
x_dropout = keras.layers.Dropout(0.8)(x_dropout) # This is new!
# Flatten layer to convert 2D feature maps into a 1D vector
x_dropout = keras.layers.Flatten()(x_dropout)
# Dense layer with 50 neurons and ReLU activation
x_dropout = keras.layers.Dense(50, activation='relu')(x_dropout)

# CNN Part 3
# Output layer with 10 units (one for each class)
outputs_dropout = keras.layers.Dense(10)(x_dropout)

# create the dropout model
model_dropout = keras.Model(inputs=inputs_dropout, outputs=outputs_dropout, name="cifar_model_dropout")

# view the dropout model summary
model_dropout.summary()


# compile the dropout model
model_dropout.compile(optimizer='adam',
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])

# fit the dropout model
history_dropout = model_dropout.fit(train_images, train_labels, epochs=20,
validation_data=(val_images, val_labels))

# save dropout model
model_dropout.save('fit_outputs/model_dropout.h5')

# convert the history to a dataframe for plotting
history_dropout_df = pd.DataFrame.from_dict(history_dropout.history)

# plot the loss and accuracy from the training process
fig, axes = plt.subplots(1, 2)
fig.suptitle('cifar_model_dropout')
sns.lineplot(ax=axes[0], data=history_dropout_df[['loss', 'val_loss']])
sns.lineplot(ax=axes[1], data=history_dropout_df[['accuracy', 'val_accuracy']])

val_loss, val_acc = model_dropout.evaluate(val_images, val_labels, verbose=2)

plt.show() #Force a new plot to be created

########################################################
# Challenge Vary Dropout Rate

dropout_rates = [0.15, 0.3, 0.45, 0.6, 0.75]
Expand Down Expand Up @@ -110,6 +122,7 @@
sns.lineplot(data=loss_df, x='dropout_rate', y='val_loss_vary')

model_vary.save('fit_outputs/model_vary.h5')
########################################################

end = time.time()

Expand Down

0 comments on commit 90fc470

Please sign in to comment.