Deep Learning Examples With Pytorch And Fastai A Developers Cookbook Bernhard J Mayr instant download
Deep Learning Examples With Pytorch And Fastai A Developers Cookbook Bernhard J Mayr instant download
https://ebookbell.com/product/deep-learning-examples-with-
pytorch-and-fastai-a-developers-cookbook-bernhard-j-mayr-43818478
https://ebookbell.com/product/practical-computer-vision-applications-
using-deep-learning-with-cnns-with-detailed-examples-in-python-using-
tensorflow-and-kivy-1st-edition-ahmed-fawzy-gad-7280142
https://ebookbell.com/product/practical-computer-vision-applications-
using-deep-learning-with-cnns-with-detailed-examples-in-python-using-
tensorflow-and-kivy-1st-edition-ahmed-fawzy-gad-61258050
https://ebookbell.com/product/introduction-to-deep-learning-with-
complexe-python-and-tensorflow-examples-jurgen-brauer-7409008
Machine Learning With Python Design And Develop Machine Learning And
Deep Learning Technique Using Real World Code Examples Abhishek
Vijayvargia
https://ebookbell.com/product/machine-learning-with-python-design-and-
develop-machine-learning-and-deep-learning-technique-using-real-world-
code-examples-abhishek-vijayvargia-50457874
Deep Learning In Visual Computing Explanations And Examples Hassan
Ugail
https://ebookbell.com/product/deep-learning-in-visual-computing-
explanations-and-examples-hassan-ugail-48754194
https://ebookbell.com/product/deep-learning-by-example-a-handson-
guide-to-implementing-advanced-machine-learning-algorithms-and-neural-
networks-menshawy-7240872
https://ebookbell.com/product/artificial-intelligence-by-example-
acquire-advanced-ai-machine-learning-and-deep-learning-design-
skills-2nd-edition-denis-rothman-10815800
https://ebookbell.com/product/deep-learning-ian-goodfellow-yoshua-
bengio-aaron-courville-44886094
https://ebookbell.com/product/deep-learning-in-biology-and-medicine-
davide-bacciu-paulo-j-g-lisboa-44899934
Deep Learning Examples with
PyTorch and fastai
A Developers’ Cookbook
Dr. Bernhard J. Mayr, MBA [email protected]
Contents
1 Introduction 7
1.1 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3 Introduction to fastai . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4 Using Google’s Colab-Tool . . . . . . . . . . . . . . . . . . . . . 12
1.5 Install fastai library . . . . . . . . . . . . . . . . . . . . . . . . 15
1.6 First Example: Dog Breed Classifier . . . . . . . . . . . . . . . 16
4 Multi-Label Classification 55
4.1 Using the DataBlock API with ColumnReader . . . . . . . . . 56
4.2 Using a different Approach – Instrumentalizing lambdaExpressions . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . 57
4.3 Third Approach – use a Custom get_items Function . . . . . . 58
4.4 Create and Train the Model . . . . . . . . . . . . . . . . . . . . 60
6 Cross-Fold-Validation 69
6.1 Create a Baseline for training . . . . . . . . . . . . . . . . . . . 72
6.2 Build the Cross-Validation-Model . . . . . . . . . . . . . . . . . 73
8 Image Segmentation 89
8.1 Let’s get the CAMVID Dataset . . . . . . . . . . . . . . . . . . . 89
8.2 Training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
11 Style-Transfer 137
11.1 The pretrained Network . . . . . . . . . . . . . . . . . . . . . . 138
11.2 Create the Loss-Function . . . . . . . . . . . . . . . . . . . . . 140
11.3 Create the Model Architecture . . . . . . . . . . . . . . . . . . 144
11.4 DataLoaders and Learners . . . . . . . . . . . . . . . . . . . . . 147
11.5 Do the Style-Transfer on one Image . . . . . . . . . . . . . . . . 149
11.6 Upscale the Model . . . . . . . . . . . . . . . . . . . . . . . . . 152
11.7 Exporting the Model . . . . . . . . . . . . . . . . . . . . . . . . 152
11.8 Export our Style-Transfer Library . . . . . . . . . . . . . . . . 153
20 Audio 255
20.1 Why is Audio different (to Images)? . . . . . . . . . . . . . . . 255
20.2 The Audio Dataset . . . . . . . . . . . . . . . . . . . . . . . . . 256
20.3 Preparing the Dataset . . . . . . . . . . . . . . . . . . . . . . . 257
20.4 Put the Pieces together – Using the Datablock API . . . . . . . 259
20.5 Training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
20.6 Add Data Augmentation . . . . . . . . . . . . . . . . . . . . . . 263
20.7 Mel Frequency Cepstral Coefficient (MFCC) . . . . . . . . . . . 266
20.8 MFCC + Delta . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Chapter 1
Introduction
This book will guide you through a journey on Deep Learning using the most
recent version of the library fast.ai (Version 2). If you install the fastai library
now, you will always get version 2.
If you need a former version for some old tutorials, follow the following link:
echo 'fastai 1.*' >> $CONDA_PREFIX/conda-meta/pinned
You can then install fastai v1. If you no longer need v1, just remove the
$CONDA_PREFIX/conda-meta/pinned file. The repository for version 1 will
be renamed in fastaiv1. There will be bug fixes but no further development.
This book will show you working examples on how to use the fastai
framework for deep learning. Fastai is well suited for developers who are just
starting out with deep learning as well as those who already have some
background on deep learning and are familiar with the pytorch framework.
There are even tutorials on how to migrate your projects from different deep
learning frameworks to fastai in the docs. See https://docs.fast.ai/#Mig rating-
from-other-libraries for further information.
You will get a quick introduction on the fastai framework and why we think
it is currently one of the best frameworks for deep learning. You will also get
an introduction to the google colab platform (an online application with free
access to GPU).
All chapters of this book are designed with a work book style in focus. You
should get the code with a few lines of prosa text describing each variable of
the examples. The book will not describe the theory on the algorithms that
were implemented but just give you as much code as possible. You should try
to replicate the examples and adapt them to suite your needs. You will get
references to papers or tutorials for further reading if you are more interested
in some background knowledge.
The aim of developing this book was to give you an idea on how you can
adapt those examples to fit your personal needs!
1.1 Prerequisites
All the code examples have been developed with Jupyter notebooks. If you
are not familiar with Jupyter, just have a look at the documentation: https:
//jupyter.org/.
If you want to run the code examples on your local machine, be sure to have
a Nvidia GPU with at least 8 GB RAM installed. Also make sure you have
installed jupyter server to run jupyter notebooks.
You can also use google colab and run the examples online, totally free of
charge. (Yes of course google will track all your activities… but who cares?
Well at least not while studying.) Nevertheless, you should always consider
data privacy with real world production projects.
1.2 Resources
While working with fastai, I have always received good advice by using the
official forum: forums.fast.ai as well as the official documentation:
docs.fast.ai.
There is also a discord server on which you can attend live coding sessions
with Jeremy Howard (founder of fastai). Just use this link to get started:
https://discord.com/invite/xnpeRdgv.
Fastai is a library for easy development of deep neural nets that helps you
make state-of-the-art model architectures more easily accessible. You can
dive deep into the basics and adapt elements from the framework to suite
your needs. The framework is open-source and available via github.
This means you can get going quickly with default models on these classes.
These are called applications in the fastai architecture model. You can,
however, dig deeper if you need more fine grained modifications.
• High Level Applications: With the high level applications you can build
state-of-the-art deep learning models with less than ten lines of codes. Most
beginners will start here.
• Mid Layer API: Provides more flexibility on designing your deep learning
applications. This level is especially useful if you need modifications on your
data pre-processing and data loaders or if you plan to use your own learners
and model architectures. With the mid layer api you can also monitor your
training progress with TensorBoard. You can schedule different values of
hyper-parameters for training or try different variants of regularization.
• Foundations: This level provides all the basic stuff if you need to work
with tensors or you want to manage operations directly on your GPU.
Fastai also integrates the concept of Callbacks. This helps you to use the
same training loop for very different deep learning applications. You can
customize your training loop to build GAN networks for example. Callbacks
in fastai are two-way callbacks. They can read anything like data, parameters,
etc. as well as write them. This means you can even do the training of a GAN
with the same training loop.
Another concept that shows the great flexibility of fastai is that of a generic
optimizer. You can create your own optimizer by just plugging in statistics,
steppers and optimizers.
These architectures allow you to easily implement any new state-of-the-art
optimizer on your own. The statistics module contains any data that you want
to measure while doing the training. The stepper uses these stats to change
the weights of the net. If you now combine these two modules (stats and
steppers) you are able to implement any of the optimizers.
With the data block api, you are able to get your data into any model. For
using the data block api you need to know the following:
databunch = mnist.databunch(untar_data(URLs.MNIST_TINY))
databunch,show_batch(max_n=9, figsize=(4,4))
With this example we say: ok, we have black/white PILImages as input and a
Category for output. We get our data with a list of files. Let’s split the data
into training and validation sets by using the grandparent path of the files.
We grab the y-labels by using the parent path.
This is enough to use the mnist data set with our models. We can have a look
at our data by using the show_batch method.
Of course the data block api works for all applications of deep learning like
image segmentation, object detection, keypoint analysis, text, etc.
According to the foundation level of fastai, a new object type was added:
Object oriented tensors. With oo tensors you can call methods directly on
your tensor objects. E.g. if you have a tensor object containing an image, you
can now just call tensor.flip_lr to flip the image or .show to display the
image. Now any transform can be defined for different types of tensor
objects.
To pre-process data, you can use the transforms pipeline to easily create a
data transform pipeline.
Generally speaking, all the development of the fast.ai library was done by
highly leveraging parallel computing on the GPU. E.g. most of the computer
vision pipeline that is included with fastai is done in parallel on the GPU,
minimizing computing time.
You can start an EC2 instance on Amazon AWS (or any other computing
platform). This will bill you for the time while running the instance. Make
sure you have selected an instance level with a GPU. There are a few pros
and cons though. If you are working on production level projects, you should
consider a secure and reliable infrastructure stack. Then running a GPU
server on a cloud platform will be a good solution.
Google provides a tool called Colab that is (kind of) a hosted Jupyter
Notebook instance which is completely free of charge, but of course with
some caveats:
• Google does not reassure access to a GPU but there are hardly times
without GPU access.
• Most of the time your session is paired to old, and rather slow, Tesla K80
GPUs.
• Your session is reset after a maximum runtime of 12 hours. This means all
your data will be lost after 12 hours if you do not upload it to another storage
facility.
You can improve performance by buying google colab pro. For 10 dol lars a
month you get access to faster V100 GPUs and your maximum runtime is
extended to 24 hours.
For most usages of deep learning we want to use a GPU because computing
takes a lot less time on the GPU rather on a CPU. To change the
processingtype of your runtime follow these steps:
1. Click on runtime
2. Change runtime type
3. Set runtime type to Python 3
4. Use Hardware accelerator -> GPU
On first opening a notebook from a public repository, you will probably get a
warning like this:
You can safely ignore this warning if you consider the repository secure and
click on the run anyway button.
If you are working with a notebook from github, you can open and run the
notebook but you cannot save your work. If you want to keep your work, you
can: – save a copy to your google drive – download your notebook and run
locally (or upload again later)
If you are using google colab, you do not have to install any additional library
as the fastai imports work out of the box.
If you are running your notebooks locally or on your custom GPU-instance
you will have to install fastai first.
I suggest you create an environment for your fastai projects and install the
fastai library afterwards into this environment. You can also read the full
installation guide online: https://docs.fast.ai/
After installing, we can import all libraries. For the first example we will use
basics, callback, and vision. Then we will try a vision task with fastai v2.
from fastai.basics import * from fastai.vision.all import * from
fastai.callback.all import *
If we want to create a model with fastai, we will mostly follow the following
path: 1. Make all the DataLoaders 1. Make a Learner with some equipment.
Here you will have to decide which task you want to solve. (See different
types of deep learning tasks) 1. Train the model 1. Evaluate, tune and retrain
your model. I will discuss different techniques on how to do this.
Let’s download the data set and have a look at some of the images. We will
directly use the Path library to set a reference to our data-folder. The URLs
class contains constant values for all the different data sets that ship with the
fastai library.
The data set we will use is called Oxford-IIIT Pet Dataset. You can have a
look at the homepage to get a better understanding of the data: (https:
//www.robots.ox.ac.uk/~vgg/data/pets/)
There are special images for the heads because back in 2015 the best model
for distinguishing the breeds was a combined model with different layers
trained on the whole image and on recognizing just parts of the image like the
head, etc.
We will use the whole image for training.
path = untar_data(URLs.PETS)
(#2) [Path('/home/bmayr/.fastai/data/oxford-iiit-pet/
→crappy'),Path('/home/bmayr/.fastai/data/oxford-iiit-pet/ →images')]
Let’s analyze how many image folders our data set contains:
len(path.ls())
5
path.ls()
(#5) [Path('/home/bmayr/.fastai/data/oxford-iiit-pet/
→crappy'),Path('/home/bmayr/.fastai/data/oxford-iiit-pet/
→images'),Path('/home/bmayr/.fastai/data/oxford-iiit-pet/
→models'),Path('/home/bmayr/.fastai/data/oxford-iiit-pet/
→image_gen'),Path('/home/bmayr/.fastai/data/oxford-iiit-pet/ →annotations')]
The fastai library is not just an add on on top of pytorch. Fastai comes with
many more features and implements state-of-the-art techniques from deep
learning science with the goal to provide a very smart abstraction layer for
building rich, performance models with four to five lines of codes.
But you are also able to dig deep into the basics and use fastai for building
your own, more sophisticated models. For this reason, fastai comes with a
three tier architecture that we have already discussed above.
Below these layers, there is a kind of “ mid-level-layer ” that allows for more
control on building models, especially on handling your data and data-
processing. The mid-layer contains the Data Block API, Learner and Cal
lbacks and Generic Optimizers. We will use this layer in later examples.
Below the mid-layer there is the foundation API. This allows for total control
on models and data flow.
Let’s get back to our starter model. First of all we need to grab all the
filenames from the images to build our DataLoaders. We use a helper
function get_image_files() from fastai that returns a list of all filenames in a
given directory.
file_names = get_image_files(path/'images/')
file_names[:2]
(#2) [Path('/home/bmayr/.fastai/data/oxford-iiit-pet/
→images/Birman_105.jpg'),Path('/home/bmayr/.fastai/data/ →oxford-iiit-
pet/images/Siamese_175.jpg')]
If we have a look at the filenames, we see that the name contains the label
that we want to predict later. E.g. /home/bmayr/.fastai/data/oxford-iiit-
pet/images/Birman_105.jpg - this is the image of a Birman cat.
With fastai we have different options on how to get the labels for our training
data. We can either extract the label from a parent folder path, from a csv-file
or data frame or we use a regular expression to extract the label from the
path.
If you are not familiar with regular expressions – don’t be afraid, you are not
alone. There are plenty of good tutorials on the web and you will find
examples for nearly all possible applications on stack overflow. To test your
regEx you can use one of the many online-editors that show which portion of
a string matches your regular expression. E.g. regex101.com: (https:
//regex101.com/)
pat = r'(.+)_\d+.jpg$'
Transforms are an excellent way of augmenting your training data. E.g. you
can zoom in or out a bit, flip your images, warp, etc. to generate even more
training examples. Fastai implements data augmentation in a very fast way by
doing image data augmentation in parallel on your GPU.
The batch size will be 64 – you can adjust the value according to your
GPUmemory. The higher the batch size, the more memory will be used on
your GPU.
data = ImageDataLoaders.from_name_re(path,
file_names,
pat,
batch_tfms=batch_tfms, item_tfms=item_tfms, bs=bs)
Now we rebuild this DataLoader with the Data-Block-API. The Data Block
API always requires the same steps: 1. define data blocks 1. get the data
items 1. define a splitter to split into training and validation set 1. get the
labels 1. set item transforms 1. set batch transforms
Why do we want to split our data into training and validation data?
We train our model on the training dataset. Yet if we use all the available data
for training, the model could easily overfit, meaning that it just memorizes
the images. E.g. it learns that the third image is a cat, but it doesn’t learn what
exactly specifies a cat. Therefore we recheck after each training epoch on a
dataset what the model has not seen during training. This dataset is called the
validation dataset.
Now with the DataBlock-object in hand, we can create our dataloaders from
the image filenames. Set the path_img to the images folder and create the
dataloaders.
path_img = path/'images/'
dls = pets.dataloaders(path_img, bs=bs)
By now we have followed all the necessary steps for defining our training
and validation data. It’s always a good idea to have a look at the data before
any training. So we can check the validity of our training examples.
We can use show_batch to print a random batch from our dataloaders. This is
a quick check on data sanity.
dls.show_batch(max_n=9, figsize=(6,7))
Let’s see how many classes there are in our training data. In fastai this is
called the vocab (vocabulary) of our dataloaders.
dls.vocab
(#37) ['Abyssinian','Bengal','Birman','Bombay',
'British_Shorthair','Egyptian_Mau','Maine_Coon',
'Persian','Ragdoll','Russian_Blue'...]
Fastai uses the class L for representing lists. L was developed for fastai to
improve the capabilities of lists in python. Internally all the classes are
translated to integer, but thanks to L we directly see class-names. If we want
to have a look at the ints, we can call dls.vocab.o2i. o2i means a translation
from objects to integer values.
dls.vocab.o2i
{'Abyssinian': 0,
'Bengal': 1,
'Birman': 2,
'Bombay': 3,
'British_Shorthair': 4,
'Egyptian_Mau': 5,
'Maine_Coon': 6,
'Persian': 7,
'Ragdoll': 8,
'Russian_Blue': 9,
'Siamese': 10,
'Sphynx': 11,
'american_bulldog': 12,
'american_pit_bull_terrier': 13,
'basset_hound': 14,
'beagle': 15,
'boxer': 16,
'chihuahua': 17,
'english_cocker_spaniel': 18, 'english_setter': 19,
'german_shorthaired': 20,
'great_pyrenees': 21,
'havanese': 22,
'japanese_chin': 23,
'keeshond': 24,
'leonberger': 25,
'miniature_pinscher': 26,
'newfoundland': 27,
'pomeranian': 28,
'pug': 29,
'saint_bernard': 30,
'samoyed': 31,
'scottish_terrier': 32,
'shiba_inu': 33,
'staffordshire_bull_terrier': 34, 'wheaten_terrier': 35,
'yorkshire_terrier': 36}
The idea of transfer learning is to use the power of a deep model and retrain
just the last layer on our specific task. This reduces training time considerably
while producing great results quickly.
First of all we create a Learner object. Fastai provides different learners for
different tasks. We have a vision-task so let’s grab a cnn_learner.
Each learner needs: – DataLoaders for managing training and validation data
– a model defining the architecture – an evaluation metric – a loss function
and – an optimizer
We will also add mixed_precision (fp16) because this reduces memory usage
during training.
learn = cnn_learner(dls,
resnet34,
pretrained=True,
metrics=error_rate).to_fp16()
0 1.885583
1 0.656676
2 0.343732
3 0.253843
valid_loss error_rate time
We can save our weights for reloading any time later by just calling save on
our learner object. Optionally we use any name for storage we like.
learn.save('stage_1') Path(‘models/stage_1.pth’)
With fastai we have a great opportunity to look into our model’s results by
getting the classification-interpretation. This allows to show the most
mismatched examples and plot a confusion matrix.
This shows the top losses with the predicted and the actual class labels and
the loss-values as well as the probability.
interp.plot_top_losses(9, figsize=(15,10))
Now we print the confusion matrix. A “perfect” model would only have
values on the diagonal because this means that the model has always
predicted the correct label.
These are the classes that were mixed up at least 4 times. Well, I am not a
dog breed expert but after some research on these breeds I have to admit,
these are really hard to differentiate.
1.6.4 Unfreeze the whole Model and retrain
Now we can unfreeze all the layers of the pretrained ResNet model and
retrain the whole model for some epochs to get better results.
learn.unfreeze()
learn.fit_one_cycle(4)
So our error rate went bad. After the first round of training we had an error
rate of about 0.08 but now we got 0.11!
What happened here? This is because this time we had unfreezed all layers of
the model before running the training loop and training such a big model is a
way more difficult task than just retraining the last layer of a given model.
Therefore we have to carefully decide on the training parameters.
When training the whole model we select a range and not a single value
because we want to train the first layers with a lower learning rate than the
last layers. This is called learning-rate annealing. A concept presented in
Stanford’s CS231: (http://cs231n.github.io/neural-networks-3/#anneal)
learn.unfreeze();
learn.fit_one_cycle(4, lr_max=slice(1e-6, 1e-4))
epoch train_loss valid_loss error_rate time
# choose a
learning rate with a high negative slope
learn.fit_one_cycle(5, lr_max=(1e-3))
epoch train_loss valid_loss error_rate time
So with the bigger model architecture, we get an even lower error rate of
0.05. Now we can do unfreezing and/or training for more epochs.
learn.save( 'resnet50')
Path(‘/home/bmayr/.fastai/data/oxford-iiit-pet/models/resnet50.pth’) Again
we can run unfreeze() and retrain the whole model
learn.load( 'resnet50')
learn.unfreeze()
learn.fit_one_cycle(2, lr_max=4e-4)
epoch train_loss valid_loss error_rate time 0 0.800914 0.891309 0.264547
01:01 1 0.403630 0.329750 0.096752 01:00
learn.save('resnet50-stage_2')
) stage_2.pth’)
Well, running the training loop for only two epochs on this large model is
definitely not sufficient. If we want to use this model in a production
environment we will have to do more epochs.
Yet for demonstration purposes the model returns acceptable results with a
minimum number of lines of code.
Chapter 2
Build an Image Classifier from
Scratch
This chapter will guide you through the creation of a deep learning model for
a classification task from scratch.
We will build our own deep learning model to recognize handwritten digits.
To train our classifier we will use the MNIST training dataset. This dataset
consists of grayscaled images of handwritten digits.
You can find some more info on this dataset at wikipedia: https://en.wikip
edia.org/wiki/MNIST_database. We will use the URLs-class from fastai for
easy downloading of all the mnist data.
Now import the vision libraries from fastai and download the mnist dataset.
This time we will use the lowest level api from fastai to access our data.
path = untar_data(URLs.MNIST)
2.1 Let’s analyze the Dataset
The first step of any ML/DL Project is always an analysis of the dataset. So
let’s have a look at our mnist dataset!
items = get_image_files(path)
Now let’s look at the data and how it’s organized.
We see that all the files are stored in folders indicating training, validation
and test set.
Within these datasets there are folders for each of the classes like 0, 1, … 9
containing all the image files.
items[[0,40,501]]
(#3) [Path(‘/home/bmayr/.fastai/data/mnist_png/training/3/31274.png’),
Path(‘/home/bmayr/.fastai/data/mnist_png/training/3/54648.png’),
Path(‘/home/bmayr/.fastai/data/mnist_png/training/3/27143.png’)]
Next we will look at how to create a PIL greyscaled image from the filename.
Most of the fastaiv2 API has this kind of create method like PILImageBW.
img = PILImageBW.create(items[0])
img.show()
<matplotlib.axes._subplots.AxesSubplot at 0x7f7297802450>
As can be seen, the dataset is organized in different folders for training and
validation. Therefore, we can use the GrandparentSplitter for splitting the
data into training and validation.
We just have to assign the names of the corresponding folders to our
GrandparentSplitter.
After creating the splitter we can use this splitter object to divide our
filenames into training and validation data. We will have a list consisting of
two lists. One containing the indices for the training data folder and one
containing the indices for the validation data folder.
Let’s recheck this! We see splits contains two objects (training and validation
set). We can index into the list and show the first three elements for training
as well as for validation. Our splitter uses indices <60000 for training and
>=60000 for validation!
splits = splitter(items)
print(len(splits))
print(splits[0][:3])
print(splits[1][:3])
• the filenames
• some transforms
• the splits for training and validation
After creating the datasource object we can look into it by using the show_at
method. We will have to pass the dataset (either train or valid) and the index
we want to look at.
We see greyscale images along with the correct labels from the datasets.
show_at(datasource.train, 3), show_at(datasource.valid, -3)
(<matplotlib.axes._subplots.AxesSubplot at 0x7f729756a990>,
<matplotlib.axes._subplots.AxesSubplot at 0x7f7297516dd0>)
The next step will be to add some transforms to augment the dataset. This
time we will have to define all the steps manually.
In general we will conduct the following tasks:
All the transforms are applied serially (read from left to right). This means we
can think of transforms to be like pipelines. First convert to tensor, second
apply some cropping, third …. )
The first transforms are those that have to be applied on each item before we
can load it into the batch. These transforms are applied on the CPU.
items_tfms = [ToTensor(),
CropPad(size=34, pad_mode=PadMode.Zeros), RandomCrop(size=28)]
Next step is to apply the batch transforms on the GPU. We call Cuda() to
enable GPU usage, convert the int-values to float-values to increase training
performance and then normalize our data.
With this model we do not need to normalize for the imagenet dataset
because we train our model from scratch.
gpu_tfms = [IntToFloatTensor(), Normalize()]
Now we can create our dataloaders-object. We set a batch-size and apply our
transforms to after_items and to after_batch.
dataloaders = datasource.dataloaders(bs=128,
after_item=items_tfms, after_batch=gpu_tfms)
This exactly represents our black and white image (remember, one channel
for each colour – so just 1 for a b/w image) with a resolution of 28x28 pixel.
Earlier we defined our batchsize to have a value of 128.
The output size of one batch is 128 because we have one label for each image
in the batch – a total of 128 labels for each batch.
• convolution layers,
• batch normalization layers,
• activation functions (ReLu) and
• a flatten layer as a top layer
Convolutional layers have shown good results when working with images.
You can even transform different data into images and work with conv-
layers. E.g. you create spectograms of your music files and use these
spectograms like images.
The filter itself consists of an array of floats – called weights (or sometimes
parameters).
Keep in mind that the depth of the filter has to be of the same size as the
depth of the input. In our example we use one color channel – so we use a
depth of 1.
Next step is to start moving the filter around on our pixel array of the image.
We define this moving as convolving around the image.
While convolving, the filter multiplies the values inside the filter with the
original values inside the highlighted receptive field. During the process the
filter does an element-wise multiplication.
All the results of these multiplications are then summed up. The filter keeps
repeating for all the pixels in the image resulting in an activation or feature
map.
In our case (24x24 pixels images) this feature map will have 784 different
locations, that turns into a 28x28 array of pixels.
While our tensors are being passed through the model we want them to be
normalized. This will improve training speed and will allow each layer to
learn independently because each layer is re-normalized according to its
outputs.
def batch_norm(number_filters: int) -> nn.BatchNorm2d: return
nn.BatchNorm2d(number_filters)
These functions take care of the learning capability of our model. The
activation function can activate (return a positive value) or deactivate (return
a value less than zero) nodes within our layers.
The ReLu (or rectified linear unit) simply returns the input value if the value
is greater than zero or it returns zero for all input values equal or less than
zero: https://github.com/Kulbear/deep-learning-nano-foundation/wiki/Re LU-
and-Softmax-Activation-Functions#rectified-linear-units
The last layer will be flattened layer. We will simply use Flatten for this
purpose. Remember we can just use double question marks to show the
source code of any function: ??Flatten.
??Flatten
The last layer must have an output size of 10 because our data has 10
different classes (digits 0 to 9). We do not apply an activation function on the
last layer but squeeze the result into a flattened layer.
model = nn.Sequential(
conv( 1, 8),
batch_norm(8),
relu(),
conv(8, 16),
batch_norm(16),
relu(),
conv(16, 32),
batch_norm(32),
relu(),
conv(32, 16),
batch_norm(16),
relu(),
conv(16, 10),
batch_norm(10),
Flatten()
Now we can create a learner object by using the Learner class and instead of
using one of the pretrained model architectures (like we did with the
cnn_learner before) we now use our own model. We use a
CrossEntropyLossFlat loss-function because our learner will predict
probabilities for different classes.
learner = Learner(dataloaders,
model,
loss_func=CrossEntropyLossFlat(),
metrics=accuracy)
To check the exact sizes of the layers and the number of weights (parameters)
of our learner we call learner.summary()
learner.summary()
epoch train_loss valid_loss accuracy time 0 None None 00:00
Callbacks:
- TrainEvalCallback
- Recorder
- ProgressCallback
Check for the best learning rate with lr_find(). Remember we look for a value
with a very steep negative slope.
This time we will use a value about 1e-2 for training. After we have chosen a
valid learning rate we can train our model for some epochs.
learner.lr_find()
SuggestedLRs(lr_min=0.33113112449646,
lr_steep=0.3019951581954956)
learner.fit_one_cycle(3, lr_max=1e-2)
epoch train_loss
valid_loss accuracy time
We can simplify our model by using ConvLayer instead of using our three
layer-types. The ConvLayer from the fastai library contains these three
layers: Conv2d, BatchNorm2d and ReLU. You can just use the double
question marks ??ConvLayer again or define a simple conv2 layer and look
at the output:
ConvLayer(1, 8)
model = nn.Sequential(
conv2(1, 8),
conv2(8, 16),
conv2(16, 32),
conv2(32, 16),
conv2(16, 10),
kernel_size=(3, 3), stride=(1, 1), BatchNorm2d(8, eps=1e-05, momen
Flatten()
)
Now repeat the same steps as before. Create the learner, find optimal learning
rate and do some training.
learner = Learner(dataloaders,
model,
loss_func=CrossEntropyLossFlat(),
Another Random Document on
Scribd Without Any Related Topics
John Brown will live in history; but his name will not be found among
the names of those who have wrought for humanity and for
righteousness; or among the names of the martyrs and the saints
who "washed their robes and made them white in the blood of the
Lamb."
"YET SHALL HE LIVE": but it will be as a soldier of fortune, an
adventurer. He will take his place in history as such: and will rank
among adventurers as Napoleon ranks among marshals: as Captain
Kidd among pirates: and as Jonathan Wild among thieves.
APPENDICES
APPENDIX I
CORRESPONDENCE WITH THE LATE D. W.
WILDER CONCERNING JOHN BROWN
T
o
p
e
k
a
,
K
a
n
s
a
s
,
D
e
c
.
1
8
t
h
,
1
9
0
2
.
General D. W. Wilder, Hiawatha, Kansas.
My Dear General:
I would like to have you kindly tell me something valuable about
John Brown. I listened to your tribute to his memory, read before the
Historical Society on the 2nd inst. It recalled the admiration which I
entertained for the "Old Hero" throughout the many years of my life;
from young manhood up to about four years ago; when I attempted
to write a sketch of his life. It was in reading up to obtain data for
this sketch that the idol, which my credulity, I suppose, or
imagination had set up, went utterly to pieces in my hands. I read
faithfully what his biographers, Sanborn, and Redpath, and the other
fellows, have written about him, but none of them give up any
valuable facts. They all seem to be long on eulogy. They do overtime
on that. The whole performance is a continuous eulogium; but
historical facts, upon which to predicate a story, or upon which his
"immortal fame" is supposed to rest, are painfully lacking.... These
are some of the things which I went up against when I tried in good
faith to write about him, and they broke me all up, so I had to quit.
John Brown, the "Hero" and "Martyr," is a creation—Charlestown
furnished a simple text and the genius of his generation did the rest.
The brilliant minds of this age have exploited him in literary effects,
in prose, in poetry and oratory. They have placarded him "upon the
walls of time"; but I am compelled to believe that his fame thus
acquired, will not survive. The "why" may "repel the philosophic
searcher," but it cannot "defy" the historical searchers. History has
no enigmas.
I will be very glad indeed to have your opinions on this business.
V
e
r
y
t
r
u
l
y
y
o
u
r
s
,
H
i
l
l
P
.
W
i
l
s
o
n
.
In this letter the writer asked Mr. Wilder for his opinion upon Brown's
motives in their relation to several incidents that occurred in his life.
His reply is as follows:[514]
H
i
a
w
a
t
h
a
,
K
a
n
s
a
s
,
D
e
c
.
2
0
,
1
9
0
2
.
My Dear Wilson:
... You have stood on various platforms and made many political
speeches. Did any of them endorse the sentiments you now hold?
The elder Booth, a man of genius, once staggered up to the
footlights and said to the crowded house: "You are all drunk," and
staggered off.
You think the people of your county, your state, your country and of
the civilized world, including its noblest spirits, do not know a hero,
an emancipator—first of his state, then of his nation. Only one
Kansan has made a speech that thrilled the world and is immortal.
You never read it. Only one Kansan lives in poetry, in song, in human
hearts, and is the constant theme of the historian, the dramatist, the
man of letters. You think he was a fool. The whole world has
pronounced its verdict on John Brown.
Y
o
u
r
s
t
r
u
l
y
,
D
.
W
.
W
i
l
d
e
r
.
To this letter the writer replied:
T
o
p
e
k
a
,
K
a
n
s
.,
J
a
n
u
a
r
y
3
,
1
9
0
3
.
My Dear General:
Your letter of the 20th ult., is received. I told you that I had gone the
limit of my vocabulary in expressing my admiration of John Brown. I
read the "speech that thrilled the world." I have read the poetry and
have sung the songs. I make the point that the speeches, the poetry,
and the songs are all there is behind John Brown. When I asked you
about some historical facts, you gave me more oratory. It seems to
have become a habit. If you ever analyze this man's character, you
will reverse your estimate of him.
The world sees Brown fighting, heroically, in the engine-house at
Harper's Ferry, but it does not inquire how he came to be there. It
was his death, and not his life, that gave him renown. Usually it is a
man's life—his actions, that determine his place among men. If it be
true that one unimpeachable fact will set aside the most plausible
opposing theory, then Brown's fame will not survive. The facts of his
life impeach the popular verdict.
Very truly yours,
Hill P. Wilson.
General D. W. Wilder, Hiawatha, Kansas.
APPENDIX II
RECOLLECTIONS OF THE JOHN BROWN RAID
BY THE HON. ALEXANDER R. BOTELER, A
VIRGINIAN WHO WITNESSED THE FIGHT
Taken from The Century
On entering the room where John Brown was, I found him alone,
lying on the floor on his left side, and with his back turned toward
me. The right side of his face was smeared with blood from a sword
cut on his head, causing his grim and grizzled countenance to look
like that of some aboriginal savage with his war-paint on.
Approaching him I began the conversation with the inquiry:
"Captain Brown, are you hurt anywhere except on the head?"
"Yes, in my side, here," said he, indicating the place with his hand.
I then told him that a surgeon would be in presently to attend to his
wounds, and expressed the hope that they were not very serious.
Thereupon he asked me who I was, and on giving him my name he
muttered as if speaking to himself.
"Yes, yes—I know you now—member of congress—this district."
I then asked the question:
"Captain, what brought you here?"
"To free your slaves," was the reply.
"How did you expect to accomplish it with the small force you
brought with you?"
"I expected help."
"Where, whence, and from whom, Captain, did you expect it?"
"Here and from elsewhere," he answered.
"Did you expect to get assistance from whites here as well as from
the blacks?" was my next question.
"I did," he replied.
"Then," said I, "you have been disappointed in not getting it from
either?"
"Yes," he muttered, "I have—been—disappointed."
Then I asked him who planned his movement on Harper's Ferry, to
which he replied: "I planned it all myself," and upon my remarking
that it was a sad affair for him and the country, and that I trusted no
one would follow his example by undertaking a similar raid, he made
no response. I next inquired if he had any family besides the sons
who accompanied him on his incursion, to which he replied by telling
me he had a wife and children in the State of New York at North
Elba, and on my then asking if he would like to write to them and let
them know how he was, he quickly responded:
"Yes, I would like to send them a letter."
"Very well," I said, "you doubtless will be permitted to do so. But,
Captain," I added, "probably you understand that, being in the
hands of the civil authorities of the State, your letters will have to be
seen by them before they can be sent."
"Certainly," he said.
"Then, with that understanding," continued I. "There will, I am sure,
be no objection to your writing home; and although I have no
authority in the premises, I promise to do what I can to have your
wishes in that respect complied with."
"Thank you—thank you, sir," he said repeating his acknowledgment
for the proffered favor and, for the first time, turning his head
toward me.
In my desire to hear him distinctly, I had placed myself by his side,
with one knee resting on the floor; so that, when he turned, it
brought his face quite close to mine, and I remember well the
earnest gaze of the gray eye that looked straight into mine. I then
remarked:
"Captain, we, too, have wives and children. This attempt of yours to
interfere with our slaves has created great excitement and naturally
causes anxiety on account of our families. Now, let me ask you: Is
this failure of yours likely to be followed by similar attempts to create
disaffection among our servants and bring upon our homes the
horrors of a servile war?"
"Time will show," was his significant reply.
Just then a Catholic priest appeared at the door of the room. He had
been administering the last consolations of religion to Quinn, the
marine, who was dying in the adjoining office; and the moment
Brown saw him he became violently angry, and plainly showed, by
the expression of his countenance, how capable he was of feeling
"hatred, malice, and all uncharitableness."
"Go out of here—I don't want you about me—go out!" was the
salutation he gave the priest, who, bowing gravely, immediately
retired. Whereupon I arose from the floor, and bidding Brown good-
morning, likewise left him.
In the entry leading to the room where Brown was, I met Major
Russell, of the marine corps, who was going to see him, and I
detailed to him the conversation I had just had. Meeting the major
subsequently he told me that when he entered the apartment Brown
was standing up—with his clothes unfastened—examining the wound
in his side, and that, as soon as he saw him, forthwith resumed his
former position on the floor; which incident tended to confirm the
impression I had already formed, that there was a good deal of
vitality left in the old man, notwithstanding his wounds—a fact more
fully developed that evening after I had left Harper's Ferry for home,
when he had his spirited and historic talk with Wise, Hunter and
Vallandigham.
APPENDIX III
THE CONSTITUTION ADOPTED AT CHATHAM,
CANADA
Copy of the Constitution, adopted at Chatham, Canada, May 8, 1858.
Mason Report, p. 48.
PREAMBLE
ARTICLE I
QUALIFICATIONS FOR MEMBERSHIP
ARTICLE II
BRANCHES OF GOVERNMENT
ARTICLE III
LEGISLATIVE
ARTICLE IV
EXECUTIVE
ARTICLE V
JUDICIAL
ARTICLE VI
VALIDITY OF ENACTMENTS
ARTICLE VII
COMMANDER-IN-CHIEF
ARTICLE VIII
OFFICERS
ARTICLE IX
SECRETARY OF WAR
ARTICLE X
CONGRESS OR HOUSE OF REPRESENTATIVES
ARTICLE XI
ARTICLE XII
SPECIAL DUTIES
ARTICLE XIII
ARTICLE XIV
ARTICLE XV
IMPEACHMENT OF JUDGES
ARTICLE XVI
ARTICLE XVII
FURTHER DUTIES
It shall be the duty of the President and Secretary of State, to find
out (as soon as possible) the real friends, as well as the enemies of
this organization in every part of the country; to secure among
them, innkeepers, private postmasters, private mail contractors,
messengers and agents: through whom may be obtained correct and
regular information, constantly; recruits for the service, places of
deposit and sale; together with needed supplies: and it shall be
matter of special regard to secure such facilities through the
Northern States.
ARTICLE XVIII
ARTICLE XIX
DUTY OF PRESIDENT—CONTINUED
ARTICLE XX
THE VICE-PRESIDENT
The Vice-President shall be the presiding officer of the Provisional
Congress and in case of tie shall give the casting vote.
ARTICLE XXI
VACANCIES
ARTICLE XXII
PUNISHMENT OF CRIMES
ARTICLE XXIII
ARMY APPOINTMENTS
ARTICLE XXIV
COURT-MARTIALS
ARTICLE XXV
SALARIES
ARTICLE XXVI
TREATIES OF PEACE
Before any treaty of peace shall take effect, it shall be signed by the
President and Vice-President, the Commander-in-Chief, a majority of
the House of Representatives, a majority of the Supreme Court, and
a majority of all general officers of the army.
ARTICLE XXVII
ARTICLE XXVIII
PROPERTY
ARTICLE XXIX
ARTICLE XXX
ARTICLE XXXI
ARTICLE XXXII
PRISONERS
ARTICLE XXXIII
VOLUNTARIES
All persons who may come forward and shall voluntarily deliver up
their slaves, and have their names registered on the Books of the
organization, shall, so long as they continue at peace, be entitled to
the fullest protection of person and property, though not connected
with this organization, and shall be treated as friends, and not
merely as persons neutral.
ARTICLE XXXIV
NEUTRALS
ARTICLE XXXV
NO NEEDLESS WASTE
ARTICLE XXXVI
PROPERTY CONFISCATED
The entire and real property of all persons known to be acting either
directly or indirectly with or for the enemy, or found in arms with
them, or found wilfully holding slaves, shall be confiscated and
taken, whenever and wherever it may be found, in either free or
slave States.
ARTICLE XXXVII
DESERTION
ARTICLE XXXVIII
ARTICLE XXXIX
All persons connected in any way with this organization, and who
may be entitled to full protection under it, shall be held as under
obligation to labor in some way for the general good, and any
persons refusing, or neglecting so to do, shall on conviction receive a
suitable and appropriate punishment.
ARTICLE XL
IRREGULARITIES
ARTICLE XLI
CRIMES
Persons convicted of the forcible violation of any female prisoner
shall be put to death.
ARTICLE XLII
The marriage relation shall be at all times respected, and the families
kept together as far as possible, and broken families encouraged to
re-unite, and intelligence offices established for that purpose,
schools and churches established, as soon as may be, for the
purpose of religious and other instructions; and the first day of the
week regarded as a day of rest and appropriated to moral and
religious instruction and improvement; relief to the suffering,
instruction of the young and ignorant, and the encouragement of
personal cleanliness; nor shall any person be required on that day to
perform ordinary manual labor, unless in extremely urgent cases.
ARTICLE XLIII
ARTICLE XLIV
ARTICLE XLV
PERSONS TO BE SEIZED
ARTICLE XLVI
ARTICLE XLVII
NO PLURALITY OF OFFICES
ARTICLE XLVIII
OATH
ebookbell.com