Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<choose_model_class>
package_paths = [
'.. /input/pytorch-image-models/pytorch-image-models-master'
]
for pth in package_paths:
sys.path.append(pth )
vgg_m.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.SGD(0.001), metrics=['accuracy'])
<train_model>
checkpoint = ModelCheckpoint('vgg_model.h5', save_best_only=True, verbose=1)
 history = vgg_m.fit_generator(train_generator, steps_per_epoch=len(train_generator),
epochs=6,
validation_data = val_generator,
validation_steps=len(val_generator),
callbacks=[checkpoint])
<load_pretrained>
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()
!ls
batch_index = 0
files_list = os.listdir(".. /input/state-farm-distracted-driver-detection/test")
 def load_test_images(batch_size=32, src='.. /input/state-farm-distracted-driver-detection/test/'):
global batch_index, files_list
imgs_list = files_list[batch_index: batch_index+batch_size]
batch_index += len(imgs_list)
 batch_imgs = []
for img_name in imgs_list:
img = Image.open(src+img_name)
 im = img.resize(( 224,224))
batch_imgs.append(np.array(im)/255.)
return np.array(batch_imgs)
<choose_model_class>

 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
def get_img(path):
im_bgr = cv2.imread(path)
 im_rgb = im_bgr[:, :, ::-1]
return im_rgb
ShiftScaleRotate, Normalize, Compose, CenterCrop, Resize, HorizontalFlip,
VerticalFlip, Transpose, RandomResizedCrop, HueSaturationValue, RandomBrightnessContrast,
CoarseDropout, Cutout 
)
 def get_infer_transforms() :
return Compose([
CenterCrop(config['img_size'], config['img_size'], p=1.0),
Resize(config['img_size'], config['img_size']),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )
preds_list = np.array([])
 batch_index=0
batch_size = 32
mm_raw = load_VGG16()
mm_model = mm_raw.output
mm_model = Dense(5000, activation='relu',kernel_regularizer=regularizers.l2(0.00001))(mm_model)
 mm_model = Dropout(0.1 )(mm_model)
 mm_model = Dense(500, activation='relu',kernel_regularizer=regularizers.l2(0.00001))(mm_model)
 mm_model = Dropout(0.1 )(mm_model)
 mm_model = Dense(10, activation='softmax' )(mm_model)
 mm = Model(inputs=vgg_model_raw.input, outputs= vgg_model)
 mm.load_weights('vgg_model.h5')
 while True:
tst_imgs = load_test_images(batch_size=batch_size)
 if(tst_imgs.shape[0] <= 0):
print("Batchsize is less : ",batch_index)
 break
preds = mm.predict(tst_imgs)
 print("\r {}, batch_size : {}, nth_batch/all_batch : {}/{}".format(preds_list.shape,batch_size, batch_index, len(files_list)) ,end="")
 sys.stdout.flush()
if len(preds_list)== 0:
preds_list = np.array(preds)
 else:
preds_list = np.append(preds_list, preds, axis=0)
<prepare_output>

 class LeafDataset(Dataset):
def __init__(self, df, img_dir, transforms=None, include_labels=True):
super().__init__()
self.df = df
self.img_dir = img_dir
self.transforms = transforms
self.include_labels = include_labels
if include_labels:
self.labels = self.df['label'].values
def __len__(self):
return len(self.df)
 def __getitem__(self, index: int):
img = get_img("{}/{}".format(self.img_dir, self.df.loc[index]['image_id']))
if self.transforms:
img = self.transforms(image=img)['image']
if self.include_labels:
label = self.labels[index]
return img, label
else:
return img;
titles = "img,c0,c1,c2,c3,c4,c5,c6,c7,c8,c9".split(",")
 names = pd.DataFrame(files_list[:len(preds_list)])
 names.columns=["img"]
df = pd.DataFrame(preds_list)
 df.columns=titles[1:]
df['img']=names['img']
df = df[titles]
df.tail()<save_to_csv>

 class LeafDiseaseClassifier(nn.Module):
def __init__(self, model_arch, num_classes, 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, num_classes)
 def forward(self, x):
x = self.model(x)
 return x
def freeze_batch_norm(self):
layers = [mod for mod in self.model.children() ]
for layer in layers:
if isinstance(layer, nn.BatchNorm2d):
for param in layer.parameters() :
param.requires_grad = False
elif isinstance(layer, nn.Sequential):
for seq_layers in layer.children() :
if isinstance(layer, nn.BatchNorm2d):
param.requires_grad = False
df.to_csv('sub_VGG16.csv',index=False )<set_options>

 if __name__ == '__main__':
seed_everything(config['seed'])
 test = pd.DataFrame()
test['image_id'] = list(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'))
test_ds = LeafDataset(test, '.. /input/cassava-leaf-disease-classification/test_images/', transforms=get_infer_transforms() , include_labels=False)
 test_loader = torch.utils.data.DataLoader(
 test_ds,
batch_size=config['test_bs'],
num_workers=config['num_workers'],
shuffle=False, 
)
 device = torch.device(config['device'])
 model = LeafDiseaseClassifier(config['model_arch'], config['num_classes'] ).to(device)
 model.load_state_dict(torch.load('.. /input/effnet-b4/tf_efficientnet_b4_ns_Fold4_Epoch5_Acc_0.8952.pth'))
model.eval()
preds = []
pbar = tqdm(enumerate(test_loader), total=len(test_loader))
for step,(test_batch)in pbar:
test_batch = test_batch.to(device ).float()
test_preds = model(test_batch)
 preds += [torch.softmax(test_preds, 1 ).detach().cpu().numpy() ]
preds = np.concatenate(preds, axis=0)
%matplotlib inline
warnings.filterwarnings("ignore")
 test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 frame_h = 5
frame_l = 5
len(test_videos)
 print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version())
 gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )<load_pretrained>
test['label'] = np.argmax(preds, axis=1)
 test.head()
test.to_csv('submission.csv', index=False )
<install_modules><EOS> 
del model
torch.cuda.empty_cache()