Kaggle competition: Cassava Leaf Disease Classification
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )<define_variables>
class CFG:
img_size = 512
num_classes = 5
num_workers = 4
batch_size = 64
epochs = 1
OUTPUT_DIR = './'
ROOT_DIR = '.. /input/cassava-leaf-disease-classification/'
TRAIN_PATH = '.. /input/cassava-leaf-disease-classification/train_images'
TEST_PATH = '.. /input/cassava-leaf-disease-classification/test_images'
MODEL_DIR = '.. /input/cassava-models'
speed_test = False<predict_on_test>
def get_augmentation(data):
if data=='test':
return A.Compose([
A.Resize(CFG.img_size, CFG.img_size),
A.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225],
max_pixel_value=255.0,
p=1.0),
ToTensorV2()
])
 test_aug = A.Compose([
A.Resize(CFG.img_size, CFG.img_size),
A.Transpose(p=0.5),
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.HueSaturationValue(
 hue_shift_limit=0.2,
sat_shift_limit=0.2,
val_shift_limit=0.2,
p=0.5 
),
A.RandomBrightnessContrast(
 brightness_limit=(-0.1,0.1),
contrast_limit=(-0.1, 0.1),
p=0.5 
),
A.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.)
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )<predict_on_test>
class TestDataset(Dataset):
def __init__(self, df, transform=None):
self.df = df
self.file_names = df['image_id'].values
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
file_name = self.file_names[idx]
file_path = f'{TEST_PATH}/{file_name}'
image = Image.open(file_path ).convert('RGB')
 image = np.array(image)
 if self.transform:
augmented = self.transform(image=image)
 image = augmented['image']
return image
predictions = predict_on_video_set(test_videos, num_workers=4 )<save_to_csv>
class CustomResNext(nn.Module):
def __init__(self, pretrained=False):
super().__init__()
self.model = timm.create_model('resnext50_32x4d', pretrained=pretrained)
 n_features = self.model.fc.in_features
self.model.fc = nn.Linear(n_features, CFG.num_classes)
 def forward(self, x):
x = self.model(x)
 return x
class LeafModel(nn.Module):
def __init__(self):
super().__init__()
self.effnet = EfficientNet.from_name("efficientnet-b4")
 self.dropout = nn.Dropout(0.1)
 self.out = nn.Linear(1792, 5)
 def forward(self, image):
batch_size,_,_,_ = image.shape
x = self.effnet.extract_features(image)
 x = F.adaptive_avg_pool2d(x, 1 ).reshape(batch_size, -1)
 x = self.out(self.dropout(x))
return x
class Efficient_b4(nn.Module):
def __init__(self, PATH, pretrained=False):
super().__init__()
self.model = timm.create_model('tf_efficientnet_b4_ns', pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, CFG.num_classes)
 checkpoint = torch.load(PATH)
 self.model.load_state_dict(checkpoint['model_state_dict'])
 def forward(self, x):
x = self.model(x)
 return x
class Efficient_b7(nn.Module):
def __init__(self, PATH, pretrained=False):
super().__init__()
self.model = timm.create_model('tf_efficientnet_b7_ns', pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, CFG.num_classes)
 checkpoint = torch.load(PATH)
 self.model.load_state_dict(checkpoint['model_state_dict'])
 def forward(self, x):
x = self.model(x)
 return x
class VitModel(nn.Module):
def __init__(self,PATH):
super().__init__()
self.model = VisionTransformer.from_name('ViT-B_16', num_classes=5)
 self.model.load_state_dict(torch.load(PATH))
def forward(self, image):
batch_size,_,_,_ = image.shape
image = F.interpolate(image,(384,384))
x = self.model(image)
 return x
submission_df_resnext = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_resnext.to_csv("submission_resnext.csv", index=False )<install_modules>
def get_leaf_model(PATH):
model = LeafModel()
checkpoint = torch.load(PATH)
 model.load_state_dict(checkpoint['model_state_dict'])
 model.eval()
return model.to(device)
 def get_resnext_model(PATH):
model = CustomResNext()
model.load_state_dict(torch.load(PATH))
model.eval()
return model.to(device)
 def get_efficient_b4_model(PATH):
model = Efficient_b4(PATH)
 model.eval()
return model.to(device)
 def get_efficient_b7_model(PATH):
model = Efficient_b7(PATH)
 model.eval()
return model.to(device)
 def get_vit_model(PATH):
model = VitModel(PATH)
 model.eval()
return model.to(device )
!pip install.. /input/deepfake-xception-trained-model/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet<define_variables>
class EnsembledModel() :
def __init__(self, model_paths):
super().__init__()
self.num_models = len(model_paths)
 self.leafmodel1 = get_leaf_model(model_paths[0])
 self.leafmodel2 = get_leaf_model(model_paths[1])
 self.effb4_model1 = get_efficient_b4_model(model_paths[2])
 self.effb4_model2 = get_efficient_b4_model(model_paths[3])
 self.effb4_model3 = get_efficient_b4_model(model_paths[4])
 self.effb4_model4 = get_efficient_b4_model(model_paths[5])
 self.effb4_model5 = get_efficient_b4_model(model_paths[6])
 self.effb7_model1 = get_efficient_b7_model(model_paths[7])
 self.effb7_model2 = get_efficient_b7_model(model_paths[8])
 self.effb7_model3 = get_efficient_b7_model(model_paths[9])
 self.res50_model1 = get_resnext_model(model_paths[10])
 self.res50_model2 = get_resnext_model(model_paths[11])
 self.res50_model3 = get_resnext_model(model_paths[12])
 self.res50_model4 = get_resnext_model(model_paths[13])
 self.res50_model5 = get_resnext_model(model_paths[14])
 self.res50_lr_model1 = get_resnext_model(model_paths[15])
 self.res50_lr_model2 = get_resnext_model(model_paths[16])
 self.res50_lr_model3 = get_resnext_model(model_paths[17])
 def predict(self, x):
with torch.no_grad() :
l1 = self.leafmodel1(x)
 l2 = self.leafmodel2(x)
 b4_e1 = self.effb4_model1(x)
 b4_e2 = self.effb4_model2(x)
 b4_e3 = self.effb4_model3(x)
 b4_e4 = self.effb4_model4(x)
 b4_e5 = self.effb4_model5(x)
 r1 = self.res50_model1(x)
 r2 = self.res50_model2(x)
 r3 = self.res50_model3(x)
 r4 = self.res50_model4(x)
 r5 = self.res50_model5(x)
 b7_e1 = self.effb7_model1(x)
 b7_e2 = self.effb7_model2(x)
 b7_e3 = self.effb7_model3(x)
 lr_r1 = self.res50_lr_model1(x)
 lr_r2 = self.res50_lr_model2(x)
 lr_r3 = self.res50_lr_model3(x)
 pred =(l1+l2 + b4_e1+b4_e2+b4_e3+b4_e4+b4_e5 + b7_e1+b7_e2+b7_e3 + r1+r2+r3+r4+r5 + lr_r1+lr_r2+lr_r3)/(self.num_models)
 return pred
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 len(test_videos )<set_options>
model_paths = [
'.. /input/cassava-trained-models/with_torch_crossentropy_LeafDiseasesModel Eff-4_fold-1.pt',
'.. /input/cassava-trained-models/with_torch_crossentropy_LeafDiseasesModel Eff-4_fold-5.pt',
'.. /input/cassava-trained-models/LeafDiseasesModel Eff-4_fold-1.pt',
'.. /input/cassava-trained-models/LeafDiseasesModel Eff-4_fold-2.pt',
'.. /input/cassava-trained-models/LeafDiseasesModel Eff-4_fold-3.pt',
'.. /input/cassava-trained-models/LeafDiseasesModel Eff-4_fold-4.pt',
'.. /input/cassava-trained-models/LeafDiseasesModel Eff-4_fold-5.pt',
'.. /input/cassava-trained-models/LeafDiseasesModel Eff-7_fold-1.pt',
'.. /input/cassava-trained-models/LeafDiseasesModel Eff-7_fold-2.pt',
'.. /input/cassava-trained-models/LeafDiseasesModel Eff-7_fold-4.pt',
'.. /input/cassava-trained-models/Resnext50_fold-1.pth',
'.. /input/cassava-trained-models/Resnext50_fold-2.pth',
'.. /input/cassava-trained-models/Resnext50_fold-3.pth',
'.. /input/cassava-trained-models/Resnext50_fold-4.pth',
'.. /input/cassava-trained-models/Resnext50_fold-5.pth',
'.. /input/cassava-trained-models/Resnext50_with_lr_scheduler_fold-1.pth',
'.. /input/cassava-trained-models/Resnext50_with_lr_scheduler_fold-2.pth',
'.. /input/cassava-trained-models/Resnext50_with_lr_scheduler_fold-3.pth'
]
model = EnsembledModel(model_paths )
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )<load_pretrained>
def inference(model, data_loader):
epoch_preds = 0
for epoch in range(CFG.epochs):
preds = []
for images in tqdm(data_loader):
images = images.to(device)
 logits = model.predict(images)
 preds += [logits.softmax(1 ).detach().cpu().numpy() ]
all_img_preds = np.concatenate(preds, axis=0)
 epoch_preds += all_img_preds
epoch_preds = epoch_preds / CFG.epochs
return epoch_preds
facedet = BlazeFace().to(gpu)
 facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
 facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
 _ = facedet.train(False )<load_pretrained>
test = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 test_data = TestDataset(test, transform=get_augmentation(data='test'))
test_loader = DataLoader(test_data, batch_size=CFG.batch_size, num_workers=CFG.num_workers )
frames_per_video = 64
video_reader = VideoReader()
video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
 face_extractor = FaceExtractor(video_read_fn, facedet )<define_variables>
predictions = inference(model, test_loader )
<normalization><EOS> 
test['label'] = predictions.argmax(1)
 test[['image_id', 'label']].to_csv(OUTPUT_DIR + 'submission.csv', index=False )