Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<save_to_csv>
!pip install -U -q.. /input/resnest/resnest-0.0.5-py3-none-any.whl
features = ['Pclass', 'Age', 'SibSp', 'Fare', 'Sex_male', 'Embarked_S']
X = train_data[features]
X_test = test_data[features]
model = RandomForestClassifier(n_estimators=250, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<define_variables>
HorizontalFlip, VerticalFlip, Transpose, RandomResizedCrop, Compose, Normalize, ShiftScaleRotate, CenterCrop, Resize, RandomResizedCrop 
)
sub_path = ".. /input/chemistry-models"
all_files = os.listdir(sub_path)
 all_files<feature_engineering>
import timm
concat_sub['m_max'] = concat_sub.iloc[:, 1:].max(axis=1)
 concat_sub['m_min'] = concat_sub.iloc[:, 1:].min(axis=1)
 concat_sub['m_median'] = concat_sub.iloc[:, 1:].median(axis=1 )<define_variables>
CFG = {
'seed': 1337,
'img_size': 512,
'bs': 32,
'num_workers': 4,
'tta': 4,
}
cutoff_lo = 0.8
cutoff_hi = 0.2<feature_engineering>
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()
rank = np.tril(concat_sub.iloc[:,1:ncol].corr().values,-1)
 m_gmean = 0
n = 8
while rank.max() >0:
mx = np.unravel_index(rank.argmax() , rank.shape)
 m_gmean += n*(np.log(concat_sub.iloc[:, mx[0]+1])+ np.log(concat_sub.iloc[:, mx[1]+1])) /2
rank[mx] = 0
n += 1<feature_engineering>
def seed_everything(seed: int):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
concat_sub['m_mean'] = np.exp(m_gmean/(n-1)**2 )<save_to_csv>
def get_img(path):
return cv2.imread(path)[:, :, ::-1]
concat_sub['scalar_coupling_constant'] = concat_sub['m_mean']
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_mean.csv',
index=False, float_format='%.6f' )<save_to_csv>
seed_everything(CFG['seed'] )
concat_sub['scalar_coupling_constant'] = concat_sub['m_median']
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_median.csv',
index=False, float_format='%.6f' )<save_to_csv>
class CassavaDataset(Dataset):
def __init__(self, df, data_root, transforms=None):
super().__init__()
self.df = df.reset_index(drop=True ).copy()
self.transforms = transforms
self.data_root = data_root
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index: int):
img = get_img(f"{self.data_root}/{self.df.iloc[index]['image_id']}")
 if self.transforms:
img = self.transforms(image=img)['image']
return img
concat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:7] > cutoff_lo, axis=1), 1,
np.where(np.all(concat_sub.iloc[:,1:7] < cutoff_hi, axis=1),
0, concat_sub['m_median']))
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_pushout_median.csv',
index=False, float_format='%.6f' )<feature_engineering>
class CassvaClassifierV1(nn.Module):
def __init__(self, n_classes: int = 5, dropout: float =.5):
super().__init__()
self.backbone = resnest50_fast_4s2x40d(pretrained=False)
 self.pool = nn.AdaptiveAvgPool2d(1)
 self.dropout = nn.Dropout(p=dropout)
 self.emb_size: int = 2048
self.classifier = nn.Linear(self.emb_size, n_classes)
 def cnn_feature_extractor(self, x):
x = self.backbone.conv1(x)
 x = self.backbone.bn1(x)
 x = self.backbone.relu(x)
 x = self.backbone.maxpool(x)
 x1 = self.backbone.layer1(x)
 x2 = self.backbone.layer2(x1)
 x3 = self.backbone.layer3(x2)
 x4 = self.backbone.layer4(x3)
 return x4
def forward(self, x):
x = self.cnn_feature_extractor(x)
 x = self.pool(x)
 x = self.dropout(x)
 x = x.view(-1, self.emb_size)
 x = self.classifier(x)
 return x
concat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:7] > cutoff_lo, axis=1),
concat_sub['m_max'],
np.where(np.all(concat_sub.iloc[:,1:7] < cutoff_hi, axis=1),
concat_sub['m_min'],
concat_sub['m_mean']))
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_minmax_mean.csv',
index=False, float_format='%.6f' )<feature_engineering>
class CassvaClassifierV2(nn.Module):
def __init__(self, n_classes: int = 5):
super().__init__()
self.model = timm.create_model('tf_efficientnet_b4_ns', pretrained=False)
 self.model.classifier = nn.Linear(self.model.classifier.in_features, n_classes)
 def forward(self, x):
return self.model(x )
concat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:7] > cutoff_lo, axis=1),
concat_sub['m_max'],
np.where(np.all(concat_sub.iloc[:,1:7] < cutoff_hi, axis=1),
concat_sub['m_min'],
concat_sub['m_median']))
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_minmax_median.csv',
index=False, float_format='%.6f' )<save_to_csv>
class CassvaClassifierV3(nn.Module):
def __init__(self, n_classes: int = 5):
super().__init__()
self.model = timm.create_model('tf_efficientnet_b3_ns', pretrained=False)
 self.model.classifier = nn.Linear(self.model.classifier.in_features, n_classes)
 def forward(self, x):
return self.model(x )
concat_sub['scalar_coupling_constant'] = concat_sub['mol0'].rank(method ='min')+ concat_sub['mol1'].rank(method ='min')+ concat_sub['mol2'].rank(method ='min')
 concat_sub['scalar_coupling_constant'] =(concat_sub['scalar_coupling_constant']-concat_sub['scalar_coupling_constant'].min())/(concat_sub['scalar_coupling_constant'].max() - concat_sub['scalar_coupling_constant'].min())
 concat_sub.describe()
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_rank.csv', index=False, float_format='%.8f' )<save_to_csv>
def inference_one_epoch(model, data_loader, device):
model.eval()
image_preds_all = []
for imgs in data_loader:
image_preds = model(imgs.to(device ).float())
 image_preds_all += [torch.softmax(image_preds, 1 ).cpu().numpy() ]
return np.concatenate(image_preds_all, axis=0 )
one = pd.read_csv('.. /input/champs-blending-tutorial/1.csv')
 two = pd.read_csv('.. /input/champs-blending-tutorial/2.csv')
 three = pd.read_csv('.. /input/champs-blending-tutorial/3.csv')
 submission = pd.DataFrame()
submission['id'] = one.id
submission['scalar_coupling_constant'] =(0.40*one.scalar_coupling_constant)+(0.40*two.scalar_coupling_constant)+(0.20*three.scalar_coupling_constant)
 submission.to_csv('super_blend2.csv', index=False )<install_modules>
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )
!pip install tensorflow-gpu==2.0a0<import_modules>
def get_models(model_paths, recipe: str):
models = []
for model_path in model_paths:
model_name: str = model_path.split('/')[-1]
n_folds: int = int([x[-1] for x in model_name.split('-')if x.startswith('fold')][0])
 n_epochs: int = int([x[6:] for x in model_name.split('-')if x.startswith('epochs')][0])
 if not model_name.startswith(recipe):
continue
if model_name.startswith('resnest50_fast_4s2x40d-fmix-cutmix-'):
model = CassvaClassifierV1().to(device)
 elif model_name.startswith('resnest50_fast_4s2x40d-cutmix-fmix-'):
model = CassvaClassifierV1().to(device)
 elif model_name.startswith('resnest50_fast_4s2x40d-fcl-'):
model = CassvaClassifierV1().to(device)
 elif model_name.startswith('effnetb4-fcl-'):
model = CassvaClassifierV2().to(device)
 elif model_name.startswith('effnetb3-cutmix-scce-'):
model = CassvaClassifierV3().to(device)
 elif model_name.startswith('effnetb4-cutmix-fmix-'):
model = CassvaClassifierV2().to(device)
 elif model_name.startswith('effnetb4-pseudo-cutmix-fmix-'):
model = CassvaClassifierV2().to(device)
 else:
continue
print(f'[+] load {model_name}')
 model.load_state_dict(torch.load(model_path, map_location=device))
model.eval()
models.append(model)
 return models
print(tf.__version__ )<set_options>
test = pd.DataFrame()
test['image_id'] = list(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'))
test_ds = CassavaDataset(test, '.. /input/cassava-leaf-disease-classification/test_images/', transforms=get_inference_transforms())
 tst_loader = torch.utils.data.DataLoader(
 test_ds,
batch_size=CFG['bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=True
)
tf.test.is_gpu_available(
 cuda_only=False,
min_cuda_compute_capability=None 
)
<load_pretrained>
recipe_list = [
'effnetb4-cutmix-fmix-',
'effnetb4-fcl-',
'resnest50_fast_4s2x40d-cutmix-fmix-',
'resnest50_fast_4s2x40d-fcl-',
'resnest50_fast_4s2x40d-fmix-cutmix-',
]
datadir = ".. /input/"
nodes_train = np.load(datadir + "champs-basic-graph/nodes_train.npz")['arr_0']
in_edges_train = np.load(datadir + "champs-basic-graph/in_edges_train.npz")['arr_0']
out_edges_train = np.load(datadir + "champs-basic-graph/out_edges_train.npz")['arr_0']
nodes_test = np.load(datadir + "champs-basic-graph/nodes_test.npz")['arr_0']
in_edges_test = np.load(datadir + "champs-basic-graph/in_edges_test.npz")['arr_0']<randomize_order>
preds = []
with torch.no_grad() :
for recipe in recipe_list:
models = get_models(model_paths, recipe)
 preds_per_recipe = np.mean(
 [
np.mean([inference_one_epoch(model, tst_loader, device)for _ in range(CFG['tta'])], axis=0)
 for model in models
], axis=0 
)
 preds.append(preds_per_recipe)
 del models
nodes_train, in_edges_train, out_labels = shuffle(nodes_train, in_edges_train, out_labels )<choose_model_class>
tst_preds = weights[0] * preds[0] + weights[1] * preds[1] + weights[2] * preds[2] + weights[3] * preds[3] + weights[4] * preds[4]
class Message_Passer_NNM(tf.keras.layers.Layer):
def __init__(self, node_dim):
super(Message_Passer_NNM, self ).__init__()
self.node_dim = node_dim
self.nn = tf.keras.layers.Dense(units=self.node_dim*self.node_dim, activation = tf.nn.relu)
 def call(self, node_j, edge_ij):
A = self.nn(edge_ij)
 A = tf.reshape(A, [-1, self.node_dim, self.node_dim])
 node_j = tf.reshape(node_j, [-1, self.node_dim, 1])
 messages = tf.linalg.matmul(A, node_j)
 messages = tf.reshape(messages, [-1, tf.shape(edge_ij)[1], self.node_dim])
 return messages<normalization>
test['label'] = np.argmax(tst_preds, axis=1 )
<choose_model_class><EOS> 
test.to_csv('submission.csv', index=False)
 test