Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<drop_column>
!pip install timm
dropFeatures = ['casual',"count","datetime","date","registered","season","weather","year","weekday","month","hour","temp","holiday"]
dataTrain = dataTrain.drop(dropFeatures,axis=1)
 dataTest = dataTest.drop(dropFeatures,axis=1 )<drop_column>
set_seed(314 )
<choose_model_class>
class CutMix(MixUp):
def __init__(self, alpha=1.) : self.distrib = Beta(tensor(alpha), tensor(alpha))
def before_batch(self):
lam = self.distrib.sample().squeeze().to(self.x.device)
 shuffle = torch.randperm(self.y.size(0)).to(self.x.device)
 self.yb1 = tuple(L(self.yb ).itemgot(shuffle))
nx_dims = len(self.x.size())
 bs, c, h, w = self.x.shape
rx, ry = w*self.distrib.sample() , h*self.distrib.sample()
rw, rh = w*(1-lam ).sqrt() , h*(1-lam ).sqrt()
x1 =(rx-rw/2 ).clamp(min=0 ).round().to(int)
 x2 =(rx+rw/2 ).clamp(max=w ).round().to(int)
 y1 =(ry-rh/2 ).clamp(min=0 ).round().to(int)
 y2 =(ry+rh/2 ).clamp(max=h ).round().to(int)
 self.learn.xb[0][:,:,y1:y2,x1:x2] = self.learn.xb[0][shuffle,:,y1:y2,x1:x2]
self.lam = 1- float(x2-x1)*(y2-y1)/(h*w)
 if not self.stack_y:
ny_dims = len(self.y.size())
 self.learn.yb = tuple(L(self.yb1,self.yb ).map_zip(torch.lerp,weight=unsqueeze(self.lam, n=ny_dims-1)) )
lrmodel = LinearRegression()
lrmodel.fit(dataTrain, yLabelsLog)
 r_sq = lrmodel.score(dataTrain, yLabelsLog)
 print('coefficient of determination(r_square)= ', r_sq)
 print('intercept = ', lrmodel.intercept_)
 print('slope = ', lrmodel.coef_)
<train_model>
df = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 df['image_id'] = df['image_id'].apply(lambda x: f'train_images/{x}')
 df.head()
idx2label = json.load(open('.. /input/cassava-leaf-disease-classification/label_num_to_disease_map.json'))
df.label = df.label.map(str ).map(idx2label)
 idxs = L.range(len(df)).shuffle()
df['valid'] = False
df.loc[idxs[:4279], 'valid'] = True
dataTrain_ols = sm.add_constant(dataTrain)
 olsmodel = sm.OLS(yLabelsLog, dataTrain_ols ).fit()
print(olsmodel.summary() )<compute_train_metric>
dls = ImageDataLoaders.from_df(df, path='.. /input/cassava-leaf-disease-classification/', bs=64,
item_tfms=Resize(320),
valid_col='valid',
batch_tfms=aug_transforms(size=300, mult=2, max_zoom=2.))
 dls.show_batch()
transformer = PolynomialFeatures(degree=2, include_bias=False)
 dataTrain_poly = transformer.fit_transform(dataTrain)
 prmodel = LinearRegression().fit(dataTrain_poly, yLabelsLog)
 r_sq = prmodel.score(dataTrain_poly, yLabelsLog)
 print('coefficient of determination(r_square)= ', r_sq)
 print('intercept = ', prmodel.intercept_)
 print('slope = ', prmodel.coef_)
<save_to_csv>
model = timm.create_model('tf_efficientnet_b3_ns', pretrained=False )
predsTest = lrmodel.predict(X= dataTest)
 submission = pd.DataFrame({
"datetime": datetimecol,
"count": [max(0, x)for x in np.exp(predsTest)]
})
 submission.to_csv('bike_predictions_LR.csv', index=False )<save_to_csv>
model.load_state_dict(torch.load('.. /input/timm-pretrained-efficientnet/efficientnet/tf_efficientnet_b3_ns-9d44bf68.pth'))
dataTest_poly = transformer.fit_transform(dataTest)
 predsTest_poly = prmodel.predict(X= dataTest_poly)
 submission = pd.DataFrame({
"datetime": datetimecol,
"count": [max(0, x)for x in np.exp(predsTest_poly)]
})
 submission.to_csv('bike_predictions_PR.csv', index=False )<load_from_csv>
model.classifier = nn.Linear(model.classifier.in_features, len(dls.vocab))
learn = Learner(dls, model, loss_func=LabelSmoothingCrossEntropy() , splitter=methodcaller('parameters'), metrics=accuracy, model_dir='/kaggle/working/models')
 learn.to_fp16()
learn.freeze()
train = pd.read_csv(".. /input/train.csv")
<load_from_csv>
learn.lr_find()
test = pd.read_csv(".. /input/test.csv" )<load_from_csv>
learn.fine_tune(16, base_lr=8.3e-4, cbs=[ShowGraphCallback() , CutMix() ] )
test = pd.read_csv(".. /input/test.csv", parse_dates = ["datetime"])
 train = pd.read_csv(".. /input/train.csv", parse_dates = ["datetime"] )<feature_engineering>
dl = dls.valid
a1, target = learn.tta(dl=dl, n=16)
 pred_1 = a1.argmax(dim=1)
(pred_1==target ).to(float ).mean()
train["year"] = train["datetime"].dt.year
train["hour"] = train["datetime"].dt.hour
train["dayofweek"] = train["datetime"].dt.dayofweek
test["year"] = test["datetime"].dt.year
test["hour"] = test["datetime"].dt.hour
test["dayofweek"] = test["datetime"].dt.dayofweek 
<prepare_x_and_y>
sample_df = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 sample_df.head()
sample_copy = sample_df.copy()
sample_copy['image_id'] = sample_copy['image_id'].apply(lambda x: f'test_images/{x}')
 test_dl = learn.dls.test_dl(sample_copy)
 test_dl.show_batch()
y_train = train["count"]
y_train = np.log1p(y_train )<drop_column>
a, _ = learn.tta(dl=test_dl, n=16)
 pred = a.argmax(dim=1 ).numpy()
sample_df['label'] = pred
<import_modules><EOS> 
sample_df.to_csv('submission.csv',index=False )