{"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":"2023-01-22T10:20:21.710308Z","iopub.execute_input":"2023-01-22T10:20:21.710670Z","iopub.status.idle":"2023-01-22T10:20:22.682226Z","shell.execute_reply.started":"2023-01-22T10:20:21.710606Z","shell.execute_reply":"2023-01-22T10:20:22.681044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(\"../input\"))","metadata":{"_uuid":"8a5d7a4850d5ac6fb2e7c6efbcd6b003da9dcfd9","execution":{"iopub.status.busy":"2023-01-22T10:39:39.731046Z","iopub.execute_input":"2023-01-22T10:39:39.731395Z","iopub.status.idle":"2023-01-22T10:39:39.743967Z","shell.execute_reply.started":"2023-01-22T10:39:39.731350Z","shell.execute_reply":"2023-01-22T10:39:39.743055Z"},"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":"2023-01-22T10:39:43.039400Z","iopub.execute_input":"2023-01-22T10:39:43.039720Z","iopub.status.idle":"2023-01-22T10:39:43.545930Z","shell.execute_reply.started":"2023-01-22T10:39:43.039677Z","shell.execute_reply":"2023-01-22T10:39:43.545069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:39:49.474597Z","iopub.execute_input":"2023-01-22T10:39:49.474923Z","iopub.status.idle":"2023-01-22T10:39:49.598033Z","shell.execute_reply.started":"2023-01-22T10:39:49.474871Z","shell.execute_reply":"2023-01-22T10:39:49.596787Z"},"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","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":"2023-01-22T11:05:37.432353Z","iopub.execute_input":"2023-01-22T11:05:37.432720Z","iopub.status.idle":"2023-01-22T11:05:59.930015Z","shell.execute_reply.started":"2023-01-22T11:05:37.432650Z","shell.execute_reply":"2023-01-22T11:05:59.928883Z"},"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":"2023-01-22T11:06:16.707141Z","iopub.execute_input":"2023-01-22T11:06:16.707788Z","iopub.status.idle":"2023-01-22T11:06:16.755360Z","shell.execute_reply.started":"2023-01-22T11:06:16.707730Z","shell.execute_reply":"2023-01-22T11:06:16.754305Z"},"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":"2023-01-22T11:06:29.260108Z","iopub.execute_input":"2023-01-22T11:06:29.260768Z","iopub.status.idle":"2023-01-22T11:06:29.266825Z","shell.execute_reply.started":"2023-01-22T11:06:29.260386Z","shell.execute_reply":"2023-01-22T11:06:29.265269Z"},"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":"2023-01-22T11:06:36.360056Z","iopub.execute_input":"2023-01-22T11:06:36.360363Z","iopub.status.idle":"2023-01-22T11:10:03.537836Z","shell.execute_reply.started":"2023-01-22T11:06:36.360312Z","shell.execute_reply":"2023-01-22T11:10:03.536748Z"},"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","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","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","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":[]}]}