Lesson 3 In-Class Discussion ✅

I was wondering myself and anlysed it :

def accuracy(input, target):
    #The input.argmax(dim=1) selects the winner class pr pixel, thereby reducing 
    #the input shape from (bs, classes, width, height) => (bs, width, height)
    #We, therefore, have to reshape the target tensor from (bs, 1, width, height) to (bs, width, height)
    sz     = target.size()
    target = target.reshape( (sz[0],sz[2],sz[3]) )
    return (input.argmax(dim=1).flatten()==target.flatten()).float().mean()

I replaced the squeeze by reshape because it turns out that sueeze does not work if the last batch only contains one tensor, In that cases it squees out 2 dimension and not 1 ie (1,1,width,height) becomes width,height .


Getting errors while loading imdb data
While creating data_clas for imdb classifier getting this error:

data_clas = (TextFileList.from_folder(path)
             #grap all the text files in path
             #label them all with their folder, only keep 'neg' and 'pos'
             #split by folder between train and validation set
             #use `TextDataset`, the flag `is_fnames=True` indicates to read the content of the files passed
             #tokenize with defaults from fastai
            .numericalize(vocab = data_lm.vocab)
             #numericalize with the same vocabulary as our pretrained model
            .databunch(TextClasDataBunch, bs=50))
IndexError                                Traceback (most recent call last)
<ipython-input-90-5e9fe410bc17> in <module>
      4             .label_from_folder(classes=['neg','pos'])
      5              #label them all with their folder, only keep 'neg' and 'pos'
----> 6             .split_by_folder(valid='test')
      7              #split by folder between train and validation set
      8             .datasets()

~/anaconda2/envs/hindinlu/lib/python3.6/site-packages/fastai/data_block.py in datasets(self, dataset_cls, **kwargs)
    231     def datasets(self, dataset_cls:type=None, **kwargs)->'SplitDatasets':
    232         "Create datasets from the underlying data using `dataset_cls` and passing along the `kwargs`."
--> 233         if dataset_cls is None: dataset_cls = self.dataset_cls()
    234         train = dataset_cls(*self.train.items.T, **kwargs)
    235         dss = [train]

~/anaconda2/envs/hindinlu/lib/python3.6/site-packages/fastai/text/data.py in dataset_cls(self)
     35     def dataset_cls(self):
---> 36         return FilesTextDataset if isinstance(self.train.items[0][0],Path) else TextDataset
     38     def add_test_folder(self, test_folder:str='test', label:Any=None):

IndexError: index 0 is out of bounds for axis 0 with size 0

can any one suggest what file format or folder format should I follow?


1 Like

I am trying to work on TGS salt classification problem which has 2 classes in its mask. While working on it i have noticed following things which i don’t quite understand:

  1. lr_find(learn) gives me very different vales every time i run it
  2. My validation error is very very high train loss= .28 but validation 4075 or so. What can cause such behavior?

I am using dice as my accuracy metric with 2 classes.

makes sense Kaspar! I guess part of the problem was I was assuming the target should just be a tensor of (width, height), but if you do mask.data.shape you get a tensor(1, width, height), so it makes sense you need to squeeze that down. thanks :slight_smile:

This may have been discussed before…so please refresh my memory or set my understanding right.

Is there merit in converting input images to gray-scale before processing further? I thing the transforms could potentially do that but Jeremy may have shared his experience in the past that conversion to gray-scale does not help a lot.

Thanks a lot

I have trained language model by following the imdb notebook for Hindi text classification and haven’t changed any parameter. Now while loading the encoder for classification getting this error.


RuntimeError: Error(s) in loading state_dict for MultiBatchRNNCore:
	size mismatch for encoder.weight: copying a param with shape torch.Size([60002, 400]) from checkpoint, the shape in current model is torch.Size([226, 400]).
	size mismatch for encoder_dp.emb.weight: copying a param with shape torch.Size([60002, 400]) from checkpoint, the shape in current model is torch.Size([226, 400]).

not getting what’s wrong .


When I run the following code, I get an error.


data = (src.datasets().transform(get_transforms(), size=128).databunch().normalize(imagenet_stats))


/opt/anaconda3/lib/python3.6/site-packages/fastai/data_block.py in datasets(self, dataset_cls, **kwargs)
    232         "Create datasets from the underlying data using `dataset_cls` and passing along the `kwargs`."
    233         if dataset_cls is None: dataset_cls = self.dataset_cls()
--> 234         train = dataset_cls(*self.train.items.T, **kwargs)
    235         dss = [train]
    236         dss += [train.new(*o.items.T, **kwargs) for o in self.lists[1:]]

TypeError: __init__() missing 2 required positional arguments: 'x' and 'y'

Which arguments am I missing?

I am not smart enough to keep track of the tensors’ dimensions and ranks on the fly :slight_smile: . So, when something is not working, I start by checking tensors’ shapes.
In this particular case, it seems that the open_mask function converters the mask in a regular 3-channel image (rank-3 tensor), but since the mask is 1-channel, the batch shape is [bs, 1, h, w]. If you are using the standard fastai pipeline, you don’t have to worry about it, since its loss functions flatten the preds and target tensors. However, when defining customs metrics, a good practice should be checking tensors’ shapes.



I was wondering what this type of graph means since it seems to have a pretty flat slot in the beginning which seems to be a problem for finding a learning rate accurately. I’m using a dataset comparing images of different types of architecture towers:

According to some of the images there is maybe a classification issue due to the similarity of towers between like an office tower or a residential. Perhaps I have to do better seperation between the images. I was just wondering if there’s a better way to approach this since I’m getting this for training:

Total time: 11:51
epoch train_loss valid_loss error_rate
1 1.866207 1.394691 0.533537 (02:55)
2 1.614861 1.335335 0.524390 (02:59)
3 1.439242 1.272921 0.500000 (02:58)
4 1.305421 1.266789 0.496951 (02:57)

any help is appreciated. Thank you.

Jeremy mentioned this type of graph in the lecture, and you can see an example in the planets notebook. He suggested you find the point where it starts rising and then divide that learning rate by 10, so in your case I’d try 1e-4.

1 Like

error rate is going down, and val loss is still above train loss, which mean that you are still underfitting. There’s no reason to stop at 4 epochs. You should increase the number of epochs until you find that error rate plateaus or starts to go up, and train loss is lower than val loss.


export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

Before bring up the jupyter notebook

1 Like

Could someone help me with how to read a .json file? It has the labels for an image dataset. I tried using:


But I get the error: ValueError: arrays must all be same length

I may be misunderstanding your question, but only the filenames in the ImageFileList that match an entry in labels.csv will be kept (from the docs: “Note: This method will only keep the filenames that are both present in the csv file and in self.items.”)

1 Like

This is a deep learning course. GBMs are not deep learning.

@safekidda there is an ML course as well from fastai which covered random forest in detail…was inquiring if there is gonna be a part 2 for ML course covering other ML algos (like we have now for DL) when Jeremy suggested looking into ML course during lesson 3

I’m not on colab, but I’m running on a server at work using docker and I had the same issue.

It’s a locale issue in the environment. I solved mine by updating the locale in the docker build file. But if you’re in colab try something like this:

!locale-gen en_US.UTF-8
!export LANG=en_US.UTF-8 LANGUAGE=en_US.en LC_ALL=en_US.UTF-8

Ah right, fair enough. I suspect they’ll do the deep learning part 2 (v3) next. So, if they do, I think it’ll be a way off.

As an aside, I think near enough everything is better in deep learning these days. The only example I’ve seen (first hand) where non DL models were better was a tiny, very sparse tabular dataset. Even then it’s a pain compared to deep learning because the data prep is much more onerous and less forgiving. Much easier to just stick all your continuous variables as-is and your categorical variables though embeddings in a DL model.


I’m trying to use image segmentation to predict single image but the following code throws error:

src = (ImageFileList.from_folder(path)

data = (src.datasets(SegmentationDataset, classes=codes, div=True)
        .transform(get_transforms(), size=128, tfm_y=True)
        .databunch(bs=1, num_workers=0)

predictor = Learner.create_unet(data, models.resnet18)

and error is:

/usr/local/lib/python3.6/dist-packages/fastai/vision/data.py in __getitem__(self, idx)
    237         x,y = self.ds[idx]
    238         x = apply_tfms(self.tfms, x, **self.kwargs)
--> 239         if self.tfm_y: y = apply_tfms(self.tfms, y, **self.y_kwargs)
    240         return x, y

/usr/local/lib/python3.6/dist-packages/fastai/vision/image.py in apply_tfms(tfms, x, do_resolve, xtra, size, mult, resize_method, padding_mode, **kwargs)
    578         tfms = sorted(listify(tfms), key=lambda o: o.tfm.order)
    579         if do_resolve: _resolve_tfms(tfms)
--> 580         x = x.clone()
    581         x.set_sample(padding_mode=padding_mode, **kwargs)
    582         if size is not None:

AttributeError: 'int' object has no attribute 'clone'

Did anyone try to predict a single image for segmentation?

where do we get lesson 4 youtube link to the live stream? Cant find it on the forum, I am subscribed to the class for lesson 1,2 received emails from Jeremy. Cannot seem to find an announcement in this forum, can someone point me into the right direction please?