Kaggle competition: SIIM-ISIC Melanoma Classification
best_lr = best_clf_lr.best_estimator_
best_knn = best_clf_knn.best_estimator_
best_svc = best_clf_svc.best_estimator_
best_rf = best_clf_rf.best_estimator_
best_xgb = best_clf_xgb.best_estimator_
voting_clf_hard = VotingClassifier(estimators = [('knn',best_knn),('rf',best_rf),('svc',best_svc)], voting = 'hard')
 voting_clf_soft = VotingClassifier(estimators = [('knn',best_knn),('rf',best_rf),('svc',best_svc)], voting = 'soft')
 voting_clf_all = VotingClassifier(estimators = [('knn',best_knn),('rf',best_rf),('svc',best_svc),('lr', best_lr)], voting = 'soft')
 voting_clf_xgb = VotingClassifier(estimators = [('knn',best_knn),('rf',best_rf),('svc',best_svc),('xgb', best_xgb),('lr', best_lr)], voting = 'soft')
 print('voting_clf_hard :',cross_val_score(voting_clf_hard,X_train,y_train,cv=5))
print('voting_clf_hard mean :',cross_val_score(voting_clf_hard,X_train,y_train,cv=5 ).mean())
 print('voting_clf_soft :',cross_val_score(voting_clf_soft,X_train,y_train,cv=5))
print('voting_clf_soft mean :',cross_val_score(voting_clf_soft,X_train,y_train,cv=5 ).mean())
 print('voting_clf_all :',cross_val_score(voting_clf_all,X_train,y_train,cv=5))
print('voting_clf_all mean :',cross_val_score(voting_clf_all,X_train,y_train,cv=5 ).mean())
 print('voting_clf_xgb :',cross_val_score(voting_clf_xgb,X_train,y_train,cv=5))
print('voting_clf_xgb mean :',cross_val_score(voting_clf_xgb,X_train,y_train,cv=5 ).mean())
<train_on_grid>
!pip install -q efficientnet
params = {'weights' : [[1,1,1],[1,2,1],[1,1,2],[2,1,1],[2,2,1],[1,2,2],[2,1,2]]}
vote_weight = GridSearchCV(voting_clf_soft, param_grid = params, cv = 5, verbose = True, n_jobs = -1)
 best_clf_weight = vote_weight.fit(X_train_scaled,y_train)
 clf_performance(best_clf_weight,'VC Weights')
 voting_clf_sub = best_clf_weight.best_estimator_.predict(X_test_scaled)
<predict_on_test>
!python pytorch-xla-env-setup.py --version nightly --apt-packages libomp5 libopenblas-dev
voting_clf_hard.fit(X_train_scaled, y_train)
 voting_clf_soft.fit(X_train_scaled, y_train)
 voting_clf_all.fit(X_train_scaled, y_train)
 voting_clf_xgb.fit(X_train_scaled, y_train)
 best_rf.fit(X_train_scaled, y_train)
 y_hat_vc_hard = voting_clf_hard.predict(X_test_scaled ).astype(int)
 y_hat_rf = best_rf.predict(X_test_scaled ).astype(int)
 y_hat_vc_soft = voting_clf_soft.predict(X_test_scaled ).astype(int)
 y_hat_vc_all = voting_clf_all.predict(X_test_scaled ).astype(int)
 y_hat_vc_xgb = voting_clf_xgb.predict(X_test_scaled ).astype(int )<create_dataframe>
%matplotlib inline
final_data = {'PassengerId': test.PassengerId, 'Survived': y_hat_rf}
submission = pd.DataFrame(data=final_data)
 final_data_2 = {'PassengerId': test.PassengerId, 'Survived': y_hat_vc_hard}
submission_2 = pd.DataFrame(data=final_data_2)
 final_data_3 = {'PassengerId': test.PassengerId, 'Survived': y_hat_vc_soft}
submission_3 = pd.DataFrame(data=final_data_3)
 final_data_4 = {'PassengerId': test.PassengerId, 'Survived': y_hat_vc_all}
submission_4 = pd.DataFrame(data=final_data_4)
 final_data_5 = {'PassengerId': test.PassengerId, 'Survived': y_hat_vc_xgb}
submission_5 = pd.DataFrame(data=final_data_5)
 final_data_comp = {'PassengerId': test.PassengerId, 'Survived_vc_hard': y_hat_vc_hard, 'Survived_rf': y_hat_rf, 'Survived_vc_soft' : y_hat_vc_soft, 'Survived_vc_all' : y_hat_vc_all, 'Survived_vc_xgb' : y_hat_vc_xgb}
comparison = pd.DataFrame(data=final_data_comp )<feature_engineering>
print("Tensorflow version " + tf.__version__)
 AUTO = tf.data.experimental.AUTOTUNE
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync)
 DATASET = '512x512-melanoma-tfrecords-70k-images'
GCS_PATH = KaggleDatasets().get_gcs_path(DATASET )
comparison['difference_rf_vc_hard'] = comparison.apply(lambda x: 1 if x.Survived_vc_hard != x.Survived_rf else 0, axis =1)
 comparison['difference_soft_hard'] = comparison.apply(lambda x: 1 if x.Survived_vc_hard != x.Survived_vc_soft else 0, axis =1)
 comparison['difference_hard_all'] = comparison.apply(lambda x: 1 if x.Survived_vc_all != x.Survived_vc_hard else 0, axis =1)
<count_values>
train_img_path = '/kaggle/input/siim-isic-melanoma-classification/jpeg/train/'
test_img_path = '/kaggle/input/siim-isic-melanoma-classification/jpeg/test/'
comparison.difference_hard_all.value_counts()<save_to_csv>
train = pd.DataFrame(pd.read_csv("/kaggle/input/siim-isic-melanoma-classification/train.csv"))
test = pd.DataFrame(pd.read_csv("/kaggle/input/siim-isic-melanoma-classification/test.csv"))
submission.to_csv('submission_rf.csv', index =False)
 submission_2.to_csv('submission_vc_hard.csv',index=False)
 submission_3.to_csv('submission_vc_soft.csv', index=False)
 submission_4.to_csv('submission_vc_all.csv', index=False)
 submission_5.to_csv('submission_vc_xgb2.csv', index=False )<import_modules>
train.isna().sum()
import numpy as np
import pandas as pd
from pathlib import Path
from fastai.text.all import *<define_variables>
test.isna().sum()
datadir = Path.cwd().parent / 'input' / 'nlp-getting-started'<load_from_csv>
train['patient_id'].nunique() , test["patient_id"].nunique()
df_train = pd.read_csv(datadir/'train.csv')
 df_test = pd.read_csv(datadir/'test.csv')
 df_submission = pd.read_csv(datadir/'sample_submission.csv' )<create_dataframe>
print(train["target"].value_counts() )
dls = TextDataLoaders.from_df(df_train, text_col='text', label_col='target' )<define_variables>
cancer_versus_sex = train.groupby(["benign_malignant","sex"] ).size()
print(cancer_versus_sex )
dls.show_batch(max_n=2 )<choose_model_class>
cancer_versus_sex = cancer_versus_sex.unstack(level = 1)/ len(train)* 100
print(cancer_versus_sex)
 print(type(cancer_versus_sex))
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy )<set_options>
temp_train = train.anatom_site_general_challenge.value_counts().sort_values(ascending=False)
 temp_test = test.anatom_site_general_challenge.value_counts().sort_values(ascending=False)
 print("Anatom_Site valuecounts for train data",temp_train)
 print("Anatom_Site valuecounts for test data",temp_test)
learn.fine_tune(4, 1e-2 )<define_variables>
print('No of samples: ' + str(train.image_name.nunique()))
print('No of patients: ' + str(train.patient_id.nunique()))
text_list = list(df_test['text'].values )<predict_on_test>
train.groupby(['benign_malignant', 'sex'] ).nunique() ['patient_id']
%%capture
prediction = [learn.predict(i)[0] for i in text_list]<data_type_conversions>
to_encode = ['sex', 'anatom_site_general_challenge', 'diagnosis']
encoded_all = []
label_encoder = LabelEncoder()
for column in to_encode:
encoded = label_encoder.fit_transform(train[column])
 encoded_all.append(encoded)
 train['sex'] = encoded_all[0]
train['anatom_site_general_challenge'] = encoded_all[1]
train['diagnosis'] = encoded_all[2]
if 'benign_malignant' in train.columns : train.drop(['benign_malignant'], axis=1, inplace=True )
df_submission['target'] = prediction
df_submission['target'] = df_submission['target'].astype(int )<save_to_csv>
to_encode = ['sex', 'anatom_site_general_challenge']
encoded_all = []
label_encoder = LabelEncoder()
for column in to_encode:
encoded = label_encoder.fit_transform(test[column])
 encoded_all.append(encoded)
 test['sex'] = encoded_all[0]
test['anatom_site_general_challenge'] = encoded_all[1]
df_submission.to_csv('submission.csv', index=False )<load_from_csv>
image_names = train["image_name"].values
image_names = image_names + ".jpg"
image_names
train, test = pd.read_csv('.. /input/titanic/train.csv'), \
pd.read_csv('.. /input/titanic/test.csv' )<count_missing_values>
random_images = [np.random.choice(image_names)for i in range(4)]
random_images
train.isna().sum()<count_missing_values>
train_dir = "/kaggle/input/siim-isic-melanoma-classification/jpeg/train/"
test.isna().sum()<data_type_conversions>
SEED = 42
BATCH_SIZE = 32 * strategy.num_replicas_in_sync
SIZE = [512,512]
LR = 0.00004
EPOCHS = 12
WARMUP = 5
WEIGHT_DECAY = 0
LABEL_SMOOTHING = 0.05
TTA = 4
datasets = [train, test]
for d in datasets:
for i in list(d.columns):
if(d[i].notnull().sum() / d.shape[0] <=.5)\
or i in ['Name', 'Ticket']:
d.drop(i, axis=1, inplace=True)
 elif d[i].dtype == float or train[i].dtype == int:
d[i] = d[i].fillna(d[i].mean())
 else:
d[i] = d[i].fillna(d[i].mode() [0] )<prepare_x_and_y>
def seed_everything(SEED):
np.random.seed(SEED)
 tf.random.set_seed(SEED)
 seed_everything(SEED)
 train_filenames = tf.io.gfile.glob(GCS_PATH + '/train*.tfrec')
 test_filenames = tf.io.gfile.glob(GCS_PATH + '/test*.tfrec' )
y_train = train['Survived']
x_train, x_test = train.drop('Survived', axis=1), test
x_train.drop('PassengerId', axis=1, inplace=True )<categorify>
train_filenames,valid_filenames = train_test_split(train_filenames,test_size = 0.2,random_state = SEED )
datasets = [x_train, x_test]
for d in datasets:
d['Embarked'] = d['Embarked'].fillna('S')
 d['Embarked'] = d['Embarked'].map({
'S': 0,
'C': 1,
'Q': 2
})
 d['Sex'] = d['Sex'].map({
'male': 0,
'female': 1
})
 d['FamilySize'] = d['SibSp'] + d['Parch'] + 1<data_type_conversions>
def decode_image(image):
image = tf.image.decode_jpeg(image, channels=3)
 image = tf.cast(image, tf.float32)/255.0
image = tf.reshape(image, [*SIZE, 3])
 return image
def data_augment(image, label=None, seed=SEED):
image = tf.image.rot90(image,k=np.random.randint(4))
image = tf.image.random_flip_left_right(image, seed=seed)
 image = tf.image.random_flip_up_down(image, seed=seed)
 if label is None:
return image
else:
return image, label
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"target": tf.io.FixedLenFeature([], tf.int64), }
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['target'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"image_name": tf.io.FixedLenFeature([], tf.string), }
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 image_name = example['image_name']
return image, image_name
def load_dataset(filenames, labeled=True, ordered=False):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset =(tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
.with_options(ignore_order)
.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO))
return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 def plot_transform(num_images):
plt.figure(figsize=(30,10))
x = load_dataset(train_filenames, labeled=False)
 image,_ = iter(x ).next()
for i in range(1,num_images+1):
plt.subplot(1,num_images+1,i)
 plt.axis('off')
 image = data_augment(image=image)
 plt.imshow(image )
scaler = StandardScaler()
for d in datasets:
for c in ['Age', 'Fare', 'Parch', \
'Pclass', 'SibSp', 'FamilySize']:
d[c] = d[c].astype(float)
 d[c] = scaler.fit_transform(d[c].values.reshape(-1, 1))<choose_model_class>
plot_transform(7 )
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(32, input_dim=x_train.shape[1], activation='relu'),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
 ])
 model.compile(
 loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy']
)<train_model>
train_dataset =(load_dataset(train_filenames, labeled=True)
.map(data_augment, num_parallel_calls=AUTO)
.shuffle(SEED)
.batch(BATCH_SIZE,drop_remainder=True)
.repeat() 
.prefetch(AUTO))
valid_dataset =(load_dataset(valid_filenames, labeled=True)
.batch(BATCH_SIZE)
.cache() 
.prefetch(AUTO))
num_epochs = 42
history = model.fit(x_train, y_train, epochs=num_epochs, \
batch_size=50, validation_split = 0.2 )<compute_test_metric>
with strategy.scope() :
model = tf.keras.Sequential([
efn.EfficientNetB7(input_shape=(*SIZE, 3),weights='imagenet',pooling='avg',include_top=False),
Dense(1, activation='sigmoid')
 ])
 model.compile(
 optimizer='adam',
loss = tf.keras.losses.BinaryCrossentropy(label_smoothing = LABEL_SMOOTHING),
metrics=['accuracy',tf.keras.metrics.AUC(name='auc')])
scores = model.evaluate(x_train, y_train, batch_size=32 )<compute_test_metric>
def train() :
STEPS_PER_EPOCH = count_data_items(train_filenames)// BATCH_SIZE
history = model.fit(
 train_dataset,
epochs=EPOCHS,
callbacks=[lr_schedule],
steps_per_epoch=STEPS_PER_EPOCH,
validation_data=valid_dataset)
 string = 'Train acc:{:.4f} Train loss:{:.4f} AUC: {:.4f}, Val acc:{:.4f} Val loss:{:.4f} Val AUC: {:.4f}'.format(\
model.history.history['accuracy'][-1],model.history.history['loss'][-1],\
model.history.history['auc'][-1],\
model.history.history['val_accuracy'][-1],model.history.history['val_loss'][-1],\
model.history.history['val_auc'][-1])
 return string
print(f'Loss: {scores[0]} Accuracy: {scores[1]}' )<predict_on_test>
train()
x_test_without_ids = x_test.drop('PassengerId', axis=1)
 y_predicted = model.predict(x_test_without_ids)
 y_test =(y_predicted > 0.5 ).astype(int ).reshape(x_test_without_ids.shape[0] )<save_to_csv>
num_test_images = count_data_items(test_filenames)
 submission_df = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv')
 for i in range(TTA):
test_dataset =(load_dataset(test_filenames, labeled=False,ordered=True)
.map(data_augment, num_parallel_calls=AUTO)
.batch(BATCH_SIZE))
test_dataset_images = test_dataset.map(lambda image, image_name: image)
 test_dataset_image_name = test_dataset.map(lambda image, image_name: image_name ).unbatch()
test_ids = next(iter(test_dataset_image_name.batch(num_test_images)) ).numpy().astype('U')
 test_pred = model.predict(test_dataset_images, verbose=1)
 pred_df = pd.DataFrame({'image_name': test_ids, 'target': np.concatenate(test_pred)})
 temp = submission_df.copy()
del temp['target']
submission_df['target'] += temp.merge(pred_df,on="image_name")['target']/TTA
<load_from_disk><EOS> 
submission_df.to_csv('submission.csv', index=False)
 pd.Series(np.round(submission_df['target'].values)).value_counts()