Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<prepare_x_and_y>
train = pd.read_csv('.. /input/siim-isic-melanoma-classification/train.csv')
 print('Examples WITH Melanoma')
 imgs = train.loc[train.target==1].sample(10 ).image_name.values
plt.figure(figsize=(20,8))
for i,k in enumerate(imgs):
img = cv2.imread('.. /input/jpeg-melanoma-128x128/train/%s.jpg'%k)
 img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
 plt.subplot(2,5,i+1); plt.axis('off')
 plt.imshow(img)
 plt.show()
print('Examples WITHOUT Melanoma')
 imgs = train.loc[train.target==0].sample(10 ).image_name.values
plt.figure(figsize=(20,8))
for i,k in enumerate(imgs):
img = cv2.imread('.. /input/jpeg-melanoma-128x128/train/%s.jpg'%k)
 img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
 plt.subplot(2,5,i+1); plt.axis('off')
 plt.imshow(img)
 plt.show()
y = surv_col.values
y<split>
!pip install -q efficientnet >> /dev/null
test_data = test_final.values
test_data<import_modules>
!pip install tensorflow-addons=='0.9.1'
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout<choose_model_class>
DEVICE = "TPU"
SEED = 47
FOLDS = 5
IMG_SIZES = [512,512,512,512,512]
INC2019 = [0,0,0,0,0]
INC2018 = [1,1,1,1,1]
PSEUDO = [1,1,1,1,1]
BATCH_SIZES = [32]*FOLDS
EPOCHS = [17]*FOLDS
EFF_NETS = [4,4,4,4,4]
focal_loss = 0
WGTS = [1/FOLDS]*FOLDS
TTA = 11
model = Sequential()
model.add(Dense(32, init = 'uniform', activation='relu', input_dim = 10))
model.add(Dense(64, init = 'uniform', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(64, init = 'uniform', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(12, init = 'uniform', activation='relu'))
model.add(Dense(1, init = 'uniform', activation='sigmoid'))
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'] )<train_model>
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}' )
model.fit(X,y, epochs=500, batch_size = 64, verbose = 1 )<predict_on_test>
MIXED_PRECISION = True
XLA_ACCELERATE = True
if MIXED_PRECISION:
if DEVICE == 'TPU': policy = tf.keras.mixed_precision.experimental.Policy('mixed_bfloat16')
 else: policy = tf.keras.mixed_precision.experimental.Policy('mixed_float16')
 mixed_precision.set_policy(policy)
 print('Mixed precision enabled')
 if XLA_ACCELERATE:
tf.config.optimizer.set_jit(True)
 print('Accelerated Linear Algebra enabled' )
pred = model.predict(test_data )<data_type_conversions>
GCS_PATH = [None]*FOLDS; GCS_PATH2 = [None]*FOLDS
GCS_PATH3 = [None] * FOLDS
for i in range(len(GCS_PATH3)) :
GCS_PATH3[i] = KaggleDatasets().get_gcs_path('melanoma-pseudo-labelling-tf-record-512x512')
 for i,k in enumerate(IMG_SIZES):
GCS_PATH[i] = KaggleDatasets().get_gcs_path('melanoma-%ix%i'%(k,k))
GCS_PATH2[i] = KaggleDatasets().get_gcs_path('isic2019-%ix%i'%(k,k))
files_train = np.sort(np.array(tf.io.gfile.glob(GCS_PATH[0] + '/train*.tfrec')))
 files_test = np.sort(np.array(tf.io.gfile.glob(GCS_PATH[0] + '/test*.tfrec')))
 files_pseulabeling = np.sort(np.array(tf.io.gfile.glob(GCS_PATH3[0] + '/train_pseudo_*.tfrec')) )
outputBin = np.zeros(0)
 for i in pred:
if i <=.5:
outputBin = np.append(outputBin, 0)
 else:
outputBin = np.append(outputBin, 1)
 output = np.array(outputBin ).astype(int )<define_variables>
cutmix_rate = 0.3
gridmask_rate = 0.3
img_size = 512
nb_classes = 1
DROP_FREQ = [0,0.75,0.75]
DROP_CT = [0,8,8]
DROP_SIZE = [0,0.2,0.2]
RATE = 0; CT = 0; SIZE = 0
d = {'PassengerId':pessengerId, 'Survived':output}<create_dataframe>
def cutmix_v2(data, label, PROBABILITY = cutmix_rate):
DIM = img_size
imgs = []; labs = []
for j in range(BATCH_SIZES[fold] * REPLICAS):
P = tf.cast(tf.random.uniform([], 0, 1)<= PROBABILITY, tf.int32)
 k = tf.cast(tf.random.uniform([], 0, BATCH_SIZES[fold] * REPLICAS), tf.int32)
 x = tf.cast(tf.random.uniform([], 0, DIM), tf.int32)
 y = tf.cast(tf.random.uniform([], 0, DIM), tf.int32)
 b = tf.random.uniform([], 0, 1)
 WIDTH = tf.cast(DIM * tf.math.sqrt(1-b),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 = data['image_data'][j,ya:yb,0:xa,:]
two = data['image_data'][k,ya:yb,xa:xb,:]
three = data['image_data'][j,ya:yb,xb:DIM,:]
middle = tf.concat([one,two,three],axis=1)
 img = tf.concat([data['image_data'][j,0:ya,:,:],middle,data['image_data'][j,yb:DIM,:,:]],axis=0)
 imgs.append(img)
 a = tf.cast(WIDTH*WIDTH/DIM/DIM,tf.float32)
 lab1 = label[j,]
lab2 = label[k,]
labs.append(( 1-a)*lab1 + a*lab2)
 image2 = tf.reshape(tf.stack(imgs),(BATCH_SIZES[fold] * REPLICAS ,DIM,DIM,3))
label2 = tf.reshape(tf.stack(labs),(BATCH_SIZES[fold] * REPLICAS, nb_classes))
data['image_data'] = image2
return data,label2
final_df = pd.DataFrame(data=d )<save_to_csv>
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))
cfg = dict(
 transform_prob = 1.0,
rot = 180.0,
shr = 2.0,
hzoom = 8.0,
wzoom = 8.0,
hshift = 8.0,
wshift = 8.0, 
)
 def transform_shear_rot(image,cfg):
DIM = img_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] )
final = final_df.to_csv('new_result.csv',index=False)
 final<train_model>
def transform_grid(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_grid(image, rot_mat_inv, image_shape)
 def GridMask(image_height, image_width, d1, d2, rotate_angle=1, ratio=0.5):
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 = random_rotate(mask, rotate_angle, [hh, hh, 1])
 mask = tf.image.crop_to_bounding_box(mask,(hh-h)//2,(hh-w)//2, image_height, image_width)
 return mask
def gridmask_v2(data, label_batch):
return apply_grid_mask_v2(data,(img_size,img_size, 3)) , label_batch
def apply_grid_mask_v2(data, image_shape, PROBABILITY = gridmask_rate):
AugParams = {
'd1' : 100,
'd2': 160,
'rotate' : 45,
'ratio' : 0.3
}
mask = GridMask(image_shape[0], image_shape[1], AugParams['d1'], AugParams['d2'], AugParams['rotate'], AugParams['ratio'])
 if image_shape[-1] == 3:
mask = tf.concat([mask, mask, mask], axis=-1)
 mask = tf.cast(mask,tf.float32)
 P = tf.cast(tf.random.uniform([], 0, 1)<= PROBABILITY, tf.int32)
 if P==1:
data['image_data'] = data['image_data'] * mask
return data
else:
return data
rf = RandomForestClassifier(n_estimators=350, max_depth=15, random_state=42)
 print("train accuracy: {} ".format(rf.fit(X, y ).score(X, y)))
<predict_on_test>
def dropout(image, DIM=img_size, 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
rf_pred = rf.predict(test_data )<define_variables>
ROT_ = 180.0
SHR_ = 2.0
HZOOM_ = 8.0
WZOOM_ = 8.0
HSHIFT_ = 8.0
WSHIFT_ = 8.0
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, DIM=img_size):
XDIM = DIM%2
rot = ROT_ * tf.random.normal([1], dtype='float32')
 shr = SHR_ * tf.random.normal([1], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ HZOOM_
w_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ WZOOM_
h_shift = HSHIFT_ * tf.random.normal([1], dtype='float32')
 w_shift = 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] )
r = {'PassengerId':pessengerId, 'Survived':rf_pred}<create_dataframe>
def read_labeled_tfrecord(example):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
'patient_id' : tf.io.FixedLenFeature([], tf.int64),
'sex' : tf.io.FixedLenFeature([], tf.int64),
'age_approx' : tf.io.FixedLenFeature([], tf.int64),
'anatom_site_general_challenge': tf.io.FixedLenFeature([], tf.int64),
'target' : tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, tfrec_format)
 data = {}
image = prepare_image(example['image'])
 data['patient_id'] = tf.cast(example['patient_id'],tf.float32)
 data['sex'] = tf.cast(example['patient_id'],tf.float32)
 data['age_approx'] = tf.cast(example['age_approx'],tf.float32)
 data['anatom_site_general_challenge'] = tf.cast(example['anatom_site_general_challenge'],tf.float32)
 label = tf.cast(example['target'], tf.float32)
 return image, label , data
def read_unlabeled_tfrecord(example,augment ,return_image_name):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
"patient_id" : tf.io.FixedLenFeature([],tf.int64),
"sex": tf.io.FixedLenFeature([],tf.int64),
"age_approx": tf.io.FixedLenFeature([],tf.int64),
"anatom_site_general_challenge": tf.io.FixedLenFeature([],tf.int64),
}
example = tf.io.parse_single_example(example, tfrec_format)
 data = {}
image = prepare_image(example['image'],augment = augment)
 img_name = example['image_name'] if return_image_name else 0
data['patient_id'] = tf.cast(example['patient_id'],tf.float32)
 data['sex'] = tf.cast(example['patient_id'],tf.float32)
 data['age_approx'] = tf.cast(example['age_approx'],tf.float32)
 data['anatom_site_general_challenge'] = tf.cast(example['anatom_site_general_challenge'],tf.float32)
 return image, img_name , data
def prepare_image(img, augment=True, dim=img_size):
img = tf.image.decode_jpeg(img, channels=3)
 img = tf.cast(img, tf.float32)/ 255.0
if augment:
img = transform(img,DIM=dim)
 if(RATE!=0)&(CT!=0)&(SIZE!=0):
img = dropout(img, DIM=dim, PROBABILITY=RATE, CT=CT, SZ=SIZE)
 img = tf.image.random_flip_left_right(img)
 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)
 img = tf.reshape(img, [dim,dim, 3])
 img = tf.image.resize(img, [img_size,img_size])
 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 )
final_rf = pd.DataFrame(data=r )<save_to_csv>
def train_setup(image,label,data):
tab_data=[tf.cast(data[tfeat], dtype=tf.float32)for tfeat in ['sex','age_approx','patient_id','anatom_site_general_challenge']]
tabular=tf.stack(tab_data)
 return {'image_data' : image , 'meta_data' : tabular} , label
def test_setup(image,image_name,data):
tab_data=[tf.cast(data[tfeat], dtype=tf.float32)for tfeat in ['sex','age_approx','patient_id','anatom_site_general_challenge']]
tabular=tf.stack(tab_data)
 return {'image_data' : image , 'meta_data' : tabular } , image_name
final_rf = final_df.to_csv('random_forest_result.csv',index=False)
 final_rf<import_modules>
def get_dataset(files, augment = False, shuffle = False, repeat = False,
labeled=True, return_image_names=True,val = False, batch_size=32, dim=img_size):
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)
 ds = ds.map(train_setup , num_parallel_calls = AUTO)
 ds = ds.batch(batch_size * REPLICAS)
 ds = ds.prefetch(AUTO)
 if val == False :
if cutmix_rate :
ds = ds.map(cutmix_v2,num_parallel_calls = AUTO)
 if gridmask_rate:
ds = ds.map(gridmask_v2, num_parallel_calls=AUTO)
 else:
ds = ds.map(lambda example: read_unlabeled_tfrecord(example,augment, return_image_names),
num_parallel_calls=AUTO)
 ds = ds.map(test_setup , num_parallel_calls = AUTO)
 ds = ds.batch(batch_size * REPLICAS)
 ds = ds.prefetch(AUTO)
 return ds
print(os.listdir(".. /input"))
sns.set(style='white', context='notebook', palette='deep')
<load_from_csv>
EFNS = [efn.EfficientNetB0, efn.EfficientNetB1, efn.EfficientNetB2, efn.EfficientNetB3,
efn.EfficientNetB4, efn.EfficientNetB5, efn.EfficientNetB6]
def build_model(dim=img_size, ef=0):
inp = tf.keras.layers.Input(shape=(dim,dim,3),name = 'image_data')
 inp2 = tf.keras.layers.Input(shape=(4),name = 'meta_data')
 base = EFNS[ef](input_shape=(dim,dim,3),weights='noisy-student',include_top=False , drop_connect_rate = 0.4)
 x = base(inp)
 x = tf.keras.layers.GlobalAveragePooling2D()(x)
 x2 = tf.keras.layers.Dense(128, activation='relu' )(inp2)
 concat = tf.keras.layers.concatenate([x,x2])
 concat = tf.keras.layers.Dense(512,activation = 'relu' )(concat)
 concat = tf.keras.layers.Dropout(0.4 )(concat)
 concat = tf.keras.layers.Dense(1,activation='sigmoid',dtype = 'float32' )(concat)
 model = tf.keras.Model(inputs=[inp,inp2],outputs=concat)
 opt = tf.keras.optimizers.Adam(learning_rate=0.001)
 if focal_loss :
loss = tfa.losses.SigmoidFocalCrossEntropy(reduction=tf.keras.losses.Reduction.AUTO)
 else :
loss = tf.keras.losses.BinaryCrossentropy(label_smoothing=0.05)
 model.compile(optimizer=opt,loss=loss,metrics=['AUC'])
 return model
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
def get_lr_callback(batch_size=8):
lr_start = 0.000005
lr_max = 0.00000125 * REPLICAS * batch_size
lr_min = 0.000001
lr_ramp_ep = 5
lr_sus_ep = 0
lr_decay = 0.8
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
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=False)
 return lr_callback
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<train_model>
VERBOSE = 0
DISPLAY_PLOT = True
skf = KFold(n_splits=FOLDS,shuffle=True,random_state=SEED)
 oof_pred = []; oof_tar = []; oof_val = []; oof_names = []; oof_folds = []
preds = np.zeros(( count_data_items(files_test),1))
for fold,(idxT,idxV)in enumerate(skf.split(np.arange(15))):
if DEVICE=='TPU':
if tpu: tf.tpu.experimental.initialize_tpu_system(tpu)
 print('
print(' 
(IMG_SIZES[fold],EFF_NETS[fold],BATCH_SIZES[fold]*REPLICAS))
files_train = tf.io.gfile.glob([GCS_PATH[fold] + '/train%.2i*.tfrec'%x for x in idxT])
 if INC2019[fold]:
files_train += tf.io.gfile.glob([GCS_PATH2[fold] + '/train%.2i*.tfrec'%x for x in idxT*2+1])
 print('
if INC2018[fold]:
files_train += tf.io.gfile.glob([GCS_PATH2[fold] + '/train%.2i*.tfrec'%x for x in idxT*2])
 print('
if PSEUDO[fold] :
files_train += tf.io.gfile.glob(GCS_PATH3[fold]+'/train_pseudo01-323.tfrec')
 files_train += tf.io.gfile.glob(GCS_PATH3[fold]+'/train_pseudo00-1000.tfrec')
 print('
np.random.shuffle(files_train); print('
files_valid = tf.io.gfile.glob([GCS_PATH[fold] + '/train%.2i*.tfrec'%x for x in idxV])
 files_test = np.sort(np.array(tf.io.gfile.glob(GCS_PATH[fold] + '/test*.tfrec')))
 K.clear_session()
with strategy.scope() :
model = build_model(dim=img_size,ef=EFF_NETS[fold])
 sv = tf.keras.callbacks.ModelCheckpoint(
 'fold-%i.h5'%fold, monitor='val_loss', verbose=0, save_best_only=True,
save_weights_only=True, mode='min', save_freq='epoch')
 print('Training...')
 history = model.fit(
 get_dataset(files_train, augment=True, shuffle=True, repeat=True,
dim=IMG_SIZES[fold], batch_size = BATCH_SIZES[fold]),
epochs=EPOCHS[fold], callbacks = [sv,get_lr_callback(BATCH_SIZES[fold])],
steps_per_epoch=count_data_items(files_train)/BATCH_SIZES[fold]//REPLICAS,
validation_data=get_dataset(files_valid,augment=False,shuffle=False,
repeat=False,dim=IMG_SIZES[fold],val=True),
verbose=VERBOSE 
)
 print('Loading best model...')
 model.load_weights('fold-%i.h5'%fold)
 print('Predicting OOF with TTA...')
 ds_valid = get_dataset(files_valid,labeled=False,return_image_names=False,augment=True,
repeat=True,shuffle=False,dim=IMG_SIZES[fold],batch_size=BATCH_SIZES[fold]*4)
 ct_valid = count_data_items(files_valid); STEPS = TTA * ct_valid/BATCH_SIZES[fold]/4/REPLICAS
pred = model.predict(ds_valid,steps=STEPS,verbose=VERBOSE)[:TTA*ct_valid,]
oof_pred.append(np.mean(pred.reshape(( ct_valid,TTA),order='F'),axis=1))
ds_valid = get_dataset(files_valid, augment=False, repeat=False, dim=IMG_SIZES[fold],
labeled=True, return_image_names=True,val=True)
 oof_tar.append(np.array([target.numpy() for img, target in iter(ds_valid.unbatch())]))
oof_folds.append(np.ones_like(oof_tar[-1],dtype='int8')*fold)
 ds = get_dataset(files_valid, augment=False, repeat=False, dim=IMG_SIZES[fold],
labeled=False, return_image_names=True)
 oof_names.append(np.array([img_name.numpy().decode("utf-8")for img, img_name in iter(ds.unbatch())]))
print('Predicting Test with TTA...')
 ds_test = get_dataset(files_test,labeled=False,return_image_names=False,augment=True,
repeat=True,shuffle=False,dim=IMG_SIZES[fold],batch_size=BATCH_SIZES[fold]*4)
 ct_test = count_data_items(files_test); STEPS = TTA * ct_test/BATCH_SIZES[fold]/4/REPLICAS
pred = model.predict(ds_test,steps=STEPS,verbose=VERBOSE)[:TTA*ct_test,]
preds[:,0] += np.mean(pred.reshape(( ct_test,TTA),order='F'),axis=1)* WGTS[fold]
auc = roc_auc_score(oof_tar[-1],oof_pred[-1])
 oof_val.append(np.max(history.history['val_auc']))
print('
if DISPLAY_PLOT:
plt.figure(figsize=(15,5))
plt.plot(np.arange(EPOCHS[fold]),history.history['auc'],'-o',label='Train AUC',color='
plt.plot(np.arange(EPOCHS[fold]),history.history['val_auc'],'-o',label='Val AUC',color='
x = np.argmax(history.history['val_auc']); y = np.max(history.history['val_auc'])
 xdist = plt.xlim() [1] - plt.xlim() [0]; ydist = plt.ylim() [1] - plt.ylim() [0]
plt.scatter(x,y,s=200,color='
%.2f'%y,size=14)
 plt.ylabel('AUC',size=14); plt.xlabel('Epoch',size=14)
 plt.legend(loc=2)
 plt2 = plt.gca().twinx()
plt2.plot(np.arange(EPOCHS[fold]),history.history['loss'],'-o',label='Train Loss',color='
plt2.plot(np.arange(EPOCHS[fold]),history.history['val_loss'],'-o',label='Val Loss',color='
x = np.argmin(history.history['val_loss']); y = np.min(history.history['val_loss'])
 ydist = plt.ylim() [1] - plt.ylim() [0]
plt.scatter(x,y,s=200,color='
plt.ylabel('Loss',size=14)
 plt.title('FOLD %i - Image Size %i, EfficientNet B%i, inc2019=%i, inc2018=%i'% 
(fold+1,IMG_SIZES[fold],EFF_NETS[fold],INC2019[fold],INC2018[fold]),size=18)
 plt.legend(loc=3)
 plt.show()
X_train.head()<count_missing_values>
oof = np.concatenate(oof_pred); true = np.concatenate(oof_tar);
names = np.concatenate(oof_names); folds = np.concatenate(oof_folds)
 auc = roc_auc_score(true,oof)
 print('Overall OOF AUC with TTA = %.3f'%auc)
 df_oof = pd.DataFrame(dict(
 image_name = names, target=true, pred = oof, fold=folds))
df_oof.to_csv('oof.csv',index=False)
 df_oof.head()
X_train.isnull().any().sum()<count_missing_values>
ds = get_dataset(files_test, augment=False, repeat=False, dim=IMG_SIZES[fold],
labeled=False, return_image_names=True)
 image_names = np.array([img_name.numpy().decode("utf-8")
 for img, img_name in iter(ds.unbatch())] )
test.isnull().any().sum()<feature_engineering>
submission = pd.DataFrame(dict(image_name=image_names, target=preds[:,0]))
submission = submission.sort_values('image_name')
 submission.to_csv('stratify_e4_128meta_512.4concat_18epochs_512img_with_pseudo_labeling_noisy_0.3gridmask_cutmix0.4drop_connect.csv', index=False)
 submission.head()
X_train = X_train / 255.0
test = test / 255.0<prepare_x_and_y>
top = -100
bottom = 2000
model_1 = pd.read_csv('.. /input/data-for-pseudolabelling-09576-09526/averaging_bestsub_withmeta_data_0.75_0.25giba.csv')
 model_2 = pd.read_csv('.. /input/data-for-pseudolabelling-09576-09526/averaging_bestsub_withmeta_data_0.7_0.3giba.csv')
 test = pd.read_csv('.. /input/siim-isic-melanoma-classification/test.csv')
 model_1.head(5 )
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )<categorify>
model1 = model_1.sort_values(by = ['target'])[top:].image_name
model1 = model1.values.tolist()
model2 = model_2.sort_values(by = ['target'])[top:].image_name
model2 = model2.values.tolist()
model3 = model_3.sort_values(by = ['target'])[top:].image_name
model3 = model3.values.tolist()
model4 = model_4.sort_values(by = ['target'])[top:].image_name
model4 = model4.values.tolist()
model5 = model_5.sort_values(by = ['target'])[top:].image_name
model5 = model5.values.tolist()
y_train = to_categorical(y_train, num_classes=10 )<define_variables>
label_1_imagename = set(model1 ).intersection(model2)
 print('Number of new positive examples: %i' %len(label_1_imagename))
random_seed = 2<split>
label_1_data = test.loc[test['image_name'].isin(label_1_imagename)].reset_index(drop = True)
 label_1_data['target'] = 1
label_1_data.head(10 )
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=random_seed )<choose_model_class>
model1 = model_1.sort_values(by = ['target'])[:bottom].image_name
model1 = model1.values.tolist()
model2 = model_2.sort_values(by = ['target'])[:bottom].image_name
model2 = model2.values.tolist()
model3 = model_3.sort_values(by = ['target'])[:bottom].image_name
model3 = model3.values.tolist()
model4 = model_4.sort_values(by = ['target'])[:bottom].image_name
model4 = model4.values.tolist()
model5 = model_5.sort_values(by = ['target'])[:bottom].image_name
model5 = model5.values.tolist()
model = Sequential()
model.add(Conv2D(filters=40, kernel_size=(5,5), padding='Same', activation='relu', input_shape=(28, 28, 1)))
 model.add(Conv2D(filters=40, kernel_size=(5,5), padding='Same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Conv2D(filters=60, kernel_size=(3,3), padding='Same', activation='relu'))
model.add(Conv2D(filters=60, kernel_size=(3,3), padding='Same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Flatten())
 model.add(Dense(300, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))<choose_model_class>
label_0_data = test.loc[test['image_name'].isin(label_0_imagename)].reset_index(drop = True)
 label_0_data['target'] = 0
label_0_data.head(10 )
optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False )<choose_model_class>
label_1_data.dropna(inplace = True)
 label_0_data.dropna(inplace = True )
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'] )<choose_model_class>
data = pd.concat([label_0_data, label_1_data])
 data.shape
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=5, factor=0.5, verbose=1, min_lr=0.00001 )<define_variables>
PATH = '.. /input/jpeg-melanoma-512x512/test/'
IMGS = data['image_name'].values
IMGS = IMGS + '.jpg'
print('Number of images: %i' %len(IMGS))
epochs = 1
batch_size = 84<define_variables>
str_col = ['patient_id','sex','anatom_site_general_challenge']
for col in str_col:
data[col], mp = data[col].factorize()
print(mp)
 data['age_approx'] = data['age_approx'].astype('int' )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<train_model>
def _bytes_feature(value):

 if isinstance(value, type(tf.constant(0))):
value = value.numpy()
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _float_feature(value):

 return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def _int64_feature(value):

 return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size),
epochs=epochs, verbose=2, callbacks=[learning_rate_reduction],
validation_data=(X_val, y_val), steps_per_epoch=X_train.shape[0] // batch_size )<predict_on_test>
def serialize_example(feature0, feature1, feature2, feature3, feature4, feature5, feature6):
feature = {
'image': _bytes_feature(feature0),
'image_name': _bytes_feature(feature1),
'patient_id': _int64_feature(feature2),
'sex': _int64_feature(feature3),
'age_approx': _int64_feature(feature4),
'anatom_site_general_challenge': _int64_feature(feature5),
'target': _int64_feature(feature6)
 }
example_proto = tf.train.Example(features = tf.train.Features(feature = feature))
return example_proto.SerializeToString()
<save_to_csv><EOS> 
SIZE = 1000
CT = len(IMGS)//SIZE + int(len(IMGS)%SIZE != 0)
 for j in range(CT):
print() ; print('Writing TFRecord %i of %i...' %(j, CT))
CT2 = min(SIZE, len(IMGS)- j*SIZE)
 with tf.io.TFRecordWriter('train_pseudo_512%.2i-%i.tfrec' %(j, CT2)) as writer:
for k in range(CT2):
img = cv2.imread(PATH + IMGS[SIZE*j + k])
 img = cv2.imencode('.jpg', img,(cv2.IMWRITE_JPEG_QUALITY, 95)) [1].tostring()
name = IMGS[SIZE*j + k].split('.')[0]
row = data.loc[data.image_name == name]
example = serialize_example(
 img,
str.encode(name),
row.patient_id.values[0],
row.sex.values[0],
row.age_approx.values[0],
row.anatom_site_general_challenge.values[0],
row.target.values[0])
 writer.write(example)
 if k%100 == 0: print(k, ', ', end = '' )