<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Multi-label classification
Step1:  Multi-label versus single-label classification
Step2:  In single-label classification each sample belongs to one class. In the previous example, each image is either a dog or a cat.
Step3:  In multi-label classification each sample can belong to one or more clases. In the previous example, the first images belongs to two clases
Step4:  We use a different set of data augmentations for this dataset - we also allow vertical flips, since we don't expect vertical orientation of satellite images to change our classifications.
Step5:  Begin Training with 64 x 64
Generate Data
Step7:  Find Optimal Learning Rate
Step8:  Looking at the above plot, we can see that 0.1 seems the best learning rate.
Traing with optimal learning rate
Step9:  Unfreeze and Train with differential learning rate
Step10:  Begin Training with 128 x 128
Generate dataset
Step11:  Freeze for Transfer Learning
Step12:  F2 Score not improving
Unfreeze and train with differential learning rate
Step13:  TODO
Step14:  
Step15:  Sample Predictions
<ASSISTANT_TASK:>
Python Code:
%reload_ext autoreload
%autoreload 2
%matplotlib inline
from fastai.conv_learner import *
import torch
torch.cuda.is_available()
PATH = '../data/planet/'
ls {PATH}
!ls {PATH}train-jpg/ | wc -l
!ls {PATH}test-jpg/ | wc -l
Explanation: Multi-label classification
End of explanation
from fastai.plots import *
def get_1st(path): return glob(f'{path}/*.*')[0]
dc_path = "../data/dogscats/valid/"
list_paths = [get_1st(f"{dc_path}cats"), get_1st(f"{dc_path}dogs")]
plots_from_files(list_paths, titles=["cat", "dog"], maintitle="Single-label classification")
Explanation: Multi-label versus single-label classification
End of explanation
list_paths = [f"{PATH}train-jpg/train_0.jpg", f"{PATH}train-jpg/train_1.jpg"]
titles=["haze primary", "agriculture clear primary water"]
plots_from_files(list_paths, titles=titles, maintitle="Multi-label classification")
Explanation: In single-label classification each sample belongs to one class. In the previous example, each image is either a dog or a cat.
End of explanation
from helper.planet import f2
metrics=[f2]
f_model = resnet34
label_csv = f'{PATH}train_v2.csv'
n = len(list(open(label_csv)))-1
val_idxs = get_cv_idxs(n)
Explanation: In multi-label classification each sample can belong to one or more clases. In the previous example, the first images belongs to two clases: haze and primary. The second image belongs to four clases: agriculture, clear, primary and  water.
Multi-label models for Planet dataset
End of explanation
def get_data(sz):
    tfms = tfms_from_model(f_model, sz, aug_tfms=transforms_top_down, max_zoom=1.05)
    return ImageClassifierData.from_csv(PATH, 'train-jpg', label_csv, tfms=tfms,
                    suffix='.jpg', val_idxs=val_idxs, test_name='test-jpg')
data = get_data(256)
data.classes
x,y = next(iter(data.val_dl))
x.shape
y.shape
sample = to_np(x[0])
sample_denorm = data.val_ds.denorm(sample)[0]
plt.imshow(sample_denorm)
list(zip(data.classes, y[0]))
plt.imshow(data.val_ds.denorm(to_np(x))[0]*1.5);
Explanation: We use a different set of data augmentations for this dataset - we also allow vertical flips, since we don't expect vertical orientation of satellite images to change our classifications.
End of explanation
sz=64
data = get_data(sz)
data = data.resize(int(sz*1.3), 'tmp')
x,y = next(iter(data.trn_dl))
x.shape, y.shape
sample = to_np(x[0])
plt.imshow(data.val_ds.denorm(sample)[0]*1.2)
metrics
Explanation: Begin Training with 64 x 64
Generate Data
End of explanation
learn = ConvLearner.pretrained(f_model, data, metrics=metrics)
lrf=learn.lr_find()
learn.sched.plot()
learn.sched.plot_loss()
def plot_loss_change(sched, sma=1, n_skip=20, y_lim=(-0.01,0.01)):
    
    Plots rate of change of the loss function.
    Parameters:
        sched - learning rate scheduler, an instance of LR_Finder class.
        sma - number of batches for simple moving average to smooth out the curve.
        n_skip - number of batches to skip on the left.
        y_lim - limits for the y axis.
    
    derivatives = [0] * (sma + 1)
    for i in range(1 + sma, len(learn.sched.lrs)):
        derivative = (learn.sched.losses[i] - learn.sched.losses[i - sma]) / sma
        derivatives.append(derivative)
        
    plt.ylabel("d/loss")
    plt.xlabel("learning rate (log scale)")
    plt.plot(learn.sched.lrs[n_skip:], derivatives[n_skip:])
    plt.xscale('log')
    plt.ylim(y_lim)
plot_loss_change(learn.sched, sma=20)
Explanation: Find Optimal Learning Rate
End of explanation
lr = 0.1
learn.fit(lr, 3, cycle_len=1, cycle_mult=2)
Explanation: Looking at the above plot, we can see that 0.1 seems the best learning rate.
Traing with optimal learning rate
End of explanation
lrs = np.array([lr/9,lr/3,lr])
learn.unfreeze()
learn.fit(lrs, 3, cycle_len=1, cycle_mult=2)
learn.save(f'{sz}')
learn.sched.plot_loss()
learn.sched.plot_lr()
Explanation: Unfreeze and Train with differential learning rate
End of explanation
sz=128
Explanation: Begin Training with 128 x 128
Generate dataset
End of explanation
learn.set_data(get_data(sz))
learn.freeze()
learn.fit(lr, n_cycle=3, cycle_len=1, cycle_mult=2)
Explanation: Freeze for Transfer Learning
End of explanation
learn.unfreeze()
learn.fit(lrs, 3, cycle_len=1, cycle_mult=2)
learn.save(f'{sz}')
Explanation: F2 Score not improving
Unfreeze and train with differential learning rate
End of explanation
sz=256
learn.set_data(get_data(sz))
learn.freeze()
learn.fit(lr, 3, cycle_len=1, cycle_mult=2)
learn.unfreeze()
learn.fit(lrs, 3, cycle_len=1, cycle_mult=2)
learn.save(f'{sz}')
Explanation: TODO
End of explanation
learn.models_path
learn.load(f'{sz}')
multi_preds, y = learn.TTA()
multi_preds.shape, y.shape
multi_preds[:,0,:]
y[0,:]
preds = np.mean(multi_preds, 0)
preds[0,:]
Explanation: 
End of explanation
plt.plot(y[0,:])
plt.plot(preds[0,:])
f2(preds,y)
Explanation: Sample Predictions
End of explanation
<END_TASK>