Loading

DGBRD

[Getting Started Notebook] DGBRD Challange

This is a Baseline Code to get you started with the challenge.

gauransh_k

You can use this code to start understanding the data and create a baseline model for further imporvments.

AIcrowd-Logo

Getting Started Code for DGBRD Challenge on AIcrowd

Author : Gauransh Kumar, Sanjay Pokkali

Import necessary packages

In [1]:
import numpy as np
import pandas as pd
import os
import cv2
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import random
import zipfile
from sklearn.utils import shuffle
from tqdm import tqdm_notebook
In [2]:
import fastai
from fastai import *
from fastai.vision import *
In [3]:
fastai.__version__
Out[3]:
'1.0.61'

Download the files

These include the train test and validation images as well the csv indexing them

In [4]:
!pip install aicrowd-cli
%load_ext aicrowd.magic
Collecting aicrowd-cli
  Downloading aicrowd_cli-0.1.10-py3-none-any.whl (44 kB)
     |████████████████████████████████| 44 kB 1.5 MB/s 
Collecting requests<3,>=2.25.1
  Downloading requests-2.27.1-py2.py3-none-any.whl (63 kB)
     |████████████████████████████████| 63 kB 1.5 MB/s 
Requirement already satisfied: toml<1,>=0.10.2 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (0.10.2)
Collecting rich<11,>=10.0.0
  Downloading rich-10.16.2-py3-none-any.whl (214 kB)
     |████████████████████████████████| 214 kB 32.3 MB/s 
Collecting pyzmq==22.1.0
  Downloading pyzmq-22.1.0-cp37-cp37m-manylinux1_x86_64.whl (1.1 MB)
     |████████████████████████████████| 1.1 MB 48.3 MB/s 
Requirement already satisfied: click<8,>=7.1.2 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (7.1.2)
Collecting GitPython==3.1.18
  Downloading GitPython-3.1.18-py3-none-any.whl (170 kB)
     |████████████████████████████████| 170 kB 49.5 MB/s 
Collecting requests-toolbelt<1,>=0.9.1
  Downloading requests_toolbelt-0.9.1-py2.py3-none-any.whl (54 kB)
     |████████████████████████████████| 54 kB 2.6 MB/s 
Requirement already satisfied: tqdm<5,>=4.56.0 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (4.62.3)
Collecting gitdb<5,>=4.0.1
  Downloading gitdb-4.0.9-py3-none-any.whl (63 kB)
     |████████████████████████████████| 63 kB 1.5 MB/s 
Requirement already satisfied: typing-extensions>=3.7.4.0 in /usr/local/lib/python3.7/dist-packages (from GitPython==3.1.18->aicrowd-cli) (3.10.0.2)
Collecting smmap<6,>=3.0.1
  Downloading smmap-5.0.0-py3-none-any.whl (24 kB)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (1.24.3)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (2021.10.8)
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (2.0.10)
Requirement already satisfied: pygments<3.0.0,>=2.6.0 in /usr/local/lib/python3.7/dist-packages (from rich<11,>=10.0.0->aicrowd-cli) (2.6.1)
Collecting colorama<0.5.0,>=0.4.0
  Downloading colorama-0.4.4-py2.py3-none-any.whl (16 kB)
Collecting commonmark<0.10.0,>=0.9.0
  Downloading commonmark-0.9.1-py2.py3-none-any.whl (51 kB)
     |████████████████████████████████| 51 kB 6.3 MB/s 
Installing collected packages: smmap, requests, gitdb, commonmark, colorama, rich, requests-toolbelt, pyzmq, GitPython, aicrowd-cli
  Attempting uninstall: requests
    Found existing installation: requests 2.23.0
    Uninstalling requests-2.23.0:
      Successfully uninstalled requests-2.23.0
  Attempting uninstall: pyzmq
    Found existing installation: pyzmq 22.3.0
    Uninstalling pyzmq-22.3.0:
      Successfully uninstalled pyzmq-22.3.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
google-colab 1.0.0 requires requests~=2.23.0, but you have requests 2.27.1 which is incompatible.
datascience 0.10.6 requires folium==0.2.1, but you have folium 0.8.3 which is incompatible.
Successfully installed GitPython-3.1.18 aicrowd-cli-0.1.10 colorama-0.4.4 commonmark-0.9.1 gitdb-4.0.9 pyzmq-22.1.0 requests-2.27.1 requests-toolbelt-0.9.1 rich-10.16.2 smmap-5.0.0
In [5]:
%aicrowd login
Please login here: https://api.aicrowd.com/auth/4Kpt0TAGJbf50lrNki4YlqEGVXUJanU1R2DqMMrvF7o
API Key valid
Saved API Key successfully!
In [6]:
!rm -rf data
!mkdir data
%aicrowd ds dl -c dgbrd -o data
In [7]:
!mkdir FinalData
# training data

with zipfile.ZipFile("data/train.zip","r") as zip_ref:
    zip_ref.extractall("FinalData/")
    
# test data

with zipfile.ZipFile("data/test.zip","r") as zip_ref:
    zip_ref.extractall("FinalData/")
    
# valdation data

with zipfile.ZipFile("data/val.zip","r") as zip_ref:
    zip_ref.extractall("FinalData/")

Check for corrupt images

In [8]:
root="FinalData/"

for path in os.listdir(root): 
    verify_images(root+path,delete=True)
In [9]:
import warnings
warnings.filterwarnings("ignore")

Loading Data

Loading CSV Data

Using pandas, we can directly load the csvs. We have created a val set for you, which has an even split of all the classes

In [10]:
train_df=pd.read_csv("data/train.csv")
val_df=pd.read_csv("data/val.csv")
test_df=pd.read_csv("data/test.csv")
In [11]:
train_df["filename"]=train_df["filename"]
val_df["filename"]=val_df["filename"]
test_df["filename"]=test_df["filename"]

Defining Transformations

When we do any sort of computer vision task, it is important to define carefully thought out transformations on the image. This is to allow our model to more accurately predict a wider range of images. Try experimenting with various other types of transformations. Click here to learn more

In [12]:
tfms = get_transforms(do_flip=True,flip_vert=False,max_zoom=1.05,max_warp=0.1)

Creating dataframes

We now load our data into dataframes and apply the transformations we defined above. For now we create the dataframes for the tran and val data.

In [13]:
root="FinalData/"
train_data = (ImageList.from_df(train_df,root+"train",cols=["filename"]) 
        .split_none()              
        .label_from_df(cols=["label"])            
        .transform(tfms, size=128))

val_data = (ImageList.from_df(val_df,root+"val",cols=["filename"]) 
        .split_none()              
        .label_from_df(cols=["label"])            
        .transform(tfms, size=128))

We now merge the two dataframes into one dataframe which contains train and val dataset.

In [14]:
train_data.valid=val_data.train
In [15]:
train_data
Out[15]:
LabelLists;

Train: LabelList (17609 items)
x: ImageList
Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128)
y: CategoryList
American_Staffordshire_terrier,English_springer,collie,briard,Doberman
Path: FinalData/train;

Valid: LabelList (962 items)
x: ImageList
Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128)
y: CategoryList
Pomeranian,Tibetan_terrier,Norfolk_terrier,Pomeranian,standard_poodle
Path: FinalData/val;

Test: None

We now create a databunch for the data that is to be used in training. A databunch in fastai is the data that is going to be passed into the neural network. While creating the databunch, we also define the batch size, number of workers, and we normalize the images according to imagenet_stats.

You can also leave the normalize as blank, and fastai will normalize according to the dataset. However since the imagenet dataset has over 1000 classes, it is very likely that imagenet has classes which overlap with our dataset

In [16]:
train_databunch=train_data.databunch(bs=16,num_workers=0).normalize(imagenet_stats)

We now create the test_data dataframe. We then add the test data to the databunch

In [17]:
test_data=ImageList.from_df(test_df,root+"test",cols=["filename"])

train_databunch.add_test(test_data)

Notice how the train_databunch has all the data we require to train the model, run validation, and to predict on!

In [18]:
train_databunch
Out[18]:
ImageDataBunch;

Train: LabelList (17609 items)
x: ImageList
Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128)
y: CategoryList
American_Staffordshire_terrier,English_springer,collie,briard,Doberman
Path: FinalData/train;

Valid: LabelList (962 items)
x: ImageList
Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128)
y: CategoryList
Pomeranian,Tibetan_terrier,Norfolk_terrier,Pomeranian,standard_poodle
Path: FinalData/val;

Test: LabelList (2009 items)
x: ImageList
Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128)
y: EmptyLabelList
,,,,
Path: FinalData/val
In [19]:
train_databunch.show_batch(rows=3, figsize=(8,10))
In [20]:
print(train_databunch.classes)
['Afghan_hound', 'African_hunting_dog', 'Airedale', 'American_Staffordshire_terrier', 'Appenzeller', 'Australian_terrier', 'Bedlington_terrier', 'Bernese_mountain_dog', 'Blenheim_spaniel', 'Border_collie', 'Border_terrier', 'Boston_bull', 'Bouvier_des_Flandres', 'Brabancon_griffon', 'Brittany_spaniel', 'Cardigan', 'Chesapeake_Bay_retriever', 'Chihuahua', 'Dandie_Dinmont', 'Doberman', 'English_foxhound', 'English_setter', 'English_springer', 'EntleBucher', 'Eskimo_dog', 'French_bulldog', 'German_shepherd', 'Gordon_setter', 'Great_Dane', 'Great_Pyrenees', 'Greater_Swiss_Mountain_dog', 'Ibizan_hound', 'Irish_setter', 'Irish_terrier', 'Irish_water_spaniel', 'Irish_wolfhound', 'Italian_greyhound', 'Japanese_spaniel', 'Kerry_blue_terrier', 'Labrador_retriever', 'Lakeland_terrier', 'Leonberg', 'Lhasa', 'Maltese_dog', 'Mexican_hairless', 'Newfoundland', 'Norfolk_terrier', 'Norwegian_elkhound', 'Norwich_terrier', 'Old_English_sheepdog', 'Pekinese', 'Pembroke', 'Pomeranian', 'Rhodesian_ridgeback', 'Rottweiler', 'Saint_Bernard', 'Saluki', 'Samoyed', 'Scotch_terrier', 'Scottish_deerhound', 'Sealyham_terrier', 'Shetland_sheepdog', 'Siberian_husky', 'Staffordshire_bullterrier', 'Sussex_spaniel', 'Tibetan_mastiff', 'Tibetan_terrier', 'Tzu', 'Walker_hound', 'Weimaraner', 'Welsh_springer_spaniel', 'West_Highland_white_terrier', 'Yorkshire_terrier', 'affenpinscher', 'basenji', 'basset', 'beagle', 'bloodhound', 'bluetick', 'borzoi', 'boxer', 'briard', 'bull_mastiff', 'cairn', 'chow', 'clumber', 'coated_retriever', 'coated_wheaten_terrier', 'cocker_spaniel', 'collie', 'dhole', 'dingo', 'giant_schnauzer', 'golden_retriever', 'groenendael', 'haired_fox_terrier', 'haired_pointer', 'keeshond', 'kelpie', 'komondor', 'kuvasz', 'malamute', 'malinois', 'miniature_pinscher', 'miniature_poodle', 'miniature_schnauzer', 'otterhound', 'papillon', 'pug', 'redbone', 'schipperke', 'silky_terrier', 'standard_poodle', 'standard_schnauzer', 'tan_coonhound', 'toy_poodle', 'toy_terrier', 'vizsla', 'whippet']

Define the Model

Now we come to the juicy part. We define our model here. FastAI has many pretrained models which you can find here. Try out different models and see which one works the best for this dataset! You can also create your own custom models using pytorch. More on that here.

In [21]:
from fastai.callbacks import *
In [22]:
learn = cnn_learner(train_databunch, models.resnet18, metrics=[error_rate, accuracy],model_dir="models/")
Downloading: "https://download.pytorch.org/models/resnet18-f37072fd.pth" to /root/.cache/torch/hub/checkpoints/resnet18-f37072fd.pth

Now that we've loaded our model, FastAI provides a convienient function to find the most efficient learning rate for your model at that time. This is what we have implemented below

In [23]:
learn.lr_find()
learn.recorder.plot(suggestion=True)
0.00% [0/1 00:00<00:00]
epoch train_loss valid_loss error_rate accuracy time

8.09% [89/1100 00:24<04:41 20.3025]
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
Min numerical gradient: 1.10E-02
Min loss divided by 10: 3.98E-03

TRAINING PHASE 🏋️

Alright enough talk and time to train. We define the number of epochs and we define a callback which saves the best model trained during this training phase based on the error_rate.

We will use fastai fit_one_cyle to train our model. To learn more about the function check here.

In [24]:
learn.fit_one_cycle(1,1.0E-03,callbacks=[ShowGraph(learn),SaveModelCallback(learn,monitor='error_rate',mode='min',
name="bestmodel")])
epoch train_loss valid_loss error_rate accuracy time
0 2.356557 1.582534 0.418919 0.581081 05:08
Better model found at epoch 0 with error_rate value: 0.4189189076423645.
In [25]:
learn.save('resnet18-5epochs-stage-1') # save model
In [26]:
learn.unfreeze() # unfreeze all layers
learn.lr_find() # find learning rate
learn.recorder.plot(suggestion=True) # plot learning rate
0.00% [0/1 00:00<00:00]
epoch train_loss valid_loss error_rate accuracy time

7.64% [84/1100 00:24<04:53 6.6014]
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
Min numerical gradient: 1.91E-06
Min loss divided by 10: 3.31E-05
In [27]:
learn.fit_one_cycle(1,6.31E-07,callbacks=[ShowGraph(learn),SaveModelCallback(learn,monitor='error_rate',mode='min',
name="bestmodel")])
epoch train_loss valid_loss error_rate accuracy time
0 2.238509 1.578089 0.438669 0.561331 05:38
Better model found at epoch 0 with error_rate value: 0.43866944313049316.

Lets try and figure out where our model messed up

Fastai provides a way to see which images our model is having trouble with.

In [28]:
interp = ClassificationInterpretation.from_learner(learn)

losses,idxs = interp.top_losses()

len(train_databunch.valid_ds)==len(losses)==len(idxs)
Out[28]:
True

As you can see here, our model is very accurate and doesn't mess up for many images

In [29]:
interp.plot_top_losses(9, figsize=(15,11))
In [30]:
interp.plot_confusion_matrix(figsize=(12,12), dpi=60)
In [31]:
interp.most_confused()
Out[31]:
[('collie', 'Shetland_sheepdog', 4),
 ('malamute', 'Eskimo_dog', 4),
 ('miniature_poodle', 'toy_poodle', 4),
 ('American_Staffordshire_terrier', 'Staffordshire_bullterrier', 3),
 ('Great_Pyrenees', 'kuvasz', 3),
 ('Ibizan_hound', 'basenji', 3),
 ('Siberian_husky', 'Eskimo_dog', 3),
 ('Tibetan_terrier', 'Lhasa', 3),
 ('Appenzeller', 'miniature_pinscher', 2),
 ('Australian_terrier', 'Norwich_terrier', 2),
 ('Boston_bull', 'toy_terrier', 2),
 ('Brittany_spaniel', 'Welsh_springer_spaniel', 2),
 ('Cardigan', 'Pembroke', 2),
 ('Chesapeake_Bay_retriever', 'haired_pointer', 2),
 ('Chihuahua', 'basenji', 2),
 ('English_setter', 'English_springer', 2),
 ('EntleBucher', 'Greater_Swiss_Mountain_dog', 2),
 ('Gordon_setter', 'Doberman', 2),
 ('Great_Pyrenees', 'Samoyed', 2),
 ('Labrador_retriever', 'Great_Pyrenees', 2),
 ('Lakeland_terrier', 'Airedale', 2),
 ('Lhasa', 'Tzu', 2),
 ('Rhodesian_ridgeback', 'redbone', 2),
 ('Rhodesian_ridgeback', 'vizsla', 2),
 ('Rottweiler', 'Doberman', 2),
 ('Scottish_deerhound', 'Irish_wolfhound', 2),
 ('Staffordshire_bullterrier', 'giant_schnauzer', 2),
 ('Tibetan_mastiff', 'Rottweiler', 2),
 ('Tzu', 'Tibetan_terrier', 2),
 ('Walker_hound', 'English_foxhound', 2),
 ('Walker_hound', 'beagle', 2),
 ('Welsh_springer_spaniel', 'Blenheim_spaniel', 2),
 ('Yorkshire_terrier', 'silky_terrier', 2),
 ('briard', 'Airedale', 2),
 ('briard', 'silky_terrier', 2),
 ('cairn', 'Norwich_terrier', 2),
 ('collie', 'Border_collie', 2),
 ('dingo', 'dhole', 2),
 ('giant_schnauzer', 'Scotch_terrier', 2),
 ('kuvasz', 'Great_Pyrenees', 2),
 ('standard_poodle', 'toy_poodle', 2),
 ('standard_schnauzer', 'miniature_schnauzer', 2),
 ('tan_coonhound', 'Rottweiler', 2),
 ('Afghan_hound', 'Newfoundland', 1),
 ('Afghan_hound', 'dhole', 1),
 ('Afghan_hound', 'otterhound', 1),
 ('African_hunting_dog', 'Scottish_deerhound', 1),
 ('Airedale', 'Rottweiler', 1),
 ('Airedale', 'tan_coonhound', 1),
 ('American_Staffordshire_terrier', 'African_hunting_dog', 1),
 ('American_Staffordshire_terrier', 'basset', 1),
 ('American_Staffordshire_terrier', 'pug', 1),
 ('Appenzeller', 'Bernese_mountain_dog', 1),
 ('Appenzeller', 'English_springer', 1),
 ('Appenzeller', 'Greater_Swiss_Mountain_dog', 1),
 ('Australian_terrier', 'Yorkshire_terrier', 1),
 ('Australian_terrier', 'silky_terrier', 1),
 ('Bedlington_terrier', 'haired_pointer', 1),
 ('Bernese_mountain_dog', 'Rottweiler', 1),
 ('Bernese_mountain_dog', 'collie', 1),
 ('Bernese_mountain_dog', 'groenendael', 1),
 ('Blenheim_spaniel', 'Irish_setter', 1),
 ('Blenheim_spaniel', 'Welsh_springer_spaniel', 1),
 ('Blenheim_spaniel', 'haired_fox_terrier', 1),
 ('Border_collie', 'Shetland_sheepdog', 1),
 ('Border_collie', 'Siberian_husky', 1),
 ('Border_collie', 'haired_fox_terrier', 1),
 ('Border_terrier', 'Airedale', 1),
 ('Bouvier_des_Flandres', 'Irish_water_spaniel', 1),
 ('Bouvier_des_Flandres', 'Scotch_terrier', 1),
 ('Bouvier_des_Flandres', 'Scottish_deerhound', 1),
 ('Brabancon_griffon', 'affenpinscher', 1),
 ('Brabancon_griffon', 'pug', 1),
 ('Brittany_spaniel', 'Blenheim_spaniel', 1),
 ('Cardigan', 'Saint_Bernard', 1),
 ('Cardigan', 'groenendael', 1),
 ('Cardigan', 'malamute', 1),
 ('Chesapeake_Bay_retriever', 'Irish_water_spaniel', 1),
 ('Chesapeake_Bay_retriever', 'coated_retriever', 1),
 ('Chesapeake_Bay_retriever', 'dhole', 1),
 ('Chesapeake_Bay_retriever', 'redbone', 1),
 ('Chesapeake_Bay_retriever', 'vizsla', 1),
 ('Chihuahua', 'English_setter', 1),
 ('Chihuahua', 'Ibizan_hound', 1),
 ('Dandie_Dinmont', 'Maltese_dog', 1),
 ('Dandie_Dinmont', 'Old_English_sheepdog', 1),
 ('Dandie_Dinmont', 'Scottish_deerhound', 1),
 ('Doberman', 'EntleBucher', 1),
 ('Doberman', 'German_shepherd', 1),
 ('Doberman', 'Mexican_hairless', 1),
 ('Doberman', 'miniature_pinscher', 1),
 ('Doberman', 'tan_coonhound', 1),
 ('English_foxhound', 'beagle', 1),
 ('English_setter', 'Brittany_spaniel', 1),
 ('English_setter', 'Great_Pyrenees', 1),
 ('English_setter', 'borzoi', 1),
 ('English_setter', 'haired_pointer', 1),
 ('English_springer', 'Blenheim_spaniel', 1),
 ('English_springer', 'English_foxhound', 1),
 ('English_springer', 'English_setter', 1),
 ('English_springer', 'Saint_Bernard', 1),
 ('EntleBucher', 'Doberman', 1),
 ('EntleBucher', 'Tibetan_mastiff', 1),
 ('Eskimo_dog', 'Norwegian_elkhound', 1),
 ('Eskimo_dog', 'Samoyed', 1),
 ('Eskimo_dog', 'Siberian_husky', 1),
 ('Eskimo_dog', 'dingo', 1),
 ('French_bulldog', 'Boston_bull', 1),
 ('French_bulldog', 'boxer', 1),
 ('French_bulldog', 'schipperke', 1),
 ('German_shepherd', 'Gordon_setter', 1),
 ('German_shepherd', 'dingo', 1),
 ('German_shepherd', 'pug', 1),
 ('Great_Dane', 'American_Staffordshire_terrier', 1),
 ('Great_Dane', 'Saluki', 1),
 ('Great_Dane', 'bloodhound', 1),
 ('Great_Dane', 'bluetick', 1),
 ('Greater_Swiss_Mountain_dog', 'Afghan_hound', 1),
 ('Greater_Swiss_Mountain_dog', 'Appenzeller', 1),
 ('Greater_Swiss_Mountain_dog', 'Bernese_mountain_dog', 1),
 ('Greater_Swiss_Mountain_dog', 'EntleBucher', 1),
 ('Greater_Swiss_Mountain_dog', 'Rottweiler', 1),
 ('Ibizan_hound', 'dingo', 1),
 ('Ibizan_hound', 'vizsla', 1),
 ('Ibizan_hound', 'whippet', 1),
 ('Irish_setter', 'Chesapeake_Bay_retriever', 1),
 ('Irish_setter', 'vizsla', 1),
 ('Irish_terrier', 'cairn', 1),
 ('Irish_terrier', 'miniature_poodle', 1),
 ('Irish_water_spaniel', 'miniature_poodle', 1),
 ('Irish_wolfhound', 'Border_terrier', 1),
 ('Irish_wolfhound', 'Lakeland_terrier', 1),
 ('Irish_wolfhound', 'Old_English_sheepdog', 1),
 ('Irish_wolfhound', 'Sealyham_terrier', 1),
 ('Irish_wolfhound', 'otterhound', 1),
 ('Irish_wolfhound', 'whippet', 1),
 ('Italian_greyhound', 'English_foxhound', 1),
 ('Italian_greyhound', 'Ibizan_hound', 1),
 ('Italian_greyhound', 'miniature_pinscher', 1),
 ('Italian_greyhound', 'vizsla', 1),
 ('Italian_greyhound', 'whippet', 1),
 ('Japanese_spaniel', 'Boston_bull', 1),
 ('Japanese_spaniel', 'English_springer', 1),
 ('Japanese_spaniel', 'papillon', 1),
 ('Labrador_retriever', 'Chesapeake_Bay_retriever', 1),
 ('Labrador_retriever', 'Lakeland_terrier', 1),
 ('Labrador_retriever', 'Staffordshire_bullterrier', 1),
 ('Lakeland_terrier', 'Irish_wolfhound', 1),
 ('Lakeland_terrier', 'Sealyham_terrier', 1),
 ('Lakeland_terrier', 'Siberian_husky', 1),
 ('Lakeland_terrier', 'toy_poodle', 1),
 ('Leonberg', 'Afghan_hound', 1),
 ('Leonberg', 'Welsh_springer_spaniel', 1),
 ('Leonberg', 'toy_poodle', 1),
 ('Lhasa', 'Tibetan_terrier', 1),
 ('Lhasa', 'West_Highland_white_terrier', 1),
 ('Lhasa', 'chow', 1),
 ('Lhasa', 'whippet', 1),
 ('Maltese_dog', 'Afghan_hound', 1),
 ('Maltese_dog', 'Great_Pyrenees', 1),
 ('Maltese_dog', 'coated_wheaten_terrier', 1),
 ('Maltese_dog', 'miniature_poodle', 1),
 ('Maltese_dog', 'toy_poodle', 1),
 ('Newfoundland', 'Border_collie', 1),
 ('Newfoundland', 'coated_retriever', 1),
 ('Newfoundland', 'toy_terrier', 1),
 ('Norfolk_terrier', 'Australian_terrier', 1),
 ('Norfolk_terrier', 'Lakeland_terrier', 1),
 ('Norfolk_terrier', 'West_Highland_white_terrier', 1),
 ('Norfolk_terrier', 'pug', 1),
 ('Norfolk_terrier', 'silky_terrier', 1),
 ('Norwegian_elkhound', 'Leonberg', 1),
 ('Norwegian_elkhound', 'pug', 1),
 ('Norwich_terrier', 'Afghan_hound', 1),
 ('Norwich_terrier', 'Maltese_dog', 1),
 ('Norwich_terrier', 'Tzu', 1),
 ('Old_English_sheepdog', 'briard', 1),
 ('Pekinese', 'Brabancon_griffon', 1),
 ('Pekinese', 'Great_Pyrenees', 1),
 ('Pekinese', 'Pomeranian', 1),
 ('Pekinese', 'keeshond', 1),
 ('Pomeranian', 'Lakeland_terrier', 1),
 ('Pomeranian', 'Samoyed', 1),
 ('Pomeranian', 'West_Highland_white_terrier', 1),
 ('Pomeranian', 'golden_retriever', 1),
 ('Rhodesian_ridgeback', 'Italian_greyhound', 1),
 ('Rhodesian_ridgeback', 'Lakeland_terrier', 1),
 ('Rhodesian_ridgeback', 'basenji', 1),
 ('Rhodesian_ridgeback', 'bloodhound', 1),
 ('Saint_Bernard', 'Blenheim_spaniel', 1),
 ('Saint_Bernard', 'Boston_bull', 1),
 ('Saint_Bernard', 'borzoi', 1),
 ('Saluki', 'German_shepherd', 1),
 ('Saluki', 'Pembroke', 1),
 ('Saluki', 'borzoi', 1),
 ('Saluki', 'dingo', 1),
 ('Saluki', 'golden_retriever', 1),
 ('Saluki', 'vizsla', 1),
 ('Samoyed', 'Great_Pyrenees', 1),
 ('Samoyed', 'Maltese_dog', 1),
 ('Samoyed', 'komondor', 1),
 ('Scotch_terrier', 'schipperke', 1),
 ('Scottish_deerhound', 'Sealyham_terrier', 1),
 ('Scottish_deerhound', 'Tibetan_terrier', 1),
 ('Scottish_deerhound', 'borzoi', 1),
 ('Scottish_deerhound', 'groenendael', 1),
 ('Scottish_deerhound', 'haired_pointer', 1),
 ('Sealyham_terrier', 'Dandie_Dinmont', 1),
 ('Sealyham_terrier', 'Old_English_sheepdog', 1),
 ('Sealyham_terrier', 'haired_fox_terrier', 1),
 ('Shetland_sheepdog', 'Pembroke', 1),
 ('Shetland_sheepdog', 'Weimaraner', 1),
 ('Siberian_husky', 'Saluki', 1),
 ('Siberian_husky', 'dingo', 1),
 ('Siberian_husky', 'malamute', 1),
 ('Siberian_husky', 'toy_terrier', 1),
 ('Staffordshire_bullterrier', 'Brabancon_griffon', 1),
 ('Staffordshire_bullterrier', 'Doberman', 1),
 ('Staffordshire_bullterrier', 'Great_Pyrenees', 1),
 ('Staffordshire_bullterrier', 'Labrador_retriever', 1),
 ('Sussex_spaniel', 'Airedale', 1),
 ('Sussex_spaniel', 'clumber', 1),
 ('Sussex_spaniel', 'vizsla', 1),
 ('Tibetan_mastiff', 'Labrador_retriever', 1),
 ('Tibetan_mastiff', 'Newfoundland', 1),
 ('Tibetan_terrier', 'Bouvier_des_Flandres', 1),
 ('Tibetan_terrier', 'Maltese_dog', 1),
 ('Tibetan_terrier', 'giant_schnauzer', 1),
 ('Tibetan_terrier', 'otterhound', 1),
 ('Tibetan_terrier', 'toy_poodle', 1),
 ('Tzu', 'Dandie_Dinmont', 1),
 ('Tzu', 'Sussex_spaniel', 1),
 ('Tzu', 'West_Highland_white_terrier', 1),
 ('Tzu', 'pug', 1),
 ('Walker_hound', 'Welsh_springer_spaniel', 1),
 ('Walker_hound', 'basenji', 1),
 ('Weimaraner', 'Italian_greyhound', 1),
 ('Weimaraner', 'Mexican_hairless', 1),
 ('Welsh_springer_spaniel', 'English_foxhound', 1),
 ('West_Highland_white_terrier', 'Sealyham_terrier', 1),
 ('Yorkshire_terrier', 'Tzu', 1),
 ('Yorkshire_terrier', 'cairn', 1),
 ('basenji', 'African_hunting_dog', 1),
 ('basenji', 'Brittany_spaniel', 1),
 ('basenji', 'Great_Dane', 1),
 ('basenji', 'Welsh_springer_spaniel', 1),
 ('basset', 'Bernese_mountain_dog', 1),
 ('basset', 'English_foxhound', 1),
 ('basset', 'Greater_Swiss_Mountain_dog', 1),
 ('basset', 'Welsh_springer_spaniel', 1),
 ('basset', 'bloodhound', 1),
 ('beagle', 'Blenheim_spaniel', 1),
 ('beagle', 'English_foxhound', 1),
 ('beagle', 'basset', 1),
 ('bloodhound', 'Irish_terrier', 1),
 ('bloodhound', 'redbone', 1),
 ('bluetick', 'Bedlington_terrier', 1),
 ('bluetick', 'Doberman', 1),
 ('borzoi', 'Blenheim_spaniel', 1),
 ('borzoi', 'Great_Pyrenees', 1),
 ('borzoi', 'Irish_wolfhound', 1),
 ('borzoi', 'clumber', 1),
 ('boxer', 'American_Staffordshire_terrier', 1),
 ('boxer', 'Rhodesian_ridgeback', 1),
 ('boxer', 'toy_terrier', 1),
 ('briard', 'Afghan_hound', 1),
 ('briard', 'Old_English_sheepdog', 1),
 ('cairn', 'West_Highland_white_terrier', 1),
 ('cairn', 'affenpinscher', 1),
 ('cairn', 'coated_wheaten_terrier', 1),
 ('clumber', 'English_setter', 1),
 ('coated_retriever', 'Bouvier_des_Flandres', 1),
 ('coated_retriever', 'Gordon_setter', 1),
 ('coated_retriever', 'Irish_water_spaniel', 1),
 ('coated_retriever', 'haired_pointer', 1),
 ('coated_retriever', 'standard_poodle', 1),
 ('coated_wheaten_terrier', 'Labrador_retriever', 1),
 ('coated_wheaten_terrier', 'Sealyham_terrier', 1),
 ('coated_wheaten_terrier', 'haired_fox_terrier', 1),
 ('cocker_spaniel', 'Blenheim_spaniel', 1),
 ('cocker_spaniel', 'Dandie_Dinmont', 1),
 ('cocker_spaniel', 'English_springer', 1),
 ('cocker_spaniel', 'Irish_setter', 1),
 ('collie', 'groenendael', 1),
 ('dhole', 'EntleBucher', 1),
 ('dingo', 'Rhodesian_ridgeback', 1),
 ('giant_schnauzer', 'affenpinscher', 1),
 ('giant_schnauzer', 'cairn', 1),
 ('golden_retriever', 'Irish_setter', 1),
 ('golden_retriever', 'Labrador_retriever', 1),
 ('golden_retriever', 'cocker_spaniel', 1),
 ('haired_fox_terrier', 'Irish_terrier', 1),
 ('haired_fox_terrier', 'Maltese_dog', 1),
 ('haired_pointer', 'bluetick', 1),
 ('haired_pointer', 'whippet', 1),
 ('kelpie', 'Doberman', 1),
 ('kelpie', 'miniature_pinscher', 1),
 ('kelpie', 'schipperke', 1),
 ('kelpie', 'toy_terrier', 1),
 ('malamute', 'whippet', 1),
 ('malinois', 'Airedale', 1),
 ('malinois', 'German_shepherd', 1),
 ('malinois', 'Great_Dane', 1),
 ('miniature_pinscher', 'Doberman', 1),
 ('miniature_pinscher', 'basenji', 1),
 ('miniature_pinscher', 'kelpie', 1),
 ('miniature_poodle', 'Dandie_Dinmont', 1),
 ('miniature_poodle', 'Kerry_blue_terrier', 1),
 ('miniature_schnauzer', 'Great_Pyrenees', 1),
 ('miniature_schnauzer', 'Lhasa', 1),
 ('miniature_schnauzer', 'Staffordshire_bullterrier', 1),
 ('miniature_schnauzer', 'giant_schnauzer', 1),
 ('miniature_schnauzer', 'standard_schnauzer', 1),
 ('otterhound', 'Afghan_hound', 1),
 ('otterhound', 'Border_terrier', 1),
 ('otterhound', 'komondor', 1),
 ('papillon', 'Japanese_spaniel', 1),
 ('papillon', 'Shetland_sheepdog', 1),
 ('papillon', 'pug', 1),
 ('papillon', 'toy_terrier', 1),
 ('redbone', 'EntleBucher', 1),
 ('redbone', 'vizsla', 1),
 ('schipperke', 'Appenzeller', 1),
 ('silky_terrier', 'Yorkshire_terrier', 1),
 ('silky_terrier', 'affenpinscher', 1),
 ('standard_poodle', 'Bedlington_terrier', 1),
 ('standard_poodle', 'Bouvier_des_Flandres', 1),
 ('standard_poodle', 'Irish_water_spaniel', 1),
 ('standard_poodle', 'Japanese_spaniel', 1),
 ('standard_poodle', 'miniature_poodle', 1),
 ('standard_schnauzer', 'African_hunting_dog', 1),
 ('standard_schnauzer', 'Border_terrier', 1),
 ('tan_coonhound', 'Gordon_setter', 1),
 ('tan_coonhound', 'bloodhound', 1),
 ('tan_coonhound', 'boxer', 1),
 ('toy_poodle', 'miniature_poodle', 1),
 ('toy_terrier', 'EntleBucher', 1),
 ('toy_terrier', 'Italian_greyhound', 1),
 ('toy_terrier', 'Walker_hound', 1),
 ('vizsla', 'Chesapeake_Bay_retriever', 1),
 ('vizsla', 'Rhodesian_ridgeback', 1),
 ('vizsla', 'redbone', 1),
 ('whippet', 'Chesapeake_Bay_retriever', 1),
 ('whippet', 'Great_Dane', 1),
 ('whippet', 'Ibizan_hound', 1),
 ('whippet', 'Italian_greyhound', 1),
 ('whippet', 'Saluki', 1),
 ('whippet', 'dingo', 1)]

Testing Phase 😅

Moment of truth. Lets run prediction on the test set and finally generate our submission file!

Here we load our best model, which is convieniently called bestmodel

In [32]:
learn.load("bestmodel")
Out[32]:
Learner(data=ImageDataBunch;

Train: LabelList (17609 items)
x: ImageList
Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128)
y: CategoryList
American_Staffordshire_terrier,English_springer,collie,briard,Doberman
Path: FinalData/train;

Valid: LabelList (962 items)
x: ImageList
Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128)
y: CategoryList
Pomeranian,Tibetan_terrier,Norfolk_terrier,Pomeranian,standard_poodle
Path: FinalData/val;

Test: LabelList (2009 items)
x: ImageList
Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128),Image (3, 128, 128)
y: EmptyLabelList
,,,,
Path: FinalData/val, model=Sequential(
  (0): Sequential(
    (0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
    (1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (2): ReLU(inplace=True)
    (3): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
    (4): Sequential(
      (0): BasicBlock(
        (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu): ReLU(inplace=True)
        (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
      (1): BasicBlock(
        (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu): ReLU(inplace=True)
        (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (5): Sequential(
      (0): BasicBlock(
        (conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
        (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu): ReLU(inplace=True)
        (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (downsample): Sequential(
          (0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)
          (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (1): BasicBlock(
        (conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu): ReLU(inplace=True)
        (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (6): Sequential(
      (0): BasicBlock(
        (conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
        (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu): ReLU(inplace=True)
        (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (downsample): Sequential(
          (0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)
          (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (1): BasicBlock(
        (conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu): ReLU(inplace=True)
        (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (7): Sequential(
      (0): BasicBlock(
        (conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
        (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu): ReLU(inplace=True)
        (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (downsample): Sequential(
          (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
          (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (1): BasicBlock(
        (conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu): ReLU(inplace=True)
        (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
  )
  (1): Sequential(
    (0): AdaptiveConcatPool2d(
      (ap): AdaptiveAvgPool2d(output_size=1)
      (mp): AdaptiveMaxPool2d(output_size=1)
    )
    (1): Flatten()
    (2): BatchNorm1d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (3): Dropout(p=0.25, inplace=False)
    (4): Linear(in_features=1024, out_features=512, bias=True)
    (5): ReLU(inplace=True)
    (6): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (7): Dropout(p=0.5, inplace=False)
    (8): Linear(in_features=512, out_features=119, bias=True)
  )
), opt_func=functools.partial(<class 'torch.optim.adam.Adam'>, betas=(0.9, 0.99)), loss_func=FlattenedLoss of CrossEntropyLoss(), metrics=[<function error_rate at 0x7f0bca60e4d0>, <function accuracy at 0x7f0cc9fd24d0>], true_wd=True, bn_wd=True, wd=0.01, train_bn=True, path=PosixPath('FinalData/train'), model_dir='models/', callback_fns=[functools.partial(<class 'fastai.basic_train.Recorder'>, add_time=True, silent=False)], callbacks=[], layer_groups=[Sequential(
  (0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
  (1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (2): ReLU(inplace=True)
  (3): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
  (4): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (5): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (6): ReLU(inplace=True)
  (7): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (8): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (9): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (10): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (11): ReLU(inplace=True)
  (12): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (13): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (14): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
  (15): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (16): ReLU(inplace=True)
  (17): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (18): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (19): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)
  (20): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (21): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (22): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (23): ReLU(inplace=True)
  (24): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (25): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
), Sequential(
  (0): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
  (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (2): ReLU(inplace=True)
  (3): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (4): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (5): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)
  (6): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (7): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (8): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (9): ReLU(inplace=True)
  (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (11): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (12): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
  (13): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (14): ReLU(inplace=True)
  (15): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (16): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (17): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
  (18): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (20): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (21): ReLU(inplace=True)
  (22): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (23): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
), Sequential(
  (0): AdaptiveAvgPool2d(output_size=1)
  (1): AdaptiveMaxPool2d(output_size=1)
  (2): Flatten()
  (3): BatchNorm1d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (4): Dropout(p=0.25, inplace=False)
  (5): Linear(in_features=1024, out_features=512, bias=True)
  (6): ReLU(inplace=True)
  (7): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (8): Dropout(p=0.5, inplace=False)
  (9): Linear(in_features=512, out_features=119, bias=True)
)], add_time=True, silent=False)
In [33]:
preds,y = learn.get_preds(ds_type=DatasetType.Test)

Now that we've run prediction, let us convert our prediction from a tensor to a list. The below code does that. In this code, we take the max of each row of the tensor, move it to the memory accessable by the cpu, convert it to a numpy array and then convert it to a list

In [34]:
preds_list=preds.argmax(dim=-1).cpu().numpy().tolist()

Since the predictions are all in numbers, lets create a dictionary for us to easily convert it into a list of string labels

In [35]:
mapping={}
for x in range(len(learn.data.classes)):
    mapping[x]=learn.data.classes[x]
print(mapping)
{0: 'Afghan_hound', 1: 'African_hunting_dog', 2: 'Airedale', 3: 'American_Staffordshire_terrier', 4: 'Appenzeller', 5: 'Australian_terrier', 6: 'Bedlington_terrier', 7: 'Bernese_mountain_dog', 8: 'Blenheim_spaniel', 9: 'Border_collie', 10: 'Border_terrier', 11: 'Boston_bull', 12: 'Bouvier_des_Flandres', 13: 'Brabancon_griffon', 14: 'Brittany_spaniel', 15: 'Cardigan', 16: 'Chesapeake_Bay_retriever', 17: 'Chihuahua', 18: 'Dandie_Dinmont', 19: 'Doberman', 20: 'English_foxhound', 21: 'English_setter', 22: 'English_springer', 23: 'EntleBucher', 24: 'Eskimo_dog', 25: 'French_bulldog', 26: 'German_shepherd', 27: 'Gordon_setter', 28: 'Great_Dane', 29: 'Great_Pyrenees', 30: 'Greater_Swiss_Mountain_dog', 31: 'Ibizan_hound', 32: 'Irish_setter', 33: 'Irish_terrier', 34: 'Irish_water_spaniel', 35: 'Irish_wolfhound', 36: 'Italian_greyhound', 37: 'Japanese_spaniel', 38: 'Kerry_blue_terrier', 39: 'Labrador_retriever', 40: 'Lakeland_terrier', 41: 'Leonberg', 42: 'Lhasa', 43: 'Maltese_dog', 44: 'Mexican_hairless', 45: 'Newfoundland', 46: 'Norfolk_terrier', 47: 'Norwegian_elkhound', 48: 'Norwich_terrier', 49: 'Old_English_sheepdog', 50: 'Pekinese', 51: 'Pembroke', 52: 'Pomeranian', 53: 'Rhodesian_ridgeback', 54: 'Rottweiler', 55: 'Saint_Bernard', 56: 'Saluki', 57: 'Samoyed', 58: 'Scotch_terrier', 59: 'Scottish_deerhound', 60: 'Sealyham_terrier', 61: 'Shetland_sheepdog', 62: 'Siberian_husky', 63: 'Staffordshire_bullterrier', 64: 'Sussex_spaniel', 65: 'Tibetan_mastiff', 66: 'Tibetan_terrier', 67: 'Tzu', 68: 'Walker_hound', 69: 'Weimaraner', 70: 'Welsh_springer_spaniel', 71: 'West_Highland_white_terrier', 72: 'Yorkshire_terrier', 73: 'affenpinscher', 74: 'basenji', 75: 'basset', 76: 'beagle', 77: 'bloodhound', 78: 'bluetick', 79: 'borzoi', 80: 'boxer', 81: 'briard', 82: 'bull_mastiff', 83: 'cairn', 84: 'chow', 85: 'clumber', 86: 'coated_retriever', 87: 'coated_wheaten_terrier', 88: 'cocker_spaniel', 89: 'collie', 90: 'dhole', 91: 'dingo', 92: 'giant_schnauzer', 93: 'golden_retriever', 94: 'groenendael', 95: 'haired_fox_terrier', 96: 'haired_pointer', 97: 'keeshond', 98: 'kelpie', 99: 'komondor', 100: 'kuvasz', 101: 'malamute', 102: 'malinois', 103: 'miniature_pinscher', 104: 'miniature_poodle', 105: 'miniature_schnauzer', 106: 'otterhound', 107: 'papillon', 108: 'pug', 109: 'redbone', 110: 'schipperke', 111: 'silky_terrier', 112: 'standard_poodle', 113: 'standard_schnauzer', 114: 'tan_coonhound', 115: 'toy_poodle', 116: 'toy_terrier', 117: 'vizsla', 118: 'whippet'}
In [36]:
submission=[]
for x in preds_list:
    submission.append(mapping[x])
In [37]:
filename=test_df.filename.tolist()
d={"filename":filename,"label":submission}
df=pd.DataFrame(d)
In [38]:
df.head()
Out[38]:
filename label
0 007d75fa66.jpg Welsh_springer_spaniel
1 6db592ce52.jpg keeshond
2 e1de52cd0b.jpg schipperke
3 e78bf70574.jpg otterhound
4 583c7b9753.jpg giant_schnauzer

Submitting the predictions

Lets convert the dataframe into the csv file! After this we are done just submit it right from here using the aicrowd cli.

In [43]:
!rm -rf assets
!mkdir assets

df.to_csv(os.path.join("assets", "submission.csv"), index=False)
In [44]:
!aicrowd submission create -c dgbrd -f assets/submission.csv
submission.csv ━━━━━━━━━━━━━━━━━━━━ 100.0%59.2/57.6 KB423.7 kB/s0:00:00
                                  ╭─────────────────────────╮                                  
                                  │ Successfully submitted! │                                  
                                  ╰─────────────────────────╯                                  
                                        Important links                                        
┌──────────────────┬──────────────────────────────────────────────────────────────────────────┐
│  This submission │ https://www.aicrowd.com/challenges/dgbrd/submissions/172253              │
│                  │                                                                          │
│  All submissions │ https://www.aicrowd.com/challenges/dgbrd/submissions?my_submissions=true │
│                  │                                                                          │
│      Leaderboard │ https://www.aicrowd.com/challenges/dgbrd/leaderboards                    │
│                  │                                                                          │
│ Discussion forum │ https://discourse.aicrowd.com/c/dgbrd                                    │
│                  │                                                                          │
│   Challenge page │ https://www.aicrowd.com/challenges/dgbrd                                 │
└──────────────────┴──────────────────────────────────────────────────────────────────────────┘
{'submission_id': 172253, 'created_at': '2022-01-19T12:26:01.336Z'}

Well Done! 👍 See your score on the leaderboard.


Comments

You must login before you can post a comment.

Execute