Jovian
⭐️
Sign In
In [1]:
%matplotlib inline
%reload_ext autoreload
%autoreload 
In [2]:
import jovian
from fastai.vision import *
import pandas as pd
In [3]:
# get_category_names
with open('list_category_cloth.txt', 'r') as f:
    categories = []
    for i, line in enumerate(f.readlines()):
        if i > 1:
            categories.append(line.split(' ')[0])
In [4]:
len(categories)
Out[4]:
50
In [5]:
# get image category map
with open('list_category_image.txt', 'r') as f:
    images = []
    for i, line in enumerate(f.readlines()):
        if i > 1:
            images.append([word.strip() for word in line.split(' ') if len(word) > 0])
In [6]:
images[-5:]
Out[6]:
[['img/Paisley_Print_Babydoll_Dress/img_00000050.jpg', '41'],
 ['img/Paisley_Print_Babydoll_Dress/img_00000051.jpg', '41'],
 ['img/Paisley_Print_Babydoll_Dress/img_00000052.jpg', '41'],
 ['img/Paisley_Print_Babydoll_Dress/img_00000053.jpg', '41'],
 ['img/Paisley_Print_Babydoll_Dress/img_00000054.jpg', '41']]
In [7]:
len(images)
Out[7]:
289222
In [8]:
#get train, valid, test split
with open('list_eval_partition.txt', 'r') as f:
    images_partition = []
    for i, line in enumerate(f.readlines()):
        if i > 1:
            images_partition.append([word.strip() for word in line.split(' ') if len(word) > 0])
In [9]:
images_partition[-5:]
Out[9]:
[['img/Paisley_Print_Babydoll_Dress/img_00000050.jpg', 'train'],
 ['img/Paisley_Print_Babydoll_Dress/img_00000051.jpg', 'train'],
 ['img/Paisley_Print_Babydoll_Dress/img_00000052.jpg', 'train'],
 ['img/Paisley_Print_Babydoll_Dress/img_00000053.jpg', 'train'],
 ['img/Paisley_Print_Babydoll_Dress/img_00000054.jpg', 'val']]
In [10]:
len(images_partition)
Out[10]:
289222
In [11]:
data_df = pd.DataFrame(images, columns=['images', 'category_label'])
partition_df = pd.DataFrame(images_partition, columns=['images', 'dataset'])
In [12]:
data_df['category_label'] = data_df['category_label'].astype(int)
In [13]:
data_df['category_label'].max()
Out[13]:
48
In [14]:
data_df.dtypes
Out[14]:
images            object
category_label     int64
dtype: object
In [15]:
partition_df.head()
Out[15]:
In [16]:
partition_df.dtypes
Out[16]:
images     object
dataset    object
dtype: object
In [17]:
data_df = data_df.merge(partition_df, on='images')
In [18]:
data_df['dataset'].value_counts()
Out[18]:
train    209222
test      40000
val       40000
Name: dataset, dtype: int64
In [19]:
data_df['category'] = data_df['category_label'].apply(lambda x: categories[int(x) - 1])
In [20]:
data_df['category_label'].nunique()
# seems like few labels were merged in Dress label
Out[20]:
46

prepare DataBunch

In [21]:
from pathlib import Path

images_path = Path('/home/jupyter/deepFashion')
In [22]:
images_path.exists()
Out[22]:
True
In [23]:
data_source = (ImageList.from_df(df=data_df, path=images_path, cols='images')
            .split_by_idxs((data_df[data_df['dataset']=='train'].index), (data_df[data_df['dataset']=='val'].index))
            .label_from_df(cols='category')
            )
In [24]:
tmfs = get_transforms()

data = data_source.transform(tmfs, size=64).databunch(bs=256).normalize(imagenet_stats)
data_224 = data_source.transform(tmfs, size=64).databunch(bs=256).normalize(imagenet_stats)
In [25]:
test_data = ImageList.from_df(df=data_df[data_df['dataset'] == 'test'], path=images_path, cols='images')
data.add_test(test_data)
In [26]:
data.device

Out[26]:
device(type='cuda')
In [30]:
data_224.show_batch()
Notebook Image
In [27]:
data
Out[27]:
ImageDataBunch;

Train: LabelList (209222 items)
x: ImageList
Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64)
y: CategoryList
Blouse,Blouse,Blouse,Blouse,Blouse
Path: /home/jupyter/deepFashion;

Valid: LabelList (40000 items)
x: ImageList
Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64)
y: CategoryList
Blouse,Blouse,Blouse,Blouse,Blouse
Path: /home/jupyter/deepFashion;

Test: LabelList (40000 items)
x: ImageList
Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64)
y: EmptyLabelList
,,,,
Path: /home/jupyter/deepFashion

Applying ResNet-50

In [28]:
from fastai.metrics import accuracy, top_k_accuracy

top_3_accuracy = partial(top_k_accuracy, k=3)
top_5_accuracy = partial(top_k_accuracy, k=5)
In [30]:
rn50_learn = cnn_learner(data, models.resnet50, metrics=[accuracy, top_3_accuracy, top_5_accuracy])

Going Parallel on multiple GPUs

In [31]:
rn50_learn.model = torch.nn.DataParallel(rn50_learn.model)
In [32]:
rn50_learn.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [33]:
rn50_learn.recorder.plot()
Notebook Image
In [34]:
rn50_learn.fit_one_cycle(5, max_lr=1e-03)
In [35]:
rn50_learn.save('resnet50-64-freezed')

Applying EfficientNet-B3

In [29]:
import torch
from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b3')
Loaded pretrained weights for efficientnet-b3

cannot apply cnn_learner, throws an error

en_learn = cnn_learner(data, model, metrics=[accuracy, top_3_accuracy, top_5_accuracy])

So I have to do all that's done in create_cnn_model

create_body

gets all the layers except the last, I guess you can cut in the middle to make this work.

create_head

  • reads the last output feature layer length
  • add a 512 linear layer in the middle and last output layer of length data.c
  • it applies AdaptiveConcatPool2d, efficientNet applies Conv2dSamePadding, TODO-findout what both are
  • I'll try using Conv2dSamePadding given by EfficientNet first
  • It joins the body and head, passes it to learner

Workarounds to make cnn_learner work

models.resnet50(True) returns the model. this line causes the issue with EN, I can take care of this by creating a wrapper

The next prob is the cut, Seems like I have to pass a custom_cut This was solved with a passing a cut at last layer

The next problem is that it can't find the output feature count of last layer in body, because some forward method is notImplemented After a lot of digging around, found the issue in fastAi, EfficientNet uses ModuleList in it's architecture, this does not have a forward method. Only work around is passing a custom_head

With all these changes, cnn_learner still doesn't work, I'll use Learner directly

In [29]:
# This must be done in model_sizes() to make it work with cnn_learner
# model_body = nn.Sequential(*(list(model.children())[:5]))
# db = dummy_batch(model_body, (64, 64))
# with hook_outputs(model_body) as hooks:
#     print(hooks)
#     #model_body.eval()(db)
#     #print(model_body.eval().forward)
#     container = model_body.eval()
#     inp = db
#     for module in container._modules.values():
#         if type(module) is torch.nn.modules.container.ModuleList:
#             for m in module:
#                 inp = m.forward(inp)
#                 print(inp.shape,'in modulelist')
#         #print(module.forward)
#         else:
#             inp = module.forward(inp)
#             print(inp.shape)
#             print('------------------------------')
In [36]:
ef_model = EfficientNet.from_pretrained('efficientnet-b3')
Loaded pretrained weights for efficientnet-b3
In [37]:
ef_head = create_head(1536, data.c, concat_pool=False)
ef_head = nn.Sequential(*(list(ef_head.children())[3:]))
In [38]:
ef_model._fc = ef_head
In [39]:
ef_model
Out[39]:
EfficientNet(
  (_conv_stem): Conv2dSamePadding(3, 40, kernel_size=(3, 3), stride=(2, 2), bias=False)
  (_bn0): BatchNorm2d(40, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
  (_blocks): ModuleList(
    (0): MBConvBlock(
      (_depthwise_conv): Conv2dSamePadding(40, 40, kernel_size=(3, 3), stride=[1, 1], groups=40, bias=False)
      (_bn1): BatchNorm2d(40, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(40, 10, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(10, 40, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(40, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(24, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (1): MBConvBlock(
      (_depthwise_conv): Conv2dSamePadding(24, 24, kernel_size=(3, 3), stride=(1, 1), groups=24, bias=False)
      (_bn1): BatchNorm2d(24, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(24, 6, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(6, 24, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(24, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(24, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (2): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(24, 144, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(144, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(144, 144, kernel_size=(3, 3), stride=[2, 2], groups=144, bias=False)
      (_bn1): BatchNorm2d(144, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(144, 6, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(6, 144, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(144, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(32, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (3): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(32, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(192, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(192, 192, kernel_size=(3, 3), stride=(1, 1), groups=192, bias=False)
      (_bn1): BatchNorm2d(192, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(192, 8, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(8, 192, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(192, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(32, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (4): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(32, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(192, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(192, 192, kernel_size=(3, 3), stride=(1, 1), groups=192, bias=False)
      (_bn1): BatchNorm2d(192, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(192, 8, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(8, 192, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(192, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(32, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (5): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(32, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(192, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(192, 192, kernel_size=(5, 5), stride=[2, 2], groups=192, bias=False)
      (_bn1): BatchNorm2d(192, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(192, 8, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(8, 192, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(192, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(48, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (6): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(48, 288, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(288, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(288, 288, kernel_size=(5, 5), stride=(1, 1), groups=288, bias=False)
      (_bn1): BatchNorm2d(288, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(288, 12, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(12, 288, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(288, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(48, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (7): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(48, 288, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(288, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(288, 288, kernel_size=(5, 5), stride=(1, 1), groups=288, bias=False)
      (_bn1): BatchNorm2d(288, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(288, 12, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(12, 288, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(288, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(48, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (8): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(48, 288, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(288, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(288, 288, kernel_size=(3, 3), stride=[2, 2], groups=288, bias=False)
      (_bn1): BatchNorm2d(288, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(288, 12, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(12, 288, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(288, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(96, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (9): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(96, 576, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(576, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(576, 576, kernel_size=(3, 3), stride=(1, 1), groups=576, bias=False)
      (_bn1): BatchNorm2d(576, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(576, 24, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(24, 576, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(576, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(96, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (10): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(96, 576, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(576, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(576, 576, kernel_size=(3, 3), stride=(1, 1), groups=576, bias=False)
      (_bn1): BatchNorm2d(576, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(576, 24, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(24, 576, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(576, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(96, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (11): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(96, 576, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(576, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(576, 576, kernel_size=(3, 3), stride=(1, 1), groups=576, bias=False)
      (_bn1): BatchNorm2d(576, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(576, 24, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(24, 576, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(576, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(96, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (12): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(96, 576, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(576, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(576, 576, kernel_size=(3, 3), stride=(1, 1), groups=576, bias=False)
      (_bn1): BatchNorm2d(576, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(576, 24, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(24, 576, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(576, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(96, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (13): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(96, 576, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(576, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(576, 576, kernel_size=(5, 5), stride=[1, 1], groups=576, bias=False)
      (_bn1): BatchNorm2d(576, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(576, 24, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(24, 576, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(576, 136, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(136, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (14): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(136, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(816, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(816, 816, kernel_size=(5, 5), stride=(1, 1), groups=816, bias=False)
      (_bn1): BatchNorm2d(816, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(816, 34, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(34, 816, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(816, 136, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(136, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (15): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(136, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(816, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(816, 816, kernel_size=(5, 5), stride=(1, 1), groups=816, bias=False)
      (_bn1): BatchNorm2d(816, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(816, 34, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(34, 816, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(816, 136, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(136, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (16): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(136, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(816, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(816, 816, kernel_size=(5, 5), stride=(1, 1), groups=816, bias=False)
      (_bn1): BatchNorm2d(816, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(816, 34, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(34, 816, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(816, 136, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(136, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (17): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(136, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(816, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(816, 816, kernel_size=(5, 5), stride=(1, 1), groups=816, bias=False)
      (_bn1): BatchNorm2d(816, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(816, 34, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(34, 816, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(816, 136, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(136, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (18): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(136, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(816, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(816, 816, kernel_size=(5, 5), stride=[2, 2], groups=816, bias=False)
      (_bn1): BatchNorm2d(816, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(816, 34, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(34, 816, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(816, 232, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(232, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (19): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(232, 1392, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(1392, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(1392, 1392, kernel_size=(5, 5), stride=(1, 1), groups=1392, bias=False)
      (_bn1): BatchNorm2d(1392, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(1392, 58, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(58, 1392, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(1392, 232, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(232, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (20): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(232, 1392, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(1392, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(1392, 1392, kernel_size=(5, 5), stride=(1, 1), groups=1392, bias=False)
      (_bn1): BatchNorm2d(1392, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(1392, 58, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(58, 1392, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(1392, 232, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(232, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (21): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(232, 1392, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(1392, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(1392, 1392, kernel_size=(5, 5), stride=(1, 1), groups=1392, bias=False)
      (_bn1): BatchNorm2d(1392, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(1392, 58, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(58, 1392, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(1392, 232, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(232, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (22): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(232, 1392, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(1392, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(1392, 1392, kernel_size=(5, 5), stride=(1, 1), groups=1392, bias=False)
      (_bn1): BatchNorm2d(1392, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(1392, 58, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(58, 1392, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(1392, 232, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(232, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (23): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(232, 1392, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(1392, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(1392, 1392, kernel_size=(5, 5), stride=(1, 1), groups=1392, bias=False)
      (_bn1): BatchNorm2d(1392, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(1392, 58, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(58, 1392, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(1392, 232, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(232, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (24): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(232, 1392, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(1392, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(1392, 1392, kernel_size=(3, 3), stride=[1, 1], groups=1392, bias=False)
      (_bn1): BatchNorm2d(1392, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(1392, 58, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(58, 1392, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(1392, 384, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(384, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
    (25): MBConvBlock(
      (_expand_conv): Conv2dSamePadding(384, 2304, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn0): BatchNorm2d(2304, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_depthwise_conv): Conv2dSamePadding(2304, 2304, kernel_size=(3, 3), stride=(1, 1), groups=2304, bias=False)
      (_bn1): BatchNorm2d(2304, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
      (_se_reduce): Conv2dSamePadding(2304, 96, kernel_size=(1, 1), stride=(1, 1))
      (_se_expand): Conv2dSamePadding(96, 2304, kernel_size=(1, 1), stride=(1, 1))
      (_project_conv): Conv2dSamePadding(2304, 384, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (_bn2): BatchNorm2d(384, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    )
  )
  (_conv_head): Conv2dSamePadding(384, 1536, kernel_size=(1, 1), stride=(1, 1), bias=False)
  (_bn1): BatchNorm2d(1536, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
  (_fc): Sequential(
    (0): Dropout(p=0.25)
    (1): Linear(in_features=1536, out_features=512, bias=True)
    (2): ReLU(inplace)
    (3): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (4): Dropout(p=0.5)
    (5): Linear(in_features=512, out_features=46, bias=True)
  )
)
In [66]:
en_learn = Learner(
    data,
    ef_model,
    metrics=[accuracy, top_3_accuracy, top_5_accuracy],
)
In [41]:
en_learn.model = torch.nn.DataParallel(en_learn.model)
In [48]:
en_learn.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [49]:
en_learn.recorder.plot()
Notebook Image
In [50]:
en_learn.fit_one_cycle(5, max_lr=1e-02)
In [51]:
en_learn.recorder.plot_metrics()
Notebook Image
In [52]:
en_learn.recorder.plot_losses()
Notebook Image
In [53]:
en_learn.save('efficientNet-b3-64-freezed')
In [42]:
en_learn = en_learn.load('efficientNet-b3-64-freezed')
In [54]:
interp = ClassificationInterpretation.from_learner(en_learn)
In [55]:
interp.most_confused(100)
Out[55]:
[('Blouse', 'Tee', 428),
 ('Romper', 'Dress', 417),
 ('Tee', 'Blouse', 377),
 ('Skirt', 'Dress', 369),
 ('Top', 'Tee', 336),
 ('Top', 'Blouse', 334),
 ('Tank', 'Tee', 299),
 ('Blouse', 'Dress', 295),
 ('Tee', 'Tank', 267),
 ('Cardigan', 'Sweater', 255),
 ('Tee', 'Dress', 255),
 ('Jumpsuit', 'Dress', 242),
 ('Tee', 'Sweater', 242),
 ('Sweater', 'Tee', 225),
 ('Top', 'Tank', 203),
 ('Tank', 'Dress', 190),
 ('Cutoffs', 'Shorts', 189),
 ('Blouse', 'Tank', 188),
 ('Dress', 'Skirt', 177),
 ('Cardigan', 'Blouse', 166),
 ('Sweater', 'Blouse', 166),
 ('Shorts', 'Skirt', 162),
 ('Cardigan', 'Dress', 161),
 ('Blouse', 'Sweater', 158),
 ('Jacket', 'Cardigan', 158),
 ('Blazer', 'Jacket', 157),
 ('Jacket', 'Blazer', 157),
 ('Shorts', 'Tee', 154),
 ('Top', 'Dress', 140),
 ('Cardigan', 'Jacket', 138),
 ('Dress', 'Blouse', 136),
 ('Tee', 'Shorts', 119),
 ('Blazer', 'Cardigan', 117),
 ('Skirt', 'Shorts', 115),
 ('Sweatpants', 'Joggers', 115),
 ('Jacket', 'Blouse', 114),
 ('Kimono', 'Cardigan', 114),
 ('Sweater', 'Cardigan', 111),
 ('Tank', 'Blouse', 111),
 ('Dress', 'Tee', 107)]
In [56]:
interp.plot_top_losses(12,heatmap=False,figsize=(15,15))
Notebook Image

Increase img size - progressive resizing

In [43]:
data_128 = data_source.transform(tmfs, size=128).databunch(bs=256//2).normalize(imagenet_stats)
In [44]:
en_learn.data = data_128
In [45]:
en_learn.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-45-54638ecbcc89> in <module> ----> 1 en_learn.lr_find() /opt/anaconda3/lib/python3.7/site-packages/fastai/train.py in lr_find(learn, start_lr, end_lr, num_it, stop_div, wd) 30 cb = LRFinder(learn, start_lr, end_lr, num_it, stop_div) 31 epochs = int(np.ceil(num_it/len(learn.data.train_dl))) ---> 32 learn.fit(epochs, start_lr, callbacks=[cb], wd=wd) 33 34 def to_fp16(learn:Learner, loss_scale:float=None, max_noskip:int=1000, dynamic:bool=True, clip:float=None, /opt/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks) 197 callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks) 198 if defaults.extra_callbacks is not None: callbacks += defaults.extra_callbacks --> 199 fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks) 200 201 def create_opt(self, lr:Floats, wd:Floats=0.)->None: /opt/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in fit(epochs, learn, callbacks, metrics) 99 for xb,yb in progress_bar(learn.data.train_dl, parent=pbar): 100 xb, yb = cb_handler.on_batch_begin(xb, yb) --> 101 loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler) 102 if cb_handler.on_batch_end(loss): break 103 /opt/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler) 33 loss,skip_bwd = cb_handler.on_backward_begin(loss) 34 if not skip_bwd: loss.backward() ---> 35 if not cb_handler.on_backward_end(): opt.step() 36 if not cb_handler.on_step_end(): opt.zero_grad() 37 /opt/anaconda3/lib/python3.7/site-packages/fastai/callback.py in step(self) 55 for p in pg2['params']: p.data.mul_(1 - wd*lr) 56 self.set_val('weight_decay', listify(0, self._wd)) ---> 57 self.opt.step() 58 59 def zero_grad(self)->None: /opt/anaconda3/lib/python3.7/site-packages/torch/optim/adam.py in step(self, closure) 91 92 # Decay the first and second moment running average coefficient ---> 93 exp_avg.mul_(beta1).add_(1 - beta1, grad) 94 exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad) 95 if amsgrad: KeyboardInterrupt:
In [72]:
en_learn.recorder.plot()
Notebook Image
In [73]:
en_learn.fit_one_cycle(5, max_lr=1e-03)



In [78]:
en_learn.save('efficientNet-b3-128-freezed')
In [67]:
en_learn = en_learn.load('efficientNet-b3-128-freezed')
In [62]:
data_224 = data_source.transform(tmfs, size=224).databunch(bs=256//2).normalize(imagenet_stats)
In [68]:
en_learn.data = data_224
In [69]:
en_learn.model = torch.nn.DataParallel(en_learn.model)
In [58]:
en_learn.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [59]:
en_learn.recorder.plot()
Notebook Image
In [70]:
en_learn.fit_one_cycle(10, max_lr=4e-04)
In [71]:
en_learn.unfreeze()
In [73]:
en_learn.freeze_to(-1)
In [74]:
en_learn.create_opt(defaults.lr)
In [75]:
en_learn.save('efficientNet-b3-224-freezed')
In [85]:
en_learn = en_learn.load('efficientNet-b3-224-freezed')
In [86]:
en_learn.unfreeze()
In [87]:
en_learn.lr_find(start_lr=1e-12)
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [81]:
en_learn.recorder.plot()
Notebook Image
In [88]:
en_learn.fit_one_cycle(2, max_lr=1e-04)
In [91]:
# thats the best val_loss I have had
In [93]:
en_learn.save('efficientNet-b3-224-unfreezed')
In [94]:
en_learn.freeze_to(-1)
en_learn.create_opt(defaults.lr)
In [99]:
en_learn.lr_find(start_lr=1e-8)
en_learn.recorder.plot()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
Notebook Image
In [100]:
en_learn.fit_one_cycle(5, max_lr=1e-05)
In [101]:
en_learn.recorder.plot_losses()
Notebook Image
In [102]:
en_learn.save('efficientNet-b3-224-freezed2')

Applying EfficientNet b5

In [30]:
efb5_model = EfficientNet.from_pretrained('efficientnet-b5')
Loaded pretrained weights for efficientnet-b5
In [31]:
ef_head = create_head(2048, data.c, concat_pool=False)
ef_head = nn.Sequential(*(list(ef_head.children())[3:]))
In [32]:
efb5_model._fc = ef_head
In [33]:
enb5_learner = Learner(
    data_224,
    efb5_model,
    metrics=[accuracy, top_3_accuracy, top_5_accuracy],
)
In [39]:
enb5_learner.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [40]:
enb5_learner.recorder.plot()
Notebook Image
In [41]:
enb5_learner.model = torch.nn.DataParallel(enb5_learner.model)
In [42]:
enb5_learner.fit_one_cycle(5, max_lr=3e-03)
In [43]:
enb5_learner.recorder.plot_losses()
Notebook Image
In [44]:
enb5_learner.recorder.plot_lr()