{"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)\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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8a5d7a4850d5ac6fb2e7c6efbcd6b003da9dcfd9"},"cell_type":"code","source":"print(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/train_labels.csv\")\ntrain,valid = train_test_split(train_df, test_size=0.3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38a7c0cc475a16084fa6b1f976ca484c38aec50b"},"cell_type":"code","source":"num_classes = 2\nresnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3048f3f4be39ba8ebb04115dfd50a1a65fb8aa80"},"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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e473b5dcc5e0bb69a6162e0bb34a29b60991bad"},"cell_type":"code","source":"model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4be3e18e5509568d8cbd51ae3f47f370d14509bc"},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"281d8e60f149fb4ae16ac3c8329b808de5c3639e"},"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    )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9c9993ebab9c6b9e39969d19a2f572341966694"},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0d8bf5f7f638b077aef6b5ceb9e53e765de280e"},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f4e0db4ec0109d8b3291cf4ff8ee34308ae49062"},"cell_type":"markdown","source":"Stolen from fadhli and fmarazzi"},{"metadata":{"trusted":true,"_uuid":"7c49a0f7a9f6ef8d8d8c0f0d9216dea76e4ee541"},"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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5fc0512625001f30e997885533577c26a51e7e44"},"cell_type":"code","source":"predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c8e39358194629b9f27dfc4df0370bc154c5a99"},"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}