{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport tensorflow as tf \n\n!pip install -U efficientnet\n\nimport efficientnet.tfkeras as eff\nfrom kaggle_datasets import KaggleDatasets","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# detect and init the TPU\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver()\ntf.config.experimental_connect_to_cluster(tpu)\ntf.tpu.experimental.initialize_tpu_system(tpu)\n\n# instantiate a distribution strategy\ntpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE\nignore_order = tf.data.Options()\nignore_order.experimental_deterministic = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gcs_path=KaggleDatasets().get_gcs_path('melanoma-512x512')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filepaths=tf.io.gfile.glob(gcs_path+'/train*.tfrec')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filepaths_train=filepaths[:-1]\nfilepaths_valid=filepaths[-1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BATCH_SIZE=8*tpu_strategy.num_replicas_in_sync\nTRAIN_STEPS=int(30952/(BATCH_SIZE))+1\nVALID_STEPS=int(2174/(BATCH_SIZE))+1","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# image augmentations","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def make_augmentations(image):\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_saturation(image, 0.7, 1.3)\n    image = tf.image.random_contrast(image, 0.8, 1.2)\n    image = tf.image.random_brightness(image, 0.1)\n    return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_tfrec_train(example):\n    feature_description={'image_name':tf.io.FixedLenFeature([],tf.string),\n                     'image':tf.io.FixedLenFeature([],tf.string),\n                     'target':tf.io.FixedLenFeature([],tf.int64)}\n    example=tf.io.parse_single_example(example,feature_description)\n    return example['image'],example['target']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def make_train_dataset(filepaths,batch_size,train=True):\n    ds=tf.data.TFRecordDataset(filepaths,num_parallel_reads=AUTO)\n    ds=ds.with_options(ignore_order)\n    ds=ds.map(read_tfrec_train,num_parallel_calls=AUTO)\n    ds=ds.repeat()\n    ds=ds.shuffle(1024)\n    ds=ds.map(lambda img,label : (tf.io.decode_image(img,channels=3),label),num_parallel_calls=AUTO)\n    ds=ds.map(lambda img,label:(tf.cast(img,dtype=tf.float32),label),num_parallel_calls=AUTO)\n    if train:\n        ds=ds.map(lambda img,label :(make_augmentations(img),label),num_parallel_calls=AUTO)\n    ds=ds.map(lambda img,label:(tf.reshape(img,[512,512,3]),label),num_parallel_calls=AUTO)\n    ds=ds.batch(batch_size)\n    ds=ds.prefetch(AUTO)\n    return ds\ntrain_ds=make_train_dataset(filepaths_train,batch_size=BATCH_SIZE,train=True)\nvalid_ds=make_train_dataset(filepaths_valid,batch_size=BATCH_SIZE,train=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_ds,valid_ds","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# with ensemble without metadata","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"class early_stopping(tf.keras.callbacks.Callback):\n    def __init__(self,patience=1):\n        self.auc_op=-1.0\n        self.patience=patience\n        self.count=0\n    def on_epoch_end(self,epoch,logs={}):\n                    \n        if logs['val_auc']>=self.auc_op:\n            self.weights_op=self.model.get_weights()\n            self.auc_op=logs['val_auc']\n            self.count=0\n            print('\\nVALIDATION AUC INCREASED')\n        else:\n            self.count=self.count+1\n            print('\\nVALIDATION AUC DID NOT INCREASE ES COUNT :{}/{}'.format(self.count,self.patience))\n            if(self.count==self.patience):\n                print('EARLY STOPPING ACTIVATED')\n                self.model.stop_training=True\n                print('RESTORING WEIGHTS OF EPOCH {} . BEST VALID AUC = {}'.format(epoch-self.patience+1,self.auc_op))\n                self.model.set_weights(self.weights_op)\n                \ndef schedule(epoch):\n    return 0.0001*(2.71828**(-0.5*epoch))\n\nlr_scheduler=tf.keras.callbacks.LearningRateScheduler(schedule)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train=pd.read_csv('../input/melanoma-512x512/train.csv')\nfrom sklearn.utils.class_weight import compute_class_weight\ncw=compute_class_weight('balanced',np.unique(train['target']),train['target'])\ncw_dict={0:cw[0],1:cw[1]}\ncw_dict","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with tpu_strategy.scope():\n    input_layer=tf.keras.layers.Input(shape=(512,512,3))\n    common_input=tf.keras.layers.Lambda(lambda x:x)(input_layer)\n\n\n    model_b0=eff.EfficientNetB1(weights='imagenet',input_shape=(512,512,3),include_top=False)(common_input)\n    model_b1=eff.EfficientNetB2(weights='imagenet',input_shape=(512,512,3),include_top=False)(common_input)\n    model_b2=eff.EfficientNetB3(weights='imagenet',input_shape=(512,512,3),include_top=False)(common_input)\n\n\n    gapb0=tf.keras.layers.GlobalAveragePooling2D()(model_b0)\n    predsb0=tf.keras.layers.Dense(1)(gapb0)\n\n\n    gapb1=tf.keras.layers.GlobalAveragePooling2D()(model_b1)\n    predsb1=tf.keras.layers.Dense(1)(gapb1)\n\n\n    gapb2=tf.keras.layers.GlobalAveragePooling2D()(model_b2)\n    predsb2=tf.keras.layers.Dense(1)(gapb2)\n\n\n    output_concat=tf.keras.layers.Concatenate()([predsb0,predsb1,predsb2])\n    real_out=tf.keras.layers.Dense(1,activation='sigmoid')(output_concat)\n    ensem_model=tf.keras.models.Model(inputs=[input_layer],outputs=[real_out])\n    \n    \n    roc_score=tf.keras.metrics.AUC(curve='ROC')\n    \n\n    ensem_model.compile(optimizer='adam',loss='binary_crossentropy',metrics=[roc_score])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history=ensem_model.fit(train_ds,epochs=100,steps_per_epoch=TRAIN_STEPS,class_weight=cw_dict,validation_data=valid_ds,validation_steps=VALID_STEPS,callbacks=[early_stopping(patience=6),lr_scheduler])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# now for the predictions","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"filepaths_test=tf.io.gfile.glob(gcs_path+'/test*.tfrec')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_tfrec_test(example,read_filenames):\n    feature_description={'image_name':tf.io.FixedLenFeature([],tf.string),\n                     'image':tf.io.FixedLenFeature([],tf.string)}\n    example=tf.io.parse_single_example(example,feature_description)\n    if read_filenames:\n        return example['image'],example['image_name']\n    else:\n        return example['image']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def make_test_dataset(filepaths,batch_size,read_filenames=False):\n    ds=tf.data.TFRecordDataset(filepaths,num_parallel_reads=AUTO)\n    ds=ds.map(lambda example: read_tfrec_test(example,read_filenames),num_parallel_calls=AUTO)\n    if read_filenames:\n        ds=ds.map(lambda img,label: (tf.io.decode_image(img,channels=3),label),num_parallel_calls=AUTO)\n        ds=ds.map(lambda img,label:(tf.cast(img,dtype=tf.float32),label),num_parallel_calls=AUTO)\n        ds=ds.map(lambda img,label:(tf.reshape(img,[512,512,3]),label),num_parallel_calls=AUTO)\n        \n    else:\n        ds=ds.map(lambda img:tf.io.decode_image(img,channels=3),num_parallel_calls=AUTO)\n        ds=ds.map(lambda img:tf.cast(img,dtype=tf.float32),num_parallel_calls=AUTO)\n        ds=ds.map(lambda img:tf.reshape(img,[512,512,3]),num_parallel_calls=AUTO)\n        \n    ds=ds.batch(batch_size)\n    ds=ds.prefetch(AUTO)\n    return ds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_ids_ds=make_test_dataset(filepaths_test,batch_size=BATCH_SIZE,read_filenames=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_imagenames=[]\nfor item in test_ids_ds.unbatch():\n    test_imagenames.append(item[1].numpy().decode('utf-8'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_ds=make_test_dataset(filepaths_test,batch_size=BATCH_SIZE,read_filenames=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds=ensem_model.predict(test_ds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit=pd.DataFrame(dict(image_name=test_imagenames,target=preds.ravel()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit=submit.sort_values('image_name')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit.to_csv('final_submit_5.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('DONE , HURRAY')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}