{"cells":[{"metadata":{},"cell_type":"markdown","source":"Solution for the problem statement."},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt\nfrom skimage.util.montage import montage2d as montage\nmontage_rgb = lambda x: np.stack([montage(x[:, :, :, i]) for i in range(x.shape[3])], -1)\ntest_image_dir = '../input/datadfy'\nfrom keras.models import load_model #to load pretrained model\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.densenet import DenseNet169 as PTModel, preprocess_input","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"301a5d939c566d1487a049bb2554d09b592b18b1"},"cell_type":"code","source":"IMG_SIZE = (299, 299) # [(224, 224), (384, 384), (512, 512), (640, 640)]\nBATCH_SIZE = 1\nRGB_FLIP = 1 # should rgb be flipped when rendering images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3ca7119188fbb4c6540d9df55f5833b55435287e"},"cell_type":"code","source":"masks = pd.read_csv('../input/datadfy/mysubmission.csv')\nprint(masks.shape[0], 'masks found')\nprint(masks['ImageId'].value_counts().shape[0])\nmasks.head()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"valid_args = dict(fill_mode = 'reflect',\n                   data_format = 'channels_last',\n                  preprocessing_function = preprocess_input)\n\nvalid_idg = ImageDataGenerator(**valid_args)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c5cc31e3117fdfa923b2acb1e6542a7007f4f955"},"cell_type":"code","source":"def flow_from_dataframe(img_data_gen, in_df, path_col, y_col, **dflow_args):\n    base_dir = os.path.dirname(in_df[path_col].values[0])\n    print('## Ignore next message from keras, values are replaced anyways')\n    df_gen = img_data_gen.flow_from_directory(base_dir, \n                                     class_mode = 'sparse',\n                                    **dflow_args)\n    df_gen.filenames = in_df[path_col].values\n    df_gen.classes = np.stack(in_df[y_col].values)\n    df_gen.samples = in_df.shape[0]\n    df_gen.n = in_df.shape[0]\n    df_gen._set_index_array()\n    df_gen.directory = '' # since we have the full path\n    print('Reinserting dataframe: {} images'.format(in_df.shape[0]))\n    return df_gen","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Loading pretrained model\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = load_model('../input/transfer-learning-for-boat-or-no-boat/full_ship_model.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"17edb177402ae51651692511827a7e9d60646533"},"cell_type":"markdown","source":"\n#We use the mysubmission file as the basis for loading and running the image."},{"metadata":{"trusted":true,"_uuid":"4911811f267f9f3397a58902da9e75c6f261ad40"},"cell_type":"code","source":"submission_df = pd.read_csv('../input/datadfy/mysubmission.csv')\nsubmission_df['path'] = submission_df['ImageId'].map(lambda x: os.path.join(test_image_dir, x))\nsubmission_df","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5b3eea954bd883d598c6ac80167dfd4353b0f558"},"cell_type":"markdown","source":"We will use the same generator as before to read and preprocess images"},{"metadata":{"trusted":true,"_uuid":"f75595679ba8606fd1ac15645b8612e117db0b30"},"cell_type":"code","source":"test_gen = flow_from_dataframe(valid_idg, \n                               submission_df, \n                             path_col = 'path',\n                            y_col = 'ImageId', \n                            target_size = IMG_SIZE,\n                             color_mode = 'rgb',\n                            batch_size = BATCH_SIZE, \n                              shuffle = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"73ef7b3b2a74bf64968c79b4005075d4f0e23143"},"cell_type":"code","source":"fig, m_axs = plt.subplots(1, 2, figsize = (20, 30))\nax1 = m_axs[0]\nax2 = m_axs[1]\nt_x = test_gen[0][0]\nt_y = test_gen[0][1]\nprint(t_y)\nt_y = model.predict(t_x)\nprint(\"Prediction Score is : \",t_y)\nt_stack = ((t_x-t_x.min())/(t_x.max()-t_x.min()))[:, :, :, ::RGB_FLIP]\nax1.imshow(montage_rgb(t_stack))\nax1.set_title('images')\nalpha_stack = np.tile(np.expand_dims(np.expand_dims(t_y, -1), -1), [1, t_stack.shape[1], t_stack.shape[2], 1])\nrgba_stack = np.concatenate([t_stack, alpha_stack], -1)\nax2.imshow(montage_rgb(rgba_stack))\nax2.set_title('boats')","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}