Kaggle competition: Cassava Leaf Disease Classification
md.is_multi, md.is_reg<init_hyperparams>
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
print('Number of layer groups:', len(learn.get_layer_groups()), '\t(first 2 groups is pretrained backbone)')
 print('This is our extra thin on top of the backbone Resnet50 architecture:')
 learn.get_layer_groups() [2]<train_model>
CFG = {
'model_arch': 'tf_efficientnet_b3_ns',
'img_size': 512,
'valid_bs': 16,
'device': 'cuda' if torch.cuda.is_available() else 'cpu'
}
base_lr = 1e-4
fc_lr = 1e-3
lrs = [base_lr, base_lr, fc_lr]
learn.fit(lrs=lrs, n_cycle=2, cycle_len=None)
 learn.unfreeze()
learn.fit(lrs, n_cycle=9, cycle_len=None)
 learn.save(MODEL_PATH )<define_variables>
df = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/sample_submission.csv')
 df_torch = df.copy()
df_tf = df.copy()
best_th = 0.4 
<prepare_output>
PATH = '/kaggle/input/cassava-leaf-disease-classification/test_images/'
preds_t,y_t = learn.TTA(is_test=True,n_aug=8)
 preds_t = np.stack(preds_t, axis=-1)
 preds_t = np.exp(preds_t)
 preds_t = preds_t.mean(axis=-1)
 preds_t = np.concatenate([np.zeros(( preds_t.shape[0],1)) +best_th, preds_t],axis=1)
 np.save("rx50_480_preds.npy",preds_t )<save_to_csv>
class CassavaImageClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x
sample_df = pd.read_csv(SAMPLE_SUB)
 sample_list = list(sample_df.Image)
 labels_list = ["new_whale"]+labels_list
pred_list = [[labels_list[i] for i in p.argsort() [-5:][::-1]] for p in preds_t]
pred_dic = dict(( key, value)for(key, value)in zip(learn.data.test_ds.fnames,pred_list))
pred_list_cor = [' '.join(pred_dic[id])for id in sample_list]
df = pd.DataFrame({'Image':sample_list,'Id': pred_list_cor})
 df.to_csv('submission_{}.csv'.format(MODEL_NAME), header=True, index=False)
 df.head()<install_modules>
class DiseaseDatasetInference(torch.utils.data.Dataset):
def __init__(self, df, transform=None, opt_label=True):
self.df = df.reset_index(drop=True ).copy()
self.transform = transform
self.opt_label = opt_label
if self.opt_label:
self.data = [(row['image_id'], row['label'])for _, row in self.df.iterrows() ]
else:
self.data = [(row['image_id'])for _, row in self.df.iterrows() ]
self.data = np.asarray(self.data)
 def __len__(self):
return len(self.data)
 def __getitem__(self, index):
if self.opt_label:
image_path, label = self.data[index]
else:
image_path = self.data[index]
image = cv2.imread(image_path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform is not None:
image = self.transform(image=image)['image']
if self.opt_label == True:
return(image, int(label))
else:
return image
!pip install fastai==0.7.0 --no-deps
!pip install torch==0.4.1 torchvision==0.2.1<define_variables>
trained_model = torch.load('/kaggle/input/cassava-effecientnet-b3/model_6.pt', map_location=torch.device(CFG['device']))
sub_files = [
'.. /input/results/sub_725.csv',
'.. /input/results/sub_728.csv',
'.. /input/results-/sub_719.csv',
]
sub_weight = [
0.719**2,
0.728**2,
0.725**2]<save_to_csv>
test_csv = df.copy()
test_csv['image_id'] = PATH + test_csv['image_id']
test_ds = DiseaseDatasetInference(test_csv, transform=get_inference_transforms() , opt_label=False)
 test_loader = torch.utils.data.DataLoader(test_ds, batch_size=CFG['valid_bs'], shuffle=False, pin_memory=False )
Hlabel = 'Image'
Htarget = 'Id'
npt = 5
place_weights = {}
for i in range(npt):
place_weights[i] =(1 /(i + 1))
print(place_weights)
 lg = len(sub_files)
 sub = [None]*lg
for i, file in enumerate(sub_files):
print("Reading {}: w={} - {}".format(i, sub_weight[i], file))
reader = csv.DictReader(open(file,"r"))
sub[i] = sorted(reader, key=lambda d: str(d[Hlabel]))
out = open("new_sub.csv", "w", newline='')
 writer = csv.writer(out)
 writer.writerow([Hlabel,Htarget])
 for p, row in enumerate(sub[0]):
target_weight = {}
for s in range(lg):
row1 = sub[s][p]
for ind, trgt in enumerate(row1[Htarget].split(' ')) :
target_weight[trgt] = target_weight.get(trgt,0)+(place_weights[ind]*sub_weight[s])
 tops_trgt = sorted(target_weight, key=target_weight.get, reverse=True)[:npt]
writer.writerow([row1[Hlabel], " ".join(tops_trgt)])
 out.close()<define_variables>
torch_outcomes = pd.concat([df_torch['image_id'], pd.DataFrame(preds_torch)], axis=1 ).sort_values(['image_id'] )
MODEL_NAME = 'Resnext50'
TRAIN = '.. /input/humpback-whale-identification/train/'
TEST = '.. /input/humpback-whale-identification/test/'
LABELS = '.. /input/humpback-whale-identification/train.csv'
SAMPLE_SUB = '.. /input/humpback-whale-identification/sample_submission.csv'
arch = resnext50
num_workers = 4<feature_engineering>
import tensorflow as tf
import pandas as pd
import numpy as np
import os
from PIL import Image
df = pd.read_csv(LABELS ).set_index('Image')
 new_whale_df = df[df.Id == "new_whale"]
train_df = df[~(df.Id == "new_whale")]
unique_labels = np.unique(train_df.Id.values)
 labels_dict = dict()
labels_list = []
for i in range(len(unique_labels)) :
labels_dict[unique_labels[i]] = i
labels_list.append(unique_labels[i])
 print("Number of classes: {}".format(len(unique_labels)))
 train_df.Id = train_df.Id.apply(lambda x: labels_dict[x])
 train_labels = np.asarray(train_df.Id.values)
 test_names = [f for f in os.listdir(TEST)]<feature_engineering>
model = tf.keras.models.load_model('/kaggle/input/plantdiseaseresnet50/resnet50.h5' )
train_df['image_name'] = train_df.index
rs = np.random.RandomState(42)
 perm = rs.permutation(len(train_df))
tr_n = train_df['image_name'].values
val_n = train_df['image_name'].values[perm][:1000]
print('Train/val:', len(tr_n), len(val_n))
print('Train classes', len(train_df.loc[tr_n].Id.unique()))
print('Val classes', len(train_df.loc[val_n].Id.unique()))<load_pretrained>
tf_outcomes = pd.concat([pd.DataFrame(test_images, columns=['image_id']), pd.DataFrame(preds_tf)], axis=1 ).sort_values(['image_id'] )
PICKL = '.. /input/cropimg/bounding-box.pickle'
with open(PICKL, 'rb')as f:
crop_boxs = pickle.load(f )<data_type_conversions>
final_preds =(torch_outcomes.drop('image_id', axis=1)*0.7 + tf_outcomes.drop('image_id', axis=1)*0.3 ).to_numpy().argmax(1 )
<categorify><EOS> 
submit = pd.DataFrame({'image_id': torch_outcomes['image_id'].values, 'label': final_preds})
 submit.to_csv('submission.csv', index=False )