{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport keras\nfrom tensorflow.keras.layers import Input, Concatenate, Flatten, Dense\nfrom tensorflow.keras.models import Model\nimport tensorflow as tf\nimport pickle as pkl\nimport os","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Loading a few Pickle Files\nThe pickle files used can be found here - https://www.kaggle.com/aryanpandey1109/melanoma-pickled-files","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"x = pkl.load(open('/kaggle/input/melanoma-pickled-files/numpy_image(3).pkl', 'rb'))\ntarget = pkl.load(open('/kaggle/input/melanoma-pickled-files/numpy_target(2).pkl', 'rb'))\nx_test = pkl.load(open('/kaggle/input/melanoma-pickled-files/numpy_image_test(2).pkl', 'rb'))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Getting the efficientnet ready","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"!pip install -q efficientnet\nimport efficientnet.tfkeras as efn\n\ntarget = keras.utils.to_categorical(target)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Loading Training data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_details = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/train.csv')\ntrain_details","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Here we will fill in some missing values","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_details['sex'] = train_details['sex'].fillna('male')\ntrain_details['age_approx'] = train_details['age_approx'].fillna(train_details['age_approx'].mean())\ntrain_details['anatom_site_general_challenge'] = train_details['anatom_site_general_challenge'].fillna('head/neck')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from category_encoders import TargetEncoder\nenc1 = TargetEncoder()\nenc2 = TargetEncoder()\n\ntrain_details['sex'] = enc1.fit_transform(train_details['sex'], train_details['target'])\ntrain_details['anatom_site_general_challenge'] = enc2.fit_transform(train_details['anatom_site_general_challenge'], train_details['target'])\n\nx_vec = train_details[['sex','age_approx','anatom_site_general_challenge']]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Building the model with both inputs","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"image_input = Input((144, 144, 3))\nvector_input = Input((3,))\n\nenet = efn.EfficientNetB7(input_shape=(144, 144, 3), weights='imagenet', include_top=False, pooling = 'avg')\nenet_result = enet(image_input)\nflat_layer = Flatten()(enet_result)\n\nconcat_layer= Concatenate()([vector_input, flat_layer])\noutput = Dense(2, activation = 'sigmoid')(concat_layer)\n\nmodel = Model(inputs=[image_input, vector_input], outputs=output)\n\nmodel.compile(optimizer=tf.keras.optimizers.Adam(), loss = 'binary_crossentropy', metrics=[tf.keras.metrics.AUC()])\n\nclass_weights = {0:98, 1:2}\n\nmodel.fit([x,x_vec], target, epochs=5, validation_split = 0.15, class_weight = class_weights)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Getting the test results","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"test_details = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/test.csv')\ntest_details.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_details['sex'] = test_details['sex'].fillna('male')\ntest_details['age_approx'] = test_details['age_approx'].fillna(test_details['age_approx'].mean())\ntest_details['anatom_site_general_challenge'] = test_details['anatom_site_general_challenge'].fillna('head/neck')\n\ntest_details['sex'] = enc1.transform(test_details['sex'])\ntest_details['anatom_site_general_challenge'] = enc2.transform(test_details['anatom_site_general_challenge'])\n\nx_vec_test = test_details[['sex','age_approx','anatom_site_general_challenge']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = model.predict([x_test, x_vec_test])\npreds = [float(x[1]) for x in preds]\n\nsubmission = {'image_name': test_details['image_name'], 'target': preds}\nsubmission = pd.DataFrame(submission)\n\nos.chdir('/kaggle/working')\nsubmission.to_csv(r'efficientnetb7_pred.csv', index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}