# 13.14. Dog Breed Identification (ImageNet Dogs) on Kaggle¶ Open the notebook in Colab Open the notebook in Colab Open the notebook in Colab

In this section, we will tackle the dog breed identification challenge in the Kaggle Competition. The competition’s web address is

In this competition, we attempt to identify 120 different breeds of dogs. The dataset used in this competition is actually a subset of the famous ImageNet dataset. Different from the images in the CIFAR-10 dataset used in the previous section, the images in the ImageNet dataset are higher and wider and their dimensions are inconsistent.

Fig. 13.14.1 shows the information on the competition’s webpage. In order to submit the results, please register an account on the Kaggle website first.

Fig. 13.14.1 Dog breed identification competition website. The dataset for the competition can be accessed by clicking the “Data” tab.

First, import the packages or modules required for the competition.

import collections
from d2l import mxnet as d2l
import math
from mxnet import autograd, gluon, init, npx
from mxnet.gluon import nn
import os
import time

npx.set_np()

## 13.14.1. Obtaining and Organizing the Dataset¶

The competition data is divided into a training set and testing set. The training set contains $$10,222$$ images and the testing set contains $$10,357$$ images. The images in both sets are in JPEG format. These images contain three RGB channels (color) and they have different heights and widths. There are 120 breeds of dogs in the training set, including Labradors, Poodles, Dachshunds, Samoyeds, Huskies, Chihuahuas, and Yorkshire Terriers.

After logging in to Kaggle, we can click on the “Data” tab on the dog breed identification competition webpage shown in Fig. 13.14.1 and download the dataset by clicking the “Download All” button. After unzipping the downloaded file in ../data, you will find the entire dataset in the following paths:

• ../data/dog-breed-identification/labels.csv

• ../data/dog-breed-identification/sample_submission.csv

• ../data/dog-breed-identification/train

• ../data/dog-breed-identification/test

You may have noticed that the above structure is quite similar to that of the CIFAR-10 competition in Section 13.13, where folders train/ and test/ contain training and testing dog images respectively, and labels.csv has the labels for the training images.

Similarly, to make it easier to get started, we provide a small-scale sample of the dataset mentioned above, “train_valid_test_tiny.zip”. If you are going to use the full dataset for the Kaggle competition, you will also need to change the demo variable below to False.

#@save
d2l.DATA_HUB['dog_tiny'] = (d2l.DATA_URL + 'kaggle_dog_tiny.zip',
'7c9b54e78c1cedaa04998f9868bc548c60101362')

# If you use the full dataset downloaded for the Kaggle competition, change
# the variable below to False
demo = True
if demo:
else:
data_dir = os.path.join('..', 'data', 'dog-breed-identification')

### 13.14.1.2. Organizing the Dataset¶

We can organize the dataset similarly to what we did in Section 13.13, namely separating a validation set from the training set, and moving images into subfolders grouped by labels.

The reorg_dog_data function below is used to read the training data labels, segment the validation set, and organize the training set.

def reorg_dog_data(data_dir, valid_ratio):
d2l.reorg_train_valid(data_dir, labels, valid_ratio)
d2l.reorg_test(data_dir)

batch_size = 1 if demo else 128
valid_ratio = 0.1
reorg_dog_data(data_dir, valid_ratio)

## 13.14.2. Image Augmentation¶

The size of the images in this section are larger than the images in the previous section. Here are some more image augmentation operations that might be useful.

transform_train = gluon.data.vision.transforms.Compose([
# Randomly crop the image to obtain an image with an area of 0.08 to 1 of
# the original area and height to width ratio between 3/4 and 4/3. Then,
# scale the image to create a new image with a height and width of 224
# pixels each
gluon.data.vision.transforms.RandomResizedCrop(224, scale=(0.08, 1.0),
ratio=(3.0/4.0, 4.0/3.0)),
gluon.data.vision.transforms.RandomFlipLeftRight(),
# Randomly change the brightness, contrast, and saturation
gluon.data.vision.transforms.RandomColorJitter(brightness=0.4,
contrast=0.4,
saturation=0.4),
gluon.data.vision.transforms.RandomLighting(0.1),
gluon.data.vision.transforms.ToTensor(),
# Standardize each channel of the image
gluon.data.vision.transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])

During testing, we only use definite image preprocessing operations.

transform_test = gluon.data.vision.transforms.Compose([
gluon.data.vision.transforms.Resize(256),
# Crop a square of 224 by 224 from the center of the image
gluon.data.vision.transforms.CenterCrop(224),
gluon.data.vision.transforms.ToTensor(),
gluon.data.vision.transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])

As in the previous section, we can create an ImageFolderDataset instance to read the dataset containing the original image files.

train_ds, valid_ds, train_valid_ds, test_ds = [
gluon.data.vision.ImageFolderDataset(
os.path.join(data_dir, 'train_valid_test', folder))
for folder in ('train', 'valid', 'train_valid', 'test')]

Here, we create DataLoader instances, just like in Section 13.13.

dataset.transform_first(transform_train), batch_size, shuffle=True,
last_batch='keep') for dataset in (train_ds, train_valid_ds)]

dataset.transform_first(transform_test), batch_size, shuffle=False,
last_batch='keep') for dataset in (valid_ds, test_ds)]

## 13.14.4. Defining the Model¶

The dataset for this competition is a subset of the ImageNet data set. Therefore, we can use the approach discussed in Section 13.2 to select a model pre-trained on the entire ImageNet dataset and use it to extract image features to be input in the custom small-scale output network. Gluon provides a wide range of pre-trained models. Here, we will use the pre-trained ResNet-34 model. Because the competition dataset is a subset of the pre-training dataset, we simply reuse the input of the pre-trained model’s output layer, i.e., the extracted features. Then, we can replace the original output layer with a small custom output network that can be trained, such as two fully connected layers in a series. Different from the experiment in Section 13.2, here, we do not retrain the pre-trained model used for feature extraction. This reduces the training time and the memory required to store model parameter gradients.

You must note that, during image augmentation, we use the mean values and standard deviations of the three RGB channels for the entire ImageNet dataset for normalization. This is consistent with the normalization of the pre-trained model.

def get_net(ctx):
finetune_net = gluon.model_zoo.vision.resnet34_v2(pretrained=True)
# Define a new output network
finetune_net.output_new = nn.HybridSequential(prefix='')
# There are 120 output categories
# Initialize the output network
finetune_net.output_new.initialize(init.Xavier(), ctx=ctx)
# Distribute the model parameters to the CPUs or GPUs used for computation
finetune_net.collect_params().reset_ctx(ctx)
return finetune_net

When calculating the loss, we first use the member variable features to obtain the input of the pre-trained model’s output layer, i.e., the extracted feature. Then, we use this feature as the input for our small custom output network and compute the output.

loss = gluon.loss.SoftmaxCrossEntropyLoss()

def evaluate_loss(data_iter, net, ctx):
l_sum, n = 0.0, 0
for X, y in data_iter:
y = y.as_in_ctx(ctx)
output_features = net.features(X.as_in_ctx(ctx))
outputs = net.output_new(output_features)
l_sum += float(loss(outputs, y).sum())
n += y.size
return l_sum / n

## 13.14.5. Defining the Training Functions¶

We will select the model and tune hyper-parameters according to the model’s performance on the validation set. The model training function train only trains the small custom output network.

def train(net, train_iter, valid_iter, num_epochs, lr, wd, ctx, lr_period,
lr_decay):
# Only train the small custom output network
trainer = gluon.Trainer(net.output_new.collect_params(), 'sgd',
{'learning_rate': lr, 'momentum': 0.9, 'wd': wd})
for epoch in range(num_epochs):
train_l_sum, n, start = 0.0, 0, time.time()
if epoch > 0 and epoch % lr_period == 0:
trainer.set_learning_rate(trainer.learning_rate * lr_decay)
for X, y in train_iter:
y = y.as_in_ctx(ctx)
output_features = net.features(X.as_in_ctx(ctx))
outputs = net.output_new(output_features)
l = loss(outputs, y).sum()
l.backward()
trainer.step(batch_size)
train_l_sum += float(l)
n += y.size
time_s = f'time {time.time() - start:.2f} sec'
if valid_iter is not None:
valid_loss = evaluate_loss(valid_iter, net, ctx)
epoch_s = (f'epoch {epoch + 1}, train loss {train_l_sum / n:f}, '
f'valid loss {valid_loss:f}, ')
else:
epoch_s = f'epoch {epoch + 1}, train loss {train_l_sum / n:f}, '
print(epoch_s + time_s + ', lr ' + str(trainer.learning_rate))

## 13.14.6. Training and Validating the Model¶

Now, we can train and validate the model. The following hyper-parameters can be tuned. For example, we can increase the number of epochs. Because lr_period and lr_decay are set to 10 and 0.1 respectively, the learning rate of the optimization algorithm will be multiplied by 0.1 after every 10 epochs.

ctx, num_epochs, lr, wd = d2l.try_gpu(), 1, 0.01, 1e-4
lr_period, lr_decay, net = 10, 0.1, get_net(ctx)
net.hybridize()
train(net, train_iter, valid_iter, num_epochs, lr, wd, ctx, lr_period,
lr_decay)
epoch 1, train loss 4.853056, valid loss 4.807044, time 10.02 sec, lr 0.01

## 13.14.7. Classifying the Testing Set and Submitting Results on Kaggle¶

After obtaining a satisfactory model design and hyper-parameters, we use all training datasets (including validation sets) to retrain the model and then classify the testing set. Note that predictions are made by the output network we just trained.

net = get_net(ctx)
net.hybridize()
train(net, train_valid_iter, None, num_epochs, lr, wd, ctx, lr_period,
lr_decay)

preds = []
for data, label in test_iter:
output_features = net.features(data.as_in_ctx(ctx))
output = npx.softmax(net.output_new(output_features))
preds.extend(output.asnumpy())
ids = sorted(os.listdir(
os.path.join(data_dir, 'train_valid_test', 'test', 'unknown')))
with open('submission.csv', 'w') as f:
f.write('id,' + ','.join(train_valid_ds.synsets) + '\n')
for i, output in zip(ids, preds):
f.write(i.split('.')[0] + ',' + ','.join(
[str(num) for num in output]) + '\n')
epoch 1, train loss 4.855917, time 11.39 sec, lr 0.01

After executing the above code, we will generate a “submission.csv” file. The format of this file is consistent with the Kaggle competition requirements. The method for submitting results is similar to method in Section 4.10.

## 13.14.8. Summary¶

• We can use a model pre-trained on the ImageNet dataset to extract features and only train a small custom output network. This will allow us to classify a subset of the ImageNet dataset with lower computing and storage overhead.

## 13.14.9. Exercises¶

1. When using the entire Kaggle dataset, what kind of results do you get when you increase the batch_size (batch size) and num_epochs (number of epochs)?

2. Do you get better results if you use a deeper pre-trained model?

3. Scan the QR code to access the relevant discussions and exchange ideas about the methods used and the results obtained with the community. Can you come up with any better techniques?

Discussions