{"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_minor":4,"nbformat":4,"cells":[{"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)\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Flatten\nfrom keras.applications import ResNet50\nfrom sklearn.model_selection import train_test_split\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-29T08:56:17.645425Z","iopub.execute_input":"2022-08-29T08:56:17.645837Z","iopub.status.idle":"2022-08-29T08:56:18.607976Z","shell.execute_reply.started":"2022-08-29T08:56:17.645759Z","shell.execute_reply":"2022-08-29T08:56:18.606726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(\"../input\"))","metadata":{"_uuid":"8a5d7a4850d5ac6fb2e7c6efbcd6b003da9dcfd9","execution":{"iopub.status.busy":"2022-08-29T08:56:32.392165Z","iopub.execute_input":"2022-08-29T08:56:32.392505Z","iopub.status.idle":"2022-08-29T08:56:32.399711Z","shell.execute_reply.started":"2022-08-29T08:56:32.392448Z","shell.execute_reply":"2022-08-29T08:56:32.398661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/train_labels.csv\")\ntrain,valid = train_test_split(train_df, test_size=0.3)","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2022-08-29T08:56:34.466516Z","iopub.execute_input":"2022-08-29T08:56:34.466826Z","iopub.status.idle":"2022-08-29T08:56:35.113565Z","shell.execute_reply.started":"2022-08-29T08:56:34.466767Z","shell.execute_reply":"2022-08-29T08:56:35.112275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 2\nresnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'","metadata":{"_uuid":"38a7c0cc475a16084fa6b1f976ca484c38aec50b","execution":{"iopub.status.busy":"2022-08-29T08:56:35.261245Z","iopub.execute_input":"2022-08-29T08:56:35.261568Z","iopub.status.idle":"2022-08-29T08:56:35.265963Z","shell.execute_reply.started":"2022-08-29T08:56:35.261508Z","shell.execute_reply":"2022-08-29T08:56:35.264937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(ResNet50(include_top=False, pooling='avg', weights=\"imagenet\"))\nmodel.add(Dense(num_classes, activation='softmax'))\nmodel.layers[0].trainable = False","metadata":{"_uuid":"3048f3f4be39ba8ebb04115dfd50a1a65fb8aa80","execution":{"iopub.status.busy":"2022-08-29T08:56:37.431266Z","iopub.execute_input":"2022-08-29T08:56:37.431578Z","iopub.status.idle":"2022-08-29T08:56:54.639694Z","shell.execute_reply.started":"2022-08-29T08:56:37.431527Z","shell.execute_reply":"2022-08-29T08:56:54.638762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"_uuid":"1e473b5dcc5e0bb69a6162e0bb34a29b60991bad","execution":{"iopub.status.busy":"2022-08-29T08:56:54.641070Z","iopub.execute_input":"2022-08-29T08:56:54.641313Z","iopub.status.idle":"2022-08-29T08:56:54.688971Z","shell.execute_reply.started":"2022-08-29T08:56:54.641268Z","shell.execute_reply":"2022-08-29T08:56:54.687976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.python.keras.applications.resnet50 import preprocess_input\nfrom tensorflow.python.keras.preprocessing.image import ImageDataGenerator\n\nimage_size = 224\ntrain_datagen = ImageDataGenerator(preprocess_input)\nvalidation_datagen = ImageDataGenerator(preprocess_input)","metadata":{"_uuid":"4be3e18e5509568d8cbd51ae3f47f370d14509bc","execution":{"iopub.status.busy":"2022-08-29T08:56:58.365635Z","iopub.execute_input":"2022-08-29T08:56:58.365983Z","iopub.status.idle":"2022-08-29T08:56:58.371436Z","shell.execute_reply.started":"2022-08-29T08:56:58.365926Z","shell.execute_reply":"2022-08-29T08:56:58.370664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(\n    dataframe=train,\n    directory='../input/train/',\n    x_col='id',\n    y_col='label',\n    has_ext=False,\n    shuffle=True\n    )","metadata":{"_uuid":"281d8e60f149fb4ae16ac3c8329b808de5c3639e","execution":{"iopub.status.busy":"2022-08-29T08:57:02.369159Z","iopub.execute_input":"2022-08-29T08:57:02.369866Z","iopub.status.idle":"2022-08-29T09:00:05.940044Z","shell.execute_reply.started":"2022-08-29T08:57:02.369799Z","shell.execute_reply":"2022-08-29T09:00:05.938831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_generator = validation_datagen.flow_from_dataframe(\n    dataframe=valid,\n    directory='../input/train/',\n    x_col='id',\n    y_col='label',\n    has_ext=False,\n    shuffle=False\n)","metadata":{"_uuid":"b9c9993ebab9c6b9e39969d19a2f572341966694","execution":{"iopub.status.busy":"2022-08-29T09:00:05.942049Z","iopub.execute_input":"2022-08-29T09:00:05.942406Z","iopub.status.idle":"2022-08-29T09:01:05.324324Z","shell.execute_reply.started":"2022-08-29T09:00:05.942337Z","shell.execute_reply":"2022-08-29T09:01:05.323048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit_generator(train_generator,\n                    steps_per_epoch=10,\n                    validation_data=validation_generator,\n                    validation_steps=10,\n                    epochs=13)","metadata":{"_uuid":"b0d8bf5f7f638b077aef6b5ceb9e53e765de280e","execution":{"iopub.status.busy":"2022-08-29T09:01:05.326000Z","iopub.execute_input":"2022-08-29T09:01:05.326380Z","iopub.status.idle":"2022-08-29T09:39:38.174775Z","shell.execute_reply.started":"2022-08-29T09:01:05.326310Z","shell.execute_reply":"2022-08-29T09:39:38.173554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Stolen from fadhli and fmarazzi","metadata":{"_uuid":"f4e0db4ec0109d8b3291cf4ff8ee34308ae49062","trusted":true}},{"cell_type":"code","source":"from glob import glob\nfrom skimage.io import imread\n\nbase_test_dir = '../input/test/'\ntest_files = glob(os.path.join(base_test_dir,'*.tif'))\nsubmission = pd.DataFrame()\nfile_batch = 5000\nmax_idx = len(test_files)\nfor idx in range(0, max_idx, file_batch):\n    print(\"Indexes: %i - %i\"%(idx, idx+file_batch))\n    test_df = pd.DataFrame({'path': test_files[idx:idx+file_batch]})\n    test_df['id'] = test_df.path.map(lambda x: x.split('/')[3].split(\".\")[0])\n    test_df['image'] = test_df['path'].map(imread)\n    K_test = np.stack(test_df[\"image\"].values)\n    K_test = (K_test - K_test.mean()) / K_test.std()\n    predictions = model.predict(K_test)\n    test_df['label'] = predictions[:,1]\n    submission = pd.concat([submission, test_df[[\"id\", \"label\"]]])\nsubmission.head()\n\nsubmission.to_csv(\"submission.csv\", index = False, header = True)\n","metadata":{"_uuid":"7c49a0f7a9f6ef8d8d8c0f0d9216dea76e4ee541","execution":{"iopub.status.busy":"2022-08-29T09:39:38.177505Z","iopub.execute_input":"2022-08-29T09:39:38.177933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"_uuid":"5fc0512625001f30e997885533577c26a51e7e44","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"4c8e39358194629b9f27dfc4df0370bc154c5a99","trusted":true},"execution_count":null,"outputs":[]}]}