{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\ntrain = pd.read_csv(\"../input/siim-isic-melanoma-classification/train.csv\")\ntest = pd.read_csv(\"../input/siim-isic-melanoma-classification/test.csv\")\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from keras_preprocessing.image import ImageDataGenerator\nfrom keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras import regularizers, optimizers\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.models import Model\n\ndef append_ext(fn):\n    return fn+\".jpg\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen=ImageDataGenerator(rescale=1./255.,validation_split=0.25)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[\"image_name\"] = train[\"image_name\"].apply(append_ext)\ntest[\"image_name\"] = test[\"image_name\"].apply(append_ext)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator = datagen.flow_from_dataframe(dataframe=train, directory=\"../input/siim-isic-melanoma-classification/jpeg/train\",\n                                               x_col=\"image_name\", y_col=\"target\",target_size=(120,160),subset=\"training\",\n                                               shuffle=True,\n                                               batch_size=64,\n                                              class_mode=\"other\")\n\nval_generator = datagen.flow_from_dataframe(dataframe=train, directory=\"../input/siim-isic-melanoma-classification/jpeg/train\",\n                                               x_col=\"image_name\", y_col=\"target\",target_size=(120,160),subset=\"validation\",\n                                               shuffle=True,\n                                               batch_size=64,\n                                               class_mode=\"other\")\n\ntest_datagen=ImageDataGenerator(rescale=1./255.)\n\ntest_generator=test_datagen.flow_from_dataframe(dataframe=test,directory=\"../input/siim-isic-melanoma-classification/jpeg/test\",\n                                                x_col=\"image_name\",y_col=None,\n                                                batch_size=64,\n                                                shuffle=False,\n                                                class_mode=None,\n                                                target_size=(120,160))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.applications import InceptionResNetV2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"basemodel = InceptionResNetV2(include_top=False,weights=\"imagenet\",input_shape=(120,160,3),pooling='avg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"headmodel = basemodel.output\nheadmodel = Dropout(0.5)(headmodel)\nheadmodel = Dense(16, activation=\"relu\")(headmodel)\nheadmodel = Dropout(0.5)(headmodel)\nheadmodel = Dense(4, activation=\"relu\")(headmodel)\nheadmodel = Dropout(0.5)(headmodel)\nheadmodel = Dense(1, activation=\"sigmoid\")(headmodel)\nmodel = Model(inputs=basemodel.input, outputs=headmodel)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"basemodel.trainable = False\n\nmodel.compile(optimizer=keras.optimizers.Adam(lr=0.01), loss=\"mse\", metrics=[tf.keras.metrics.AUC()])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit_generator(generator=train_generator,\n                          steps_per_epoch = train_generator.n//train_generator.batch_size,\n                            validation_data = val_generator,\n                          validation_steps = val_generator.n//val_generator.batch_size,\n                          epochs=3)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"STEP_SIZE_TEST=test_generator.n//test_generator.batch_size","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.evaluate_generator(generator=val_generator,\nsteps=STEP_SIZE_TEST)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_generator.reset()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds=model.predict_generator(test_generator)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = pd.DataFrame(preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/siim-isic-melanoma-classification/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds[\"image_name\"] = df[\"image_name\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds.columns = [\"target\",\"image_name\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"column_order = [\"image_name\", \"target\"]\npreds=preds.reindex(columns=column_order)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds.to_csv(\"sub1.csv\")","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}