Skip to content

Commit

Permalink
markdown source builds
Browse files Browse the repository at this point in the history
Auto-generated via {sandpaper}
Source  : 30c3549
Branch  : main
Author  : Vlad Dracula <[email protected]>
Time    : 2023-11-28 06:00:47 +0000
Message : adding demo of onehot per issue number 3
  • Loading branch information
actions-user committed Nov 28, 2023
1 parent 03f5a0c commit 8568ede
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 38 deletions.
54 changes: 25 additions & 29 deletions 01-introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ It is the goal of this training workshop to produce a Deep Learning program, usi
```python
# load the required packages
from tensorflow import keras # library for neural networks
from sklearn.model_selection import train_test_split # library for splitting data into sets
import matplotlib.pyplot as plt # library for plotting
from icwithcnn_functions import prepare_image_icwithcnn # custom function
import numpy as np # library for working with images as arrays

# load the CIFAR-10 dataset included with the keras library
(train_images, train_labels), (test_images, test_labels) = keras.datasets.cifar10.load_data()
Expand Down Expand Up @@ -131,11 +132,11 @@ print('Test: Images=%s, Labels=%s' % (test_images.shape, test_labels.shape))
## Output

```output
Train: Images=(50000, 32, 32, 3), Labels=(50000, 10)
Train: Images=(50000, 32, 32, 3), Labels=(40000, 10)
Validate: Images=(10000, 32, 32, 3), Labels=(10000, 10)
Test: Images=(10000, 32, 32, 3), Labels=(10000, 10)
```
The training set consists of 50000 images of 32x32 pixels and 3 channels (RGB values) and labels.
The training set consists of 40000 images of 32x32 pixels and 3 channels (RGB values) and labels.

The validation and test datasets consist of 10000 images of 32x32 pixels and 3 channels (RGB values) and labels.

Expand Down Expand Up @@ -195,7 +196,9 @@ x_intro = keras.layers.Dense(64, activation='relu')(x_intro)
outputs_intro = keras.layers.Dense(10, activation='softmax')(x_intro)

# create the model
model_intro = keras.Model(inputs=inputs_intro, outputs=outputs_intro, name="cifar_model_intro")
model_intro = keras.Model(inputs = inputs_intro,
outputs = outputs_intro,
name = "cifar_model_intro")
```

### Step 5. Choose a loss function and optimizer
Expand All @@ -208,7 +211,7 @@ The optimizer is responsible for taking the output of the loss function and then
# compile the model
model_intro.compile(optimizer = 'adam',
loss = keras.losses.CategoricalCrossentropy(),
metrics=['accuracy'])
metrics = ['accuracy'])
```

### Step 6. Train the model
Expand All @@ -219,7 +222,7 @@ We can now go ahead and start training our neural network. We will probably keep
# fit the model
history_intro = model_intro.fit(train_images, train_labels, epochs = 10,
validation_data = (val_images, val_labels),
batch_size=32)
batch_size = 32)

```
Your output will begin to print similar to the output below:
Expand All @@ -228,42 +231,40 @@ Epoch 1/10
1250/1250 [==============================] - 15s 12ms/step - loss: 1.4651 - accuracy: 0.4738 - val_loss: 1.2736 - val_accuracy: 0.5507
```
::::::::::::::::::::::::::::::::::::::::: spoiler

### What does this output mean?
#### What does this output mean?

This output printed during the fit phase, i.e. training the model against known image labels, can be broken down as follows:

- `Epoch` describes the number of full passes over all *training data*. In the output above there are **1250 training observations**. This number is calculated as the total number of images used as input divided by the batch size (40000/32). An epoch will conclude and move to the next epoch after a training pass over all 1563 observations.
- `Epoch` describes the number of full passes over all *training data*. In the output above there are **1250 training observations**. This number is calculated as the total number of images used as input divided by the batch size (40000/32). An epoch will conclude and move to the next epoch after a training pass over all observations.

- `loss` and `val_loss` can be considered as related. Where `loss` is a value the model will attempt to minimise, and is the distance between the true label of an image and the models prediction. Minimising this distance is where *learning* occurs to adjust weights and bias which reduce `loss`. On the other hand `val_loss` is a value calculated against the validation data and is a measurement of the models performance against **unseen data**. Both values are a summation of errors made for each example when fitting to the training or validation sets.

- `accuracy` and `val_accuracy` can also be considered as related. Unlike `loss` and `val_loss`, these values are a percentage and are only revelant to **classification problems**. The `val_accuracy` score can be used to communicate a percentage value of model effectiveness on unseen data.

:::::::::::::::::::::::::::::::::::::::::

### Step 7. Perform a Prediction/Classification

After training the network we can use it to perform predictions. This is the mode you would use the network in after you have fully trained it to a satisfactory performance. Doing predictions on a special hold-out set is used in the next step to measure the performance of the network.

```python
# specify a new image and prepare it to match CIFAR-10 dataset
from icwithcnn_functions import prepare_image_icwithcnn
# predict the class name of the first test image
result_intro = model_intro.predict(test_images[0].reshape(1,32,32,3))

new_img_path = "../data/Jabiru_TGS.JPG" # path to image
new_img_prepped = prepare_image_icwithcnn(new_img_path)

# predict the class name
result_intro = model_intro.predict(new_img_prepped) # make prediction

print(' The predicted probability of each class is: \n', result_intro.round(4))
print('The predicted probability of each class is: ', result_intro.round(4))
print('The class with the highest predicted probability is: ', class_names[result_intro.argmax()])

# plot the image with its true label
plt.imshow(test_images[0], cmap=plt.cm.binary)
plt.title('True class:' + class_names[test_labels[0,].argmax()])
plt.show()
```

```output
The predicted probability of each class is: [[0.0058 0.714 0. 0.0024 0. 0. 0.2777 0. 0. 0. ]]
The class with the highest predicted probability is: automobile
The predicted probability of each class is: [[0.0074 0.0006 0.0456 0.525 0.0036 0.1062 0.0162 0.0006 0.2908 0.004 ]]
The class with the highest predicted probability is: cat
```
![](fig/01_test_image.png){alt='poor resolution image of a cat'}

::::::::::::::::::::::::::::::::::::::::: callout
My result is different!
Expand All @@ -275,10 +276,11 @@ If you are finding significant differences in the model predictions, this could

Congratulations, you just created your first image classification model and used it to classify an image!

Unfortunately the classification was incorrect. Why might that be? and What can we do about?
Was the classification correct? Why might it be incorrect and What can we do about?

There are many ways we can try to improve the accuracy of our model, such as adding or removing layers to the model definition and fine-tuning the hyperparameters, which takes us to the next steps in our workflow.


### Step 8. Measure Performance

Once we trained the network we want to measure its performance. To do this we use some additional data that was **not** part of the training; this is known as a test set. There are many different methods available for measuring performance and which one is best depends on the type of task we are attempting. These metrics are often published as an indication of how well our network performs.
Expand All @@ -289,13 +291,7 @@ When building image recognition models in Python, especially using libraries lik

#### What are hyperparameters?

Hyperparameters are all the parameters set by the person configuring the machine learning instead of those learned by the algorithm itself. These hyperparameters can include the learning rate, the number of layers in the network, the number of neurons per layer, and many more. Hyperparameter tuning refers to the process of systematically searching for the best combination of hyperparameters that will optimize the model's performance. One common method for hyperparameter tuning is **grid search**.

#### What is Grid Search?

Grid Search or **GridSearch** (as per the library function call) is foundation method for hyperparameter tuning. The aim of hyperparameter tuning is to define a grid of possible values for each hyperparameter you want to tune. GridSearch will then evaluate the model performance for each combination of hyperparameters in a brute-force manner, iterating through every possible combination in the grid.

These concepts will be continued, with practical examples, in Episode 05.
Hyperparameters are all the parameters set by the person configuring the machine learning instead of those learned by the algorithm itself. These hyperparameters can include the learning rate, the number of layers in the network, the number of neurons per layer, and many more. Hyperparameter tuning refers to the process of systematically searching for the best combination of hyperparameters that will optimize the model's performance. This concept will be continued, with practical examples, in [Episode 05 Evaluate a Convolutional Neural Network and Make Predictions (Classifications)](./05-evaluate-predict-cnn.md)

### Step 10. Share Model

Expand Down
57 changes: 50 additions & 7 deletions 02-image-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ In some cases you will be able to download an image dataset that is already labe
Where labelled data exists, in most cases the data provider or other users will have created functions that you can use to load the data. We already saw an example of this in the introduction:

```python
# load the CIFAR-10 dataset included with the keras packages
from tensorflow import keras

# commented out in case these are already be in memory
# load the cifar dataset included with the keras library
(train_images, train_labels), (test_images, test_labels) = keras.datasets.cifar10.load_data()
```

Expand Down Expand Up @@ -147,7 +146,7 @@ Two of the most commonly used libraries for image representation and manipulatio

::::::::::::::::::::::::::::::::::::::::::::::::::

Let us start by looking at the image we used in the introduction.
Let us start by taking a closer look at the Jabiru image.

```python
# load the libraries required
Expand Down Expand Up @@ -249,7 +248,7 @@ The min, max, and mean pixel values are 0.0 , 255.0 , and 87.0 respectively.
After normalization, the min, max, and mean pixel values are 0.0 , 1.0 , and 0.0 respectively.
```

Of course, if there are a large number of images to preprocess you do not want to copy and paste these steps for each image! Fortunately, keras has a solution for that: [tf.keras.utils.image_dataset_from_directory]
Of course, if there are a large number of images to preprocess you do not want to copy and paste these steps for each image! Fortunately, Keras has a solution for that: [tf.keras.utils.image_dataset_from_directory]


### One-hot encoding
Expand Down Expand Up @@ -280,6 +279,48 @@ Table 2. After One-Hot Encoding.

Each category has its own binary column, and the value is set to 1 in the corresponding column for each row that matches that category.

The Keras function for one_hot encoding is called [to_categorical]:

`tf.keras.utils.to_categorical(y, num_classes=None, dtype="float32")`

- `y` is array-like with class values to be converted into a matrix (integers from 0 to num_classes - 1)
- `num_classes` is the total number of classes. If None, this would be inferred as max(y) + 1
- `dtype` is the data type expected by the input. Default: 'float32'

We performed this operation in **Step 3. Prepare data** of the Introduction but let us look at the labels before and after one-hot encoding.

```
print()
print('train_labels before one hot encoding')
print(train_labels)
# one-hot encode labels
train_labels = keras.utils.to_categorical(train_labels, len(class_names))
val_labels = keras.utils.to_categorical(val_labels, len(class_names))
print()
print('train_labels after one hot encoding')
print(train_labels)
```
```output
train_labels before one hot encoding
[[6]
[9]
[9]
...
[9]
[1]
[1]]
train_labels after one hot encoding
[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 1.]
[0. 0. 0. ... 0. 0. 1.]
...
[0. 0. 0. ... 0. 0. 1.]
[0. 1. 0. ... 0. 0. 0.]
[0. 1. 0. ... 0. 0. 0.]]
```

### Image augmentation

Expand All @@ -293,8 +334,9 @@ There are several ways to augment your data to increase the diversity of the tra
- brightness, contrast, or hue
- these changes simulate variations in lighting conditions

We will look at image augmentation in a later episode.
We will not be looking at image augmentation in this lesson but it is important that you be aware of this type of data preparation because it can make a big difference in your model's ability to predict outside of your training data.

Have a look at [Image augmentation layers] for information about these operations.

### Data Splitting

Expand Down Expand Up @@ -448,7 +490,8 @@ Our dataset is preprocessed and split into three sets which means we are ready t


[tf.keras.utils.image_dataset_from_directory]: https://keras.io/api/data_loading/image/
[to_categorical]: https://keras.io/api/utils/python_utils/#to_categorical-function
[train_test_split]: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html
[tf.data.Dataset]: https://www.tensorflow.org/api_docs/python/tf/data/Dataset

[CINIC-10]: https://github.com/BayesWatch/cinic-10/
[Image augmentation layers]: https://keras.io/api/layers/preprocessing_layers/image_augmentation/

Binary file added fig/01_test_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions md5sum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"index.md" "a02c9c785ed98ddd84fe3d34ddb12fcd" "site/built/index.md" "2023-08-22"
"links.md" "8184cf4149eafbf03ce8da8ff0778c14" "site/built/links.md" "2023-08-22"
"episodes/setup-gpu.md" "afed8a1e1d954863d39c15c4ad14f34a" "site/built/setup-gpu.md" "2023-11-09"
"episodes/01-introduction.md" "5cfbe50cadc4500fbd4596c2b110280e" "site/built/01-introduction.md" "2023-11-22"
"episodes/02-image-data.md" "c7313b07d5a7edb80e81383d87d5dba3" "site/built/02-image-data.md" "2023-11-16"
"episodes/01-introduction.md" "8e043ba0d1e5b43fa02a0092138f9b48" "site/built/01-introduction.md" "2023-11-28"
"episodes/02-image-data.md" "0ca9bc92b9bf83e54d48787faa681f3d" "site/built/02-image-data.md" "2023-11-28"
"episodes/03-build-cnn.md" "06e0475df3f110651bde9662144e1732" "site/built/03-build-cnn.md" "2023-11-22"
"episodes/04-fit-cnn.md" "50dedd6b106f690a24e9495e1c2a93b9" "site/built/04-fit-cnn.md" "2023-11-23"
"episodes/05-evaluate-predict-cnn.md" "b2a1d8d4c1ee2400b665b80f47e3fcd0" "site/built/05-evaluate-predict-cnn.md" "2023-11-23"
Expand Down

0 comments on commit 8568ede

Please sign in to comment.