Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<set_options>
!pip install.. /input/vision-transformer/vision_transformer_pytorch-1.0.2-py2.py3-none-any.whl
img_size = 512
test_aug = albumentations.Compose([
albumentations.RandomResizedCrop(img_size, img_size),
albumentations.Transpose(p=0.5),
albumentations.HorizontalFlip(p=0.5),
albumentations.VerticalFlip(p=0.5),
albumentations.HueSaturationValue(
 hue_shift_limit=0.2,
sat_shift_limit=0.2,
val_shift_limit=0.2,
p=0.5 
),
albumentations.RandomBrightnessContrast(
 brightness_limit=(-0.1,0.1),
contrast_limit=(-0.1, 0.1),
p=0.5 
),
albumentations.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225],
max_pixel_value=255.0,
p=1.0 
)
 ], p=1.)<load_from_csv>
model = VisionTransformer.from_name('ViT-B_16', num_classes=5)
 model.load_state_dict(torch.load('.. /input/vitb16trained/ViT-B_16_trained.pt',map_location=torch.device('cpu')) )
dfx = pd.read_csv(".. /input/cassava-leaf-disease-classification/sample_submission.csv")
 image_path = ".. /input/cassava-leaf-disease-classification/test_images/"
test_image_paths = [os.path.join(image_path, x)for x in dfx.image_id.values]
test_targets = dfx.label.values
test_dataset = ImageDataset(
 image_paths=test_image_paths,
targets=test_targets,
augmentations=test_aug,
)<load_pretrained>
test_df = pd.read_csv(".. /input/cassava-leaf-disease-classification/sample_submission.csv")
 image_path = ".. /input/cassava-leaf-disease-classification/test_images/"
test_targets = test_df.label.values
test_aug = albu.Compose([
albu.CenterCrop(512, 512, p=1.) ,
albu.Resize(384, 384),
albu.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.)
test_dataset=CassavaDataset(
 df=test_df,
imfolder=image_path,
train=False,
transforms=test_aug 
)
 test_loader = DataLoader(
 test_dataset,
batch_size=4,
num_workers=4,
shuffle=False,
)
train_dfx = pd.read_csv(".. /input/cassava-leaf-disease-classification/train.csv")
 model_path = ".. /input/cassava-model-3"
model0 = EfficientnetModel(num_classes=train_dfx.label.nunique())
 model0.load(f"{model_path}/efficentnet_model_fold0.bin", device='cuda')
 model1 = EfficientnetModel(num_classes=train_dfx.label.nunique())
 model1.load(f"{model_path}/efficentnet_model_fold1.bin", device='cuda')
 model2 = EfficientnetModel(num_classes=train_dfx.label.nunique())
 model2.load(f"{model_path}/efficentnet_model_fold2.bin", device='cuda')
 model3 = EfficientnetModel(num_classes=train_dfx.label.nunique())
 model3.load(f"{model_path}/efficentnet_model_fold3.bin", device='cuda')
 model4 = EfficientnetModel(num_classes=train_dfx.label.nunique())
 model4.load(f"{model_path}/efficentnet_model_fold4.bin", device='cuda')
 model5= CustomResNext(num_classes=train_dfx.label.nunique())
 model5.load(f"{model_path}/resnet_model_fold0.bin", device='cuda')
 model6= CustomResNext(num_classes=train_dfx.label.nunique())
 model6.load(f"{model_path}/resnet_model_fold1.bin", device='cuda')
 model7= CustomResNext(num_classes=train_dfx.label.nunique())
 model7.load(f"{model_path}/resnet_model_fold2.bin", device='cuda')
 model8= CustomResNext(num_classes=train_dfx.label.nunique())
 model8.load(f"{model_path}/resnet_model_fold3.bin", device='cuda')
 model9= CustomResNext(num_classes=train_dfx.label.nunique())
 model9.load(f'{model_path}/resnet_model_fold4.bin', device='cuda')
<predict_on_test>
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 predictions=[]
for imgs in test_loader:
imgs = imgs.to(device)
 with torch.no_grad() :
model=model.to(device)
 outputs = model(imgs)
 _, predicted = torch.max(outputs, dim=1)
 predicted=predicted.to('cpu')
 predictions.append(predicted )
<feature_engineering><EOS> 
test_df['label'] = np.concatenate(predictions)
 test_df.to_csv('submission.csv', index=False )