So, in the end, this is how my code looks like:
First, transfer learning (follows notebook from lesson3),
-
Load Resnet50 with the weights from imagenet:
from fastai.vision import *
from fastai import *path = Config.data_path()/‘MYPATH’
path.mkdir(parents=True, exist_ok=True)df = pd.read_csv(path/‘train_v2.csv’)
tfms = get_transforms(flip_vert=True, max_lighting=0.1, max_zoom=1.05, max_warp=0.)
np.random.seed(42)
src = (ImageList.from_csv(path, ‘train_v2.csv’, folder=‘train-jpg’, suffix=’.jpg’)
.split_by_rand_pct(0.2)
.label_from_df(label_delim=’ '))
data = (src.transform(tfms, size=128)
.databunch().normalize(imagenet_stats))arch = models.resnet50
acc_02 = partial(accuracy_thresh, thresh=0.2)
f_score = partial(fbeta, thresh=0.2)
learn = cnn_learner(data, arch, metrics=[acc_02, f_score]) -
Then, fit the model to the planet database.
learn.lr_find()
lr = 0.01
learn.fit_one_cycle(5, slice(lr))learn.model[-1][-1]=nn.Linear(in_features=512,out_features=5, bias=True)
learn.save(‘Planetstage-1-rn50’)
Notice that before saving we change the number of categories that the model outputs so we can then open it with the other data (we change from 7 to 5).
I also re-run the whole thing changing the last bit with:
learn.unfreeze()
learn.lr_find()
learn.fit_one_cycle(5, slice(1e-5, lr/5))
learn.model[-1][-1]=nn.Linear(in_features=512,out_features=5, bias=True)
learn.save('Planetstage-2-rn50')
Second, Re-train the model fitted for “Planet” with my images
Then I loaded my images with the tweaked models (do not worry about the “self” parts, this is inside of a python class, they mainly carry the information on where to find the images):
path = Config.data_path()/self.path
path.mkdir(parents=True, exist_ok=True)
df = pd.read_csv(self.path+self.labelFileName)
df.head()
tfms = get_transforms(flip_vert=True, max_lighting=0.1, max_zoom=1.05, max_warp=0.)
np.random.seed(42)
src = (ImageList.from_csv(path, self.labelFileName, folder=self.imageDir, suffix=self.suffix).split_by_rand_pct(0.2).label_from_df(label_delim=' '))
data = (src.transform(tfms, size=128).databunch().normalize(imagenet_stats))
arch = models.resnet50
acc_02 = partial(accuracy_thresh, thresh=0.2)
f_score = partial(fbeta, thresh=0.2)
learn = cnn_learner(data, arch, metrics=[acc_02, f_score])
learn.load(self.modelFile)
And then I was able to fit the model again.
Hope this helps.