Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<set_options>
OUTPUT_DIR = "./"
MODEL_DIR = ".. /input/cassava-model/"
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
 TRAIN_PATH = ".. /input/cassava-leaf-disease-classification/train_images"
TEST_PATH = ".. /input/cassava-leaf-disease-classification/test_images"
%matplotlib inline 
<create_dataframe>
class CFG:
debug = False
num_workers = 4
models = [
"tf_efficientnet_b4_ns",
"vit_base_patch16_384",
"seresnext50_32x4d",
]
size = {
"tf_efficientnet_b3_ns": 512,
"tf_efficientnet_b4_ns": 512,
"vit_base_patch16_384": 384,
"deit_base_patch16_384": 384,
"seresnext50_32x4d": 512,
}
batch_size = 64
seed = 7097
target_size = 5
target_col = "label"
n_fold = 5
trn_fold = {
"tf_efficientnet_b3_ns": {
"best": [0, 1, 2, 3, 4],
"final": [],
},
"tf_efficientnet_b4_ns": {
"best": [0, 1, 2, 3, 4],
"final": [],
},
"vit_base_patch16_384": {"best": [0, 1, 2, 3, 4], "final": []},
"deit_base_patch16_384": {"best": [0, 1, 2, 3, 4], "final": []},
"seresnext50_32x4d": {"best": [5, 6, 7, 8, 9], "final": []},
}
data_parallel = {
"tf_efficientnet_b3_ns": False,
"tf_efficientnet_b4_ns": True,
"vit_base_patch16_384": False,
"deit_base_patch16_384": False,
"seresnext50_32x4d": False,
}
transform = {
"tf_efficientnet_b4_ns": "rotate",
"vit_base_patch16_384": "rotate",
"seresnext50_32x4d": "rotate",
}
weight = {
"tf_efficientnet_b4_ns": 1,
"vit_base_patch16_384": 1,
"seresnext50_32x4d": 1,
}
tta = 10
no_tta_weight = tta - 1
train = False
inference = True
frames_per_vid = [17, 25, 30, 32, 35, 36, 40]
public_LB = [0.46788, 0.46776, 0.46611, 0.46542, 0.46643, 0.46484, 0.46635]
df_viz = pd.DataFrame({'frames_per_vid': frames_per_vid, 'public_LB':public_LB} )<define_variables>
tta_weight_sum = CFG.no_tta_weight +(CFG.tta - 1)
 weight_sum = sum([CFG.weight[model] for model in CFG.models])* tta_weight_sum
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 )<import_modules>
test = pd.read_csv(".. /input/cassava-leaf-disease-classification/sample_submission.csv")
 test.head()
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )<set_options>
class CassvaImgClassifier(nn.Module):
def __init__(self, model_name="resnext50_32x4d", pretrained=False):
super().__init__()
if model_name == "deit_base_patch16_384":
self.model = torch.hub.load(".. /input/fair-deit", model_name, pretrained=pretrained, source="local")
 n_features = self.model.head.in_features
self.model.head = nn.Linear(n_features, CFG.target_size)
 else:
self.model = timm.create_model(model_name, pretrained=pretrained)
 if "resnext50_32x4d" in model_name:
n_features = self.model.fc.in_features
self.model.fc = nn.Linear(n_features, CFG.target_size)
 elif model_name.startswith("tf_efficientnet"):
n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, CFG.target_size)
 elif model_name.startswith("vit_"):
n_features = self.model.head.in_features
self.model.head = nn.Linear(n_features, CFG.target_size)
 def forward(self, x):
x = self.model(x)
 return x
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu<load_pretrained>
def inference(model, states, test_loader, device, data_parallel):
model.to(device)
 if device == torch.device("cuda")and data_parallel:
model = torch.nn.DataParallel(model)
 tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for state in states:
model.load_state_dict(state["model"])
 model.eval()
with torch.no_grad() :
y_preds = model(images)
 avg_preds.append(y_preds.softmax(1 ).to("cpu" ).numpy())
 avg_preds = np.mean(avg_preds, axis=0)
 probs.append(avg_preds)
 probs = np.concatenate(probs)
 return probs
<load_pretrained><EOS> 
predictions = None
for model_name in CFG.models:
for i in range(CFG.tta):
model = CassvaImgClassifier(model_name, pretrained=False)
 states = []
for saved_model in ["best", "final"]:
if CFG.trn_fold[model_name][saved_model] != []:
LOGGER.info(
 f"========== Model: {model_name}, TTA: {i}, Saved: {saved_model}, Fold: {CFG.trn_fold[model_name][saved_model]} ==========" 
)
 states += [
torch.load(MODEL_DIR + f"{model_name}_fold{fold}_{saved_model}.pth")
 for fold in CFG.trn_fold[model_name][saved_model]
]
if i == 0:
test_dataset = TestDataset(test, transform=get_transforms(data="valid", size=CFG.size[model_name]))
tta_weight = CFG.no_tta_weight
else:
test_dataset = TestDataset(
 test, transform=get_transforms(data=CFG.transform[model_name], size=CFG.size[model_name])
)
 tta_weight = 1
test_loader = DataLoader(
 test_dataset, batch_size=CFG.batch_size, shuffle=False, num_workers=CFG.num_workers, pin_memory=True 
)
 inf = inference(model, states, test_loader, device, CFG.data_parallel[model_name])
 LOGGER.info(f"Inference example: {inf[0]}")
 if predictions is None:
predictions = inf[np.newaxis] * CFG.weight[model_name] * tta_weight
else:
predictions = np.append(predictions, inf[np.newaxis] * CFG.weight[model_name] * tta_weight, axis=0)
 sub = np.sum(predictions, axis=0)/ weight_sum
LOGGER.info(f"========== Overall ==========")
 LOGGER.info(f"Submission example: {sub[0]}")
 test["label"] = sub.argmax(1)
 test[["image_id", "label"]].to_csv(OUTPUT_DIR + "submission.csv", index=False)
 test.head()