Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<prepare_x_and_y>
%matplotlib inline
warnings.filterwarnings("ignore", category=FutureWarning )
a = np.array(lsti)
 a<feature_engineering>
def open_json_file(file):

 json_opened = json.load(open(file))
return json_opened
preds['target'] = a.mean(axis=0 )<save_to_csv>
train = open_json_file('.. /input/train.json')
 test = open_json_file('.. /input/test.json' )
%%time
del sub['target']
sub = sub.merge(preds, on='image_name')
 sub.head()
sub.to_csv('effB1_512_5x15epochs_3Stratif.csv', index=False)
<load_from_csv>
print("--- sample of training data set ---")
 train[0:1]
def MinMaxBestBaseStacking(input_folder, best_base, output_path):
sub_base = pd.read_csv(best_base)
 all_files = os.listdir(input_folder)
 outs = [pd.read_csv(os.path.join(input_folder, f), index_col=0)for f in all_files]
concat_sub = pd.concat(outs, axis=1)
 cols = list(map(lambda x: "target" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols
concat_sub.reset_index(inplace=True)
 concat_sub['is_iceberg_max'] = concat_sub.iloc[:, 1:6].max(axis=1)
 concat_sub['is_iceberg_min'] = concat_sub.iloc[:, 1:6].min(axis=1)
 concat_sub['is_iceberg_mean'] = concat_sub.iloc[:, 1:6].mean(axis=1)
 concat_sub['is_iceberg_median'] = concat_sub.iloc[:, 1:6].median(axis=1)
 cutoff_lo = 0.66
cutoff_hi = 0.33
concat_sub['is_iceberg_base'] = sub_base['target']
concat_sub['target'] = np.where(np.all(concat_sub.iloc[:, 1:6] > cutoff_lo, axis=1),
concat_sub['is_iceberg_max'],
np.where(np.all(concat_sub.iloc[:, 1:6] < cutoff_hi, axis=1),
concat_sub['is_iceberg_min'],
concat_sub['is_iceberg_base']))
concat_sub[['image_name', 'target']].to_csv(output_path,
index=False, float_format='%.12f' )<load_from_csv>
print("--- sample of testing data set ---")
 test[0:1]
MinMaxBestBaseStacking('.. /input/cs0099/', '.. /input/cs0099/submission_mean.csv', 'submission.csv')
<define_variables>
print("Training data set size:",len(train))
print("Testing data set size:",len(test))
DEVICE = "TPU"
CFG = dict(
 net_count = 8,
batch_size = 8,
read_size = 256,
crop_size = 250,
net_size = 224,
LR_START = 0.000004,
LR_MAX = 0.000020,
LR_MIN = 0.000001,
LR_RAMPUP_EPOCHS = 5,
LR_SUSTAIN_EPOCHS = 0,
LR_EXP_DECAY = 0.0075,
epochs = 12,
rot = 180.0,
shr = 2.0,
hzoom = 8.0,
wzoom = 8.0,
hshift = 8.0,
wshift = 8.0,
optimizer = 'adam',
label_smooth_fac = 0.05,
tta_steps = 50
)<install_modules>
def pre_process_data(data):

 new_data = []
for recipe in data:
new_recipe = []
for ingredient in recipe:
new_ingredient_list = []
for word in ingredient.split() :
word = re.sub('[^a-zA-Z -]+', '', word)
 new_ingredient_list.append(wn.morphy(word.lower().strip(",.!:?;' ")) or word.strip(",.!:?;' "))
new_recipe.append(' '.join(new_ingredient_list))
new_data.append(new_recipe)
 return new_data
!pip install -q efficientnet<set_options>
x_train = list([train[i]['ingredients'] for i in range(len(train)) ])
 y_train_labels = [train[i]['cuisine'] for i in range(len(train)) ]
random.seed(a=42)
<sort_values>
x_test = list([test[i]['ingredients'] for i in range(len(test)) ] )
BASEPATH = ".. /input/siim-isic-melanoma-classification"
GCS_PATH = KaggleDatasets().get_gcs_path('oc-d-512512')
 files_train = np.sort(np.array(tf.io.gfile.glob(GCS_PATH + '/*')))
<choose_model_class>
new_x_train = pre_process_data(x_train)
 new_x_test = pre_process_data(x_test )
if DEVICE == "TPU":
print("connecting to TPU...")
 try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
print("Could not connect to TPU")
 tpu = None
if tpu:
try:
print("initializing TPU...")
 tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("TPU initialized")
 except _:
print("failed to initialize TPU")
 else:
DEVICE = "GPU"
if DEVICE != "TPU":
print("Using default strategy for CPU and single GPU")
 strategy = tf.distribute.get_strategy()
if DEVICE == "GPU":
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
 AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print(f'REPLICAS: {REPLICAS}' )<normalization>
print("--- Label Encode the Target Variable ---")
 lb_enc = LabelEncoder()
y_enc_labels = lb_enc.fit_transform(y_train_labels)
 print(y_enc_labels )
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 def get_3x3_mat(lst):
return tf.reshape(tf.concat([lst],axis=0), [3,3])
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = get_3x3_mat([c1, s1, zero,
-s1, c1, zero,
zero, zero, one])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = get_3x3_mat([one, s2, zero,
zero, c2, zero,
zero, zero, one])
 zoom_matrix = get_3x3_mat([one/height_zoom, zero, zero,
zero, one/width_zoom, zero,
zero, zero, one])
 shift_matrix = get_3x3_mat([one, zero, height_shift,
zero, one, width_shift,
zero, zero, one])
 return K.dot(K.dot(rotation_matrix, shear_matrix),
K.dot(zoom_matrix, shift_matrix))
def transform(image, cfg):
DIM = cfg["read_size"]
XDIM = DIM%2
rot = cfg['rot'] * tf.random.normal([1], dtype='float32')
 shr = cfg['shr'] * tf.random.normal([1], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ cfg['hzoom']
w_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ cfg['wzoom']
h_shift = cfg['hshift'] * tf.random.normal([1], dtype='float32')
 w_shift = cfg['wshift'] * tf.random.normal([1], dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2, -DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2, DIM//2), [DIM])
 z = tf.ones([DIM*DIM], dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m, tf.cast(idx, dtype='float32'))
idx2 = K.cast(idx2, dtype='int32')
 idx2 = K.clip(idx2, -DIM//2+XDIM+1, DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d,[DIM, DIM,3] )<prepare_x_and_y>
def bag_of_words(data):
	text_data = [' '.join(recipe ).lower() for recipe in data]
	return text_data
def concatenated_words(data):
	text_data = [' '.join(word.replace(" ","_" ).lower() for word in recipe)for recipe in data]
	return text_data
def read_labeled_tfrecord(example):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'filename' : tf.io.FixedLenFeature([], tf.string),
'target' : tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['target']
def read_unlabeled_tfrecord(example, return_image_name):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'filename' : tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['filename'] if return_image_name else 0
def prepare_image(img, cfg=None, augment=True):
img = tf.image.decode_jpeg(img, channels=3)
 img = tf.image.resize(img, [cfg['read_size'], cfg['read_size']])
 img = tf.cast(img, tf.float32)/ 255.0
if augment:
img = transform(img, cfg)
 img = tf.image.random_crop(img, [cfg['crop_size'], cfg['crop_size'], 3])
 img = tf.image.random_flip_left_right(img)
 img = tf.image.random_hue(img, 0.01)
 img = tf.image.random_saturation(img, 0.7, 1.3)
 img = tf.image.random_contrast(img, 0.8, 1.2)
 img = tf.image.random_brightness(img, 0.1)
 else:
img = tf.image.central_crop(img, cfg['crop_size'] / cfg['read_size'])
 img = tf.image.resize(img, [cfg['net_size'], cfg['net_size']])
 img = tf.reshape(img, [cfg['net_size'], cfg['net_size'], 3])
 return img
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1))
for filename in filenames]
return np.sum(n )<categorify>
print("--- Preparing text data ---")
 train_text = bag_of_words(x_train)
 submission_text = bag_of_words(x_test)
 prep_train_text = bag_of_words(new_x_train)
 prep_submission_text = bag_of_words(new_x_test)
 prep_train_text_underscore = concatenated_words(new_x_train)
 prep_submission_text_underscore = concatenated_words(new_x_test)
def get_dataset(files, cfg, augment = False, shuffle = False, repeat = False,
labeled=True, return_image_names=True):
ds = tf.data.TFRecordDataset(files, num_parallel_reads=AUTO)
 ds = ds.cache()
if repeat:
ds = ds.repeat()
if shuffle:
ds = ds.shuffle(1024*8)
 opt = tf.data.Options()
opt.experimental_deterministic = False
ds = ds.with_options(opt)
 if labeled:
ds = ds.map(read_labeled_tfrecord, num_parallel_calls=AUTO)
 else:
ds = ds.map(lambda example: read_unlabeled_tfrecord(example, return_image_names),
num_parallel_calls=AUTO)
 ds = ds.map(lambda img, imgname_or_label:(prepare_image(img, augment=augment, cfg=cfg),
imgname_or_label),
num_parallel_calls=AUTO)
 ds = ds.batch(cfg['batch_size'] * REPLICAS)
 ds = ds.prefetch(AUTO)
 return ds<prepare_x_and_y>
tfidf_enc = TfidfVectorizer()
def tf_idf_features(text, flag):

 if flag == "train":
x = tfidf_enc.fit_transform(text)
 else:
x = tfidf_enc.transform(text)
 x = x.astype('float16')
 return x
def show_dataset(thumb_size, cols, rows, ds):
mosaic = PIL.Image.new(mode='RGB', size=(thumb_size*cols +(cols-1),
thumb_size*rows +(rows-1)))
 for idx, data in enumerate(iter(ds)) :
img, target_or_imgid = data
ix = idx % cols
iy = idx // cols
img = np.clip(img.numpy() * 255, 0, 255 ).astype(np.uint8)
 img = PIL.Image.fromarray(img)
 img = img.resize(( thumb_size, thumb_size), resample=PIL.Image.BILINEAR)
 mosaic.paste(img,(ix*thumb_size + ix,
iy*thumb_size + iy))
display(mosaic)
 ds = get_dataset(files_train, CFG ).unbatch().take(12*5)
 show_dataset(64, 12, 5, ds )<categorify>
train_text_features = tf_idf_features(train_text, flag="train")
 submission_text_features = tf_idf_features(submission_text, flag="submission")
 prep_train_text_features = tf_idf_features(prep_train_text, flag="train")
 prep_submission_text_features = tf_idf_features(prep_submission_text, flag="submission")
 prep_train_text_underscore_features = tf_idf_features(prep_train_text_underscore, flag="train")
 prep_submission_text_underscore_features = tf_idf_features(prep_submission_text_underscore, flag="submission" )
print("image before the augmentaion ")
 ds = get_dataset(files_train, CFG ).unbatch().take(1)
 show_dataset(200, 1, 1, ds)
 print("image after the augmentaion ")
 ds = tf.data.TFRecordDataset(files_train, num_parallel_reads=AUTO)
 ds = ds.take(1 ).cache().repeat()
ds = ds.map(read_labeled_tfrecord, num_parallel_calls=AUTO)
 ds = ds.map(lambda img, target:(prepare_image(img, cfg=CFG, augment=True), target),
num_parallel_calls=AUTO)
 ds = ds.take(12*5)
 ds = ds.prefetch(AUTO)
 show_dataset(64, 12, 5, ds )<install_modules>
RANDOM_SEED = 1
names = ['DecisionTree','RandomForestClassifier','Logistic_Regression', "SVC"]
regressors = [DecisionTreeClassifier(random_state = RANDOM_SEED),
RandomForestClassifier(n_estimators = 10, random_state = RANDOM_SEED),
LogisticRegression(random_state = RANDOM_SEED),
SVC(C=10, gamma = 1, decision_function_shape=None, random_state = RANDOM_SEED),
]
def kfold_cross_validation(X,y, col_names):
N_FOLDS = 3
cv_results = np.zeros(( N_FOLDS, len(names)))
 kf = KFold(n_splits = N_FOLDS, shuffle = False, random_state = RANDOM_SEED)
 index_for_fold = 0
for train_index, test_index in kf.split(X,y):
print('
Fold index:', index_for_fold + 1,
'------------------------------------------')
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
 print('
Shape of input data for this fold:',
'
Data Set:(Observations, Variables)')
 print('X_train:', X_train.shape)
 print('X_test:',X_test.shape)
 print('y_train:', y_train.shape)
 print('y_test:',y_test.shape)
 index_for_method = 0
for name, reg_model in zip(names, regressors):
print('
Regression model evaluation for:', name)
 print(' Scikit Learn method:', reg_model)
 reg_model.fit(X_train, y_train)
 y_test_predict = reg_model.predict(X_test)
 fold_method_result = reg_model.score(X_test, y_test)
 cv_results[index_for_fold, index_for_method] = fold_method_result
index_for_method += 1
index_for_fold += 1
cv_results_df = pd.DataFrame(cv_results)
 cv_results_df.columns = col_names
return cv_results_df
!pip install -q efficientnet
!pip install -q git+https://github.com/AmedeoBiolatti/dsqol<import_modules>
starttime = time.monotonic()
print("--- Performing 3-fold cross validation ---
")
 print("---------Standard Results---------")
 train_text_cv = kfold_cross_validation(train_text_features,
y_enc_labels,
col_names = ["DecisionTree",
"RandomForest",
"LogisticRegression",
"SVM"])
 print("
---------Removing Special Characters & Punctuations Results---------")
 prep_train_text_cv = kfold_cross_validation(prep_train_text_features,
y_enc_labels,
col_names = ["DecisionTree",
"RandomForest",
"LogisticRegression",
"SVM"])
 print("
---------Concatenated Words Results---------")
 prep_train_text_underscore_cv = kfold_cross_validation(prep_train_text_underscore_features,
y_enc_labels,
col_names = ["DecisionTree",
'RandomForest',
'LogisticRegression',
"SVM"])
 print("
That took ",(time.monotonic() -starttime)/60, " minutes" )
import os, re, time, tqdm
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import metrics, model_selection
import tensorflow as tf
import tensorflow_addons as tfa
from tensorflow import keras
from tensorflow.keras import backend as K
from efficientnet import tfkeras as efnet
from kaggle_datasets import KaggleDatasets<import_modules>
baseline_models = pd.DataFrame(prep_train_text_cv.mean() ,columns=["Avg Accuracy"])
 baseline_models = baseline_models.reset_index()
baseline_models.columns = ["Model","Baseline Score"]
from dsqol.tf import imgaug
from dsqol.tf.data import balance
from dsqol.tf.utils import average
from dsqol.tf import losses<define_variables>
def grid_search_function(func_X_train, func_X_test, func_y_train, func_y_test, parameters, model):
model = model(random_state=RANDOM_SEED)
 grid_search = GridSearchCV(model, parameters)
 classifier= grid_search.fit(func_X_train,func_y_train)
 return classifier
SEED = 42
tf.random.set_seed(SEED)
 np.random.seed(SEED)
 TIME_BUDGET = 2.5 * 3600
FOLDS = 6
INCLUDE_2019 = 1
INCLUDE_2018 = 1
INCLUDE_MALIGNANT = 1
IMG_READ_SIZE = 384
IMG_SIZE = 384
BALANCE_POS_RATIO = 0.08
EFF_NET = 1
LOSS_TYPE = 'BCE'
LOSS_PARAMS = dict(label_smoothing=0.09)
 BATCH_SIZE = 64
EPOCHS = 25
TBM = 6
TTA = 15
VALID_FREQ = 1
N_SWA = 3
SWA_DECAY = 0.9<train_on_grid>
def train_test_split_function(X,y, test_size_percent):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size_percent, random_state=RANDOM_SEED)
 return X_train, X_test, y_train, y_test
DEVICE = "TPU"
print("connecting to TPU...")
 try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
print("Could not connect to TPU")
 tpu = None
if tpu:
try:
print("initializing TPU...")
 tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("TPU initialized")
 except _:
print("failed to initialize TPU")
 else:
DEVICE = "GPU"
if DEVICE != "TPU":
print("Using default strategy for CPU and single GPU")
 strategy = tf.distribute.get_strategy()
if DEVICE == "GPU":
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
 AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
GLOBAL_BATCH_SIZE = BATCH_SIZE * REPLICAS
print("REPLICAS: %d" % REPLICAS )<define_variables>
starttime = time.monotonic()
parameters = {'n_estimators':[10,100,300]}
X_train, X_test, y_train, y_test = train_test_split_function(prep_train_text_features,
y_enc_labels,
test_size_percent = 0.20)
 rf_classifier = grid_search_function(X_train, X_test, y_train, y_test,
parameters,
model = RandomForestClassifier)
 print("
That took ",(time.monotonic() -starttime)/60, " minutes" )
GCS_PATH1 = KaggleDatasets().get_gcs_path('melanoma-%ix%i' %(IMG_READ_SIZE, IMG_READ_SIZE))
GCS_PATH2 = KaggleDatasets().get_gcs_path('isic2019-%ix%i' %(IMG_READ_SIZE, IMG_READ_SIZE))
GCS_PATH3 = KaggleDatasets().get_gcs_path('malignant-v2-%ix%i' %(IMG_READ_SIZE, IMG_READ_SIZE))<load_from_csv>
rf_classifier.best_estimator_
df_base_train = pd.read_csv(".. /input/siim-isic-melanoma-classification/train.csv")
 df_base_test = pd.read_csv(".. /input/siim-isic-melanoma-classification/test.csv" )<define_variables>
def training_info_and_classification_report(X,y,test_size_percent, model):

 starttime = time.monotonic()
X_train, X_test, y_train, y_test = train_test_split_function(X,y, test_size_percent)
 print("
--- Fitting Model ---")
 model.fit(X_train, y_train)
 print("
--- Predicting Cuisines ---")
 y_predict = model.predict(X_test)
 print("
--- Scoring Model ---")
 model_training_score = model.score(X_train,y_train)
 model_testing_score = model.score(X_test,y_test)
 print("
--- Creating Classification Report ---")
 clf_report = classification_report(y_test,
y_predict,
target_names = lb_enc.inverse_transform(model.classes_ ).tolist())
 array = confusion_matrix(y_test, y_predict)
 row_sums = array.sum(axis=1,keepdims=True)
 norm_conf_mx = array/row_sums
error_matrix_df = pd.DataFrame(norm_conf_mx, index = [i for i in lb_enc.inverse_transform(model.classes_ ).tolist() ],
columns = [i for i in lb_enc.inverse_transform(model.classes_ ).tolist() ])
 print("
That took ",(time.monotonic() -starttime)/60, " minutes")
 return model_training_score, model_testing_score, clf_report, array, norm_conf_mx, error_matrix_df
train_files = tf.io.gfile.glob(os.path.join(GCS_PATH1, "train*.tfrec"))
if INCLUDE_2019:
train_files += tf.io.gfile.glob([os.path.join(GCS_PATH2, "train%.2i*.tfrec" % i)for i in range(1, 30, 2)])
 if INCLUDE_2018:
train_files += tf.io.gfile.glob([os.path.join(GCS_PATH2, "train%.2i*.tfrec" % i)for i in range(0, 30, 2)])
 if INCLUDE_MALIGNANT:
train_files += tf.io.gfile.glob([os.path.join(GCS_PATH3, "train%.2i*.tfrec" % i)for i in range(15, 30, 1)])
 print("%d train files found" % len(train_files))<train_model>
training_score_rf,testing_score_rf,rf_classification_report, rf_array, rf_norm_conf_mx, rf_error_matrix_df = training_info_and_classification_report(prep_train_text_features,
y_enc_labels,
test_size_percent = 0.20,
model = rf_classifier.best_estimator_ )
test_files = tf.io.gfile.glob(os.path.join(GCS_PATH1, "test*.tfrec"))
print("%d test files found" % len(test_files))<prepare_x_and_y>
print(" Random Forest Classification Report ")
 print(rf_classification_report )
def read_labeled_tfrecord(example):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
'target' : tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['target']
def read_unlabeled_tfrecord(example, return_image_name=True):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['image_name'] if return_image_name else 0
def prepare_image(img):
img = tf.image.decode_jpeg(img, channels=3)
 img = tf.cast(img, tf.float32)/ 255.0
return img
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1))
for filename in filenames]
return np.sum(n )<categorify>
starttime = time.monotonic()
parameters = {'C':[1,5,10,100,1000]}
X_train, X_test, y_train, y_test = train_test_split_function(prep_train_text_features,
y_enc_labels,
test_size_percent = 0.20)
 logreg_classifier = grid_search_function(X_train, X_test, y_train, y_test, parameters, model = LogisticRegression)
 print("
That took ",(time.monotonic() -starttime)/60, " minutes" )
def dropout(image, DIM=256, PROBABILITY = 0.75, CT = 8, SZ = 0.2):
P = tf.cast(tf.random.uniform([],0,1)<PROBABILITY, tf.int32)
 if(P==0)|(CT==0)|(SZ==0):
return image
for k in range(CT):
x = tf.cast(tf.random.uniform([],0,DIM),tf.int32)
 y = tf.cast(tf.random.uniform([],0,DIM),tf.int32)
 WIDTH = tf.cast(SZ*DIM,tf.int32)* P
ya = tf.math.maximum(0,y-WIDTH//2)
 yb = tf.math.minimum(DIM,y+WIDTH//2)
 xa = tf.math.maximum(0,x-WIDTH//2)
 xb = tf.math.minimum(DIM,x+WIDTH//2)
 one = image[ya:yb,0:xa,:]
two = tf.zeros([yb-ya,xb-xa,3])
 three = image[ya:yb,xb:DIM,:]
middle = tf.concat([one,two,three],axis=1)
 image = tf.concat([image[0:ya,:,:],middle,image[yb:DIM,:,:]],axis=0)
 image = tf.reshape(image,[DIM,DIM,3])
 return image<create_dataframe>
logreg_classifier.best_estimator_
def get_dataset(files, augment=False, repeat=False, shuffle=False, labeled=True, batch_size=16, drop_remainder=False,
dim=256, read_dim=None 
)-> tf.data.Dataset:
if read_dim is None:
read_dim = dim
ds = tf.data.TFRecordDataset(files, num_parallel_reads=AUTO)
 ds = ds.cache()
if repeat:
ds = ds.repeat()
if shuffle:
ds = ds.shuffle(1024 * 8)
 opt = tf.data.Options()
opt.experimental_deterministic = False
ds = ds.with_options(opt)
 if labeled:
ds = ds.map(read_labeled_tfrecord, num_parallel_calls=AUTO)
 else:
ds = ds.map(read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 ds = ds.map(lambda i, o:(prepare_image(i), o), num_parallel_calls=AUTO)
 if augment:
ds = basic_augmentation_pipeline(ds, batch_size=8 * batch_size, dim=read_dim)
 if isinstance(augment, list):
for a in augment:
ds = ds.map(a, num_parallel_calls=AUTO)
 ds = ds.map(lambda i, o:(tf.image.resize(i, [dim, dim]), o), num_parallel_calls=AUTO)
 ds = ds.batch(batch_size, drop_remainder=drop_remainder)
 ds = ds.prefetch(AUTO)
 return ds
def get_balanced_dataset(files, augment=False, repeat=False, shuffle=False, batch_size=16, drop_remainder=False,
dim=256, read_dim=None, pos_ratio=False)-> tf.data.Dataset:
if read_dim is None:
read_dim = dim
ds = tf.data.TFRecordDataset(files, num_parallel_reads=AUTO)
 ds = ds.cache()
if repeat:
ds = ds.repeat()
ds = ds.map(read_labeled_tfrecord, num_parallel_calls=AUTO)
 ds0, ds1 = balance.separate_by_target(ds)
 ds0 = ds.map(lambda i, o:(prepare_image(i), o), num_parallel_calls=AUTO)
 ds1 = ds.map(lambda i, o:(prepare_image(i), o), num_parallel_calls=AUTO)
 ds = balance.merge_ds(ds0, ds1, pos_ratio)
 if shuffle:
ds = ds.shuffle(1024)
 opt = tf.data.Options()
opt.experimental_deterministic = False
ds = ds.with_options(opt)
 if augment:
ds = basic_augmentation_pipeline(ds, batch_size=8 * batch_size, dim=read_dim)
 if isinstance(augment, list):
for a in augment:
ds = ds.map(a, num_parallel_calls=AUTO)
 ds = ds.map(lambda i, o:(tf.image.resize(i, [dim, dim]), o), num_parallel_calls=AUTO)
 ds = ds.batch(batch_size, drop_remainder=drop_remainder)
 ds = ds.prefetch(AUTO)
 return ds<choose_model_class>
training_score_lgr,testing_score_lgr,lgr_classification_report, lgr_array, lgr_norm_conf_mx, lgr_error_matrix_df = training_info_and_classification_report(prep_train_text_features,
y_enc_labels,
test_size_percent = 0.20,
model = logreg_classifier.best_estimator_ )
def build_model(dim=128, ef=0):
inp = keras.layers.Input(shape=(dim,dim,3))
base = getattr(efnet, 'EfficientNetB%d' % ef )(input_shape=(dim, dim, 3), weights='imagenet', include_top=False)
 x = base(inp)
 x = keras.layers.GlobalAveragePooling2D()(x)
 x = keras.layers.Dropout(0.3 )(x)
 x = keras.layers.Dense(512 )(x)
 x = keras.layers.Dropout(0.4 )(x)
 x = keras.layers.Dense(256 )(x)
 x = keras.layers.Dropout(0.3 )(x)
 x = keras.layers.Dense(128 )(x)
 x = keras.layers.Dense(1 )(x)
 x = keras.layers.Activation('sigmoid', dtype='float32' )(x)
 model = keras.Model(inputs=inp,outputs=x)
 opt = keras.optimizers.Adam(learning_rate=1e-3)
 if LOSS_TYPE.upper() == 'BCE':
loss = keras.losses.BinaryCrossentropy(**LOSS_PARAMS)
 elif LOSS_TYPE.upper() == 'FOCAL':
loss = losses.BinaryFocalLoss(**LOSS_PARAMS)
 model.compile(optimizer=opt, loss=loss, metrics=['AUC'])
 return model<init_hyperparams>
That took ",(time.monotonic() -starttime)/60, " minutes")
mult = 1
lr_start = 6e-6
lr_max = 1.45e-6 * GLOBAL_BATCH_SIZE
lr_min = 1e-6
lr_ramp_ep = 5
lr_sus_ep = 0
lr_decay = 0.85
def lrfn(epoch):
if epoch < lr_ramp_ep:
lr =(lr_max - lr_start)/ lr_ramp_ep * epoch + lr_start
elif epoch < lr_ramp_ep + lr_sus_ep:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_decay**(epoch - lr_ramp_ep - lr_sus_ep)+ lr_min
return lr * mult
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
 plt.plot([lrfn(e)for e in range(EPOCHS)])
 plt.xlabel('Epoch'); plt.ylabel('LR')
 plt.subplot(1, 2, 2)
 plt.plot([lrfn(e)for e in range(EPOCHS)])
 plt.xlabel('Epoch'); plt.ylabel('Log LR')
 plt.yscale('log');<load_from_csv>
svm_clf = SVC(C=10, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=None, degree=3, gamma=1, kernel='rbf',
max_iter=-1, probability=False, random_state=1, shrinking=True,
tol=0.001, verbose=False)
 svm_ovr = OneVsRestClassifier(svm_clf )
<merge>
def get_training_info(model,full_X_train, full_y_train):

 X_train, X_test, y_train, y_test = train_test_split_function(full_X_train,
full_y_train,
test_size_percent = 0.20)
 print("
--- Fitting Model ---")
 model.fit(X_train, y_train)
 print("
--- Scoring Model ---")
 model_training_score = model.score(X_train,y_train)
 model_testing_score = model.score(X_test,y_test)
 return model_training_score,model_testing_score
xxx = df_oof.groupby('image_name' ).mean().reset_index().merge(df_base_train, on='image_name')
 print("OOF AUC(TTA %d)= %.4f" %(TTA, metrics.roc_auc_score(xxx.target, xxx.pred)) )<save_to_csv>
def get_classification_report(X,y,test_size_percent, model):

 X_train, X_test, y_train, y_test = train_test_split_function(X,y, test_size_percent)
 model.fit(X_train, y_train)
 y_predict = model.predict(X_test)
 clf_report = classification_report(y_test,
y_predict,
target_names = lb_enc.inverse_transform(model.classes_ ).tolist())
 array = confusion_matrix(y_test, y_predict)
 row_sums = array.sum(axis=1,keepdims=True)
 norm_conf_mx = array/row_sums
error_matrix_df = pd.DataFrame(norm_conf_mx, index = [i for i in lb_enc.inverse_transform(model.classes_ ).tolist() ],
columns = [i for i in lb_enc.inverse_transform(model.classes_ ).tolist() ])
 return clf_report, array, norm_conf_mx, error_matrix_df
<install_modules>
training_score_svm, testing_score_svm = get_training_info(svm_ovr,prep_train_text_features,y_enc_labels )
!pip install iterative-stratification
!pip install efficientnet-pytorch<import_modules>
p_svm_clf = SVC(C=10, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=None, degree=3, gamma=1, kernel='rbf',
max_iter=-1, probability=True, random_state=1, shrinking=True,
tol=0.001, verbose=False )
import random
import numpy as np
import pandas as pd
import torch
import PIL.Image as pil
import matplotlib.pyplot as plt
from fastai.vision import *
from efficientnet_pytorch import EfficientNet
from sklearn.model_selection import StratifiedKFold
import os<set_options>
print(" SVM Classification Report ")
 print(svm_classification_report )
warnings.filterwarnings("ignore", category=UserWarning, module="torch.nn.functional" )<install_modules>
rf_clf = rf_classifier.best_estimator_
lgr_clf = logreg_classifier.best_estimator_
print(rf_clf,"
")
 print(lgr_clf,"
")
 print(svm_clf,"
" )
!pip install torch==1.4.0 torchvision==0.5.0<set_options>
model_names = [
"Random Forest Classifier",
"Logistic Regression",
"SVM",
"VotingClassifier"]
best_model_testing_score = [
testing_score_rf,
testing_score_lgr,
testing_score_svm,
"-"
]
best_model_training_score = [
training_score_rf,
training_score_lgr,
training_score_svm,
"-"
]
%reload_ext autoreload
%autoreload 2
%matplotlib inline<set_options>
def model_to_submission_file(model, full_X_train, full_y_train, full_X_test ,nameOfcsvfile):
print("
--- Fitting Model ---")
 starttime = time.monotonic()
model_clf = model.fit(full_X_train,full_y_train)
 print("
--- Predicting Cuisines ---")
 submission_pred = model_clf.predict(full_X_test)
 test_cuisine = lb_enc.inverse_transform(submission_pred)
 test_id = [recipe['id'] for recipe in test]
submission_df = pd.DataFrame({'id':test_id, 'cuisine':test_cuisine},columns = ['id','cuisine'])
 submission_df.to_csv('{}'.format(nameOfcsvfile), index= False)
 print("
--- Results have been saved ---")
 print("
That took ",(time.monotonic() -starttime)/60, " minutes" )
seed = 42
def random_seed(seed_value):
random.seed(seed_value)
 np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 os.environ['PYTHONHASHSEED'] = str(seed_value)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
random_seed(seed )<define_variables>
ovr_svm = OneVsRestClassifier(svm_clf, n_jobs = 4)
 model_to_submission_file(model=ovr_svm,
full_X_train=prep_train_text_features,
full_y_train=y_enc_labels,
full_X_test=prep_submission_text_features,
nameOfcsvfile="svm_model.csv")
path = '/kaggle/input/siim-isic-melanoma-classification'
path<define_variables>
model_testing_score = [0.75663, 0.78751, 0.81999, 0.81989]
pd.DataFrame({"Model":model_names,
"Training Score":best_model_training_score,
"Testing Score": best_model_testing_score,
"Submission Score": model_testing_score},
columns=["Model","Training Score", "Testing Score", "Submission Score"] )