Wiki: Lesson 1

(Quang Duong) #186

Try updating your fastai library first. Since the current module is

from .imports import *
from .torch_imports import *

def accuracy_np(preds, targs):
preds = np.argmax(preds, 1)
return (preds==targs).mean()

def accuracy(preds, targs):
preds = torch.max(preds, dim=1)[1]
return (preds==targs).float().mean()

def accuracy_thresh(thresh):
return lambda preds,targs: accuracy_multi(preds, targs, thresh)

def accuracy_multi(preds, targs, thresh):
return ((preds>thresh).float()==targs).float().mean()

Notice the different between your accuray() and the current up-to-date accuracy() function. Then, try using the current up-to-date notebook. For that part code it is now (using accuracy_np() )

log_preds,y = learn.TTA()
probs = np.mean(np.exp(log_preds),0)
accuracy_np(probs, y)

Further, check out the dimension of log_preds, and probs in your code. log_preds should be a 3D-array, then after average over the first dimension, probs should be a 2D-array. If probs is not a 2D-array, you get that error.

(Quang Duong) #187

To get through this course GPU is required. You can have it by using cloud services like AWS, Paperspace, GCP, … or you have it on your own computer. In either cases, you need to set up the environment to run the course’s notebooks. Set up might take a lot of time and hassle depending on what platform you choose. The easiest way is using Paperspace and the hardest way might be to configure your own computer. Please refer to this excellent repo where @reshama documented how to configure platforms for this course.

(Nyx) #188

Is there an alternative to Paperspace that does not require weeks of waiting to get access to the GPU instances?


Crestle is a quick and easy alternative to Paperspace @nyxynyx

(Quang Duong) #190

Google Cloud Compute is a good option. I follow this guide last week to setup the platform for this course and it works well. Just one thing is that it took 2-3 days waiting Google to approve GPU quotas request but you gonna got 300$ credits free which is 300 hours, enough to explore the course.

(Nikhil Utane) #191

Thanks for your response.
The notebook does a “!pip install fastai” at the beginning. So perhaps the package needs to be updated at the source.

In my case, I see log_preds is 2D array and probs is 1D. I understood what the error was hence replaced probs with log_preds and everything worked. Just need to figure out now if with that change it is working as intended. :slightly_smiling_face: Thanks.

(Said Aspen) #192

Found it perfect to listen to that CNN Mini Episode while training my network!

(Priit) #193

I set up Paperspace yesterday and there was actually very little time needed.
It took me 2 tries though. The first one got stuck loading when trying to open. I removed that one and created a new machine, that one loaded up in seconds.

(Vadim) #194

Hello all,
Just to share one more experience with paperspace - I signed up today around 6 hours ago, tried to set up a machine with no luck so far. After couple of hours of waiting I created a ticket to support, and that’s the info I got from them recently by email:

Thanks for reaching out, and apologies for the delay on your request. Typically we’re able to get users approved within a couple hours With a surge in requests for these instance types, we’re a little behind. We’re in the process of adding capacity right now, so please sit tight and you’ll get a notification the moment a seat is available. We appreciate your patience!

So, waiting…
Hope this helps.

P.S. Meanwhile setting up crestle took 2 minutes and 3 clicks, and allowed to successfully go through the 1st lesson :wink:

(Vikas Bahirwani) #195

I have questions about cell 15 from the Lesson1 Notebook about precomputed activations.

# Uncomment the below if you need to reset your precomputed activations# #shutil.rmtree(f'{PATH}tmp', ignore_errors=True)

  1. Why does, 2) in the next cell execute faster with precomputed activations and slower if I execute shutil.rmtree(f'{PATH}tmp', ignore_errors=True) ? Isnt it running on the same data in either case (i.e data from dogscats/train)

  2. If I delete the precomputed activations, how do I get them back? Do I have to do a fresh download of the “tmp” folder from the paperspace script?

  3. Just to clarify using precomputed activations - our network has already learned weights using a large image data and we are going to fine-tune them using our “train” folder. Correct?

Thanks a whole bunch

(Vikas Bahirwani) #196

My models folder is blank. Did anyone else have the same issue?

I am assuming it should not be blank because this is the “pretrained” model we start with in Lesson 1 yes?

(Ismaël Koné) #197


First let us explained the idea of precomputed activations
Let’s go back to the network, consider resnet50.


When finetuning a model we train only the last layer and previous ones (freeze layers) remain unchanged. So all computations on the freeze layers will be the same ie: the result of image 1 (of my training set) going from C1 to FC7 layers will never change or we do it many times with same images. In fact an epoch is a tour over all images of the training set so training during 10 epochs means I’ll do the same computation 10 times. This is a waste, so we take a shortcut, we precompute all training image results from 1st to last fixed layers, here from C1 to FC7 and save them: this is the precomputed activations. In doing so it’ll be fast because we’ll use results of fixed layers computations (precomputed activations) and compute only from FC7 to FC8 for each image.

So the question 1:
The response is clear, it will take much time because it’ll apply all fixed layers to all training images. But the next times will be faster.

Question 2:
No, you have to run fit but make sure to use the attribute precompute=True:, 2, precompute=True)

Question 3:
Precomputed activations and pretrained weights are two differents things.
Precomputed activations help speeding computations during training a network while pretrained weights are like the level of knowledge acquired by the network after some training on some data.

Hopefully, it helps.

(Agajan Torayev) #198

I have tried distinguishing between flying birds and flying planes. It worked perfect. 98% accuracy :slight_smile:

(Ismaël Koné) #199

Where is located your models folder? if possible show us the full path.

(Vikas Bahirwani) #200

Yes absolutely. And thank you for responding.

it is located in /data/dogscats/models

The Lecture Notes suggested that ResNet model will be downloaded and I did not see that when I ran the Lesson1Jupyter Notebook (hence the confusion). Search for “Let’s run the Model!” in the Lecture Notes you will see what I mean.

The confusion also stems from the fact that the Lesson1 notebook says we use “pretrained model” but I dont see it anywhere on the disk to be able to use it.

(Vikas Bahirwani) #201

I see. Let me see if I can rephrase it (confirm if I am understanding this right)

Let’s say that C1-FC7 are represented by the equation y= wx (x is the input image, w is the weights and y is the result aka activation)

If we did not save precomputed activations before, we “precompute” y i.e. we compute y first time an image x passes through the network. But, everytime after that we do not compute y but use the cached values saved in the tmp folder.

And last but not the least, the weights w.
Our jupyter notebook uses a pretrained model i.e. w learned from some other data sets. As we train it on our data set, w’s get updated but the starting point was the pretrained model we began with?

Is that accurate?
(Thank you very much for investing your time and giving a detailed reply)

(Vikas Bahirwani) #203

Hello Rachel,

Thank you for the summary post. Helps me keep my thoughts organized. I have one quick question

In Lesson 1 video, Jeremy asks us to download images of our choice from google and try the image classification. My worry is that if we download say 100 images (and use 50 to train, 20 for validation, and 20 to test) - wouldn’t the model over fit?

Perhaps I am missing something - What is the purpose of trying our own data?
Is there a number of images you recommend ?


(Vikas Bahirwani) #204

Could you share the number of samples you used for train/validation/test ?

(Agajan Torayev) #205

For the training set I have:
Birds: 409 images
Planes: 322 images

For the validation set I have:
Birds: 146 images
Planes: 80 items

I have grabbed images from

(Agajan Torayev) #206

In the lecture Jeremy Howard gives examples about cricket and baseball with 10 images, but personally I think you should try with more images.

The purpose of training our own data is to see for what kinds of images we can use this pretrained model. And besides that I found it more interesting when you train own model and see for which images model gave incorrect labels. Beside that with our own images we need to fine tune some hyparameters.