previous benchmark in a kernel, v0.0.0

From: https://www.kaggle.com/interneuron/previous-benchmark-in-a-kernel-v0-0-0

Author: interneuron

attempt 1 to get the inception benchmark working code is from : https://github.com/macaodha/inat_comp_2018 very premature modifications to work with torch v1 and the current dataset, wip, or I should say, not yet working...in progress

In [1]:
import numpy as np 
import pandas as pd 
import os
import json
print(os.listdir("../input"))
['kaggle_sample_submission.csv', 'train_val2019', 'val2019.json', 'train2019.json', 'test2019.json', 'test2019']
In [2]:
ann_file = '../input/train2019.json'
In [3]:
with open(ann_file) as data_file:
        ann_data = json.load(data_file)
In [4]:
import torch.utils.data as data
from PIL import Image
import os
import json
from torchvision import transforms
import random
import numpy as np
In [5]:
class INAT(data.Dataset):
    def __init__(self, root, ann_file, is_train=True):

        # load annotations
        print('Loading annotations from: ' + os.path.basename(ann_file))
        with open(ann_file) as data_file:
            ann_data = json.load(data_file)

        # set up the filenames and annotations
        self.imgs = [aa['file_name'] for aa in ann_data['images']]
        self.ids = [aa['id'] for aa in ann_data['images']]

        # if we dont have class labels set them to '0'
        if 'annotations' in ann_data.keys():
            self.classes = [aa['category_id'] for aa in ann_data['annotations']]
        else:
            self.classes = [0]*len(self.imgs)

        # load taxonomy
        self.tax_levels = ['id', 'genus', 'family', 'order', 'class', 'phylum', 'kingdom']
                           #8142, 4412,    1120,     273,     57,      25,       6
        self.taxonomy, self.classes_taxonomic = load_taxonomy(ann_data, self.tax_levels, self.classes)

        # print out some stats
        print( '\t' + str(len(self.imgs)) + ' images')
        print( '\t' + str(len(set(self.classes))) + ' classes')

        self.root = root
        self.is_train = is_train
        self.loader = default_loader

        # augmentation params
        self.im_size = [299, 299]  # can change this to train on higher res
        self.mu_data = [0.485, 0.456, 0.406]
        self.std_data = [0.229, 0.224, 0.225]
        self.brightness = 0.4
        self.contrast = 0.4
        self.saturation = 0.4
        self.hue = 0.25

        # augmentations
        self.center_crop = transforms.CenterCrop((self.im_size[0], self.im_size[1]))
        self.scale_aug = transforms.RandomResizedCrop(size=self.im_size[0])
        self.flip_aug = transforms.RandomHorizontalFlip()
        self.color_aug = transforms.ColorJitter(self.brightness, self.contrast, self.saturation, self.hue)
        self.tensor_aug = transforms.ToTensor()
        self.norm_aug = transforms.Normalize(mean=self.mu_data, std=self.std_data)

    def __getitem__(self, index):
        path = self.root + self.imgs[index]
        im_id = self.ids[index]
        img = self.loader(path)
        species_id = self.classes[index]
        tax_ids = self.classes_taxonomic[species_id]

        if self.is_train:
            img = self.scale_aug(img)
            img = self.flip_aug(img)
            img = self.color_aug(img)
        else:
            img = self.center_crop(img)

        img = self.tensor_aug(img)
        img = self.norm_aug(img)

        return img, im_id, species_id, tax_ids

    def __len__(self):
        return len(self.imgs)
In [6]:
def default_loader(path):
    return Image.open(path).convert('RGB')

def load_taxonomy(ann_data, tax_levels, classes):
    # loads the taxonomy data and converts to ints
    taxonomy = {}

    if 'categories' in ann_data.keys():
        num_classes = len(ann_data['categories'])
        for tt in tax_levels:
            tax_data = [aa[tt] for aa in ann_data['categories']]
            _, tax_id = np.unique(tax_data, return_inverse=True)
            taxonomy[tt] = dict(zip(range(num_classes), list(tax_id)))
    else:
        # set up dummy data
        for tt in tax_levels:
            taxonomy[tt] = dict(zip([0], [0]))

    # create a dictionary of lists containing taxonomic labels
    classes_taxonomic = {}
    for cc in np.unique(classes):
        tax_ids = [0]*len(tax_levels)
        for ii, tt in enumerate(tax_levels):
            tax_ids[ii] = taxonomy[tt][cc]
        classes_taxonomic[cc] = tax_ids

    return taxonomy, classes_taxonomic
In [7]:
trl=INAT(root='../input/train_val2019/', ann_file=ann_file)
Loading annotations from: train2019.json
	265213 images
	1010 classes
In [8]:
#next(iter(trl))
In [9]:
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.model_zoo as model_zoo


__all__ = ['Inception3', 'inception_v3']


model_urls = {
    # Inception v3 ported from TensorFlow
    'inception_v3_google': 'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth',
}


def inception_v3(pretrained=False, **kwargs):
    r"""Inception v3 model architecture from
    `"Rethinking the Inception Architecture for Computer Vision" <http://arxiv.org/abs/1512.00567>`_.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    if pretrained:
        if 'transform_input' not in kwargs:
            kwargs['transform_input'] = True
        model = Inception3(**kwargs)
        model.load_state_dict(model_zoo.load_url(model_urls['inception_v3_google']))
        return model

    return Inception3(**kwargs)


class Inception3(nn.Module):

    def __init__(self, num_classes=1000, aux_logits=True, transform_input=False):
        super(Inception3, self).__init__()
        self.aux_logits = aux_logits
        self.transform_input = transform_input
        self.Conv2d_1a_3x3 = BasicConv2d(3, 32, kernel_size=3, stride=2)
        self.Conv2d_2a_3x3 = BasicConv2d(32, 32, kernel_size=3)
        self.Conv2d_2b_3x3 = BasicConv2d(32, 64, kernel_size=3, padding=1)
        self.Conv2d_3b_1x1 = BasicConv2d(64, 80, kernel_size=1)
        self.Conv2d_4a_3x3 = BasicConv2d(80, 192, kernel_size=3)
        self.Mixed_5b = InceptionA(192, pool_features=32)
        self.Mixed_5c = InceptionA(256, pool_features=64)
        self.Mixed_5d = InceptionA(288, pool_features=64)
        self.Mixed_6a = InceptionB(288)
        self.Mixed_6b = InceptionC(768, channels_7x7=128)
        self.Mixed_6c = InceptionC(768, channels_7x7=160)
        self.Mixed_6d = InceptionC(768, channels_7x7=160)
        self.Mixed_6e = InceptionC(768, channels_7x7=192)
        if aux_logits:
            self.AuxLogits = InceptionAux(768, num_classes)
        self.Mixed_7a = InceptionD(768)
        self.Mixed_7b = InceptionE(1280)
        self.Mixed_7c = InceptionE(2048)
        self.fc = nn.Linear(2048, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
                import scipy.stats as stats
                stddev = m.stddev if hasattr(m, 'stddev') else 0.1
                X = stats.truncnorm(-2, 2, scale=stddev)
                values = torch.Tensor(X.rvs(m.weight.data.numel()))
                values = values.view(m.weight.data.size())
                m.weight.data.copy_(values)
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def forward(self, x):
        if self.transform_input:
            x = x.clone()
            x[:, 0] = x[:, 0] * (0.229 / 0.5) + (0.485 - 0.5) / 0.5
            x[:, 1] = x[:, 1] * (0.224 / 0.5) + (0.456 - 0.5) / 0.5
            x[:, 2] = x[:, 2] * (0.225 / 0.5) + (0.406 - 0.5) / 0.5
        # 299 x 299 x 3
        x = self.Conv2d_1a_3x3(x)
        # 149 x 149 x 32
        x = self.Conv2d_2a_3x3(x)
        # 147 x 147 x 32
        x = self.Conv2d_2b_3x3(x)
        # 147 x 147 x 64
        x = F.max_pool2d(x, kernel_size=3, stride=2)
        # 73 x 73 x 64
        x = self.Conv2d_3b_1x1(x)
        # 73 x 73 x 80
        x = self.Conv2d_4a_3x3(x)
        # 71 x 71 x 192
        x = F.max_pool2d(x, kernel_size=3, stride=2)
        # 35 x 35 x 192
        x = self.Mixed_5b(x)
        # 35 x 35 x 256
        x = self.Mixed_5c(x)
        # 35 x 35 x 288
        x = self.Mixed_5d(x)
        # 35 x 35 x 288
        x = self.Mixed_6a(x)
        # 17 x 17 x 768
        x = self.Mixed_6b(x)
        # 17 x 17 x 768
        x = self.Mixed_6c(x)
        # 17 x 17 x 768
        x = self.Mixed_6d(x)
        # 17 x 17 x 768
        x = self.Mixed_6e(x)
        # 17 x 17 x 768
        if self.training and self.aux_logits:
            aux = self.AuxLogits(x)
        # 17 x 17 x 768
        x = self.Mixed_7a(x)
        # 8 x 8 x 1280
        x = self.Mixed_7b(x)
        # 8 x 8 x 2048
        x = self.Mixed_7c(x)
        # 8 x 8 x 2048
        x = F.adaptive_avg_pool2d(x, 1)
        #x = F.avg_pool2d(x, kernel_size=8)
        # 1 x 1 x 2048
        x = F.dropout(x, training=self.training)
        # 1 x 1 x 2048
        x = x.view(x.size(0), -1)
        # 2048
        x = self.fc(x)
        # 1000 (num_classes)
        if self.training and self.aux_logits:
            return x, aux
        return x


class InceptionA(nn.Module):

    def __init__(self, in_channels, pool_features):
        super(InceptionA, self).__init__()
        self.branch1x1 = BasicConv2d(in_channels, 64, kernel_size=1)

        self.branch5x5_1 = BasicConv2d(in_channels, 48, kernel_size=1)
        self.branch5x5_2 = BasicConv2d(48, 64, kernel_size=5, padding=2)

        self.branch3x3dbl_1 = BasicConv2d(in_channels, 64, kernel_size=1)
        self.branch3x3dbl_2 = BasicConv2d(64, 96, kernel_size=3, padding=1)
        self.branch3x3dbl_3 = BasicConv2d(96, 96, kernel_size=3, padding=1)

        self.branch_pool = BasicConv2d(in_channels, pool_features, kernel_size=1)

    def forward(self, x):
        branch1x1 = self.branch1x1(x)

        branch5x5 = self.branch5x5_1(x)
        branch5x5 = self.branch5x5_2(branch5x5)

        branch3x3dbl = self.branch3x3dbl_1(x)
        branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
        branch3x3dbl = self.branch3x3dbl_3(branch3x3dbl)

        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
        branch_pool = self.branch_pool(branch_pool)

        outputs = [branch1x1, branch5x5, branch3x3dbl, branch_pool]
        return torch.cat(outputs, 1)


class InceptionB(nn.Module):

    def __init__(self, in_channels):
        super(InceptionB, self).__init__()
        self.branch3x3 = BasicConv2d(in_channels, 384, kernel_size=3, stride=2)

        self.branch3x3dbl_1 = BasicConv2d(in_channels, 64, kernel_size=1)
        self.branch3x3dbl_2 = BasicConv2d(64, 96, kernel_size=3, padding=1)
        self.branch3x3dbl_3 = BasicConv2d(96, 96, kernel_size=3, stride=2)

    def forward(self, x):
        branch3x3 = self.branch3x3(x)

        branch3x3dbl = self.branch3x3dbl_1(x)
        branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
        branch3x3dbl = self.branch3x3dbl_3(branch3x3dbl)

        branch_pool = F.max_pool2d(x, kernel_size=3, stride=2)

        outputs = [branch3x3, branch3x3dbl, branch_pool]
        return torch.cat(outputs, 1)


class InceptionC(nn.Module):

    def __init__(self, in_channels, channels_7x7):
        super(InceptionC, self).__init__()
        self.branch1x1 = BasicConv2d(in_channels, 192, kernel_size=1)

        c7 = channels_7x7
        self.branch7x7_1 = BasicConv2d(in_channels, c7, kernel_size=1)
        self.branch7x7_2 = BasicConv2d(c7, c7, kernel_size=(1, 7), padding=(0, 3))
        self.branch7x7_3 = BasicConv2d(c7, 192, kernel_size=(7, 1), padding=(3, 0))

        self.branch7x7dbl_1 = BasicConv2d(in_channels, c7, kernel_size=1)
        self.branch7x7dbl_2 = BasicConv2d(c7, c7, kernel_size=(7, 1), padding=(3, 0))
        self.branch7x7dbl_3 = BasicConv2d(c7, c7, kernel_size=(1, 7), padding=(0, 3))
        self.branch7x7dbl_4 = BasicConv2d(c7, c7, kernel_size=(7, 1), padding=(3, 0))
        self.branch7x7dbl_5 = BasicConv2d(c7, 192, kernel_size=(1, 7), padding=(0, 3))

        self.branch_pool = BasicConv2d(in_channels, 192, kernel_size=1)

    def forward(self, x):
        branch1x1 = self.branch1x1(x)

        branch7x7 = self.branch7x7_1(x)
        branch7x7 = self.branch7x7_2(branch7x7)
        branch7x7 = self.branch7x7_3(branch7x7)

        branch7x7dbl = self.branch7x7dbl_1(x)
        branch7x7dbl = self.branch7x7dbl_2(branch7x7dbl)
        branch7x7dbl = self.branch7x7dbl_3(branch7x7dbl)
        branch7x7dbl = self.branch7x7dbl_4(branch7x7dbl)
        branch7x7dbl = self.branch7x7dbl_5(branch7x7dbl)

        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
        branch_pool = self.branch_pool(branch_pool)

        outputs = [branch1x1, branch7x7, branch7x7dbl, branch_pool]
        return torch.cat(outputs, 1)


class InceptionD(nn.Module):

    def __init__(self, in_channels):
        super(InceptionD, self).__init__()
        self.branch3x3_1 = BasicConv2d(in_channels, 192, kernel_size=1)
        self.branch3x3_2 = BasicConv2d(192, 320, kernel_size=3, stride=2)

        self.branch7x7x3_1 = BasicConv2d(in_channels, 192, kernel_size=1)
        self.branch7x7x3_2 = BasicConv2d(192, 192, kernel_size=(1, 7), padding=(0, 3))
        self.branch7x7x3_3 = BasicConv2d(192, 192, kernel_size=(7, 1), padding=(3, 0))
        self.branch7x7x3_4 = BasicConv2d(192, 192, kernel_size=3, stride=2)

    def forward(self, x):
        branch3x3 = self.branch3x3_1(x)
        branch3x3 = self.branch3x3_2(branch3x3)

        branch7x7x3 = self.branch7x7x3_1(x)
        branch7x7x3 = self.branch7x7x3_2(branch7x7x3)
        branch7x7x3 = self.branch7x7x3_3(branch7x7x3)
        branch7x7x3 = self.branch7x7x3_4(branch7x7x3)

        branch_pool = F.max_pool2d(x, kernel_size=3, stride=2)
        outputs = [branch3x3, branch7x7x3, branch_pool]
        return torch.cat(outputs, 1)


class InceptionE(nn.Module):

    def __init__(self, in_channels):
        super(InceptionE, self).__init__()
        self.branch1x1 = BasicConv2d(in_channels, 320, kernel_size=1)

        self.branch3x3_1 = BasicConv2d(in_channels, 384, kernel_size=1)
        self.branch3x3_2a = BasicConv2d(384, 384, kernel_size=(1, 3), padding=(0, 1))
        self.branch3x3_2b = BasicConv2d(384, 384, kernel_size=(3, 1), padding=(1, 0))

        self.branch3x3dbl_1 = BasicConv2d(in_channels, 448, kernel_size=1)
        self.branch3x3dbl_2 = BasicConv2d(448, 384, kernel_size=3, padding=1)
        self.branch3x3dbl_3a = BasicConv2d(384, 384, kernel_size=(1, 3), padding=(0, 1))
        self.branch3x3dbl_3b = BasicConv2d(384, 384, kernel_size=(3, 1), padding=(1, 0))

        self.branch_pool = BasicConv2d(in_channels, 192, kernel_size=1)

    def forward(self, x):
        branch1x1 = self.branch1x1(x)

        branch3x3 = self.branch3x3_1(x)
        branch3x3 = [
            self.branch3x3_2a(branch3x3),
            self.branch3x3_2b(branch3x3),
        ]
        branch3x3 = torch.cat(branch3x3, 1)

        branch3x3dbl = self.branch3x3dbl_1(x)
        branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
        branch3x3dbl = [
            self.branch3x3dbl_3a(branch3x3dbl),
            self.branch3x3dbl_3b(branch3x3dbl),
        ]
        branch3x3dbl = torch.cat(branch3x3dbl, 1)

        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
        branch_pool = self.branch_pool(branch_pool)

        outputs = [branch1x1, branch3x3, branch3x3dbl, branch_pool]
        return torch.cat(outputs, 1)


class InceptionAux(nn.Module):

    def __init__(self, in_channels, num_classes):
        super(InceptionAux, self).__init__()
        self.conv0 = BasicConv2d(in_channels, 128, kernel_size=1)
        self.conv1 = BasicConv2d(128, 768, kernel_size=5)
        self.conv1.stddev = 0.01
        self.fc = nn.Linear(768, num_classes)
        self.fc.stddev = 0.001

    def forward(self, x):
        # 17 x 17 x 768
        x = F.avg_pool2d(x, kernel_size=5, stride=3)
        # 5 x 5 x 768
        x = self.conv0(x)
        # 5 x 5 x 128
        x = self.conv1(x)
        # 1 x 1 x 768
        x = x.view(x.size(0), -1)
        # 768
        x = self.fc(x)
        # 1000
        return x


class BasicConv2d(nn.Module):

    def __init__(self, in_channels, out_channels, **kwargs):
        super(BasicConv2d, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs)
        self.bn = nn.BatchNorm2d(out_channels, eps=0.001)

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        return F.relu(x, inplace=True)
In [10]:
import os
import shutil
import time
import numpy as np

import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
#import torchvision.models as models
#from inception import *

#import inat2018_loader

class Params:
    # arch = 'inception_v3'
    num_classes = 1010
    workers = 0
    epochs = 3
    start_epoch = 0
    batch_size = 64  # might want to make smaller 
    lr = 0.0045
    lr_decay = 0.94
    epoch_decay = 4
    momentum = 0.9
    weight_decay = 1e-4
    print_freq = 100

    resume = ''  # set this to path of model to resume training
    
    train_file = '../input/train2019.json/'
    val_file = '../input/val2019.json/'
    data_root = '../input/train_val2019/'

    # set evaluate to True to run the test set
    evaluate = False
    save_preds = True
    op_file_name = '../input/kaggle_sample_submission.csv' # submission file
    if evaluate == True:
        val_file = '../input/test2019/'

best_prec3 = 0.0  # store current best top 3
In [11]:
num_classes = 1010
model = inception_v3(pretrained=True)
model.fc = nn.Linear(2048, num_classes)
model.aux_logits = False
#model = torch.nn.DataParallel(model).cuda()
model = model.cuda()
Downloading: "https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth" to /tmp/.torch/models/inception_v3_google-1a9a5a14.pth
108857766it [00:13, 8105940.76it/s] 
In [12]:
criterion = nn.CrossEntropyLoss().cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)#,
#                                momentum=0.1,
#                                weight_decay=0.1)
In [13]:
    # optionally resume from a checkpoint
    #if args.resume:
     #   if os.path.isfile(args.resume):
   #         print("=> loading checkpoint '{}'".format(args.resume))
   #        checkpoint = torch.load(args.resume)
   #         args.start_epoch = checkpoint['epoch']
   #         best_prec3 = checkpoint['best_prec3']
  #         model.load_state_dict(checkpoint['state_dict'])
  #          optimizer.load_state_dict(checkpoint['optimizer'])
  #          print("=> loaded checkpoint '{}' (epoch {})"
  #                .format(args.resume, checkpoint['epoch']))
  #      else:
   #         print("=> no checkpoint found at '{}'".format(args.resume))
In [14]:
train_file = '../input/train2019.json'
val_file = '../input/val2019.json'
test_file = '../input/test2019.json'
In [15]:
with open(train_file) as data_file:
        train_data = json.load(data_file)
In [16]:
with open(val_file) as data_file:
        val_data = json.load(data_file)
In [17]:
with open(test_file) as data_file:
        test_data = json.load(data_file)
In [18]:
    cudnn.benchmark = True

    # data loading code
    train_dataset = INAT(root='../input/train_val2019/', ann_file=train_file,
                     is_train=True)
    val_dataset = INAT(root='../input/train_val2019/', ann_file=val_file,
                     is_train=False)

    train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=48,
                   shuffle=True, num_workers=0, pin_memory=True)
    val_loader = torch.utils.data.DataLoader(val_dataset,
                  batch_size=32, shuffle=False,
                  num_workers=0, pin_memory=True)
Loading annotations from: train2019.json
	265213 images
	1010 classes
Loading annotations from: val2019.json
	3030 images
	1010 classes
In [19]:
test_dataset = INAT(root='../input/test2019/', ann_file=test_file,
                     is_train=False)


test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=4,
                   shuffle=False, num_workers=0, pin_memory=True)
Loading annotations from: test2019.json
	35350 images
	1 classes
In [20]:
evaluate = True
save_preds = True
In [21]:
op_file_name = 'wtf'
In [22]:
if evaluate:
    prec3, preds, im_ids = validate(val_loader, model, criterion, True)
    # write predictions to file
    if save_preds:
        with open(op_file_name, 'w') as opfile:
            opfile.write('id,predicted\n')
            for ii in range(len(im_ids)):
                opfile.write(str(im_ids[ii]) + ',' + ' '.join(str(x) for x in preds[ii,:])+'\n')
            #return
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-22-540edeaccfee> in <module>()
      1 if evaluate:
----> 2     prec3, preds, im_ids = validate(val_loader, model, criterion, True)
      3     # write predictions to file
      4     if save_preds:
      5         with open(op_file_name, 'w') as opfile:

NameError: name 'validate' is not defined
In [23]:
print_freq = 10 
In [24]:
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
    torch.save(state, filename)
    if is_best:
        print("\tSaving new best model")
        shutil.copyfile(filename, 'model_best.pth.tar')


class AverageMeter(object):
    """Computes and stores the average and current value"""
    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count


def adjust_learning_rate(optimizer, epoch):
    """Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
    lr = max_lr * (0.1 ** (epoch // 30))
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr


def accuracy(output, target, topk=(1,)):
    """Computes the precision@k for the specified values of k"""
    maxk = max(topk)
    batch_size = target.size(0)

    _, pred = output.topk(maxk, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.view(1, -1).expand_as(pred))

    res = []
    for k in topk:
        correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
        res.append(correct_k.mul_(100.0 / batch_size))
    return res
In [25]:
def train(train_loader, model, criterion, optimizer, epoch):
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top3 = AverageMeter()
    

    # switch to train mode
    model.train()

    end = time.time()
    print('Epoch:{0}'.format(epoch))
    print('Itr\t\tTime\t\tData\t\tLoss\t\tPrec@1\t\tPrec@3')
    for i, (input, im_id, target, tax_ids) in enumerate(train_loader):
        # measure data loading time
        data_time.update(time.time() - end)

        input = input.cuda()
        target = target.cuda()
        input_var = torch.autograd.Variable(input)
        target_var = torch.autograd.Variable(target)

        # compute output
        output = model(input_var)
        loss = criterion(output, target_var)

        # measure accuracy and record loss
        prec1, prec3 = accuracy(output.data, target, topk=(1, 3))
        losses.update(loss.item(), input.size(0))
        top1.update(prec1[0], input.size(0))
        top3.update(prec3[0], input.size(0))

        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % 100 == 0:
            print('[{0}/{1}]\t'
                '{batch_time.val:.2f} ({batch_time.avg:.2f})\t'
                '{data_time.val:.2f} ({data_time.avg:.2f})\t'
                '{loss.val:.3f} ({loss.avg:.3f})\t'
                '{top1.val:.2f} ({top1.avg:.2f})\t'
                '{top3.val:.2f} ({top3.avg:.2f})'.format(
                i, len(train_loader), batch_time=batch_time,
                data_time=data_time, loss=losses, top1=top1, top3=top3))
In [26]:
def validate(val_loader, model, criterion, save_preds=False):
    batch_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top3 = AverageMeter()

    # switch to evaluate mode
    model.eval()

    end = time.time()
    pred = []
    im_ids = []

    print('Validate:\tTime\t\tLoss\t\tPrec@1\t\tPrec@3')
    for i, (input, im_id, target, tax_ids) in enumerate(val_loader):

        input = input.cuda()
        target = target.cuda()
        input_var = torch.autograd.Variable(input, volatile=True)
        target_var = torch.autograd.Variable(target, volatile=True)

        # compute output
        output = model(input_var)
        loss = criterion(output, target_var)

        if save_preds:
            # store the top K classes for the prediction
            im_ids.append(im_id.cpu().numpy().astype(np.int))
            _, pred_inds = output.data.topk(3,1,True,True)
            pred.append(pred_inds.cpu().numpy().astype(np.int))

        # measure accuracy and record loss
        prec1, prec3 = accuracy(output.data, target, topk=(1, 3))
        losses.update(loss.item(), input.size(0))
        top1.update(prec1[0], input.size(0))
        top3.update(prec3[0], input.size(0))

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % 10 == 0:
            print('[{0}/{1}]\t'
                  '{batch_time.val:.2f} ({batch_time.avg:.2f})\t'
                  '{loss.val:.3f} ({loss.avg:.3f})\t'
                  '{top1.val:.2f} ({top1.avg:.2f})\t'
                  '{top3.val:.2f} ({top3.avg:.2f})'.format(
                   i, len(val_loader), batch_time=batch_time, loss=losses,
                   top1=top1, top3=top3))

    print(' * Prec@1 {top1.avg:.3f} Prec@3 {top3.avg:.3f}'
          .format(top1=top1, top3=top3))

    if save_preds:
        return top3.avg, np.vstack(pred), np.hstack(im_ids)
    else:
        return top3.avg
In [27]:
start_epoch=0
epochs=1
max_lr = 1e-2
    
for epoch in range(start_epoch, epochs):
    adjust_learning_rate(optimizer, epoch)

    # train for one epoch
    train(train_loader, model, criterion, optimizer, epoch)

    # evaluate on validation set
    prec3 = validate(val_loader, model, criterion, False)

    # remember best prec@1 and save checkpoint
    is_best = prec3 > best_prec3
    best_prec3 = max(prec3, best_prec3)
    save_checkpoint({
        'epoch': epoch + 1,
        #'arch': args.arch,
        'state_dict': model.state_dict(),
        'best_prec3': best_prec3,
        'optimizer' : optimizer.state_dict(),
    }, is_best)
Epoch:0
Itr		Time		Data		Loss		Prec@1		Prec@3
[0/5526]	6.41 (6.41)	2.01 (2.01)	6.943 (6.943)	0.00 (0.00)	0.00 (0.00)
[100/5526]	1.71 (1.87)	1.36 (1.47)	6.814 (6.927)	0.00 (0.12)	2.08 (0.39)
[200/5526]	1.87 (1.83)	1.51 (1.45)	6.620 (6.855)	0.00 (0.16)	0.00 (0.49)
[300/5526]	1.72 (1.82)	1.37 (1.45)	6.702 (6.827)	0.00 (0.16)	0.00 (0.50)
[400/5526]	1.78 (1.81)	1.43 (1.44)	6.729 (6.810)	0.00 (0.16)	2.08 (0.55)
[500/5526]	1.73 (1.80)	1.38 (1.44)	6.859 (6.801)	0.00 (0.16)	0.00 (0.55)
[600/5526]	1.65 (1.80)	1.30 (1.43)	6.707 (6.791)	0.00 (0.18)	0.00 (0.56)
[700/5526]	1.83 (1.79)	1.48 (1.43)	6.672 (6.784)	0.00 (0.18)	0.00 (0.55)
[800/5526]	1.60 (1.79)	1.24 (1.43)	6.699 (6.778)	0.00 (0.18)	4.17 (0.56)
[900/5526]	1.72 (1.78)	1.36 (1.42)	6.689 (6.772)	0.00 (0.17)	0.00 (0.53)
[1000/5526]	1.78 (1.78)	1.43 (1.42)	6.775 (6.767)	0.00 (0.17)	0.00 (0.54)
[1100/5526]	1.74 (1.78)	1.38 (1.42)	6.727 (6.763)	0.00 (0.17)	0.00 (0.54)
[1200/5526]	1.77 (1.78)	1.42 (1.42)	6.729 (6.759)	2.08 (0.16)	2.08 (0.53)
[1300/5526]	1.80 (1.78)	1.45 (1.42)	6.705 (6.758)	0.00 (0.16)	0.00 (0.54)
[1400/5526]	1.78 (1.78)	1.43 (1.42)	6.542 (6.755)	0.00 (0.17)	0.00 (0.55)
[1500/5526]	1.73 (1.77)	1.38 (1.42)	6.717 (6.753)	0.00 (0.17)	0.00 (0.56)
[1600/5526]	1.72 (1.77)	1.36 (1.42)	6.749 (6.751)	0.00 (0.17)	0.00 (0.55)
[1700/5526]	1.72 (1.77)	1.36 (1.41)	6.812 (6.750)	2.08 (0.17)	2.08 (0.55)
[1800/5526]	1.90 (1.77)	1.55 (1.41)	6.698 (6.749)	0.00 (0.17)	0.00 (0.54)
[1900/5526]	1.91 (1.77)	1.55 (1.41)	6.800 (6.748)	0.00 (0.17)	0.00 (0.55)
[2000/5526]	1.77 (1.77)	1.41 (1.41)	6.770 (6.746)	0.00 (0.17)	2.08 (0.55)
[2100/5526]	1.65 (1.77)	1.30 (1.41)	6.620 (6.744)	0.00 (0.17)	0.00 (0.55)
[2200/5526]	1.73 (1.77)	1.38 (1.41)	6.784 (6.743)	0.00 (0.17)	0.00 (0.54)
[2300/5526]	1.85 (1.77)	1.49 (1.41)	6.789 (6.743)	2.08 (0.17)	2.08 (0.54)
[2400/5526]	1.78 (1.77)	1.42 (1.41)	6.668 (6.742)	0.00 (0.17)	0.00 (0.54)
[2500/5526]	1.73 (1.77)	1.37 (1.41)	6.804 (6.742)	2.08 (0.17)	4.17 (0.54)
[2600/5526]	1.64 (1.77)	1.28 (1.41)	6.645 (6.741)	0.00 (0.16)	0.00 (0.53)
[2700/5526]	1.76 (1.77)	1.40 (1.41)	6.719 (6.740)	0.00 (0.16)	0.00 (0.53)
[2800/5526]	1.89 (1.77)	1.53 (1.41)	6.676 (6.739)	0.00 (0.17)	2.08 (0.54)
[2900/5526]	1.68 (1.77)	1.31 (1.41)	6.694 (6.738)	0.00 (0.17)	0.00 (0.54)
[3000/5526]	1.73 (1.77)	1.37 (1.41)	6.794 (6.738)	0.00 (0.17)	0.00 (0.54)
[3100/5526]	1.73 (1.77)	1.37 (1.41)	6.777 (6.737)	0.00 (0.17)	0.00 (0.54)
[3200/5526]	1.78 (1.77)	1.41 (1.41)	6.644 (6.737)	0.00 (0.17)	0.00 (0.54)
[3300/5526]	1.73 (1.77)	1.37 (1.41)	6.735 (6.736)	0.00 (0.17)	0.00 (0.54)
[3400/5526]	2.02 (1.77)	1.65 (1.41)	6.632 (6.736)	0.00 (0.17)	0.00 (0.54)
[3500/5526]	1.67 (1.77)	1.30 (1.41)	6.681 (6.735)	0.00 (0.17)	2.08 (0.55)
[3600/5526]	1.80 (1.77)	1.44 (1.41)	6.740 (6.735)	0.00 (0.17)	0.00 (0.54)
[3700/5526]	1.73 (1.77)	1.37 (1.41)	6.701 (6.735)	0.00 (0.17)	0.00 (0.54)
[3800/5526]	1.67 (1.77)	1.31 (1.41)	6.700 (6.735)	0.00 (0.17)	0.00 (0.54)
[3900/5526]	1.81 (1.77)	1.45 (1.41)	6.689 (6.734)	0.00 (0.17)	0.00 (0.54)
[4000/5526]	1.74 (1.77)	1.37 (1.41)	6.620 (6.733)	0.00 (0.17)	0.00 (0.53)
[4100/5526]	1.80 (1.77)	1.43 (1.41)	6.746 (6.733)	0.00 (0.17)	0.00 (0.53)
[4200/5526]	1.75 (1.77)	1.39 (1.41)	6.718 (6.732)	0.00 (0.17)	0.00 (0.53)
[4300/5526]	1.72 (1.77)	1.36 (1.41)	6.732 (6.732)	0.00 (0.17)	0.00 (0.53)
[4400/5526]	1.69 (1.77)	1.33 (1.41)	6.682 (6.731)	0.00 (0.17)	4.17 (0.53)
[4500/5526]	1.67 (1.77)	1.31 (1.41)	6.783 (6.731)	0.00 (0.17)	0.00 (0.54)
[4600/5526]	1.74 (1.77)	1.38 (1.41)	6.750 (6.731)	0.00 (0.17)	2.08 (0.54)
[4700/5526]	1.71 (1.77)	1.35 (1.41)	6.686 (6.730)	0.00 (0.17)	0.00 (0.54)
[4800/5526]	1.85 (1.77)	1.49 (1.41)	6.730 (6.730)	0.00 (0.17)	2.08 (0.54)
[4900/5526]	1.95 (1.77)	1.58 (1.41)	6.744 (6.730)	0.00 (0.17)	0.00 (0.54)
[5000/5526]	1.78 (1.77)	1.42 (1.41)	6.821 (6.730)	0.00 (0.17)	0.00 (0.54)
[5100/5526]	1.72 (1.77)	1.36 (1.41)	6.718 (6.730)	0.00 (0.17)	0.00 (0.54)
[5200/5526]	1.74 (1.77)	1.38 (1.41)	6.759 (6.729)	2.08 (0.17)	4.17 (0.55)
[5300/5526]	1.89 (1.77)	1.53 (1.41)	6.704 (6.729)	0.00 (0.17)	0.00 (0.55)
[5400/5526]	1.87 (1.77)	1.51 (1.41)	6.807 (6.729)	0.00 (0.17)	0.00 (0.54)
[5500/5526]	1.69 (1.77)	1.33 (1.41)	6.743 (6.729)	0.00 (0.17)	0.00 (0.54)
Validate:	Time		Loss		Prec@1		Prec@3
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:19: UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:20: UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.
[0/95]	1.73 (1.73)	7.279 (7.279)	0.00 (0.00)	0.00 (0.00)
[10/95]	0.66 (0.75)	7.261 (7.177)	0.00 (0.00)	0.00 (0.00)
[20/95]	0.70 (0.74)	7.440 (7.215)	0.00 (0.00)	0.00 (0.00)
[30/95]	0.67 (0.73)	7.231 (7.180)	0.00 (0.00)	0.00 (0.10)
[40/95]	0.62 (0.71)	7.347 (7.187)	0.00 (0.00)	0.00 (0.15)
[50/95]	0.62 (0.70)	7.056 (7.178)	0.00 (0.00)	0.00 (0.18)
[60/95]	0.61 (0.69)	7.176 (7.175)	0.00 (0.05)	0.00 (0.31)
[70/95]	0.65 (0.69)	7.102 (7.185)	0.00 (0.04)	0.00 (0.26)
[80/95]	0.65 (0.68)	7.304 (7.187)	0.00 (0.04)	0.00 (0.27)
[90/95]	0.67 (0.68)	6.866 (7.181)	0.00 (0.07)	0.00 (0.27)
 * Prec@1 0.099 Prec@3 0.297
	Saving new best model
In [28]:
!wget http://vision.caltech.edu/~macaodha/inat2018/iNat_2018_InceptionV3.pth.tar
--2019-04-03 07:09:30--  http://vision.caltech.edu/~macaodha/inat2018/iNat_2018_InceptionV3.pth.tar
Resolving vision.caltech.edu (vision.caltech.edu)... 34.208.54.77
Connecting to vision.caltech.edu (vision.caltech.edu)|34.208.54.77|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.vision.caltech.edu/~macaodha/inat2018/iNat_2018_InceptionV3.pth.tar [following]
--2019-04-03 07:09:30--  http://www.vision.caltech.edu/~macaodha/inat2018/iNat_2018_InceptionV3.pth.tar
Resolving www.vision.caltech.edu (www.vision.caltech.edu)... 34.208.54.77
Reusing existing connection to vision.caltech.edu:80.
HTTP request sent, awaiting response... 200 OK
Length: 321312907 (306M) [application/x-tar]
Saving to: ‘iNat_2018_InceptionV3.pth.tar’

iNat_2018_Inception 100%[===================>] 306.43M  3.49MB/s    in 59s     

2019-04-03 07:10:30 (5.16 MB/s) - ‘iNat_2018_InceptionV3.pth.tar’ saved [321312907/321312907]

In [29]:
sub=pd.read_csv('../input/kaggle_sample_submission.csv')

this next part is wrong, but it doesnt have to be.

In [30]:
def test(test_loader, model, criterion, save_preds=True):
    batch_time = AverageMeter()
    #losses = AverageMeter()
    ttop1 = AverageMeter()
    ttop3 = AverageMeter()

    # switch to evaluate mode
    model.eval()

    end = time.time()
    pred = []
    im_ids = []

    print('Validate:\tTime\t\tLoss\t\tPrec@1\t\tPrec@3')
    for i, (input, im_id, target, tax_ids) in enumerate(test_loader):

        input = input.cuda()
        #target = target.cuda()
        with torch.no_grad():
            input_var = input
            #target_var = target
            return input_var #, target_var
        #input_var = torch.autograd.Variable(input, volatile=True)
        #target_var = torch.autograd.Variable(target, volatile=True)

        # compute output
        output = model(input_var)
        #loss = criterion(output, target_var)

        if save_preds:
            # store the top K classes for the prediction
            im_ids.append(im_id.cpu().numpy().astype(np.int))
            _, pred_inds = output.data.topk(3,1,True,True)
            pred.append(pred_inds.cpu().numpy().astype(np.int))

        # measure accuracy and record loss
        tprec1, tprec3 = accuracy(output.data, target, topk=(1, 3))
        #losses.update(loss.item(), input.size(0))
        ttop1.update(tprec1[0], input.size(0))
        ttop3.update(tprec3[0], input.size(0))
In [31]:
wat = test(test_loader, model, criterion, save_preds=True)
Validate:	Time		Loss		Prec@1		Prec@3
In [32]:
for epoch in range(start_epoch, 10):
    #adjust_learning_rate(optimizer, epoch)

    # train for one epoch
    #train(train_loader, model, criterion, optimizer, epoch)

    # evaluate on validation set
    prec3 = test(test_loader, model, criterion, save_preds=True)
Validate:	Time		Loss		Prec@1		Prec@3
Validate:	Time		Loss		Prec@1		Prec@3
Validate:	Time		Loss		Prec@1		Prec@3
Validate:	Time		Loss		Prec@1		Prec@3
Validate:	Time		Loss		Prec@1		Prec@3
Validate:	Time		Loss		Prec@1		Prec@3
Validate:	Time		Loss		Prec@1		Prec@3
Validate:	Time		Loss		Prec@1		Prec@3
Validate:	Time		Loss		Prec@1		Prec@3
Validate:	Time		Loss		Prec@1		Prec@3
In [33]:
prec3.shape
Out[33]:
torch.Size([4, 3, 299, 299])
In [34]:
import torchvision
import matplotlib.pyplot as plt
%matplotlib inline
In [35]:
def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()


# get some random training images
dataiter = iter(test_loader)
images, ids, labels, wut = dataiter.next()

# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % labels[j] for j in range(4)))
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
tensor(0) tensor(0) tensor(0) tensor(0)
In [36]:
classes = trl.classes
In [37]:
outputs = model(images.cuda())
In [38]:
_, predicted = torch.max(outputs, 1)

print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
                              for j in range(4)))
Predicted:    431   431   431   431
In [39]:
psz=[]
with torch.no_grad():
    for data in test_loader:
        images, ids, labels, wut = data
        outputs = model(images.cuda())
        _, predicted = torch.max(outputs.data, 1)
        psz.append(predicted)
In [40]:
outputs.shape
Out[40]:
torch.Size([2, 1010])