!unzip '/content/flowers_google.zip' -d '/content/flower/train/'
!unzip '/content/test_flower.zip' -d '/content/flower/test/'
# !unzip 'source_location' -d 'destination_location'
from fastai.vision import *
from fastai import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('/content/flowers_idx.csv')
label = pd.read_csv('/content/flowers_label.csv')
df.head()
label.head()
path = Path('/content/flower/train/') # Locates to the folder of train images
path1 = Path('/content/flower/test/') # Locates to the folder of test images
tfms = get_transforms(do_flip=True,max_rotate=0.1,max_lighting=0.15) # Each time it gets the image it will added some kind of transform like flip some times, rotate some times
test = (ImageList.from_folder(path1,extensions='.jpeg')) # creating a test set
data = (ImageList.from_df(df,path,folder='flowers_google',suffix='.jpeg',cols='id')
# Creating a image list from dataframe folder= folder_name, suffix = image_etxn, cols = column that contains image ids
.split_by_rand_pct(0.15)
# will take 15% of images as validation set
.label_from_df(cols='flower_cls')
# Label from dataframe cols= columns that contains category
.transform(tfms)
# add transforms
.add_test(test)
# add a test set
.databunch(bs=128)
# create a databuch for the model
.normalize(imagenet_stats))
# normalize with imagenet stats because we are using a pretrained resnet model which was trained on image net
data.show_batch(rows=3) # rows = no. of rows
len(data.classes),len(data.train_ds),len(data.valid_ds),len(data.test_ds)
fb = FBeta()
fb.average='macro'
learn = cnn_learner(data,models.resnet50,metrics=[accuracy,fb]).to_fp16() # .to_fp16() uses mixed precision traing computes operation in 16 bit floating point numbers
learn.lr_find()
learn.recorder.plot()
gc.collect()
lr = 1e-2
learn.fit_one_cycle(10,lr,moms=(0.8,0.7)) # training for 10 epochs
learn.save('model1') # let's save the model
learn.load('model1') # let's load it again and unfreeze it for further training
learn.unfreeze()
#learn.export()
learn.lr_find() # let's use learning rate again to train some more
learn.recorder.plot()
gc.collect()
learn.fit_one_cycle(3,1e-5,wd=0.2) # wd = weight decay it's regularization method to stop model from overfitting
learn.save('model2')
learn.load('model2')
learn.unfreeze()
#learn.to_fp32().export('/content/flower91.pkl')
learn.to_fp32().export()
#!cp '/content/flower91.pkl' '/content/drive/My Drive/Dataset'
learn.lr_find()
learn.recorder.plot()
gc.collect()
learn.freeze_to(-2)
learn.fit_one_cycle(2,1e-6,wd=0.3)
learn32 = load_learner(path)
img = open_image('/content/flower/test/d9cb87ad0.jpeg') # open the image using ope_image func from fast.ai
print(learn32.predict(img)[0]) # lets make some prediction
img
#!cp '/content/flower.pkl' '/content/drive/My Drive/Dataset/'
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_top_losses(12,figsize=(20,8))
interp.most_confused(min_val=3) # here it show max number of times it was confused between which clases
pred,output=learn.get_preds(DatasetType.Test) # getting prediction of Test folder
# using
learn.to_fp32().export('flower.pkl') # converting model back to 32 bit floating point numbers to save the model