Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 1: Image classification #19

Closed
datalass1 opened this issue Jan 30, 2019 · 3 comments
Closed

Lesson 1: Image classification #19

datalass1 opened this issue Jan 30, 2019 · 3 comments

Comments

@datalass1
Copy link
Owner

The key outcome of this lesson is that we'll have trained an image classifier which can recognize pet breeds at state of the art accuracy. The key to this success is the use of transfer learning, which will be a key platform for much of this course. We'll also see how to analyze the model to understand its failure modes. In this case, we'll see that the places where the model is making mistakes is in the same areas that even breeding experts can make mistakes.

@datalass1
Copy link
Owner Author

datalass1 commented Jan 30, 2019

Some info on GPUs

By Rachel Thomas
CUDA and OpenCL are the two main ways for programming GPUs. CUDA is by far the most developed, has the most extensive ecosystem, and is the most robustly supported by deep learning libraries. CUDA is a proprietary language created by Nvidia, so it can’t be used by GPUs from other companies. When fast.ai recommends Nvidia GPUs, it is not out of any special affinity or loyalty to Nvidia on our part, but that this is by far the best option for deep learning.

Things noticeably changed so far on the fastai experience compared with the previous version

The interface for the lessons has a pythonic feel to it, like geopandas, with the blue vertical bar on the left for index.
I can enlarge the videos to fill the desired browser space.
And there is a summary and lots of links on each lesson page.

Introductions 0-10mins

why fastai etc

Lesson-1 Pets (10mins - 47mins)

Always start a jupyter notebook with these three lines of code:

%reload_ext autoreload
%autoreload 2
%matplotlib inline

the % is special directives to jupyter notebooks. If someone changes the code while running, reload. If someone plots something, plot it in Jupyter notebook.

PyTorch is one of the most popular libraries in the world. More modern than Tensorflow.

Fastai library sits on top of PyTorch to do things faster and easily.
4 Core uses: Computer Vision, natural language text, tabular data and collaborative filtering.
import * is great, does not agree with PEP8 software engineering practice but in Data Science the rules are different, there is a need to interactively experiment.

Looking at the Oxford-IIIT Pet Dataset, an academic dataset and interestingly in 2012 gained an accuracy of 59.21%. Now let's find out how accurate we can learn to differentiate between the 37 distinct categories of dog and cat breeds. No longer looking at a binary classification, dog vs cats problem, is now too easy. The models are doing so well and getting everything right.

untar data function, pass in a URL which will download and extract the data!
path set a path and using new fastai functionality to list the files. Cool.
images and annotations files exist. In images the filenames are the labels, so regular expressions will extract the label from the text.

So lets get the data we need to build the model using the ImageDataBunch object. This object will contain two or three datasets: train, validation and test data.
data = ImageDataBunch.from_name_re(path_img, fnames, re_pattern, ds_tfms=get_transforms(), size=224)

The size of the images is important, a GPU has to apply the same instruction to a whole bunch of things/images at the same time. As a result all the images have to be the same size. 224x224 is a very common size for machine learning models. And this will do something called centre cropping, but data augmentation can change this.

Normalization means that you will have the same standard deviation. The pixel values will start out from 0-255, so the channels (commonly RGB) will have a mean of 0 and standard deviation of 1.

Let's look at the data:
data.show_batch(rows=3, figsize=(7,6)

Let's look at the data classes:
data.classes
using len on this will check you have the 37 classes.

Train the model using resnet34.
A model is trained in fastai on a learner.
learn = ConvLearner(data, models.resnet34, metrics=error_rate)
the notebook shows:
learn = create_cnn(data, models.resnet34, metrics=error_rate)
Tell the learner a couple of things: what is data (use the DataBunch) and what is the model. The metrics will also be returned.

resnet34 will train faster than resnet50. Start with the smaller one and see if it is good enough. The Downloading shows that resnet is a pretrained model based on ImageNet, and it will already know a lot about what pets look like.
ImageNet is so good that the resnet classifier is so good at recognising smaller datasets. But we will need to be aware of overfitting. By using a validation set, we use the model on a set of images that the model has never seen.

Now time to fit the model.
learn.fit_one_cycle(4)
This was recently developed compared to last years course. The number 4 decides how many times we go through the full dataset as an epoch. Starting out with four the error rate is looking pretty good at 6%, so 94% of the time we get recognition correct.

Lessons learnt from previous fastai courses (47min - 1hr:07mins)

The basic feedback is that the code needs to be run, run run run the code.
Also focus what goes in and what goes out from the code.
fastai is used a lot, use the documentation. Compared to Keras fastai has 5 lines of code and Keras has 31 lines of code.

Natural Language Semantic Code Search in GitHub is using the fastai library.
Sara Hooker, set up a project to monitor Kenyan rainforests listening to chainsaw noises, stopping illegal deforestation. Read the Deep Learning book by Ian Goodfellow.
Neural Net Music Generator at OpenAI by Christine Mcleavey Payne.

"Pick one project, do it really well, make it fantastic!" Jeremy Howard

Combine your domain/professional knowledge with the deep learning skills from the fastai course.

Another great example is gendered facial recognition by a previous female student. Most AI researchers a white men, IBMs system is 99.8% accurate on white faced men, whereas it is 65% accurate on black faced women.

An app for the blind which will tell you what it sees.

The fastai algorithm was used on the ImageNet database in 18minutes using 16 AWS instances, at a total compute cost of around $40!!!

Back to Lesson-1 Pets (1hr:07mins - 1hr:30mins)

Save the model and its weights: ```learn.save('stage-1')

Now what comes out of the model? Something to get really good at. Use this class for classification and interpretation, learn is the model:
interp = ClassificationInterpretation.from_learner(learn)

visualisation: plot top loss. How good is your prediction?
Check out fastai documentation to understand how to use fastai functions. Click on source to view the source code for the functions :)

the confusion matrix shows that the model was almost right all the time, so a confusion matrix won't always be that useful.

Use the most confused function to grab the most confused classifications.

The reason this CNN ran so quickly was that we only trained the last few layers. Now we want to go back and train the full model. Call unfreeze.
Why did the model get worse? Look at Zeilers paper on Visualizing and Understanding Convolutional Networks.
We can visualise the actual coefficients in the first layer, and so on. We can see what bits of the image were activated, a diagonal line for example. As we move through the model we are able to find different levels of semantic complexity, fluffy stuff, geometric objects to then finding specific breeds of dog as the model improves.

So reload the model and find the learning rate the thing that ficture out the fastest I can train the model without making it zip off of the rails and fall apart.
lr_find will plot how quickly am I updating the parameters in my model. Once the learning rate get 10-4 the learning rate gets worse.
learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-4)) to train with a lower learning rate for the first layers then at the 10-4 for the latter layers. The model improves!

resnet50 is bigger than resnet34, so if you run out of memory this is normal.

Another Dataset (1hr30mins -

A guide is being created on how to download images from google images!

How to create labels in lots of different ways with MNIST
ImageNet style data filing which is a directory with the labels.
Or it may come with a csv, the label is 0 or 1 values for is it a 7 or not?

The documentation for fastai doesn't just tell you what to do it will tell you how to do it step by step.
The really cool thing is that there are jupyter notebooks for all the docs on fastai/fastai_docs on GitHub - comment: no it doesn't.

Questions

(1hr06mins) Why resnet not another architecture? Wide ranging and wins a lot of competitions on Kaggle.

(1hr37mins) Will the fastai library use multi GPU in parallel by default? It will with multi CPU, multi GPU may be covered in part two of the fastai course.

(1hr38mins) Can the library use 3D data? Yes is can!

Homework:

Run the lesson-1 notebook and then get own dataset and make a notebook to do an image classification.

@datalass1
Copy link
Owner Author

datalass1 commented Jan 31, 2019

Transferring files from Linux instances to local repo

Transferring files using the gcloud command-line tool
gcloud compute scp jupyter@my-fastai-instance:~/tutorials/fastai/course-v3/nbs/dl1/lesson1v3-pets-20190131.ipynb ~/home/ubuntu/git/fastai/dl1/

An error:
ERROR: (gcloud.compute.ssh) Could not fetch resource: - Insufficient Permission

The solution:
It looks like the attempt to SSH is recognising the instance in your project, but the user doesn't have the required permissions to access the machine. Run:
$ gcloud auth login
And completing the web-based authorization to ensure you are attempting to access the machine as the correct (authenticated) user.

Another error:
ERROR: (gcloud.compute.scp) Could not fetch resource:

  • The resource 'projects/rek-fastai/zones/us-west1-b/instances/my-fastai-instance' was not found

Tried on local and VM:
gcloud config list compute/region to view the region
gcloud config set compute/region us-west2 to set a new region

To see the full GCP config for a project:
gcloud config list
To change zone:
gcloud config set compute/zone us-west2-b

Finally:
gcloud compute scp jupyter@my-fastai-instance:~/tutorials/fastai/course-v3/nbs/dl1/lesson1v3-pets-20190131.ipynb ~/home/ubuntu/git/fastai/dl1/

helpful pages:
change region and zone
change metadata
how to transfer files to instances

@datalass1
Copy link
Owner Author

datalass1 commented Jan 31, 2019

close issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant