Kaggle competition: What's Cooking? (Kernels Only)
k_fold(train_df, n_folds, seed )<filter>
x=pd.read_json('.. /input/train.json')
 y=pd.read_json('.. /input/test.json' )
def oversample(fld, df, os_size, num_fld=5):
train_df_fld = df.loc[df['fold'] != fld]
valid_df_fld = df.loc[df['fold'] == fld]
train_df_md = train_df_fld.loc[train_df_fld['target'] == 1]
if os_size == 'auto':
os_size = int(np.floor(train_df_fld.loc[train_df_fld['target'] == 0]['target'].value_counts() [0]/train_df_fld.loc[train_df_fld['target'] == 1]['target'].value_counts() [1]))
train_df_md = train_df_md.append([train_df_md] *(os_size - 1))
full_df_fld = pd.concat([train_df_fld, train_df_md, valid_df_fld] ).sample(frac=1.0, random_state=seed ).reset_index(drop=True)
 return full_df_fld
for x in range(n_folds):
fold_df = train_df.copy()
if oversampling_flag:
fold_df = oversample(x, train_df, oversampling_size)
 globals() ['src_%s' %x] =(ImageList.from_df(fold_df, img_path + '/512x512-dataset-melanoma', folder='512x512-dataset-melanoma', suffix='.jpg' ).split_from_df(col =(3 + x)))
<categorify>
z=x['cuisine']
def get_data(fold, size, bs, padding_mode='reflection'):
return(globals() ['src_%s' %fold].label_from_df(cols='target')
.add_test(test_data)
.transform(tfms, size=size, padding_mode=padding_mode)
.databunch(bs=bs, num_workers = num_wkrs ).normalize(imagenet_stats))<categorify>
x['separated_ing']=x['ingredients'].map(lambda x: ' '.join(x))
y['separated_ing']=y['ingredients'].map(lambda x: ' '.join(x))
def preds_smoothing(encodings , alpha):
K = encodings.shape[1]
y_ls =(1 - alpha)* encodings + alpha / K
return y_ls<compute_test_metric>
def purify(f):
f=f.lower()
f=re.sub('[%s]' % re.escape(string.punctuation),'',f)
 f=re.sub('\s+',' ',f)
 return f
x['cleared_ing']=x['separated_ing'].map(lambda g :purify(g))
y['cleared_ing']=y['separated_ing'].map(lambda g :purify(g))
def print_metrics(val_preds, val_labels):
targs, preds = LongTensor([]), Tensor([])
 val_preds = F.softmax(val_preds, dim=1)[:,-1]
preds = torch.cat(( preds, val_preds.cpu()))
targs = torch.cat(( targs, val_labels.cpu().long()))
print('AUCROC = ' + str(auc_roc_score(preds, targs ).item()))<set_options>
sb=SnowballStemmer('english')
 def stemmer(f):
lists=[sb.stem(c)for c in f.split(" ")]
return lists
l=WordNetLemmatizer()
def lemmar(f):
lists=[l.lemmatize(g)for g in f.split(" ")]
return lists
x['separated_ing_stemmed']=[stemmer(l)for l in x['cleared_ing']]
x['separated_ing_stemmed']=x['separated_ing_stemmed'].map(lambda x: ' '.join(x))
x['separated_ing_lemma']=[lemmar(l)for l in x['separated_ing_stemmed']]
x['separated_ing_lemma']=x['separated_ing_lemma'].map(lambda x: ' '.join(x))
y['separated_ing_stemmed']=[stemmer(l)for l in y['cleared_ing']]
y['separated_ing_stemmed']=y['separated_ing_stemmed'].map(lambda x: ' '.join(x))
y['separated_ing_lemma']=[lemmar(l)for l in y['separated_ing_stemmed']]
y['separated_ing_lemma']=y['separated_ing_lemma'].map(lambda x: ' '.join(x))
gc.collect()<feature_engineering>
x.columns
x=x.drop(['ingredients','separated_ing','cleared_ing','separated_ing_stemmed'],axis=1)
 y=y.drop(['ingredients','separated_ing','cleared_ing','separated_ing_stemmed'],axis=1 )
for model in arch:
if hasattr(model, '__name__'):
model_name = model.__name__
else:
model_name = "EfficientNet"
globals() [model_name + '___val_preds'] = []
globals() [model_name + '___val_labels'] = []
globals() [model_name + '___test_preds'] = []
print(f'/////////////////////////////////////////////////////')
 print(f'//////////////// MODEL: {model_name} ////////////////')
 print(f'/////////////////////////////////////////////////////
')
 for fld in range(n_folds):
print(f'
//////////////// FOLD {fld} ////////////////
')
 for sz in size:
print(f'-------- Size: {sz} --------
')
 print("Preparing data & applying settings...
 ")
 data = get_data(fld, sz, bs)
 gc.collect()
if sz == size[0]:
if model_name != "EfficientNet":
learn = cnn_learner(data, model, metrics=[AUROC() ], bn_final=True)
 else:
model._fc = nn.Sequential(nn.Linear(fc_size, lin_size, bias=True),
nn.ReLU() ,
nn.Dropout(p=0.5),
nn.Linear(lin_size, num_classes, bias = True))
learn = Learner(data, model, metrics=[AUROC() ])
 learn = learn.split([learn.model._conv_stem,learn.model._blocks,learn.model._conv_head])
 else:
learn.data = data
if custom_loss:
learn.loss_func = loss_func
if use_fp16:
learn = learn.to_fp16()
if model_name != "EfficientNet":
lr = lr1[size.index(sz)]
else:
lr = lr_eff_1[size.index(sz)]
print("Data is ready.Learning - Stage 1...")
 learn.fit_one_cycle(stage_1_epochs, slice(lr), wd=w_decay)
 if is_stage_2:
print("Stage 1 complete.Stage 2...")
 if model_name != "EfficientNet":
lr = lr2[size.index(sz)]
else:
lr = lr_eff_2[size.index(sz)]
learn.unfreeze()
learn.fit_one_cycle(stage_2_epochs, lr, wd=w_decay)
 print(f"Final learning is over for size {sz}
")
 val_preds, val_labels = learn.get_preds()
print_metrics(val_preds, val_labels)
 globals() [model_name + '___val_preds'].append(val_preds)
 globals() [model_name + '___val_labels'].append(val_labels)
 if use_tta == False:
print(f'
Saving test results for fold {fld}...')
 test_preds, _ = learn.get_preds(DatasetType.Test)
 globals() [model_name + '___test_preds'].append(test_preds[:, 1])
 else:
print(f'
Saving test TTA results for fold {fld}...')
 test_preds, _ = learn.TTA(ds_type=DatasetType.Test)
 globals() [model_name + '___test_preds'].append(test_preds[:, 1])
 print("Done!")
 gc.collect()
print("All folds are trained successfully
")
 print_metrics(torch.cat(globals() [model_name + '___val_preds']), torch.cat(globals() [model_name + '___val_labels']))
print("
Writing submission file...")
 test_df_output = pd.concat([test_df, pd.DataFrame(np.mean(np.stack(globals() [model_name + '___test_preds']), axis=0), columns=['target'])], axis=1)
 sample_df.iloc[:,1:] = test_df_output.iloc[:,5]
sample_df.to_csv(f'submission_v{submission_ver}.csv', index=False)
 print(f'File is ready to submit
')
 if submit_after_train:
print("Submitting to Kaggle
")
 !kaggle competitions submit -c siim-isic-melanoma-classification -f 'submission_v{submission_ver}.csv' -m 'Md: {model_name}, Fd: {n_folds}, Bs: {bs}, Sz: {size[0]}, Os: {oversampling_flag}, TTa: {use_tta}'
print('
')
<import_modules>
lists=list(ENGLISH_STOP_WORDS)+stopwords.words()
!pip install -q efficientnet 
<set_options>
from sklearn.feature_extraction.text import TfidfVectorizer as tfidf,CountVectorizer as cv
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 )<define_variables>
z=x['cuisine']
AUTO = tf.data.experimental.AUTOTUNE
GCS_PATH = KaggleDatasets().get_gcs_path('melanoma-384x384')
 GCS_PATH2 = KaggleDatasets().get_gcs_path('isic2019-384x384')
 EPOCHS = 40
BATCH_SIZE = 4 * strategy.num_replicas_in_sync
AUG_BATCH = BATCH_SIZE
IMAGE_SIZE = [384, 384]
SEED = 123
LR = 0.0003
TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train*.tfrec')
 TRAINING_FILENAMES_OLD_DATA = tf.io.gfile.glob(GCS_PATH2 + '/train%.2i*.tfrec'%(2*x)for x in range(15))
TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test*.tfrec')
 SUB = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv' )<define_variables>
tfidf1=tfidf(max_df=0.9,stop_words=lists,analyzer=u'word')
 train=tfidf1.fit_transform(x['separated_ing_lemma'])
 test=tfidf1.transform(y['separated_ing_lemma'] )
TRAINING_FILENAMES += TRAINING_FILENAMES_OLD_DATA<choose_model_class>
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV as gsc
from sklearn.ensemble import RandomForestClassifier
from xgboost.sklearn import XGBClassifier as xgb
from lightgbm import LGBMClassifier as lgb
from sklearn.linear_model import LogisticRegression as lr
cutout_aug = get_random_eraser(s_h=0.2 )<categorify>
svm={'C':[6]}
def random_erasing(img, probability = 0.5, sl = 0.02, sh = 0.2, r1 = 0.1):

 height = tf.shape(img)[0]
width = tf.shape(img)[1]
channel = tf.shape(img)[2]
area = tf.cast(width*height, tf.float32)
 erase_area_low_bound = tf.cast(tf.round(tf.sqrt(sl * area * r1)) , tf.int32)
 erase_area_up_bound = tf.cast(tf.round(tf.sqrt(( sh * area)/ r1)) , tf.int32)
 h_upper_bound = tf.minimum(erase_area_up_bound, height)
 w_upper_bound = tf.minimum(erase_area_up_bound, width)
 h = tf.random.uniform([], erase_area_low_bound, h_upper_bound, tf.int32)
 w = tf.random.uniform([], erase_area_low_bound, w_upper_bound, tf.int32)
 x1 = tf.random.uniform([], 0, height+1 - h, tf.int32)
 y1 = tf.random.uniform([], 0, width+1 - w, tf.int32)
 erase_area = tf.cast(tf.random.uniform([h, w, channel], 0, 255, tf.int32), tf.uint8)
 erasing_img = img[x1:x1+h, y1:y1+w, :].assign(erase_area)
 return tf.cond(tf.random.uniform([], 0, 1)> probability, lambda: img, lambda: erasing_img )<prepare_x_and_y>
p=le().fit(z )
def transform_cut(image, inv_mat, image_shape):
h, w, c = image_shape
cx, cy = w//2, h//2
new_xs = tf.repeat(tf.range(-cx, cx, 1), h)
 new_ys = tf.tile(tf.range(-cy, cy, 1), [w])
 new_zs = tf.ones([h*w], dtype=tf.int32)
 old_coords = tf.matmul(inv_mat, tf.cast(tf.stack([new_xs, new_ys, new_zs]), tf.float32))
old_coords_x, old_coords_y = tf.round(old_coords[0, :] + w//2), tf.round(old_coords[1, :] + h//2)
 clip_mask_x = tf.logical_or(old_coords_x<0, old_coords_x>w-1)
 clip_mask_y = tf.logical_or(old_coords_y<0, old_coords_y>h-1)
 clip_mask = tf.logical_or(clip_mask_x, clip_mask_y)
 old_coords_x = tf.boolean_mask(old_coords_x, tf.logical_not(clip_mask))
old_coords_y = tf.boolean_mask(old_coords_y, tf.logical_not(clip_mask))
new_coords_x = tf.boolean_mask(new_xs+cx, tf.logical_not(clip_mask))
new_coords_y = tf.boolean_mask(new_ys+cy, tf.logical_not(clip_mask))
old_coords = tf.cast(tf.stack([old_coords_y, old_coords_x]), tf.int32)
 new_coords = tf.cast(tf.stack([new_coords_y, new_coords_x]), tf.int64)
 rotated_image_values = tf.gather_nd(image, tf.transpose(old_coords))
rotated_image_channel = list()
for i in range(c):
vals = rotated_image_values[:,i]
sparse_channel = tf.SparseTensor(tf.transpose(new_coords), vals, [h, w])
 rotated_image_channel.append(tf.sparse.to_dense(sparse_channel, default_value=0, validate_indices=False))
return tf.transpose(tf.stack(rotated_image_channel), [1,2,0])
 def random_rotate(image, angle, image_shape):
def get_rotation_mat_inv(angle):
angle = math.pi * angle / 180
cos_val = tf.math.cos(angle)
 sin_val = tf.math.sin(angle)
 one = tf.constant([1], tf.float32)
 zero = tf.constant([0], tf.float32)
 rot_mat_inv = tf.concat([cos_val, sin_val, zero,
-sin_val, cos_val, zero,
zero, zero, one], axis=0)
 rot_mat_inv = tf.reshape(rot_mat_inv, [3,3])
 return rot_mat_inv
angle = float(angle)* tf.random.normal([1],dtype='float32')
 rot_mat_inv = get_rotation_mat_inv(angle)
 return transform_cut(image, rot_mat_inv, image_shape)
 def GridMask(image_height, image_width, d1, d2, rotate_angle=1, ratio=0.2):
h, w = image_height, image_width
hh = int(np.ceil(np.sqrt(h*h+w*w)))
 hh = hh+1 if hh%2==1 else hh
d = tf.random.uniform(shape=[], minval=d1, maxval=d2, dtype=tf.int32)
 l = tf.cast(tf.cast(d,tf.float32)*ratio+0.5, tf.int32)
 st_h = tf.random.uniform(shape=[], minval=0, maxval=d, dtype=tf.int32)
 st_w = tf.random.uniform(shape=[], minval=0, maxval=d, dtype=tf.int32)
 y_ranges = tf.range(-1 * d + st_h, -1 * d + st_h + l)
 x_ranges = tf.range(-1 * d + st_w, -1 * d + st_w + l)
 for i in range(0, hh//d+1):
s1 = i * d + st_h
s2 = i * d + st_w
y_ranges = tf.concat([y_ranges, tf.range(s1,s1+l)], axis=0)
 x_ranges = tf.concat([x_ranges, tf.range(s2,s2+l)], axis=0)
 x_clip_mask = tf.logical_or(x_ranges <0 , x_ranges > hh-1)
 y_clip_mask = tf.logical_or(y_ranges <0 , y_ranges > hh-1)
 clip_mask = tf.logical_or(x_clip_mask, y_clip_mask)
 x_ranges = tf.boolean_mask(x_ranges, tf.logical_not(clip_mask))
y_ranges = tf.boolean_mask(y_ranges, tf.logical_not(clip_mask))
hh_ranges = tf.tile(tf.range(0,hh), [tf.cast(tf.reduce_sum(tf.ones_like(x_ranges)) , tf.int32)])
 x_ranges = tf.repeat(x_ranges, hh)
 y_ranges = tf.repeat(y_ranges, hh)
 y_hh_indices = tf.transpose(tf.stack([y_ranges, hh_ranges]))
x_hh_indices = tf.transpose(tf.stack([hh_ranges, x_ranges]))
y_mask_sparse = tf.SparseTensor(tf.cast(y_hh_indices, tf.int64), tf.zeros_like(y_ranges), [hh, hh])
 y_mask = tf.sparse.to_dense(y_mask_sparse, 1, False)
 x_mask_sparse = tf.SparseTensor(tf.cast(x_hh_indices, tf.int64), tf.zeros_like(x_ranges), [hh, hh])
 x_mask = tf.sparse.to_dense(x_mask_sparse, 1, False)
 mask = tf.expand_dims(tf.clip_by_value(x_mask + y_mask, 0, 1), axis=-1)
 mask = tf.image.crop_to_bounding_box(mask,(hh-h)//2,(hh-w)//2, image_height, image_width)
 return mask
def apply_grid_mask(image, image_shape):
mask = GridMask(image_shape[0],
image_shape[1],
50,100,
0.2)
 if image_shape[-1] == 3:
mask = tf.concat([mask, mask, mask], axis=-1)
 return image * tf.cast(mask, tf.float32)
 def random_cuout_aug(image):
if tf.random.uniform(shape=[], minval=0.0, maxval=1.0)>=0.5:
image = apply_grid_mask(image,(*IMAGE_SIZE,3))
return tf.cast(image, tf.float32 )<normalization>
z=p.transform(z )
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = tf.reshape(tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3])
 zoom_matrix = tf.reshape(tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3])
 shift_matrix = tf.reshape(tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3])
 return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))
def transform(image, label):
DIM = IMAGE_SIZE[0]
XDIM = DIM%2
if 0.5 > tf.random.uniform([1], minval = 0, maxval = 1):
rot = 15.* tf.random.normal([1],dtype='float32')
 else:
rot = 180.* tf.random.normal([1],dtype='float32')
 shr = 5.* tf.random.normal([1],dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 w_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 h_shift = 16.* tf.random.normal([1],dtype='float32')
 w_shift = 16.* 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['inp1'],tf.transpose(idx3))
return {'inp1': tf.reshape(d,[DIM,DIM,3]), 'inp2': image['inp2']}, label
def seed_everything(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed)
 def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"target": tf.io.FixedLenFeature([], tf.int64),
"age_approx": tf.io.FixedLenFeature([], tf.int64),
"sex": tf.io.FixedLenFeature([], tf.int64),
"anatom_site_general_challenge": 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)
 data = {}
data['age_approx'] = tf.cast(example['age_approx'], tf.int32)
 data['sex'] = tf.cast(example['sex'], tf.int32)
 data['anatom_site_general_challenge'] = tf.cast(tf.one_hot(example['anatom_site_general_challenge'], 7), tf.int32)
 return image, label, data
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"image_name": tf.io.FixedLenFeature([], tf.string),
"age_approx": tf.io.FixedLenFeature([], tf.int64),
"sex": tf.io.FixedLenFeature([], tf.int64),
"anatom_site_general_challenge": tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 image_name = example['image_name']
data = {}
data['age_approx'] = tf.cast(example['age_approx'], tf.int32)
 data['sex'] = tf.cast(example['sex'], tf.int32)
 data['anatom_site_general_challenge'] = tf.cast(tf.one_hot(example['anatom_site_general_challenge'], 7), tf.int32)
 return image, image_name, data
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)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls = AUTO)
 return dataset
def setup_input1(image, label, data):
anatom = [tf.cast(data['anatom_site_general_challenge'][i], dtype = tf.float32)for i in range(7)]
tab_data = [tf.cast(data[tfeat], dtype = tf.float32)for tfeat in ['age_approx', 'sex']]
tabular = tf.stack(tab_data + anatom)
 return {'inp1': image, 'inp2': tabular}, label
def setup_input2(image, image_name, data):
anatom = [tf.cast(data['anatom_site_general_challenge'][i], dtype = tf.float32)for i in range(7)]
tab_data = [tf.cast(data[tfeat], dtype = tf.float32)for tfeat in ['age_approx', 'sex']]
tabular = tf.stack(tab_data + anatom)
 return {'inp1': image, 'inp2': tabular}, image_name
def setup_input3(image, image_name, target, data):
anatom = [tf.cast(data['anatom_site_general_challenge'][i], dtype = tf.float32)for i in range(7)]
tab_data = [tf.cast(data[tfeat], dtype = tf.float32)for tfeat in ['age_approx', 'sex']]
tabular = tf.stack(tab_data + anatom)
 return {'inp1': image, 'inp2': tabular}, image_name, target
def data_augment(data, label):
data['inp1'] = tf.image.random_flip_left_right(data['inp1'])
 data['inp1'] = tf.image.random_flip_up_down(data['inp1'])
 data['inp1'] = tf.image.random_hue(data['inp1'], 0.01)
 data['inp1'] = tf.image.random_saturation(data['inp1'], 0.7, 1.3)
 data['inp1'] = tf.image.random_contrast(data['inp1'], 0.8, 1.2)
 data['inp1'] = tf.image.random_brightness(data['inp1'], 0.1)
 return data, label
def get_training_dataset(filenames, labeled = True, ordered = False):
dataset = load_dataset(filenames, labeled = labeled, ordered = ordered)
 dataset = dataset.map(setup_input1, num_parallel_calls = AUTO)
 dataset = dataset.map(data_augment, num_parallel_calls = AUTO)
 dataset = dataset.map(transform, num_parallel_calls = AUTO)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_validation_dataset(filenames, labeled = True, ordered = True):
dataset = load_dataset(filenames, labeled = labeled, ordered = ordered)
 dataset = dataset.map(setup_input1, num_parallel_calls = AUTO)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(filenames, labeled = False, ordered = True):
dataset = load_dataset(filenames, labeled = labeled, ordered = ordered)
 dataset = dataset.map(setup_input2, num_parallel_calls = AUTO)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(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 read_tfrecord_full(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"image_name": tf.io.FixedLenFeature([], tf.string),
"target": tf.io.FixedLenFeature([], tf.int64),
"age_approx": tf.io.FixedLenFeature([], tf.int64),
"sex": tf.io.FixedLenFeature([], tf.int64),
"anatom_site_general_challenge": tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 image_name = example['image_name']
target = tf.cast(example['target'], tf.int32)
 data = {}
data['age_approx'] = tf.cast(example['age_approx'], tf.int32)
 data['sex'] = tf.cast(example['sex'], tf.int32)
 data['anatom_site_general_challenge'] = tf.cast(tf.one_hot(example['anatom_site_general_challenge'], 7), tf.int32)
 return image, image_name, target, data
def load_dataset_full(filenames):
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads = AUTO)
 dataset = dataset.map(read_tfrecord_full, num_parallel_calls = AUTO)
 return dataset
def get_data_full(filenames):
dataset = load_dataset_full(filenames)
 dataset = dataset.map(setup_input3, num_parallel_calls = AUTO)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
NUM_TRAINING_IMAGES = int(count_data_items(TRAINING_FILENAMES)* 0.8)
 NUM_VALIDATION_IMAGES = int(count_data_items(TRAINING_FILENAMES)* 0.2)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
print('Dataset: {} training images, {} validation images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))<compute_train_metric>
xtrain,xtest,ztrain,ztest=tts(train,z,train_size=0.7 )
def binary_focal_loss(gamma=2., alpha=.25):

 def binary_focal_loss_fixed(y_true, y_pred):

 pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
epsilon = K.epsilon()
pt_1 = K.clip(pt_1, epsilon, 1.- epsilon)
 pt_0 = K.clip(pt_0, epsilon, 1.- epsilon)
 return -K.sum(alpha * K.pow(1.- pt_1, gamma)* K.log(pt_1)) \
-K.sum(( 1 - alpha)* K.pow(pt_0, gamma)* K.log(1.- pt_0))
return binary_focal_loss_fixed
def get_model() :
with strategy.scope() :
inp1 = tf.keras.layers.Input(shape =(*IMAGE_SIZE, 3), name = 'inp1')
 inp2 = tf.keras.layers.Input(shape =(9), name = 'inp2')
 efnetb4 = efn.EfficientNetB2(weights = 'imagenet', include_top = False)
 x = efnetb4(inp1)
 x = tf.keras.layers.GlobalAveragePooling2D()(x)
 x1 = tf.keras.layers.Dense(4 )(inp2)
 x1 = tf.keras.layers.Activation('relu' )(x1)
 x1 = tf.keras.layers.BatchNormalization()(x1)
 concat = tf.keras.layers.concatenate([x, x1])
 output = tf.keras.layers.Dense(1, activation = 'sigmoid' )(concat)
 model = tf.keras.models.Model(inputs = [inp1, inp2], outputs = [output])
 opt = tf.keras.optimizers.Adam(learning_rate = LR)
 opt = tfa.optimizers.RectifiedAdam(lr=LR)
 model.compile(
 optimizer = opt,
loss = [tfa.losses.SigmoidFocalCrossEntropy(gamma = 2.0, alpha = 0.80)],
metrics = [tf.keras.metrics.BinaryAccuracy() , tf.keras.metrics.AUC() ] 
)
 return model
def train_and_predict(SUB, folds = 3):
models = []
oof_image_name = []
oof_target = []
oof_prediction = []
seed_everything(SEED)
 kfold = KFold(folds, shuffle = True, random_state = SEED)
 for fold,(trn_ind, val_ind)in enumerate(kfold.split(TRAINING_FILENAMES)) :
print('
')
 print('-'*50)
 print(f'Training fold {fold + 1}')
 train_dataset = get_training_dataset([TRAINING_FILENAMES[x] for x in trn_ind], labeled = True, ordered = False)
 val_dataset = get_validation_dataset([TRAINING_FILENAMES[x] for x in val_ind], labeled = True, ordered = True)
 K.clear_session()
model = get_model()
early_stopping = tf.keras.callbacks.EarlyStopping(monitor = 'val_auc', mode = 'max', patience = 5,
verbose = 1, min_delta = 0.0001, restore_best_weights = True)
 cb_lr_schedule = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_auc', factor = 0.4, patience = 2, verbose = 1, min_delta = 0.0001, mode = 'max')
 history = model.fit(train_dataset,
steps_per_epoch = STEPS_PER_EPOCH,
epochs = EPOCHS,
callbacks = [early_stopping, cb_lr_schedule],
validation_data = val_dataset,
verbose = 2)
 models.append(model)
 number_of_files = count_data_items([TRAINING_FILENAMES[x] for x in val_ind])
 dataset = get_data_full([TRAINING_FILENAMES[x] for x in val_ind])
 image_name = dataset.map(lambda image, image_name, target: image_name ).unbatch()
image_name = next(iter(image_name.batch(number_of_files)) ).numpy().astype('U')
 target = dataset.map(lambda image, image_name, target: target ).unbatch()
target = next(iter(target.batch(number_of_files)) ).numpy()
image = dataset.map(lambda image, image_name, target: image)
 probabilities = model.predict(image)
 oof_image_name.extend(list(image_name))
oof_target.extend(list(target))
oof_prediction.extend(list(np.concatenate(probabilities)))
 print('
')
 print('-'*50)
 oof_df = pd.DataFrame({'image_name': oof_image_name, 'target': oof_target, 'predictions': oof_prediction})
 oof_df.to_csv('OOF_EfficientNetB2_384.csv', index = False)
 test_ds = get_test_dataset(TEST_FILENAMES, labeled = False, ordered = True)
 test_images_ds = test_ds.map(lambda image, image_name: image)
 print('Computing predictions...')
 probabilities = np.average([np.concatenate(models[i].predict(test_images_ds)) for i in range(folds)], axis = 0)
 print('Generating submission.csv file...')
 test_ids_ds = test_ds.map(lambda image, image_name: image_name ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 pred_df = pd.DataFrame({'image_name': test_ids, 'target': probabilities})
 SUB.drop('target', inplace = True, axis = 1)
 SUB = SUB.merge(pred_df, on = 'image_name')
 SUB.to_csv('sub_EfficientNetB2_384.csv', index = False)
 return oof_target, oof_prediction
oof_target, oof_prediction = train_and_predict(SUB )<compute_test_metric>
r1=lgb(n_estimators=500,max_depth=7,objective='multiclass',metric='multi_logloss',num_classes=20,bagging_fraction=0.6,feature_fraction=0.6 )
roc_auc = metrics.roc_auc_score(oof_target, oof_prediction)
 print('Our out of folds roc auc score is: ', roc_auc )<install_modules>
a=gsc(lr() ,svm )
!pip install efficientnet_pytorch torchtoolbox<set_options>
k={'n_neighbors':[5,7,9]}
k1=gsc(knn() ,k )
%matplotlib inline<set_options>
from sklearn.multiclass import OneVsRestClassifier as orc
from sklearn.ensemble import VotingClassifier as vc
warnings.simplefilter('ignore')
 def seed_everything(seed):
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 = True
seed_everything(47 )<set_options>
v=vc(estimators=[('lr',a),('k1',k1),('lg',r1)],voting='soft' )
device = torch.device("cuda" if torch.cuda.is_available() else "cpu" )<define_variables>
v.fit(xtrain,ztrain )
basepath = ".. /input/jpeg-melanoma-256x256/"
train_csv = basepath + "train.csv"
test_csv = basepath + "test.csv"
train_img_path = '.. /input/jpeg-melanoma-256x256/train/'
test_img_path = '.. /input/jpeg-melanoma-256x256/test/'
print(train_csv )<load_from_csv>
print(accuracy_score(ztest,v.predict(xtest)) )
train_df = pd.read_csv(train_csv)
 test_df = pd.read_csv(test_csv)
 display(train_df.head())
 print("Train shape: ", train_df.shape)
 print("Test shape: ",test_df.shape )<categorify>
z1=v.predict(test )
location = 'anatom_site_general_challenge'
concat = pd.concat([train_df[location], test_df[location]], ignore_index=True)
 dummies = pd.get_dummies(concat, dummy_na=True, dtype=np.uint8, prefix='site')
 train_df = pd.concat([train_df, dummies.iloc[:train_df.shape[0]].reset_index(drop=True)], axis=1)
 test_df = pd.concat([test_df, dummies.iloc[train_df.shape[0]:].reset_index(drop=True)], axis=1)
 train_df['sex'] = train_df['sex'].map({'male': 1, 'female': 0})
 train_df['sex'] = train_df['sex'].fillna(-1)
 test_df['sex'] = test_df['sex'].map({'male': 1, 'female': 0})
 test_df['sex'] = test_df['sex'].fillna(-1)
 train_df['age_approx'] /= train_df['age_approx'].max()
train_df['age_approx'] = train_df['age_approx'].fillna(0)
 test_df['age_approx'] /= test_df['age_approx'].max()
test_df['age_approx'] = test_df['age_approx'].fillna(0)
 print("Train shape: ", train_df.shape)
 print("Test shape: ", test_df.shape )<drop_column>
z=p.inverse_transform(z1 )
meta_features = ['sex', 'age_approx'] + [col for col in train_df.columns if 'site_' in col]
meta_features.remove('anatom_site_general_challenge')
 meta_features<train_model>
ff=pd.DataFrame(z,index=y['id'],columns=['cuisine'] )
class SIIMDataset(Dataset):
def __init__(self, df: pd.DataFrame, imgfolder: str, train: bool = True, transforms = None, meta_features = None):
self.df = df
self.imgfolder = imgfolder
self.transforms = transforms
self.train = train
self.meta_features = meta_features
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
im_path = os.path.join(self.imgfolder, self.df.iloc[idx]['image_name'] + '.jpg')
 img = cv2.imread(im_path)
 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 meta = np.array(self.df.iloc[idx][self.meta_features].values, dtype=np.float32)
 meta = torch.from_numpy(meta)
 if self.transforms:
img = self.transforms(img)
 if self.train:
target = torch.tensor(self.df.loc[idx, 'target'], dtype=torch.float)
 return(img, meta), target
else:
return(img, meta )<define_variables>
ff.index.name='id'
<randomize_order><EOS> 
ff.to_csv('aagya.csv' )