{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","scrolled":true,"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport time\nimport gc\nfrom matplotlib.pyplot import imshow\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import roc_auc_score\n# from keras import layers\nfrom keras.layers import Dropout, Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, MaxPooling2D, AveragePooling2D, GlobalMaxPooling2D, GlobalAveragePooling2D, Concatenate\nfrom keras.models import Model, load_model\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.initializers import glorot_uniform\nfrom keras import optimizers\nfrom keras import regularizers\nfrom keras.callbacks import ModelCheckpoint\nfrom keras.applications.densenet import DenseNet201\nfrom IPython.display import FileLink\nfrom IPython.display import FileLinks\nos.listdir(\"../input\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1dc8f305d9350e0e086ac37a4e099b619f3639e8","trusted":true},"cell_type":"code","source":"def read_image_test_lw(img_id):\n    path = \"../input/test/\" + img_id + \".tif\"\n    img = cv2.imread(path)/ 255\n    return img","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"eb6884e837fb1987730ce998ea4e65d31ba79b0e","trusted":true},"cell_type":"code","source":"def model_f(input_shape):\n    X_input = Input(input_shape)\n    X = DenseNet201()(X_input)\n    X = Dropout(0.5)(X)\n    X = Dense(1, activation='sigmoid', kernel_initializer = glorot_uniform(seed=0))(X)\n    model = Model(inputs = X_input, outputs = X)\n    return model","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"977a25f6ba4480227577f5273fc0744d46da3f03","trusted":true},"cell_type":"code","source":"df_data = pd.read_csv('../input/train_labels.csv')\nbatch_size = 32\nepochs = 8\nrandom_state = 5\nmodel_name = \"model_best_1\"\nsize_original = 96","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"64ffad5bbb382ddbe7feda909ca5ef5b83bdd017","trusted":true},"cell_type":"code","source":"path_data = \"../input/\"\nX_train_index, X_val_index, y_train, y_val = train_test_split(df_data['id'].values, df_data['label'].values, test_size=0.2, random_state=random_state)\ndf_train = pd.DataFrame({\"id\": X_train_index + \".tif\", \"label\":y_train.astype(str)})\ndf_val = pd.DataFrame({\"id\": X_val_index + \".tif\", \"label\":y_val.astype(str)})\n\ntrain_steps = len(X_train_index)//batch_size\nval_steps = len(X_val_index)//batch_size\n\ndf_test = pd.read_csv('../input/sample_submission.csv')\ndf_test['id'] = df_test['id'] + '.tif'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1d3d4587d4b28943374fc8dcb0c8b048e0745298","trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255,horizontal_flip=True,vertical_flip=True)\ntrain_generator = train_datagen.flow_from_dataframe(\n        df_train,\n        directory = path_data + \"train/\",\n        x_col = \"id\",\n        y_col = \"label\",\n        target_size=(size_original, size_original),\n        batch_size=batch_size,\n        class_mode='binary')\n\nval_datagen = ImageDataGenerator(rescale=1./255)\nval_generator = val_datagen.flow_from_dataframe(\n        df_val,\n        directory = path_data + \"train/\",\n        x_col = \"id\",\n        y_col = \"label\",\n        target_size=(size_original, size_original),\n        batch_size=batch_size,\n        class_mode='binary')\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntest_generator = val_datagen.flow_from_dataframe(\n        df_test,\n        directory = path_data + \"test/\",\n        x_col = \"id\",\n        y_col = \"label\",\n        target_size=(size_original, size_original),\n        batch_size=256,\n        shuffle=False,\n        class_mode=None)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d7897c2344b3e1ce10b12b172702b3f7be02bf9b","trusted":true},"cell_type":"code","source":"model_lw = model_f(input_shape=(size_original,size_original,3))\nmodel_lw.compile(optimizer = optimizers.Adam(lr=0.0001), loss = \"binary_crossentropy\", metrics = [\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0bdb4694abbf44090a9731a71fd8a0b97b34e75d","trusted":true},"cell_type":"code","source":"filepath = model_name + \".h5\"\ncheckpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')\ntrain_history = model_lw.fit_generator(train_generator,\n                                    epochs=epochs,\n                                    steps_per_epoch = train_steps,\n                                    validation_data = val_generator,\n                                    validation_steps = val_steps,\n                                    shuffle = True,\n                                    callbacks=[checkpoint])\n\nprint(str(train_history.history), file = open(\"Model_Details.txt\", \"w\"))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d8c040a5065f7ccec4c8489a1ba03e55619dc1b3","trusted":true},"cell_type":"code","source":"model_lw.load_weights(model_name + \".h5\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"93da9b7edf1a676c590e11e02677eac23471cd6e","trusted":true},"cell_type":"code","source":"predict = model_lw.predict_generator(test_generator, steps = len(test_generator), workers=0, verbose=1)\ndf_test = pd.read_csv('../input/sample_submission.csv')\ndf_test['label'] = predict\ndf_test.to_csv(model_name + \"_result.csv\", index=False)\ndf_test.head\n\n# time_start = time.time()\n# print(\"--- %s minutes ---\" % ((time.time() - time_start)/60))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0ee4c99402ff86779acb133d4da1ebc5bcf6f58b","trusted":true},"cell_type":"code","source":"FileLink(\"model_best_1_result.csv\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1d9ad4a27963a3aef4ce7f61a8926422def18430","trusted":true},"cell_type":"code","source":"FileLink(\"model_best_1.h5\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7c41837ece0f1dd86e70a382b9a24c20b96a9358","scrolled":true,"trusted":true},"cell_type":"code","source":"FileLink(\"Model_Details.txt\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b906029d325074dc49a051f2b5889b72bab1fd04","trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}