Kaggle competition: Cassava Leaf Disease Classification
pipe.fit(train.values.astype('float32'), target.values )<save_to_csv>
net_predictions = pipe.predict_proba(test.values.astype('float32'))
net_submission = pd.DataFrame({'id': test_id, 'target': net_predictions[:, 1]})
 net_submission.to_csv('net.csv', index=False)
 net_submission.head()<save_to_csv>
%matplotlib inline
sns.set_style('darkgrid' )
vote_predictions =(lr1_predictions + lr2_predictions)/ 2
vote_submission = pd.DataFrame({'id': test_id, 'target': vote_predictions[:, 1]})
 vote_submission.to_csv('vote1.csv', index=False)
 vote_submission.head()<save_to_csv>
path = ".. /input/cassava-leaf-disease-classification/"
vote_predictions =(lr1_predictions * 0.1 + lr2_predictions)/ 1.1
vote_submission = pd.DataFrame({'id': test_id, 'target': vote_predictions[:, 1]})
 vote_submission.to_csv('vote2.csv', index=False)
 vote_submission.head()<save_to_csv>
train = pd.read_csv(f'{path}train.csv' )
vote_predictions =(lr1_predictions * 0.1 + net_predictions * 0.1 + lr2_predictions)/ 1.2
vote_submission = pd.DataFrame({'id': test_id, 'target': vote_predictions[:, 1]})
 vote_submission.to_csv('vote3.csv', index=False)
 vote_submission.head()<load_from_csv>
with open(f'{path}/label_num_to_disease_map.json', 'r')as f:
name_mapping = json.load(f)
 name_mapping = {int(k): v for k, v in name_mapping.items() }
name_mapping
train = pd.read_csv(".. /input/train.csv")
 print(train.head())
 test = pd.read_csv(".. /input/test.csv" )<prepare_x_and_y>
def log_t(u, t):

 if t == 1.0:
return torch.log(u)
 else:
return(u **(1.0 - t)- 1.0)/(1.0 - t)
 def exp_t(u, t):

 if t == 1.0:
return torch.exp(u)
 else:
return torch.relu(1.0 +(1.0 - t)* u)**(1.0 /(1.0 - t))
def compute_normalization_fixed_point(activations, t, num_iters=5):

 mu = torch.max(activations, dim=-1 ).values.view(-1, 1)
 normalized_activations_step_0 = activations - mu
normalized_activations = normalized_activations_step_0
i = 0
while i < num_iters:
i += 1
logt_partition = torch.sum(exp_t(normalized_activations, t), dim=-1 ).view(-1, 1)
 normalized_activations = normalized_activations_step_0 *(logt_partition **(1.0 - t))
logt_partition = torch.sum(exp_t(normalized_activations, t), dim=-1 ).view(-1, 1)
 return -log_t(1.0 / logt_partition, t)+ mu
def compute_normalization(activations, t, num_iters=5):

 if t < 1.0:
return None
else:
return compute_normalization_fixed_point(activations, t, num_iters)
 def tempered_softmax(activations, t, num_iters=5):

 if t == 1.0:
normalization_constants = torch.log(torch.sum(torch.exp(activations), dim=-1))
activations = activations.transpose(0, 1)
 else:
normalization_constants = compute_normalization(activations, t, num_iters)
 normalization_constants = normalization_constants.transpose(0, 1)
 activations = activations.transpose(0, 1)
 return exp_t(activations - normalization_constants, t)
 def bi_tempered_logistic_loss(activations, labels, t1, t2, label_smoothing=0.0, num_iters=5):

 if label_smoothing > 0.0:
num_classes = labels.shape[-1]
labels =(1 - num_classes /(num_classes - 1)* label_smoothing)* labels + label_smoothing /(num_classes - 1)
 probabilities = tempered_softmax(activations, t2, num_iters)
 probabilities = probabilities.transpose(0, 1)
 temp1 =(log_t(labels + 1e-10, t1)- log_t(probabilities, t1)) * labels
temp2 =(1 /(2 - t1)) *(torch.pow(labels, 2 - t1)- torch.pow(probabilities, 2 - t1))
loss_values = temp1 - temp2
return torch.sum(loss_values, dim=-1 )
X_train = train.drop(["id", "target"], axis=1)
 Y_train = train["target"].copy()
X_test = test.drop("id", axis = 1 ).copy()<split>
class ImageClassificationDataset(Dataset):
def __init__(
 self,
image_names: List,
transforms: Compose,
labels: Optional[List[int]],
img_path: str = '',
mode: str = 'train',
labels_to_ohe: bool = False,
n_classes: int = 5, 
):

 self.mode = mode
self.transforms = transforms
self.img_path = img_path
self.image_names = image_names
if labels is not None:
if not labels_to_ohe:
self.labels = np.array(labels)
 else:
self.labels = np.zeros(( len(labels), n_classes))
self.labels[np.arange(len(labels)) , np.array(labels)] = 1
def __getitem__(self, idx: int)-> Dict[str, np.array]:
image_path = self.img_path + self.image_names[idx]
image = cv2.imread(f'{image_path}', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if image is None:
raise FileNotFoundError(image_path)
 target = self.labels[idx]
img = self.transforms(image=image)['image']
sample = {'image_path': image_path, 'image': img, 'target': np.array(target ).astype('int64')}
return sample
def __len__(self)-> int:
return len(self.image_names )
params = {"objective": "multi:softprob", "eval_metric":"mlogloss", "num_class": 9}
iter = [110, 120, 130, 140, 150, 160, 170]
for i in iter:
skf = StratifiedKFold(n_splits=10)
 score = 0
for train, test in skf.split(X_train, Y_train):
X_train_k = X_train.iloc[train]
Y_train_k = Y_train.iloc[train]
X_test_k = X_train.iloc[test]
Y_test_k = Y_train.iloc[test]
T_train_xgb = xgb.DMatrix(X_train_k, Y_train_k)
 X_test_xgb = xgb.DMatrix(X_test_k)
 gbm = xgb.train(params, T_train_xgb, i)
 Y_pred = gbm.predict(X_test_xgb)
 val_score =0
Y_k = Y_test_k.as_matrix()
for i in range(len(Y_pred)) :
if(Y_k[i] == Y_pred[i].argmax()):
val_score +=1
val_score = val_score/len(Y_pred)
 score += val_score
score = score/10
print(i)
 print(score)
<compute_test_metric>
class CassavaDataModule(pl.LightningDataModule):
def __init__(self,
df,
train_augs,
valid_augs,
path):
super().__init__()
self.df = df
self.train_augs = train_augs
self.valid_augs = valid_augs
self.path = path
def prepare_data(self):
pass
def setup(self, stage=None):
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
 train_indexes, valid_indexes = list(folds.split(self.df, self.df['label'])) [0]
train_df = self.df.iloc[train_indexes]
valid_df = self.df.iloc[valid_indexes]
self.train_dataset = ImageClassificationDataset(image_names=train_df['image_id'].values,
transforms=train_augs,
labels=train_df['label'].values,
img_path=self.path,
mode='train',
labels_to_ohe=False,
n_classes=5)
 self.valid_dataset = ImageClassificationDataset(image_names=valid_df['image_id'].values,
transforms=valid_augs,
labels=valid_df['label'].values,
img_path=self.path,
mode='valid',
labels_to_ohe=False,
n_classes=5)
 def train_dataloader(self):
train_loader = torch.utils.data.DataLoader(
 self.train_dataset,
batch_size=16,
num_workers=4,
shuffle=True, 
)
 return train_loader
def val_dataloader(self):
valid_loader = torch.utils.data.DataLoader(
 self.valid_dataset,
batch_size=32,
num_workers=4,
shuffle=False, 
)
 return valid_loader
def test_dataloader(self):
return None
score = score/10
print(score)
<save_to_csv>
class CassvaImgClassifier(nn.Module):
def __init__(self, model_arch, n_class):
super().__init__()
self.model = EfficientNet.from_pretrained(model_arch)
 n_features = self.model._fc.in_features
self.model._fc = nn.Linear(n_features, n_class)
 def forward(self, x, targets):
logits = self.model(x)
 batch_size = targets.size() [0]
targets = nn.functional.one_hot(targets, num_classes=5)
 loss = bi_tempered_logistic_loss(activations=logits, labels=targets, t1=0.4, t2=2.0)
 loss = loss.sum() / batch_size
return logits, loss
submission = pd.DataFrame({ "id": test["id"]})
 i = 0
for num in class_range:
col_name = str("Class_{}".format(num))
submission[col_name] = Y_pred[:,i]
i = i + 1
submission.to_csv('otto1.csv', index=False )<set_options>
class LitCassava(pl.LightningModule):
def __init__(self, model):
super(LitCassava, self ).__init__()
self.model = model
self.metric = pl.metrics.Accuracy()
self.learning_rate = 1e-4
def forward(self, x, targets, *args, **kwargs):
return self.model(x, targets)
 def configure_optimizers(self):
optimizer = torch.optim.AdamW(self.model.parameters() , lr=self.learning_rate, weight_decay=0.001)
 scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.1, patience=2)
 return(
 [optimizer],
[{'scheduler': scheduler, 'interval': 'epoch', 'monitor': 'valid_loss'}], 
)
 def training_step(
 self, batch: torch.Tensor, batch_idx: int 
)-> Union[int, Dict[str, Union[torch.Tensor, Dict[str, torch.Tensor]]]]:
image = batch['image']
target = batch['target']
logits, loss = self(image, target)
 score = self.metric(logits.argmax(1), target)
 self.log('train_loss', loss, on_step=False, on_epoch=True, logger=True)
 logs = {'train_loss': loss, f'train_accuracy': score}
return {
'loss': loss,
'log': logs,
'progress_bar': logs,
'logits': logits,
'target': target,
f'train_accuracy': score,
}
def training_epoch_end(self, outputs):
avg_loss = torch.stack([x['loss'] for x in outputs] ).mean()
y_true = torch.cat([x['target'] for x in outputs])
 y_pred = torch.cat([x['logits'] for x in outputs])
 score = self.metric(y_pred.argmax(1), y_true)
 self.log('train_score', score, logger=True)
 logs = {'train_loss': avg_loss, 'train_accuracy': score}
return {'log': logs, 'progress_bar': logs}
def validation_step(
 self, batch: torch.Tensor, batch_idx: int 
)-> Union[int, Dict[str, Union[torch.Tensor, Dict[str, torch.Tensor]]]]:
image = batch['image']
target = batch['target']
logits, loss = self(image, target)
 score = self.metric(logits.argmax(1), target)
 self.log('val_loss', loss, on_step=False, on_epoch=True, logger=True)
 logs = {'valid_loss': loss, f'valid_accuracy': score}
return {
'loss': loss,
'log': logs,
'progress_bar': logs,
'logits': logits,
'target': target,
f'valid_accuracy': score,
}
def validation_epoch_end(self, outputs):
avg_loss = torch.stack([x['loss'] for x in outputs] ).mean()
y_true = torch.cat([x['target'] for x in outputs])
 y_pred = torch.cat([x['logits'] for x in outputs])
 score = self.metric(y_pred.argmax(1), y_true)
 self.log('val_score', score, logger=True)
 logs = {'valid_loss': avg_loss, f'valid_accuracy': score, 'accuracy': score}
return {'valid_loss': avg_loss, 'log': logs, 'progress_bar': logs}
warnings.simplefilter('error', SettingWithCopyWarning)
 gc.enable()
%matplotlib inline<set_options>
model = CassvaImgClassifier(model_arch="efficientnet-b3", n_class=5 )
gc.enable()
features = ['channelGrouping', 'date', 'fullVisitorId', 'visitId',\
'visitNumber', 'visitStartTime', 'device.browser',\
'device.deviceCategory', 'device.isMobile', 'device.operatingSystem',\
'geoNetwork.city', 'geoNetwork.continent', 'geoNetwork.country',\
'geoNetwork.metro', 'geoNetwork.networkDomain', 'geoNetwork.region',\
'geoNetwork.subContinent', 'totals.bounces', 'totals.hits',\
'totals.newVisits', 'totals.pageviews', 'totals.transactionRevenue',\
'trafficSource.adContent', 'trafficSource.campaign',\
'trafficSource.isTrueDirect', 'trafficSource.keyword',\
'trafficSource.medium', 'trafficSource.referralPath',\
'trafficSource.source', 'customDimensions'] 
<load_from_csv>
dm = CassavaDataModule(train, train_augs, valid_augs, f'{path}train_images/' )
def load_df(csv_path):
JSON_COLUMNS = ['device', 'geoNetwork', 'totals', 'trafficSource']
ans = pd.DataFrame()
dfs = pd.read_csv(csv_path, sep=',',
converters={column: json.loads for column in JSON_COLUMNS},
dtype={'fullVisitorId': 'str'},
chunksize=100000)
 for df in dfs:
df.reset_index(drop=True, inplace=True)
 for column in JSON_COLUMNS:
column_as_df = json_normalize(df[column])
 column_as_df.columns = [f"{column}.{subcolumn}" for subcolumn in column_as_df.columns]
df = df.drop(column, axis=1 ).merge(column_as_df, right_index=True, left_index=True)
 use_df = df[features]
del df
gc.collect()
ans = pd.concat([ans, use_df], axis=0 ).reset_index(drop=True)
 return ans<load_from_csv>
trainer = pl.Trainer(
 checkpoint_callback=ModelCheckpoint(monitor='val_loss',
save_top_k=1, filepath='{epoch}_{val_loss:.4f}_{val_score:.4f}', mode='min'),
gpus=1,
max_epochs=50,
num_sanity_val_steps=0,
weights_summary='top',
callbacks = [EarlyStopping(monitor='val_loss', patience=10, mode='min')],
fast_dev_run=False,
)
%%time
train = load_df('/kaggle/input/ga-customer-revenue-prediction/train_v2.csv')
 test = load_df('/kaggle/input/ga-customer-revenue-prediction/test_v2.csv')
 print('train date:', min(train['date']), 'to', max(train['date']))
print('test date:', min(test['date']), 'to', max(test['date']))<sort_values>
lit_model = LitCassava(model )
def missing_values(data):
total = data.isnull().sum().sort_values(ascending = False)
 percent =(data.isnull().sum() / data.isnull().count() * 100 ).sort_values(ascending = False)
 df = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 print("Total columns at least one Values: ")
 print(df[~(df['Total'] == 0)])
 print("
Total of Sales % of Total: ", round(( train[train['totals.transactionRevenue'] != np.nan]['totals.transactionRevenue'].count() / len(train['totals.transactionRevenue'])* 100),4))
return<count_missing_values>
trainer.fit(lit_model, dm )
missing_values(train )<feature_engineering>
lit_model.model.eval() ;
test['totals.transactionRevenue'] = np.nan<concatenate>
image_paths = []
targets = []
predictions = []
for i, batch in tqdm(enumerate(dm.val_dataloader())) :
with torch.no_grad() :
targets.append(batch['target'].detach().cpu().numpy())
 image_paths.append(batch['image_path'])
 pred = lit_model.model(batch['image'], batch['target'])[0]
predictions.append(pred.detach().cpu().numpy() )
all_data = train.append(test, sort=False ).reset_index(drop=True )<sort_values>
preds_df = pd.DataFrame({'target': np.concatenate(targets),
'prediction': np.concatenate(predictions ).argmax(1),
'logits': np.concatenate(predictions ).max(1),
'image_paths': [i for j in image_paths for i in j]})
 preds_df.head()
null_cnt = train.isnull().sum().sort_values()
print(null_cnt[null_cnt > 0] )<data_type_conversions>
print(metrics.classification_report(preds_df['target'], preds_df['prediction']))
for col in ['trafficSource.keyword',
'trafficSource.referralPath',
'trafficSource.adContent']:
all_data[col].fillna('unknown', inplace=True)
 all_data['totals.pageviews'].fillna(1, inplace=True)
 all_data['totals.newVisits'].fillna(0, inplace=True)
 all_data['totals.bounces'].fillna(0, inplace=True)
 all_data['totals.pageviews'] = all_data['totals.pageviews'].astype(int)
 all_data['totals.newVisits'] = all_data['totals.newVisits'].astype(int)
 all_data['totals.bounces'] = all_data['totals.bounces'].astype(int)
 all_data['trafficSource.isTrueDirect'].fillna(False, inplace=True )<drop_column>
metrics.confusion_matrix(preds_df['target'], preds_df['prediction'] )
constant_column = [col for col in all_data.columns if all_data[col].nunique() == 1]
print('drop columns:', constant_column)
 all_data.drop(constant_column, axis=1, inplace=True )<sort_values>
sub = pd.read_csv(f'{path}/sample_submission.csv')
 sub.head()
all_data[all_data['fullVisitorId'] == '7813149961404844386'].sort_values(by='visitNumber')[
['date','visitId','visitNumber','totals.hits','totals.pageviews']].head(20 )<count_unique_values>
test_dataset = ImageClassificationDataset(image_names=sub['image_id'].values,
transforms=valid_augs,
labels=sub['label'].values,
img_path=f'{path}test_images/',
mode='test',
labels_to_ohe=False,
n_classes=5)
 test_loader = torch.utils.data.DataLoader(
 test_dataset,
batch_size=4,
num_workers=4,
shuffle=False, 
)
print('unique customDimensions count:', train['customDimensions'].nunique())
 plotCategoryRateBar(all_data, train_rev, 'customDimensions' )<feature_engineering>
predictions = []
for batch in test_loader:
image = batch['image'].to('cuda')
 target = batch['target'].to('cuda')
 with torch.no_grad() :
outputs = lit_model.model(image, target)[0]
preds = outputs.argmax(1 ).detach().cpu().numpy()
predictions.append(preds )
format_str = '%Y%m%d'
all_data['formated_date'] = all_data['date'].apply(lambda x: datetime.strptime(str(x), format_str))
all_data['_year'] = all_data['formated_date'].apply(lambda x:x.year)
 all_data['_month'] = all_data['formated_date'].apply(lambda x:x.month)
 all_data['_quarterMonth'] = all_data['formated_date'].apply(lambda x:x.day//8)
 all_data['_day'] = all_data['formated_date'].apply(lambda x:x.day)
 all_data['_weekday'] = all_data['formated_date'].apply(lambda x:x.weekday())
 all_data.drop(['date','formated_date'], axis=1, inplace=True )<count_unique_values>
sub['label'] = np.concatenate(predictions )
print('train all:', len(train))
print('train unique fullVisitorId:', train['fullVisitorId'].nunique())
 print('train unique visitId:', train['visitId'].nunique())
 print('-' * 30)
 print('test all:', len(test))
print('test unique fullVisitorId:', test['fullVisitorId'].nunique())
 print('test unique visitId:', test['visitId'].nunique())
<count_values>
sub.to_csv('submission.csv', index=False )
<feature_engineering><EOS> 
sub.to_csv('submission.csv', index=False )