Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<train_model>
import numpy as np
import pandas as pd
import xgboost as xgb
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_iris
x_train = x_train.reshape(42000, 28, 28, 1)
 x_test = x_test.reshape(28000, 28, 28, 1)
 x_train = x_train / 255.0
x_test = x_test / 255.0<choose_model_class>
train= pd.read_csv('.. /input/siim-isic-melanoma-classification/train.csv')
 test= pd.read_csv('.. /input/siim-isic-melanoma-classification/test.csv' )
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3, 3), activation='relu', input_shape=(28, 28, 1)) ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64,(3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(1024, activation='relu'),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
 ])
 model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 model.summary()<train_model>
train.target.value_counts()
model.fit(x_train, y_train, epochs=20 )<save_to_csv>
train['sex'] = train['sex'].fillna('na')
 train['age_approx'] = train['age_approx'].fillna(0)
 train['anatom_site_general_challenge'] = train['anatom_site_general_challenge'].fillna('na' )
classifications = model.predict(x_test)
 write_submissions('submission_2.csv', pd.Series(range(1,28001)) , np.argmax(classifications, axis=1))<choose_model_class>
test['sex'] = test['sex'].fillna('na')
 test['age_approx'] = test['age_approx'].fillna(0)
 test['anatom_site_general_challenge'] = test['anatom_site_general_challenge'].fillna('na' )
f, axarr = plt.subplots(3, 4)
 FIRST_IMAGE = 0
SECOND_IMAGE = 7
THIRD_IMAGE = 8
CONVOLUTION_NUMBER = 1
layer_outputs = [layer.output for layer in model.layers]
activation_model = tf.keras.models.Model(inputs=model.input, outputs=layer_outputs)
 for x in range(0, 4):
f1 = activation_model.predict(x_train[FIRST_IMAGE].reshape(1, 28, 28, 1)) [x]
axarr[0, x].imshow(f1[0, :, :, CONVOLUTION_NUMBER], cmap='inferno')
 axarr[0, x].grid(False)
 f2 = activation_model.predict(x_train[SECOND_IMAGE].reshape(1, 28, 28, 1)) [x]
axarr[1, x].imshow(f2[0, :, :, CONVOLUTION_NUMBER], cmap='inferno')
 axarr[1, x].grid(False)
 f3 = activation_model.predict(x_train[THIRD_IMAGE].reshape(1, 28, 28, 1)) [x]
axarr[2, x].imshow(f3[0, :, :, CONVOLUTION_NUMBER], cmap='inferno')
 axarr[2, x].grid(False)
<install_modules>
train['sex'] = train['sex'].astype("category" ).cat.codes +1
train['anatom_site_general_challenge'] = train['anatom_site_general_challenge'].astype("category" ).cat.codes +1
train.head()
!pip install transformers<import_modules>
test['sex'] = test['sex'].astype("category" ).cat.codes +1
test['anatom_site_general_challenge'] = test['anatom_site_general_challenge'].astype("category" ).cat.codes +1
test.head()
from tqdm.auto import tqdm
from torch import nn
from torch.nn import functional as F
from torch import optim
from torch.utils import data as torchdata
from sklearn import metrics as ms
from sklearn import model_selection as md
from matplotlib import pyplot as plt
import torch
import random
import os
import argparse as ap
import pandas as pd
import numpy as np
import transformers as tr<define_variables>
x_train = train[['sex', 'age_approx','anatom_site_general_challenge']]
y_train = train['target']
def seed_everything(seed = 42):
random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed_all(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 seed_everything()<string_transform>
x_test = test[['sex', 'age_approx','anatom_site_general_challenge']]
def process_tweet(s, save_text_formatting=False, keep_emoji=False, keep_usernames=False):
EMOJI_DESCRIPTION_SCRUB = re.compile(r':(\S+?):')
 HASHTAG_BEFORE = re.compile(r'
BAD_HASHTAG_LOGIC = re.compile(r'(\S+)!!')
 FIND_MENTIONS = re.compile(r'@(\S+)')
 LEADING_NAMES = re.compile(r'^\s*(( ?:@\S+\s*)+)')
 TAIL_NAMES = re.compile(r'\s*(( ?:@\S+\s*)+)$')
 def remove_accents(input_str):
nfkd_form = unicodedata.normalize('NFKD', input_str)
 return u"".join([c for c in nfkd_form if not unicodedata.combining(c)])
 if save_text_formatting:
s = re.sub(r'https\S+', r'xxxx', str(s))
else:
s = re.sub(r'https\S+', r' ', str(s))
s = re.sub(r'x{3,5}', r' ', str(s))
s = re.sub(r'\
', ' ', s)
 s = re.sub(r'\s', ' ', s)
 s = re.sub(r'
', ' ', s)
 s = re.sub(r'&amp;', '&', s)
 s = re.sub(r'&
s = re.sub(r'&gt;', '>', s)
 s = re.sub(r'&lt;', '<', s)
 s = re.sub(r<set_options>
train_DMatrix = xgb.DMatrix(x_train, label= y_train)
 test_DMatrix = xgb.DMatrix(x_test )
cfg = ap.Namespace()
cfg.train_file = ".. /input/nlp-getting-started/train.csv"
cfg.test_file = ".. /input/nlp-getting-started/test.csv"
cfg.x_column = "text"
cfg.y_column = "target"
cfg.device = "cuda:0"
cfg.model_name = 'roberta-base'
cfg.num_classes = 1
cfg.max_length = 128
cfg.batch_size = 32
cfg.lr = 2e-5
cfg.weight_decay = 5e-5
cfg.n_epochs = 3
cfg.warmup_ratio = 0.1
cfg.max_grad_norm = 1.0
device = torch.device(cfg.device )<prepare_x_and_y>
xgb_model = xgb.XGBClassifier(n_estimators=2000,
max_depth=8,
objective='multi:softprob',
seed=0,
nthread=-1,
learning_rate=0.15,
num_class = 2,
scale_pos_weight =(32542/584))
class DataframeDataset(torchdata.Dataset):
def __init__(self, dataframe: pd.DataFrame, x_column: str, y_column: str, tokenizer: tr.PreTrainedTokenizer, max_length: int):
super(DataframeDataset, self ).__init__()
self.X = dataframe.loc[:, [x_column]].values
if y_column is not None:
self.Y = dataframe.loc[:, [y_column]].values
else:
self.Y = None
self.tokenizer = tokenizer
self.max_length = max_length
def __len__(self):
return len(self.X)
 def __getitem__(self, idx):
x = process_tweet(self.X[idx])
 x = self.tokenizer.encode_plus(x)['input_ids']
x_tensor = np.full(( self.max_length), fill_value=self.tokenizer.pad_token_id)
 attn_mask = np.zeros_like(x_tensor)
 for i, tok in enumerate(x):
x_tensor[i] = tok
attn_mask[i] = 1
if self.Y is None:
return x_tensor, attn_mask
else:
y = self.Y[idx]
return x_tensor, attn_mask, y.astype(np.float32 )<load_pretrained>
xgb_model.fit(x_train, y_train )
tokenizer = tr.RobertaTokenizer.from_pretrained(cfg.model_name)
 model = tr.RobertaForSequenceClassification.from_pretrained(
 cfg.model_name,
num_labels=cfg.num_classes,
)<load_from_csv>
xgb_pred_result = xgb_model.predict_proba(x_test)[:,1]
print(xgb_pred_result )
train_df = pd.read_csv(cfg.train_file)
 train, valid = md.train_test_split(train_df, test_size=0.2, random_state=42)
 test = pd.read_csv(cfg.test_file )<create_dataframe>
xgb_df = pd.DataFrame({
"image_name": test["image_name"],
"target": xgb_pred_result
})
 xgb_df.to_csv('tuned_XGBClassifier_submission.csv', index=False )
train_dataset = DataframeDataset(train, cfg.x_column, cfg.y_column, tokenizer, cfg.max_length)
 valid_dataset = DataframeDataset(valid, cfg.x_column, cfg.y_column, tokenizer, cfg.max_length)
 test_dataset = DataframeDataset(test, cfg.x_column, None, tokenizer, cfg.max_length )<load_pretrained>
main_submission = pd.read_csv('.. /input/my-siim-isic-submissions/my_siim_isic_main_submission.csv')
 efficient_b7 = pd.read_csv('.. /input/my-siim-isic-submissions/EfficientNetB7_submission.csv')
 efficient_b7_blend_6 = pd.read_csv('.. /input/my-siim-isic-submissions/EfficientNetB7_submission_Blend_6.csv')
 model_blend_0_6 = pd.read_csv('.. /input/my-siim-isic-submissions/submission_models_blended_0-6.csv' )
train_dl = torchdata.DataLoader(train_dataset, batch_size=cfg.batch_size, num_workers=8, sampler=torchdata.RandomSampler(train_dataset))
valid_dl = torchdata.DataLoader(valid_dataset, batch_size=cfg.batch_size, num_workers=8, sampler=torchdata.SequentialSampler(valid_dataset))<define_variables>
final_target = main_submission.target *0.85 + efficient_b7.target *0.05 + xgb_df.target *0.10
<compute_train_metric><EOS> 
result = pd.DataFrame({
"image_name": test["image_name"],
"target": final_target
})
 result.to_csv('final_submission_blend.csv', index=False )