Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<categorify>
import numpy as np
import pandas as pd
import os
base_transform = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 DATA.transform = base_transform<categorify>
import matplotlib.pyplot as plt
predictions, targets = np.zeros(len(DATA)) , np.zeros(len(DATA))
batch_slice =(0, 0)
 softmax = nn.Softmax(dim=1)
 for no in range(NUM_FOLDS):
_, data_loader_eval = get_dataloader_for_fold(no,
DATA, data_train, data_eval, 64)
 model = get_trained_model(no)
 for bi, d in enumerate(data_loader_eval):
inputs = d["image"].to(DEVICE, dtype=torch.float)
 batch_slice =(batch_slice[1], batch_slice[1]+inputs.size(0))
with torch.no_grad() :
outputs = model(inputs)
 outputs = softmax(outputs)
 predictions[batch_slice[0]:batch_slice[1]] = \
outputs.cpu().detach().squeeze(0 ).numpy().argmax(axis=1)
 targets[batch_slice[0]:batch_slice[1]] = d["label"]
print("Cohen Kappa quadratic score",
cohen_kappa_score(targets, predictions, weights="quadratic"))<choose_model_class>
df = pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/train.csv')
predictions, targets = np.zeros(0), np.zeros(0)
 for no in range(NUM_FOLDS):
_, data_loader_eval = get_dataloader_for_fold(no,
DATA, data_train, data_eval, batch_size)
 features_eval, targets_eval = get_train_features(data_loader_eval,
extractor)
 print("")
 xgb_model = xgb.XGBClassifier()
model_path = os.path.join("xgb_model_"+str(no))
xgb_model = pickle.load(open(model_path, "rb"))
prediction = xgb_model.predict(features_eval)
 predictions = np.concatenate([predictions, prediction], axis=0)
 targets = np.concatenate([targets, targets_eval], axis=0)
 print("Cohen Kappa quadratic score",
cohen_kappa_score(targets, predictions, weights="quadratic"))<create_dataframe>
test_df=pd.read_csv('/kaggle/input/plant-pathology-2020-fgvc7/test.csv')
data_augmentation = transforms.Compose([
transforms.Resize(( MODEL_SIZE, MODEL_SIZE)) ,
transforms.RandomHorizontalFlip() ,
transforms.RandomVerticalFlip() ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 data_test = RetinopathyDataset(data_augmentation, is_test=True)
 data_loader = torch.utils.data.DataLoader(data_test,
batch_size=16, shuffle=False,
num_workers=0, drop_last=False)
 def get_test_features(data_loader, extractor):

 for bi, d in enumerate(data_loader):
if bi % 8 == 0 : print(".", end="")
 img_tensor = d["image"].to(DEVICE)
 with torch.no_grad() : feature = extractor(img_tensor)
 feature = feature.cpu().detach().numpy()
if bi == 0 :
features = feature
else :
features = np.concatenate([features, feature], axis=0)
 return features<choose_model_class>
ImageFile.LOAD_TRUNCATED_IMAGES = True
print("................................ v")
 predictions = np.zeros(( len(data_test),5))
for tta in range(2):
print("............ tta"+str(tta)+"................ ")
 for no in range(NUM_FOLDS):
extractor = get_extractor_model(no)
 features = get_test_features(data_loader, extractor)
 print("",no)
 xgb_model = xgb.XGBClassifier()
model_path = os.path.join("xgb_model_"+str(no))
xgb_model = pickle.load(open(model_path, "rb"))
prediction = xgb_model.predict_proba(features)
 predictions = predictions + prediction<save_to_csv>
!pip install --upgrade efficientnet-pytorch
prediction_final = predictions.argmax(axis=1)
 csv_file = os.path.join(DATA_SOURCE, "sample_submission.csv")
 df = pd.read_csv(csv_file)
 df["diagnosis"] = prediction_final
df.to_csv('submission.csv',index=False )<import_modules>
from efficientnet_pytorch import EfficientNet
gc.enable()
print(os.listdir(".. /input/"))<define_variables>
from sklearn.model_selection import train_test_split
SEED = 7
np.random.seed(SEED)
 set_random_seed(SEED)
 dir_path = ".. /input/aptos2019-blindness-detection/"
IMG_DIM = 299
BATCH_SIZE = 12
CHANNEL_SIZE = 3
NUM_EPOCHS = 60
TRAIN_DIR = 'train_images'
TEST_DIR = 'test_images'
FREEZE_LAYERS = 2
CLASSS = {0: "No DR", 1: "Mild", 2: "Moderate", 3: "Severe", 4: "Proliferative DR"}<load_from_csv>
train_df=df
df_train = pd.read_csv(os.path.join(dir_path, "train.csv"))
df_test = pd.read_csv(os.path.join(dir_path, "test.csv"))
NUM_CLASSES = df_train['diagnosis'].nunique()<train_model>
train_df.reset_index(drop=True,inplace=True)
 test_df.reset_index(drop=True,inplace=True )
print("Training set has {} samples and {} classes.".format(df_train.shape[0], df_train.shape[1]))
print("Testing set has {} samples and {} classes.".format(df_test.shape[0], df_test.shape[1]))<split>
class CustomDataset(Dataset):
def __init__(self,df,root_dir,transform=None,iftest=False):
self.df=df
self.root_dir=root_dir
self.transform=transform
self.iftest=iftest
def __len__(self):
return len(self.df)
 def __getitem__(self,idx):
if torch.is_tensor(idx):
idx=idx.tolist()
img_name=self.root_dir+self.df.iloc[idx,0]+'.jpg'
image= cv2.imread(img_name,cv2.IMREAD_COLOR)
 image= cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
 if self.transform:
image=self.transform(image=image)['image']
if self.iftest:
return image
labels=torch.tensor(np.argmax(self.df.iloc[idx,1:].values))
return(image,labels)
x_train, x_test, y_train, y_test = train_test_split(df_train.id_code, df_train.diagnosis, test_size=0.2,
random_state=SEED, stratify=df_train.diagnosis )<train_model>
IMSIZE=545
IMSIZE=EfficientNet.get_image_size('efficientnet-b5' )
def check_max_min_img_height_width(df, img_dir):
max_Height , max_Width =0 ,0
min_Height , min_Width =sys.maxsize ,sys.maxsize
for idx, row in df.iterrows() :
imgPath=os.path.join(dir_path,f"{img_dir}/{row['id_code']}.png")
 img=cv2.imread(imgPath)
 H,W=img.shape[:2]
max_Height=max(H,max_Height)
 max_Width =max(W,max_Width)
 min_Height=min(H,min_Height)
 min_Width =min(W,min_Width)
 return max_Height, max_Width, min_Height, min_Width<set_options>
train_dataset=CustomDataset(df=train_df,root_dir='/kaggle/input/plant-pathology-2020-fgvc7/images/',
transform=Compose([augmentations.transforms.Resize(height=IMSIZE,width=IMSIZE,always_apply=True),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
ShiftScaleRotate(rotate_limit=25.0,p=0.7),
OneOf([IAAEmboss(p=1),IAASharpen(p=1),Blur(p=1)],p=0.5),
IAAPiecewiseAffine(p=0.5),
Normalize(( 0.485,0.456,0.406), 
(0.229,0.224,0.225),always_apply=True),
ToTensor()
]))
check_max_min_img_height_width(df_train, TRAIN_DIR )<compute_test_metric>
test_dataset=CustomDataset(df=test_df,root_dir='/kaggle/input/plant-pathology-2020-fgvc7/images/',
transform=Compose([augmentations.transforms.Resize(height=IMSIZE,width=IMSIZE,always_apply=True),
Normalize(( 0.485,0.456,0.406), 
(0.229,0.224,0.225),always_apply=True),
ToTensor()
]),iftest=True )
check_max_min_img_height_width(df_test, TEST_DIR )<train_model>
BATCH_SIZE=4
def crop_image1(img,tol=7):
mask = img>tol
return img[np.ix_(mask.any(1),mask.any(0)) ] 
<prepare_x_and_y>
train_loader=DataLoader(train_dataset,batch_size=BATCH_SIZE,shuffle=True,num_workers=2)
 test_loader=DataLoader(test_dataset,batch_size=BATCH_SIZE,shuffle=False,num_workers=2 )
def crop_image(img,tol=7):
w, h = img.shape[1],img.shape[0]
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
 gray_img = cv2.blur(gray_img,(5,5))
shape = gray_img.shape
gray_img = gray_img.reshape(-1,1)
 quant = quantile_transform(gray_img, n_quantiles=256, random_state=0, copy=True)
 quant =(quant*256 ).astype(int)
 gray_img = quant.reshape(shape)
 xp =(gray_img.mean(axis=0)>tol)
 yp =(gray_img.mean(axis=1)>tol)
 x1, x2 = np.argmax(xp), w-np.argmax(np.flip(xp))
y1, y2 = np.argmax(yp), h-np.argmax(np.flip(yp))
if x1 >= x2 or y1 >= y2 :
return img
else:
img1=img[y1:y2,x1:x2,0]
img2=img[y1:y2,x1:x2,1]
img3=img[y1:y2,x1:x2,2]
img = np.stack([img1,img2,img3],axis=-1)
 return img
def process_image(image, size=512):
image = cv2.resize(image,(size,int(size*image.shape[0]/image.shape[1])))
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 try:
image = crop_image(image, tol=15)
 except Exception as e:
image = image
print(str(e))
return image<categorify>
use_cuda = torch.cuda.is_available()
if use_cuda:
device='cuda:0'
use_tpu=False
use_device=True
if use_tpu:
device='idk'
def random_crop(img, random_crop_size):
assert img.shape[2] == 3
height, width = img.shape[0], img.shape[1]
dy, dx = random_crop_size
x = np.random.randint(0, width - dx + 1)
 y = np.random.randint(0, height - dy + 1)
 img = img[y:(y + dy), x:(x + dx), :]
return img

 def crop_generator(batches, crop_length):
while True:
batch_x, batch_y = next(batches)
 batch_crops = np.zeros(( batch_x.shape[0], crop_length, 3))
for i in range(batch_x.shape[0]):
batch_crops[0] = random_crop(batch_x[i],(crop_length, crop_length))
yield(batch_crops, batch_y)
<data_type_conversions>
def train(n_epochs,train_loader,valid_loader,model,optimizer,criterion,use_device,save_path,final_train=False,ifsched=False):
for epoch in range(1,n_epochs+1):
train_loss=0.0
valid_loss=0.0
labels_for_acc=[]
output_for_acc=[]
labels_for_accv=[]
output_for_accv=[]
model.train()
for batch_idx,(data,target)in enumerate(train_loader):
if use_device:
data,target=data.to(device),target.to(device)
 optimizer.zero_grad()
output=model(data)
 loss=criterion(output,target)
 train_loss+=loss.item() *data.size(0)
 loss.backward()
optimizer.step()
if ifsched:
scheduler.step()
labels_for_acc=np.concatenate(( labels_for_acc,target.cpu().numpy()),0)
 output_for_acc=np.concatenate(( output_for_acc,np.argmax(output.cpu().detach().numpy() ,1)) ,0)
 train_loss=train_loss/len(train_loader.dataset)
 train_acc=accuracy_score(labels_for_acc,output_for_acc)
 if not final_train:
with torch.no_grad() :
model.eval()
for batch_idx,(data,target)in enumerate(valid_loader):
if use_device:
data,target=data.to(device),target.to(device)
 output=model(data)
 loss=criterion(output,target)
 valid_loss+=loss.item() *data.size(0)
 labels_for_accv=np.concatenate(( labels_for_accv,target.cpu().numpy()),0)
 output_for_accv=np.concatenate(( output_for_accv,np.argmax(output.cpu().detach().numpy() ,1)) ,0)
 valid_loss=valid_loss/len(valid_loader.dataset)
 valid_acc=accuracy_score(labels_for_accv,output_for_accv)
 print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f} \tTrain Acc: {:.6f} \tValidation Acc: {:.6f}'.format(
 epoch,
train_loss,
valid_loss,
train_acc,
valid_acc 
))
if final_train:
print('Epoch: {} \tTraining Loss: {:.6f} \tTrain Acc: {:.6f} '.format(
 epoch,
train_loss,
train_acc 
))
gc.collect()
df_train.id_code = df_train.id_code.apply(lambda x: x + ".png")
 df_test.id_code = df_test.id_code.apply(lambda x: x + ".png")
 df_train['diagnosis'] = df_train['diagnosis'].astype('str')
<choose_model_class>
datagenerator=ImageDataGenerator(
 horizontal_flip=True,
vertical_flip=True,
rotation_range=40,
zoom_range=0.2,
shear_range=0.1,
fill_mode='nearest' )<choose_model_class>
model_efficient=EfficientNet.from_pretrained('efficientnet-b7' )
train_datagen = ImageDataGenerator(rescale=1./ 255,
validation_split=0.15,
horizontal_flip=True,
vertical_flip=True,
rotation_range=40,
zoom_range=0.2,
shear_range=0.1,
fill_mode='nearest')
<create_dataframe>
model_efficient._fc=nn.Sequential(nn.Linear(model_efficient._fc.in_features,1000,bias=True),
nn.ReLU() ,
nn.Dropout(p=0.5),
nn.Linear(1000,4,bias=True))
if use_device:
model_efficient = model_efficient.to(device )
train_generator = train_datagen.flow_from_dataframe(dataframe=df_train,
directory=".. /input/aptos2019-blindness-detection/train_images/",
x_col="id_code",
y_col="diagnosis",
batch_size=BATCH_SIZE,
class_mode="categorical",
target_size=(IMG_DIM, IMG_DIM),
subset='training',
shaffle=True,
seed=SEED, 
)
 valid_generator = train_datagen.flow_from_dataframe(dataframe=df_train,
directory=".. /input/aptos2019-blindness-detection/train_images/",
x_col="id_code",
y_col="diagnosis",
batch_size=BATCH_SIZE,
class_mode="categorical",
target_size=(IMG_DIM, IMG_DIM),
subset='validation',
shaffle=True,
seed=SEED 
)
 del x_train
del y_train
gc.collect() 
<choose_model_class>
NEPOCHS=40
print(IMSIZE)
 criterion_transfer = nn.CrossEntropyLoss()
learning_rate=8e-4
optimizer_transfer = optim.AdamW(model_efficient.parameters() ,learning_rate,weight_decay=1e-3)
 num_train_steps = int(len(train_dataset)/ BATCH_SIZE * NEPOCHS)
 scheduler = get_cosine_schedule_with_warmup(optimizer_transfer, num_warmup_steps=len(train_dataset)/BATCH_SIZE*5, num_training_steps=num_train_steps)
def design_model() :
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=(2, 2), input_shape=[IMG_DIM, IMG_DIM, CHANNEL_SIZE], activation=relu))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(filters=32, kernel_size=(2, 2), activation=relu))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(filters=64, kernel_size=(2, 2), activation=relu))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(GlobalAveragePooling2D())
 model.add(Dense(units=1000, activation=relu))
model.add(Dropout(rate=0.2))
model.add(Dense(units=1000, activation=relu))
model.add(Dropout(rate=0.2))
model.add(Dense(5, activation='softmax'))
return model
model = design_model() 
<choose_model_class>
train(NEPOCHS, train_loader,None, model_efficient, optimizer_transfer, criterion_transfer, use_device, 'model_transfer.pt',ifsched=True,final_train=True )
model.compile(optimizer=RMSprop() , loss='categorical_crossentropy', metrics=['accuracy'] )<choose_model_class>
def test(model,test_loader,use_device):
preds_for_output=np.zeros(( 1,4))
with torch.no_grad() :
model.eval()
for images in test_loader:
if use_device:
images=images.to(device)
 preds=model(images)
 preds_for_output=np.concatenate(( preds_for_output,preds.cpu().detach().numpy()),0)
 return preds_for_output
eraly_stop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=3, verbose=1, mode='auto')
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', min_delta=0.0004, patience=2, factor=0.1, min_lr=1e-6, mode='auto',
verbose=1 )<define_variables>
num_runs=5
subs=[]
for i in range(num_runs):
out=test(model_efficient,test_loader,use_device)
 output=pd.DataFrame(scipy.special.softmax(out,1),columns=['healthy','multiple_diseases','rust','scab'])
 output.drop(0,inplace=True)
 output.reset_index(drop=True,inplace=True)
 subs.append(output)
 sub_eff=sum(subs)/num_runs
<define_search_model><EOS> 
sub1=sub_eff.copy()
sub1['image_id']=test_df.image_id
sub1=sub1[['image_id','healthy','multiple_diseases','rust','scab']]
sub1.to_csv('sub_densenet.csv',index=False)
 sub1.head()