Kaggle competition: Cassava Leaf Disease Classification
for i in weiji:
new_data['is_'+str(i)]=new_data['release_year'].apply(lambda x:1 if x==i else 0 )<feature_engineering>
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
new_data['homepage_fact']=new_data['homepage'].apply(lambda x: 0 if x is np.nan else 1 )<feature_engineering>
CFG = {
'vit_img_size': 384,
'tta': 3,
'valid_bs': 16,
'device': 'cuda' if torch.cuda.is_available() else 'cpu',
'vit_models': ['model_5.pt', 'model_6.pt', 'model_7.pt', 'model_8.pt']
}
new_data['homepage_end']=new_data[new_data['homepage'].notna() ]['homepage'].str.findall(r'\.( [a-z]+ )(?:\/|$)' ).apply(lambda x:x[0])
 new_data['homepage_end'].head()<data_type_conversions>
class DiseaseDatasetInference(torch.utils.data.Dataset):
def __init__(self, df, transform=None, opt_label=True):
self.df = df.reset_index(drop=True ).copy()
self.transform = transform
self.opt_label = opt_label
if self.opt_label:
self.data = [(row['image_id'], row['label'])for _, row in self.df.iterrows() ]
else:
self.data = [(row['image_id'])for _, row in self.df.iterrows() ]
self.data = np.asarray(self.data)
 def __len__(self):
return len(self.data)
 def __getitem__(self, index):
if self.opt_label:
image_path, label = self.data[index]
else:
image_path = self.data[index]
image = cv2.imread(image_path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform is not None:
image = self.transform(image=image)['image']
if self.opt_label == True:
return(image, int(label))
else:
return image
new_data['homepage_end']=new_data['homepage_end'].fillna('unknow' )<categorify>
def get_inference_transforms(img_size = 512):
return Compose([
CenterCrop(img_size, img_size, p=0.5),
Resize(img_size, img_size),
Transpose(p=0.5),
RandomRotate90(p=0.25),
ShiftScaleRotate(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
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.)
page=pd.get_dummies(new_data['homepage_end'])
 page.head()<drop_column>
df = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/sample_submission.csv')
 PATH = '/kaggle/input/cassava-leaf-disease-classification/test_images/'
new_data=new_data.drop('homepage',axis=1 )<drop_column>
test_csv = df.copy()
test_csv['image_id'] = PATH + test_csv['image_id']
new_data=new_data.drop('poster_path',axis=1 )<feature_engineering>
def inference(model, data_loader, device):
preds = []
model.eval()
test_tqdm = tq.tqdm(data_loader, total=len(data_loader), desc="Testing", position=0, leave=True)
 for images in test_tqdm:
images = images.to(device)
 preds.extend(model(images ).detach().cpu().numpy())
 return preds
new_data['len_overview']=new_data['overview'].fillna('NAN' ).apply(lambda x:len(x))
new_data['len_overview'][new_data['len_overview']==0]=1
new_data['len_overview_budget']=new_data['budget']/(new_data['len_overview']+1 )<sort_values>
class CassavaImageClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.head.in_features
self.model.head = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x
len_rew_sort=new_data['len_overview'].sort_values(ascending=True)
 len_rew_sort.head()<define_variables>
vit_test_ds = DiseaseDatasetInference(test_csv, transform=get_inference_transforms(img_size=CFG['vit_img_size']), opt_label=False)
 vit_test_loader = torch.utils.data.DataLoader(vit_test_ds, batch_size=CFG['valid_bs'], shuffle=False, pin_memory=False )
length=len(new_data['len_overview'])
 m=0.1
n=0.1
arr_len_ove=[]
for i in range(1,11):
arr_len_ove.append(round(length*m))
m+=n
arr_len_ove<filter>
vit_preds = []
for vit_model_name in CFG['vit_models']:
print("Model: ", vit_model_name)
 vit_model = torch.load('/kaggle/input/vit-cassava/'+vit_model_name, map_location=torch.device(CFG['device']))
with torch.no_grad() :
for i in range(CFG['tta']):
vit_preds += [inference(vit_model, vit_test_loader, CFG['device'])]
vit_preds = np.mean(vit_preds, axis=0 )
for i in range(10):
qu=qu_arr[i]
if i==0:
new_data['len_overview'].loc[(new_data['len_overview']<len_rew_sort.iloc[arr_len_ove[i]-1])]=qu
else:
new_data['len_overview'].loc[(new_data['len_overview']<len_rew_sort.iloc[arr_len_ove[i]-1])&(new_data['len_overview'] >qu_arr[i-1])]=qu
print(i,qu )<groupby>
vit_outcomes = pd.concat([df['image_id'], pd.DataFrame(vit_preds)], axis=1 ).sort_values(['image_id'] )
len_ove_agg=new_data.groupby('len_overview' ).revenue.aggregate(['min','max','std'] )<feature_engineering>
final_preds = vit_outcomes.drop('image_id', axis=1 ).to_numpy().argmax(1 )
new_data['geres_name']=new_data['genres'].str.findall(r''name'\s?:\s?'(\w+)'')
 new_data['geres_name'].head()<drop_column>
submit = pd.DataFrame({'image_id': df['image_id'].values, 'label': final_preds})
 submit.to_csv('submission.csv', index=False )