Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<predict_on_test>
import numpy as np
import os
import pandas as pd
from fastai.vision.all import *
totIoU = 0.0
m = val_X.shape[0]
for i in tqdm(range(m)) :
img = val_X[i:i+1, :, :]
origMask = val_Y[i:i+1, :, :]
predMask = model1.predict(img)
 origMask = cv2.inRange(origMask[0, :, :, 0], 0.55, 255)
 predMask = cv2.inRange(predMask[0, :, :, 0], 0.55, 255)
 totIoU += meanHit(origMask, predMask)
 print("mean IoU = ", totIoU/m )<feature_engineering>
set_seed(42 )
X_test_image = X_test_image/255.0<set_options>
train_df = pd.read_csv(dataset_path/'train.csv' )
gc.collect()<categorify>
train_df['path'] = train_df['image_id'].map(lambda x:dataset_path/'train_images'/x)
 train_df = train_df.drop(columns=['image_id'])
 train_df = train_df.sample(frac=1 ).reset_index(drop=True)
 train_df.head(10 )
origSize =(101, 101)
 submit_names = []
submit_rleMasks = []
for i in tqdm(range(len(test_image_list))):
img = X_test_image[i:i+1,:,:]
predMask = model1.predict(img)
 predMask = cv2.inRange(predMask[0, :, :, 0], 0.55, 255)
 predMask = cv2.resize(predMask, origSize)
 submit_names.append(test_image_list[i][:-4])
 submit_rleMasks.append(rle_encoding(np.transpose(predMask)) )<prepare_output>
dls = ImageDataLoaders.from_df(train_df,
splitter=RandomSplitter(0.2, seed=42),
label_col=0,
fn_col=1,
bs=bs,
item_tfms=item_tfms,
batch_tfms=batch_tfms )
sub = pd.DataFrame({'id':submit_names, 'rle_mask': submit_rleMasks})
 print(sub.shape)
 sub.head()<feature_engineering>
learn = cnn_learner(dls, resnet50, metrics=[error_rate, accuracy] ).to_native_fp16()
for i in tqdm(range(len(test_image_list))):
if(len(sub.iloc[i, 1])== 0):
sub.iloc[i, 1] = np.nan<save_to_csv>
learn.freeze()
learn.fine_tune(1, cbs=[MixUp(0.5)] )
sub.to_csv("tgsModel1.csv", index = False )<install_modules>
learn.save('estagio-1' )
!pip3 install pycocotools<set_options>
learn = learn.load('estagio-1' )
%matplotlib inline
%reload_ext autoreload
%autoreload 2
print(torch.__version__)
 torch.cuda.is_available()
torch.backends.cudnn.benchmark=True<load_from_csv>
learn = learn.to_native_fp32()
MASKS_FN = 'train.csv'
TRAIN_DN = Path('train/images/')
 MASKS_DN = Path('train/masks/')
 TEST = Path('test/images/')
 PATH = Path('/kaggle/input/tgs-salt-identification-challenge/')
 PATH128 = Path('/tmp/128/')
 TMP = Path('/tmp/')
 MODEL = Path('/tmp/model/')
 PRETRAINED = Path('/kaggle/input/is-there-salt-resnet34/model/resnet34_issalt.h5')
 seg = pd.read_csv(PATH/MASKS_FN ).set_index('id')
 seg.head()
sz = 128
bs = 64
nw = 4<define_variables>
learn.save('estagio-2' )
train_names_png = [TRAIN_DN/f for f in os.listdir(PATH/TRAIN_DN)]
train_names = list(seg.index.values)
 masks_names_png = [MASKS_DN/f for f in os.listdir(PATH/MASKS_DN)]
test_names_png = [TEST/f for f in os.listdir(PATH/TEST)]<categorify>
sample_df = pd.read_csv(dataset_path/'sample_submission.csv')
 sample_df.head()
with ThreadPoolExecutor(4)as e: e.map(resize_mask, train_names_png )<categorify>
_sample_df = sample_df.copy()
_sample_df['path'] = _sample_df['image_id'].map(lambda x:dataset_path/'test_images'/x)
 _sample_df = _sample_df.drop(columns=['image_id'])
 test_dl = dls.test_dl(_sample_df )
with ThreadPoolExecutor(4)as e: e.map(resize_mask, masks_names_png )<categorify>
test_dl.show_batch()
with ThreadPoolExecutor(4)as e: e.map(resize_mask, test_names_png )<define_variables>
preds, _ = learn.tta(dl=test_dl, n=8, beta=0 )
PATH = PATH128<import_modules>
sample_df['label'] = preds.argmax(dim=-1 ).numpy()
<compute_test_metric><EOS> 
sample_df.to_csv('submission.csv',index=False )