Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<choose_model_class>
sys.path.append('.. /input/timm-pytorch-image-models/pytorch-image-models-master')
 warnings.filterwarnings("ignore" )
model1 = load_model(save_model_name, custom_objects={'my_iou_metric':my_iou_metric})
 input_x = model1.layers[0].input
output_layer = model1.layers[-1].input
model2 = Model(input_x, output_layer)
 model2.compile(loss=symmetric_lovasz, optimizer=Adam(lr=0.01), metrics=[my_iou_metric_2] )<train_on_grid>
DATA_PATH = '.. /input/cassava-leaf-disease-classification/'
TRAIN_DIR = DATA_PATH + 'train_images/'
TEST_DIR = DATA_PATH + 'test_images/'
MODEL_PATH = '.. /input/cassavanet-baseline-models/'
N_TTA = 8
HEIGHT = 512
WIDTH = 512
CHANNELS = 3
N_CLASSES = 5
MODEL_LIST = [0,1,2,3,4,5]
IMG_MEAN = [0.485, 0.456, 0.406]
IMG_STD = [0.229, 0.224, 0.225]
early_stopping = EarlyStopping(monitor='val_my_iou_metric_2', mode = 'max',patience=30, verbose=1)
 model_checkpoint = ModelCheckpoint(save_model_name,monitor='val_my_iou_metric_2', mode = 'max', save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='val_my_iou_metric_2', mode = 'max',factor=0.5, patience=10, min_lr=0.00005, verbose=1)
 train_generator = make_image_gen(x_train, y_train,batch_size//2)
 t_model2_start = time.time()
history = model2.fit_generator(
 train_generator,
steps_per_epoch = x_train.shape[0]*2//batch_size,
validation_data=[x_valid, y_valid],
epochs=epochs + int(epochs*(1/5)) ,
callbacks = [early_stopping, model_checkpoint, reduce_lr],
verbose = 1 
)
 t_model2_end = time.time()
print(f"Run time = {(t_model2_end - t_model2_start)/3600} hours" )<load_pretrained>
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
SEED = 1234
seed_everything(SEED)
 DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
model = load_model(save_model_name,custom_objects={'my_iou_metric_2': my_iou_metric_2, 'symmetric_lovasz': symmetric_lovasz} )<predict_on_test>
class CassavaNet(nn.Module):
def __init__(self, model_name=None, pretrained=False):
super().__init__()
self.model_name = model_name
if model_name == 'deit_base_patch16_224' or model_name == 'deit_base_patch16_384':
self.model = torch.hub.load('facebookresearch/deit:main', model_name, pretrained=pretrained)
 else:
self.model = timm.create_model(model_name, pretrained=pretrained)
 if 'efficientnet' in model_name:
self.n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(self.n_features, N_CLASSES)
 elif model_name == 'vit_large_patch16_384' or model_name == 'deit_base_patch16_224' or model_name == 'deit_base_patch16_384':
self.n_features = self.model.head.in_features
self.model.head = nn.Linear(self.n_features, N_CLASSES)
 elif 'resnext' in model_name:
self.n_features = self.model.fc.in_features
self.model.fc = nn.Linear(self.n_features, N_CLASSES)
 def forward(self, x):
return self.model(x)
 def freeze(self):
for param in self.model.parameters() :
param.requires_grad = False
if 'efficientnet' in self.model_name:
for param in self.model.classifier.parameters() :
param.requires_grad = True
elif self.model_name == 'vit_large_patch16_384' or 'deit_base_patch16_224':
for param in self.model.head.parameters() :
param.requires_grad = True
elif 'resnext' in self.model_name:
for param in self.model.fc.parameters() :
param.requires_grad = True
def unfreeze(self):
for param in self.model.parameters() :
param.requires_grad = True
def predict_result(model,x_test,img_size):
x_test_reflect = np.array([np.fliplr(x)for x in x_test])
 preds_test = model.predict(x_test ).reshape(-1, img_size, img_size)
 preds_test2_refect = model.predict(x_test_reflect ).reshape(-1, img_size, img_size)
 preds_test += np.array([ np.fliplr(x)for x in preds_test2_refect])
 return preds_test/2
preds_valid = predict_result(model,x_valid,img_size )<compute_train_metric>
class GetData(Dataset):
def __init__(self, Dir, FNames, labels,Type):
self.dir = Dir
self.fnames = FNames
self.lbs = labels
self.type = Type
def __len__(self):
return len(self.fnames)
 def __getitem__(self, index):
x = imread(os.path.join(self.dir, self.fnames[index]))
if "train" in self.type:
aug_data = train_transforms(image = x)
 return aug_data['image'], self.lbs[index]
elif "valid" in self.type:
aug_data = valid_transforms(image = x)
 return aug_data['image'], self.lbs[index]
elif "tr-tst" in self.type:
return x, self.lbs[index]
elif "test" in self.type:
return x, self.fnames[index]
thresholds_ori = np.linspace(0.3, 0.7, 31)
 thresholds = np.log(thresholds_ori/(1-thresholds_ori))
ious = np.array([iou_metric_batch(y_valid, preds_valid > threshold)for threshold in tqdm_notebook(thresholds)])
 print(ious);<categorify>
Aug_Norm = A.Normalize(mean=IMG_MEAN, std=IMG_STD, max_pixel_value=255.0, p=1.0)
 test_aug = Compose([
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.ShiftScaleRotate(p = 1.0),
A.ColorJitter(brightness=0.1, contrast=0.2, saturation=0.2, hue=0.00, always_apply=False, p=1.0),
A.RandomCrop(height= HEIGHT, width = WIDTH,always_apply=True, p=1.0),
Aug_Norm,
ToTensorV2(p=1.0)
 ], p=1.)
def rle_encode(im):

 pixels = im.flatten(order='F')
 pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs )<predict_on_test>
models = []
count = 0
for model_fpath in os.listdir(MODEL_PATH):
if count in MODEL_LIST:
print("Model Loaded:",model_fpath)
 model_name_split = model_fpath.split('_f')[0]
model = CassavaNet(model_name_split,pretrained = False)
 info = torch.load(MODEL_PATH + model_fpath,map_location = torch.device(DEVICE))
model.load_state_dict(info)
 models.append(model)
 count+=1
test_images = os.listdir(TEST_IMAGE_DIR)
 x_test = np.array([(np.array(load_img(TEST_IMAGE_DIR + "{}".format(idx), color_mode = "grayscale")))/ 255
for idx in tqdm_notebook(test_images)] ).reshape(-1, img_size, img_size, 1)
 preds_test = predict_result(model,x_test,img_size)
 pred_dict = {idx[:10]: rle_encode(np.round(preds_test[i] > threshold_best))
for i, idx in enumerate(tqdm_notebook(test_images)) };<save_to_csv>
submission = pd.DataFrame()
list_files = os.listdir(TEST_DIR)
 submission['image_id'] = pd.Series(list_files)
 submission['label'] = 0
submission.head()
sub = pd.DataFrame.from_dict(pred_dict,orient='index')
 sub.index.names = ['id']
sub.columns = ['rle_mask']
sub.to_csv(submission_file )<train_model>
start_time = time.time()
BATCH_SIZE = 1
test_set = GetData(TEST_DIR,submission['image_id'], submission['label'], Type = 'test')
 test_loader = DataLoader(test_set, batch_size=BATCH_SIZE, shuffle=False, num_workers=8,pin_memory = True)
 with torch.no_grad() :
for i,(images,labels)in enumerate(test_loader):
voting = np.zeros(( len(models),N_TTA,N_CLASSES))
aug_images = np.zeros(( N_TTA,CHANNELS,HEIGHT,WIDTH))
for aug_no in range(N_TTA):
img_np = images.numpy()
aug_data = test_aug(image = np.reshape(img_np,(600,800,CHANNELS)))
 aug_images[aug_no,:,:,:] = aug_data['image'].numpy()
aug_images = torch.from_numpy(aug_images ).to(torch.float32 ).to(DEVICE)
 for model_no in range(len(models)) :
model = models[model_no]
model = model.to(DEVICE)
 model.eval()
logits = model(aug_images)
 voting[model_no,:,:] = F.softmax(logits ).cpu().numpy()
voting = np.sum(voting,axis = 1)/ N_TTA
voting = np.sum(voting,axis = 0)/ len(models)
 label = np.argmax(voting)
 submission['label'].loc[submission['image_id'] == labels[0]] = label
print(time.time() -start_time )
<set_options><EOS> 
submission.to_csv('submission.csv',index=False)
 submission.head()