cd /content/drive/My Drive/Logical-Rhythm-2k20/Sports_Image_Classification
cd /content/drive/My Drive/Logical-Rhythm-2k20/
# ls
Sports_Image_Classification
Train
Sports_name_1
Sports_name_2
Val
Sports_name_1
Sports_name_2
test
test
import fastai
print(fastai.__version__ )
import numpy as np
from fastai import *
from fastai.vision import *
from fastai.callbacks import *
import warnings
warnings.filterwarnings('ignore')
pwd
path = Path('/content/drive/My Drive/Logical-Rhythm-2k20/Sports_Image_Classification')
path
# path.ls()
# Try and Experiment with values to get better results
# do_flip = True --> a random flip is applied with probability 0.5
# flip_vert = True --> requires do_flip = True, the image will be flipped vertically or rotated by 90 degrees
# max_rotate = 10.0 --> random rotation between -10 to +10 with probability of p_affine
# max_zoom = 1.1 --> random zoom applied between 1 and 1.1 with probability of p_affine
# max_lighting = 0.2 --> lighting and contrast of magnitude between -0.2 and 0.2 applied with probability of p_lighting
# max_wrap = 0.2 --> symmetric warp of magnitude between -0.2 and 0.2 with probability of p_affine
# p_affine = 0.75 --> 0.75 probability used to apply max_rotate, max_zoom, max_wrap
# p_lighting = 0.75 --> 0.75 probability used to apply max_lighting
# trfm = get_transforms(do_flip=True, flip_vert=True, max_rotate=10.0, max_zoom=1.1, max_lighting=0.2, max_warp=0.2, p_affine=0.75, p_lighting=0.75)
trfm = get_transforms()
np.random.seed(42)
data = ImageDataBunch.from_folder(path, train='Train', valid='Val', test = 'test',ds_tfms=trfm, size=224, num_workers=4, bs=32).normalize(imagenet_stats)
len(data.classes)
# data.show_batch(rows=3, figsize=(9,10))
data.classes, data.c, len(data.train_ds), len(data.valid_ds), len(data.test_ds)
learn = cnn_learner(data, models.densenet161, metrics=[error_rate, accuracy])
First find best learning_rate using lr_find()
Train the model using fit_one_cycle(no_of_epochs, range_of_learning_rate)
Now comes Fine-Tuning part
find range of learning_rate
#### For more details see Sylvain Gugger Blog
learn.lr_find()
learn.recorder.plot()
# lr = 3e-02 or lr = 1e-01 gives worst result accuracy does not increase after 1st epoch and valid_loss never decreases
# trying lr = 1e-02
lr = 1e-02
lr
learn.callback_fns.append(partial(SaveModelCallback, name='res50-stage-1'))
learn.callback_fns.append(partial(SaveModelCallback, name='densenet-stage-1'))
learn.fit_one_cycle(15, slice(lr))
learn.load('densenet-stage-1'); # remove ';' to see model
from pathlib import Path
p = Path("/content/drive/My Drive/Logical-Rhythm-2k20/Sports_Image_Classification/test/test")
dire = p.glob("*.jpg")
img_pred = {}
for img_path in dire:
img = open_image(img_path)
pred_class,pred_idx,outputs = learn.predict(img)
img_label = str(img_path).split('/')[-1]
img_pred[img_label] = pred_class
print(len(img_pred))
imggg = open_image('/content/drive/My Drive/Logical-Rhythm-2k20/Sports_Image_Classification/test/test/0.jpg')
pred_class,pred_idx,outputs = learn.predict(imggg)
print(pred_class, pred_idx, outputs, img_path)
test_images_list = pd.read_csv('/content/drive/My Drive/Logical-Rhythm-2k20/Sports_Image_Classification/test_images_list.csv')
test_images_list.head()
df_pred = pd.DataFrame(columns=['image', 'sports'])
for idx in test_images_list.index:
img_name = test_images_list['image'][idx]
img_no = test_images_list['image'][idx].split('/')[-1]
label = img_pred[img_no]
df_pred = df_pred.append({'image' : img_name, 'sports' : label}, ignore_index=True)
print(df_pred.shape)
print(df_pred.head())
df_pred.to_csv('my_sub_fastai_dense_1.csv', index = False)