New model based on zoomed-in images of the butterflies. Do the bounding box predictions, and zooming in on them, help us get better performance in the classification task?
This notebook should be almost identical to the the '3 - Basic Train' notebook (so we're comparing like-for-like in training approach) except that we are processing the zoomed
subfolder of zoomed-in images created in notebook number 4. Take a look at notebook 3 if you would like more explanation about the code itself.
from fastai.script import *
from fastai.vision import *
from fastai.callbacks import *
from fastai.distributed import *
from fastprogress import fastprogress
torch.backends.cudnn.benchmark = True
fastprogress.MAX_COLS = 80
n_gpus = num_distrib() or 1
workers = min(8, num_cpus()//n_gpus)
size = 256 # Image size width and height
bs = 32 # Batch size
if torch.cuda.is_available():
torch.cuda.set_device(0)
np.random.seed(13)
BUTTERFLIES_BBOXES_FILEPATH = Path('./butterflies_bboxes.csv')
IMAGE_FOLDER = Path('./butterfly_medium_images/zoomed')
df = pd.read_csv(BUTTERFLIES_BBOXES_FILEPATH)
df = df[df['exclude'] == 0]
df = df.reset_index(drop=True)
# Take first 80% of each class as training data.
# Prepare a list of indexes that have been selected for the validation set.
train_class_count = int(0.8*df['class'].value_counts().values.min())
valid_idxs = []
for c in df['class'].unique(): # Step through 'gatekeeper_butterfly' and 'meadow_brown_butterfly'
valid_idxs.extend(df[df['class'] == c].index.values[train_class_count:])
print(len(valid_idxs), len(df))
111 541
data = (ImageList.from_df(df, path=IMAGE_FOLDER, folder='', cols='filename')
.split_by_idx(valid_idxs)
.label_from_df('class')
.transform(get_transforms(), size=size)
.databunch(bs=bs, num_workers=workers)
.presize(size, scale=(0.75,1))
.normalize(imagenet_stats))
data.show_batch(ds_type=DatasetType.Train)
# Set up training params
lr = 1e-5 # Learning rate
alpha = 0.9 # Alpha
mom = 0.9 # Momentum
eps = 1e-7 # epsilon
# Use Adam optimiser
opt_func = partial(optim.Adam, betas=(mom,alpha), eps=eps)
learn = cnn_learner(data, models.resnet50, metrics=[accuracy, error_rate])
learn.fit_one_cycle(10, lr)
epoch | train_loss | valid_loss | accuracy | error_rate | time |
---|---|---|---|---|---|
0 | 0.939168 | 0.709669 | 0.468468 | 0.531532 | 00:21 |
1 | 0.897783 | 0.756983 | 0.468468 | 0.531532 | 00:10 |
2 | 0.926424 | 0.752058 | 0.495495 | 0.504505 | 00:10 |
3 | 0.899932 | 0.728457 | 0.513514 | 0.486486 | 00:10 |
4 | 0.901617 | 0.707840 | 0.540541 | 0.459459 | 00:10 |
5 | 0.892678 | 0.681837 | 0.531532 | 0.468468 | 00:10 |
6 | 0.893380 | 0.668000 | 0.540541 | 0.459459 | 00:10 |
7 | 0.873984 | 0.671693 | 0.540541 | 0.459459 | 00:10 |
8 | 0.870603 | 0.678714 | 0.513514 | 0.486486 | 00:10 |
9 | 0.869204 | 0.671934 | 0.522523 | 0.477477 | 00:10 |
learn.save('butterflies-zoom-1')
#learn.load('butterflies-zoom-1');
lr = 1e-4 # Try a higher learning rate
learn.unfreeze()
learn.fit_one_cycle(5, lr)
epoch | train_loss | valid_loss | accuracy | error_rate | time |
---|---|---|---|---|---|
0 | 0.851383 | 0.538164 | 0.711712 | 0.288288 | 00:15 |
1 | 0.725030 | 0.496404 | 0.801802 | 0.198198 | 00:13 |
2 | 0.601951 | 0.536164 | 0.837838 | 0.162162 | 00:13 |
3 | 0.512691 | 0.498893 | 0.837838 | 0.162162 | 00:13 |
4 | 0.452123 | 0.499345 | 0.837838 | 0.162162 | 00:13 |
learn.save('butterflies-zoom-2')
#learn.load('butterflies-zoom-2');
learn.show_results()