Sota Image Classification Models With Pytorch Api
Sota Image Classification Models With Pytorch Api
Sota Image Classification Models With Pytorch Api
We improve the top-1 accuracy in one of our previous image classification projects from
91.35 % to 94.73%.
We mainly used :
[ ]: import time
import copy
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import transforms, datasets
[ ]: from FixRes.transforms_v2 import get_transforms # Implement FixRes method for␣
,→fine-tuning
[ ]: torch.__version__
1
[ ]: '1.5.0+cu101'
[ ]: root_model = 'drive/My Drive/Colab Notebooks/P7/model/'
[ ]: dir_train = 'drive/My Drive/Colab Notebooks/P7/data/train'
[ ]: dir_valid = 'drive/My Drive/Colab Notebooks/P7/data/valid'
[ ]: dir_test = 'drive/My Drive/Colab Notebooks/P7/data/test'
#FixRes method for image preprocessing
[ ]: fixRes_image_transforms = get_transforms(input_size=320,test_size=224,
kind='full', crop=True,
need=('train', 'val'),
backbone=None)
#Data Loader
[ ]: batch_size = 32
[ ]: data_images = {
'train': datasets.ImageFolder(root=dir_train,
transform=fixRes_image_transforms['train']),
'valid': datasets.ImageFolder(root=dir_valid,
transform=fixRes_image_transforms['val_test']),
'test': datasets.ImageFolder(root=dir_test,
transform=fixRes_image_transforms['val_test'])
}
[ ]: dataloaders = {
'train': DataLoader(data_images['train'],
batch_size=batch_size,
shuffle=True),
'valid': DataLoader(data_images['valid'],
batch_size=batch_size,
shuffle=True),
'test': DataLoader(data_images['test'],
batch_size=batch_size,
shuffle=True)
}
[ ]: dataset_sizes = {x: len(data_images[x]) for x in ['train', 'valid', 'test']}
[ ]: dataset_sizes
[ ]: {'test': 4140, 'train': 12240, 'valid': 4200}
[ ]: class_names = data_images['train'].classes
[ ]: class_names[:3]
[ ]: ['Afghan_hound', 'African_hunting_dog', 'Airedale']
2
#ResNext as fixed feature extractor
[ ]: # Load resnext model
fixRes_model = torch.hub.load('facebookresearch/WSL-Images',
'resnext101_32x48d_wsl')
Downloading: "https://github.com/facebookresearch/WSL-Images/archive/master.zip"
to /root/.cache/torch/hub/master.zip
Downloading:
"https://download.pytorch.org/models/ig_resnext101_32x48-3e41cc8a.pth" to
/root/.cache/torch/hub/checkpoints/ig_resnext101_32x48-3e41cc8a.pth
[ ]: criterion = nn.CrossEntropyLoss()
[ ]: optimizer = optim.Adam(fixRes_model.parameters())
#Training
3
[ ]: def train_model(model, criterion, optimizer, num_epochs=30):
since = time.time()
global history
history = {'train':{'val_accuracy':[], 'val_loss':[]},
'valid':{'val_accuracy':[], 'val_loss':[]}}
# Weights model
best_acc = 0.0
best_model = copy.deepcopy(model.state_dict())
running_loss = 0.0
running_corrects = 0
i = 0
print('{} :'.format(phase), end=" ")
for inputs, labels in dataloaders[phase]:
inputs = inputs.to(device)
labels = labels.to(device)
# forward
# track history if only in train
with torch.set_grad_enabled(phase == 'train'):
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
4
loss.backward()
optimizer.step()
# statistics
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
# display evolving
if(not i % 50):
print("{}%".format((i*100//(dataset_sizes[phase]//
,→batch_size))), end=" => ")
i += 1
5
print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed//60,
time_elapsed%60))
print('Best val Acc: {:4f}'.format(best_acc))
Epoch 1/30
----------
train : 0% => 13% => 26% => 39% => 52% => 65% => 78% => 91% => 100%
train Loss: 1.3776 Acc: 0.7016
valid : 0% => 38% => 76% => 100%
valid Loss: 0.3538 Acc: 0.9190
Valeur de perte minimale : 0.3538; Nombre epochs non amélioré : 0
Epoch 2/30
----------
train : 0% => 13% => 26% => 39% => 52% => 65% => 78% => 91% => 100%
train Loss: 0.8354 Acc: 0.7993
valid : 0% => 38% => 76% => 100%
valid Loss: 0.3248 Acc: 0.9271
Valeur de perte minimale : 0.3248; Nombre epochs non amélioré : 0
Epoch 3/30
----------
train : 0% => 13% => 26% => 39% => 52% => 65% => 78% => 91% => 100%
train Loss: 0.8409 Acc: 0.8105
valid : 0% => 38% => 76% => 100%
valid Loss: 0.3177 Acc: 0.9407
Valeur de perte minimale : 0.3177; Nombre epochs non amélioré : 0
Epoch 4/30
----------
train : 0% => 13% => 26% => 39% => 52% => 65% => 78% => 91% => 100%
train Loss: 0.8790 Acc: 0.8068
valid : 0% => 38% => 76% => 100%
valid Loss: 0.3381 Acc: 0.9336
Valeur de perte minimale : 0.3177; Nombre epochs non amélioré : 1
Epoch 5/30
----------
train : 0% => 13% => 26% => 39% => 52% => 65% => 78% => 91% => 100%
train Loss: 0.7823 Acc: 0.8261
valid : 0% => 38% => 76% => 100%
valid Loss: 0.3380 Acc: 0.9340
6
Valeur de perte minimale : 0.3177; Nombre epochs non amélioré : 2
Epoch 6/30
----------
train : 0% => 13% => 26% => 39% => 52% => 65% => 78% => 91% => 100%
train Loss: 0.8061 Acc: 0.8230
valid : 0% => 38% => 76% => 100%
valid Loss: 0.3516 Acc: 0.9386
Valeur de perte minimale : 0.3177; Nombre epochs non amélioré : 3
Epoch 7/30
----------
train : 0% => 13% => 26% => 39% => 52% => 65% => 78% => 91% => 100%
train Loss: 0.8011 Acc: 0.8297
valid : 0% => 38% => 76% => 100%
valid Loss: 0.3929 Acc: 0.9345
Valeur de perte minimale : 0.3177; Nombre epochs non amélioré : 4
Epoch 8/30
----------
train : 0% => 13% => 26% => 39% => 52% => 65% => 78% => 91% => 100%
train Loss: 0.8176 Acc: 0.8324
valid : 0% => 38% => 76% => 100%
valid Loss: 0.3819 Acc: 0.9424
Early stopping!
Training complete in 356m 18s
Best val Acc: 0.942381
7
[ ]: fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(111)
ax.plot(history['train']['val_loss'], color='b', label="Training accuracy")
ax.plot(history['valid']['val_loss'], color='r',label="Validation accuracy")
ax.set_xticks(np.arange(1, 30, 1))
legend = plt.legend(loc='best', shadow=True)
plt.tight_layout()
plt.show()
#Evaluation
8
[ ]: def evaluation(fixRes_model):
equals = 0
i = 0
corrects = 0
with torch.no_grad():
for data, targets in dataloaders['test']:
data = data.to(device)
targets = targets.to(device)
outputs = fixRes_model(data)
_, preds = torch.max(outputs, 1)
if(not i % 20):
print("{}%".format((i*100//(dataset_sizes["test"]//
,→batch_size))), end=" => ")
i += 1
print("100%")
print("Accuracy is : {}".format(corrects.item()/dataset_sizes['test']))
[ ]: evaluation(fixRes_model)
0% => 15% => 31% => 46% => 62% => 77% => 93% => 100%
Accuracy is : 0.9473429951690822
9
with torch.no_grad():
for i, (inputs, labels) in enumerate(dataloaders['test']):
inputs = inputs.to(device)
labels = labels.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
for j in range(inputs.size()[0]):
images_so_far += 1
ax = plt.subplot(num_images//2, 2, images_so_far)
ax.axis('off')
ax.set_title('predicted: {}'.format(class_names[preds[j]]))
imshow(inputs.cpu().data[j])
if images_so_far == num_images:
model.train(mode=was_training)
return
model.train(mode=was_training)
[ ]: visualize_model(fixRes_model)
10