Lesson 1 In-Class Discussion ✅

It seems that you put too much data on your GPU RAM.

Try to check with the bash command nvidia-smi (or I like to use gpustat) before and after the operation causing the CUDA memory error if this is the case.
You can call it directly from your jupyter notebook with !nvidia-smi (with “!” you can run bash commands in a jupyter notebook).

Also restart the notebook kernel (shortcut Esc+00) and if this does not help your entire system.

If it is the case, try decreasing the bs and/or the image size.

I hope this helps. :slight_smile:

@prajjwal i think jeremy will cover that in the coming lessions. However if u want to give it a shot, refer to this link in the documentation

Is there a way to quickly spot the possible errors in the labels of the validation set data? (Similarly also errors in the training data, though that should be less of a problem.) The mislabeled validation data are more likely to be misclassified by any deep network that predicts them, so maybe one could look at the intersection of the wrong predictions of different models, especially in the intersection of the confident predictions that are wrong. Does anyone have any links to such literature, blogs, or past fastai threads?

2 Likes

This helps. It worked when I changed the image size. Thanks a lot.

can you give a brief explanation of what its trying to do?

Have a look at this question which focuses on semantic segmentation.

1 Like

So Guys, Jeremy has mentioned about issues with images being rectangular and not square. What is it the exact issue that happens if the images do not have a X * X resolution and have X * Y resolution ?

1 Like

Hi Daniele,

Usually the first parameter of DataBunch constructors (“path”) refers to DataBunch root folder.

Example:
data = ImageDataBunch.from_lists(path=pathToRoot, fnames=files, labels=labels, valid_pct=0.2, test='test', ds_tfms=tfms, bs=32)

In all the sample notebooks, the path folder contains: both training data and the saved models (in “models” subfolder).
For example:

  • learn.save('last') will write the file “last.pth” inside the “models” subfolder in path
  • learn.load('last') will read it

NB: usually fast.ai samples has this “root folder” inside ~/.fastai/data
(ie: ~/.fastai/data/mnist_sample)

Link to working sample:

1 Like

I did, that’s why I asked for example that’s proven to work better when added as head.

Sure! In the first lesson the architecture (=model parameter) has been loaded from prebuilt one:

learn = ConvLearner(data, models.resnet50, metrics=error_rate)

But you can define you own using fast.ai (https://docs.fast.ai/layers.html#layers) in a way very similar to Keras:

model = nn.Sequential(
    nn.Conv2d(3,  16, kernel_size=3, stride=2, padding=1), nn.ReLU(),
    nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(),
    nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1), nn.ReLU(),
    nn.AdaptiveAvgPool2d(1),
)

For what I’ve understand fastai library wraps and simplify PyTorch as Keras wraps and simplify Tensorflow.

Says that, I think that pretrained Resnet34 model is a good starting point to address the bioimaging dataset you’ve proposed.
Of course, you have to unfreeze almost all layers (probably I would keep the first 3 to 15 layers frozen as starting point), and train with A LOT images, normalizing on new dataset and not relying on imagenet normalization.

1 Like

I’m playing with some data from kaggle whose evaluation is based on macro F1 score. Do we have a f1_score metric in fastai? I’ve tried f1_score from sklearn.metrics but it doesn’t seem to work

Sure!

http://docs.fast.ai/metrics.html#Fbeta

3 Likes

Thank you!

I downloaded images (my dataset) from google images following @lesscomfortable 's wonderful jupyter notebook. I am using AWS Sagemaker as my platform. I got a 66% error rate, which is bad :frowning: . On looking at the images (show_batch), I found some irrelevant images. How do I remove irrelevant images from my dataset?

I have 6 classes and 300 images in each class. Should I open each image and verify if they are relevant or not? Is it possible to download the dataset from sagemaker to my laptop, so that I can quickly delete irrelevant images?

2 Likes

Hey! You have two options:

  1. You can inspect them in your notebook by running .show_batch() a number of times. Then you can delete the ones you don’t need with os.remove(filename)

  2. A smarter way is to train a model and then plot the images that got the top losses with .plot_top_losses(). These are the best candidates to be deleted from the dataset because your model is having trouble classifying them. You have to be careful here, only delete images that do not correspond to the label they have assigned. If you delete other images you would be helping your model overfit (in the extreme, if you delete every misclassified image, you would artificially achieve 100% accuracy in the validation set). You can get the filenames of the top losses by running the following command and then delete them by running
    os.remove(filename):

3 Likes

Created a lesson 1 notes - somewhere between @PoonamV’s notes and @stas’s transcript:

16 Likes

I know this was answered, but here would be my way of solving @kofi’s / @Galactrion’s questions:

for number of training examples:

len(data.y)

For the actual classes

data.classes has those, so

num_classes = len(data.classes)

data.c has the same number ready for you from fastai. :wink:

to get a list of the number of examples per class I leverage pandas:

pd.concat([pd.Series(data.classes), pd.Series(data.train_ds.y).value_counts()], axis=1)

now thanks to @raghavab1992’s reply there is an even simpler version using data.class2idx

pd.Series(data.class2idx).map(pd.Series(data.train_ds.y).value_counts())

The key here is using the .value_counts() method on the training or validation ys. In order to use that, we have to create a pandas Series object. (pd.Series(data.train_ds.y).value_counts())
The rest is for replacing the class numbers with class names.
If you want them sorted by number per class, stick .sort_values(1) or .sort_values(1, ascending=False) on the end of that line. The output looks something like this:

image

The first number is the class-ID which can be ignored mostly.

9 Likes

Like it better, will edit my answer to include your method for the number of classes.

Sure,

image_extensions = set(k for k,v in mimetypes.types_map.items() if v.startswith('image/'))

it uses a nifty built-in python library called mimetypes. It can be found here: https://docs.python.org/3/library/mimetypes.html

It uses the mimetypes.types_map function to get this output:

{'.a': 'application/octet-stream',
 '.ai': 'application/postscript',
 '.aif': 'audio/x-aiff',
 '.aifc': 'audio/x-aiff',
 '.aiff': 'audio/x-aiff',
 '.au': 'audio/basic',
 '.avi': 'video/x-msvideo',
 '.bat': 'text/plain',
 '.bcpio': 'application/x-bcpio',
 '.bin': 'application/octet-stream',
 '.bmp': 'image/x-ms-bmp',
 '.c': 'text/plain',
 '.cdf': 'application/x-netcdf',
 '.cpio': 'application/x-cpio',
 '.csh': 'application/x-csh',
 '.css': 'text/css',
 '.csv': 'text/csv',
 '.dll': 'application/octet-stream',
 '.doc': 'application/msword',
 '.dot': 'application/msword',
 '.dvi': 'application/x-dvi',
 '.eml': 'message/rfc822',
 '.eps': 'application/postscript',
 '.etx': 'text/x-setext',
 '.exe': 'application/octet-stream',
 '.gif': 'image/gif',
 '.gtar': 'application/x-gtar',
 '.h': 'text/plain',
 '.hdf': 'application/x-hdf',
 '.htm': 'text/html',
 '.html': 'text/html',
 '.ico': 'image/vnd.microsoft.icon',
 '.ief': 'image/ief',
 '.jpe': 'image/jpeg',
 '.jpeg': 'image/jpeg',
 '.jpg': 'image/jpeg',
 '.js': 'application/javascript',
 '.json': 'application/json',
 '.ksh': 'text/plain',
 '.latex': 'application/x-latex',
 '.m1v': 'video/mpeg',
 '.m3u': 'application/vnd.apple.mpegurl',
 '.m3u8': 'application/vnd.apple.mpegurl',
 '.man': 'application/x-troff-man',
 '.me': 'application/x-troff-me',
 '.mht': 'message/rfc822',
 '.mhtml': 'message/rfc822',
 '.mif': 'application/x-mif',
 '.mov': 'video/quicktime',
 '.movie': 'video/x-sgi-movie',
 '.mp2': 'audio/mpeg',
 '.mp3': 'audio/mpeg',
 '.mp4': 'video/mp4',
 '.mpa': 'video/mpeg',
 '.mpe': 'video/mpeg',
 '.mpeg': 'video/mpeg',
 '.mpg': 'video/mpeg',
 '.ms': 'application/x-troff-ms',
 '.nc': 'application/x-netcdf',
 '.nws': 'message/rfc822',
 '.o': 'application/octet-stream',
 '.obj': 'application/octet-stream',
 '.oda': 'application/oda',
 '.p12': 'application/x-pkcs12',
 '.p7c': 'application/pkcs7-mime',
 '.pbm': 'image/x-portable-bitmap',
 '.pdf': 'application/pdf',
 '.pfx': 'application/x-pkcs12',
 '.pgm': 'image/x-portable-graymap',
 '.pl': 'text/plain',
 '.png': 'image/png',
 '.pnm': 'image/x-portable-anymap',
 '.pot': 'application/vnd.ms-powerpoint',
 '.ppa': 'application/vnd.ms-powerpoint',
 '.ppm': 'image/x-portable-pixmap',
 '.pps': 'application/vnd.ms-powerpoint',
 '.ppt': 'application/vnd.ms-powerpoint',
 '.ps': 'application/postscript',
 '.pwz': 'application/vnd.ms-powerpoint',
 '.py': 'text/x-python',
 '.pyc': 'application/x-python-code',
 '.pyo': 'application/x-python-code',
 '.qt': 'video/quicktime',
 '.ra': 'audio/x-pn-realaudio',
 '.ram': 'application/x-pn-realaudio',
 '.ras': 'image/x-cmu-raster',
 '.rdf': 'application/xml',
 '.rgb': 'image/x-rgb',
 '.roff': 'application/x-troff',
 '.rtx': 'text/richtext',
 '.sgm': 'text/x-sgml',
 '.sgml': 'text/x-sgml',
 '.sh': 'application/x-sh',
 '.shar': 'application/x-shar',
 '.snd': 'audio/basic',
 '.so': 'application/octet-stream',
 '.src': 'application/x-wais-source',
 '.sv4cpio': 'application/x-sv4cpio',
 '.sv4crc': 'application/x-sv4crc',
 '.svg': 'image/svg+xml',
 '.swf': 'application/x-shockwave-flash',
 '.t': 'application/x-troff',
 '.tar': 'application/x-tar',
 '.tcl': 'application/x-tcl',
 '.tex': 'application/x-tex',
 '.texi': 'application/x-texinfo',
 '.texinfo': 'application/x-texinfo',
 '.tif': 'image/tiff',
 '.tiff': 'image/tiff',
 '.tr': 'application/x-troff',
 '.tsv': 'text/tab-separated-values',
 '.txt': 'text/plain',
 '.ustar': 'application/x-ustar',
 '.vcf': 'text/x-vcard',
 '.wav': 'audio/x-wav',
 '.webm': 'video/webm',
 '.wiz': 'application/msword',
 '.wsdl': 'application/xml',
 '.xbm': 'image/x-xbitmap',
 '.xlb': 'application/vnd.ms-excel',
 '.xls': 'application/vnd.ms-excel',
 '.xml': 'text/xml',
 '.xpdl': 'application/xml',
 '.xpm': 'image/x-xpixmap',
 '.xsl': 'application/xml',
 '.xwd': 'image/x-xwindowdump',
 '.zip': 'application/zip'}

Then, it loops through each of these (k = extension, v = extension type) and checks to see if the extension type (v) starts with image (image/). If it does, it adds the extension to a set.

This is what is stored in the image_extensions variable.

3 Likes

Another thing you should look for in your images is any systematic bias. I was trying to classify sport pictures. It turns out that team pictures exist more in the hockey/lacrosse data sets I had built from a Google search across ten sports - so any other sport was then more likely to classify a team photo as one of these sports rather than the correct one. I refined my search to look for action images - then weeded out the team shots. Same for crowd scenes, cheerleaders and general stadium shots - which appeared more often in baseball.

2 Likes