Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<drop_column>
OUTPUT_DIR = './'
MODEL_DIR = '.. /input/cassava-resnext50-32x4d-weights/'
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'
cols = list(X_train.columns)
 cols.remove('TransactionDT')
 for c in ['D6','D7','D8','D9','D12','D13','D14']:
cols.remove(c)
 for c in ['DT_M','day','uid']:
cols.remove(c)
 for c in ['C3','M5','id_08','id_33']:
cols.remove(c)
 for c in ['card4','id_07','id_14','id_21','id_30','id_32','id_34']:
cols.remove(c)
 for c in ['id_'+str(x)for x in range(22,28)]:
cols.remove(c )<choose_model_class>
class CFG:
debug=False
num_workers=8
model_name='resnext50_32x4d'
size=512
batch_size=32
seed=2020
target_size=5
target_col='label'
n_fold=5
trn_fold=[0, 1, 2, 3, 4]
inference=True
def create_model(data, catcols, numcols, hidden_units, spatial_dropout_rate, dropout_rate):
inp_cat = []
outputs = []
for c in catcols:
num_unique_values = int(data[c].nunique())
 embed_dim = int(min(np.ceil(( num_unique_values)/ 2), 50))
inp = layers.Input(shape =(1,))
out = layers.Embedding(num_unique_values + 1, embed_dim, name = c )(inp)
 out = layers.SpatialDropout1D(spatial_dropout_rate )(out)
 out = layers.Reshape(( embed_dim,))(out)
 inp_cat.append(inp)
 outputs.append(out)
 inp_num = layers.Input(shape =(len(numcols),))
outputs.append(inp_num)
 x = layers.Concatenate()(outputs)
 x = layers.BatchNormalization()(x)
 for units in hidden_units:
x = layers.Dense(units, activation = "relu" )(x)
 x = layers.Dropout(dropout_rate )(x)
 x = layers.BatchNormalization()(x)
 y = layers.Dense(1, activation = 'sigmoid' )(x)
 model = Model(inputs = [inp_cat, inp_num], outputs = y)
 return model<define_variables>
test = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 test.head()
catcols = []
numcols = []
for c in X_train[cols].columns:
if(X_train[c].dtype == 'int64')or(X_train[c].dtype == 'int32'):
catcols.append(c)
 else:
numcols.append(c)
 print(catcols )<categorify>
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 = cv2.imread(file_path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform:
augmented = self.transform(image=image)
 image = augmented['image']
return image
for c in catcols:
le = LabelEncoder()
le.fit(np.concatenate([X_train[c], X_test[c]]))
X_train[c] = le.transform(X_train[c])
 X_test[c] = le.transform(X_test[c])
 del le
gc.collect()<normalization>
def get_transforms(*, data):
if data == 'valid':
return A.Compose([
A.Resize(CFG.size, CFG.size),
A.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], 
),
ToTensorV2() ,
] )
%%time
scaler = StandardScaler()
scaler.fit(pd.concat([X_train[numcols], X_test[numcols]]))
X_train[numcols] = scaler.transform(X_train[numcols])
 X_test[numcols] = scaler.transform(X_test[numcols])
 del scaler
gc.collect()<choose_model_class>
class CustomResNext(nn.Module):
def __init__(self, model_name='resnext50_32x4d', pretrained=False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=pretrained)
 n_features = self.model.fc.in_features
self.model.fc = nn.Linear(n_features, CFG.target_size)
 def forward(self, x):
x = self.model(x)
 return x
hidden_units = [1024, 512, 256]
spatial_dropout_rate = 0.3
dropout_rate = 0.5
data = pd.concat([X_train[cols], X_test[cols]])
 with strategy.scope() :
model = create_model(data, catcols, numcols, hidden_units, spatial_dropout_rate, dropout_rate)
 model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = [tf.metrics.AUC(name = 'AUC')])
 model.summary()
del data
gc.collect()<define_variables>
def load_state(model_path):
model = CustomResNext(CFG.model_name, pretrained=False)
 try:
model.load_state_dict(torch.load(model_path)['model'], strict=True)
 state_dict = torch.load(model_path)['model']
except:
state_dict = torch.load(model_path)['model']
state_dict = {k[7:] if k.startswith('module.')else k: state_dict[k] for k in state_dict.keys() }
return state_dict
def inference(model, states, test_loader, device):
model.to(device)
 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.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
<set_options><EOS> 
model = CustomResNext(CFG.model_name, pretrained=False)
 states = [load_state(MODEL_DIR+f'{CFG.model_name}_fold{fold}.pth')for fold in CFG.trn_fold]
test_dataset = TestDataset(test, transform=get_transforms(data='valid'))
test_loader = DataLoader(test_dataset, batch_size=CFG.batch_size, shuffle=False,
num_workers=CFG.num_workers, pin_memory=True)
 predictions = inference(model, states, test_loader, device)
 test['label'] = predictions.argmax(1)
 test[['image_id', 'label']].to_csv(OUTPUT_DIR+'submission.csv', index=False)
 test.head()