Kaggle Kernel - inaturalist-2019-fgvc6

Inat2019 Starter Keras (EfficientNet)

From: https://www.kaggle.com/ateplyuk/inat2019-starter-keras-efficientnet

Author: Alexander Teplyuk

Score: 0.9417

Example of transfer learning from pretrained model using Keras and Efficientnet (https://pypi.org/project/efficientnet/).

In [1]:

!pip install git+https://github.com/qubvel/efficientnet

Collecting git+https://github.com/qubvel/efficientnet
  Cloning https://github.com/qubvel/efficientnet to /tmp/pip-req-build-hfbnsc0k
Building wheels for collected packages: efficientnet
  Building wheel for efficientnet (setup.py) ... - done
  Stored in directory: /tmp/pip-ephem-wheel-cache-tr_hg919/wheels/64/60/2e/30ebaa76ed1626e86bfb0cc0579b737fdb7d9ff8cb9522663a
Successfully built efficientnet
Installing collected packages: efficientnet
Successfully installed efficientnet-0.0.2
You are using pip version 19.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

In [2]:

from efficientnet import EfficientNetB3

Using TensorFlow backend.

In [3]:

import os
import cv2
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import json
from keras.models import Sequential, Model
from keras.layers import Dense, Flatten, Activation, Dropout, GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers, applications
from keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping
from keras import backend as K

Train data¶

In [4]:

ann_file = '../input/inaturalist-2019-fgvc6/train2019.json'
with open(ann_file) as data_file:
        train_anns = json.load(data_file)

In [5]:

train_anns_df = pd.DataFrame(train_anns['annotations'])[['image_id','category_id']]
train_img_df = pd.DataFrame(train_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
df_train_file_cat = pd.merge(train_img_df, train_anns_df, on='image_id')
df_train_file_cat['category_id']=df_train_file_cat['category_id'].astype(str)
df_train_file_cat.head()

Out[5]:

| image_id | file_name | category_id
0 | 0 | train_val2019/Plants/400/d1322d13ccd856eb4236c... | 400
1 | 1 | train_val2019/Plants/570/15edbc1e2ef000d8ace48... | 570
2 | 2 | train_val2019/Reptiles/167/c87a32e8927cbf4f06d... | 167
3 | 3 | train_val2019/Birds/254/9fcdd1d37e96d8fd94dfdc... | 254
4 | 4 | train_val2019/Plants/739/ffa06f951e99de9d220ae... | 739

In [6]:

len(df_train_file_cat['category_id'].unique())

Out[6]:

1010

In [7]:

# Example of images for category_id = 400
img_names = df_train_file_cat[df_train_file_cat['category_id']=='400']['file_name'][:30]

plt.figure(figsize=[15,15])
i = 1
for img_name in img_names:
    img = cv2.imread("../input/inaturalist-2019-fgvc6/train_val2019/%s" % img_name)[...,[2, 1, 0]]
    plt.subplot(6, 5, i)
    plt.imshow(img)
    i += 1
plt.show()

Validation data¶

In [8]:

valid_ann_file = '../input/inaturalist-2019-fgvc6/val2019.json'
with open(valid_ann_file) as data_file:
        valid_anns = json.load(data_file)

In [9]:

valid_anns_df = pd.DataFrame(valid_anns['annotations'])[['image_id','category_id']]
valid_anns_df.head()

Out[9]:

| image_id | category_id
0 | 265213 | 644
1 | 265214 | 597
2 | 265215 | 883
3 | 265216 | 300
4 | 265217 | 881

In [10]:

valid_img_df = pd.DataFrame(valid_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
valid_img_df.head()

Out[10]:

| image_id | file_name
0 | 265213 | train_val2019/Plants/644/716a69838526f3ada3b2f...
1 | 265214 | train_val2019/Plants/597/0942cc64d2e759c5ee059...
2 | 265215 | train_val2019/Plants/883/acfdbfd9fa675f1c84558...
3 | 265216 | train_val2019/Birds/300/5f3194ff536c7dd31d80b7...
4 | 265217 | train_val2019/Plants/881/76acaf0b2841f91982d21...

In [11]:

df_valid_file_cat = pd.merge(valid_img_df, valid_anns_df, on='image_id')
df_valid_file_cat['category_id']=df_valid_file_cat['category_id'].astype(str)
df_valid_file_cat.head()

Out[11]:

| image_id | file_name | category_id
0 | 265213 | train_val2019/Plants/644/716a69838526f3ada3b2f... | 644
1 | 265214 | train_val2019/Plants/597/0942cc64d2e759c5ee059... | 597
2 | 265215 | train_val2019/Plants/883/acfdbfd9fa675f1c84558... | 883
3 | 265216 | train_val2019/Birds/300/5f3194ff536c7dd31d80b7... | 300
4 | 265217 | train_val2019/Plants/881/76acaf0b2841f91982d21... | 881

In [12]:

nb_classes = 1010
batch_size = 256
img_size = 96
nb_epochs = 10

In [13]:

%%time
train_datagen=ImageDataGenerator(rescale=1./255,
    validation_split=0.25,
    horizontal_flip = True,
    zoom_range = 0.3,
    width_shift_range = 0.3,
    height_shift_range=0.3
    )

train_generator=train_datagen.flow_from_dataframe(
    dataframe=df_train_file_cat,
    directory="../input/inaturalist-2019-fgvc6/train_val2019",
    x_col="file_name",
    y_col="category_id",
    batch_size=batch_size,
    shuffle=True,
    class_mode="categorical",
    target_size=(img_size,img_size))

Found 265213 images belonging to 1010 classes.
CPU times: user 3.4 s, sys: 6.03 s, total: 9.44 s
Wall time: 41.1 s

In [14]:

%%time
test_datagen = ImageDataGenerator(rescale=1./255)

valid_generator=test_datagen.flow_from_dataframe(
    dataframe=df_valid_file_cat,
    directory="../input/inaturalist-2019-fgvc6/train_val2019",
    x_col="file_name",
    y_col="category_id",
    batch_size=batch_size,
    shuffle=True,
    class_mode="categorical",
    target_size=(img_size,img_size))

Found 3030 images belonging to 1010 classes.
CPU times: user 32 ms, sys: 80 ms, total: 112 ms
Wall time: 416 ms

Model¶

In [15]:

model = EfficientNetB3(weights='imagenet', include_top=False, input_shape=(img_size, img_size, 3))

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/function.py:1007: calling Graph.create_op (from tensorflow.python.framework.ops) with compute_shapes is deprecated and will be removed in a future version.
Instructions for updating:
Shapes are always computed; don't use the compute_shapes as it has no effect.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/efficientnet/layers.py:29: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Deprecated in favor of operator or tf.math.divide.
Downloading data from https://github.com/qubvel/efficientnet/releases/download/v0.0.1/efficientnet-b3_imagenet_1000_notop.h5
43974656/43966704 [==============================] - 2s 0us/step

In [16]:

model.trainable = False

In [17]:

# Freeze some layers
# for layer in model.layers[:-4]:
#     layer.trainable = False

In [18]:

#Adding custom layers
x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.5)(x)
predictions = Dense(nb_classes, activation="softmax")(x)
model_final = Model(input = model.input, output = predictions)

model_final.compile(optimizers.rmsprop(lr=0.0001, decay=1e-6),loss='categorical_crossentropy',metrics=['accuracy'])

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.

/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:7: UserWarning: Update your `Model` call to the Keras 2 API: `Model(inputs=Tensor("in..., outputs=Tensor("de...)`
  import sys

In [19]:

# Callbacks

checkpoint = ModelCheckpoint("vgg16_1.h5", monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')

In [20]:

%%time
history = model_final.fit_generator(generator=train_generator,

                                    steps_per_epoch=5,

                                    validation_data=valid_generator,

                                    validation_steps=2,

                                    epochs=nb_epochs,
                                    callbacks = [checkpoint, early],
                                    verbose=2)

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Epoch 1/10
 - 44s - loss: 7.2508 - acc: 7.8125e-04 - val_loss: 7.0851 - val_acc: 0.0020

Epoch 00001: val_loss improved from inf to 7.08512, saving model to vgg16_1.h5
Epoch 2/10
 - 4s - loss: 7.1507 - acc: 0.0031 - val_loss: 7.0564 - val_acc: 0.0000e+00

Epoch 00002: val_loss improved from 7.08512 to 7.05643, saving model to vgg16_1.h5
Epoch 3/10
 - 4s - loss: 7.2046 - acc: 0.0016 - val_loss: 7.0570 - val_acc: 0.0000e+00

Epoch 00003: val_loss did not improve from 7.05643
Epoch 4/10
 - 20s - loss: 7.0902 - acc: 7.8125e-04 - val_loss: 7.0295 - val_acc: 0.0000e+00

Epoch 00004: val_loss improved from 7.05643 to 7.02947, saving model to vgg16_1.h5
Epoch 5/10
 - 26s - loss: 7.0863 - acc: 0.0000e+00 - val_loss: 6.9757 - val_acc: 0.0000e+00

Epoch 00005: val_loss improved from 7.02947 to 6.97572, saving model to vgg16_1.h5
Epoch 6/10
 - 26s - loss: 7.0095 - acc: 0.0031 - val_loss: 6.9849 - val_acc: 0.0000e+00

Epoch 00006: val_loss did not improve from 6.97572
Epoch 7/10
 - 26s - loss: 7.0256 - acc: 0.0000e+00 - val_loss: 7.0053 - val_acc: 0.0000e+00

Epoch 00007: val_loss did not improve from 6.97572
Epoch 8/10
 - 26s - loss: 6.9991 - acc: 0.0000e+00 - val_loss: 7.0454 - val_acc: 0.0059

Epoch 00008: val_loss did not improve from 6.97572
Epoch 9/10
 - 26s - loss: 6.9938 - acc: 0.0039 - val_loss: 6.9751 - val_acc: 0.0039

Epoch 00009: val_loss improved from 6.97572 to 6.97510, saving model to vgg16_1.h5
Epoch 10/10
 - 26s - loss: 6.9391 - acc: 0.0039 - val_loss: 6.9797 - val_acc: 0.0059

Epoch 00010: val_loss did not improve from 6.97510
CPU times: user 7min 54s, sys: 26.4 s, total: 8min 21s
Wall time: 5min 33s

In [21]:

with open('history.json', 'w') as f:
    json.dump(history.history, f)

history_df = pd.DataFrame(history.history)
history_df[['loss', 'val_loss']].plot()
history_df[['acc', 'val_acc']].plot()

Out[21]:

<matplotlib.axes._subplots.AxesSubplot at 0x7f063f9bbb38>

Test data¶

In [22]:

test_ann_file = '../input/inaturalist-2019-fgvc6/test2019.json'
with open(test_ann_file) as data_file:
        test_anns = json.load(data_file)

In [23]:

test_img_df = pd.DataFrame(test_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
test_img_df.head()

Out[23]:

| image_id | file_name
0 | 268243 | test2019/e295f3c7046b1f1e80c0301401324aa9.jpg
1 | 268244 | test2019/ad3dcbb6846ed0b4dab58d7b1a4210ba.jpg
2 | 268245 | test2019/e697be8e296b4b140cff4f96f85c364f.jpg
3 | 268246 | test2019/7e7ba55e6aa26ba99e814d63b15d0121.jpg
4 | 268247 | test2019/6cb6372079d23702511c06923970f13f.jpg

In [24]:

%%time

test_generator = test_datagen.flow_from_dataframe(

        dataframe=test_img_df,

        directory = "../input/inaturalist-2019-fgvc6/test2019",
        x_col="file_name",
        target_size = (img_size,img_size),
        batch_size = 1,
        shuffle = False,
        class_mode = None
        )

Found 35350 images.
CPU times: user 7.28 s, sys: 852 ms, total: 8.14 s
Wall time: 7.34 s

Prediction¶

In [25]:

%%time
test_generator.reset()
predict=model_final.predict_generator(test_generator, steps = len(test_generator.filenames))

CPU times: user 47min 31s, sys: 35.6 s, total: 48min 6s
Wall time: 40min 25s

In [26]:

len(predict)

Out[26]:

35350

In [27]:

predicted_class_indices=np.argmax(predict,axis=1)

In [28]:

labels = (train_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
predictions = [labels[k] for k in predicted_class_indices]

In [29]:

sam_sub_df = pd.read_csv('../input/inaturalist-2019-fgvc6/kaggle_sample_submission.csv')
sam_sub_df.head()

Out[29]:

| id | predicted
0 | 268243 | 842
1 | 268244 | 139
2 | 268245 | 988
3 | 268246 | 612
4 | 268247 | 468

In [30]:

filenames=test_generator.filenames
results=pd.DataFrame({"file_name":filenames,
                      "predicted":predictions})
df_res = pd.merge(test_img_df, results, on='file_name')[['image_id','predicted']]\
    .rename(columns={'image_id':'id'})

df_res.head()

Out[30]:

| id | predicted
0 | 268243 | 270
1 | 268244 | 325
2 | 268245 | 393
3 | 268246 | 656
4 | 268247 | 124

In [31]:

df_res.to_csv("submission.csv",index=False)

fast.ai ootb (cutout+efficientnet)

From: https://www.kaggle.com/khursani8/fast-ai-ootb-cutout-efficientnet

Author: khursani

Score: 0.38584

Reference

https://www.kaggle.com/sujoykg/xception-keras

Try

Use mixup

fp_16

Oversampling

cutout

efficientnet b3

In [1]:

from fastai.vision import *
from fastai.metrics import *
PATH = Path('../input')

In [2]:

ann_file = '../input/train2019.json'
with open(ann_file) as data_file:
        train_anns = json.load(data_file)

train_anns_df = pd.DataFrame(train_anns['annotations'])[['image_id','category_id']]
train_img_df = pd.DataFrame(train_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
df_train_file_cat = pd.merge(train_img_df, train_anns_df, on='image_id')
df_train_file_cat['category_id']=df_train_file_cat['category_id'].astype(str)
df_train_file_cat = df_train_file_cat.drop(['image_id'],axis=1)
df_train_file_cat.head()

Out[2]:

| file_name | category_id
0 | train_val2019/Plants/400/d1322d13ccd856eb4236c... | 400
1 | train_val2019/Plants/570/15edbc1e2ef000d8ace48... | 570
2 | train_val2019/Reptiles/167/c87a32e8927cbf4f06d... | 167
3 | train_val2019/Birds/254/9fcdd1d37e96d8fd94dfdc... | 254
4 | train_val2019/Plants/739/ffa06f951e99de9d220ae... | 739

In [3]:

%%time
# Try Oversampling

res = None
sample_to = df_train_file_cat.category_id.value_counts().max() # which is 500

for grp in df_train_file_cat.groupby('category_id'):
    n = grp[1].shape[0]
    additional_rows = grp[1].sample(0 if sample_to < n  else sample_to - n, replace=True)
    rows = pd.concat((grp[1], additional_rows))

    if res is None: res = rows
    else: res = pd.concat((res, rows))

CPU times: user 11.3 s, sys: 20 ms, total: 11.3 s
Wall time: 11.3 s

In [4]:

res.category_id.value_counts()[:10]

Out[4]:

298    500
879    500
42     500
161    500
268    500
819    500
452    500
511    500
195    500
593    500
Name: category_id, dtype: int64

In [5]:

test_ann_file = '../input/test2019.json'
with open(test_ann_file) as data_file:
        test_anns = json.load(data_file)
test_img_df = pd.DataFrame(test_anns['images'])[['file_name','id']].rename(columns={'id':'image_id'})
test_img_df.head()

Out[5]:

| file_name | image_id
0 | test2019/e295f3c7046b1f1e80c0301401324aa9.jpg | 268243
1 | test2019/ad3dcbb6846ed0b4dab58d7b1a4210ba.jpg | 268244
2 | test2019/e697be8e296b4b140cff4f96f85c364f.jpg | 268245
3 | test2019/7e7ba55e6aa26ba99e814d63b15d0121.jpg | 268246
4 | test2019/6cb6372079d23702511c06923970f13f.jpg | 268247

In [6]:

src = (
ImageList.from_df(df=res,path=PATH/"train_val2019")
    .use_partial_data(0.3)
    .split_by_rand_pct(0.1)
    .label_from_df()
    .add_test(ImageList.from_df(df=test_img_df,path=PATH/"test2019"))
)

In [7]:

data = (
    src
    .transform(get_transforms(),size=128)
    .databunch(bs=64*2)
    .normalize(imagenet_stats)
)

In [8]:

!pip install efficientnet_pytorch

Collecting efficientnet_pytorch
  Downloading https://files.pythonhosted.org/packages/06/ff/881afd965c46b11fc6f3c8316de9e08d37fc3b71056dbab861b76faee6ca/efficientnet_pytorch-0.1.0-py3-none-any.whl
Requirement already satisfied: torch in /opt/conda/lib/python3.6/site-packages (from efficientnet_pytorch) (1.0.1.post2)
Installing collected packages: efficientnet-pytorch
Successfully installed efficientnet-pytorch-0.1.0
You are using pip version 19.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

In [9]:

from efficientnet_pytorch import EfficientNet

In [10]:

model_name = 'efficientnet-b3'
def getModel(pret):
    model = EfficientNet.from_pretrained(model_name)
#     model._bn1 = nn.Identity()
    model._fc = nn.Linear(1536,data.c)
    return model

In [11]:

# learn = cnn_learner(data,models.densenet201,metrics=[error_rate],model_dir='/kaggle/working',pretrained=True,loss_func=LabelSmoothingCrossEntropy()).mixup()

In [12]:

learn = Learner(data,getModel(False),metrics=[error_rate],model_dir='/kaggle/working',loss_func=LabelSmoothingCrossEntropy()).mixup().to_fp16()

Downloading: "http://storage.googleapis.com/public-models/efficientnet-b3-c8376fa2.pth" to /tmp/.torch/models/efficientnet-b3-c8376fa2.pth
49380963it [00:00, 152758227.72it/s]

Loaded pretrained weights for efficientnet-b3

In [13]:

learn.lr_find()
learn.recorder.plot()

LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.

In [14]:

learn.fit_one_cycle(3,1e-3)

66.67% [2/3 1:39:07<49:33]

epoch | train_loss | valid_loss | error_rate | time
0 | 4.651476 | 3.750852 | 0.711023 | 49:33
1 | 3.969582 | 2.883739 | 0.512937 | 49:33

43.76% [466/1065 19:53<25:33 3.6636]

In [15]:

SZ=224
cutout_frac = 0.25
p_cutout = 0.75
cutout_sz = round(SZ*cutout_frac)
cutout_tfm = cutout(n_holes=(1,1), length=(cutout_sz, cutout_sz), p=p_cutout)

In [16]:

learn.data = (
    src
    .transform(get_transforms(xtra_tfms=[cutout_tfm]),size=SZ)
    .databunch(bs=64)
    .normalize(imagenet_stats)
)

In [17]:

learn.fit_one_cycle(7,1e-3)

0.00% [0/7 00:00<00:00]

epoch | train_loss | valid_loss | error_rate | time

Interrupted

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-17-2a1f08144d6a> in <module>()
----> 1 learn.fit_one_cycle(7,1e-3)

/opt/conda/lib/python3.6/site-packages/fastai/train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, final_div, wd, callbacks, tot_epochs, start_epoch)
     20     callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,
     21                                        final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))
---> 22     learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
     23
     24 def lr_find(learn:Learner, start_lr:Floats=1e-7, end_lr:Floats=10, num_it:int=100, stop_div:bool=True, wd:float=None):

/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)
    194         callbacks = [cb(self) for cb in self.callback_fns] + listify(callbacks)
    195         if defaults.extra_callbacks is not None: callbacks += defaults.extra_callbacks
--> 196         fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
    197
    198     def create_opt(self, lr:Floats, wd:Floats=0.)->None:

/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in fit(epochs, learn, callbacks, metrics)
     98             for xb,yb in progress_bar(learn.data.train_dl, parent=pbar):
     99                 xb, yb = cb_handler.on_batch_begin(xb, yb)
--> 100                 loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler)
    101                 if cb_handler.on_batch_end(loss): break
    102

/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler)
     23     if not is_listy(xb): xb = [xb]
     24     if not is_listy(yb): yb = [yb]
---> 25     out = model(*xb)
     26     out = cb_handler.on_loss_begin(out)
     27

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

/opt/conda/lib/python3.6/site-packages/efficientnet_pytorch/model.py in forward(self, inputs)
    167
    168         # Convolution layers
--> 169         x = self.extract_features(inputs)
    170
    171         # Head

/opt/conda/lib/python3.6/site-packages/efficientnet_pytorch/model.py in extract_features(self, inputs)
    152
    153         # Stem
--> 154         x = relu_fn(self._bn0(self._conv_stem(inputs)))
    155
    156         # Blocks

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

/opt/conda/lib/python3.6/site-packages/efficientnet_pytorch/utils.py in forward(self, x)
     91         if pad_h > 0 or pad_w > 0:
     92             x = F.pad(x, [pad_w//2, pad_w - pad_w//2, pad_h//2, pad_h - pad_h//2])
---> 93         return F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
     94
     95

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.cuda.HalfTensor) should be the same

In [18]:

learn.save('cutout-efficient')

In [19]:

# learn.unfreeze()
# learn.fit_one_cycle(8,slice(1e-6,1e-4))

In [20]:

preds,y = learn.get_preds(DatasetType.Test)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-20-dba8a276a455> in <module>()
----> 1 preds,y = learn.get_preds(DatasetType.Test)

/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in get_preds(self, ds_type, with_loss, n_batch, pbar)
    332         lf = self.loss_func if with_loss else None
    333         return get_preds(self.model, self.dl(ds_type), cb_handler=CallbackHandler(self.callbacks),
--> 334                          activ=_loss_func2activ(self.loss_func), loss_func=lf, n_batch=n_batch, pbar=pbar)
    335
    336     def pred_batch(self, ds_type:DatasetType=DatasetType.Valid, batch:Tuple=None, reconstruct:bool=False) -> List[Tensor]:

/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in get_preds(model, dl, pbar, cb_handler, activ, loss_func, n_batch)
     41     "Tuple of predictions and targets, and optional losses (if `loss_func`) using `dl`, max batches `n_batch`."
     42     res = [torch.cat(o).cpu() for o in
---> 43            zip(*validate(model, dl, cb_handler=cb_handler, pbar=pbar, average=False, n_batch=n_batch))]
     44     if loss_func is not None:
     45         with NoneReduceOnCPU(loss_func) as lf: res.append(lf(res[0], res[1]))

/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in validate(model, dl, loss_func, cb_handler, pbar, average, n_batch)
     56         for xb,yb in progress_bar(dl, parent=pbar, leave=(pbar is not None)):
     57             if cb_handler: xb, yb = cb_handler.on_batch_begin(xb, yb, train=False)
---> 58             val_loss = loss_batch(model, xb, yb, loss_func, cb_handler=cb_handler)
     59             val_losses.append(val_loss)
     60             if not is_listy(yb): yb = [yb]

/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler)
     23     if not is_listy(xb): xb = [xb]
     24     if not is_listy(yb): yb = [yb]
---> 25     out = model(*xb)
     26     out = cb_handler.on_loss_begin(out)
     27

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

/opt/conda/lib/python3.6/site-packages/efficientnet_pytorch/model.py in forward(self, inputs)
    167
    168         # Convolution layers
--> 169         x = self.extract_features(inputs)
    170
    171         # Head

/opt/conda/lib/python3.6/site-packages/efficientnet_pytorch/model.py in extract_features(self, inputs)
    152
    153         # Stem
--> 154         x = relu_fn(self._bn0(self._conv_stem(inputs)))
    155
    156         # Blocks

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

/opt/conda/lib/python3.6/site-packages/efficientnet_pytorch/utils.py in forward(self, x)
     91         if pad_h > 0 or pad_w > 0:
     92             x = F.pad(x, [pad_w//2, pad_w - pad_w//2, pad_h//2, pad_h - pad_h//2])
---> 93         return F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
     94
     95

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.cuda.HalfTensor) should be the same

In [21]:

results = torch.topk(preds,5)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-21-72b74067e8c1> in <module>()
----> 1 results = torch.topk(preds,5)

NameError: name 'preds' is not defined

In [22]:

out = []
for i in results[1].numpy():
    temp = ""
    for j in i:
        temp += (" "+str(data.classes[j]))
    out.append(temp)
# print(out)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-22-e55f5aed5bfc> in <module>()
      1 out = []
----> 2 for i in results[1].numpy():
      3     temp = ""
      4     for j in i:
      5         temp += (" "+str(data.classes[j]))

NameError: name 'results' is not defined

In [23]:

sam_sub_df = pd.read_csv('../input/kaggle_sample_submission.csv')
# sam_sub_df.head()
sam_sub_df["predicted"] = out
sam_sub_df.head()

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-fb66247ff237> in <module>()
      1 sam_sub_df = pd.read_csv('../input/kaggle_sample_submission.csv')
      2 # sam_sub_df.head()
----> 3 sam_sub_df["predicted"] = out
      4 sam_sub_df.head()

/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in __setitem__(self, key, value)
   3117         else:
   3118             # set column
-> 3119             self._set_item(key, value)
   3120
   3121     def _setitem_slice(self, key, value):

/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in _set_item(self, key, value)
   3192
   3193         self._ensure_valid_index(value)
-> 3194         value = self._sanitize_column(key, value)
   3195         NDFrame._set_item(self, key, value)
   3196

/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in _sanitize_column(self, key, value, broadcast)
   3389
   3390             # turn me into an ndarray
-> 3391             value = _sanitize_index(value, self.index, copy=False)
   3392             if not isinstance(value, (np.ndarray, Index)):
   3393                 if isinstance(value, list) and len(value) > 0:

/opt/conda/lib/python3.6/site-packages/pandas/core/series.py in _sanitize_index(data, index, copy)
   3999
   4000     if len(data) != len(index):
-> 4001         raise ValueError('Length of values does not match length of ' 'index')
   4002
   4003     if isinstance(data, ABCIndexClass) and not copy:

ValueError: Length of values does not match length of index

In [24]:

sam_sub_df.to_csv("submission.csv",index=False)

Keras Data Augmentation & Visualize

From: https://www.kaggle.com/hsinwenchang/keras-data-augmentation-visualize

Author: Beans

Score: 0.96392

This kernel is base on Alexander Teplyuk here I applied Data Augmentation technic from Udacity as following:

random 45 degree rotation

random zoom of up to 50%

random horizontal flip

width shift of 0.15

height shfit of 0.15

Model: vgg16
Apply transfer learning skill from pretrained model using Keras. Using vgg16 with a flatten layer followed by 2 fully connected layer with 1024 units and 1010 units. The output class probabilities based on 1010 classes which is done by the softmax activation function. Add a layer use a relu activation function. Add Dropout layers with a probability of 50%, where appropriate.

In [1]:

import os
import cv2
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import json
from keras.models import Sequential
from keras.layers import Dense, Flatten, Activation, Dropout, Conv2D,MaxPooling2D
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.applications import VGG16
from keras.applications import ResNet50
from keras import optimizers

Using TensorFlow backend.

Train data¶

In [2]:

ann_file = '../input/train2019.json'
with open(ann_file) as data_file:
        train_anns = json.load(data_file)

In [3]:

train_anns_df = pd.DataFrame(train_anns['annotations'])[['image_id','category_id']]
train_img_df = pd.DataFrame(train_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
df_train_file_cat = pd.merge(train_img_df, train_anns_df, on='image_id')
df_train_file_cat['category_id']=df_train_file_cat['category_id'].astype(str)
df_train_file_cat.head()

Out[3]:

| image_id | file_name | category_id
0 | 0 | train_val2019/Plants/400/d1322d13ccd856eb4236c... | 400
1 | 1 | train_val2019/Plants/570/15edbc1e2ef000d8ace48... | 570
2 | 2 | train_val2019/Reptiles/167/c87a32e8927cbf4f06d... | 167
3 | 3 | train_val2019/Birds/254/9fcdd1d37e96d8fd94dfdc... | 254
4 | 4 | train_val2019/Plants/739/ffa06f951e99de9d220ae... | 739

In [4]:

len(df_train_file_cat['category_id'].unique())

Out[4]:

1010

In [5]:

# Example of images for category_id = 400
img_names = df_train_file_cat[df_train_file_cat['category_id']=='400']['file_name'][:30]

plt.figure(figsize=[15,15])
i = 1
for img_name in img_names:
    img = cv2.imread("../input/train_val2019/%s" % img_name)[...,[2, 1, 0]]
    plt.subplot(6, 5, i)
    plt.imshow(img)
    i += 1
plt.show()

Validation data¶

In [6]:

valid_ann_file = '../input/val2019.json'
with open(valid_ann_file) as data_file:
        valid_anns = json.load(data_file)

In [7]:

valid_anns_df = pd.DataFrame(valid_anns['annotations'])[['image_id','category_id']]
valid_anns_df.head()

Out[7]:

| image_id | category_id
0 | 265213 | 644
1 | 265214 | 597
2 | 265215 | 883
3 | 265216 | 300
4 | 265217 | 881

In [8]:

valid_img_df = pd.DataFrame(valid_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
valid_img_df.head()

Out[8]:

| image_id | file_name
0 | 265213 | train_val2019/Plants/644/716a69838526f3ada3b2f...
1 | 265214 | train_val2019/Plants/597/0942cc64d2e759c5ee059...
2 | 265215 | train_val2019/Plants/883/acfdbfd9fa675f1c84558...
3 | 265216 | train_val2019/Birds/300/5f3194ff536c7dd31d80b7...
4 | 265217 | train_val2019/Plants/881/76acaf0b2841f91982d21...

In [9]:

df_valid_file_cat = pd.merge(valid_img_df, valid_anns_df, on='image_id')
df_valid_file_cat['category_id']=df_valid_file_cat['category_id'].astype(str)
df_valid_file_cat.head()

Out[9]:

| image_id | file_name | category_id
0 | 265213 | train_val2019/Plants/644/716a69838526f3ada3b2f... | 644
1 | 265214 | train_val2019/Plants/597/0942cc64d2e759c5ee059... | 597
2 | 265215 | train_val2019/Plants/883/acfdbfd9fa675f1c84558... | 883
3 | 265216 | train_val2019/Birds/300/5f3194ff536c7dd31d80b7... | 300
4 | 265217 | train_val2019/Plants/881/76acaf0b2841f91982d21... | 881

In [10]:

nb_classes = 1010
batch_size = 128
img_size = 128
nb_epochs = 10

In the cell below, use ImageDataGenerator to create a transformation that rescales the images by 255 here I applied Data Augmentation as following:

random 45 degree rotation

random zoom of up to 50%

random horizontal flip

width shift of 0.15

height shfit of 0.15

In [11]:

%%time
train_datagen=ImageDataGenerator(rescale=1./255, rotation_range=45,
                    width_shift_range=.15,
                    height_shift_range=.15,
                    horizontal_flip=True,
                    zoom_range=0.5)

train_generator=train_datagen.flow_from_dataframe(
    dataframe=df_train_file_cat,
    directory="../input/train_val2019",
    x_col="file_name",
    y_col="category_id",
    batch_size=batch_size,
    shuffle=True,
    class_mode="sparse",
    target_size=(img_size,img_size))

Found 265213 images belonging to 1010 classes.
CPU times: user 5.5 s, sys: 7.65 s, total: 13.2 s
Wall time: 1min 30s

In [12]:

# udacity_intro_to_tensorflow_for_deep_learning/l05c04_exercise_flowers_with_data_augmentation_solution.ipynb#scrollTo=jqb9OGoVKIOi
# This function will plot images in the form of a grid with 1 row and 5 columns where images are placed in each column.
def plotImages(images_arr):
    fig, axes = plt.subplots(1, 5, figsize=(20,20))
    axes = axes.flatten()
    for img, ax in zip( images_arr, axes):
        ax.imshow(img)
    plt.tight_layout()
    plt.show()

augmented_images = [train_generator[0][0][0] for i in range(5)]
plotImages(augmented_images)

In [13]:

%%time
test_datagen = ImageDataGenerator(rescale=1./255)

valid_generator=test_datagen.flow_from_dataframe(
    dataframe=df_valid_file_cat,
    directory="../input/train_val2019",
    x_col="file_name",
    y_col="category_id",
    batch_size=batch_size,
    class_mode="sparse",
    target_size=(img_size,img_size))

Found 3030 images belonging to 1010 classes.
CPU times: user 64 ms, sys: 80 ms, total: 144 ms
Wall time: 1.03 s

In [14]:

vgg16_net = VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(img_size, img_size, 3))
vgg16_net.trainable = False

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5
58892288/58889256 [==============================] - 1s 0us/step

In [15]:

resnet = ResNet50(include_top=False, weights='imagenet',
               input_shape=(img_size,img_size,3))
resnet.trainable = False

/opt/conda/lib/python3.6/site-packages/keras_applications/resnet50.py:265: UserWarning: The output shape of `ResNet50(include_top=False)` has been changed since Keras 2.2.0.
  warnings.warn('The output shape of `ResNet50(include_top=False)` '

Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
94658560/94653016 [==============================] - 6s 0us/step

Model¶

In [16]:

model = Sequential()
model.add(vgg16_net)
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes, activation='softmax'))

model.compile(optimizers.rmsprop(lr=0.0001, decay=1e-6),loss='sparse_categorical_crossentropy',metrics=['accuracy'])

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.

In [17]:

%%time
history = model.fit_generator(generator=train_generator,

                    steps_per_epoch=500,

                    validation_data=valid_generator,

                    validation_steps=100,

                    epochs=nb_epochs,
                    verbose=0)

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
CPU times: user 3h 42min 11s, sys: 6min 16s, total: 3h 48min 28s
Wall time: 4h 22min 20s

In [18]:

with open('history.json', 'w') as f:
    json.dump(history.history, f)

history_df = pd.DataFrame(history.history)
history_df[['loss', 'val_loss']].plot()
history_df[['acc', 'val_acc']].plot()

Out[18]:

<matplotlib.axes._subplots.AxesSubplot at 0x7f96f03ef828>

In [19]:

acc = history.history['acc']
val_acc = history.history['val_acc']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(nb_epochs)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

Test data¶

In [20]:

test_ann_file = '../input/test2019.json'
with open(test_ann_file) as data_file:
        test_anns = json.load(data_file)

In [21]:

test_img_df = pd.DataFrame(test_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
test_img_df.head()

Out[21]:

| image_id | file_name
0 | 268243 | test2019/e295f3c7046b1f1e80c0301401324aa9.jpg
1 | 268244 | test2019/ad3dcbb6846ed0b4dab58d7b1a4210ba.jpg
2 | 268245 | test2019/e697be8e296b4b140cff4f96f85c364f.jpg
3 | 268246 | test2019/7e7ba55e6aa26ba99e814d63b15d0121.jpg
4 | 268247 | test2019/6cb6372079d23702511c06923970f13f.jpg

In [22]:

%%time
test_datagen = ImageDataGenerator(rescale=1./255.)
test_generator = test_datagen.flow_from_dataframe(

        dataframe=test_img_df,

        directory = "../input/test2019",
        x_col="file_name",
        target_size = (img_size,img_size),
        batch_size = 1,
        shuffle=False,
        class_mode = None
        )

Found 35350 images.
CPU times: user 1.62 s, sys: 756 ms, total: 2.38 s
Wall time: 13.2 s

Prediction¶

In [23]:

%%time
test_generator.reset()
predict=model.predict_generator(test_generator, steps = len(test_generator.filenames),verbose=1)

10647/35350 [========>.....................] - ETA: 28:49

In [24]:

len(predict)

Out[24]:

35350

In [25]:

predicted_class_indices=np.argmax(predict,axis=1)

In [26]:

labels = (train_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
predictions = [labels[k] for k in predicted_class_indices]

In [27]:

sam_sub_df = pd.read_csv('../input/kaggle_sample_submission.csv')
sam_sub_df.head()

Out[27]:

| id | predicted
0 | 268243 | 842
1 | 268244 | 139
2 | 268245 | 988
3 | 268246 | 612
4 | 268247 | 468

In [28]:

filenames=test_generator.filenames
results=pd.DataFrame({"file_name":filenames,
                      "predicted":predictions})
df_res = pd.merge(test_img_df, results, on='file_name')[['image_id','predicted']]\
    .rename(columns={'image_id':'id'})

df_res.head()

Out[28]:

| id | predicted
0 | 268243 | 5
1 | 268244 | 326
2 | 268245 | 976
3 | 268246 | 263
4 | 268247 | 373

In [29]:

df_res.to_csv("submission.csv",index=False)

Hope you like it and finds this kernel helpful :)!¶

Reference¶

Alexander Teplyuk
Udacity
https://medium.com/@vijayabhaskar96/tutorial-on-keras-flow-from-dataframe-1fd4493d237c

Keras MobileNet Data Augmentation & Visualize

From: https://www.kaggle.com/hsinwenchang/keras-mobilenet-data-augmentation-visualize

Author: Beans

Score: 0.97509

This kernel is base on Alexander Teplyuk here I applied Data Augmentation technic from Udacity as following:

random 45 degree rotation

random zoom of up to 50%

random horizontal flip

width shift of 0.15

height shfit of 0.15

Model: MobileNet
Apply transfer learning skill from pretrained model using Keras. Using MobileNet.

In [1]:

import os
import cv2
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import json
from keras.models import Sequential
from keras.layers import Dense, Flatten, Activation, Dropout, Conv2D,MaxPooling2D
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.applications import MobileNet
from keras import optimizers

Using TensorFlow backend.

Train data¶

In [2]:

ann_file = '../input/train2019.json'
with open(ann_file) as data_file:
        train_anns = json.load(data_file)

In [3]:

train_anns_df = pd.DataFrame(train_anns['annotations'])[['image_id','category_id']]
train_img_df = pd.DataFrame(train_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
df_train_file_cat = pd.merge(train_img_df, train_anns_df, on='image_id')
df_train_file_cat['category_id']=df_train_file_cat['category_id'].astype(str)
df_train_file_cat.head()

Out[3]:

| image_id | file_name | category_id
0 | 0 | train_val2019/Plants/400/d1322d13ccd856eb4236c... | 400
1 | 1 | train_val2019/Plants/570/15edbc1e2ef000d8ace48... | 570
2 | 2 | train_val2019/Reptiles/167/c87a32e8927cbf4f06d... | 167
3 | 3 | train_val2019/Birds/254/9fcdd1d37e96d8fd94dfdc... | 254
4 | 4 | train_val2019/Plants/739/ffa06f951e99de9d220ae... | 739

In [4]:

len(df_train_file_cat['category_id'].unique())

Out[4]:

1010

In [5]:

# Example of images for category_id = 400
img_names = df_train_file_cat[df_train_file_cat['category_id']=='400']['file_name'][:30]

plt.figure(figsize=[15,15])
i = 1
for img_name in img_names:
    img = cv2.imread("../input/train_val2019/%s" % img_name)[...,[2, 1, 0]]
    plt.subplot(6, 5, i)
    plt.imshow(img)
    i += 1
plt.show()

Validation data¶

In [6]:

valid_ann_file = '../input/val2019.json'
with open(valid_ann_file) as data_file:
        valid_anns = json.load(data_file)

In [7]:

valid_anns_df = pd.DataFrame(valid_anns['annotations'])[['image_id','category_id']]
valid_anns_df.head()

Out[7]:

| image_id | category_id
0 | 265213 | 644
1 | 265214 | 597
2 | 265215 | 883
3 | 265216 | 300
4 | 265217 | 881

In [8]:

valid_img_df = pd.DataFrame(valid_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
valid_img_df.head()

Out[8]:

| image_id | file_name
0 | 265213 | train_val2019/Plants/644/716a69838526f3ada3b2f...
1 | 265214 | train_val2019/Plants/597/0942cc64d2e759c5ee059...
2 | 265215 | train_val2019/Plants/883/acfdbfd9fa675f1c84558...
3 | 265216 | train_val2019/Birds/300/5f3194ff536c7dd31d80b7...
4 | 265217 | train_val2019/Plants/881/76acaf0b2841f91982d21...

In [9]:

df_valid_file_cat = pd.merge(valid_img_df, valid_anns_df, on='image_id')
df_valid_file_cat['category_id']=df_valid_file_cat['category_id'].astype(str)
df_valid_file_cat.head()

Out[9]:

| image_id | file_name | category_id
0 | 265213 | train_val2019/Plants/644/716a69838526f3ada3b2f... | 644
1 | 265214 | train_val2019/Plants/597/0942cc64d2e759c5ee059... | 597
2 | 265215 | train_val2019/Plants/883/acfdbfd9fa675f1c84558... | 883
3 | 265216 | train_val2019/Birds/300/5f3194ff536c7dd31d80b7... | 300
4 | 265217 | train_val2019/Plants/881/76acaf0b2841f91982d21... | 881

In [10]:

nb_classes = 1010
batch_size = 128
img_size = 128
nb_epochs = 10

In the cell below, use ImageDataGenerator to create a transformation that rescales the images by 255 here I applied Data Augmentation as following:

random 45 degree rotation

random zoom of up to 50%

random horizontal flip

width shift of 0.15

height shfit of 0.15

In [11]:

%%time
train_datagen=ImageDataGenerator(rescale=1./255, rotation_range=45,
                    width_shift_range=.15,
                    height_shift_range=.15,
                    horizontal_flip=True,
                    zoom_range=0.5)

train_generator=train_datagen.flow_from_dataframe(
    dataframe=df_train_file_cat,
    directory="../input/train_val2019",
    x_col="file_name",
    y_col="category_id",
    batch_size=batch_size,
    shuffle=True,
    class_mode="sparse",
    target_size=(img_size,img_size))

Found 265213 images belonging to 1010 classes.
CPU times: user 5.7 s, sys: 8.46 s, total: 14.2 s
Wall time: 2min 36s

In [12]:

# udacity_intro_to_tensorflow_for_deep_learning/l05c04_exercise_flowers_with_data_augmentation_solution.ipynb#scrollTo=jqb9OGoVKIOi
# This function will plot images in the form of a grid with 1 row and 5 columns where images are placed in each column.
def plotImages(images_arr):
    fig, axes = plt.subplots(1, 5, figsize=(20,20))
    axes = axes.flatten()
    for img, ax in zip( images_arr, axes):
        ax.imshow(img)
    plt.tight_layout()
    plt.show()

augmented_images = [train_generator[0][0][0] for i in range(5)]
plotImages(augmented_images)

In [13]:

%%time
test_datagen = ImageDataGenerator(rescale=1./255)

valid_generator=test_datagen.flow_from_dataframe(
    dataframe=df_valid_file_cat,
    directory="../input/train_val2019",
    x_col="file_name",
    y_col="category_id",
    batch_size=batch_size,
    class_mode="sparse",
    target_size=(img_size,img_size))

Found 3030 images belonging to 1010 classes.
CPU times: user 68 ms, sys: 104 ms, total: 172 ms
Wall time: 1.61 s

In [14]:

model = MobileNet(input_shape=(img_size, img_size, 3), alpha=1., weights=None, classes=nb_classes)

model.compile(optimizers.rmsprop(lr=0.0001, decay=1e-6),loss='sparse_categorical_crossentropy',metrics=['accuracy'])
print(model.summary())

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         (None, 128, 128, 3)       0
_________________________________________________________________
conv1_pad (ZeroPadding2D)    (None, 129, 129, 3)       0
_________________________________________________________________
conv1 (Conv2D)               (None, 64, 64, 32)        864
_________________________________________________________________
conv1_bn (BatchNormalization (None, 64, 64, 32)        128
_________________________________________________________________
conv1_relu (ReLU)            (None, 64, 64, 32)        0
_________________________________________________________________
conv_dw_1 (DepthwiseConv2D)  (None, 64, 64, 32)        288
_________________________________________________________________
conv_dw_1_bn (BatchNormaliza (None, 64, 64, 32)        128
_________________________________________________________________
conv_dw_1_relu (ReLU)        (None, 64, 64, 32)        0
_________________________________________________________________
conv_pw_1 (Conv2D)           (None, 64, 64, 64)        2048
_________________________________________________________________
conv_pw_1_bn (BatchNormaliza (None, 64, 64, 64)        256
_________________________________________________________________
conv_pw_1_relu (ReLU)        (None, 64, 64, 64)        0
_________________________________________________________________
conv_pad_2 (ZeroPadding2D)   (None, 65, 65, 64)        0
_________________________________________________________________
conv_dw_2 (DepthwiseConv2D)  (None, 32, 32, 64)        576
_________________________________________________________________
conv_dw_2_bn (BatchNormaliza (None, 32, 32, 64)        256
_________________________________________________________________
conv_dw_2_relu (ReLU)        (None, 32, 32, 64)        0
_________________________________________________________________
conv_pw_2 (Conv2D)           (None, 32, 32, 128)       8192
_________________________________________________________________
conv_pw_2_bn (BatchNormaliza (None, 32, 32, 128)       512
_________________________________________________________________
conv_pw_2_relu (ReLU)        (None, 32, 32, 128)       0
_________________________________________________________________
conv_dw_3 (DepthwiseConv2D)  (None, 32, 32, 128)       1152
_________________________________________________________________
conv_dw_3_bn (BatchNormaliza (None, 32, 32, 128)       512
_________________________________________________________________
conv_dw_3_relu (ReLU)        (None, 32, 32, 128)       0
_________________________________________________________________
conv_pw_3 (Conv2D)           (None, 32, 32, 128)       16384
_________________________________________________________________
conv_pw_3_bn (BatchNormaliza (None, 32, 32, 128)       512
_________________________________________________________________
conv_pw_3_relu (ReLU)        (None, 32, 32, 128)       0
_________________________________________________________________
conv_pad_4 (ZeroPadding2D)   (None, 33, 33, 128)       0
_________________________________________________________________
conv_dw_4 (DepthwiseConv2D)  (None, 16, 16, 128)       1152
_________________________________________________________________
conv_dw_4_bn (BatchNormaliza (None, 16, 16, 128)       512
_________________________________________________________________
conv_dw_4_relu (ReLU)        (None, 16, 16, 128)       0
_________________________________________________________________
conv_pw_4 (Conv2D)           (None, 16, 16, 256)       32768
_________________________________________________________________
conv_pw_4_bn (BatchNormaliza (None, 16, 16, 256)       1024
_________________________________________________________________
conv_pw_4_relu (ReLU)        (None, 16, 16, 256)       0
_________________________________________________________________
conv_dw_5 (DepthwiseConv2D)  (None, 16, 16, 256)       2304
_________________________________________________________________
conv_dw_5_bn (BatchNormaliza (None, 16, 16, 256)       1024
_________________________________________________________________
conv_dw_5_relu (ReLU)        (None, 16, 16, 256)       0
_________________________________________________________________
conv_pw_5 (Conv2D)           (None, 16, 16, 256)       65536
_________________________________________________________________
conv_pw_5_bn (BatchNormaliza (None, 16, 16, 256)       1024
_________________________________________________________________
conv_pw_5_relu (ReLU)        (None, 16, 16, 256)       0
_________________________________________________________________
conv_pad_6 (ZeroPadding2D)   (None, 17, 17, 256)       0
_________________________________________________________________
conv_dw_6 (DepthwiseConv2D)  (None, 8, 8, 256)         2304
_________________________________________________________________
conv_dw_6_bn (BatchNormaliza (None, 8, 8, 256)         1024
_________________________________________________________________
conv_dw_6_relu (ReLU)        (None, 8, 8, 256)         0
_________________________________________________________________
conv_pw_6 (Conv2D)           (None, 8, 8, 512)         131072
_________________________________________________________________
conv_pw_6_bn (BatchNormaliza (None, 8, 8, 512)         2048
_________________________________________________________________
conv_pw_6_relu (ReLU)        (None, 8, 8, 512)         0
_________________________________________________________________
conv_dw_7 (DepthwiseConv2D)  (None, 8, 8, 512)         4608
_________________________________________________________________
conv_dw_7_bn (BatchNormaliza (None, 8, 8, 512)         2048
_________________________________________________________________
conv_dw_7_relu (ReLU)        (None, 8, 8, 512)         0
_________________________________________________________________
conv_pw_7 (Conv2D)           (None, 8, 8, 512)         262144
_________________________________________________________________
conv_pw_7_bn (BatchNormaliza (None, 8, 8, 512)         2048
_________________________________________________________________
conv_pw_7_relu (ReLU)        (None, 8, 8, 512)         0
_________________________________________________________________
conv_dw_8 (DepthwiseConv2D)  (None, 8, 8, 512)         4608
_________________________________________________________________
conv_dw_8_bn (BatchNormaliza (None, 8, 8, 512)         2048
_________________________________________________________________
conv_dw_8_relu (ReLU)        (None, 8, 8, 512)         0
_________________________________________________________________
conv_pw_8 (Conv2D)           (None, 8, 8, 512)         262144
_________________________________________________________________
conv_pw_8_bn (BatchNormaliza (None, 8, 8, 512)         2048
_________________________________________________________________
conv_pw_8_relu (ReLU)        (None, 8, 8, 512)         0
_________________________________________________________________
conv_dw_9 (DepthwiseConv2D)  (None, 8, 8, 512)         4608
_________________________________________________________________
conv_dw_9_bn (BatchNormaliza (None, 8, 8, 512)         2048
_________________________________________________________________
conv_dw_9_relu (ReLU)        (None, 8, 8, 512)         0
_________________________________________________________________
conv_pw_9 (Conv2D)           (None, 8, 8, 512)         262144
_________________________________________________________________
conv_pw_9_bn (BatchNormaliza (None, 8, 8, 512)         2048
_________________________________________________________________
conv_pw_9_relu (ReLU)        (None, 8, 8, 512)         0
_________________________________________________________________
conv_dw_10 (DepthwiseConv2D) (None, 8, 8, 512)         4608
_________________________________________________________________
conv_dw_10_bn (BatchNormaliz (None, 8, 8, 512)         2048
_________________________________________________________________
conv_dw_10_relu (ReLU)       (None, 8, 8, 512)         0
_________________________________________________________________
conv_pw_10 (Conv2D)          (None, 8, 8, 512)         262144
_________________________________________________________________
conv_pw_10_bn (BatchNormaliz (None, 8, 8, 512)         2048
_________________________________________________________________
conv_pw_10_relu (ReLU)       (None, 8, 8, 512)         0
_________________________________________________________________
conv_dw_11 (DepthwiseConv2D) (None, 8, 8, 512)         4608
_________________________________________________________________
conv_dw_11_bn (BatchNormaliz (None, 8, 8, 512)         2048
_________________________________________________________________
conv_dw_11_relu (ReLU)       (None, 8, 8, 512)         0
_________________________________________________________________
conv_pw_11 (Conv2D)          (None, 8, 8, 512)         262144
_________________________________________________________________
conv_pw_11_bn (BatchNormaliz (None, 8, 8, 512)         2048
_________________________________________________________________
conv_pw_11_relu (ReLU)       (None, 8, 8, 512)         0
_________________________________________________________________
conv_pad_12 (ZeroPadding2D)  (None, 9, 9, 512)         0
_________________________________________________________________
conv_dw_12 (DepthwiseConv2D) (None, 4, 4, 512)         4608
_________________________________________________________________
conv_dw_12_bn (BatchNormaliz (None, 4, 4, 512)         2048
_________________________________________________________________
conv_dw_12_relu (ReLU)       (None, 4, 4, 512)         0
_________________________________________________________________
conv_pw_12 (Conv2D)          (None, 4, 4, 1024)        524288
_________________________________________________________________
conv_pw_12_bn (BatchNormaliz (None, 4, 4, 1024)        4096
_________________________________________________________________
conv_pw_12_relu (ReLU)       (None, 4, 4, 1024)        0
_________________________________________________________________
conv_dw_13 (DepthwiseConv2D) (None, 4, 4, 1024)        9216
_________________________________________________________________
conv_dw_13_bn (BatchNormaliz (None, 4, 4, 1024)        4096
_________________________________________________________________
conv_dw_13_relu (ReLU)       (None, 4, 4, 1024)        0
_________________________________________________________________
conv_pw_13 (Conv2D)          (None, 4, 4, 1024)        1048576
_________________________________________________________________
conv_pw_13_bn (BatchNormaliz (None, 4, 4, 1024)        4096
_________________________________________________________________
conv_pw_13_relu (ReLU)       (None, 4, 4, 1024)        0
_________________________________________________________________
global_average_pooling2d_1 ( (None, 1024)              0
_________________________________________________________________
reshape_1 (Reshape)          (None, 1, 1, 1024)        0
_________________________________________________________________
dropout (Dropout)            (None, 1, 1, 1024)        0
_________________________________________________________________
conv_preds (Conv2D)          (None, 1, 1, 1010)        1035250
_________________________________________________________________
act_softmax (Activation)     (None, 1, 1, 1010)        0
_________________________________________________________________
reshape_2 (Reshape)          (None, 1010)              0
=================================================================
Total params: 4,264,114
Trainable params: 4,242,226
Non-trainable params: 21,888
_________________________________________________________________
None

In [15]:

%%time
history = model.fit_generator(generator=train_generator,

                    steps_per_epoch=500,

                    validation_data=valid_generator,

                    validation_steps=100,

                    epochs=nb_epochs,
                    verbose=0)

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_grad.py:102: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Deprecated in favor of operator or tf.math.divide.
CPU times: user 3h 45min 2s, sys: 13min 10s, total: 3h 58min 13s
Wall time: 4h 29min 17s

In [16]:

with open('history.json', 'w') as f:
    json.dump(history.history, f)

history_df = pd.DataFrame(history.history)
history_df[['loss', 'val_loss']].plot()
history_df[['acc', 'val_acc']].plot()

Out[16]:

<matplotlib.axes._subplots.AxesSubplot at 0x7fdbfb857a58>

In [17]:

acc = history.history['acc']
val_acc = history.history['val_acc']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(nb_epochs)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

Test data¶

In [18]:

test_ann_file = '../input/test2019.json'
with open(test_ann_file) as data_file:
        test_anns = json.load(data_file)

In [19]:

test_img_df = pd.DataFrame(test_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
test_img_df.head()

Out[19]:

| image_id | file_name
0 | 268243 | test2019/e295f3c7046b1f1e80c0301401324aa9.jpg
1 | 268244 | test2019/ad3dcbb6846ed0b4dab58d7b1a4210ba.jpg
2 | 268245 | test2019/e697be8e296b4b140cff4f96f85c364f.jpg
3 | 268246 | test2019/7e7ba55e6aa26ba99e814d63b15d0121.jpg
4 | 268247 | test2019/6cb6372079d23702511c06923970f13f.jpg

In [20]:

%%time
test_datagen = ImageDataGenerator(rescale=1./255.)
test_generator = test_datagen.flow_from_dataframe(

        dataframe=test_img_df,

        directory = "../input/test2019",
        x_col="file_name",
        target_size = (img_size,img_size),
        batch_size = 1,
        shuffle=False,
        class_mode = None
        )

Found 35350 images.
CPU times: user 2.2 s, sys: 572 ms, total: 2.77 s
Wall time: 15.3 s

Prediction¶

In [21]:

%%time
test_generator.reset()
predict=model.predict_generator(test_generator, steps = len(test_generator.filenames),verbose=1)

11178/35350 [========>.....................] - ETA: 27:53

In [22]:

len(predict)

Out[22]:

35350

In [23]:

predicted_class_indices=np.argmax(predict,axis=1)

In [24]:

labels = (train_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
predictions = [labels[k] for k in predicted_class_indices]

In [25]:

sam_sub_df = pd.read_csv('../input/kaggle_sample_submission.csv')
sam_sub_df.head()

Out[25]:

| id | predicted
0 | 268243 | 842
1 | 268244 | 139
2 | 268245 | 988
3 | 268246 | 612
4 | 268247 | 468

In [26]:

filenames=test_generator.filenames
results=pd.DataFrame({"file_name":filenames,
                      "predicted":predictions})
df_res = pd.merge(test_img_df, results, on='file_name')[['image_id','predicted']]\
    .rename(columns={'image_id':'id'})

df_res.head()

Out[26]:

| id | predicted
0 | 268243 | 523
1 | 268244 | 320
2 | 268245 | 577
3 | 268246 | 73
4 | 268247 | 738

In [27]:

df_res.to_csv("submission.csv",index=False)

Hope you like it and finds this kernel helpful :)!¶

Reference¶

Alexander Teplyuk
Udacity
https://medium.com/@vijayabhaskar96/tutorial-on-keras-flow-from-dataframe-1fd4493d237c

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])

CNN HyperNetwork

From: https://www.kaggle.com/jas10022/cnn-hypernetwork

Author: Jaskaran Bakshi

CNN HyperNetwork¶

The idea behind this CNN HyperNetwork is to take the data classification given in this competition for each species and create a set of "Upper Neural Networks". There will be 6 upper networks, each of which will predict the pictures class, family, genus, kingdom, order, phylum. Using this classification, there will then be a set of "Lower Neural Networks",where the predictions of the upper networks will feed into the group predicted by the network in order to specify the species in the group. Essentialy this method is taking a picture and making the classification simplier for the entire network to get a better accuracy.¶

This is a design layout of the HyperNetwork: Hyper Neural Network Design

This is how the HyperNetwork will traing on all these diffrent models: Theory of HyperNetwork

These are all the libraries that will be used throught this Kernel:

In [1]:

from __future__ import absolute_import, division, print_function
from skimage import transform
from skimage.color import rgb2gray
from PIL import Image
from resizeimage import resizeimage
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

import os
import pickle
import json
import tensorflow as tf
import numpy as np
import pandas as pd

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-8f0aba7aab2f> in <module>()
      3 from skimage.color import rgb2gray
      4 from PIL import Image
----> 5 from resizeimage import resizeimage
      6 from sklearn.model_selection import train_test_split
      7 from sklearn.preprocessing import LabelEncoder

ModuleNotFoundError: No module named 'resizeimage'

Step 1: Organize Data¶

The initial step is to take all the data that is currently given through this competition, in the JSON files, and organize all the data that has been given.¶

First, taking the values of all the Json files and loading the important parts of the files into Data Frames to be used to create Data Set with all the species classification. Then each column in this data set is encoded to be used for training part of the Upper Network.

In [2]:

# open open category to id file
with open('val2019.json') as f:
    values = json.load(f)

#open id to images file
with open('train2019.json') as f:
    images = json.load(f)

annotation_images = pd.DataFrame.from_dict(images["annotations"])
catagory_images = pd.DataFrame.from_dict(images["categories"])
images_id = pd.DataFrame.from_dict(images["images"])

train_data = pd.DataFrame({'ImageId':None,'Class':None,'Family':None,'Genus':None,'Kingdom':None,'Name':None,'Order':None,'Phylum':None}, index = [0])

scaled_features = catagory_images.copy()
col_names = ['class', 'family','kingdom','order','phylum']
features = catagory_images[col_names]
for i in range(0,7):
    labelencoder = LabelEncoder()
    features.values[:,i] = labelencoder.fit_transform(features.values[:,i])

scaled_features[col_names] = features

index = 0;
for t in annotation_images.category_id:
    current_info = pd.DataFrame(scaled_features.loc[t]).T

    train_data.loc[index] = [index, current_info["class"].values[0], current_info["family"].values[0], current_info["genus"].values[0], current_info["kingdom"].values[0], current_info["name"].values[0], current_info["order"].values[0], current_info["phylum"].values[0]]
    print(index)
    index += 1

train_data = train_data.assign(File_Name=images_id['file_name'].values)

train_data.to_csv(r'/upperNN.csv', index=False)

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-2-3fa2ef4b7b50> in <module>()
      1 # open open category to id file
----> 2 with open('val2019.json') as f:
      3     values = json.load(f)
      4
      5 #open id to images file

FileNotFoundError: [Errno 2] No such file or directory: 'val2019.json'

Next, the Lower training set is created to have the data sorted by per speices in order to feed the Lower Network the training data.

In [3]:

lower_train_data = pd.DataFrame({'FileName': images_id['file_name'],'ImageId': annotation_images['id'], 'CatagoryID':annotation_images['category_id']})

lower_train_data = lower_train_data.sort_values(by=['CatagoryID'])

lower_train_data.set_index(keys=['CatagoryID'], drop=False,inplace=True)

# get a list of names
numbers=lower_train_data['CatagoryID'].unique().tolist()

for number in numbers:
    data = lower_train_data.loc[lower_train_data['CatagoryID']==number]

    data.to_csv( str(number) + '.csv', index=False)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-f1388ebfbf1a> in <module>()
----> 1 lower_train_data = pd.DataFrame({'FileName': images_id['file_name'],'ImageId': annotation_images['id'], 'CatagoryID':annotation_images['category_id']})
      2
      3 lower_train_data = lower_train_data.sort_values(by=['CatagoryID'])
      4
      5 lower_train_data.set_index(keys=['CatagoryID'], drop=False,inplace=True)

NameError: name 'pd' is not defined

Last, in order to create all of the lower models there needs to be grouping for all the species in the data set based on the Class, Family, Genus, Kingdom, Order, Phylum. Each of these groupings will create an individual model for the overall Hypernetwork.

In [4]:

# now we can perform a lookup on a 'view' of the dataframe

ordered_features = scaled_features.sort_values(['class', 'family', 'genus', 'kingdom', 'order', 'phylum'])

features = ['class', 'family', 'genus', 'kingdom', 'order', 'phylum']

classt = scaled_features['class'].unique().tolist()
family = scaled_features['family'].unique().tolist()
genus = scaled_features['genus'].unique().tolist()
kingdom = scaled_features['kingdom'].unique().tolist()
order = scaled_features['order'].unique().tolist()
phylum = scaled_features['phylum'].unique().tolist()

for a in kingdom:
    for b in phylum:
        for c in classt:
            for d in order:
                for e in family:
                        current = ordered_features['id'].loc[(ordered_features['kingdom'] == a) & (ordered_features['phylum'] == b) & (ordered_features['class'] == c) & (ordered_features['order'] == d) & (ordered_features['family'] == e)]
                        if len(current) != 0:
                            print(len(current))
                            current.to_csv(str(a) + '_' + str(b) + '_' + str(c) + '_' + str(d) + '_' + str(e) + '.csv', index=False)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-aad405a3f59b> in <module>()
      1 # now we can perform a lookup on a 'view' of the dataframe
      2
----> 3 ordered_features = scaled_features.sort_values(['class', 'family', 'genus', 'kingdom', 'order', 'phylum'])
      4
      5 features = ['class', 'family', 'genus', 'kingdom', 'order', 'phylum']

NameError: name 'scaled_features' is not defined

Step 2: Converting Images¶

The next step in the Hypernetwork is to convert all the Images given into a format that can be used to train the models in pixel values¶

The code below takes all the files Names through the upperNN.csv file and resizing the image to 75 x 75 and grayscaling the images. Because the data is so large required the data to be split into 11 diffrent files because of how large this was.

In [5]:

#this is how we can create a data file with all the images in a 1,75,75 shape
#modify the for loop in order to use it and the allImages are all the images file names
#update the .open method to the directory of the train images then the + im
# the Images variable will contain all the picures each resized to 75 by 75

upperNN = pd.read_csv('upperNN .csv')

file_names = upperNN['File_Name']

Images = np.empty([1,75, 75])
i = 0
a = 0
for im in file_names:
        if i >= 5000:
            Images = Images[1:]
            output = open(str(a)+'.pkl', 'wb')
            pickle.dump(Images, output)
            output.close()
            Images = np.empty([1,75, 75])
            a += 1
            i = 0
        img = Image.open(im, 'r').convert('LA')
        cover = resizeimage.resize_cover(img, [75, 75], validate=False)
        np_im = np.array(cover)

        pix_val_flat = np.array([x for sets in np_im for x in sets])
        train_data = pix_val_flat[:,0].astype('float64') /255
        train_data = np.resize(train_data, (1,75,75))

        Images = np.concatenate((Images,train_data))
        i += 1
        print(i)

Images = Images[1:]
output = open(str(a)+'.pkl', 'wb')
pickle.dump(Images, output)
output.close()

Images = np.empty([1,75, 75])
i = 0
a = 54

for num in range(54,65):
    if i == 5:
        Images = Images[1:]
        output = open(str(a)+'.pkl', 'wb')
        pickle.dump(Images, output)
        output.close()
        Images = np.empty([1,75, 75])
        i = 0
        a += 1
    pkl_file = open(str(num) + '.pkl', 'rb')

    data1 = pickle.load(pkl_file)

    Images = np.concatenate((Images,data1))

    pkl_file.close()
    print(i)
    i += 1
Images = Images[1:]
output = open(str(a)+'.pkl', 'wb')
pickle.dump(Images, output)
output.close()

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-fa4da10e17ad> in <module>()
      4 # the Images variable will contain all the picures each resized to 75 by 75
      5
----> 6 upperNN = pd.read_csv('upperNN .csv')
      7
      8 file_names = upperNN['File_Name']

NameError: name 'pd' is not defined

Next, After all the image data has been converted to pixel values, I sorted all the pictures based on their picture ID for the Lower Network Train data as each picture will be sorted by the specie ID.

In [6]:

Images = np.empty([1,75, 75])
i = 0
for num in range(0,1010):
    current = pd.read_csv('Lower_NN_Data/' + str(num) + '.csv')
    for id in current['ImageId']:
        file_num = int(id / 25000)
        index = id - (file_num * 25000)
        if file_num == 0:
            im = data1[index].reshape(1,75,75)
            Images = np.concatenate((Images,im))
        if file_num == 1:
            im = data2[index].reshape(1,75,75)
            Images = np.concatenate((Images,im))
        if file_num == 2:
            im = data3[index].reshape(1,75,75)
            Images = np.concatenate((Images,im))
        if file_num == 3:
            im = data4[index].reshape(1,75,75)
            Images = np.concatenate((Images,im))
        if file_num == 4:
            im = data5[index].reshape(1,75,75)
            Images = np.concatenate((Images,im))
        if file_num == 5:
            im = data6[index].reshape(1,75,75)
            Images = np.concatenate((Images,im))
        if file_num == 6:
            im = data7[index].reshape(1,75,75)
            Images = np.concatenate((Images,im))
        if file_num == 7:
            im = data8[index].reshape(1,75,75)
            Images = np.concatenate((Images,im))
        if file_num == 8:
            im = data9[index].reshape(1,75,75)
            Images = np.concatenate((Images,im))
        if file_num == 9:
            im = data10[index].reshape(1,75,75)
            Images = np.concatenate((Images,im))
        if file_num == 10:
            im = data11[index].reshape(1,75,75)
            Images = np.concatenate((Images,im))
    Images = Images[1:]
    output = open(str(i) + '.pkl', 'wb')
    pickle.dump(Images, output)
    output.close()
    Images = np.empty([1,75, 75])
    print(i)
    i += 1

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-d3e1c3ec28cd> in <module>()
----> 1 Images = np.empty([1,75, 75])
      2 i = 0
      3 for num in range(0,1010):
      4     current = pd.read_csv('Lower_NN_Data/' + str(num) + '.csv')
      5     for id in current['ImageId']:

NameError: name 'np' is not defined

Step 3: Tensorflow Models¶

This part of the proccess is the Tensorflow models for the lower and the upper network models¶

The way this code is written is so that creating a model is automated by calling on the CNN function with the arguments of train_data, train_val, eval_data, eval_labels, output_nodes_number, model_name, and model_type (lower or upper). After this function finishes training on 2000 steps it will save the model in the local directory with the model_name as its title.

In [7]:

tf.logging.set_verbosity(tf.logging.INFO)

def CNN(train_data, train_labels, eval_data, eval_labels, output_nodes_number, model_name, model_type):

    if model_type == "lower":
        #tensorflow model function for lowerNN
        def cnn_model(features, labels, mode):

            input_layer = tf.reshape(features["x"], [-1, 75, 75,1])

            # Convolutional Layer #1
            conv1 = tf.layers.conv2d(
                  inputs=input_layer,
                  filters=32,
                  kernel_size=[5, 5],
                  padding="same",
                  activation=tf.nn.relu)

              # Pooling Layer #1
            pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)

              # Convolutional Layer #2 and Pooling Layer #2
            conv2 = tf.layers.conv2d(
                  inputs=pool1,
                  filters=64,
                  kernel_size=[5, 5],
                  padding="same",
                  activation=tf.nn.relu)
            pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)

              # Dense Layer
            pool2_flat = tf.reshape(pool2, [-1,  18 * 18 * 64])
            dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
            dropout = tf.layers.dropout(
                  inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)

              # Logits Layer
            logits = tf.layers.dense(inputs=dropout, units=output_nodes_number)

            predicted_classes =tf.argmax(input=logits, axis=1)
            predictions = {
                        'class_ids': predicted_classes[:, tf.newaxis],
                        'probabilities': tf.nn.softmax(logits, name="softmax_tensor"),
                        'logits': logits,
                    }
            export_outputs = {
              'prediction': tf.estimator.export.PredictOutput(predictions)
              }
            if mode == tf.estimator.ModeKeys.PREDICT:
                return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, export_outputs=export_outputs)

              # Calculate Loss (for both TRAIN and EVAL modes)
            loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
              # Configure the Training Op (for TRAIN mode)
            if mode == tf.estimator.ModeKeys.TRAIN:
                optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.0001)
                train_op = optimizer.minimize(
                    loss=loss,
                    global_step=tf.train.get_global_step())
                return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)

              # Add evaluation metrics (for EVAL mode)
            eval_metric_ops = {
                  "accuracy": tf.metrics.accuracy(
                      labels=labels, predictions=predictions["class_ids"])
            }
            return tf.estimator.EstimatorSpec(
                  mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)

    if model_type == "upper":
        #tensorflow model function for upperNN
        def cnn_model(features, labels, mode):

            input_layer = tf.reshape(features["x"], [-1, 75, 75, 1])

            # Convolutional Layer #1
            conv1 = tf.layers.conv2d(
                  inputs=input_layer,
                  filters=32,
                  kernel_size=[5, 5],
                  padding="same",
                  activation=tf.nn.relu)

              # Pooling Layer #1
            pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)

              # Convolutional Layer #2 and Pooling Layer #2
            conv2 = tf.layers.conv2d(
                  inputs=pool1,
                  filters=64,
                  kernel_size=[5, 5],
                  padding="same",
                  activation=tf.nn.relu)
            pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)

              # Dense Layer
            pool2_flat = tf.reshape(pool2, [-1, 18 * 18 * 64])
            dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
            dropout = tf.layers.dropout(
                  inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)

              # Logits Layer
            logits = tf.layers.dense(inputs=dropout, units=output_nodes_number)

            predicted_classes =tf.argmax(input=logits, axis=1)
            predictions = {
                        'class_ids': predicted_classes[:, tf.newaxis],
                        'probabilities': tf.nn.softmax(logits, name="softmax_tensor"),
                        'logits': logits,
                    }
            export_outputs = {
              'prediction': tf.estimator.export.PredictOutput(predictions)
              }
            if mode == tf.estimator.ModeKeys.PREDICT:
                return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, export_outputs=export_outputs)

              # Calculate Loss (for both TRAIN and EVAL modes)
            loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)

              # Configure the Training Op (for TRAIN mode)
            if mode == tf.estimator.ModeKeys.TRAIN:
                optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.0001)
                train_op = optimizer.minimize(
                    loss=loss,
                    global_step=tf.train.get_global_step())
                return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)

              # Add evaluation metrics (for EVAL mode)
            eval_metric_ops = {
                  "accuracy": tf.metrics.accuracy(
                      labels=labels, predictions=predictions["class_ids"])
            }
            return tf.estimator.EstimatorSpec(
                  mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
    #This is where we need to load up the data for each group

    ModelDir = model_name
    # Create the Estimator

    run_config = tf.contrib.learn.RunConfig(
    model_dir=ModelDir,
    keep_checkpoint_max=1)

    cnn_classifier = tf.estimator.Estimator(
        model_fn=cnn_model, config=run_config)

    # Set up logging for predictions
    tensors_to_log = {"probabilities": "softmax_tensor"}

    logging_hook = tf.train.LoggingTensorHook(
        tensors=tensors_to_log, every_n_iter=50)

    # Train the model
    train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": train_data},
        y=train_labels,
        batch_size=100,
        num_epochs=None,
        shuffle=True)

    #change steps to 20000
    cnn_classifier.train(input_fn=train_input_fn, steps=2000, hooks=[logging_hook])

    # Evaluation of the neural network
    eval_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": eval_data},
        y=eval_labels,
        num_epochs=1,
        shuffle=False)

    eval_results = cnn_classifier.evaluate(input_fn=eval_input_fn)
    print(eval_results)

        #instead of predicting on a test data set we will save the model
    #model_dir = cnn_classifier.export_savedmodel(
       # model_name,
        #serving_input_receiver_fn=serving_input_receiver_fn)

    return  ModelDir

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-ce1116b866e1> in <module>()
----> 1 tf.logging.set_verbosity(tf.logging.INFO)
      2
      3 def CNN(train_data, train_labels, eval_data, eval_labels, output_nodes_number, model_name, model_type):
      4
      5     if model_type == "lower":

NameError: name 'tf' is not defined

In [8]:

def cnn_model_test(features, labels, mode):

            input_layer = tf.reshape(features["x"], [-1, 75, 75,1])

            # Convolutional Layer #1
            conv1 = tf.layers.conv2d(
                  inputs=input_layer,
                  filters=32,
                  kernel_size=[5, 5],
                  padding="same",
                  activation=tf.nn.relu)

              # Pooling Layer #1
            pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)

              # Convolutional Layer #2 and Pooling Layer #2
            conv2 = tf.layers.conv2d(
                  inputs=pool1,
                  filters=64,
                  kernel_size=[5, 5],
                  padding="same",
                  activation=tf.nn.relu)
            pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)

              # Dense Layer
            pool2_flat = tf.reshape(pool2, [-1,  18 * 18 * 64])
            dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
            dropout = tf.layers.dropout(
                  inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)

              # Logits Layer
            logits = tf.layers.dense(inputs=dropout, units=1)

            predicted_classes =tf.argmax(input=logits, axis=1)
            predictions = {
                        'class_ids': predicted_classes[:, tf.newaxis],
                        'probabilities': tf.nn.softmax(logits, name="softmax_tensor"),
                        'logits': logits,
                    }
            export_outputs = {
              'prediction': tf.estimator.export.PredictOutput(predictions)
              }
            if mode == tf.estimator.ModeKeys.PREDICT:
                return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, export_outputs=export_outputs)

              # Calculate Loss (for both TRAIN and EVAL modes)
            loss = tf.losses.sigmoid_cross_entropy(multi_class_labels=labels, logits = logits)

              # Configure the Training Op (for TRAIN mode)
            if mode == tf.estimator.ModeKeys.TRAIN:
                optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.0001)
                train_op = optimizer.minimize(
                    loss=loss,
                    global_step=tf.train.get_global_step())
                return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)

              # Add evaluation metrics (for EVAL mode)
            eval_metric_ops = {
                  "accuracy": tf.metrics.accuracy(
                      labels=labels, predictions=predictions["class_ids"])
            }
            return tf.estimator.EstimatorSpec(
                  mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)

Step 4: Training Model¶

This step is required to take all the sorted data and being able to train the models on the train data and labels data in order to create this hypernetwork.¶

The code below is written as an automation to create all the Upper Neural Network. A model is created for every one of the col_names and is trained and evaluated on all the training data provided.

In [9]:

#just a start on how to automate creating all the models for the upper NN

#creating a basic model to start the model training
# this code chunk will auto run and create a model trained on all the data for the species classification
# this is all automated and may take a long time to create al 5 models as it has to go through
#25000 images 11 times as the data is split up inot 11 files for each model
# bellow is the code for Upper NN to give to ur dad to run
col_names = ['Class', 'Family','Kingdom','Order','Phylum']
#, 'Family','Kingdom','Order','Phylum'
a = 0
b = 1
output_nodes = 0
for cat in col_names:

    pkl_file = open('Data/UpperNN_data/64.pkl', 'rb')
    data1 = pickle.load(pkl_file)

    upperNN = pd.read_csv('Data/upperNN.csv')
    Labels = upperNN[cat][249999:265214]
    output_nodes = upperNN[cat].unique().size

    #this splits the data into training and val data for the model and also reshapes the label data
    X_train, X_val, y_train, y_val = train_test_split(data1, Labels, test_size = 0.05, random_state = 0)
    y_train = np.asarray(y_train).reshape((-1,1))
    y_val = np.asarray(y_val).reshape((-1,1))

    model_location = CNN(X_train,y_train,X_val,y_val,output_nodes, str(cat), "upper")

    ##need prediction values

    for i in range(54,64):

        pkl_file = open('Data/UpperNN_data/' + str(i) + '.pkl', 'rb')
        data1 = pickle.load(pkl_file)

        upperNN = pd.read_csv('Data/upperNN.csv')
        Labels = upperNN[cat][a * 25000:b * 25000]
        a += 1
        b += 1

        #this splits the data into training and val data for the model and also reshapes the label data
        X_train, X_val, y_train, y_val = train_test_split(data1, Labels, test_size = 0.05, random_state = 0)
        y_train = np.asarray(y_train).reshape((-1,1))
        y_val = np.asarray(y_val).reshape((-1,1))

        #this session will open up any saved model created in directory and will run prediction on that
        # you can also train with it using the training lines
        with tf.Session() as sess:
          # Restore variables from disk.
            currentCheckpoint = tf.train.latest_checkpoint(cat)
            saver = tf.train.import_meta_graph(currentCheckpoint + ".meta")
            saver.restore(sess, currentCheckpoint)
            print("Model restored.")

            sunspot_classifier = tf.estimator.Estimator(
            model_fn=cnn_model_test, model_dir=cat)

                # Set up logging for predictions
                # Log the values in the "Softmax" tensor with label "probabilities"
            tensors_to_log = {"probabilities": "softmax_tensor"}
            logging_hook = tf.train.LoggingTensorHook(
                tensors=tensors_to_log, every_n_iter=50)

                  ## train here
            train_input_fn = tf.estimator.inputs.numpy_input_fn(
                           x={"x": X_train},
                           y=y_train,
                           batch_size=100,
                           num_epochs=None,
                           shuffle=True)

                #change steps to 20000
            sunspot_classifier.train(input_fn=train_input_fn, steps=2000)

                # Evaluation of the neural network
            eval_input_fn = tf.estimator.inputs.numpy_input_fn(
                    x={"x": X_val},
                    y=y_val,
                    num_epochs=1,
                    shuffle=False)

            eval_results = sunspot_classifier.evaluate(input_fn=eval_input_fn)
            print(eval_results)
    if b == 11:
        break

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-9-305bd9c3b64e> in <module>()
     13 for cat in col_names:
     14
---> 15     pkl_file = open('Data/UpperNN_data/64.pkl', 'rb')
     16     data1 = pickle.load(pkl_file)
     17

FileNotFoundError: [Errno 2] No such file or directory: 'Data/UpperNN_data/64.pkl'

Next, the code below is written to automatically train a model based on all the species grouping in the Sorted_species file, creating a set of training and eval data for the model to be created and saved in the current directory to be used for predicitons.

In [10]:

#this will be the lower NN code to run
#this code is very basic where it will look into the Data/Sorted_species files one by one as these
# files contains the groups for all the species and it will train one model for each of the groupings
# once read it will look at all the species ids and then look into the Data/Train_data file to get all
# the picture data and store itinto train_data for each species
# the code will also take the labels from the Data/Lower_NN_Data files to create the Labels for the training
# then after the code splits the data created into training and validation data to train the model
output_nodes_number = 0

for filename in os.listdir("Data/Sorted_species"):
    if filename.endswith(".csv"):
        data = pd.read_csv("Data/Sorted_species/" + filename, names=['a'])
        output_nodes_number = data.size
        name = os.path.splitext(filename)[0]
        train_data = np.empty([1,75, 75])
        Labels = np.empty([1])
        for species in data['a']:

            pkl_file = open("Data/Train_data/" + str(species) + '.pkl', 'rb')
            data1 = pickle.load(pkl_file)
            train_data = np.concatenate((train_data,data1))
            pkl_file.close()

            current_labels = pd.read_csv("Data/Lower_NN_Data/" + str(species) + '.csv')["CatagoryID"]
            Labels = np.concatenate((Labels,current_labels.values))

        Labels = Labels[1:]
        train_data = train_data[1:]

        labelencoder = LabelEncoder()
        Labels[0:] = labelencoder.fit_transform(Labels[0:])

        X_train, X_val, y_train, y_val = train_test_split(train_data, Labels, test_size = 0.20, random_state = 0)
        y_train = np.asarray(y_train).astype('int32').reshape((-1,1))
        y_val = np.asarray(y_val).astype('int32').reshape((-1,1))

        model_location = CNN(X_train,y_train,X_val,y_val,output_nodes_number, name, "lower")

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-5384bdb3081e> in <module>()
      8 output_nodes_number = 0
      9
---> 10 for filename in os.listdir("Data/Sorted_species"):
     11     if filename.endswith(".csv"):
     12         data = pd.read_csv("Data/Sorted_species/" + filename, names=['a'])

NameError: name 'os' is not defined

Part 5: Predictions¶

Now that all the models have been created the last step is to predict all the test data using all of these models¶

First, convert all the image files to the same style of the Test data.

In [11]:

# open open category to id file
with open('test2019.json') as f:
    test = json.load(f)

Test_Data = pd.DataFrame.from_dict(test["file_name","id"])

file_names = Test_data['file_name']

Images = np.empty([1,75, 75])
i = 0
for im in file_names:
        img = Image.open(im, 'r').convert('LA')
        cover = resizeimage.resize_cover(img, [75, 75], validate=False)
        np_im = np.array(cover)

        pix_val_flat = np.array([x for sets in np_im for x in sets])
        train_data = pix_val_flat[:,0].astype('float64') /255
        train_data = np.resize(train_data, (1,75,75))

        Images = np.concatenate((Images,train_data))
        i += 1
        print(i)

TestData = Images[1:]

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-11-284fc367f10e> in <module>()
      1 # open open category to id file
----> 2 with open('test2019.json') as f:
      3     test = json.load(f)
      4
      5 Test_Data = pd.DataFrame.from_dict(test["file_name","id"])

FileNotFoundError: [Errno 2] No such file or directory: 'test2019.json'

In [12]:

col_names = ['Class', 'Family','Kingdom','Order','Phylum']
pred = np.empty([1])

with tf.Session() as sess:
    test_catagory = pd.DataFrame({'Class':None,'Family':None,'Kingdom':None,'Order':None,'Phylum':None}, index = [0])

    for cat in col_names:
        # Restore variables from disk.
        currentCheckpoint = tf.train.latest_checkpoint(cat)
        saver = tf.train.import_meta_graph(currentCheckpoint + ".meta")
        saver.restore(sess, currentCheckpoint)
        print("Model restored.")
        sunspot_classifier = tf.estimator.Estimator(
            model_fn=cnn_model_test, model_dir=cat)
        # predict with the model and print results
        pred_input_fn = tf.estimator.inputs.numpy_input_fn(
                x={"x": TestData},shuffle=False)
        pred_results = sunspot_classifier.predict(input_fn=pred_input_fn)

        pred_val = np.array([p['class_ids'] for p in pred_results]).squeeze()

        test_catagory[cat] = pred_val

    test_catagory = test_catagory[1:]

    for pred_val in range(0,len(test_catagory.index)):
        # Restore variables from disk.
        currentCheckpoint = tf.train.latest_checkpoint(test_catagory['Kingdom'][pred_val] + '_' + test_catagory['Phylum'][pred_val]
                                                       + '_' + test_catagory['Class'][pred_val] + '_' + test_catagory['Order'][pred_val] + '_'
                                                       + test_catagory['Family'][pred_val] + '.csv')
        saver = tf.train.import_meta_graph(currentCheckpoint + ".meta")
        saver.restore(sess, currentCheckpoint)
        print("Model restored.")
        sunspot_classifier = tf.estimator.Estimator(
            model_fn=cnn_model_test, model_dir=cat)
        # predict with the model and print results
        pred_input_fn = tf.estimator.inputs.numpy_input_fn(
                x={"x": TestData[pred_val]},shuffle=False)
        pred_results = sunspot_classifier.predict(input_fn=pred_input_fn)

        pred_value = np.array([p['class_ids'] for p in pred_results]).squeeze()

        np.concatenate((pred,pred_value))

pred = pred[1:]

output1 = pd.DataFrame({'Id':Test_Data['id'],'Prediction':pred})

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-d2820d1d3361> in <module>()
      1 col_names = ['Class', 'Family','Kingdom','Order','Phylum']
----> 2 pred = np.empty([1])
      3
      4 with tf.Session() as sess:
      5     test_catagory = pd.DataFrame({'Class':None,'Family':None,'Kingdom':None,'Order':None,'Phylum':None}, index = [0])

NameError: name 'np' is not defined

iNaturalist 2019 at FGVC6

From: https://www.kaggle.com/s3chwartz/inaturalist-2019-at-fgvc6

Author: singing man

Score: 0.86491

In [1]:

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import os
print(os.listdir("../input"))

['test2019.json', 'train_val2019', 'kaggle_sample_submission.csv', 'train2019.json', 'val2019.json', 'test2019']

In [2]:

    from fastai.vision import *
    from fastai.metrics import *
    PATH = Path("../input")

In [3]:

ann_file = "../input/train2019.json"

with open(ann_file) as data_file:
    train_anns = json.load(data_file)

train_anns_df = pd.DataFrame(train_anns["annotations"])[["id", "category_id"]]
train_img_df = pd.DataFrame(train_anns["images"])[["id", "file_name"]]

In [4]:

df_train = pd.merge(train_img_df, train_anns_df, on = "id")
df_train.drop(["id"], axis = 1, inplace = True)

In [5]:

sample_to = df_train.category_id.value_counts().max()
res = None

for grp in df_train.groupby("category_id"):
    n = grp[1].shape[0]
    additional_rows = grp[1].sample(0 if sample_to < n else sample_to - n, replace=True)
    rows = pd.concat((grp[1], additional_rows))
    res = pd.concat((res, rows))

In [6]:

test_ann_file = PATH/"test2019.json"

with open(test_ann_file) as data_file:
    test_anns = json.load(data_file)

test_img_df = pd.DataFrame(test_anns["images"])[["file_name", "id"]]
#test_img_df.head()

In [7]:

df_train_sub = df_train[:10000]
#print(df_train_sub)
#print(df_train_sub.shape)
res_sub = res[:10000]
#res_sub.head()
test_img_df_sub = test_img_df[:1000]

In [8]:

src = (ImageList.from_df(df=res, path=PATH/"train_val2019", cols = 0)
    .use_partial_data(0.2)
    .split_by_rand_pct(0.1)
    .label_from_df("category_id")
    .add_test(ImageList.from_df(df=test_img_df, path=PATH/"test2019", cols = 0))
    )

In [9]:

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

In [10]:

data.classes
data.show_batch(rows=3, figsize=(7,6))

In [11]:

#learn = cnn_learner(data, models.resnet34, metrics = accuracy, model_dir="/tmp/model/")
#learn.save("StaticWeights_resnet34_v1")
#learn.lr_find()
#learn.recorder.plot()
#learn.unfreeze()
#learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-1))
#learn.save("FittedWeights_resnet34_v1")
#interp = ClassificationInterpretation.from_learner(learn)
#losses,idxs = interp.top_losses()
#interp.plot_top_losses(9, figsize=(15,11))
#interp.plot_confusion_matrix(figsize=(12,12), dpi=60)
#interp.most_confused(min_val=2)

In [12]:

import re
for grp in res_sub.groupby("category_id"):
    n = grp[1].iloc[0,0]
    n = re.search("(?<=/)(.*)(?=/.*/)",n).group(0)
    cat = grp[1].iloc[0,1]
    print("Type {} has label {}".format(n, cat))

Type Fungi has label 0
Type Fungi has label 1
Type Fungi has label 2
Type Fungi has label 3
Type Fungi has label 4
Type Fungi has label 5
Type Fungi has label 6
Type Fungi has label 7
Type Fungi has label 8
Type Fungi has label 9
Type Fungi has label 10
Type Fungi has label 11
Type Insects has label 12
Type Insects has label 13
Type Insects has label 14
Type Insects has label 15
Type Insects has label 16
Type Insects has label 17
Type Insects has label 18
Type Insects has label 19

In [13]:

learn = cnn_learner(data, models.resnet50, metrics = accuracy, model_dir="/tmp/model/")
#learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-1))
#learn.save("StaticWeights_resnet50_v1")

Downloading: "https://download.pytorch.org/models/resnet50-19c8e357.pth" to /tmp/.torch/models/resnet50-19c8e357.pth
102502400it [00:06, 15599076.53it/s]

In [14]:

learn.lr_find()
learn.recorder.plot()
learn.unfreeze()
learn.fit_one_cycle(10, max_lr=slice(1e-6,1e-1))
learn.save("FittedWeights_resnet50_v1")

LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.

10.00% [1/10 39:05<5:51:45]

epoch | train_loss | valid_loss | accuracy | time
0 | 5.730565 | 5.225624 | 0.098218 | 39:05

57.61% [1636/2840 19:55<14:39 8.2655]

In [15]:

interp = ClassificationInterpretation.from_learner(learn)
losses,idxs = interp.top_losses()
interp.plot_top_losses(9, figsize=(15,11))
interp.plot_confusion_matrix(figsize=(12,12), dpi=60)

In [16]:

preds, y = learn.get_preds(DatasetType.Test)

In [17]:

#pred_t, _ = learn.TTA(ds_type=DatasetType.Test)
#pred_t_max = np.argmax(pred_t, 1); pred_t_max[0:5]

In [18]:

#result = torch.topk(pred_t, 5)
results = torch.topk(preds, 5)

In [19]:

predictions = []
for i in results[1].numpy():
    temp = ""
    for j in i:
        temp += (" "+str(data.classes[j]))
    predictions.append(temp)

In [20]:

submission_df = pd.read_csv(PATH/"kaggle_sample_submission.csv")
#submission_df_sub = submission_df[:1000]
#submission_df_sub["predicted"] = predictions
submission_df["predicted"] = predictions

In [21]:

#submission_df_sub.to_csv("submission_sub.csv", index = False)
submission_df.to_csv("submission.csv", index = False)

Learn From Other Kernels - Fork From Me

From: https://www.kaggle.com/lowecoryr/learn-from-other-kernels-fork-from-me

Author: lowecoryr

See: https://www.kaggle.com/c/inaturalist-2019-fgvc6/kernels

Specific Kernel URL:

PASTE URL HERE

In General, What Is the Kernel doing? What Type of Information Is the Kenrel Communicating?

PASTE ANSWER HERE

In Specific, List the Insights from the Kernel

PASTE ANSWERS HERE

What Are The Implications from Your Answers Above? How Can We Incorporate This in Our Analysis?

PASTE ANSWERS HERE

What Valuable Pieces of Code Are Used That We Can Borrow/Modify?

Add Links to Relevant Documentation (TensorFlow/Keras/etc.)

PASTE ANSWERS IN CELLS BELOW. BEGIN TO MODIFY THE CODE

inaturalist xception 512

From: https://www.kaggle.com/cedriclacrambe/inaturalist-xception-512

Author: Cédric Lacrambe

In [1]:

# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in
import  datetime
date_depart=datetime.datetime.now()
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import weakref
import warnings

warnings.simplefilter("ignore")
import logging
logging.basicConfig(filename='python.log',level=logging.DEBUG)
logging.captureWarnings(True)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

from functools import partial
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory
import cv2
import matplotlib.pyplot as plt
import imageio
import imgaug
from imgaug import augmenters as iaa
from imgaug import parameters as iap
import keras
from keras.models import Sequential, Model
from keras.layers import Dense, Flatten, Activation, Dropout, GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers, applications
from keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping
from keras import backend as K
from concurrent import futures
import os
import json
print(os.listdir("../input"))

# Any results you write to the current directory are saved as output.

Using TensorFlow backend.

['inaturalist-2019-fgvc6', 'xception', 'inaturalist-xception-512']

In [2]:

#!ls -Rlh ../input

In [3]:

duree_max=datetime.timedelta(hours=7,minutes=30)

fichier_modele_base="inaturalist"
train_batch_size=64
full_train_batch_size=4
val_batch_size=4
epochs=int(1e8)

load_keras_weights=False
dmax=1024
dmin=600
dcrop=512
date_limite= date_depart+duree_max

In [4]:

ann_file = '../input/inaturalist-2019-fgvc6/train2019.json'
with open(ann_file) as data_file:
        train_anns = json.load(data_file)
def get_file_list(ann_file):
    with open(ann_file) as data_file:
            train_anns = json.load(data_file)

    train_anns_df = pd.DataFrame(train_anns['annotations'])[['image_id','category_id']]
    train_img_df = pd.DataFrame(train_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
    train_anns_df [train_anns_df.image_id.duplicated()]

    df_train=pd.merge(train_img_df,train_anns_df)[['category_id', 'file_name']]
    return  df_train

df_train=get_file_list(ann_file)
df_train

Out[4]:

| category_id | file_name
0 | 400 | train_val2019/Plants/400/d1322d13ccd856eb4236c...
1 | 570 | train_val2019/Plants/570/15edbc1e2ef000d8ace48...
2 | 167 | train_val2019/Reptiles/167/c87a32e8927cbf4f06d...
3 | 254 | train_val2019/Birds/254/9fcdd1d37e96d8fd94dfdc...
4 | 739 | train_val2019/Plants/739/ffa06f951e99de9d220ae...
5 | 246 | train_val2019/Birds/246/ef5109f4c0bd04ca509fad...
6 | 25 | train_val2019/Insects/25/5ee1691712dffd9e3a178...
7 | 386 | train_val2019/Plants/386/3af7b0ad2e2bc118e38c5...
8 | 515 | train_val2019/Plants/515/4bfd750cbb32bb0e302ca...
9 | 259 | train_val2019/Birds/259/353bd26bf9e5e1606a9189...
10 | 824 | train_val2019/Plants/824/2543714c2f1a8611f1a78...
11 | 234 | train_val2019/Birds/234/dc64a999cacea6eb5ed512...
12 | 363 | train_val2019/Plants/363/aec09afe1abc6aa7dc70c...
13 | 235 | train_val2019/Birds/235/73d440f2157a161fec3e77...
14 | 862 | train_val2019/Plants/862/e4d28d6347aa0d38322a6...
15 | 731 | train_val2019/Plants/731/032b0860dcd8ae04aaab4...
16 | 325 | train_val2019/Birds/325/c1db84df7d22e0933df6b6...
17 | 213 | train_val2019/Birds/213/091a60ad7d3cbec3141eea...
18 | 149 | train_val2019/Insects/149/2cd40eee1996f0eb3187...
19 | 131 | train_val2019/Insects/131/6e84516b34cdc45be2a3...
20 | 167 | train_val2019/Reptiles/167/e21448daf063c25cb56...
21 | 151 | train_val2019/Insects/151/c84bc5a5187096098f4f...
22 | 751 | train_val2019/Plants/751/c3fb975e1120f46671632...
23 | 270 | train_val2019/Birds/270/8d00e0ba26503ec41aa884...
24 | 205 | train_val2019/Birds/205/430ba5e10dc2a2888f8b14...
25 | 415 | train_val2019/Plants/415/c49a1cd3a1d83a15e9274...
26 | 149 | train_val2019/Insects/149/e0587065186b2bd34478...
27 | 739 | train_val2019/Plants/739/16494b1e8cdcc0cbf5c13...
28 | 8 | train_val2019/Fungi/8/ceea3c6d5ff90364486f5683...
29 | 922 | train_val2019/Plants/922/9635afacfa8e46e0390c2...
... | ... | ...
265183 | 604 | train_val2019/Plants/604/32958abfc2eb70b849356...
265184 | 236 | train_val2019/Birds/236/e2197c5fa669685dc45221...
265185 | 660 | train_val2019/Plants/660/6300ce1be902e9c5ca97e...
265186 | 353 | train_val2019/Plants/353/39882460221ea4acf9879...
265187 | 387 | train_val2019/Plants/387/e9060a2a376ef9d53d55e...
265188 | 907 | train_val2019/Plants/907/45503a52d6ff851ee2aac...
265189 | 968 | train_val2019/Plants/968/0ee365c29cbf8e328ccb1...
265190 | 188 | train_val2019/Reptiles/188/128f298609594ba84cc...
265191 | 153 | train_val2019/Amphibians/153/f1e8b2d3b668b9922...
265192 | 970 | train_val2019/Plants/970/8fd903f36f2132605d3bb...
265193 | 350 | train_val2019/Plants/350/c60f16cb442d9f40e11eb...
265194 | 712 | train_val2019/Plants/712/649689c72339cd76f0220...
265195 | 526 | train_val2019/Plants/526/feb980e3cad11fbf56940...
265196 | 233 | train_val2019/Birds/233/c06b3e72c9796fc266969c...
265197 | 328 | train_val2019/Plants/328/65bfb2a6fe3dceb3e0b02...
265198 | 211 | train_val2019/Birds/211/f1f83215f9cdbc29064b9e...
265199 | 268 | train_val2019/Birds/268/6f303db38e86c4dd5ba943...
265200 | 321 | train_val2019/Birds/321/540a4789c221ca78be9a30...
265201 | 273 | train_val2019/Birds/273/335c36a57fbd52e6071c4e...
265202 | 477 | train_val2019/Plants/477/04234ec77dd9d48512a6d...
265203 | 420 | train_val2019/Plants/420/5802a39c2f868ffa4ad6e...
265204 | 77 | train_val2019/Insects/77/41f0485a9fc909328b8a5...
265205 | 1001 | train_val2019/Plants/1001/c1813d4501453affaeaf...
265206 | 375 | train_val2019/Plants/375/d4b9b81094127add57afe...
265207 | 532 | train_val2019/Plants/532/19b8c68c716998290f242...
265208 | 223 | train_val2019/Birds/223/ebf4f99d55fb0071c49e08...
265209 | 188 | train_val2019/Reptiles/188/cb0600062644540aaf2...
265210 | 70 | train_val2019/Insects/70/bfd566f9ae5177f5f2b52...
265211 | 587 | train_val2019/Plants/587/59c3e60d021606bf352de...
265212 | 287 | train_val2019/Birds/287/99da04ba30e67391f9a35b...

265213 rows × 2 columns

In [5]:

df_val=get_file_list('../input/inaturalist-2019-fgvc6/val2019.json')
df_val

Out[5]:

| category_id | file_name
0 | 644 | train_val2019/Plants/644/716a69838526f3ada3b2f...
1 | 597 | train_val2019/Plants/597/0942cc64d2e759c5ee059...
2 | 883 | train_val2019/Plants/883/acfdbfd9fa675f1c84558...
3 | 300 | train_val2019/Birds/300/5f3194ff536c7dd31d80b7...
4 | 881 | train_val2019/Plants/881/76acaf0b2841f91982d21...
5 | 771 | train_val2019/Plants/771/5d190fd90da893988a3c9...
6 | 607 | train_val2019/Plants/607/8fb0ddfe92dadbf9c5753...
7 | 698 | train_val2019/Plants/698/6d71521a64d1e2fe8bb34...
8 | 714 | train_val2019/Plants/714/09508f2bf937d21d63297...
9 | 316 | train_val2019/Birds/316/21c700bc90523485af6730...
10 | 726 | train_val2019/Plants/726/847b2ebf8efc1a528c2d3...
11 | 27 | train_val2019/Insects/27/e5d141185a3a50b544d15...
12 | 640 | train_val2019/Plants/640/cbe5ff1a159b614ae8677...
13 | 756 | train_val2019/Plants/756/a99c33a5f954ffecd7340...
14 | 73 | train_val2019/Insects/73/2fc6b41247af765af9984...
15 | 594 | train_val2019/Plants/594/d98519788816a0f61bd15...
16 | 179 | train_val2019/Reptiles/179/914aab5662e77a128c7...
17 | 440 | train_val2019/Plants/440/02f1f67068df3d8855467...
18 | 859 | train_val2019/Plants/859/23ba5d0baf198007ec097...
19 | 5 | train_val2019/Fungi/5/3308225981fc58e6eb6a7889...
20 | 401 | train_val2019/Plants/401/6c73829cb13a02a69123e...
21 | 928 | train_val2019/Plants/928/d65f36eaf70136e1d4c2e...
22 | 216 | train_val2019/Birds/216/f5902f2a9566038b67ff72...
23 | 200 | train_val2019/Reptiles/200/ef2471af2f01a252250...
24 | 983 | train_val2019/Plants/983/5b1e6397e48bec711b3a3...
25 | 350 | train_val2019/Plants/350/8ebad1a2af78183b35bac...
26 | 223 | train_val2019/Birds/223/e951479ab2f49714025c1e...
27 | 438 | train_val2019/Plants/438/f94013bba01de6a654f81...
28 | 834 | train_val2019/Plants/834/4c3d81073f661f93c20c3...
29 | 248 | train_val2019/Birds/248/0a2da2111a989327d9935b...
... | ... | ...
3000 | 681 | train_val2019/Plants/681/3458385697acabf822006...
3001 | 830 | train_val2019/Plants/830/9a30a84d57d2aa127298e...
3002 | 60 | train_val2019/Insects/60/cb0032132cbad87f49115...
3003 | 784 | train_val2019/Plants/784/0cadfce23ea5c5aaff90b...
3004 | 873 | train_val2019/Plants/873/b6a4bebe95e25647b50fd...
3005 | 868 | train_val2019/Plants/868/7e6410f5ea26657666206...
3006 | 704 | train_val2019/Plants/704/e226a1d3713feaafc0443...
3007 | 617 | train_val2019/Plants/617/dbea4253dbcfb3b06b3c1...
3008 | 631 | train_val2019/Plants/631/191450a32f3105071caf2...
3009 | 548 | train_val2019/Plants/548/edd75e1d3fec72691b046...
3010 | 424 | train_val2019/Plants/424/047f1de7a61605cb15b5a...
3011 | 14 | train_val2019/Insects/14/d1d2163a1b64882cd2a7c...
3012 | 976 | train_val2019/Plants/976/963055c5e9726e06ae73c...
3013 | 321 | train_val2019/Birds/321/98713839fa00a49b75dc5e...
3014 | 391 | train_val2019/Plants/391/7628608962c27c640c38d...
3015 | 862 | train_val2019/Plants/862/f9969d436af626938b9dc...
3016 | 809 | train_val2019/Plants/809/fe521339f28d17599928c...
3017 | 813 | train_val2019/Plants/813/275660d70fe22e5ddc0c0...
3018 | 219 | train_val2019/Birds/219/99e8afe389a7b46fefdb86...
3019 | 292 | train_val2019/Birds/292/5a1af52f0434e3996f7a5a...
3020 | 155 | train_val2019/Amphibians/155/009611cb3d44f7a9c...
3021 | 772 | train_val2019/Plants/772/d83e9fd8c2e3493df4e49...
3022 | 968 | train_val2019/Plants/968/59d8da88969576e7bae10...
3023 | 893 | train_val2019/Plants/893/627c72786a18242784b7f...
3024 | 157 | train_val2019/Amphibians/157/6d3b4f10fa385dd81...
3025 | 468 | train_val2019/Plants/468/3260e4346ba127a28b564...
3026 | 1006 | train_val2019/Plants/1006/3b33c0e1fb866c452f01...
3027 | 472 | train_val2019/Plants/472/44a19d7731dccd92e4c0e...
3028 | 192 | train_val2019/Reptiles/192/c5f142b44904508c866...
3029 | 835 | train_val2019/Plants/835/a6f21e1829ff47c54d474...

3030 rows × 2 columns

In [6]:

train_anns.keys()

Out[6]:

dict_keys(['info', 'images', 'licenses', 'annotations', 'categories'])

In [7]:

len(df_train.category_id.unique())

Out[7]:

1010

In [8]:

df_valid=get_file_list('../input/inaturalist-2019-fgvc6/train2019.json')
df_valid

Out[8]:

| category_id | file_name
0 | 400 | train_val2019/Plants/400/d1322d13ccd856eb4236c...
1 | 570 | train_val2019/Plants/570/15edbc1e2ef000d8ace48...
2 | 167 | train_val2019/Reptiles/167/c87a32e8927cbf4f06d...
3 | 254 | train_val2019/Birds/254/9fcdd1d37e96d8fd94dfdc...
4 | 739 | train_val2019/Plants/739/ffa06f951e99de9d220ae...
5 | 246 | train_val2019/Birds/246/ef5109f4c0bd04ca509fad...
6 | 25 | train_val2019/Insects/25/5ee1691712dffd9e3a178...
7 | 386 | train_val2019/Plants/386/3af7b0ad2e2bc118e38c5...
8 | 515 | train_val2019/Plants/515/4bfd750cbb32bb0e302ca...
9 | 259 | train_val2019/Birds/259/353bd26bf9e5e1606a9189...
10 | 824 | train_val2019/Plants/824/2543714c2f1a8611f1a78...
11 | 234 | train_val2019/Birds/234/dc64a999cacea6eb5ed512...
12 | 363 | train_val2019/Plants/363/aec09afe1abc6aa7dc70c...
13 | 235 | train_val2019/Birds/235/73d440f2157a161fec3e77...
14 | 862 | train_val2019/Plants/862/e4d28d6347aa0d38322a6...
15 | 731 | train_val2019/Plants/731/032b0860dcd8ae04aaab4...
16 | 325 | train_val2019/Birds/325/c1db84df7d22e0933df6b6...
17 | 213 | train_val2019/Birds/213/091a60ad7d3cbec3141eea...
18 | 149 | train_val2019/Insects/149/2cd40eee1996f0eb3187...
19 | 131 | train_val2019/Insects/131/6e84516b34cdc45be2a3...
20 | 167 | train_val2019/Reptiles/167/e21448daf063c25cb56...
21 | 151 | train_val2019/Insects/151/c84bc5a5187096098f4f...
22 | 751 | train_val2019/Plants/751/c3fb975e1120f46671632...
23 | 270 | train_val2019/Birds/270/8d00e0ba26503ec41aa884...
24 | 205 | train_val2019/Birds/205/430ba5e10dc2a2888f8b14...
25 | 415 | train_val2019/Plants/415/c49a1cd3a1d83a15e9274...
26 | 149 | train_val2019/Insects/149/e0587065186b2bd34478...
27 | 739 | train_val2019/Plants/739/16494b1e8cdcc0cbf5c13...
28 | 8 | train_val2019/Fungi/8/ceea3c6d5ff90364486f5683...
29 | 922 | train_val2019/Plants/922/9635afacfa8e46e0390c2...
... | ... | ...
265183 | 604 | train_val2019/Plants/604/32958abfc2eb70b849356...
265184 | 236 | train_val2019/Birds/236/e2197c5fa669685dc45221...
265185 | 660 | train_val2019/Plants/660/6300ce1be902e9c5ca97e...
265186 | 353 | train_val2019/Plants/353/39882460221ea4acf9879...
265187 | 387 | train_val2019/Plants/387/e9060a2a376ef9d53d55e...
265188 | 907 | train_val2019/Plants/907/45503a52d6ff851ee2aac...
265189 | 968 | train_val2019/Plants/968/0ee365c29cbf8e328ccb1...
265190 | 188 | train_val2019/Reptiles/188/128f298609594ba84cc...
265191 | 153 | train_val2019/Amphibians/153/f1e8b2d3b668b9922...
265192 | 970 | train_val2019/Plants/970/8fd903f36f2132605d3bb...
265193 | 350 | train_val2019/Plants/350/c60f16cb442d9f40e11eb...
265194 | 712 | train_val2019/Plants/712/649689c72339cd76f0220...
265195 | 526 | train_val2019/Plants/526/feb980e3cad11fbf56940...
265196 | 233 | train_val2019/Birds/233/c06b3e72c9796fc266969c...
265197 | 328 | train_val2019/Plants/328/65bfb2a6fe3dceb3e0b02...
265198 | 211 | train_val2019/Birds/211/f1f83215f9cdbc29064b9e...
265199 | 268 | train_val2019/Birds/268/6f303db38e86c4dd5ba943...
265200 | 321 | train_val2019/Birds/321/540a4789c221ca78be9a30...
265201 | 273 | train_val2019/Birds/273/335c36a57fbd52e6071c4e...
265202 | 477 | train_val2019/Plants/477/04234ec77dd9d48512a6d...
265203 | 420 | train_val2019/Plants/420/5802a39c2f868ffa4ad6e...
265204 | 77 | train_val2019/Insects/77/41f0485a9fc909328b8a5...
265205 | 1001 | train_val2019/Plants/1001/c1813d4501453affaeaf...
265206 | 375 | train_val2019/Plants/375/d4b9b81094127add57afe...
265207 | 532 | train_val2019/Plants/532/19b8c68c716998290f242...
265208 | 223 | train_val2019/Birds/223/ebf4f99d55fb0071c49e08...
265209 | 188 | train_val2019/Reptiles/188/cb0600062644540aaf2...
265210 | 70 | train_val2019/Insects/70/bfd566f9ae5177f5f2b52...
265211 | 587 | train_val2019/Plants/587/59c3e60d021606bf352de...
265212 | 287 | train_val2019/Birds/287/99da04ba30e67391f9a35b...

265213 rows × 2 columns

In [9]:

classes=len(df_valid.category_id.unique())
print("random accuracy:",1/classes)
classes

random accuracy: 0.0009900990099009901

Out[9]:

1010

In [10]:

df_train["catstr"]=df_train.category_id.astype("str")

In [11]:

p=next(df_train.sample(n=5).itertuples())
p.category_id
p.file_name

Out[11]:

'train_val2019/Birds/246/ee8c33c450ac2243493896decff953be.jpg'

In [12]:

plt.figure(figsize=(25,25))
for n,tu in enumerate(df_train.sample(n=5).itertuples()):
    cat=tu.category_id
    im=tu.file_name
    plt.subplot(1,5,n+1)
    im=os.path.join("../input/inaturalist-2019-fgvc6/train_val2019/",im)
    plt.axis("off")
    plt.title(cat)
    plt.imshow(imageio.imread(im))

In [13]:

augmenters=[iaa.Sometimes(0.5,
                                [iaa.Affine(scale=(0.99,1.05),
                                 translate_percent=(0,0.05),
                                 rotate=iap.Normal(0,3),
                                 shear=iap.Normal(0,3),
                                 order=3)]),
                      iaa.Sometimes(0.3,[iaa.PiecewiseAffine(scale=(0,0.02))]),

                    iaa.Sometimes(0.1,
                                    [iaa.GaussianBlur(sigma=(0, 0.5)) ]),
                    iaa.Sometimes(0.1,
                                        [iaa.AverageBlur(k=(1, 2))]),
                    iaa.AdditiveGaussianNoise(scale=(0, 0.05*255)),
                    iaa.AddElementwise((-5, 5)),

                    iaa.Sometimes(0.1,
                                    [iaa.Superpixels(p_replace=(0.05, 0.8), n_segments=(16, 128))]
                                 ),

                    iaa.Sometimes(0.3,
                                    [iaa.ElasticTransformation(alpha=(0, 5.0), sigma=(0.1,0.6) )]
                                 ),

                    iaa.SaltAndPepper(p=(0.005,0.1)),
                                      iaa.Sometimes(0.4,
                                        [iaa.CoarseDropout(p=(0, 0.3),size_percent=(0.02, 0.5))]),

]

augmenters=[iaa.Sometimes(0.8,[iaa.Sequential(augmenters)])]

In [14]:

all(os.path.isfile(os.path.join("../input/inaturalist-2019-fgvc6/train_val2019/",f)) for f in df_train.sample(n=6000).file_name)

Out[14]:

True

In [15]:

tu=next(df_train.itertuples())
tu.file_name
tu.category_id

df=df_train.copy()
df["req"]=None

In [16]:

def  imgaug_batch_gen(df,batch=16,executor=None,dmax=1024,dmin=512):
    if executor is None:
        executor=futures.ThreadPoolExecutor(max_workers=2)
    prefetch=int(batch*2.1+1)
    df_len=len(df)
    def load_resize(f,dmax=dmax,dmin=dmin):
        img=imageio.imread(f,pilmode="RGB")
        resmin_img=np.min( img.shape[:2] )
        resmin=np.clip(resmin_img,dmin,dmax)
        r=resmin_img/resmin

        if r>1.2 or r<1.0:
            img=cv2.resize(img,None,fx=1.05/r, fy=1.05/r, interpolation = cv2.INTER_CUBIC)
        return img

    while True:

        df=df[["category_id","file_name"]].sample(frac=1).reset_index(drop=True)
        df["req"]=None
        i=0
        while i <(df_len-batch):
            df[df.req.notnull()].loc[:i]=None
            for j in range(prefetch):
                try:
                    if df.loc[j+i,"req"] is None:
                        f=os.path.join("../input/inaturalist-2019-fgvc6/train_val2019/",df.loc[j+i,"file_name"])
                        df.loc[j+i,"req"]=executor.submit(load_resize ,f)
                except KeyError:
                    logging.exception("imgaug_batch_gen")

            df_batch=df.loc[i:i+batch-1]
            imgs=[req.result() for req in  df_batch.req]
            resmin=np.min(np.array([im.shape[:2]  for im in imgs]))
            resmin=np.clip(resmin,dmin,dmax)
            for j in range(batch):
                x,y,_=imgs[j].shape
                img_resmin=min(x,y)
                r=img_resmin/resmin

                if r>1.2 or r<1.0:
                    imgs[j]=cv2.resize(imgs[j],None,fx=1.05/r, fy=1.05/r, interpolation = cv2.INTER_CUBIC)

            categories=df_batch.category_id.values.astype("int32")

            yield imgaug.imgaug.Batch(images=imgs,data=categories)
            df.loc[i:i+batch-1,"req"]=None
            i+=batch

def  batch_gen(df,batch=16,augmenters=[],executor=None,dmax=1024,dmin=512,dcrop=512):
    if executor is None:
        executor=futures.ThreadPoolExecutor(max_workers=2)

    dmin=max(dmin,dcrop)
    dmax=max(dmin,dmax)
    gen =imgaug_batch_gen(df,batch=batch,executor=executor,dmax=dmax,dmin=dmin)
    aug=iaa.Sequential(augmenters+[iaa.CropToFixedSize(dmin,dmin),iaa.PadToFixedSize(dmin,dmin)])
    #aug_pool=aug.pool(processes=-1,maxtasksperchild=8)
    #gen=aug_pool.imap_batches_unordered(gen, chunksize=1)

    def aug_closure_gen(gen=gen):
        b=next(gen)
        fut=executor.submit(aug.augment_batches,[b],background=False)
        for b in gen:
            imgs=list(fut.result())[0].images_aug
            fut=executor.submit(aug.augment_batches,[b],background=False)
            #b=list(aug.augment_batches([b],background=False))[0]

            imgs=[im[None,...] for im in imgs]
            X=np.concatenate(imgs).astype("float32")/256
            Y=b.data[...,None]
            yield X,Y

    aug_closure=aug_closure_gen(gen)

    return aug_closure

aug=iaa.Sequential(augmenters)
#aug_pool=aug.pool(processes=None, maxtasksperchild=None)

gen=batch_gen(df_train,batch=16,augmenters=augmenters)
X,Y=next(gen)
del gen
b_len=X.shape[0]
cols=4
rows=b_len//cols
if b_len%cols!=0:
    rows=rows+1

plt.figure(figsize=(25,25))
X.shape[0]
for n in range(b_len):

    cat=Y[n][0]

    plt.subplot(rows,cols,n+1)

    plt.axis("off")
    plt.title(cat)
    plt.imshow(X[n])

In [17]:

pretrained_model=keras.applications.Xception(include_top=False, weights=None,pooling=None)
pretrained_model.load_weights("../input/xception/xception_weights_tf_dim_ordering_tf_kernels_notop.h5")
fichier_modele=f"{fichier_modele_base}_{pretrained_model.name}.h5"
fichier_modele

tensorflow  : WARNING  From /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.

Out[17]:

'inaturalist_xception.h5'

In [18]:

def mean_anwer_guess(y_true, y_pred):
    n=K.get_variable_shape(y_pred)[1]
    y_pred=K.cast(y_pred,"int64")
    preds=K.gather(y_true,y_pred)
    return K.mean(preds)

def mean_prandom_ratio(y_true, y_pred):
    n=K.get_variable_shape(y_pred)[1]
    y_pred=K.cast(y_pred,"int64")
    preds=K.gather(y_true,y_pred)
    return K.mean(preds*n)
import functools
def get_sparse_topn__categorical_accuracy(k):
    func=functools.partial(keras.metrics.sparse_top_k_categorical_accuracy,k=k)
    func.__name__=f"sparse_top_{k}_categorical_accuracy"
    return func

In [19]:

out_regulariser=keras.regularizers.l1_l2(l1=0.01, l2=0.05)

image_input=keras.Input(shape=(None,None,3), name="image_input", dtype="float32")
bottleneck1=pretrained_model(image_input)
bottleneck=keras.layers.Conv2D(filters=600,
                               kernel_size=3,padding="same",
                               kernel_initializer=keras.initializers.Orthogonal(),
                               activation="selu",
                               activity_regularizer=keras.regularizers.l1_l2(l1=0.01, l2=0.05),
                               strides=2

                              )(bottleneck1)

pool1=(keras.layers.GlobalMaxPool2D()(bottleneck1))

pool=(keras.layers.GlobalMaxPool2D()(bottleneck))
pool=keras.layers.Concatenate()([pool,pool1])
pool=keras.layers.AlphaDropout(0.3)(pool)
pre_out=keras.layers.Dense(1200 ,
                           name="pre_out",
                            activation="selu",
                           kernel_regularizer=out_regulariser
                          )(pool)
out=keras.layers.Dense(classes,
                       activation="softmax"
                       ,name="out",
                       kernel_initializer=keras.initializers.Orthogonal(),
                       kernel_regularizer=out_regulariser)(pre_out)

model=keras.Model(inputs=image_input,outputs=out)
pretrained_model.trainable=False
optimizer=keras.optimizers.Adam(clipnorm=5. , clipvalue=5.,amsgrad=False,lr=0.0005)
model.compile(optimizer,
                            loss="sparse_categorical_crossentropy",
              metrics=["sparse_categorical_accuracy","sparse_categorical_crossentropy",mean_anwer_guess,get_sparse_topn__categorical_accuracy(2),
                       get_sparse_topn__categorical_accuracy(5),get_sparse_topn__categorical_accuracy(10)]
             )
model.summary()

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
image_input (InputLayer)        (None, None, None, 3 0
__________________________________________________________________________________________________
xception (Model)                (None, None, None, 2 20861480    image_input[0][0]
__________________________________________________________________________________________________
conv2d_5 (Conv2D)               (None, None, None, 6 11059800    xception[1][0]
__________________________________________________________________________________________________
global_max_pooling2d_2 (GlobalM (None, 600)          0           conv2d_5[0][0]
__________________________________________________________________________________________________
global_max_pooling2d_1 (GlobalM (None, 2048)         0           xception[1][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 2648)         0           global_max_pooling2d_2[0][0]
                                                                 global_max_pooling2d_1[0][0]
__________________________________________________________________________________________________
alpha_dropout_1 (AlphaDropout)  (None, 2648)         0           concatenate_1[0][0]
__________________________________________________________________________________________________
pre_out (Dense)                 (None, 1200)         3178800     alpha_dropout_1[0][0]
__________________________________________________________________________________________________
out (Dense)                     (None, 1010)         1213010     pre_out[0][0]
==================================================================================================
Total params: 36,313,090
Trainable params: 15,451,610
Non-trainable params: 20,861,480
__________________________________________________________________________________________________

In [20]:

class termination_date(keras.callbacks.Callback ):
    def __init__(self,end_date):
        self.end_date=end_date
    def on_epoch_end(self, batch, logs=None):
        if datetime.datetime.now()>self.end_date:
            self.model.stop_training = True
            logging.info("end date")

class logcallback(keras.callbacks.Callback):
    def __init__(self,logger=None):
        if logger is None:
            logger=logging.getLogger('traincallback')

        self.logger=logger
    def on_train_begin(self, logs={}):
        self.logger.info("training start: %s",self.model.name)

    def on_batch_end(self, batch, logs={}):
        met=""
        for k,v in logs.items():
            met+=f"{k}: {str(v)} "
        self.logger.debug("batch: %s - %s",batch,met)

    def on_epoch_end(self, epoch, logs=None):
        met=""
        for k,v in logs.items():
            met+=f"{k}: {str(v)} "
        self.logger.info("epoch: %s - %s",epoch,met)
    def on_train_end(self, logs={}):
        self.logger.info("training end: %s",self.model.name)

In [21]:

callbacks=[
        keras.callbacks.ReduceLROnPlateau(monitor='val_sparse_categorical_accuracy',
                                          patience=10,
                                          min_delta=0.0005,
                                          factor=0.6,
                                          #min_lr=1e-6,
                                          verbose=1,
                                          cooldown=5

                                          ),
        keras.callbacks.ModelCheckpoint(monitor='val_sparse_categorical_accuracy',
                                        filepath=fichier_modele,
                                        verbose=1,
                                        save_best_only=True,
                                        period=20),
        keras.callbacks.EarlyStopping(monitor='val_sparse_categorical_accuracy',
                                      patience=20,

                                          verbose=1,
                                          restore_best_weights=True

                                          ),
         keras.callbacks.CSVLogger("train.csv", separator=',', append=True),
         termination_date(date_limite-datetime.timedelta(minutes=30)),
            keras.callbacks.BaseLogger(),
        logcallback()

        ]

In [22]:

if load_keras_weights:

    for fp in glob.glob(f"../input/**/{fichier_modele}",recursive=True):
        try:
            model.load_weights(fp, by_name=True, skip_mismatch=True)
            logging.info("loaded weights:",fb)
        except Exception as e:
            print(type(e),e)
            logging.exception("exception loading: %s %s",fp,e)
    if os.path.exists(fichier_modele):
        model.load_weights(fichier_modele, by_name=True, skip_mismatch=True)
        logging.info("loaded weights:",fb)

In [23]:

val_gen=batch_gen(df_val,batch=val_batch_size,dmax=dcrop,dmin=dcrop,dcrop=dcrop)

In [24]:

uptime=datetime.datetime.now()-date_depart
logging.info("pre train start %s",uptime)

root        : INFO     pre train start 0:00:41.700056

In [25]:

hist_pre=model.fit_generator(batch_gen(df_train,batch=train_batch_size,augmenters=augmenters,dmax=dmax,dmin=dmin,dcrop=dcrop),
                     steps_per_epoch=1280/train_batch_size,
                             epochs=150,
                             verbose=1,
                     validation_data=val_gen,
                     validation_steps=300/val_batch_size,
                     callbacks=   callbacks+    [ keras.callbacks.EarlyStopping(monitor='val_sparse_categorical_accuracy',
                                      patience=20,

                                          verbose=1,
                                          restore_best_weights=True

                                          )]

                   )
model.save(fichier_modele)

logging.info("pre train end %s",datetime.datetime.now()-date_depart)
logging.info("remaining time %s",datetime.timedelta(hours=9)+date_depart-datetime.datetime.now())

tensorflow  : WARNING  From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
tensorflow  : WARNING  From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_grad.py:102: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Deprecated in favor of operator or tf.math.divide.
traincallback: INFO     training start: model_1

Epoch 1/150
20/20 [==============================] - 336s 17s/step - loss: 72279.2942 - sparse_categorical_accuracy: 7.8125e-04 - sparse_categorical_crossentropy: 7.8264 - mean_anwer_guess: 451.1500 - sparse_top_2_categorical_accuracy: 0.0031 - sparse_top_5_categorical_accuracy: 0.0070 - sparse_top_10_categorical_accuracy: 0.0109 - val_loss: 1892.6826 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 7.3714 - val_mean_anwer_guess: 473.1333 - val_sparse_top_2_categorical_accuracy: 0.0067 - val_sparse_top_5_categorical_accuracy: 0.0100 - val_sparse_top_10_categorical_accuracy: 0.0100

traincallback: INFO     epoch: 0 - val_loss: 1892.6826123046876 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 7.371352685292562 val_mean_anwer_guess: 473.1333333333333 val_sparse_top_2_categorical_accuracy: 0.006666666666666667 val_sparse_top_5_categorical_accuracy: 0.01 val_sparse_top_10_categorical_accuracy: 0.01 loss: 72279.29423828125 sparse_categorical_accuracy: 0.00078125 sparse_categorical_crossentropy: 7.826378536224365 mean_anwer_guess: 451.15 sparse_top_2_categorical_accuracy: 0.003125 sparse_top_5_categorical_accuracy: 0.00703125 sparse_top_10_categorical_accuracy: 0.0109375 lr: 0.0005

Epoch 2/150
20/20 [==============================] - 331s 17s/step - loss: 22305.4429 - sparse_categorical_accuracy: 0.0031 - sparse_categorical_crossentropy: 7.3037 - mean_anwer_guess: 519.3000 - sparse_top_2_categorical_accuracy: 0.0047 - sparse_top_5_categorical_accuracy: 0.0055 - sparse_top_10_categorical_accuracy: 0.0133 - val_loss: 1158.1500 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 7.1955 - val_mean_anwer_guess: 496.5200 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0033

traincallback: INFO     epoch: 1 - val_loss: 1158.1499674479167 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 7.195486653645833 val_mean_anwer_guess: 496.52 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.0033333333333333335 loss: 22305.44287109375 sparse_categorical_accuracy: 0.003125 sparse_categorical_crossentropy: 7.303728413581848 mean_anwer_guess: 519.3 sparse_top_2_categorical_accuracy: 0.0046875 sparse_top_5_categorical_accuracy: 0.00546875 sparse_top_10_categorical_accuracy: 0.01328125 lr: 0.0005

Epoch 3/150
20/20 [==============================] - 327s 16s/step - loss: 14746.6170 - sparse_categorical_accuracy: 0.0023 - sparse_categorical_crossentropy: 6.9962 - mean_anwer_guess: 366.9000 - sparse_top_2_categorical_accuracy: 0.0023 - sparse_top_5_categorical_accuracy: 0.0055 - sparse_top_10_categorical_accuracy: 0.0117 - val_loss: 713.7605 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 7.0041 - val_mean_anwer_guess: 535.3333 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0000e+00 - val_sparse_top_10_categorical_accuracy: 0.0067

traincallback: INFO     epoch: 2 - val_loss: 713.7604996744792 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 7.004112447102864 val_mean_anwer_guess: 535.3333333333334 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0 val_sparse_top_10_categorical_accuracy: 0.006666666666666667 loss: 14746.6169921875 sparse_categorical_accuracy: 0.00234375 sparse_categorical_crossentropy: 6.996247887611389 mean_anwer_guess: 366.9 sparse_top_2_categorical_accuracy: 0.00234375 sparse_top_5_categorical_accuracy: 0.00546875 sparse_top_10_categorical_accuracy: 0.01171875 lr: 0.0005

Epoch 4/150
20/20 [==============================] - 332s 17s/step - loss: 11757.3366 - sparse_categorical_accuracy: 7.8125e-04 - sparse_categorical_crossentropy: 6.8674 - mean_anwer_guess: 461.7000 - sparse_top_2_categorical_accuracy: 0.0039 - sparse_top_5_categorical_accuracy: 0.0117 - sparse_top_10_categorical_accuracy: 0.0227 - val_loss: 477.2980 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9355 - val_mean_anwer_guess: 510.4133 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0133

traincallback: INFO     epoch: 3 - val_loss: 477.29796549479164 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.935500621795654 val_mean_anwer_guess: 510.41333333333336 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.013333333333333334 loss: 11757.33662109375 sparse_categorical_accuracy: 0.00078125 sparse_categorical_crossentropy: 6.867412948608399 mean_anwer_guess: 461.7 sparse_top_2_categorical_accuracy: 0.00390625 sparse_top_5_categorical_accuracy: 0.01171875 sparse_top_10_categorical_accuracy: 0.02265625 lr: 0.0005

Epoch 5/150
20/20 [==============================] - 313s 16s/step - loss: 9759.2302 - sparse_categorical_accuracy: 0.0031 - sparse_categorical_crossentropy: 6.8926 - mean_anwer_guess: 527.5500 - sparse_top_2_categorical_accuracy: 0.0047 - sparse_top_5_categorical_accuracy: 0.0117 - sparse_top_10_categorical_accuracy: 0.0219 - val_loss: 349.2577 - val_sparse_categorical_accuracy: 0.0033 - val_sparse_categorical_crossentropy: 6.9212 - val_mean_anwer_guess: 458.2133 - val_sparse_top_2_categorical_accuracy: 0.0033 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0167

traincallback: INFO     epoch: 4 - val_loss: 349.25768595377605 val_sparse_categorical_accuracy: 0.0033333333333333335 val_sparse_categorical_crossentropy: 6.921210231781006 val_mean_anwer_guess: 458.2133333333333 val_sparse_top_2_categorical_accuracy: 0.0033333333333333335 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.016666666666666666 loss: 9759.230224609375 sparse_categorical_accuracy: 0.003125 sparse_categorical_crossentropy: 6.892594838142395 mean_anwer_guess: 527.55 sparse_top_2_categorical_accuracy: 0.0046875 sparse_top_5_categorical_accuracy: 0.01171875 sparse_top_10_categorical_accuracy: 0.021875 lr: 0.0005

Epoch 6/150
20/20 [==============================] - 314s 16s/step - loss: 8383.8431 - sparse_categorical_accuracy: 0.0039 - sparse_categorical_crossentropy: 6.9110 - mean_anwer_guess: 237.0000 - sparse_top_2_categorical_accuracy: 0.0055 - sparse_top_5_categorical_accuracy: 0.0141 - sparse_top_10_categorical_accuracy: 0.0266 - val_loss: 276.3368 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9180 - val_mean_anwer_guess: 493.2267 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0100

traincallback: INFO     epoch: 5 - val_loss: 276.3368009440104 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.917951691945394 val_mean_anwer_guess: 493.2266666666667 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.01 loss: 8383.843115234375 sparse_categorical_accuracy: 0.00390625 sparse_categorical_crossentropy: 6.910976839065552 mean_anwer_guess: 237.0 sparse_top_2_categorical_accuracy: 0.00546875 sparse_top_5_categorical_accuracy: 0.0140625 sparse_top_10_categorical_accuracy: 0.0265625 lr: 0.0005

Epoch 7/150
20/20 [==============================] - 316s 16s/step - loss: 7551.0763 - sparse_categorical_accuracy: 0.0031 - sparse_categorical_crossentropy: 6.9137 - mean_anwer_guess: 513.4500 - sparse_top_2_categorical_accuracy: 0.0047 - sparse_top_5_categorical_accuracy: 0.0086 - sparse_top_10_categorical_accuracy: 0.0203 - val_loss: 233.6073 - val_sparse_categorical_accuracy: 0.0033 - val_sparse_categorical_crossentropy: 6.9181 - val_mean_anwer_guess: 481.6667 - val_sparse_top_2_categorical_accuracy: 0.0033 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0133

traincallback: INFO     epoch: 6 - val_loss: 233.60728352864584 val_sparse_categorical_accuracy: 0.0033333333333333335 val_sparse_categorical_crossentropy: 6.918062299092611 val_mean_anwer_guess: 481.6666666666667 val_sparse_top_2_categorical_accuracy: 0.0033333333333333335 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.013333333333333334 loss: 7551.076342773437 sparse_categorical_accuracy: 0.003125 sparse_categorical_crossentropy: 6.913731050491333 mean_anwer_guess: 513.45 sparse_top_2_categorical_accuracy: 0.0046875 sparse_top_5_categorical_accuracy: 0.00859375 sparse_top_10_categorical_accuracy: 0.0203125 lr: 0.0005

Epoch 8/150
20/20 [==============================] - 320s 16s/step - loss: 7037.9725 - sparse_categorical_accuracy: 7.8125e-04 - sparse_categorical_crossentropy: 6.9135 - mean_anwer_guess: 433.8500 - sparse_top_2_categorical_accuracy: 0.0016 - sparse_top_5_categorical_accuracy: 0.0055 - sparse_top_10_categorical_accuracy: 0.0133 - val_loss: 250.0103 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9175 - val_mean_anwer_guess: 504.5200 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0000e+00 - val_sparse_top_10_categorical_accuracy: 0.0067

traincallback: INFO     epoch: 7 - val_loss: 250.01030049641926 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.917485218048096 val_mean_anwer_guess: 504.52 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0 val_sparse_top_10_categorical_accuracy: 0.006666666666666667 loss: 7037.9724609375 sparse_categorical_accuracy: 0.00078125 sparse_categorical_crossentropy: 6.913473224639892 mean_anwer_guess: 433.85 sparse_top_2_categorical_accuracy: 0.0015625 sparse_top_5_categorical_accuracy: 0.00546875 sparse_top_10_categorical_accuracy: 0.01328125 lr: 0.0005

Epoch 9/150
20/20 [==============================] - 321s 16s/step - loss: 6745.8842 - sparse_categorical_accuracy: 0.0000e+00 - sparse_categorical_crossentropy: 6.9122 - mean_anwer_guess: 461.6500 - sparse_top_2_categorical_accuracy: 0.0031 - sparse_top_5_categorical_accuracy: 0.0156 - sparse_top_10_categorical_accuracy: 0.0266 - val_loss: 207.7814 - val_sparse_categorical_accuracy: 0.0033 - val_sparse_categorical_crossentropy: 6.9172 - val_mean_anwer_guess: 500.9467 - val_sparse_top_2_categorical_accuracy: 0.0033 - val_sparse_top_5_categorical_accuracy: 0.0067 - val_sparse_top_10_categorical_accuracy: 0.0167

traincallback: INFO     epoch: 8 - val_loss: 207.78143046061197 val_sparse_categorical_accuracy: 0.0033333333333333335 val_sparse_categorical_crossentropy: 6.917235730489095 val_mean_anwer_guess: 500.94666666666666 val_sparse_top_2_categorical_accuracy: 0.0033333333333333335 val_sparse_top_5_categorical_accuracy: 0.006666666666666667 val_sparse_top_10_categorical_accuracy: 0.016666666666666666 loss: 6745.8841796875 sparse_categorical_accuracy: 0.0 sparse_categorical_crossentropy: 6.912249827384949 mean_anwer_guess: 461.65 sparse_top_2_categorical_accuracy: 0.003125 sparse_top_5_categorical_accuracy: 0.015625 sparse_top_10_categorical_accuracy: 0.0265625 lr: 0.0005

Epoch 10/150
20/20 [==============================] - 322s 16s/step - loss: 6187.9865 - sparse_categorical_accuracy: 0.0031 - sparse_categorical_crossentropy: 6.9120 - mean_anwer_guess: 459.1500 - sparse_top_2_categorical_accuracy: 0.0047 - sparse_top_5_categorical_accuracy: 0.0102 - sparse_top_10_categorical_accuracy: 0.0195 - val_loss: 201.7798 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9166 - val_mean_anwer_guess: 451.2800 - val_sparse_top_2_categorical_accuracy: 0.0033 - val_sparse_top_5_categorical_accuracy: 0.0067 - val_sparse_top_10_categorical_accuracy: 0.0067

traincallback: INFO     epoch: 9 - val_loss: 201.77976094563803 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.916606928507487 val_mean_anwer_guess: 451.28 val_sparse_top_2_categorical_accuracy: 0.0033333333333333335 val_sparse_top_5_categorical_accuracy: 0.006666666666666667 val_sparse_top_10_categorical_accuracy: 0.006666666666666667 loss: 6187.986499023437 sparse_categorical_accuracy: 0.003125 sparse_categorical_crossentropy: 6.911971855163574 mean_anwer_guess: 459.15 sparse_top_2_categorical_accuracy: 0.0046875 sparse_top_5_categorical_accuracy: 0.01015625 sparse_top_10_categorical_accuracy: 0.01953125 lr: 0.0005

Epoch 11/150

root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3030] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3030] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3031] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3031] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3032] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3032] is not in the [index]'

20/20 [==============================] - 312s 16s/step - loss: 6009.7854 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.9112 - mean_anwer_guess: 367.0500 - sparse_top_2_categorical_accuracy: 0.0031 - sparse_top_5_categorical_accuracy: 0.0094 - sparse_top_10_categorical_accuracy: 0.0187 - val_loss: 179.8454 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9179 - val_mean_anwer_guess: 437.6667 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0067

traincallback: INFO     epoch: 10 - val_loss: 179.8454327392578 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.917929712931315 val_mean_anwer_guess: 437.6666666666667 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.006666666666666667 loss: 6009.785375976562 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.9111556053161625 mean_anwer_guess: 367.05 sparse_top_2_categorical_accuracy: 0.003125 sparse_top_5_categorical_accuracy: 0.009375 sparse_top_10_categorical_accuracy: 0.01875 lr: 0.0005

Epoch 12/150
20/20 [==============================] - 321s 16s/step - loss: 5702.8190 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.9107 - mean_anwer_guess: 449.5000 - sparse_top_2_categorical_accuracy: 0.0016 - sparse_top_5_categorical_accuracy: 0.0055 - sparse_top_10_categorical_accuracy: 0.0172 - val_loss: 180.1914 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9188 - val_mean_anwer_guess: 521.4000 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0000e+00 - val_sparse_top_10_categorical_accuracy: 0.0100

traincallback: INFO     epoch: 11 - val_loss: 180.19140665690105 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.918752930959066 val_mean_anwer_guess: 521.4 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0 val_sparse_top_10_categorical_accuracy: 0.01 loss: 5702.8189697265625 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.910686731338501 mean_anwer_guess: 449.5 sparse_top_2_categorical_accuracy: 0.0015625 sparse_top_5_categorical_accuracy: 0.00546875 sparse_top_10_categorical_accuracy: 0.0171875 lr: 0.0005

Epoch 13/150
20/20 [==============================] - 334s 17s/step - loss: 5418.8233 - sparse_categorical_accuracy: 0.0000e+00 - sparse_categorical_crossentropy: 6.9098 - mean_anwer_guess: 411.1500 - sparse_top_2_categorical_accuracy: 7.8125e-04 - sparse_top_5_categorical_accuracy: 0.0109 - sparse_top_10_categorical_accuracy: 0.0211 - val_loss: 168.6256 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9169 - val_mean_anwer_guess: 554.9200 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0067 - val_sparse_top_10_categorical_accuracy: 0.0067

traincallback: INFO     epoch: 12 - val_loss: 168.62560038248697 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.916946201324463 val_mean_anwer_guess: 554.92 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.006666666666666667 val_sparse_top_10_categorical_accuracy: 0.006666666666666667 loss: 5418.823291015625 sparse_categorical_accuracy: 0.0 sparse_categorical_crossentropy: 6.909760808944702 mean_anwer_guess: 411.15 sparse_top_2_categorical_accuracy: 0.00078125 sparse_top_5_categorical_accuracy: 0.0109375 sparse_top_10_categorical_accuracy: 0.02109375 lr: 0.0005

Epoch 14/150
20/20 [==============================] - 331s 17s/step - loss: 5436.2248 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.9085 - mean_anwer_guess: 517.9500 - sparse_top_2_categorical_accuracy: 0.0023 - sparse_top_5_categorical_accuracy: 0.0094 - sparse_top_10_categorical_accuracy: 0.0148 - val_loss: 186.9243 - val_sparse_categorical_accuracy: 0.0067 - val_sparse_categorical_crossentropy: 6.9170 - val_mean_anwer_guess: 492.3333 - val_sparse_top_2_categorical_accuracy: 0.0067 - val_sparse_top_5_categorical_accuracy: 0.0067 - val_sparse_top_10_categorical_accuracy: 0.0100

traincallback: INFO     epoch: 13 - val_loss: 186.92434041341147 val_sparse_categorical_accuracy: 0.006666666666666667 val_sparse_categorical_crossentropy: 6.916962973276774 val_mean_anwer_guess: 492.3333333333333 val_sparse_top_2_categorical_accuracy: 0.006666666666666667 val_sparse_top_5_categorical_accuracy: 0.006666666666666667 val_sparse_top_10_categorical_accuracy: 0.01 loss: 5436.224755859375 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.908514475822448 mean_anwer_guess: 517.95 sparse_top_2_categorical_accuracy: 0.00234375 sparse_top_5_categorical_accuracy: 0.009375 sparse_top_10_categorical_accuracy: 0.01484375 lr: 0.0005

Epoch 15/150
20/20 [==============================] - 323s 16s/step - loss: 5270.2783 - sparse_categorical_accuracy: 7.8125e-04 - sparse_categorical_crossentropy: 6.9088 - mean_anwer_guess: 505.9500 - sparse_top_2_categorical_accuracy: 0.0039 - sparse_top_5_categorical_accuracy: 0.0086 - sparse_top_10_categorical_accuracy: 0.0187 - val_loss: 180.5704 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9181 - val_mean_anwer_guess: 524.0800 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0000e+00 - val_sparse_top_10_categorical_accuracy: 0.0033

traincallback: INFO     epoch: 14 - val_loss: 180.57043375651043 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.918078174591065 val_mean_anwer_guess: 524.08 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0 val_sparse_top_10_categorical_accuracy: 0.0033333333333333335 loss: 5270.278271484375 sparse_categorical_accuracy: 0.00078125 sparse_categorical_crossentropy: 6.90878643989563 mean_anwer_guess: 505.95 sparse_top_2_categorical_accuracy: 0.00390625 sparse_top_5_categorical_accuracy: 0.00859375 sparse_top_10_categorical_accuracy: 0.01875 lr: 0.0005

Epoch 16/150
20/20 [==============================] - 325s 16s/step - loss: 5393.2199 - sparse_categorical_accuracy: 0.0023 - sparse_categorical_crossentropy: 6.9075 - mean_anwer_guess: 470.8000 - sparse_top_2_categorical_accuracy: 0.0023 - sparse_top_5_categorical_accuracy: 0.0086 - sparse_top_10_categorical_accuracy: 0.0180 - val_loss: 179.1251 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9160 - val_mean_anwer_guess: 521.4133 - val_sparse_top_2_categorical_accuracy: 0.0033 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0067

traincallback: INFO     epoch: 15 - val_loss: 179.12506388346355 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.9159996096293135 val_mean_anwer_guess: 521.4133333333333 val_sparse_top_2_categorical_accuracy: 0.0033333333333333335 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.006666666666666667 loss: 5393.219946289062 sparse_categorical_accuracy: 0.00234375 sparse_categorical_crossentropy: 6.90752956867218 mean_anwer_guess: 470.8 sparse_top_2_categorical_accuracy: 0.00234375 sparse_top_5_categorical_accuracy: 0.00859375 sparse_top_10_categorical_accuracy: 0.01796875 lr: 0.0005

Epoch 17/150
20/20 [==============================] - 318s 16s/step - loss: 4978.4317 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.9074 - mean_anwer_guess: 443.1500 - sparse_top_2_categorical_accuracy: 0.0031 - sparse_top_5_categorical_accuracy: 0.0086 - sparse_top_10_categorical_accuracy: 0.0203 - val_loss: 221.3766 - val_sparse_categorical_accuracy: 0.0033 - val_sparse_categorical_crossentropy: 6.9185 - val_mean_anwer_guess: 545.5333 - val_sparse_top_2_categorical_accuracy: 0.0067 - val_sparse_top_5_categorical_accuracy: 0.0067 - val_sparse_top_10_categorical_accuracy: 0.0133

traincallback: INFO     epoch: 16 - val_loss: 221.37662109375 val_sparse_categorical_accuracy: 0.0033333333333333335 val_sparse_categorical_crossentropy: 6.918507448832194 val_mean_anwer_guess: 545.5333333333333 val_sparse_top_2_categorical_accuracy: 0.006666666666666667 val_sparse_top_5_categorical_accuracy: 0.006666666666666667 val_sparse_top_10_categorical_accuracy: 0.013333333333333334 loss: 4978.431665039063 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.9073686599731445 mean_anwer_guess: 443.15 sparse_top_2_categorical_accuracy: 0.003125 sparse_top_5_categorical_accuracy: 0.00859375 sparse_top_10_categorical_accuracy: 0.0203125 lr: 0.0005

Epoch 18/150
20/20 [==============================] - 327s 16s/step - loss: 5361.6376 - sparse_categorical_accuracy: 0.0023 - sparse_categorical_crossentropy: 6.9069 - mean_anwer_guess: 588.4000 - sparse_top_2_categorical_accuracy: 0.0031 - sparse_top_5_categorical_accuracy: 0.0094 - sparse_top_10_categorical_accuracy: 0.0172 - val_loss: 164.1036 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9183 - val_mean_anwer_guess: 473.7467 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0133

traincallback: INFO     epoch: 17 - val_loss: 164.10355163574218 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.918310362497966 val_mean_anwer_guess: 473.74666666666667 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.013333333333333334 loss: 5361.637622070312 sparse_categorical_accuracy: 0.00234375 sparse_categorical_crossentropy: 6.906924247741699 mean_anwer_guess: 588.4 sparse_top_2_categorical_accuracy: 0.003125 sparse_top_5_categorical_accuracy: 0.009375 sparse_top_10_categorical_accuracy: 0.0171875 lr: 0.0005

Epoch 19/150
20/20 [==============================] - 323s 16s/step - loss: 4651.0018 - sparse_categorical_accuracy: 0.0047 - sparse_categorical_crossentropy: 6.9048 - mean_anwer_guess: 447.4500 - sparse_top_2_categorical_accuracy: 0.0055 - sparse_top_5_categorical_accuracy: 0.0094 - sparse_top_10_categorical_accuracy: 0.0203 - val_loss: 146.1939 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9199 - val_mean_anwer_guess: 490.9600 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0100

traincallback: INFO     epoch: 18 - val_loss: 146.19388153076173 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.919873574574789 val_mean_anwer_guess: 490.96 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.01 loss: 4651.0018310546875 sparse_categorical_accuracy: 0.0046875 sparse_categorical_crossentropy: 6.904846167564392 mean_anwer_guess: 447.45 sparse_top_2_categorical_accuracy: 0.00546875 sparse_top_5_categorical_accuracy: 0.009375 sparse_top_10_categorical_accuracy: 0.0203125 lr: 0.0005

Epoch 20/150
20/20 [==============================] - 325s 16s/step - loss: 5107.9682 - sparse_categorical_accuracy: 0.0023 - sparse_categorical_crossentropy: 6.9058 - mean_anwer_guess: 463.1500 - sparse_top_2_categorical_accuracy: 0.0031 - sparse_top_5_categorical_accuracy: 0.0047 - sparse_top_10_categorical_accuracy: 0.0133 - val_loss: 153.7441 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9174 - val_mean_anwer_guess: 514.1467 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0067

Epoch 00020: val_sparse_categorical_accuracy improved from -inf to 0.00000, saving model to inaturalist_xception.h5

traincallback: INFO     epoch: 19 - val_loss: 153.74413940429687 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.917436389923096 val_mean_anwer_guess: 514.1466666666666 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.006666666666666667 loss: 5107.968176269531 sparse_categorical_accuracy: 0.00234375 sparse_categorical_crossentropy: 6.905842852592468 mean_anwer_guess: 463.15 sparse_top_2_categorical_accuracy: 0.003125 sparse_top_5_categorical_accuracy: 0.0046875 sparse_top_10_categorical_accuracy: 0.01328125 lr: 0.0005

Epoch 21/150
19/20 [===========================>..] - ETA: 12s - loss: 4671.2478 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.9031 - mean_anwer_guess: 528.1053 - sparse_top_2_categorical_accuracy: 0.0041 - sparse_top_5_categorical_accuracy: 0.0082 - sparse_top_10_categorical_accuracy: 0.0132

root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3030] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3030] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3031] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3031] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3032] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3032] is not in the [index]'

20/20 [==============================] - 323s 16s/step - loss: 4658.8865 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.9031 - mean_anwer_guess: 510.5000 - sparse_top_2_categorical_accuracy: 0.0039 - sparse_top_5_categorical_accuracy: 0.0078 - sparse_top_10_categorical_accuracy: 0.0125 - val_loss: 140.8388 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9200 - val_mean_anwer_guess: 471.5333 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0000e+00 - val_sparse_top_10_categorical_accuracy: 0.0000e+00

traincallback: INFO     epoch: 20 - val_loss: 140.83882486979167 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.919992713928223 val_mean_anwer_guess: 471.53333333333336 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0 val_sparse_top_10_categorical_accuracy: 0.0 loss: 4658.886547851563 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.903148984909057 mean_anwer_guess: 510.5 sparse_top_2_categorical_accuracy: 0.00390625 sparse_top_5_categorical_accuracy: 0.0078125 sparse_top_10_categorical_accuracy: 0.0125 lr: 0.0005

Epoch 22/150
20/20 [==============================] - 333s 17s/step - loss: 4933.3177 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.9026 - mean_anwer_guess: 446.8000 - sparse_top_2_categorical_accuracy: 0.0031 - sparse_top_5_categorical_accuracy: 0.0055 - sparse_top_10_categorical_accuracy: 0.0141 - val_loss: 170.8401 - val_sparse_categorical_accuracy: 0.0033 - val_sparse_categorical_crossentropy: 6.9207 - val_mean_anwer_guess: 514.9200 - val_sparse_top_2_categorical_accuracy: 0.0067 - val_sparse_top_5_categorical_accuracy: 0.0133 - val_sparse_top_10_categorical_accuracy: 0.0200

traincallback: INFO     epoch: 21 - val_loss: 170.8401141357422 val_sparse_categorical_accuracy: 0.0033333333333333335 val_sparse_categorical_crossentropy: 6.920669473012288 val_mean_anwer_guess: 514.92 val_sparse_top_2_categorical_accuracy: 0.006666666666666667 val_sparse_top_5_categorical_accuracy: 0.013333333333333334 val_sparse_top_10_categorical_accuracy: 0.02 loss: 4933.317687988281 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.9026219844818115 mean_anwer_guess: 446.8 sparse_top_2_categorical_accuracy: 0.003125 sparse_top_5_categorical_accuracy: 0.00546875 sparse_top_10_categorical_accuracy: 0.0140625 lr: 0.0005

Epoch 23/150
20/20 [==============================] - 338s 17s/step - loss: 4780.6695 - sparse_categorical_accuracy: 0.0031 - sparse_categorical_crossentropy: 6.9022 - mean_anwer_guess: 466.6000 - sparse_top_2_categorical_accuracy: 0.0047 - sparse_top_5_categorical_accuracy: 0.0094 - sparse_top_10_categorical_accuracy: 0.0180 - val_loss: 190.9515 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9179 - val_mean_anwer_guess: 502.5733 - val_sparse_top_2_categorical_accuracy: 0.0033 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0133

traincallback: INFO     epoch: 22 - val_loss: 190.95153991699217 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.9179022471110025 val_mean_anwer_guess: 502.5733333333333 val_sparse_top_2_categorical_accuracy: 0.0033333333333333335 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.013333333333333334 loss: 4780.669470214843 sparse_categorical_accuracy: 0.003125 sparse_categorical_crossentropy: 6.902244973182678 mean_anwer_guess: 466.6 sparse_top_2_categorical_accuracy: 0.0046875 sparse_top_5_categorical_accuracy: 0.009375 sparse_top_10_categorical_accuracy: 0.01796875 lr: 0.0005

Epoch 24/150
20/20 [==============================] - 331s 17s/step - loss: 4796.8529 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.9015 - mean_anwer_guess: 393.7000 - sparse_top_2_categorical_accuracy: 0.0016 - sparse_top_5_categorical_accuracy: 0.0102 - sparse_top_10_categorical_accuracy: 0.0180 - val_loss: 179.2027 - val_sparse_categorical_accuracy: 0.0033 - val_sparse_categorical_crossentropy: 6.9161 - val_mean_anwer_guess: 473.8400 - val_sparse_top_2_categorical_accuracy: 0.0033 - val_sparse_top_5_categorical_accuracy: 0.0067 - val_sparse_top_10_categorical_accuracy: 0.0133

traincallback: INFO     epoch: 23 - val_loss: 179.20270853678386 val_sparse_categorical_accuracy: 0.0033333333333333335 val_sparse_categorical_crossentropy: 6.916128889719645 val_mean_anwer_guess: 473.84 val_sparse_top_2_categorical_accuracy: 0.0033333333333333335 val_sparse_top_5_categorical_accuracy: 0.006666666666666667 val_sparse_top_10_categorical_accuracy: 0.013333333333333334 loss: 4796.852868652344 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.9015449523925785 mean_anwer_guess: 393.7 sparse_top_2_categorical_accuracy: 0.0015625 sparse_top_5_categorical_accuracy: 0.01015625 sparse_top_10_categorical_accuracy: 0.01796875 lr: 0.0005

Epoch 00024: ReduceLROnPlateau reducing learning rate to 0.0003000000142492354.
Epoch 25/150
20/20 [==============================] - 327s 16s/step - loss: 3995.6191 - sparse_categorical_accuracy: 7.8125e-04 - sparse_categorical_crossentropy: 6.9012 - mean_anwer_guess: 497.9000 - sparse_top_2_categorical_accuracy: 0.0016 - sparse_top_5_categorical_accuracy: 0.0078 - sparse_top_10_categorical_accuracy: 0.0133 - val_loss: 119.3189 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9167 - val_mean_anwer_guess: 502.7200 - val_sparse_top_2_categorical_accuracy: 0.0033 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0067

traincallback: INFO     epoch: 24 - val_loss: 119.31890045166016 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.916702817281087 val_mean_anwer_guess: 502.72 val_sparse_top_2_categorical_accuracy: 0.0033333333333333335 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.006666666666666667 loss: 3995.6191162109376 sparse_categorical_accuracy: 0.00078125 sparse_categorical_crossentropy: 6.901170468330383 mean_anwer_guess: 497.9 sparse_top_2_categorical_accuracy: 0.0015625 sparse_top_5_categorical_accuracy: 0.0078125 sparse_top_10_categorical_accuracy: 0.01328125 lr: 0.0003

Epoch 26/150
20/20 [==============================] - 328s 16s/step - loss: 3407.6282 - sparse_categorical_accuracy: 0.0000e+00 - sparse_categorical_crossentropy: 6.9009 - mean_anwer_guess: 325.7000 - sparse_top_2_categorical_accuracy: 0.0000e+00 - sparse_top_5_categorical_accuracy: 0.0070 - sparse_top_10_categorical_accuracy: 0.0180 - val_loss: 107.8284 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9181 - val_mean_anwer_guess: 417.0800 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0100 - val_sparse_top_10_categorical_accuracy: 0.0133

traincallback: INFO     epoch: 25 - val_loss: 107.82840647379557 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.9180589866638185 val_mean_anwer_guess: 417.08 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.01 val_sparse_top_10_categorical_accuracy: 0.013333333333333334 loss: 3407.6282470703127 sparse_categorical_accuracy: 0.0 sparse_categorical_crossentropy: 6.900906300544738 mean_anwer_guess: 325.7 sparse_top_2_categorical_accuracy: 0.0 sparse_top_5_categorical_accuracy: 0.00703125 sparse_top_10_categorical_accuracy: 0.01796875 lr: 0.0003

Epoch 27/150
20/20 [==============================] - 330s 16s/step - loss: 3168.7803 - sparse_categorical_accuracy: 0.0031 - sparse_categorical_crossentropy: 6.8998 - mean_anwer_guess: 420.0000 - sparse_top_2_categorical_accuracy: 0.0039 - sparse_top_5_categorical_accuracy: 0.0078 - sparse_top_10_categorical_accuracy: 0.0172 - val_loss: 98.6710 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9190 - val_mean_anwer_guess: 526.6800 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0067

traincallback: INFO     epoch: 26 - val_loss: 98.67104654947917 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.918981240590413 val_mean_anwer_guess: 526.68 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.006666666666666667 loss: 3168.7803466796877 sparse_categorical_accuracy: 0.003125 sparse_categorical_crossentropy: 6.899770498275757 mean_anwer_guess: 420.0 sparse_top_2_categorical_accuracy: 0.00390625 sparse_top_5_categorical_accuracy: 0.0078125 sparse_top_10_categorical_accuracy: 0.0171875 lr: 0.0003

Epoch 28/150
20/20 [==============================] - 342s 17s/step - loss: 3400.8443 - sparse_categorical_accuracy: 0.0031 - sparse_categorical_crossentropy: 6.8982 - mean_anwer_guess: 461.4000 - sparse_top_2_categorical_accuracy: 0.0055 - sparse_top_5_categorical_accuracy: 0.0125 - sparse_top_10_categorical_accuracy: 0.0258 - val_loss: 94.9354 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9178 - val_mean_anwer_guess: 458.9067 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0000e+00 - val_sparse_top_10_categorical_accuracy: 0.0033

traincallback: INFO     epoch: 27 - val_loss: 94.93542175292968 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.917813180287679 val_mean_anwer_guess: 458.9066666666667 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0 val_sparse_top_10_categorical_accuracy: 0.0033333333333333335 loss: 3400.8443359375 sparse_categorical_accuracy: 0.003125 sparse_categorical_crossentropy: 6.898177647590638 mean_anwer_guess: 461.4 sparse_top_2_categorical_accuracy: 0.00546875 sparse_top_5_categorical_accuracy: 0.0125 sparse_top_10_categorical_accuracy: 0.02578125 lr: 0.0003

Epoch 29/150
20/20 [==============================] - 322s 16s/step - loss: 3169.0374 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.8987 - mean_anwer_guess: 577.0000 - sparse_top_2_categorical_accuracy: 0.0023 - sparse_top_5_categorical_accuracy: 0.0109 - sparse_top_10_categorical_accuracy: 0.0164 - val_loss: 95.6620 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9161 - val_mean_anwer_guess: 457.8800 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0033

traincallback: INFO     epoch: 28 - val_loss: 95.66195810953776 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.916140003204346 val_mean_anwer_guess: 457.88 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.0033333333333333335 loss: 3169.0373779296874 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.898715519905091 mean_anwer_guess: 577.0 sparse_top_2_categorical_accuracy: 0.00234375 sparse_top_5_categorical_accuracy: 0.0109375 sparse_top_10_categorical_accuracy: 0.01640625 lr: 0.0003

Epoch 30/150
20/20 [==============================] - 329s 16s/step - loss: 3284.2957 - sparse_categorical_accuracy: 0.0031 - sparse_categorical_crossentropy: 6.8982 - mean_anwer_guess: 441.1500 - sparse_top_2_categorical_accuracy: 0.0063 - sparse_top_5_categorical_accuracy: 0.0141 - sparse_top_10_categorical_accuracy: 0.0242 - val_loss: 95.3214 - val_sparse_categorical_accuracy: 0.0033 - val_sparse_categorical_crossentropy: 6.9179 - val_mean_anwer_guess: 498.9200 - val_sparse_top_2_categorical_accuracy: 0.0033 - val_sparse_top_5_categorical_accuracy: 0.0067 - val_sparse_top_10_categorical_accuracy: 0.0100

traincallback: INFO     epoch: 29 - val_loss: 95.32142049153646 val_sparse_categorical_accuracy: 0.0033333333333333335 val_sparse_categorical_crossentropy: 6.917855644226075 val_mean_anwer_guess: 498.92 val_sparse_top_2_categorical_accuracy: 0.0033333333333333335 val_sparse_top_5_categorical_accuracy: 0.006666666666666667 val_sparse_top_10_categorical_accuracy: 0.01 loss: 3284.295703125 sparse_categorical_accuracy: 0.003125 sparse_categorical_crossentropy: 6.8982326030731205 mean_anwer_guess: 441.15 sparse_top_2_categorical_accuracy: 0.00625 sparse_top_5_categorical_accuracy: 0.0140625 sparse_top_10_categorical_accuracy: 0.02421875 lr: 0.0003

Epoch 31/150
19/20 [===========================>..] - ETA: 12s - loss: 3104.8761 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.8976 - mean_anwer_guess: 481.2632 - sparse_top_2_categorical_accuracy: 0.0049 - sparse_top_5_categorical_accuracy: 0.0090 - sparse_top_10_categorical_accuracy: 0.0189

root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3030] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3030] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3031] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3031] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3032] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3032] is not in the [index]'

20/20 [==============================] - 316s 16s/step - loss: 3102.6513 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.8976 - mean_anwer_guess: 505.1500 - sparse_top_2_categorical_accuracy: 0.0047 - sparse_top_5_categorical_accuracy: 0.0086 - sparse_top_10_categorical_accuracy: 0.0187 - val_loss: 113.0610 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9211 - val_mean_anwer_guess: 425.9467 - val_sparse_top_2_categorical_accuracy: 0.0033 - val_sparse_top_5_categorical_accuracy: 0.0033 - val_sparse_top_10_categorical_accuracy: 0.0067

traincallback: INFO     epoch: 30 - val_loss: 113.06103647867839 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.921059652964274 val_mean_anwer_guess: 425.94666666666666 val_sparse_top_2_categorical_accuracy: 0.0033333333333333335 val_sparse_top_5_categorical_accuracy: 0.0033333333333333335 val_sparse_top_10_categorical_accuracy: 0.006666666666666667 loss: 3102.65126953125 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.897599792480468 mean_anwer_guess: 505.15 sparse_top_2_categorical_accuracy: 0.0046875 sparse_top_5_categorical_accuracy: 0.00859375 sparse_top_10_categorical_accuracy: 0.01875 lr: 0.0003

Epoch 32/150
20/20 [==============================] - 322s 16s/step - loss: 3015.0873 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.8968 - mean_anwer_guess: 426.7500 - sparse_top_2_categorical_accuracy: 0.0039 - sparse_top_5_categorical_accuracy: 0.0086 - sparse_top_10_categorical_accuracy: 0.0164 - val_loss: 91.9944 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9179 - val_mean_anwer_guess: 525.2800 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0000e+00 - val_sparse_top_10_categorical_accuracy: 0.0033

traincallback: INFO     epoch: 31 - val_loss: 91.99441772460938 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.91790880203247 val_mean_anwer_guess: 525.28 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0 val_sparse_top_10_categorical_accuracy: 0.0033333333333333335 loss: 3015.0873168945313 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.896802258491516 mean_anwer_guess: 426.75 sparse_top_2_categorical_accuracy: 0.00390625 sparse_top_5_categorical_accuracy: 0.00859375 sparse_top_10_categorical_accuracy: 0.01640625 lr: 0.0003

Epoch 33/150
20/20 [==============================] - 327s 16s/step - loss: 3070.9360 - sparse_categorical_accuracy: 7.8125e-04 - sparse_categorical_crossentropy: 6.8980 - mean_anwer_guess: 474.6000 - sparse_top_2_categorical_accuracy: 0.0023 - sparse_top_5_categorical_accuracy: 0.0055 - sparse_top_10_categorical_accuracy: 0.0141 - val_loss: 95.6509 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9154 - val_mean_anwer_guess: 470.7600 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0000e+00 - val_sparse_top_10_categorical_accuracy: 0.0133

traincallback: INFO     epoch: 32 - val_loss: 95.65090382893881 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.9153985595703125 val_mean_anwer_guess: 470.76 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0 val_sparse_top_10_categorical_accuracy: 0.013333333333333334 loss: 3070.9359619140623 sparse_categorical_accuracy: 0.00078125 sparse_categorical_crossentropy: 6.897995805740356 mean_anwer_guess: 474.6 sparse_top_2_categorical_accuracy: 0.00234375 sparse_top_5_categorical_accuracy: 0.00546875 sparse_top_10_categorical_accuracy: 0.0140625 lr: 0.0003

Epoch 34/150
20/20 [==============================] - 326s 16s/step - loss: 3158.7775 - sparse_categorical_accuracy: 7.8125e-04 - sparse_categorical_crossentropy: 6.8959 - mean_anwer_guess: 486.2000 - sparse_top_2_categorical_accuracy: 0.0031 - sparse_top_5_categorical_accuracy: 0.0063 - sparse_top_10_categorical_accuracy: 0.0180 - val_loss: 95.4145 - val_sparse_categorical_accuracy: 0.0033 - val_sparse_categorical_crossentropy: 6.9177 - val_mean_anwer_guess: 497.6400 - val_sparse_top_2_categorical_accuracy: 0.0033 - val_sparse_top_5_categorical_accuracy: 0.0100 - val_sparse_top_10_categorical_accuracy: 0.0133
Restoring model weights from the end of the best epoch

traincallback: INFO     epoch: 33 - val_loss: 95.41447835286458 val_sparse_categorical_accuracy: 0.0033333333333333335 val_sparse_categorical_crossentropy: 6.917749404907227 val_mean_anwer_guess: 497.64 val_sparse_top_2_categorical_accuracy: 0.0033333333333333335 val_sparse_top_5_categorical_accuracy: 0.01 val_sparse_top_10_categorical_accuracy: 0.013333333333333334 loss: 3158.7774658203125 sparse_categorical_accuracy: 0.00078125 sparse_categorical_crossentropy: 6.89590904712677 mean_anwer_guess: 486.2 sparse_top_2_categorical_accuracy: 0.003125 sparse_top_5_categorical_accuracy: 0.00625 sparse_top_10_categorical_accuracy: 0.01796875 lr: 0.0003

Restoring model weights from the end of the best epoch

traincallback: INFO     training end: model_1

Epoch 00034: early stopping
Epoch 00034: early stopping

root        : INFO     pre train end 3:05:34.855935
root        : INFO     remaining time 5:54:25.142503

In [26]:

pretrained_model.trainable=True
optimizer=keras.optimizers.Adam(clipnorm=5. , clipvalue=5.,amsgrad=True)
model.compile(optimizer,
                            loss="sparse_categorical_crossentropy",
              metrics=model.metrics
             )
model.summary()

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
image_input (InputLayer)        (None, None, None, 3 0
__________________________________________________________________________________________________
xception (Model)                (None, None, None, 2 20861480    image_input[0][0]
__________________________________________________________________________________________________
conv2d_5 (Conv2D)               (None, None, None, 6 11059800    xception[1][0]
__________________________________________________________________________________________________
global_max_pooling2d_2 (GlobalM (None, 600)          0           conv2d_5[0][0]
__________________________________________________________________________________________________
global_max_pooling2d_1 (GlobalM (None, 2048)         0           xception[1][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 2648)         0           global_max_pooling2d_2[0][0]
                                                                 global_max_pooling2d_1[0][0]
__________________________________________________________________________________________________
alpha_dropout_1 (AlphaDropout)  (None, 2648)         0           concatenate_1[0][0]
__________________________________________________________________________________________________
pre_out (Dense)                 (None, 1200)         3178800     alpha_dropout_1[0][0]
__________________________________________________________________________________________________
out (Dense)                     (None, 1010)         1213010     pre_out[0][0]
==================================================================================================
Total params: 36,313,090
Trainable params: 36,258,562
Non-trainable params: 54,528
__________________________________________________________________________________________________

In [27]:

uptime=datetime.datetime.now()-date_depart
logging.info("main train %s",uptime)

root        : INFO     main train 3:05:37.701667

In [28]:

hist=model.fit_generator(batch_gen(df_train,batch=full_train_batch_size,augmenters=augmenters,dmax=dmax,dmin=dmin,dcrop=dcrop),
                    steps_per_epoch=1280/full_train_batch_size, epochs=epochs, verbose=1,
                     validation_data=val_gen,
                     validation_steps=400/val_batch_size,
                         callbacks=   callbacks
                        )
model.save(fichier_modele)
print(datetime.datetime.now()-date_depart)

traincallback: INFO     training start: model_1

Epoch 1/100000000
320/320 [==============================] - 827s 3s/step - loss: 48.1410 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.8948 - mean_anwer_guess: 448.5000 - sparse_top_2_categorical_accuracy: 0.0039 - sparse_top_5_categorical_accuracy: 0.0086 - sparse_top_10_categorical_accuracy: 0.0180 - val_loss: 12.1783 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9228 - val_mean_anwer_guess: 505.3300 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0000e+00 - val_sparse_top_10_categorical_accuracy: 0.0050

traincallback: INFO     epoch: 0 - val_loss: 12.17833743095398 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.922817440032959 val_mean_anwer_guess: 505.33 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0 val_sparse_top_10_categorical_accuracy: 0.005 loss: 48.14103074967861 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.894803048670292 mean_anwer_guess: 448.5 sparse_top_2_categorical_accuracy: 0.00390625 sparse_top_5_categorical_accuracy: 0.00859375 sparse_top_10_categorical_accuracy: 0.01796875 lr: 0.001

Epoch 2/100000000
320/320 [==============================] - 816s 3s/step - loss: 12.4288 - sparse_categorical_accuracy: 0.0023 - sparse_categorical_crossentropy: 6.8629 - mean_anwer_guess: 441.7938 - sparse_top_2_categorical_accuracy: 0.0063 - sparse_top_5_categorical_accuracy: 0.0156 - sparse_top_10_categorical_accuracy: 0.0250 - val_loss: 12.6009 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9368 - val_mean_anwer_guess: 506.7000 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0025 - val_sparse_top_10_categorical_accuracy: 0.0075

traincallback: INFO     epoch: 1 - val_loss: 12.600927228927612 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.936796236038208 val_mean_anwer_guess: 506.7 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0025 val_sparse_top_10_categorical_accuracy: 0.0075 loss: 12.428814068436623 sparse_categorical_accuracy: 0.00234375 sparse_categorical_crossentropy: 6.8629228070378305 mean_anwer_guess: 441.79375 sparse_top_2_categorical_accuracy: 0.00625 sparse_top_5_categorical_accuracy: 0.015625 sparse_top_10_categorical_accuracy: 0.025 lr: 0.001

Epoch 3/100000000
320/320 [==============================] - 819s 3s/step - loss: 12.6151 - sparse_categorical_accuracy: 0.0047 - sparse_categorical_crossentropy: 6.8430 - mean_anwer_guess: 454.4031 - sparse_top_2_categorical_accuracy: 0.0078 - sparse_top_5_categorical_accuracy: 0.0141 - sparse_top_10_categorical_accuracy: 0.0219 - val_loss: 12.6996 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9636 - val_mean_anwer_guess: 509.5800 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0025 - val_sparse_top_10_categorical_accuracy: 0.0025

traincallback: INFO     epoch: 2 - val_loss: 12.69960337638855 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.9635548448562625 val_mean_anwer_guess: 509.58 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0025 val_sparse_top_10_categorical_accuracy: 0.0025 loss: 12.615091639757157 sparse_categorical_accuracy: 0.0046875 sparse_categorical_crossentropy: 6.842999868094921 mean_anwer_guess: 454.403125 sparse_top_2_categorical_accuracy: 0.0078125 sparse_top_5_categorical_accuracy: 0.0140625 sparse_top_10_categorical_accuracy: 0.021875 lr: 0.001

Epoch 4/100000000
320/320 [==============================] - 798s 2s/step - loss: 12.6081 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.8457 - mean_anwer_guess: 471.2844 - sparse_top_2_categorical_accuracy: 0.0039 - sparse_top_5_categorical_accuracy: 0.0086 - sparse_top_10_categorical_accuracy: 0.0164 - val_loss: 12.3423 - val_sparse_categorical_accuracy: 0.0025 - val_sparse_categorical_crossentropy: 6.9547 - val_mean_anwer_guess: 486.5700 - val_sparse_top_2_categorical_accuracy: 0.0075 - val_sparse_top_5_categorical_accuracy: 0.0100 - val_sparse_top_10_categorical_accuracy: 0.0125

traincallback: INFO     epoch: 3 - val_loss: 12.342320804595948 val_sparse_categorical_accuracy: 0.0025 val_sparse_categorical_crossentropy: 6.954720191955566 val_mean_anwer_guess: 486.57 val_sparse_top_2_categorical_accuracy: 0.0075 val_sparse_top_5_categorical_accuracy: 0.01 val_sparse_top_10_categorical_accuracy: 0.0125 loss: 12.608069211244583 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.84574968367815 mean_anwer_guess: 471.284375 sparse_top_2_categorical_accuracy: 0.00390625 sparse_top_5_categorical_accuracy: 0.00859375 sparse_top_10_categorical_accuracy: 0.01640625 lr: 0.001

Epoch 5/100000000
319/320 [============================>.] - ETA: 2s - loss: 12.6846 - sparse_categorical_accuracy: 0.0024 - sparse_categorical_crossentropy: 6.8427 - mean_anwer_guess: 471.7962 - sparse_top_2_categorical_accuracy: 0.0039 - sparse_top_5_categorical_accuracy: 0.0086 - sparse_top_10_categorical_accuracy: 0.0165

root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3030] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3030] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3031] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3031] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3032] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3032] is not in the [index]'

320/320 [==============================] - 807s 3s/step - loss: 12.6841 - sparse_categorical_accuracy: 0.0023 - sparse_categorical_crossentropy: 6.8420 - mean_anwer_guess: 471.2313 - sparse_top_2_categorical_accuracy: 0.0039 - sparse_top_5_categorical_accuracy: 0.0086 - sparse_top_10_categorical_accuracy: 0.0164 - val_loss: 12.8512 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9653 - val_mean_anwer_guess: 520.8500 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0025 - val_sparse_top_10_categorical_accuracy: 0.0125

traincallback: INFO     epoch: 4 - val_loss: 12.851205167770386 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.9653181552886965 val_mean_anwer_guess: 520.85 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0025 val_sparse_top_10_categorical_accuracy: 0.0125 loss: 12.684120106697083 sparse_categorical_accuracy: 0.00234375 sparse_categorical_crossentropy: 6.842004343867302 mean_anwer_guess: 471.23125 sparse_top_2_categorical_accuracy: 0.00390625 sparse_top_5_categorical_accuracy: 0.00859375 sparse_top_10_categorical_accuracy: 0.01640625 lr: 0.001

Epoch 6/100000000
320/320 [==============================] - 807s 3s/step - loss: 12.7279 - sparse_categorical_accuracy: 7.8125e-04 - sparse_categorical_crossentropy: 6.8252 - mean_anwer_guess: 444.2563 - sparse_top_2_categorical_accuracy: 0.0039 - sparse_top_5_categorical_accuracy: 0.0063 - sparse_top_10_categorical_accuracy: 0.0141 - val_loss: 12.5491 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9697 - val_mean_anwer_guess: 464.0200 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0050 - val_sparse_top_10_categorical_accuracy: 0.0100

Epoch 00006: val_sparse_categorical_accuracy did not improve from 0.00000

traincallback: INFO     epoch: 5 - val_loss: 12.549130172729493 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.969736785888672 val_mean_anwer_guess: 464.02 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.005 val_sparse_top_10_categorical_accuracy: 0.01 loss: 12.727933862805367 sparse_categorical_accuracy: 0.00078125 sparse_categorical_crossentropy: 6.825203020870686 mean_anwer_guess: 444.25625 sparse_top_2_categorical_accuracy: 0.00390625 sparse_top_5_categorical_accuracy: 0.00625 sparse_top_10_categorical_accuracy: 0.0140625 lr: 0.001

Epoch 7/100000000
320/320 [==============================] - 804s 3s/step - loss: 12.7585 - sparse_categorical_accuracy: 0.0023 - sparse_categorical_crossentropy: 6.8074 - mean_anwer_guess: 429.9875 - sparse_top_2_categorical_accuracy: 0.0055 - sparse_top_5_categorical_accuracy: 0.0125 - sparse_top_10_categorical_accuracy: 0.0211 - val_loss: 12.7553 - val_sparse_categorical_accuracy: 0.0025 - val_sparse_categorical_crossentropy: 6.9717 - val_mean_anwer_guess: 526.0300 - val_sparse_top_2_categorical_accuracy: 0.0050 - val_sparse_top_5_categorical_accuracy: 0.0100 - val_sparse_top_10_categorical_accuracy: 0.0225

traincallback: INFO     epoch: 6 - val_loss: 12.755282974243164 val_sparse_categorical_accuracy: 0.0025 val_sparse_categorical_crossentropy: 6.97173378944397 val_mean_anwer_guess: 526.03 val_sparse_top_2_categorical_accuracy: 0.005 val_sparse_top_5_categorical_accuracy: 0.01 val_sparse_top_10_categorical_accuracy: 0.0225 loss: 12.758541065454484 sparse_categorical_accuracy: 0.00234375 sparse_categorical_crossentropy: 6.807421277463436 mean_anwer_guess: 429.9875 sparse_top_2_categorical_accuracy: 0.00546875 sparse_top_5_categorical_accuracy: 0.0125 sparse_top_10_categorical_accuracy: 0.02109375 lr: 0.001

Epoch 8/100000000
320/320 [==============================] - 794s 2s/step - loss: 12.7336 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.8156 - mean_anwer_guess: 451.8687 - sparse_top_2_categorical_accuracy: 0.0016 - sparse_top_5_categorical_accuracy: 0.0086 - sparse_top_10_categorical_accuracy: 0.0148 - val_loss: 12.7757 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9916 - val_mean_anwer_guess: 541.2100 - val_sparse_top_2_categorical_accuracy: 0.0025 - val_sparse_top_5_categorical_accuracy: 0.0050 - val_sparse_top_10_categorical_accuracy: 0.0100

traincallback: INFO     epoch: 7 - val_loss: 12.775742168426513 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.991552557945251 val_mean_anwer_guess: 541.21 val_sparse_top_2_categorical_accuracy: 0.0025 val_sparse_top_5_categorical_accuracy: 0.005 val_sparse_top_10_categorical_accuracy: 0.01 loss: 12.733552849292755 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.815575960278511 mean_anwer_guess: 451.86875 sparse_top_2_categorical_accuracy: 0.0015625 sparse_top_5_categorical_accuracy: 0.00859375 sparse_top_10_categorical_accuracy: 0.01484375 lr: 0.001

Epoch 9/100000000
320/320 [==============================] - 786s 2s/step - loss: 12.7661 - sparse_categorical_accuracy: 7.8125e-04 - sparse_categorical_crossentropy: 6.8123 - mean_anwer_guess: 441.0469 - sparse_top_2_categorical_accuracy: 0.0023 - sparse_top_5_categorical_accuracy: 0.0055 - sparse_top_10_categorical_accuracy: 0.0156 - val_loss: 12.7747 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9789 - val_mean_anwer_guess: 501.0600 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0000e+00 - val_sparse_top_10_categorical_accuracy: 0.0050

traincallback: INFO     epoch: 8 - val_loss: 12.774747228622436 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.978899388313294 val_mean_anwer_guess: 501.06 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0 val_sparse_top_10_categorical_accuracy: 0.005 loss: 12.766103932261467 sparse_categorical_accuracy: 0.00078125 sparse_categorical_crossentropy: 6.812343089282512 mean_anwer_guess: 441.046875 sparse_top_2_categorical_accuracy: 0.00234375 sparse_top_5_categorical_accuracy: 0.00546875 sparse_top_10_categorical_accuracy: 0.015625 lr: 0.001

Epoch 10/100000000
320/320 [==============================] - 781s 2s/step - loss: 12.8401 - sparse_categorical_accuracy: 0.0023 - sparse_categorical_crossentropy: 6.8295 - mean_anwer_guess: 439.9531 - sparse_top_2_categorical_accuracy: 0.0039 - sparse_top_5_categorical_accuracy: 0.0078 - sparse_top_10_categorical_accuracy: 0.0164 - val_loss: 12.8246 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9870 - val_mean_anwer_guess: 507.5800 - val_sparse_top_2_categorical_accuracy: 0.0025 - val_sparse_top_5_categorical_accuracy: 0.0050 - val_sparse_top_10_categorical_accuracy: 0.0075

traincallback: INFO     epoch: 9 - val_loss: 12.824644756317138 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.986988925933838 val_mean_anwer_guess: 507.58 val_sparse_top_2_categorical_accuracy: 0.0025 val_sparse_top_5_categorical_accuracy: 0.005 val_sparse_top_10_categorical_accuracy: 0.0075 loss: 12.840075600147248 sparse_categorical_accuracy: 0.00234375 sparse_categorical_crossentropy: 6.829490113258362 mean_anwer_guess: 439.953125 sparse_top_2_categorical_accuracy: 0.00390625 sparse_top_5_categorical_accuracy: 0.0078125 sparse_top_10_categorical_accuracy: 0.01640625 lr: 0.001

Epoch 11/100000000
320/320 [==============================] - 769s 2s/step - loss: 12.7090 - sparse_categorical_accuracy: 0.0016 - sparse_categorical_crossentropy: 6.7856 - mean_anwer_guess: 438.3531 - sparse_top_2_categorical_accuracy: 0.0023 - sparse_top_5_categorical_accuracy: 0.0086 - sparse_top_10_categorical_accuracy: 0.0172 - val_loss: 12.6598 - val_sparse_categorical_accuracy: 0.0025 - val_sparse_categorical_crossentropy: 6.9976 - val_mean_anwer_guess: 535.0700 - val_sparse_top_2_categorical_accuracy: 0.0025 - val_sparse_top_5_categorical_accuracy: 0.0050 - val_sparse_top_10_categorical_accuracy: 0.0075

traincallback: INFO     epoch: 10 - val_loss: 12.659755830764771 val_sparse_categorical_accuracy: 0.0025 val_sparse_categorical_crossentropy: 6.997620878219604 val_mean_anwer_guess: 535.07 val_sparse_top_2_categorical_accuracy: 0.0025 val_sparse_top_5_categorical_accuracy: 0.005 val_sparse_top_10_categorical_accuracy: 0.0075 loss: 12.708958914875984 sparse_categorical_accuracy: 0.0015625 sparse_categorical_crossentropy: 6.785642342269421 mean_anwer_guess: 438.353125 sparse_top_2_categorical_accuracy: 0.00234375 sparse_top_5_categorical_accuracy: 0.00859375 sparse_top_10_categorical_accuracy: 0.0171875 lr: 0.001

Epoch 12/100000000
320/320 [==============================] - 783s 2s/step - loss: 12.7437 - sparse_categorical_accuracy: 7.8125e-04 - sparse_categorical_crossentropy: 6.7799 - mean_anwer_guess: 440.8656 - sparse_top_2_categorical_accuracy: 0.0016 - sparse_top_5_categorical_accuracy: 0.0070 - sparse_top_10_categorical_accuracy: 0.0148 - val_loss: 12.8805 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 7.0201 - val_mean_anwer_guess: 497.7000 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0050 - val_sparse_top_10_categorical_accuracy: 0.0100

traincallback: INFO     epoch: 11 - val_loss: 12.880471305847168 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 7.020058689117431 val_mean_anwer_guess: 497.7 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.005 val_sparse_top_10_categorical_accuracy: 0.01 loss: 12.743705308437347 sparse_categorical_accuracy: 0.00078125 sparse_categorical_crossentropy: 6.77988765835762 mean_anwer_guess: 440.865625 sparse_top_2_categorical_accuracy: 0.0015625 sparse_top_5_categorical_accuracy: 0.00703125 sparse_top_10_categorical_accuracy: 0.01484375 lr: 0.001

Epoch 13/100000000
319/320 [============================>.] - ETA: 2s - loss: 12.8122 - sparse_categorical_accuracy: 7.8370e-04 - sparse_categorical_crossentropy: 6.7959 - mean_anwer_guess: 490.0690 - sparse_top_2_categorical_accuracy: 0.0016 - sparse_top_5_categorical_accuracy: 0.0063 - sparse_top_10_categorical_accuracy: 0.0172

root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3030] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3030] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3031] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3031] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3032] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3032] is not in the [index]'

320/320 [==============================] - 769s 2s/step - loss: 12.8128 - sparse_categorical_accuracy: 7.8125e-04 - sparse_categorical_crossentropy: 6.7966 - mean_anwer_guess: 490.5000 - sparse_top_2_categorical_accuracy: 0.0016 - sparse_top_5_categorical_accuracy: 0.0063 - sparse_top_10_categorical_accuracy: 0.0172 - val_loss: 12.9069 - val_sparse_categorical_accuracy: 0.0025 - val_sparse_categorical_crossentropy: 6.9953 - val_mean_anwer_guess: 468.6600 - val_sparse_top_2_categorical_accuracy: 0.0050 - val_sparse_top_5_categorical_accuracy: 0.0125 - val_sparse_top_10_categorical_accuracy: 0.0175

traincallback: INFO     epoch: 12 - val_loss: 12.906928291320801 val_sparse_categorical_accuracy: 0.0025 val_sparse_categorical_crossentropy: 6.995328159332275 val_mean_anwer_guess: 468.66 val_sparse_top_2_categorical_accuracy: 0.005 val_sparse_top_5_categorical_accuracy: 0.0125 val_sparse_top_10_categorical_accuracy: 0.0175 loss: 12.812796035408974 sparse_categorical_accuracy: 0.00078125 sparse_categorical_crossentropy: 6.796648205816746 mean_anwer_guess: 490.5 sparse_top_2_categorical_accuracy: 0.0015625 sparse_top_5_categorical_accuracy: 0.00625 sparse_top_10_categorical_accuracy: 0.0171875 lr: 0.001

Epoch 14/100000000
320/320 [==============================] - 796s 2s/step - loss: 12.8612 - sparse_categorical_accuracy: 7.8125e-04 - sparse_categorical_crossentropy: 6.7987 - mean_anwer_guess: 458.7531 - sparse_top_2_categorical_accuracy: 0.0039 - sparse_top_5_categorical_accuracy: 0.0070 - sparse_top_10_categorical_accuracy: 0.0172 - val_loss: 13.0151 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 7.0287 - val_mean_anwer_guess: 462.7200 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0025 - val_sparse_top_10_categorical_accuracy: 0.0075

traincallback: INFO     epoch: 13 - val_loss: 13.015073108673096 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 7.028693838119507 val_mean_anwer_guess: 462.72 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.0025 val_sparse_top_10_categorical_accuracy: 0.0075 loss: 12.86115721464157 sparse_categorical_accuracy: 0.00078125 sparse_categorical_crossentropy: 6.798652696609497 mean_anwer_guess: 458.753125 sparse_top_2_categorical_accuracy: 0.00390625 sparse_top_5_categorical_accuracy: 0.00703125 sparse_top_10_categorical_accuracy: 0.0171875 lr: 0.001

Epoch 00014: ReduceLROnPlateau reducing learning rate to 0.0006000000284984708.
Epoch 15/100000000
320/320 [==============================] - 795s 2s/step - loss: 10.4306 - sparse_categorical_accuracy: 0.0023 - sparse_categorical_crossentropy: 6.7886 - mean_anwer_guess: 453.1719 - sparse_top_2_categorical_accuracy: 0.0031 - sparse_top_5_categorical_accuracy: 0.0055 - sparse_top_10_categorical_accuracy: 0.0211 - val_loss: 10.2737 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9637 - val_mean_anwer_guess: 490.0400 - val_sparse_top_2_categorical_accuracy: 0.0025 - val_sparse_top_5_categorical_accuracy: 0.0025 - val_sparse_top_10_categorical_accuracy: 0.0175

traincallback: INFO     epoch: 14 - val_loss: 10.273672542572022 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.963716921806335 val_mean_anwer_guess: 490.04 val_sparse_top_2_categorical_accuracy: 0.0025 val_sparse_top_5_categorical_accuracy: 0.0025 val_sparse_top_10_categorical_accuracy: 0.0175 loss: 10.430625811219215 sparse_categorical_accuracy: 0.00234375 sparse_categorical_crossentropy: 6.788645024597645 mean_anwer_guess: 453.171875 sparse_top_2_categorical_accuracy: 0.003125 sparse_top_5_categorical_accuracy: 0.00546875 sparse_top_10_categorical_accuracy: 0.02109375 lr: 0.0006

Epoch 16/100000000
320/320 [==============================] - 784s 2s/step - loss: 10.3938 - sparse_categorical_accuracy: 0.0023 - sparse_categorical_crossentropy: 6.7822 - mean_anwer_guess: 432.9562 - sparse_top_2_categorical_accuracy: 0.0047 - sparse_top_5_categorical_accuracy: 0.0070 - sparse_top_10_categorical_accuracy: 0.0148 - val_loss: 10.3957 - val_sparse_categorical_accuracy: 0.0025 - val_sparse_categorical_crossentropy: 6.9853 - val_mean_anwer_guess: 480.5500 - val_sparse_top_2_categorical_accuracy: 0.0075 - val_sparse_top_5_categorical_accuracy: 0.0075 - val_sparse_top_10_categorical_accuracy: 0.0175

traincallback: INFO     epoch: 15 - val_loss: 10.395728225708007 val_sparse_categorical_accuracy: 0.0025 val_sparse_categorical_crossentropy: 6.98533519744873 val_mean_anwer_guess: 480.55 val_sparse_top_2_categorical_accuracy: 0.0075 val_sparse_top_5_categorical_accuracy: 0.0075 val_sparse_top_10_categorical_accuracy: 0.0175 loss: 10.393761798739433 sparse_categorical_accuracy: 0.00234375 sparse_categorical_crossentropy: 6.782198789715767 mean_anwer_guess: 432.95625 sparse_top_2_categorical_accuracy: 0.0046875 sparse_top_5_categorical_accuracy: 0.00703125 sparse_top_10_categorical_accuracy: 0.01484375 lr: 0.0006

Epoch 17/100000000
320/320 [==============================] - 795s 2s/step - loss: 10.3923 - sparse_categorical_accuracy: 0.0031 - sparse_categorical_crossentropy: 6.7879 - mean_anwer_guess: 436.1344 - sparse_top_2_categorical_accuracy: 0.0031 - sparse_top_5_categorical_accuracy: 0.0117 - sparse_top_10_categorical_accuracy: 0.0172 - val_loss: 10.5543 - val_sparse_categorical_accuracy: 0.0025 - val_sparse_categorical_crossentropy: 6.9715 - val_mean_anwer_guess: 510.8900 - val_sparse_top_2_categorical_accuracy: 0.0025 - val_sparse_top_5_categorical_accuracy: 0.0025 - val_sparse_top_10_categorical_accuracy: 0.0050

traincallback: INFO     epoch: 16 - val_loss: 10.554329509735107 val_sparse_categorical_accuracy: 0.0025 val_sparse_categorical_crossentropy: 6.971497311592102 val_mean_anwer_guess: 510.89 val_sparse_top_2_categorical_accuracy: 0.0025 val_sparse_top_5_categorical_accuracy: 0.0025 val_sparse_top_10_categorical_accuracy: 0.005 loss: 10.392290279269218 sparse_categorical_accuracy: 0.003125 sparse_categorical_crossentropy: 6.787858499586582 mean_anwer_guess: 436.134375 sparse_top_2_categorical_accuracy: 0.003125 sparse_top_5_categorical_accuracy: 0.01171875 sparse_top_10_categorical_accuracy: 0.0171875 lr: 0.0006

Epoch 18/100000000
320/320 [==============================] - 785s 2s/step - loss: 10.3798 - sparse_categorical_accuracy: 0.0039 - sparse_categorical_crossentropy: 6.7880 - mean_anwer_guess: 453.6187 - sparse_top_2_categorical_accuracy: 0.0078 - sparse_top_5_categorical_accuracy: 0.0117 - sparse_top_10_categorical_accuracy: 0.0187 - val_loss: 10.6460 - val_sparse_categorical_accuracy: 0.0000e+00 - val_sparse_categorical_crossentropy: 6.9940 - val_mean_anwer_guess: 533.2100 - val_sparse_top_2_categorical_accuracy: 0.0000e+00 - val_sparse_top_5_categorical_accuracy: 0.0050 - val_sparse_top_10_categorical_accuracy: 0.0100

root        : INFO     end date
traincallback: INFO     epoch: 17 - val_loss: 10.646020088195801 val_sparse_categorical_accuracy: 0.0 val_sparse_categorical_crossentropy: 6.994029064178466 val_mean_anwer_guess: 533.21 val_sparse_top_2_categorical_accuracy: 0.0 val_sparse_top_5_categorical_accuracy: 0.005 val_sparse_top_10_categorical_accuracy: 0.01 loss: 10.379760584235191 sparse_categorical_accuracy: 0.00390625 sparse_categorical_crossentropy: 6.788010680675507 mean_anwer_guess: 453.61875 sparse_top_2_categorical_accuracy: 0.0078125 sparse_top_5_categorical_accuracy: 0.01171875 sparse_top_10_categorical_accuracy: 0.01875 lr: 0.0006
traincallback: INFO     training end: model_1

7:04:41.866862

In [29]:

uptime=datetime.datetime.now()-date_depart
logging.info("post train %s",uptime)
logging.info("remaining time %s",datetime.timedelta(hours=9)+date_depart-datetime.datetime.now())

root        : INFO     post train 7:04:44.719416
root        : INFO     remaining time 1:55:15.274879

In [30]:

if (datetime.datetime.now()-date_depart)<datetime.timedelta(hours=8,minutes=30):
    fig=plt.figure(figsize=(15,7))
    plt.subplot("211")
    train_history=hist.history
    histories=[hist_pre.history,hist.history,hist_post]
    for k in train_history.keys():
        train_history[k]=[]
        for h in histories:
            train_history[k]+=h.get(k,[])

    for k in train_history.keys():
        if "acc" in k:
            plt.plot(train_history[k],label=k)
    plt.ylim(ymin=0.95,ymax=1.0)
    plt.legend()
    plt.subplot("212")
    plt.yscale("log")
    for k in train_history.keys():
        if "loss" in k:
            plt.plot(train_history[k],label=k)
    plt.legend()

    plt.ylim(ymax=0.8)
    fig.savefig("graph.png",dpi=200,transparent=False)
    #IPython.display.Image(filename="graph.png")

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-30-6e6b8d3c5db1> in <module>()
      3     plt.subplot("211")
      4     train_history=hist.history
----> 5     histories=[hist_pre.history,hist.history,hist_post]
      6     for k in train_history.keys():
      7         train_history[k]=[]

NameError: name 'hist_post' is not defined

In [31]:

if (datetime.datetime.now()-date_depart)<datetime.timedelta(hours=7,minutes=30):
    for m,e in zip(model.metrics_names,
                   model.evaluate_generator(batch_gen(df_val,batch=64),steps=50, verbose=1) ):
                   print (m,e)

45/50 [==========================>...] - ETA: 12s

root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3030] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3030] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3031] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3031] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3032] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3032] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3033] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3033] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3034] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3034] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3035] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3035] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3036] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3036] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3037] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3037] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3038] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3038] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3039] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3039] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3040] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3040] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3041] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3041] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3042] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3042] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3043] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3043] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3044] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3044] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3045] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3045] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3046] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3046] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3047] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3047] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3048] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3048] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3049] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3049] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3050] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3050] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3051] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3051] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3052] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3052] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3053] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3053] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3054] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3054] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3055] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3055] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3056] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3056] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3057] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3057] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3058] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3058] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3059] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3059] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3060] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3060] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3061] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3061] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3062] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3062] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3063] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3063] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3064] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3064] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3065] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3065] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3066] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3066] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3067] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3067] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3068] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3068] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3069] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3069] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3070] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3070] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3071] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3071] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3072] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3072] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3073] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3073] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3074] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3074] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3075] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3075] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3076] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3076] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3077] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3077] is not in the [index]'
root        : ERROR    imgaug_batch_gen
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3078] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-16-814ea07d8116>", line 27, in imgaug_batch_gen
    if df.loc[j+i,"req"] is None:
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 998, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [3078] is not in the [index]'

50/50 [==============================] - 127s 3s/step
loss 12.688553867340088
sparse_categorical_accuracy 0.0009375
sparse_categorical_crossentropy 7.004177312850953
mean_anwer_guess 465.0
sparse_top_2_categorical_accuracy 0.001875
sparse_top_5_categorical_accuracy 0.004375
sparse_top_10_categorical_accuracy 0.0090625

In [32]:

if (datetime.datetime.now()-date_depart)<datetime.timedelta(hours=7,minutes=30):

    ann_file = '../input/inaturalist-2019-fgvc6/test2019.json'
    with open(ann_file) as data_file:
            test_anns = json.load(data_file)

    df_test=pd.DataFrame(test_anns['images'])[["id","file_name"]]
    df_test

In [33]:

test_batch=50

if (datetime.datetime.now()-date_depart)<datetime.timedelta(hours=7,minutes=0):
    test_datagen = ImageDataGenerator(rescale=1./255)
    test_generator = test_datagen.flow_from_dataframe(

            dataframe=df_test,

            directory = "../input/inaturalist-2019-fgvc6/test2019",
            x_col="file_name",
            target_size = (dcrop,dcrop),
            batch_size = test_batch,
            shuffle = False,
            class_mode = None
            )
    lengen=len(test_generator.filenames)
    #math.gcd(lengen)
    predict=model.predict_generator(test_generator, steps = len(test_generator.filenames)//test_batch+1,verbose=1)
    sub=np.argsort(predict)[:lengen,-10:]
    sub=np.flip(sub,1)
    df_test["predicted"]=[" ".join(str(n) for n in  pred  )for pred in sub  ]
    df_test["preds"]=df_test["predicted"]

    df_test[["id","preds", 'predicted']].to_csv("submission.csv", index=False)
    df_test

In [34]:

uptime=datetime.datetime.now()-date_depart
logging.info("end %s",uptime)
logging.info("remaining time %s",datetime.timedelta(hours=9)+date_depart-datetime.datetime.now())

root        : INFO     end 7:06:57.295425
root        : INFO     remaining time 1:53:02.703319

iNaturalist 2019 EDA + DL

From: https://www.kaggle.com/praxitelisk/inaturalist-2019-eda-dl

Author: Praxitelis-Nikolaos Kouroupetroglou

Score: 0.78337

iNaturalist 2019 EDA + DL¶

Kudos and main ideas / reference:

hsinwenchang/keras-data-augmentation-visualize

ateplyuk/inat2019-starter-keras-efficientnet

image-source

In [1]:

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import os
print(os.listdir("../input"))

import os
import cv2
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import json
from keras.models import Sequential, Model
from keras.layers import Dense, Flatten, Activation, Dropout, GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers, applications
from keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping

['train2019.json', 'train_val2019', 'val2019.json', 'test2019', 'kaggle_sample_submission.csv', 'test2019.json']

Using TensorFlow backend.

Train data¶

In [2]:

ann_file = '../input/train2019.json'
with open(ann_file) as data_file:
        train_anns = json.load(data_file)

In [3]:

train_anns_df = pd.DataFrame(train_anns['annotations'])[['image_id','category_id']]
train_img_df = pd.DataFrame(train_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
df_train_file_cat = pd.merge(train_img_df, train_anns_df, on='image_id')
df_train_file_cat['category_id']=df_train_file_cat['category_id'].astype(str)
df_train_file_cat.head()

Out[3]:

| image_id | file_name | category_id
0 | 0 | train_val2019/Plants/400/d1322d13ccd856eb4236c... | 400
1 | 1 | train_val2019/Plants/570/15edbc1e2ef000d8ace48... | 570
2 | 2 | train_val2019/Reptiles/167/c87a32e8927cbf4f06d... | 167
3 | 3 | train_val2019/Birds/254/9fcdd1d37e96d8fd94dfdc... | 254
4 | 4 | train_val2019/Plants/739/ffa06f951e99de9d220ae... | 739

In [4]:

df_train_file_cat.shape

Out[4]:

(265213, 3)

In [5]:

len(df_train_file_cat['category_id'].unique())

Out[5]:

1010

In [6]:

# Example of images for category_id = 400
img_names = df_train_file_cat[df_train_file_cat['category_id']=='400']['file_name'][:30]

plt.figure(figsize=[15,15])
i = 1
for img_name in img_names:
    img = cv2.imread("../input/train_val2019/%s" % img_name)[...,[2, 1, 0]]
    plt.subplot(6, 5, i)
    plt.imshow(img)
    i += 1
plt.show()

Validation data¶

In [7]:

valid_ann_file = '../input/val2019.json'
with open(valid_ann_file) as data_file:
        valid_anns = json.load(data_file)

In [8]:

valid_anns_df = pd.DataFrame(valid_anns['annotations'])[['image_id','category_id']]
valid_anns_df.head()

Out[8]:

| image_id | category_id
0 | 265213 | 644
1 | 265214 | 597
2 | 265215 | 883
3 | 265216 | 300
4 | 265217 | 881

In [9]:

valid_img_df = pd.DataFrame(valid_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
valid_img_df.head()

Out[9]:

| image_id | file_name
0 | 265213 | train_val2019/Plants/644/716a69838526f3ada3b2f...
1 | 265214 | train_val2019/Plants/597/0942cc64d2e759c5ee059...
2 | 265215 | train_val2019/Plants/883/acfdbfd9fa675f1c84558...
3 | 265216 | train_val2019/Birds/300/5f3194ff536c7dd31d80b7...
4 | 265217 | train_val2019/Plants/881/76acaf0b2841f91982d21...

In [10]:

df_valid_file_cat = pd.merge(valid_img_df, valid_anns_df, on='image_id')
df_valid_file_cat['category_id']=df_valid_file_cat['category_id'].astype(str)
df_valid_file_cat.head()

Out[10]:

| image_id | file_name | category_id
0 | 265213 | train_val2019/Plants/644/716a69838526f3ada3b2f... | 644
1 | 265214 | train_val2019/Plants/597/0942cc64d2e759c5ee059... | 597
2 | 265215 | train_val2019/Plants/883/acfdbfd9fa675f1c84558... | 883
3 | 265216 | train_val2019/Birds/300/5f3194ff536c7dd31d80b7... | 300
4 | 265217 | train_val2019/Plants/881/76acaf0b2841f91982d21... | 881

In [11]:

nb_classes = 1010
batch_size = 128
img_size = 150
nb_epochs = 65

Oversampling¶

In [12]:

#from imblearn.over_sampling import RandomOverSampler

#ros = RandomOverSampler(random_state=0)
#X_resampled, y_resampled = ros.fit_resample(df_train_file_cat[["image_id", "file_name"]], df_train_file_cat["category_id"])

#train_df = pd.DataFrame(X_resampled, columns=["image_id", "file_name"])
#train_df["category_id"] = y_resampled

here I applied Data Augmentation technic from Udacity as following:

random 45 degree rotation

random zoom of up to 50%

random horizontal flip

width shift of 0.15

height shfit of 0.15

In [13]:

%%time
train_datagen=ImageDataGenerator(rescale=1./255, rotation_range=45,
                    width_shift_range=.15,
                    height_shift_range=.15,
                    horizontal_flip=True,
                    zoom_range=0.5)

train_generator=train_datagen.flow_from_dataframe(
    dataframe=df_train_file_cat,
    directory="../input/train_val2019",
    x_col="file_name",
    y_col="category_id",
    batch_size=batch_size,
    shuffle=True,
    class_mode="categorical",
    target_size=(img_size,img_size))

Found 265213 images belonging to 1010 classes.
CPU times: user 5.88 s, sys: 7.94 s, total: 13.8 s
Wall time: 1min 20s

In [14]:

# udacity_intro_to_tensorflow_for_deep_learning/l05c04_exercise_flowers_with_data_augmentation_solution.ipynb#scrollTo=jqb9OGoVKIOi
# This function will plot images in the form of a grid with 1 row and 5 columns where images are placed in each column.
def plotImages(images_arr):
    fig, axes = plt.subplots(1, 5, figsize=(20,20))
    axes = axes.flatten()
    for img, ax in zip( images_arr, axes):
        ax.imshow(img)
    plt.tight_layout()
    plt.show()

augmented_images = [train_generator[0][0][0] for i in range(5)]
plotImages(augmented_images)

In [15]:

%%time
test_datagen = ImageDataGenerator(rescale=1./255)

valid_generator=test_datagen.flow_from_dataframe(
    dataframe=df_valid_file_cat,
    directory="../input/train_val2019",
    x_col="file_name",
    y_col="category_id",
    batch_size=batch_size,
    shuffle=True,
    class_mode="categorical",
    target_size=(img_size,img_size))

Found 3030 images belonging to 1010 classes.
CPU times: user 72 ms, sys: 68 ms, total: 140 ms
Wall time: 829 ms

In [16]:

import gc
gc.collect();

Model¶

In [17]:

#from keras.applications.vgg16 import VGG16
#from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_resnet_v2 import InceptionResNetV2
#from keras.applications.nasnet import NASNetLarge
#from keras.applications.densenet import DenseNet121

#model = VGG16(weights='imagenet', include_top=False, input_shape=(img_size, img_size, 3))
model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(img_size, img_size, 3))
model_name = "InceptionResNetV2"

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.7/inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5
219062272/219055592 [==============================] - 2s 0us/step

In [18]:

#Adding custom layers
model_final = Sequential()
model_final.add(model)
model_final.add(Flatten())
model_final.add(Dense(1024, activation='relu'))
model_final.add(Dropout(0.5))
model_final.add(Dense(nb_classes, activation='softmax'))

model_final.compile(optimizers.rmsprop(lr=0.0001, decay=1e-5),loss='categorical_crossentropy',metrics=['accuracy'])

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.

In [19]:

#Callbacks

checkpoint = ModelCheckpoint(model_name, monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')

In [20]:

model_final.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
inception_resnet_v2 (Model)  (None, 3, 3, 1536)        54336736
_________________________________________________________________
flatten_1 (Flatten)          (None, 13824)             0
_________________________________________________________________
dense_1 (Dense)              (None, 1024)              14156800
_________________________________________________________________
dropout_1 (Dropout)          (None, 1024)              0
_________________________________________________________________
dense_2 (Dense)              (None, 1010)              1035250
=================================================================
Total params: 69,528,786
Trainable params: 69,468,242
Non-trainable params: 60,544
_________________________________________________________________

In [21]:

%%time
history = model_final.fit_generator(generator=train_generator,
                    steps_per_epoch=80,
                    validation_data=valid_generator,
                    validation_steps=40,
                    epochs=nb_epochs,
                    callbacks = [checkpoint, early],
                    verbose=1)

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Epoch 1/65
80/80 [==============================] - 356s 4s/step - loss: 6.9083 - acc: 0.0037 - val_loss: 6.9055 - val_acc: 0.0045

Epoch 00001: val_loss improved from inf to 6.90547, saving model to InceptionResNetV2
Epoch 2/65
80/80 [==============================] - 284s 4s/step - loss: 6.6840 - acc: 0.0079 - val_loss: 6.7927 - val_acc: 0.0097

Epoch 00002: val_loss improved from 6.90547 to 6.79267, saving model to InceptionResNetV2
Epoch 3/65
80/80 [==============================] - 286s 4s/step - loss: 6.3318 - acc: 0.0234 - val_loss: 6.5874 - val_acc: 0.0216

Epoch 00003: val_loss improved from 6.79267 to 6.58745, saving model to InceptionResNetV2
Epoch 4/65
80/80 [==============================] - 287s 4s/step - loss: 5.9367 - acc: 0.0368 - val_loss: 6.3406 - val_acc: 0.0353

Epoch 00004: val_loss improved from 6.58745 to 6.34062, saving model to InceptionResNetV2
Epoch 5/65
80/80 [==============================] - 287s 4s/step - loss: 5.6083 - acc: 0.0501 - val_loss: 6.2182 - val_acc: 0.0463

Epoch 00005: val_loss improved from 6.34062 to 6.21818, saving model to InceptionResNetV2
Epoch 6/65
80/80 [==============================] - 285s 4s/step - loss: 5.3328 - acc: 0.0677 - val_loss: 5.9901 - val_acc: 0.0550

Epoch 00006: val_loss improved from 6.21818 to 5.99013, saving model to InceptionResNetV2
Epoch 7/65
80/80 [==============================] - 286s 4s/step - loss: 5.1518 - acc: 0.0766 - val_loss: 5.8476 - val_acc: 0.0670

Epoch 00007: val_loss improved from 5.99013 to 5.84757, saving model to InceptionResNetV2
Epoch 8/65
80/80 [==============================] - 287s 4s/step - loss: 4.9322 - acc: 0.0919 - val_loss: 5.6232 - val_acc: 0.0764

Epoch 00008: val_loss improved from 5.84757 to 5.62316, saving model to InceptionResNetV2
Epoch 9/65
80/80 [==============================] - 284s 4s/step - loss: 4.7662 - acc: 0.1095 - val_loss: 5.4944 - val_acc: 0.0862

Epoch 00009: val_loss improved from 5.62316 to 5.49438, saving model to InceptionResNetV2
Epoch 10/65
80/80 [==============================] - 288s 4s/step - loss: 4.6913 - acc: 0.1072 - val_loss: 5.4311 - val_acc: 0.0861

Epoch 00010: val_loss improved from 5.49438 to 5.43106, saving model to InceptionResNetV2
Epoch 11/65
80/80 [==============================] - 287s 4s/step - loss: 4.5764 - acc: 0.1138 - val_loss: 5.3167 - val_acc: 0.0905

Epoch 00011: val_loss improved from 5.43106 to 5.31670, saving model to InceptionResNetV2
Epoch 12/65
80/80 [==============================] - 284s 4s/step - loss: 4.4179 - acc: 0.1352 - val_loss: 5.2237 - val_acc: 0.1088

Epoch 00012: val_loss improved from 5.31670 to 5.22368, saving model to InceptionResNetV2
Epoch 13/65
80/80 [==============================] - 287s 4s/step - loss: 4.3519 - acc: 0.1370 - val_loss: 5.1422 - val_acc: 0.1119

Epoch 00013: val_loss improved from 5.22368 to 5.14220, saving model to InceptionResNetV2
Epoch 14/65
80/80 [==============================] - 288s 4s/step - loss: 4.2697 - acc: 0.1420 - val_loss: 5.0044 - val_acc: 0.1267

Epoch 00014: val_loss improved from 5.14220 to 5.00435, saving model to InceptionResNetV2
Epoch 15/65
80/80 [==============================] - 286s 4s/step - loss: 4.1748 - acc: 0.1526 - val_loss: 5.1474 - val_acc: 0.1259

Epoch 00015: val_loss did not improve from 5.00435
Epoch 16/65
80/80 [==============================] - 285s 4s/step - loss: 4.1067 - acc: 0.1642 - val_loss: 4.9083 - val_acc: 0.1314

Epoch 00016: val_loss improved from 5.00435 to 4.90827, saving model to InceptionResNetV2
Epoch 17/65
80/80 [==============================] - 291s 4s/step - loss: 4.0509 - acc: 0.1677 - val_loss: 4.8343 - val_acc: 0.1340

Epoch 00017: val_loss improved from 4.90827 to 4.83430, saving model to InceptionResNetV2
Epoch 18/65
80/80 [==============================] - 286s 4s/step - loss: 3.9883 - acc: 0.1786 - val_loss: 4.7462 - val_acc: 0.1362

Epoch 00018: val_loss improved from 4.83430 to 4.74621, saving model to InceptionResNetV2
Epoch 19/65
80/80 [==============================] - 287s 4s/step - loss: 3.9329 - acc: 0.1801 - val_loss: 4.7836 - val_acc: 0.1442

Epoch 00019: val_loss did not improve from 4.74621
Epoch 20/65
80/80 [==============================] - 286s 4s/step - loss: 3.8636 - acc: 0.1883 - val_loss: 4.7084 - val_acc: 0.1481

Epoch 00020: val_loss improved from 4.74621 to 4.70838, saving model to InceptionResNetV2
Epoch 21/65
80/80 [==============================] - 284s 4s/step - loss: 3.8378 - acc: 0.1934 - val_loss: 4.6525 - val_acc: 0.1531

Epoch 00021: val_loss improved from 4.70838 to 4.65249, saving model to InceptionResNetV2
Epoch 22/65
80/80 [==============================] - 288s 4s/step - loss: 3.7650 - acc: 0.2045 - val_loss: 4.6476 - val_acc: 0.1591

Epoch 00022: val_loss improved from 4.65249 to 4.64760, saving model to InceptionResNetV2
Epoch 23/65
80/80 [==============================] - 284s 4s/step - loss: 3.7396 - acc: 0.2101 - val_loss: 4.5605 - val_acc: 0.1606

Epoch 00023: val_loss improved from 4.64760 to 4.56049, saving model to InceptionResNetV2
Epoch 24/65
80/80 [==============================] - 282s 4s/step - loss: 3.6721 - acc: 0.2151 - val_loss: 4.5303 - val_acc: 0.1595

Epoch 00024: val_loss improved from 4.56049 to 4.53029, saving model to InceptionResNetV2
Epoch 25/65
80/80 [==============================] - 287s 4s/step - loss: 3.6266 - acc: 0.2170 - val_loss: 4.3988 - val_acc: 0.1705

Epoch 00025: val_loss improved from 4.53029 to 4.39878, saving model to InceptionResNetV2
Epoch 26/65
80/80 [==============================] - 285s 4s/step - loss: 3.5826 - acc: 0.2298 - val_loss: 4.5749 - val_acc: 0.1581

Epoch 00026: val_loss did not improve from 4.39878
Epoch 27/65
80/80 [==============================] - 280s 4s/step - loss: 3.5306 - acc: 0.2341 - val_loss: 4.5384 - val_acc: 0.1757

Epoch 00027: val_loss did not improve from 4.39878
Epoch 28/65
80/80 [==============================] - 282s 4s/step - loss: 3.4787 - acc: 0.2397 - val_loss: 4.4502 - val_acc: 0.1753

Epoch 00028: val_loss did not improve from 4.39878
Epoch 29/65
80/80 [==============================] - 285s 4s/step - loss: 3.4512 - acc: 0.2407 - val_loss: 4.3671 - val_acc: 0.1785

Epoch 00029: val_loss improved from 4.39878 to 4.36713, saving model to InceptionResNetV2
Epoch 30/65
80/80 [==============================] - 284s 4s/step - loss: 3.4433 - acc: 0.2481 - val_loss: 4.2958 - val_acc: 0.1775

Epoch 00030: val_loss improved from 4.36713 to 4.29582, saving model to InceptionResNetV2
Epoch 31/65
80/80 [==============================] - 286s 4s/step - loss: 3.4167 - acc: 0.2473 - val_loss: 4.3397 - val_acc: 0.1910

Epoch 00031: val_loss did not improve from 4.29582
Epoch 32/65
80/80 [==============================] - 285s 4s/step - loss: 3.3980 - acc: 0.2479 - val_loss: 4.1655 - val_acc: 0.1946

Epoch 00032: val_loss improved from 4.29582 to 4.16553, saving model to InceptionResNetV2
Epoch 33/65
80/80 [==============================] - 286s 4s/step - loss: 3.3893 - acc: 0.2574 - val_loss: 4.4498 - val_acc: 0.1837

Epoch 00033: val_loss did not improve from 4.16553
Epoch 34/65
80/80 [==============================] - 287s 4s/step - loss: 3.3685 - acc: 0.2514 - val_loss: 4.1476 - val_acc: 0.2044

Epoch 00034: val_loss improved from 4.16553 to 4.14755, saving model to InceptionResNetV2
Epoch 35/65
80/80 [==============================] - 285s 4s/step - loss: 3.3437 - acc: 0.2637 - val_loss: 4.1630 - val_acc: 0.1980

Epoch 00035: val_loss did not improve from 4.14755
Epoch 36/65
80/80 [==============================] - 282s 4s/step - loss: 3.2950 - acc: 0.2611 - val_loss: 4.1488 - val_acc: 0.1938

Epoch 00036: val_loss did not improve from 4.14755
Epoch 37/65
80/80 [==============================] - 282s 4s/step - loss: 3.3103 - acc: 0.2603 - val_loss: 4.0952 - val_acc: 0.2135

Epoch 00037: val_loss improved from 4.14755 to 4.09521, saving model to InceptionResNetV2
Epoch 38/65
80/80 [==============================] - 282s 4s/step - loss: 3.2796 - acc: 0.2687 - val_loss: 4.1278 - val_acc: 0.2103

Epoch 00038: val_loss did not improve from 4.09521
Epoch 39/65
80/80 [==============================] - 282s 4s/step - loss: 3.2746 - acc: 0.2704 - val_loss: 4.2665 - val_acc: 0.2109

Epoch 00039: val_loss did not improve from 4.09521
Epoch 40/65
80/80 [==============================] - 283s 4s/step - loss: 3.2272 - acc: 0.2821 - val_loss: 4.1167 - val_acc: 0.2044

Epoch 00040: val_loss did not improve from 4.09521
Epoch 41/65
80/80 [==============================] - 285s 4s/step - loss: 3.2223 - acc: 0.2833 - val_loss: 4.0089 - val_acc: 0.2238

Epoch 00041: val_loss improved from 4.09521 to 4.00890, saving model to InceptionResNetV2
Epoch 42/65
80/80 [==============================] - 283s 4s/step - loss: 3.2335 - acc: 0.2779 - val_loss: 3.9840 - val_acc: 0.2186

Epoch 00042: val_loss improved from 4.00890 to 3.98400, saving model to InceptionResNetV2
Epoch 43/65
80/80 [==============================] - 287s 4s/step - loss: 3.1826 - acc: 0.2877 - val_loss: 4.1019 - val_acc: 0.2174

Epoch 00043: val_loss did not improve from 3.98400
Epoch 44/65
80/80 [==============================] - 283s 4s/step - loss: 3.1778 - acc: 0.2867 - val_loss: 4.0194 - val_acc: 0.2276

Epoch 00044: val_loss did not improve from 3.98400
Epoch 45/65
80/80 [==============================] - 284s 4s/step - loss: 3.1890 - acc: 0.2890 - val_loss: 4.0372 - val_acc: 0.2282

Epoch 00045: val_loss did not improve from 3.98400
Epoch 46/65
80/80 [==============================] - 285s 4s/step - loss: 3.1507 - acc: 0.2865 - val_loss: 4.0892 - val_acc: 0.2212

Epoch 00046: val_loss did not improve from 3.98400
Epoch 47/65
80/80 [==============================] - 290s 4s/step - loss: 3.1302 - acc: 0.2928 - val_loss: 4.0334 - val_acc: 0.2188

Epoch 00047: val_loss did not improve from 3.98400
Epoch 00047: early stopping
CPU times: user 4h 9min 19s, sys: 19min 9s, total: 4h 28min 29s
Wall time: 3h 45min 51s

In [22]:

import gc
gc.collect();

In [23]:

with open('history.json', 'w') as f:
    json.dump(history.history, f)

history_df = pd.DataFrame(history.history)
history_df[['loss', 'val_loss']].plot()
history_df[['acc', 'val_acc']].plot()

Out[23]:

<matplotlib.axes._subplots.AxesSubplot at 0x7f84f98710b8>

In [24]:

test_ann_file = '../input/test2019.json'
with open(test_ann_file) as data_file:
        test_anns = json.load(data_file)

In [25]:

test_img_df = pd.DataFrame(test_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
test_img_df.head()

Out[25]:

| image_id | file_name
0 | 268243 | test2019/e295f3c7046b1f1e80c0301401324aa9.jpg
1 | 268244 | test2019/ad3dcbb6846ed0b4dab58d7b1a4210ba.jpg
2 | 268245 | test2019/e697be8e296b4b140cff4f96f85c364f.jpg
3 | 268246 | test2019/7e7ba55e6aa26ba99e814d63b15d0121.jpg
4 | 268247 | test2019/6cb6372079d23702511c06923970f13f.jpg

In [26]:

%%time

test_generator = test_datagen.flow_from_dataframe(

        dataframe=test_img_df,

        directory = "../input/test2019",
        x_col="file_name",
        target_size = (img_size,img_size),
        batch_size = 1,
        shuffle = False,
        class_mode = None
        )

Found 35350 images.
CPU times: user 800 ms, sys: 708 ms, total: 1.51 s
Wall time: 11.2 s

Prediction¶

In [27]:

gc.collect();

In [28]:

%%time
predict_valid=model_final.predict_generator(valid_generator, steps = np.ceil(valid_generator.samples / valid_generator.batch_size), verbose=1)

24/24 [==============================] - 41s 2s/step
CPU times: user 47.7 s, sys: 2.04 s, total: 49.7 s
Wall time: 40.8 s

In [29]:

predict_valid_class=np.argmax(predict_valid,axis=1)

In [30]:

len(predict_valid_class)

Out[30]:

3030

In [31]:

from sklearn.metrics import classification_report

print(classification_report(valid_generator.classes, predict_valid_class))

              precision    recall  f1-score   support

           0       0.00      0.00      0.00         3
           1       0.00      0.00      0.00         3
           2       0.00      0.00      0.00         3
           3       0.00      0.00      0.00         3
           4       0.00      0.00      0.00         3
           5       0.00      0.00      0.00         3
           6       0.00      0.00      0.00         3
           7       0.00      0.00      0.00         3
           8       0.00      0.00      0.00         3
           9       0.00      0.00      0.00         3
          10       0.00      0.00      0.00         3
          11       0.00      0.00      0.00         3
          12       0.00      0.00      0.00         3
          13       0.00      0.00      0.00         3
          14       0.00      0.00      0.00         3
          15       0.00      0.00      0.00         3
          16       0.00      0.00      0.00         3
          17       0.00      0.00      0.00         3
          18       0.00      0.00      0.00         3
          19       0.00      0.00      0.00         3
          20       0.00      0.00      0.00         3
          21       0.00      0.00      0.00         3
          22       0.00      0.00      0.00         3
          23       0.00      0.00      0.00         3
          24       0.00      0.00      0.00         3
          25       0.00      0.00      0.00         3
          26       0.00      0.00      0.00         3
          27       0.00      0.00      0.00         3
          28       0.00      0.00      0.00         3
          29       0.00      0.00      0.00         3
          30       0.00      0.00      0.00         3
          31       0.00      0.00      0.00         3
          32       0.00      0.00      0.00         3
          33       0.00      0.00      0.00         3
          34       0.00      0.00      0.00         3
          35       0.00      0.00      0.00         3
          36       0.00      0.00      0.00         3
          37       0.00      0.00      0.00         3
          38       0.00      0.00      0.00         3
          39       0.00      0.00      0.00         3
          40       0.00      0.00      0.00         3
          41       0.00      0.00      0.00         3
          42       0.00      0.00      0.00         3
          43       0.00      0.00      0.00         3
          44       0.00      0.00      0.00         3
          45       0.00      0.00      0.00         3
          46       0.00      0.00      0.00         3
          47       0.00      0.00      0.00         3
          48       0.00      0.00      0.00         3
          49       0.00      0.00      0.00         3
          50       0.00      0.00      0.00         3
          51       0.00      0.00      0.00         3
          52       0.00      0.00      0.00         3
          53       0.00      0.00      0.00         3
          54       0.00      0.00      0.00         3
          55       0.00      0.00      0.00         3
          56       0.00      0.00      0.00         3
          57       0.00      0.00      0.00         3
          58       0.00      0.00      0.00         3
          59       0.00      0.00      0.00         3
          60       0.00      0.00      0.00         3
          61       0.00      0.00      0.00         3
          62       0.00      0.00      0.00         3
          63       0.00      0.00      0.00         3
          64       0.00      0.00      0.00         3
          65       0.00      0.00      0.00         3
          66       0.00      0.00      0.00         3
          67       0.00      0.00      0.00         3
          68       0.00      0.00      0.00         3
          69       0.00      0.00      0.00         3
          70       0.00      0.00      0.00         3
          71       0.00      0.00      0.00         3
          72       0.00      0.00      0.00         3
          73       0.00      0.00      0.00         3
          74       0.00      0.00      0.00         3
          75       0.00      0.00      0.00         3
          76       0.00      0.00      0.00         3
          77       0.00      0.00      0.00         3
          78       0.00      0.00      0.00         3
          79       0.00      0.00      0.00         3
          80       0.00      0.00      0.00         3
          81       0.00      0.00      0.00         3
          82       0.00      0.00      0.00         3
          83       0.00      0.00      0.00         3
          84       0.00      0.00      0.00         3
          85       0.00      0.00      0.00         3
          86       0.00      0.00      0.00         3
          87       0.00      0.00      0.00         3
          88       0.00      0.00      0.00         3
          89       0.00      0.00      0.00         3
          90       0.00      0.00      0.00         3
          91       0.00      0.00      0.00         3
          92       0.00      0.00      0.00         3
          93       0.00      0.00      0.00         3
          94       0.00      0.00      0.00         3
          95       0.00      0.00      0.00         3
          96       0.00      0.00      0.00         3
          97       0.00      0.00      0.00         3
          98       0.00      0.00      0.00         3
          99       0.00      0.00      0.00         3
         100       0.00      0.00      0.00         3
         101       0.00      0.00      0.00         3
         102       0.00      0.00      0.00         3
         103       0.00      0.00      0.00         3
         104       0.00      0.00      0.00         3
         105       0.00      0.00      0.00         3
         106       0.00      0.00      0.00         3
         107       0.00      0.00      0.00         3
         108       0.00      0.00      0.00         3
         109       0.05      0.33      0.09         3
         110       0.00      0.00      0.00         3
         111       0.00      0.00      0.00         3
         112       0.00      0.00      0.00         3
         113       0.00      0.00      0.00         3
         114       0.00      0.00      0.00         3
         115       0.00      0.00      0.00         3
         116       0.06      0.33      0.11         3
         117       0.00      0.00      0.00         3
         118       0.00      0.00      0.00         3
         119       0.00      0.00      0.00         3
         120       0.00      0.00      0.00         3
         121       0.00      0.00      0.00         3
         122       0.00      0.00      0.00         3
         123       0.00      0.00      0.00         3
         124       0.00      0.00      0.00         3
         125       0.00      0.00      0.00         3
         126       0.00      0.00      0.00         3
         127       0.00      0.00      0.00         3
         128       0.00      0.00      0.00         3
         129       0.00      0.00      0.00         3
         130       0.00      0.00      0.00         3
         131       0.00      0.00      0.00         3
         132       0.00      0.00      0.00         3
         133       0.00      0.00      0.00         3
         134       0.00      0.00      0.00         3
         135       0.00      0.00      0.00         3
         136       0.00      0.00      0.00         3
         137       0.00      0.00      0.00         3
         138       0.00      0.00      0.00         3
         139       0.00      0.00      0.00         3
         140       0.00      0.00      0.00         3
         141       0.00      0.00      0.00         3
         142       0.00      0.00      0.00         3
         143       0.00      0.00      0.00         3
         144       0.00      0.00      0.00         3
         145       0.00      0.00      0.00         3
         146       0.00      0.00      0.00         3
         147       0.00      0.00      0.00         3
         148       0.00      0.00      0.00         3
         149       0.00      0.00      0.00         3
         150       0.00      0.00      0.00         3
         151       0.00      0.00      0.00         3
         152       0.00      0.00      0.00         3
         153       0.00      0.00      0.00         3
         154       0.00      0.00      0.00         3
         155       0.00      0.00      0.00         3
         156       0.00      0.00      0.00         3
         157       0.00      0.00      0.00         3
         158       0.00      0.00      0.00         3
         159       0.00      0.00      0.00         3
         160       0.00      0.00      0.00         3
         161       0.00      0.00      0.00         3
         162       0.00      0.00      0.00         3
         163       0.00      0.00      0.00         3
         164       0.00      0.00      0.00         3
         165       0.00      0.00      0.00         3
         166       0.00      0.00      0.00         3
         167       0.00      0.00      0.00         3
         168       0.00      0.00      0.00         3
         169       0.00      0.00      0.00         3
         170       0.00      0.00      0.00         3
         171       0.00      0.00      0.00         3
         172       0.00      0.00      0.00         3
         173       0.00      0.00      0.00         3
         174       0.00      0.00      0.00         3
         175       0.00      0.00      0.00         3
         176       0.00      0.00      0.00         3
         177       0.00      0.00      0.00         3
         178       0.00      0.00      0.00         3
         179       0.00      0.00      0.00         3
         180       0.00      0.00      0.00         3
         181       0.00      0.00      0.00         3
         182       0.00      0.00      0.00         3
         183       0.00      0.00      0.00         3
         184       0.00      0.00      0.00         3
         185       0.00      0.00      0.00         3
         186       0.00      0.00      0.00         3
         187       0.00      0.00      0.00         3
         188       0.00      0.00      0.00         3
         189       0.00      0.00      0.00         3
         190       0.00      0.00      0.00         3
         191       0.00      0.00      0.00         3
         192       0.00      0.00      0.00         3
         193       0.00      0.00      0.00         3
         194       0.00      0.00      0.00         3
         195       0.00      0.00      0.00         3
         196       0.00      0.00      0.00         3
         197       0.00      0.00      0.00         3
         198       0.00      0.00      0.00         3
         199       0.00      0.00      0.00         3
         200       0.00      0.00      0.00         3
         201       0.00      0.00      0.00         3
         202       0.00      0.00      0.00         3
         203       0.00      0.00      0.00         3
         204       0.00      0.00      0.00         3
         205       0.00      0.00      0.00         3
         206       0.00      0.00      0.00         3
         207       0.00      0.00      0.00         3
         208       0.00      0.00      0.00         3
         209       0.00      0.00      0.00         3
         210       0.00      0.00      0.00         3
         211       0.00      0.00      0.00         3
         212       0.00      0.00      0.00         3
         213       0.00      0.00      0.00         3
         214       0.00      0.00      0.00         3
         215       0.00      0.00      0.00         3
         216       0.00      0.00      0.00         3
         217       0.00      0.00      0.00         3
         218       0.00      0.00      0.00         3
         219       0.00      0.00      0.00         3
         220       0.00      0.00      0.00         3
         221       0.00      0.00      0.00         3
         222       0.00      0.00      0.00         3
         223       0.00      0.00      0.00         3
         224       0.00      0.00      0.00         3
         225       0.00      0.00      0.00         3
         226       0.00      0.00      0.00         3
         227       0.00      0.00      0.00         3
         228       0.00      0.00      0.00         3
         229       0.00      0.00      0.00         3
         230       0.00      0.00      0.00         3
         231       0.00      0.00      0.00         3
         232       0.00      0.00      0.00         3
         233       0.00      0.00      0.00         3
         234       0.00      0.00      0.00         3
         235       0.00      0.00      0.00         3
         236       0.00      0.00      0.00         3
         237       0.00      0.00      0.00         3
         238       0.00      0.00      0.00         3
         239       0.00      0.00      0.00         3
         240       0.00      0.00      0.00         3
         241       0.00      0.00      0.00         3
         242       0.00      0.00      0.00         3
         243       0.00      0.00      0.00         3
         244       0.00      0.00      0.00         3
         245       0.00      0.00      0.00         3
         246       0.00      0.00      0.00         3
         247       0.00      0.00      0.00         3
         248       0.00      0.00      0.00         3
         249       0.00      0.00      0.00         3
         250       0.00      0.00      0.00         3
         251       0.00      0.00      0.00         3
         252       0.00      0.00      0.00         3
         253       0.00      0.00      0.00         3
         254       0.00      0.00      0.00         3
         255       0.00      0.00      0.00         3
         256       0.00      0.00      0.00         3
         257       0.00      0.00      0.00         3
         258       0.00      0.00      0.00         3
         259       0.00      0.00      0.00         3
         260       0.00      0.00      0.00         3
         261       0.00      0.00      0.00         3
         262       0.00      0.00      0.00         3
         263       0.00      0.00      0.00         3
         264       0.00      0.00      0.00         3
         265       0.00      0.00      0.00         3
         266       0.00      0.00      0.00         3
         267       0.00      0.00      0.00         3
         268       0.00      0.00      0.00         3
         269       0.00      0.00      0.00         3
         270       0.00      0.00      0.00         3
         271       0.00      0.00      0.00         3
         272       0.00      0.00      0.00         3
         273       0.00      0.00      0.00         3
         274       0.00      0.00      0.00         3
         275       0.00      0.00      0.00         3
         276       0.00      0.00      0.00         3
         277       0.00      0.00      0.00         3
         278       0.00      0.00      0.00         3
         279       0.00      0.00      0.00         3
         280       0.00      0.00      0.00         3
         281       0.00      0.00      0.00         3
         282       0.00      0.00      0.00         3
         283       0.00      0.00      0.00         3
         284       0.00      0.00      0.00         3
         285       0.00      0.00      0.00         3
         286       0.00      0.00      0.00         3
         287       0.00      0.00      0.00         3
         288       0.00      0.00      0.00         3
         289       0.00      0.00      0.00         3
         290       0.00      0.00      0.00         3
         291       0.00      0.00      0.00         3
         292       0.00      0.00      0.00         3
         293       0.00      0.00      0.00         3
         294       0.00      0.00      0.00         3
         295       0.00      0.00      0.00         3
         296       0.00      0.00      0.00         3
         297       0.00      0.00      0.00         3
         298       0.00      0.00      0.00         3
         299       0.00      0.00      0.00         3
         300       0.00      0.00      0.00         3
         301       0.00      0.00      0.00         3
         302       0.00      0.00      0.00         3
         303       0.00      0.00      0.00         3
         304       0.00      0.00      0.00         3
         305       0.00      0.00      0.00         3
         306       0.00      0.00      0.00         3
         307       0.00      0.00      0.00         3
         308       0.00      0.00      0.00         3
         309       0.00      0.00      0.00         3
         310       0.00      0.00      0.00         3
         311       0.00      0.00      0.00         3
         312       0.00      0.00      0.00         3
         313       0.00      0.00      0.00         3
         314       0.00      0.00      0.00         3
         315       0.00      0.00      0.00         3
         316       0.00      0.00      0.00         3
         317       0.00      0.00      0.00         3
         318       0.00      0.00      0.00         3
         319       0.00      0.00      0.00         3
         320       0.00      0.00      0.00         3
         321       0.00      0.00      0.00         3
         322       0.00      0.00      0.00         3
         323       0.00      0.00      0.00         3
         324       0.00      0.00      0.00         3
         325       0.00      0.00      0.00         3
         326       0.00      0.00      0.00         3
         327       0.00      0.00      0.00         3
         328       0.00      0.00      0.00         3
         329       0.00      0.00      0.00         3
         330       0.00      0.00      0.00         3
         331       0.00      0.00      0.00         3
         332       0.00      0.00      0.00         3
         333       0.00      0.00      0.00         3
         334       0.00      0.00      0.00         3
         335       0.00      0.00      0.00         3
         336       0.00      0.00      0.00         3
         337       0.00      0.00      0.00         3
         338       0.00      0.00      0.00         3
         339       0.00      0.00      0.00         3
         340       0.00      0.00      0.00         3
         341       0.00      0.00      0.00         3
         342       0.00      0.00      0.00         3
         343       0.00      0.00      0.00         3
         344       0.00      0.00      0.00         3
         345       0.00      0.00      0.00         3
         346       0.00      0.00      0.00         3
         347       0.00      0.00      0.00         3
         348       0.00      0.00      0.00         3
         349       0.00      0.00      0.00         3
         350       0.00      0.00      0.00         3
         351       0.00      0.00      0.00         3
         352       0.00      0.00      0.00         3
         353       0.00      0.00      0.00         3
         354       0.00      0.00      0.00         3
         355       0.00      0.00      0.00         3
         356       0.00      0.00      0.00         3
         357       0.00      0.00      0.00         3
         358       0.00      0.00      0.00         3
         359       0.00      0.00      0.00         3
         360       0.00      0.00      0.00         3
         361       0.00      0.00      0.00         3
         362       0.00      0.00      0.00         3
         363       0.00      0.00      0.00         3
         364       0.00      0.00      0.00         3
         365       0.00      0.00      0.00         3
         366       0.00      0.00      0.00         3
         367       0.00      0.00      0.00         3
         368       0.00      0.00      0.00         3
         369       0.00      0.00      0.00         3
         370       0.00      0.00      0.00         3
         371       0.00      0.00      0.00         3
         372       0.00      0.00      0.00         3
         373       0.00      0.00      0.00         3
         374       0.00      0.00      0.00         3
         375       0.00      0.00      0.00         3
         376       0.00      0.00      0.00         3
         377       0.00      0.00      0.00         3
         378       0.00      0.00      0.00         3
         379       0.00      0.00      0.00         3
         380       0.00      0.00      0.00         3
         381       0.00      0.00      0.00         3
         382       0.00      0.00      0.00         3
         383       0.00      0.00      0.00         3
         384       0.00      0.00      0.00         3
         385       0.00      0.00      0.00         3
         386       0.00      0.00      0.00         3
         387       0.00      0.00      0.00         3
         388       0.00      0.00      0.00         3
         389       0.00      0.00      0.00         3
         390       0.00      0.00      0.00         3
         391       0.00      0.00      0.00         3
         392       0.00      0.00      0.00         3
         393       0.00      0.00      0.00         3
         394       0.00      0.00      0.00         3
         395       0.00      0.00      0.00         3
         396       0.00      0.00      0.00         3
         397       0.00      0.00      0.00         3
         398       0.00      0.00      0.00         3
         399       0.00      0.00      0.00         3
         400       0.00      0.00      0.00         3
         401       0.00      0.00      0.00         3
         402       0.00      0.00      0.00         3
         403       0.00      0.00      0.00         3
         404       0.00      0.00      0.00         3
         405       0.00      0.00      0.00         3
         406       0.00      0.00      0.00         3
         407       0.00      0.00      0.00         3
         408       0.00      0.00      0.00         3
         409       0.00      0.00      0.00         3
         410       0.00      0.00      0.00         3
         411       0.00      0.00      0.00         3
         412       0.00      0.00      0.00         3
         413       0.00      0.00      0.00         3
         414       0.00      0.00      0.00         3
         415       0.00      0.00      0.00         3
         416       0.00      0.00      0.00         3
         417       0.00      0.00      0.00         3
         418       0.00      0.00      0.00         3
         419       0.00      0.00      0.00         3
         420       0.00      0.00      0.00         3
         421       0.00      0.00      0.00         3
         422       0.00      0.00      0.00         3
         423       0.00      0.00      0.00         3
         424       0.00      0.00      0.00         3
         425       0.00      0.00      0.00         3
         426       0.00      0.00      0.00         3
         427       0.00      0.00      0.00         3
         428       0.00      0.00      0.00         3
         429       0.00      0.00      0.00         3
         430       0.00      0.00      0.00         3
         431       0.00      0.00      0.00         3
         432       0.00      0.00      0.00         3
         433       0.00      0.00      0.00         3
         434       0.00      0.00      0.00         3
         435       0.00      0.00      0.00         3
         436       0.00      0.00      0.00         3
         437       0.00      0.00      0.00         3
         438       0.00      0.00      0.00         3
         439       0.00      0.00      0.00         3
         440       0.00      0.00      0.00         3
         441       0.00      0.00      0.00         3
         442       0.00      0.00      0.00         3
         443       0.00      0.00      0.00         3
         444       0.00      0.00      0.00         3
         445       0.00      0.00      0.00         3
         446       0.00      0.00      0.00         3
         447       0.00      0.00      0.00         3
         448       0.00      0.00      0.00         3
         449       0.00      0.00      0.00         3
         450       0.00      0.00      0.00         3
         451       0.00      0.00      0.00         3
         452       0.00      0.00      0.00         3
         453       0.00      0.00      0.00         3
         454       0.00      0.00      0.00         3
         455       0.00      0.00      0.00         3
         456       0.00      0.00      0.00         3
         457       0.00      0.00      0.00         3
         458       0.00      0.00      0.00         3
         459       0.00      0.00      0.00         3
         460       0.00      0.00      0.00         3
         461       0.00      0.00      0.00         3
         462       0.00      0.00      0.00         3
         463       0.00      0.00      0.00         3
         464       0.00      0.00      0.00         3
         465       0.00      0.00      0.00         3
         466       0.00      0.00      0.00         3
         467       0.00      0.00      0.00         3
         468       0.00      0.00      0.00         3
         469       0.00      0.00      0.00         3
         470       0.00      0.00      0.00         3
         471       0.00      0.00      0.00         3
         472       0.00      0.00      0.00         3
         473       0.00      0.00      0.00         3
         474       0.00      0.00      0.00         3
         475       0.00      0.00      0.00         3
         476       0.00      0.00      0.00         3
         477       0.00      0.00      0.00         3
         478       0.00      0.00      0.00         3
         479       0.00      0.00      0.00         3
         480       0.00      0.00      0.00         3
         481       0.00      0.00      0.00         3
         482       0.00      0.00      0.00         3
         483       0.00      0.00      0.00         3
         484       0.00      0.00      0.00         3
         485       0.00      0.00      0.00         3
         486       0.00      0.00      0.00         3
         487       0.00      0.00      0.00         3
         488       0.00      0.00      0.00         3
         489       0.00      0.00      0.00         3
         490       0.00      0.00      0.00         3
         491       0.00      0.00      0.00         3
         492       0.00      0.00      0.00         3
         493       0.00      0.00      0.00         3
         494       0.00      0.00      0.00         3
         495       0.00      0.00      0.00         3
         496       0.00      0.00      0.00         3
         497       0.00      0.00      0.00         3
         498       0.00      0.00      0.00         3
         499       0.00      0.00      0.00         3
         500       0.00      0.00      0.00         3
         501       0.00      0.00      0.00         3
         502       0.00      0.00      0.00         3
         503       0.00      0.00      0.00         3
         504       0.00      0.00      0.00         3
         505       0.00      0.00      0.00         3
         506       0.00      0.00      0.00         3
         507       0.00      0.00      0.00         3
         508       0.00      0.00      0.00         3
         509       0.00      0.00      0.00         3
         510       0.00      0.00      0.00         3
         511       0.00      0.00      0.00         3
         512       0.00      0.00      0.00         3
         513       0.00      0.00      0.00         3
         514       0.00      0.00      0.00         3
         515       0.00      0.00      0.00         3
         516       0.00      0.00      0.00         3
         517       0.00      0.00      0.00         3
         518       0.00      0.00      0.00         3
         519       0.00      0.00      0.00         3
         520       0.00      0.00      0.00         3
         521       0.00      0.00      0.00         3
         522       0.00      0.00      0.00         3
         523       0.00      0.00      0.00         3
         524       0.00      0.00      0.00         3
         525       0.00      0.00      0.00         3
         526       0.00      0.00      0.00         3
         527       0.00      0.00      0.00         3
         528       0.00      0.00      0.00         3
         529       0.00      0.00      0.00         3
         530       0.00      0.00      0.00         3
         531       0.00      0.00      0.00         3
         532       0.00      0.00      0.00         3
         533       0.00      0.00      0.00         3
         534       0.00      0.00      0.00         3
         535       0.00      0.00      0.00         3
         536       0.00      0.00      0.00         3
         537       0.00      0.00      0.00         3
         538       0.00      0.00      0.00         3
         539       0.00      0.00      0.00         3
         540       0.00      0.00      0.00         3
         541       0.00      0.00      0.00         3
         542       0.00      0.00      0.00         3
         543       0.00      0.00      0.00         3
         544       0.00      0.00      0.00         3
         545       0.00      0.00      0.00         3
         546       0.00      0.00      0.00         3
         547       0.00      0.00      0.00         3
         548       0.00      0.00      0.00         3
         549       0.00      0.00      0.00         3
         550       0.00      0.00      0.00         3
         551       0.00      0.00      0.00         3
         552       0.00      0.00      0.00         3
         553       0.00      0.00      0.00         3
         554       0.00      0.00      0.00         3
         555       0.00      0.00      0.00         3
         556       0.00      0.00      0.00         3
         557       0.00      0.00      0.00         3
         558       0.00      0.00      0.00         3
         559       0.00      0.00      0.00         3
         560       0.00      0.00      0.00         3
         561       0.00      0.00      0.00         3
         562       0.00      0.00      0.00         3
         563       0.00      0.00      0.00         3
         564       0.00      0.00      0.00         3
         565       0.00      0.00      0.00         3
         566       0.00      0.00      0.00         3
         567       0.00      0.00      0.00         3
         568       0.00      0.00      0.00         3
         569       0.00      0.00      0.00         3
         570       0.00      0.00      0.00         3
         571       0.00      0.00      0.00         3
         572       0.00      0.00      0.00         3
         573       0.00      0.00      0.00         3
         574       0.00      0.00      0.00         3
         575       0.00      0.00      0.00         3
         576       0.00      0.00      0.00         3
         577       0.00      0.00      0.00         3
         578       0.00      0.00      0.00         3
         579       0.00      0.00      0.00         3
         580       0.00      0.00      0.00         3
         581       0.00      0.00      0.00         3
         582       0.00      0.00      0.00         3
         583       0.00      0.00      0.00         3
         584       0.00      0.00      0.00         3
         585       0.00      0.00      0.00         3
         586       0.00      0.00      0.00         3
         587       0.00      0.00      0.00         3
         588       0.00      0.00      0.00         3
         589       0.00      0.00      0.00         3
         590       0.00      0.00      0.00         3
         591       0.00      0.00      0.00         3
         592       0.00      0.00      0.00         3
         593       0.00      0.00      0.00         3
         594       0.00      0.00      0.00         3
         595       0.00      0.00      0.00         3
         596       0.00      0.00      0.00         3
         597       0.00      0.00      0.00         3
         598       0.00      0.00      0.00         3
         599       0.00      0.00      0.00         3
         600       0.00      0.00      0.00         3
         601       0.00      0.00      0.00         3
         602       0.00      0.00      0.00         3
         603       0.00      0.00      0.00         3
         604       0.00      0.00      0.00         3
         605       0.00      0.00      0.00         3
         606       0.00      0.00      0.00         3
         607       0.00      0.00      0.00         3
         608       0.00      0.00      0.00         3
         609       0.00      0.00      0.00         3
         610       0.00      0.00      0.00         3
         611       0.00      0.00      0.00         3
         612       0.00      0.00      0.00         3
         613       0.00      0.00      0.00         3
         614       0.00      0.00      0.00         3
         615       0.00      0.00      0.00         3
         616       0.00      0.00      0.00         3
         617       0.00      0.00      0.00         3
         618       0.00      0.00      0.00         3
         619       0.00      0.00      0.00         3
         620       0.00      0.00      0.00         3
         621       0.00      0.00      0.00         3
         622       0.00      0.00      0.00         3
         623       0.00      0.00      0.00         3
         624       0.00      0.00      0.00         3
         625       0.00      0.00      0.00         3
         626       0.00      0.00      0.00         3
         627       0.00      0.00      0.00         3
         628       0.00      0.00      0.00         3
         629       0.00      0.00      0.00         3
         630       0.00      0.00      0.00         3
         631       0.00      0.00      0.00         3
         632       0.00      0.00      0.00         3
         633       0.00      0.00      0.00         3
         634       0.00      0.00      0.00         3
         635       0.00      0.00      0.00         3
         636       0.00      0.00      0.00         3
         637       0.00      0.00      0.00         3
         638       0.00      0.00      0.00         3
         639       0.00      0.00      0.00         3
         640       0.00      0.00      0.00         3
         641       0.00      0.00      0.00         3
         642       0.00      0.00      0.00         3
         643       0.00      0.00      0.00         3
         644       0.00      0.00      0.00         3
         645       0.00      0.00      0.00         3
         646       0.00      0.00      0.00         3
         647       0.00      0.00      0.00         3
         648       0.00      0.00      0.00         3
         649       0.00      0.00      0.00         3
         650       0.00      0.00      0.00         3
         651       0.00      0.00      0.00         3
         652       0.00      0.00      0.00         3
         653       0.00      0.00      0.00         3
         654       0.00      0.00      0.00         3
         655       0.00      0.00      0.00         3
         656       0.00      0.00      0.00         3
         657       0.00      0.00      0.00         3
         658       0.00      0.00      0.00         3
         659       0.00      0.00      0.00         3
         660       0.00      0.00      0.00         3
         661       0.00      0.00      0.00         3
         662       0.00      0.00      0.00         3
         663       0.00      0.00      0.00         3
         664       0.00      0.00      0.00         3
         665       0.00      0.00      0.00         3
         666       0.00      0.00      0.00         3
         667       0.00      0.00      0.00         3
         668       0.00      0.00      0.00         3
         669       0.00      0.00      0.00         3
         670       0.00      0.00      0.00         3
         671       0.00      0.00      0.00         3
         672       0.00      0.00      0.00         3
         673       0.00      0.00      0.00         3
         674       0.00      0.00      0.00         3
         675       0.00      0.00      0.00         3
         676       0.00      0.00      0.00         3
         677       0.00      0.00      0.00         3
         678       0.00      0.00      0.00         3
         679       0.00      0.00      0.00         3
         680       0.00      0.00      0.00         3
         681       0.00      0.00      0.00         3
         682       0.00      0.00      0.00         3
         683       0.00      0.00      0.00         3
         684       0.00      0.00      0.00         3
         685       0.00      0.00      0.00         3
         686       0.00      0.00      0.00         3
         687       0.00      0.00      0.00         3
         688       0.00      0.00      0.00         3
         689       0.00      0.00      0.00         3
         690       0.00      0.00      0.00         3
         691       0.00      0.00      0.00         3
         692       0.00      0.00      0.00         3
         693       0.00      0.00      0.00         3
         694       0.00      0.00      0.00         3
         695       0.00      0.00      0.00         3
         696       0.00      0.00      0.00         3
         697       0.00      0.00      0.00         3
         698       0.00      0.00      0.00         3
         699       0.00      0.00      0.00         3
         700       0.00      0.00      0.00         3
         701       0.00      0.00      0.00         3
         702       0.00      0.00      0.00         3
         703       0.00      0.00      0.00         3
         704       0.00      0.00      0.00         3
         705       0.00      0.00      0.00         3
         706       0.00      0.00      0.00         3
         707       0.00      0.00      0.00         3
         708       0.00      0.00      0.00         3
         709       0.00      0.00      0.00         3
         710       0.00      0.00      0.00         3
         711       0.00      0.00      0.00         3
         712       0.00      0.00      0.00         3
         713       0.00      0.00      0.00         3
         714       0.00      0.00      0.00         3
         715       0.00      0.00      0.00         3
         716       0.00      0.00      0.00         3
         717       0.00      0.00      0.00         3
         718       0.00      0.00      0.00         3
         719       0.00      0.00      0.00         3
         720       0.00      0.00      0.00         3
         721       0.00      0.00      0.00         3
         722       0.00      0.00      0.00         3
         723       0.00      0.00      0.00         3
         724       0.00      0.00      0.00         3
         725       0.00      0.00      0.00         3
         726       0.00      0.00      0.00         3
         727       0.00      0.00      0.00         3
         728       0.00      0.00      0.00         3
         729       0.00      0.00      0.00         3
         730       0.00      0.00      0.00         3
         731       0.00      0.00      0.00         3
         732       0.00      0.00      0.00         3
         733       0.00      0.00      0.00         3
         734       0.00      0.00      0.00         3
         735       0.00      0.00      0.00         3
         736       0.00      0.00      0.00         3
         737       0.00      0.00      0.00         3
         738       0.00      0.00      0.00         3
         739       0.00      0.00      0.00         3
         740       0.00      0.00      0.00         3
         741       0.00      0.00      0.00         3
         742       0.00      0.00      0.00         3
         743       0.00      0.00      0.00         3
         744       0.00      0.00      0.00         3
         745       0.00      0.00      0.00         3
         746       0.00      0.00      0.00         3
         747       0.00      0.00      0.00         3
         748       0.00      0.00      0.00         3
         749       0.00      0.00      0.00         3
         750       0.00      0.00      0.00         3
         751       0.00      0.00      0.00         3
         752       0.00      0.00      0.00         3
         753       0.00      0.00      0.00         3
         754       0.00      0.00      0.00         3
         755       0.00      0.00      0.00         3
         756       0.00      0.00      0.00         3
         757       0.00      0.00      0.00         3
         758       0.00      0.00      0.00         3
         759       0.00      0.00      0.00         3
         760       0.00      0.00      0.00         3
         761       0.00      0.00      0.00         3
         762       0.00      0.00      0.00         3
         763       0.00      0.00      0.00         3
         764       0.00      0.00      0.00         3
         765       0.00      0.00      0.00         3
         766       0.00      0.00      0.00         3
         767       0.00      0.00      0.00         3
         768       0.00      0.00      0.00         3
         769       0.00      0.00      0.00         3
         770       0.00      0.00      0.00         3
         771       0.00      0.00      0.00         3
         772       0.00      0.00      0.00         3
         773       0.00      0.00      0.00         3
         774       0.00      0.00      0.00         3
         775       0.00      0.00      0.00         3
         776       0.00      0.00      0.00         3
         777       0.00      0.00      0.00         3
         778       0.00      0.00      0.00         3
         779       0.00      0.00      0.00         3
         780       0.00      0.00      0.00         3
         781       0.00      0.00      0.00         3
         782       0.00      0.00      0.00         3
         783       0.00      0.00      0.00         3
         784       0.00      0.00      0.00         3
         785       0.00      0.00      0.00         3
         786       0.00      0.00      0.00         3
         787       0.00      0.00      0.00         3
         788       0.00      0.00      0.00         3
         789       0.00      0.00      0.00         3
         790       0.00      0.00      0.00         3
         791       0.00      0.00      0.00         3
         792       0.00      0.00      0.00         3
         793       0.00      0.00      0.00         3
         794       0.00      0.00      0.00         3
         795       0.00      0.00      0.00         3
         796       0.00      0.00      0.00         3
         797       0.00      0.00      0.00         3
         798       0.00      0.00      0.00         3
         799       0.00      0.00      0.00         3
         800       0.00      0.00      0.00         3
         801       0.00      0.00      0.00         3
         802       0.00      0.00      0.00         3
         803       0.00      0.00      0.00         3
         804       0.00      0.00      0.00         3
         805       0.00      0.00      0.00         3
         806       0.00      0.00      0.00         3
         807       0.00      0.00      0.00         3
         808       0.00      0.00      0.00         3
         809       0.00      0.00      0.00         3
         810       0.00      0.00      0.00         3
         811       0.00      0.00      0.00         3
         812       0.00      0.00      0.00         3
         813       0.00      0.00      0.00         3
         814       0.00      0.00      0.00         3
         815       0.00      0.00      0.00         3
         816       0.00      0.00      0.00         3
         817       0.00      0.00      0.00         3
         818       0.00      0.00      0.00         3
         819       0.00      0.00      0.00         3
         820       0.00      0.00      0.00         3
         821       0.00      0.00      0.00         3
         822       0.00      0.00      0.00         3
         823       0.00      0.00      0.00         3
         824       0.00      0.00      0.00         3
         825       0.00      0.00      0.00         3
         826       0.00      0.00      0.00         3
         827       0.00      0.00      0.00         3
         828       0.00      0.00      0.00         3
         829       0.00      0.00      0.00         3
         830       0.00      0.00      0.00         3
         831       0.00      0.00      0.00         3
         832       0.00      0.00      0.00         3
         833       0.00      0.00      0.00         3
         834       0.00      0.00      0.00         3
         835       0.00      0.00      0.00         3
         836       0.00      0.00      0.00         3
         837       0.00      0.00      0.00         3
         838       0.00      0.00      0.00         3
         839       0.00      0.00      0.00         3
         840       0.00      0.00      0.00         3
         841       0.00      0.00      0.00         3
         842       0.00      0.00      0.00         3
         843       0.00      0.00      0.00         3
         844       0.00      0.00      0.00         3
         845       0.00      0.00      0.00         3
         846       0.00      0.00      0.00         3
         847       0.00      0.00      0.00         3
         848       0.00      0.00      0.00         3
         849       0.00      0.00      0.00         3
         850       0.00      0.00      0.00         3
         851       0.00      0.00      0.00         3
         852       0.00      0.00      0.00         3
         853       0.00      0.00      0.00         3
         854       0.00      0.00      0.00         3
         855       0.00      0.00      0.00         3
         856       0.00      0.00      0.00         3
         857       0.00      0.00      0.00         3
         858       0.00      0.00      0.00         3
         859       0.00      0.00      0.00         3
         860       0.00      0.00      0.00         3
         861       0.00      0.00      0.00         3
         862       0.00      0.00      0.00         3
         863       0.00      0.00      0.00         3
         864       0.00      0.00      0.00         3
         865       0.00      0.00      0.00         3
         866       0.00      0.00      0.00         3
         867       0.00      0.00      0.00         3
         868       0.00      0.00      0.00         3
         869       0.00      0.00      0.00         3
         870       0.00      0.00      0.00         3
         871       0.00      0.00      0.00         3
         872       0.00      0.00      0.00         3
         873       0.00      0.00      0.00         3
         874       0.00      0.00      0.00         3
         875       0.00      0.00      0.00         3
         876       0.00      0.00      0.00         3
         877       0.00      0.00      0.00         3
         878       0.00      0.00      0.00         3
         879       0.00      0.00      0.00         3
         880       0.00      0.00      0.00         3
         881       0.00      0.00      0.00         3
         882       0.00      0.00      0.00         3
         883       0.00      0.00      0.00         3
         884       0.00      0.00      0.00         3
         885       0.00      0.00      0.00         3
         886       0.00      0.00      0.00         3
         887       0.00      0.00      0.00         3
         888       0.00      0.00      0.00         3
         889       0.00      0.00      0.00         3
         890       0.00      0.00      0.00         3
         891       0.00      0.00      0.00         3
         892       0.00      0.00      0.00         3
         893       0.00      0.00      0.00         3
         894       0.00      0.00      0.00         3
         895       0.00      0.00      0.00         3
         896       0.00      0.00      0.00         3
         897       0.00      0.00      0.00         3
         898       0.00      0.00      0.00         3
         899       0.00      0.00      0.00         3
         900       0.00      0.00      0.00         3
         901       0.00      0.00      0.00         3
         902       0.00      0.00      0.00         3
         903       0.00      0.00      0.00         3
         904       0.00      0.00      0.00         3
         905       0.00      0.00      0.00         3
         906       0.00      0.00      0.00         3
         907       0.00      0.00      0.00         3
         908       0.00      0.00      0.00         3
         909       0.00      0.00      0.00         3
         910       0.00      0.00      0.00         3
         911       0.00      0.00      0.00         3
         912       0.00      0.00      0.00         3
         913       0.00      0.00      0.00         3
         914       0.00      0.00      0.00         3
         915       0.00      0.00      0.00         3
         916       0.00      0.00      0.00         3
         917       0.00      0.00      0.00         3
         918       0.00      0.00      0.00         3
         919       0.00      0.00      0.00         3
         920       0.00      0.00      0.00         3
         921       0.00      0.00      0.00         3
         922       0.00      0.00      0.00         3
         923       0.00      0.00      0.00         3
         924       0.00      0.00      0.00         3
         925       0.00      0.00      0.00         3
         926       0.00      0.00      0.00         3
         927       0.00      0.00      0.00         3
         928       0.00      0.00      0.00         3
         929       0.00      0.00      0.00         3
         930       0.00      0.00      0.00         3
         931       0.00      0.00      0.00         3
         932       0.00      0.00      0.00         3
         933       0.00      0.00      0.00         3
         934       0.00      0.00      0.00         3
         935       0.00      0.00      0.00         3
         936       0.00      0.00      0.00         3
         937       0.00      0.00      0.00         3
         938       0.00      0.00      0.00         3
         939       0.00      0.00      0.00         3
         940       0.00      0.00      0.00         3
         941       0.00      0.00      0.00         3
         942       0.00      0.00      0.00         3
         943       0.00      0.00      0.00         3
         944       0.00      0.00      0.00         3
         945       0.00      0.00      0.00         3
         946       0.00      0.00      0.00         3
         947       0.00      0.00      0.00         3
         948       0.00      0.00      0.00         3
         949       0.00      0.00      0.00         3
         950       0.00      0.00      0.00         3
         951       0.00      0.00      0.00         3
         952       0.00      0.00      0.00         3
         953       0.00      0.00      0.00         3
         954       0.00      0.00      0.00         3
         955       0.00      0.00      0.00         3
         956       0.00      0.00      0.00         3
         957       0.00      0.00      0.00         3
         958       0.00      0.00      0.00         3
         959       0.00      0.00      0.00         3
         960       0.00      0.00      0.00         3
         961       0.00      0.00      0.00         3
         962       0.00      0.00      0.00         3
         963       0.00      0.00      0.00         3
         964       0.00      0.00      0.00         3
         965       0.00      0.00      0.00         3
         966       0.00      0.00      0.00         3
         967       0.00      0.00      0.00         3
         968       0.00      0.00      0.00         3
         969       0.00      0.00      0.00         3
         970       0.00      0.00      0.00         3
         971       0.00      0.00      0.00         3
         972       0.00      0.00      0.00         3
         973       0.00      0.00      0.00         3
         974       0.00      0.00      0.00         3
         975       0.00      0.00      0.00         3
         976       0.00      0.00      0.00         3
         977       0.00      0.00      0.00         3
         978       0.00      0.00      0.00         3
         979       0.00      0.00      0.00         3
         980       0.00      0.00      0.00         3
         981       0.00      0.00      0.00         3
         982       0.00      0.00      0.00         3
         983       0.00      0.00      0.00         3
         984       0.00      0.00      0.00         3
         985       0.00      0.00      0.00         3
         986       0.00      0.00      0.00         3
         987       0.00      0.00      0.00         3
         988       0.00      0.00      0.00         3
         989       0.00      0.00      0.00         3
         990       0.00      0.00      0.00         3
         991       0.00      0.00      0.00         3
         992       0.00      0.00      0.00         3
         993       0.00      0.00      0.00         3
         994       0.00      0.00      0.00         3
         995       0.00      0.00      0.00         3
         996       0.00      0.00      0.00         3
         997       0.00      0.00      0.00         3
         998       0.00      0.00      0.00         3
         999       0.00      0.00      0.00         3
        1000       0.00      0.00      0.00         3
        1001       0.00      0.00      0.00         3
        1002       0.00      0.00      0.00         3
        1003       0.00      0.00      0.00         3
        1004       0.00      0.00      0.00         3
        1005       0.00      0.00      0.00         3
        1006       0.00      0.00      0.00         3
        1007       0.00      0.00      0.00         3
        1008       0.00      0.00      0.00         3
        1009       0.00      0.00      0.00         3

   micro avg       0.00      0.00      0.00      3030
   macro avg       0.00      0.00      0.00      3030
weighted avg       0.00      0.00      0.00      3030

/opt/conda/lib/python3.6/site-packages/sklearn/metrics/classification.py:1143: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
  'precision', 'predicted', average, warn_for)

In [32]:

%%time
test_generator.reset()
predict=model_final.predict_generator(test_generator, steps = len(test_generator.filenames), verbose=1)

10952/35350 [========>.....................] - ETA: 32:23

In [33]:

predicted_class_indices=np.argmax(predict,axis=1)

In [34]:

gc.collect();

In [35]:

labels = (train_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
predictions = [labels[k] for k in predicted_class_indices]

In [36]:

sam_sub_df = pd.read_csv('../input/kaggle_sample_submission.csv')
sam_sub_df.head()

Out[36]:

| id | predicted
0 | 268243 | 842
1 | 268244 | 139
2 | 268245 | 988
3 | 268246 | 612
4 | 268247 | 468

In [37]:

sam_sub_df.shape

Out[37]:

(35350, 2)

In [38]:

filenames=test_generator.filenames
results=pd.DataFrame({"file_name":filenames,
                      "predicted":predictions})
df_res = pd.merge(test_img_df, results, on='file_name')[['image_id','predicted']]\
    .rename(columns={'image_id':'id'})

df_res.head()

Out[38]:

| id | predicted
0 | 268243 | 4
1 | 268244 | 212
2 | 268245 | 370
3 | 268246 | 161
4 | 268247 | 476

In [39]:

df_res.to_csv("submission.csv",index=False)

Inception3 - last years baseline

From: https://www.kaggle.com/feichin/inception3-last-years-baseline

Author: Florian Eichin

Score: 0.51697

iNaturalist Competition 2018 Training Code¶

I did not write the following code myself. I copied this from last years baseline and only did some refactorization to make the code run in this kernel. Actually, I was just exploring the dataset and trying to see how past solutions do. All the credit goes to user macaoda.

So far, I managed to make the pre trained model run and also trained it on a few epochs over the training data. As you can see, the scores do not match this years baseline yet. According to user zz, scores of at least 0.22 are possible with slight changes. Sadly, I will not be able to spend more time with this competition.

Hope, this kernel helps you in some way.

Step 0: Import modules¶

In [1]:

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

import urllib.request

import torch.nn.functional as F
import torch.utils.model_zoo as model_zoo

import torch.utils.data as data
from PIL import Image
import os
import json
from torchvision import transforms
import random
import numpy as np

Step 1: Download pre-trained model and define global variables¶

In [2]:

# uncomment this to work with last years baseline model
# performance will however be bad, with this commented out, we will just work
# on a pre trained image net model
# print('Downloading model files...', end="")
# url = 'http://vision.caltech.edu/~macaodha/inat2018/iNat_2018_InceptionV3.pth.tar'
# urllib.request.urlretrieve(url, 'iNat_2018_InceptionV3.pth.tar')
# print('Done.')

In [3]:

class Params:
    # arch = 'inception_v3'
    num_classes = 1010
    workers = 8
    epochs = 4
    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 = 'iNat_2018_InceptionV3.pth.tar'    # path to trained model
    train_file = '../input/train2019.json'      # path to train file
    val_file = '../input/test2019.json'         # path to test file
    data_root_train = '../input/train_val2019/' # path to train images
    data_root_test = '../input/test2019/'       # path to test images
    op_file_name = 'submission.csv'             # submission filename

    # set evaluate to True to run the test set
    evaluate = True
    save_preds = True

Step 2: Model initialization¶

I simply copied the code from the different modules and did some refactorization to execute it in this kernel.

In [4]:

# This code was copied from https://github.com/macaodha/inat_comp_2018/blob/master/inception.py

# Same as the version from the official start_epoch
# ttps://github.com/pytorch/vision/blob/master/torchvision/models/inception.py
# Only change being that it can take variable sized inputs
# See line 122

__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 [5]:

# The following was copied from https://github.com/macaodha/inat_comp_2018/blob/master/train_inat.py

# Adapted from https://github.com/pytorch/examples/blob/master/imagenet/main.py

best_prec3 = 0.0  # store current best top 3

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\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(async=True)
        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.data[0], 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 % args.print_freq == 0:
            print('[{0}/{1}]\t'
                '{batch_time.val:.2f} ({batch_time.avg:.2f})\t'
                '{data_time.val:.2f} ({data_time.avg:.2f})\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, top1=top1, top3=top3))

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\tPrec@1\t\tPrec@3')
    for i, (input, im_id, target, tax_ids) in enumerate(val_loader):
        input = input.cuda()
        target = target.cuda(async=True)
        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(5,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.data[0], 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 % args.print_freq == 0:
            print('[{0}/{1}]\t'
                  '{batch_time.val:.2f} ({batch_time.avg:.2f})\t'
                  '{top1.val:.2f} ({top1.avg:.2f})\t'
                  '{top3.val:.2f} ({top3.avg:.2f})'.format(
                   i, len(val_loader), batch_time=batch_time,
                   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

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 = args.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 [6]:

# The following was copied from https://github.com/macaodha/inat_comp_2018/blob/master/inat2018_loader.py

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

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)

Step 3: Train and Predict¶

In [7]:

# The following was copied from https://github.com/macaodha/inat_comp_2018/blob/master/inat2018_loader.py

args = Params()

# load pretrained model
print("Using pre-trained inception_v3")
model = inception_v3(pretrained=True)
model.fc = nn.Linear(2048, args.num_classes)
model.aux_logits = False
model = model.cuda()

# define loss function (criterion) and optimizer
criterion = nn.CrossEntropyLoss().cuda()
optimizer = torch.optim.SGD(model.parameters(), args.lr,
                            momentum=args.momentum,
                            weight_decay=args.weight_decay)

# 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))

    cudnn.benchmark = True

# data loading code
train_dataset = INAT(args.data_root_train, args.train_file,
                     is_train=True)
val_dataset = INAT(args.data_root_test, args.val_file,
                     is_train=False)

train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size,
                   shuffle=True, num_workers=args.workers, pin_memory=True)
val_loader = torch.utils.data.DataLoader(val_dataset,
                                         batch_size=args.batch_size, shuffle=False,
                                         num_workers=args.workers, pin_memory=True)

for epoch in range(args.start_epoch, args.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)

if args.evaluate:
    prec3, preds, im_ids = validate(val_loader, model, criterion, True)
    # write predictions to file
    if args.save_preds:
        with open(args.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(j) for j in preds[ii, :]]) + '\n')

Using pre-trained inception_v3

Downloading: "https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth" to /tmp/.torch/models/inception_v3_google-1a9a5a14.pth
108857766it [00:07, 14436811.27it/s]

=> no checkpoint found at 'iNat_2018_InceptionV3.pth.tar'
Loading annotations from: train2019.json
	265213 images
	1010 classes
Loading annotations from: test2019.json
	35350 images
	1 classes
Epoch:0
Itr		Time		Data		Prec@1		Prec@3
[0/4144]	21.64 (21.64)	11.95 (11.95)	0.00 (0.00)	0.00 (0.00)
[100/4144]	0.52 (1.60)	0.01 (0.98)	1.56 (0.84)	4.69 (1.75)
[200/4144]	5.92 (1.56)	5.32 (0.98)	0.00 (1.73)	9.38 (4.18)
[300/4144]	0.53 (1.52)	0.00 (0.97)	4.69 (2.52)	9.38 (6.13)
[400/4144]	3.52 (1.51)	3.00 (0.96)	4.69 (3.43)	12.50 (8.10)
[500/4144]	0.51 (1.50)	0.00 (0.96)	7.81 (4.35)	14.06 (9.77)
[600/4144]	0.54 (1.50)	0.01 (0.96)	9.38 (5.13)	17.19 (11.34)
[700/4144]	0.52 (1.50)	0.00 (0.96)	6.25 (5.82)	15.62 (12.86)
[800/4144]	0.51 (1.49)	0.01 (0.96)	10.94 (6.45)	21.88 (14.24)
[900/4144]	0.50 (1.50)	0.00 (0.96)	12.50 (7.19)	26.56 (15.65)
[1000/4144]	0.64 (1.49)	0.00 (0.96)	21.88 (7.87)	31.25 (16.95)
[1100/4144]	0.55 (1.49)	0.00 (0.96)	14.06 (8.54)	34.38 (18.15)
[1200/4144]	0.68 (1.49)	0.13 (0.95)	18.75 (9.12)	31.25 (19.29)
[1300/4144]	0.54 (1.49)	0.00 (0.96)	21.88 (9.75)	39.06 (20.35)
[1400/4144]	0.67 (1.49)	0.15 (0.95)	23.44 (10.34)	39.06 (21.37)
[1500/4144]	0.58 (1.49)	0.00 (0.96)	20.31 (10.87)	32.81 (22.29)
[1600/4144]	0.59 (1.49)	0.00 (0.95)	20.31 (11.42)	39.06 (23.21)
[1700/4144]	0.51 (1.49)	0.00 (0.95)	17.19 (11.94)	39.06 (24.02)
[1800/4144]	0.55 (1.49)	0.01 (0.95)	21.88 (12.46)	42.19 (24.86)
[1900/4144]	0.56 (1.49)	0.00 (0.95)	29.69 (12.96)	46.88 (25.66)
[2000/4144]	0.55 (1.49)	0.01 (0.95)	23.44 (13.39)	42.19 (26.39)
[2100/4144]	0.55 (1.49)	0.00 (0.95)	21.88 (13.85)	46.88 (27.13)
[2200/4144]	0.55 (1.49)	0.00 (0.95)	23.44 (14.30)	40.62 (27.81)
[2300/4144]	0.60 (1.49)	0.00 (0.95)	31.25 (14.72)	46.88 (28.48)
[2400/4144]	0.53 (1.49)	0.01 (0.95)	23.44 (15.19)	40.62 (29.19)
[2500/4144]	0.55 (1.49)	0.01 (0.95)	31.25 (15.58)	51.56 (29.78)
[2600/4144]	1.94 (1.48)	1.41 (0.95)	21.88 (15.97)	35.94 (30.33)
[2700/4144]	0.50 (1.49)	0.00 (0.95)	23.44 (16.33)	43.75 (30.90)
[2800/4144]	4.43 (1.49)	3.91 (0.95)	23.44 (16.71)	48.44 (31.46)
[2900/4144]	0.52 (1.48)	0.00 (0.95)	21.88 (17.05)	40.62 (32.00)
[3000/4144]	5.72 (1.48)	5.20 (0.95)	28.12 (17.39)	45.31 (32.51)
[3100/4144]	0.50 (1.48)	0.01 (0.95)	25.00 (17.72)	48.44 (32.99)
[3200/4144]	7.22 (1.48)	6.70 (0.95)	28.12 (18.06)	50.00 (33.48)
[3300/4144]	0.50 (1.48)	0.00 (0.95)	25.00 (18.41)	35.94 (33.96)
[3400/4144]	5.39 (1.48)	4.87 (0.95)	29.69 (18.72)	57.81 (34.43)
[3500/4144]	0.52 (1.48)	0.00 (0.95)	31.25 (19.04)	48.44 (34.88)
[3600/4144]	0.54 (1.48)	0.00 (0.95)	28.12 (19.36)	39.06 (35.33)
[3700/4144]	0.53 (1.48)	0.00 (0.95)	21.88 (19.66)	43.75 (35.77)
[3800/4144]	0.51 (1.48)	0.00 (0.95)	26.56 (19.96)	46.88 (36.17)
[3900/4144]	0.50 (1.48)	0.00 (0.95)	34.38 (20.27)	59.38 (36.59)
[4000/4144]	1.16 (1.48)	0.63 (0.95)	39.06 (20.56)	60.94 (36.98)
[4100/4144]	0.57 (1.48)	0.01 (0.95)	23.44 (20.84)	53.12 (37.37)
Validate:	Time		Prec@1		Prec@3

/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:75: 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:76: UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.

[0/553]	6.85 (6.85)	0.00 (0.00)	0.00 (0.00)
[100/553]	0.15 (0.87)	0.00 (0.00)	0.00 (0.03)
[200/553]	5.54 (0.86)	0.00 (0.00)	0.00 (0.03)
[300/553]	0.16 (0.84)	0.00 (0.00)	0.00 (0.02)
[400/553]	5.58 (0.84)	0.00 (0.00)	0.00 (0.02)
[500/553]	0.13 (0.83)	0.00 (0.00)	0.00 (0.02)
 * Prec@1 0.000 Prec@3 0.017
	Saving new best model
Epoch:1
Itr		Time		Data		Prec@1		Prec@3
[0/4144]	12.66 (12.66)	12.00 (12.00)	29.69 (29.69)	46.88 (46.88)
[100/4144]	0.53 (1.58)	0.00 (1.03)	34.38 (33.35)	65.62 (55.31)
[200/4144]	7.83 (1.55)	7.19 (1.01)	32.81 (33.90)	56.25 (55.74)
[300/4144]	0.52 (1.52)	0.01 (0.98)	32.81 (33.71)	59.38 (55.32)
[400/4144]	7.92 (1.52)	7.40 (0.99)	28.12 (33.70)	48.44 (55.16)
[500/4144]	0.54 (1.51)	0.00 (0.98)	34.38 (33.68)	53.12 (55.06)
[600/4144]	4.16 (1.51)	3.60 (0.98)	35.94 (33.77)	57.81 (55.10)
[700/4144]	0.51 (1.50)	0.00 (0.96)	35.94 (33.75)	50.00 (55.17)
[800/4144]	0.52 (1.50)	0.00 (0.97)	26.56 (33.94)	51.56 (55.28)
[900/4144]	0.50 (1.49)	0.00 (0.96)	35.94 (34.15)	48.44 (55.52)
[1000/4144]	1.33 (1.50)	0.73 (0.96)	42.19 (34.26)	53.12 (55.63)
[1100/4144]	0.58 (1.49)	0.00 (0.96)	34.38 (34.40)	56.25 (55.80)
[1200/4144]	0.54 (1.49)	0.01 (0.95)	32.81 (34.51)	54.69 (55.86)
[1300/4144]	0.54 (1.49)	0.01 (0.95)	43.75 (34.63)	54.69 (56.03)
[1400/4144]	0.52 (1.49)	0.00 (0.95)	40.62 (34.72)	57.81 (56.14)
[1500/4144]	0.52 (1.48)	0.01 (0.95)	25.00 (34.87)	51.56 (56.27)
[1600/4144]	0.52 (1.49)	0.01 (0.95)	34.38 (34.93)	48.44 (56.33)
[1700/4144]	0.59 (1.49)	0.00 (0.96)	31.25 (34.99)	54.69 (56.47)
[1800/4144]	0.47 (1.50)	0.00 (0.96)	45.31 (35.08)	60.94 (56.51)
[1900/4144]	0.50 (1.50)	0.00 (0.96)	32.81 (35.19)	56.25 (56.61)
[2000/4144]	0.48 (1.50)	0.00 (0.96)	39.06 (35.28)	60.94 (56.75)
[2100/4144]	0.51 (1.50)	0.01 (0.96)	37.50 (35.34)	57.81 (56.80)
[2200/4144]	0.54 (1.50)	0.00 (0.96)	35.94 (35.48)	54.69 (56.93)
[2300/4144]	0.55 (1.49)	0.00 (0.96)	35.94 (35.55)	62.50 (57.03)
[2400/4144]	0.63 (1.49)	0.00 (0.96)	40.62 (35.66)	65.62 (57.15)
[2500/4144]	0.52 (1.49)	0.01 (0.95)	34.38 (35.74)	59.38 (57.26)
[2600/4144]	0.79 (1.49)	0.29 (0.96)	31.25 (35.80)	48.44 (57.35)
[2700/4144]	0.47 (1.49)	0.00 (0.95)	43.75 (35.88)	59.38 (57.47)
[2800/4144]	0.58 (1.49)	0.00 (0.95)	42.19 (35.97)	54.69 (57.56)
[2900/4144]	0.52 (1.48)	0.00 (0.95)	40.62 (36.07)	57.81 (57.68)
[3000/4144]	0.51 (1.48)	0.00 (0.95)	32.81 (36.15)	51.56 (57.77)
[3100/4144]	0.54 (1.48)	0.00 (0.95)	39.06 (36.28)	70.31 (57.89)
[3200/4144]	0.52 (1.48)	0.00 (0.95)	35.94 (36.35)	56.25 (57.97)
[3300/4144]	0.51 (1.47)	0.01 (0.94)	31.25 (36.45)	56.25 (58.06)
[3400/4144]	1.14 (1.47)	0.68 (0.94)	46.88 (36.57)	70.31 (58.16)
[3500/4144]	0.49 (1.47)	0.00 (0.94)	42.19 (36.65)	65.62 (58.22)
[3600/4144]	0.69 (1.47)	0.20 (0.94)	35.94 (36.78)	54.69 (58.34)
[3700/4144]	0.51 (1.46)	0.00 (0.94)	35.94 (36.89)	51.56 (58.46)
[3800/4144]	6.62 (1.46)	6.11 (0.94)	28.12 (36.97)	56.25 (58.53)
[3900/4144]	0.57 (1.46)	0.00 (0.93)	45.31 (37.06)	70.31 (58.61)
[4000/4144]	7.73 (1.46)	7.21 (0.93)	45.31 (37.12)	68.75 (58.70)
[4100/4144]	0.60 (1.46)	0.00 (0.93)	39.06 (37.22)	68.75 (58.79)
Validate:	Time		Prec@1		Prec@3
[0/553]	7.50 (7.50)	0.00 (0.00)	0.00 (0.00)
[100/553]	0.15 (0.85)	0.00 (0.03)	0.00 (0.14)
[200/553]	3.97 (0.83)	0.00 (0.02)	0.00 (0.12)
[300/553]	0.16 (0.81)	0.00 (0.03)	0.00 (0.13)
[400/553]	3.35 (0.81)	0.00 (0.02)	0.00 (0.13)
[500/553]	0.18 (0.80)	0.00 (0.02)	0.00 (0.13)
 * Prec@1 0.025 Prec@3 0.144
	Saving new best model
Epoch:2
Itr		Time		Data		Prec@1		Prec@3
[0/4144]	13.66 (13.66)	13.02 (13.02)	45.31 (45.31)	60.94 (60.94)
[100/4144]	0.52 (1.46)	0.01 (0.93)	46.88 (42.87)	71.88 (64.36)
[200/4144]	4.43 (1.43)	3.83 (0.91)	48.44 (42.70)	60.94 (63.90)
[300/4144]	0.47 (1.41)	0.00 (0.88)	40.62 (42.37)	67.19 (63.86)
[400/4144]	3.01 (1.40)	2.51 (0.87)	51.56 (42.20)	67.19 (63.71)
[500/4144]	0.65 (1.42)	0.00 (0.90)	50.00 (42.40)	71.88 (63.86)
[600/4144]	0.52 (1.43)	0.00 (0.90)	48.44 (42.53)	71.88 (63.98)
[700/4144]	0.86 (1.44)	0.35 (0.91)	40.62 (42.59)	59.38 (63.98)
[800/4144]	1.60 (1.44)	1.08 (0.91)	28.12 (42.62)	56.25 (63.97)
[900/4144]	0.51 (1.45)	0.01 (0.92)	48.44 (42.63)	64.06 (64.05)
[1000/4144]	1.54 (1.46)	0.96 (0.93)	37.50 (42.67)	48.44 (64.07)
[1100/4144]	1.36 (1.47)	0.85 (0.93)	45.31 (42.81)	64.06 (64.17)
[1200/4144]	0.55 (1.47)	0.00 (0.94)	42.19 (42.88)	68.75 (64.21)
[1300/4144]	2.42 (1.48)	1.80 (0.94)	31.25 (42.92)	56.25 (64.32)
[1400/4144]	0.51 (1.48)	0.01 (0.94)	42.19 (42.96)	62.50 (64.36)
[1500/4144]	3.27 (1.48)	2.67 (0.94)	35.94 (43.00)	64.06 (64.40)
[1600/4144]	0.53 (1.48)	0.00 (0.95)	29.69 (43.02)	50.00 (64.41)
[1700/4144]	4.65 (1.48)	4.06 (0.95)	54.69 (43.09)	71.88 (64.49)
[1800/4144]	0.50 (1.49)	0.00 (0.95)	35.94 (43.11)	59.38 (64.48)
[1900/4144]	4.12 (1.49)	3.50 (0.95)	43.75 (43.09)	57.81 (64.49)
[2000/4144]	0.82 (1.48)	0.33 (0.95)	51.56 (43.13)	59.38 (64.53)
[2100/4144]	6.73 (1.49)	6.27 (0.95)	37.50 (43.19)	57.81 (64.62)
[2200/4144]	0.50 (1.48)	0.00 (0.95)	39.06 (43.25)	64.06 (64.63)
[2300/4144]	6.02 (1.48)	5.41 (0.95)	43.75 (43.32)	68.75 (64.68)
[2400/4144]	0.52 (1.48)	0.00 (0.94)	51.56 (43.36)	65.62 (64.70)
[2500/4144]	6.85 (1.48)	6.34 (0.94)	31.25 (43.42)	67.19 (64.81)
[2600/4144]	0.50 (1.47)	0.01 (0.94)	40.62 (43.45)	56.25 (64.83)
[2700/4144]	7.74 (1.47)	7.15 (0.94)	50.00 (43.51)	79.69 (64.89)
[2800/4144]	0.46 (1.47)	0.00 (0.93)	45.31 (43.53)	68.75 (64.91)
[2900/4144]	6.95 (1.47)	6.36 (0.93)	56.25 (43.54)	82.81 (64.94)
[3000/4144]	0.50 (1.46)	0.00 (0.93)	37.50 (43.60)	57.81 (64.99)
[3100/4144]	7.09 (1.46)	6.57 (0.93)	43.75 (43.62)	73.44 (65.02)
[3200/4144]	0.50 (1.46)	0.00 (0.92)	45.31 (43.66)	65.62 (65.04)
[3300/4144]	7.73 (1.45)	7.19 (0.92)	43.75 (43.75)	54.69 (65.09)
[3400/4144]	0.50 (1.45)	0.00 (0.92)	50.00 (43.76)	70.31 (65.10)
[3500/4144]	6.98 (1.45)	6.51 (0.92)	32.81 (43.81)	65.62 (65.15)
[3600/4144]	1.14 (1.45)	0.61 (0.92)	32.81 (43.83)	56.25 (65.18)
[3700/4144]	8.40 (1.45)	7.88 (0.92)	46.88 (43.88)	71.88 (65.24)
[3800/4144]	0.63 (1.45)	0.12 (0.92)	48.44 (43.92)	67.19 (65.25)
[3900/4144]	8.18 (1.45)	7.67 (0.92)	40.62 (43.97)	71.88 (65.34)
[4000/4144]	0.54 (1.45)	0.00 (0.92)	54.69 (44.01)	79.69 (65.37)
[4100/4144]	6.83 (1.46)	6.23 (0.93)	45.31 (44.05)	71.88 (65.41)
Validate:	Time		Prec@1		Prec@3
[0/553]	7.20 (7.20)	0.00 (0.00)	0.00 (0.00)
[100/553]	0.13 (0.90)	0.00 (0.11)	0.00 (0.22)
[200/553]	2.20 (0.87)	0.00 (0.09)	0.00 (0.23)
[300/553]	0.15 (0.86)	0.00 (0.08)	0.00 (0.20)
[400/553]	4.03 (0.86)	0.00 (0.07)	0.00 (0.19)
[500/553]	0.15 (0.86)	0.00 (0.07)	0.00 (0.18)
 * Prec@1 0.071 Prec@3 0.190
	Saving new best model
Epoch:3
Itr		Time		Data		Prec@1		Prec@3
[0/4144]	15.26 (15.26)	14.53 (14.53)	45.31 (45.31)	67.19 (67.19)
[100/4144]	0.51 (1.63)	0.00 (1.09)	42.19 (46.35)	59.38 (67.50)
[200/4144]	7.78 (1.60)	7.13 (1.06)	59.38 (46.74)	76.56 (67.81)
[300/4144]	0.53 (1.57)	0.00 (1.04)	45.31 (46.85)	67.19 (67.97)
[400/4144]	7.26 (1.57)	6.70 (1.04)	50.00 (46.91)	54.69 (68.17)
[500/4144]	0.58 (1.56)	0.00 (1.03)	45.31 (46.77)	70.31 (68.10)
[600/4144]	3.95 (1.56)	3.30 (1.02)	53.12 (46.95)	73.44 (68.22)
[700/4144]	0.51 (1.55)	0.00 (1.02)	43.75 (47.09)	68.75 (68.36)
[800/4144]	5.70 (1.55)	5.03 (1.02)	60.94 (47.01)	79.69 (68.39)
[900/4144]	0.52 (1.55)	0.00 (1.02)	57.81 (47.00)	71.88 (68.34)
[1000/4144]	8.32 (1.55)	7.66 (1.02)	42.19 (47.19)	57.81 (68.46)
[1100/4144]	0.54 (1.55)	0.00 (1.01)	51.56 (47.20)	64.06 (68.43)
[1200/4144]	9.32 (1.55)	8.73 (1.02)	51.56 (47.16)	73.44 (68.33)
[1300/4144]	0.56 (1.55)	0.00 (1.02)	56.25 (47.19)	85.94 (68.34)
[1400/4144]	6.88 (1.55)	6.29 (1.02)	54.69 (47.26)	68.75 (68.37)
[1500/4144]	1.79 (1.55)	1.28 (1.02)	54.69 (47.30)	68.75 (68.44)
[1600/4144]	6.66 (1.55)	6.05 (1.02)	56.25 (47.35)	65.62 (68.47)
[1700/4144]	0.55 (1.55)	0.01 (1.01)	54.69 (47.38)	70.31 (68.54)
[1800/4144]	7.53 (1.55)	7.00 (1.01)	50.00 (47.42)	75.00 (68.60)
[1900/4144]	0.52 (1.55)	0.01 (1.01)	43.75 (47.47)	60.94 (68.59)
[2000/4144]	8.83 (1.55)	8.21 (1.02)	50.00 (47.51)	71.88 (68.64)
[2100/4144]	0.48 (1.55)	0.00 (1.01)	46.88 (47.52)	71.88 (68.68)
[2200/4144]	9.08 (1.55)	8.47 (1.01)	48.44 (47.54)	60.94 (68.70)
[2300/4144]	0.52 (1.55)	0.01 (1.01)	40.62 (47.60)	65.62 (68.70)
[2400/4144]	9.24 (1.55)	8.71 (1.01)	39.06 (47.63)	67.19 (68.70)
[2500/4144]	0.48 (1.55)	0.00 (1.01)	34.38 (47.68)	59.38 (68.72)
[2600/4144]	8.75 (1.55)	8.22 (1.01)	50.00 (47.77)	71.88 (68.77)
[2700/4144]	0.53 (1.55)	0.01 (1.01)	46.88 (47.80)	70.31 (68.79)
[2800/4144]	8.14 (1.55)	7.52 (1.01)	48.44 (47.84)	68.75 (68.84)
[2900/4144]	0.51 (1.55)	0.01 (1.01)	51.56 (47.87)	67.19 (68.85)
[3000/4144]	8.26 (1.55)	7.70 (1.01)	42.19 (47.89)	64.06 (68.87)
[3100/4144]	0.57 (1.55)	0.00 (1.01)	57.81 (47.89)	79.69 (68.88)
[3200/4144]	3.08 (1.55)	2.58 (1.01)	43.75 (47.91)	65.62 (68.89)
[3300/4144]	0.51 (1.55)	0.01 (1.01)	40.62 (47.92)	67.19 (68.90)
[3400/4144]	6.10 (1.55)	5.61 (1.01)	39.06 (47.91)	57.81 (68.88)
[3500/4144]	0.52 (1.55)	0.00 (1.01)	64.06 (47.93)	81.25 (68.90)
[3600/4144]	6.01 (1.55)	5.48 (1.01)	53.12 (47.93)	71.88 (68.91)
[3700/4144]	0.58 (1.55)	0.00 (1.01)	53.12 (47.96)	71.88 (68.91)
[3800/4144]	5.78 (1.55)	5.27 (1.01)	48.44 (47.98)	62.50 (68.92)
[3900/4144]	0.53 (1.55)	0.00 (1.01)	43.75 (47.99)	68.75 (68.95)
[4000/4144]	0.95 (1.54)	0.36 (1.01)	57.81 (48.02)	76.56 (68.99)
[4100/4144]	0.53 (1.54)	0.01 (1.01)	53.12 (48.03)	75.00 (69.00)
Validate:	Time		Prec@1		Prec@3
[0/553]	7.53 (7.53)	0.00 (0.00)	0.00 (0.00)
[100/553]	0.14 (0.89)	0.00 (0.06)	0.00 (0.22)
[200/553]	4.23 (0.87)	0.00 (0.07)	0.00 (0.19)
[300/553]	0.15 (0.85)	0.00 (0.06)	0.00 (0.17)
[400/553]	5.84 (0.86)	0.00 (0.06)	0.00 (0.16)
[500/553]	0.15 (0.85)	0.00 (0.06)	0.00 (0.17)
 * Prec@1 0.059 Prec@3 0.173
Validate:	Time		Prec@1		Prec@3
[0/553]	7.79 (7.79)	0.00 (0.00)	0.00 (0.00)
[100/553]	0.15 (0.89)	0.00 (0.06)	0.00 (0.22)
[200/553]	1.85 (0.85)	0.00 (0.07)	0.00 (0.19)
[300/553]	0.17 (0.84)	0.00 (0.06)	0.00 (0.17)
[400/553]	2.78 (0.84)	0.00 (0.06)	0.00 (0.16)
[500/553]	0.18 (0.83)	0.00 (0.06)	0.00 (0.17)
 * Prec@1 0.059 Prec@3 0.173

Basic iNat2019 data exploration

From: https://www.kaggle.com/macaodha/basic-inat2019-data-exploration

Author: macaodha

Overview¶

This script will load the iNat2019 dataset, print a summary, and display a random image.

In [1]:

import numpy as np
import json
import os
import matplotlib.pyplot as plt
%matplotlib inline

# print contents of input directory
print(os.listdir("../input"))

['test2019.json', 'train_val2019', 'kaggle_sample_submission.csv', 'train2019.json', 'val2019.json', 'test2019']

In [2]:

# load train set meta data
train_anns_file = '../input/train2019.json'
with open(train_anns_file) as da:
    train_anns = json.load(da)

In [3]:

# print train set stats
print('Number of train images ' + str(len(train_anns['images'])))
print('Number of classes      ' + str(len(train_anns['categories'])))
category_ids = [cc['category_id'] for cc in train_anns['annotations']]
_, category_counts = np.unique(category_ids, return_counts=True)
plt.plot(np.sort(category_counts))
plt.title('classes sorted by amount of train images')
plt.xlabel('sorted class')
plt.ylabel('number of train images per class')
plt.show()

Number of train images 265213
Number of classes      1010

In [4]:

# display random image
rand_id = np.random.randint(len(train_anns['images']))
im_meta = train_anns['images'][rand_id]
im_category = train_anns['annotations'][rand_id]['category_id']
im = plt.imread('../input/train_val2019/' + im_meta['file_name'])
plt.imshow(im)
plt.title('image id: ' + str(rand_id) + ', class:' + str(im_category) + ', rights holder: ' + im_meta['rights_holder'])
plt.xticks([])
plt.yticks([])
plt.axis()
plt.show()

kernel11ab6576ac

From: https://www.kaggle.com/alainminda/kernel11ab6576ac

Author: robur

In [1]:

# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

import os
print(os.listdir("../input"))

# Any results you write to the current directory are saved as output.

['inceptionv3', 'inaturalist-2019-fgvc6']

In [2]:

import json

ann_file = '../input/inaturalist-2019-fgvc6/train2019.json'
with open(ann_file) as data_file:
        train_anns = json.load(data_file)

In [3]:

train_anns_df = pd.DataFrame(train_anns['annotations'])[['image_id','category_id']]
train_img_df = pd.DataFrame(train_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
df_train_file_cat = pd.merge(train_img_df, train_anns_df, on='image_id')
df_train_file_cat['category_id']=df_train_file_cat['category_id'].astype(str)
df_train_file_cat.head()

Out[3]:

| image_id | file_name | category_id
0 | 0 | train_val2019/Plants/400/d1322d13ccd856eb4236c... | 400
1 | 1 | train_val2019/Plants/570/15edbc1e2ef000d8ace48... | 570
2 | 2 | train_val2019/Reptiles/167/c87a32e8927cbf4f06d... | 167
3 | 3 | train_val2019/Birds/254/9fcdd1d37e96d8fd94dfdc... | 254
4 | 4 | train_val2019/Plants/739/ffa06f951e99de9d220ae... | 739

In [4]:

print(len(df_train_file_cat))

265213

In [5]:

# splitting data into train and validation
from sklearn.model_selection import train_test_split
train, valid = train_test_split(df_train_file_cat, stratify=df_train_file_cat.category_id, test_size=0.2)

In [6]:

nb_classes = 1010
batch_size = 64
img_size = 299
nb_epochs = 2

In [7]:

from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Add our data-augmentation parameters to ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255.,
                                   rotation_range = 20,
                                   width_shift_range = 0.2,
                                   height_shift_range = 0.2,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator( rescale = 1.0/255. )

# Flow training images in batches of 20 using train_datagen generator
train_generator = train_datagen.flow_from_dataframe(train,
                                                    directory="../input/inaturalist-2019-fgvc6/train_val2019",
                                                    x_col='file_name',
                                                    y_col='category_id',
                                                    batch_size = batch_size,
                                                    class_mode = 'categorical',
                                                    target_size = (img_size, img_size),
                                                    color_mode='rgb')

# Flow validation images in batches of 20 using test_datagen generator
validation_generator =  test_datagen.flow_from_dataframe( valid,
                                                          directory="../input/inaturalist-2019-fgvc6/train_val2019",
                                                          x_col='file_name',
                                                          y_col='category_id',
                                                          batch_size  = batch_size,
                                                          class_mode  = 'categorical',
                                                          target_size = (img_size, img_size),
                                                          color_mode='rgb')

Found 212170 images belonging to 1010 classes.
Found 53043 images belonging to 1010 classes.

In [8]:

from tensorflow.keras import layers
from tensorflow.keras import Model

from tensorflow.keras.applications.inception_v3 import InceptionV3

local_weights_file = '../input/inceptionv3/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'

pre_trained_model = InceptionV3(input_shape = (img_size, img_size, 3),
                                include_top = False,
                                weights = None)

pre_trained_model.load_weights(local_weights_file)
for layer in pre_trained_model.layers:
    layer.trainable = True
for layer in pre_trained_model.layers:
  if layer.name == 'mixed6':
    break
  layer.trainable = False

# pre_trained_model.summary()

last_layer = pre_trained_model.get_layer('mixed7')
print('last layer output shape: ', last_layer.output_shape)
last_output = last_layer.output

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
last layer output shape:  (None, 17, 17, 768)

In [9]:

# Check the trainable status of the individual layers
for layer in pre_trained_model.layers:
    print(layer, layer.trainable)

<tensorflow.python.keras.engine.input_layer.InputLayer object at 0x7fb4313ff710> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb4313ffa58> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb4313ffdd8> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb4313ffeb8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb430bf64a8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb4313d4b70> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb430c18780> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb43136fc50> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb431607668> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb43142c518> False
<tensorflow.python.keras.layers.pooling.MaxPooling2D object at 0x7fb4313869e8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb4313867b8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb43122d9e8> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb43122db38> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb431247a90> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb4311ded68> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb4311de588> False
<tensorflow.python.keras.layers.pooling.MaxPooling2D object at 0x7fb43117de48> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb430f96ac8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb430f2fc88> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb430f2fbe0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb4310bfeb8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb430ed16d8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb4310d25c0> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb430defac8> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb4310d2390> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb430e6a2b0> False
<tensorflow.python.keras.layers.pooling.AveragePooling2D object at 0x7fb430d4b780> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb43117d6a0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb430ff9898> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb430e10be0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb430d01cf8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb4310f5b38> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb430fdaf28> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb430da7780> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb430cc3588> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb4310f5c18> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb431012080> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb430da74a8> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb430cc3630> False
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb430c6def0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb4301dce48> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb4301dc668> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb43017b860> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb430364ef0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb43011bc50> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb43037a080> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb43012e5f8> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb43037a780> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb43012e240> False
<tensorflow.python.keras.layers.pooling.AveragePooling2D object at 0x7fb42fff5a90> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb430c6df60> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb43029fb70> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb4300d2eb8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42fff5a58> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb430c84080> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb4302b6e48> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb43006d048> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42ff95048> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb430c84320> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb4302b6630> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb43006d1d0> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42ff95a90> False
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42ff33940> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42fd46f98> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42fcdf4e0> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42fc586a0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42fe6c4a8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42fc81d30> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42fe0bac8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42fc1acc0> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42fe81278> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42fc1a470> False
<tensorflow.python.keras.layers.pooling.AveragePooling2D object at 0x7fb42fafbd30> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42ff33c50> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42fda5e10> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42fbbe7b8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42fb106d8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42ff458d0> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42fdbb550> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42fade940> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42fa6b908> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42ff45828> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42fdbb2b0> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42fbd7860> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42fa6ba20> False
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42fa35be0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f971ac8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f989518> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f989240> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f90edd8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f8a8240> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f8a8e80> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42fa356a0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f84d9e8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f9a9828> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f7709e8> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f9a9940> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f7e5cc0> False
<tensorflow.python.keras.layers.pooling.MaxPooling2D object at 0x7fb42f78be48> False
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42f78b5c0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f459ba8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f470e80> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f470668> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f398e80> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f3378d0> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f3aea58> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f6c77b8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f357be0> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f5e59b0> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f357278> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f6587f0> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f2eb630> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f5ffcc0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f28eef0> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f614710> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f22b048> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f614438> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f22b2e8> False
<tensorflow.python.keras.layers.pooling.AveragePooling2D object at 0x7fb42f0eeeb8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f78b6a0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f539e10> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f1b1ac8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f0ee550> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f6aa7b8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f553080> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f1b19b0> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42f066be0> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f6aaf28> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f553278> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f1575c0> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42f08e7f0> False
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42f02cdd8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42ed7e668> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42eca09b0> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42ed1a898> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42ecbecc0> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42ecce710> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42ecce550> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42ef69d68> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42ebf8860> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42ef69400> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42ec12080> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42ef7b320> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42ec122e8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42ef08fd0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42eb19ba8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42eea3a58> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42eb31e80> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42eebd080> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42eb313c8> False
<tensorflow.python.keras.layers.pooling.AveragePooling2D object at 0x7fb42e998be0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42f03e828> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42ee41fd0> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42ea59e80> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e998358> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42ef9fac8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42eddb6a0> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e9f98d0> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e98b860> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42ef9ffd0> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42eddb4a8> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42ea6fa58> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e98b978> False
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42e950b38> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e5a9e80> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e5bf5c0> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e5bf390> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e548ef0> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e4e3550> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e4fd128> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e88db00> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e485cf8> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e872d30> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e41ccc0> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e8407b8> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e41cba8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e7aacf8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e3c35c0> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e7c2198> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e2e1a90> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e7c2e10> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e3598d0> False
<tensorflow.python.keras.layers.pooling.AveragePooling2D object at 0x7fb42e23df28> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e904780> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e669978> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e2f7cf8> False
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e221f98> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e8c4a58> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e60bb38> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e317748> False
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e1b3588> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e8c46d8> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e686908> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e317588> False
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e1b3630> False
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42e175eb8> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42dece518> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42dece160> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42de654e0> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42ddedd30> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42de061d0> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42de06e48> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e099e48> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42dd2c9b0> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e0af2b0> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42dccd860> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e0afef0> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42dd43c18> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e053a90> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42dc69e10> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42dfedd68> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42dc7e860> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42dfed2b0> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42dc7e6a0> True
<tensorflow.python.keras.layers.pooling.AveragePooling2D object at 0x7fb42db45ac8> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42e15ce48> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42df91a90> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42dc08f28> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42db45cc0> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42e0eda20> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42deaf7b8> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42dba5588> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42da63278> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42e0ed4a8> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42df25940> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42dbc1080> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42da63fd0> True
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42da84ba8> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d8faeb8> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d819898> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d911a20> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d837ba8> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d84e5f8> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d84e320> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42da84eb8> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d771eb8> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d99f160> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d78e048> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d99feb8> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d78e1d0> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d9bfb38> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d713a90> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d9d3e10> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d6acd68> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d9d3358> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d6ac2b0> True
<tensorflow.python.keras.layers.pooling.MaxPooling2D object at 0x7fb42d651eb8> True
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42d5a6860> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d323b70> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d33b470> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d2c51d0> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d593550> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d262b70> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d4adb00> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d279e48> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d5248d0> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d279390> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d4c77b8> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d3e7f98> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d19def0> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d0debe0> True
<tensorflow.python.keras.layers.pooling.AveragePooling2D object at 0x7fb42d07aef0> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d651630> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d45d780> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d4044e0> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d1408d0> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d0f3630> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42d095240> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42d5c8b70> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d45d438> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d3786a0> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d1b4a58> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d0f3358> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42cfb9908> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42d5c8cf8> True
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42d323d30> True
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42d07ae80> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42cfb9f28> True
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42cfb9d68> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42ccab898> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42ccbf2e8> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42cc229b0> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42cef2cc0> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42cbe7cf8> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42cf0c550> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42cbfd6a0> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42cea8128> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42cbfd2e8> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42ce2dac8> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42cd6f6d8> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42cb80ef0> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42cac2b70> True
<tensorflow.python.keras.layers.pooling.AveragePooling2D object at 0x7fb42ca00f60> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42cfd3a58> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42ce47c88> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42cd0fa90> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42cb20080> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42ca5ae48> True
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb42c978c88> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42cf46860> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42ce47be0> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42cd84278> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42cb20a58> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42ca5a390> True
<tensorflow.python.keras.layers.normalization.BatchNormalizationV1 object at 0x7fb42c91f898> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42cf466d8> True
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42ccabdd8> True
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42ca00e48> True
<tensorflow.python.keras.layers.core.Activation object at 0x7fb42c93c7b8> True
<tensorflow.python.keras.layers.merge.Concatenate object at 0x7fb42c93c940> True

In [10]:

from tensorflow.keras.optimizers import RMSprop

# Flatten the output layer to 1 dimension
x = layers.Flatten()(last_output)
# Add a fully connected layer with 1,024 hidden units and ReLU activation
x = layers.Dense(1024, activation='relu')(x)
# Add a dropout rate of 0.2
x = layers.Dropout(rate=0.2)(x)
# Add a final sigmoid layer for classification
x = layers.Dense  (nb_classes, activation='softmax')(x)

model = Model( pre_trained_model.input, x)

model.compile(optimizer = RMSprop(lr=0.0001),
              loss = 'categorical_crossentropy',
              metrics = ['accuracy'])

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py:143: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.

In [11]:

history = model.fit_generator(train_generator, epochs=nb_epochs, validation_data = validation_generator, verbose = 1)

model.save("rps.h5")

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Epoch 1/2
829/829 [==============================] - 1134s 1s/step - loss: 3.8342 - acc: 0.2115
3316/3316 [==============================] - 10317s 3s/step - loss: 4.7124 - acc: 0.1258 - val_loss: 3.8342 - val_acc: 0.2115
Epoch 2/2
1808/3316 [===============>..............] - ETA: 1:08:56 - loss: 3.6260 - acc: 0.2327

In [12]:

import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()

plt.show()

<Figure size 432x288 with 0 Axes>

Xception Keras

From: https://www.kaggle.com/sujoykg/xception-keras

Author: Sujoy K Goswami

Score: 0.99106

Transfer learning from pretrained model using Keras.

In [1]:

import os
import cv2
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import json
from keras.models import Sequential, Model
from keras.layers import Dense, Flatten, Activation, Dropout, GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers, applications
from keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping
from keras import backend as K

Using TensorFlow backend.

Train data¶

In [2]:

ann_file = '../input/inaturalist-2019-fgvc6/train2019.json'
with open(ann_file) as data_file:
        train_anns = json.load(data_file)

In [3]:

train_anns_df = pd.DataFrame(train_anns['annotations'])[['image_id','category_id']]
train_img_df = pd.DataFrame(train_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
df_train_file_cat = pd.merge(train_img_df, train_anns_df, on='image_id')
df_train_file_cat['category_id']=df_train_file_cat['category_id'].astype(str)
df_train_file_cat.head()

Out[3]:

| image_id | file_name | category_id
0 | 0 | train_val2019/Plants/400/d1322d13ccd856eb4236c... | 400
1 | 1 | train_val2019/Plants/570/15edbc1e2ef000d8ace48... | 570
2 | 2 | train_val2019/Reptiles/167/c87a32e8927cbf4f06d... | 167
3 | 3 | train_val2019/Birds/254/9fcdd1d37e96d8fd94dfdc... | 254
4 | 4 | train_val2019/Plants/739/ffa06f951e99de9d220ae... | 739

In [4]:

len(df_train_file_cat['category_id'].unique())

Out[4]:

1010

In [5]:

# Example of images for category_id = 400
img_names = df_train_file_cat[df_train_file_cat['category_id']=='400']['file_name'][:30]

plt.figure(figsize=[15,15])
i = 1
for img_name in img_names:
    img = cv2.imread("../input/inaturalist-2019-fgvc6/train_val2019/%s" % img_name)[...,[2, 1, 0]]
    plt.subplot(6, 5, i)
    plt.imshow(img)
    i += 1
plt.show()

Validation data¶

In [6]:

valid_ann_file = '../input/inaturalist-2019-fgvc6/val2019.json'
with open(valid_ann_file) as data_file:
        valid_anns = json.load(data_file)

In [7]:

valid_anns_df = pd.DataFrame(valid_anns['annotations'])[['image_id','category_id']]
valid_anns_df.head()

Out[7]:

| image_id | category_id
0 | 265213 | 644
1 | 265214 | 597
2 | 265215 | 883
3 | 265216 | 300
4 | 265217 | 881

In [8]:

valid_img_df = pd.DataFrame(valid_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
valid_img_df.head()

Out[8]:

| image_id | file_name
0 | 265213 | train_val2019/Plants/644/716a69838526f3ada3b2f...
1 | 265214 | train_val2019/Plants/597/0942cc64d2e759c5ee059...
2 | 265215 | train_val2019/Plants/883/acfdbfd9fa675f1c84558...
3 | 265216 | train_val2019/Birds/300/5f3194ff536c7dd31d80b7...
4 | 265217 | train_val2019/Plants/881/76acaf0b2841f91982d21...

In [9]:

df_valid_file_cat = pd.merge(valid_img_df, valid_anns_df, on='image_id')
df_valid_file_cat['category_id']=df_valid_file_cat['category_id'].astype(str)
df_valid_file_cat.head()

Out[9]:

| image_id | file_name | category_id
0 | 265213 | train_val2019/Plants/644/716a69838526f3ada3b2f... | 644
1 | 265214 | train_val2019/Plants/597/0942cc64d2e759c5ee059... | 597
2 | 265215 | train_val2019/Plants/883/acfdbfd9fa675f1c84558... | 883
3 | 265216 | train_val2019/Birds/300/5f3194ff536c7dd31d80b7... | 300
4 | 265217 | train_val2019/Plants/881/76acaf0b2841f91982d21... | 881

In [10]:

nb_classes = 1010
batch_size = 64
img_size = 80
nb_epochs = 30

In [11]:

%%time
train_datagen=ImageDataGenerator(rescale=1./255,
    validation_split=0.25,
    horizontal_flip = True,
    zoom_range = 0.3,
    width_shift_range = 0.3,
    height_shift_range=0.3
    )

train_generator=train_datagen.flow_from_dataframe(
    dataframe=df_train_file_cat,
    directory="../input/inaturalist-2019-fgvc6/train_val2019",
    x_col="file_name",
    y_col="category_id",
    batch_size=batch_size,
    shuffle=True,
    class_mode="categorical",
    target_size=(img_size,img_size))

Found 265213 images belonging to 1010 classes.
CPU times: user 5.7 s, sys: 8.5 s, total: 14.2 s
Wall time: 1min 39s

In [12]:

%%time
test_datagen = ImageDataGenerator(rescale=1./255)

valid_generator=test_datagen.flow_from_dataframe(
    dataframe=df_valid_file_cat,
    directory="../input/inaturalist-2019-fgvc6/train_val2019",
    x_col="file_name",
    y_col="category_id",
    batch_size=batch_size,
    shuffle=True,
    class_mode="categorical",
    target_size=(img_size,img_size))

Found 3030 images belonging to 1010 classes.
CPU times: user 76 ms, sys: 88 ms, total: 164 ms
Wall time: 1.16 s

Model¶

In [13]:

model = applications.Xception(weights='imagenet',
                              include_top=False,
                              input_shape=(img_size, img_size, 3))
#model.load_weights('../input/NASNet-large-no-top/NASNet-large-no-top.h5')
#model.summary()

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels_notop.h5
83689472/83683744 [==============================] - 2s 0us/step

In [14]:

# Freeze last 5 layers
for layer in model.layers[:-5]:
    layer.trainable = False

In [15]:

#Adding custom layers
x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.5)(x)
predictions = Dense(nb_classes, activation="softmax")(x)
model_final = Model(input = model.input, output = predictions)

model_final.compile(optimizers.rmsprop(lr=0.0001, decay=1e-6),loss='categorical_crossentropy',metrics=['accuracy'])

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.

/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:7: UserWarning: Update your `Model` call to the Keras 2 API: `Model(inputs=Tensor("in..., outputs=Tensor("de...)`
  import sys

In [16]:

# Callbacks

checkpoint = ModelCheckpoint("vgg16_1.h5", monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=7, verbose=2, mode='auto')

In [17]:

%%time
history = model_final.fit_generator(generator=train_generator,
                                    steps_per_epoch=500,
                                    validation_data=valid_generator,
                                    validation_steps=200,
                                    epochs=nb_epochs,
                                    callbacks = [checkpoint, early],
                                    verbose=2)

WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Epoch 1/30
 - 911s - loss: 6.8118 - acc: 0.0044 - val_loss: 6.9597 - val_acc: 0.0049

Epoch 00001: val_loss improved from inf to 6.95973, saving model to vgg16_1.h5
Epoch 2/30
 - 896s - loss: 6.5046 - acc: 0.0108 - val_loss: 7.1008 - val_acc: 0.0038

Epoch 00002: val_loss did not improve from 6.95973
Epoch 3/30
 - 875s - loss: 6.3273 - acc: 0.0164 - val_loss: 7.0901 - val_acc: 0.0055

Epoch 00003: val_loss did not improve from 6.95973
Epoch 4/30
 - 878s - loss: 6.1926 - acc: 0.0184 - val_loss: 7.1257 - val_acc: 0.0074

Epoch 00004: val_loss did not improve from 6.95973
Epoch 5/30
 - 884s - loss: 6.1270 - acc: 0.0239 - val_loss: 7.1310 - val_acc: 0.0075

Epoch 00005: val_loss did not improve from 6.95973
Epoch 6/30
 - 915s - loss: 6.0510 - acc: 0.0276 - val_loss: 7.1678 - val_acc: 0.0071

Epoch 00006: val_loss did not improve from 6.95973
Epoch 7/30
 - 895s - loss: 6.0149 - acc: 0.0275 - val_loss: 7.1353 - val_acc: 0.0081

Epoch 00007: val_loss did not improve from 6.95973
Epoch 8/30
 - 890s - loss: 5.9669 - acc: 0.0303 - val_loss: 7.0645 - val_acc: 0.0095

Epoch 00008: val_loss did not improve from 6.95973
Epoch 00008: early stopping
CPU times: user 1h 43min 41s, sys: 3min 14s, total: 1h 46min 56s
Wall time: 1h 59min 13s

In [18]:

with open('history.json', 'w') as f:
    json.dump(history.history, f)

history_df = pd.DataFrame(history.history)
history_df[['loss', 'val_loss']].plot()
history_df[['acc', 'val_acc']].plot()

Out[18]:

<matplotlib.axes._subplots.AxesSubplot at 0x7f94c143b160>

Test data¶

In [19]:

test_ann_file = '../input/inaturalist-2019-fgvc6/test2019.json'
with open(test_ann_file) as data_file:
        test_anns = json.load(data_file)

In [20]:

test_img_df = pd.DataFrame(test_anns['images'])[['id', 'file_name']].rename(columns={'id':'image_id'})
test_img_df.head()

Out[20]:

| image_id | file_name
0 | 268243 | test2019/e295f3c7046b1f1e80c0301401324aa9.jpg
1 | 268244 | test2019/ad3dcbb6846ed0b4dab58d7b1a4210ba.jpg
2 | 268245 | test2019/e697be8e296b4b140cff4f96f85c364f.jpg
3 | 268246 | test2019/7e7ba55e6aa26ba99e814d63b15d0121.jpg
4 | 268247 | test2019/6cb6372079d23702511c06923970f13f.jpg

In [21]:

%%time

test_generator = test_datagen.flow_from_dataframe(

        dataframe=test_img_df,

        directory = "../input/inaturalist-2019-fgvc6/test2019",
        x_col="file_name",
        target_size = (img_size,img_size),
        batch_size = 1,
        shuffle = False,
        class_mode = None
        )

Found 35350 images.
CPU times: user 1.5 s, sys: 796 ms, total: 2.29 s
Wall time: 11.5 s

Prediction¶

In [22]:

%%time
test_generator.reset()
predict=model_final.predict_generator(test_generator, steps = len(test_generator.filenames))

CPU times: user 41min 19s, sys: 1min 12s, total: 42min 32s
Wall time: 40min 59s

In [23]:

len(predict)

Out[23]:

35350

In [24]:

predicted_class_indices=np.argmax(predict,axis=1)

In [25]:

labels = (train_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
predictions = [labels[k] for k in predicted_class_indices]

In [26]:

sam_sub_df = pd.read_csv('../input/inaturalist-2019-fgvc6/kaggle_sample_submission.csv')
sam_sub_df.head()

Out[26]:

| id | predicted
0 | 268243 | 842
1 | 268244 | 139
2 | 268245 | 988
3 | 268246 | 612
4 | 268247 | 468

In [27]:

filenames=test_generator.filenames
results=pd.DataFrame({"file_name":filenames,
                      "predicted":predictions})
df_res = pd.merge(test_img_df, results, on='file_name')[['image_id','predicted']]\
    .rename(columns={'image_id':'id'})

df_res.head()

Out[27]:

| id | predicted
0 | 268243 | 285
1 | 268244 | 320
2 | 268245 | 976
3 | 268246 | 373
4 | 268247 | 434

In [28]:

df_res.to_csv("submission.csv",index=False)

kernelfea8793d3e

From: https://www.kaggle.com/chrisevans/kernelfea8793d3e

Author: ChrisEvans

In [1]:

# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

import os
print(os.listdir("../input"))

# Any results you write to the current directory are saved as output.

['kaggle_sample_submission.csv', 'train_val2019', 'val2019.json', 'train2019.json', 'test2019.json', 'test2019']

In [2]:

cd train_val2019

[Errno 2] No such file or directory: 'train_val2019'
/kaggle/working

In [3]:

!ls Birds/202/0046f8c09d5d6acaa78baeffb2ba5c43.jpg

ls: cannot access 'Birds/202/0046f8c09d5d6acaa78baeffb2ba5c43.jpg': No such file or directory

In [4]:

from matplotlib.image import imread
import numpy as np

data = imread('Birds/202/0046f8c09d5d6acaa78baeffb2ba5c43.jpg')

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-4-6b778f749de4> in <module>()
      2 import numpy as np
      3
----> 4 data = imread('Birds/202/0046f8c09d5d6acaa78baeffb2ba5c43.jpg')
      5

/opt/conda/lib/python3.6/site-packages/matplotlib/image.py in imread(fname, format)
   1349                              'with Pillow installed matplotlib can handle '
   1350                              'more images' % list(handlers))
-> 1351         with Image.open(fname) as image:
   1352             return pil_to_array(image)
   1353

/opt/conda/lib/python3.6/site-packages/PIL/Image.py in open(fp, mode)
   2546
   2547     if filename:
-> 2548         fp = builtins.open(filename, "rb")
   2549         exclusive_fp = True
   2550

FileNotFoundError: [Errno 2] No such file or directory: 'Birds/202/0046f8c09d5d6acaa78baeffb2ba5c43.jpg'

In [5]:

data

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-c5d84736ba45> in <module>()
----> 1 data

NameError: name 'data' is not defined

In [6]:

from PIL import Image

img = Image.fromarray(data, 'RGB')
img

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-e3d8ff92350a> in <module>()
      1 from PIL import Image
      2
----> 3 img = Image.fromarray(data, 'RGB')
      4 img

NameError: name 'data' is not defined