{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport sys\nfrom tqdm import tqdm\nfrom pathlib import Path\nimport tensorflow as tf\nimport skimage.io\nimport matplotlib.pyplot as plt\n\nfrom skimage.color import rgb2gray\n%matplotlib inline\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-19T19:29:33.003294Z","iopub.execute_input":"2022-03-19T19:29:33.003665Z","iopub.status.idle":"2022-03-19T19:29:59.652892Z","shell.execute_reply.started":"2022-03-19T19:29:33.003627Z","shell.execute_reply":"2022-03-19T19:29:59.651754Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\"Thresholding is a type of image segmentation, where we change the pixels of an image to make the image easier to analyze. In thresholding, we convert an image from color or grayscale into a binary image, i.e., one that is simply black and white.\"\n\nhttps://datacarpentry.org/image-processing/07-thresholding/#:~:text=Thresholding%20is%20a%20type%20of,is%20simply%20black%20and%20white.","metadata":{}},{"cell_type":"markdown","source":"![](https://slidetodoc.com/presentation_image_h/7e87127686d09dcae37cf586e988e6b9/image-33.jpg)SlideToDoc.com","metadata":{}},{"cell_type":"markdown","source":"#Prepare mask_rcnn\n\nSpoiler alert: I couldn't perform the masks images. ","metadata":{}},{"cell_type":"code","source":"# https://www.kaggle.com/pednoi/training-mask-r-cnn-to-be-a-fashionista-lb-0-07\n\n#Code by S_Shohei https://www.kaggle.com/code/iiyamaiiyama/how-to-submit-prediction\n\n!git clone https://www.github.com/matterport/Mask_RCNN.git\nos.chdir('Mask_RCNN')\n\n!rm -rf .git # to prevent an error when the kernel is committed\n!rm -rf images assets # to prevent displaying images at the bottom of a kernel","metadata":{"execution":{"iopub.status.busy":"2022-03-19T18:33:50.333151Z","iopub.execute_input":"2022-03-19T18:33:50.333758Z","iopub.status.idle":"2022-03-19T18:34:07.560468Z","shell.execute_reply.started":"2022-03-19T18:33:50.333719Z","shell.execute_reply":"2022-03-19T18:34:07.559372Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by S_Shohei https://www.kaggle.com/code/iiyamaiiyama/how-to-submit-prediction\n\nDATA_DIR = Path('/kaggle/input')\nROOT_DIR = Path('/kaggle/working')","metadata":{"execution":{"iopub.status.busy":"2022-03-19T18:34:56.411209Z","iopub.execute_input":"2022-03-19T18:34:56.411818Z","iopub.status.idle":"2022-03-19T18:34:56.416058Z","shell.execute_reply.started":"2022-03-19T18:34:56.411773Z","shell.execute_reply":"2022-03-19T18:34:56.415103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by S_Shohei https://www.kaggle.com/code/iiyamaiiyama/how-to-submit-prediction\n\nsys.path.append(ROOT_DIR/'Mask_RCNN')","metadata":{"execution":{"iopub.status.busy":"2022-03-19T18:35:17.074697Z","iopub.execute_input":"2022-03-19T18:35:17.075503Z","iopub.status.idle":"2022-03-19T18:35:17.080069Z","shell.execute_reply.started":"2022-03-19T18:35:17.075459Z","shell.execute_reply":"2022-03-19T18:35:17.079135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pycocotools","metadata":{"execution":{"iopub.status.busy":"2022-03-19T18:35:34.232012Z","iopub.execute_input":"2022-03-19T18:35:34.232829Z","iopub.status.idle":"2022-03-19T18:36:12.978746Z","shell.execute_reply.started":"2022-03-19T18:35:34.232787Z","shell.execute_reply":"2022-03-19T18:36:12.977901Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by S_Shohei https://www.kaggle.com/code/iiyamaiiyama/how-to-submit-prediction\n\nfrom mrcnn.config import Config\nfrom mrcnn import utils\n#import mrcnn.model as modellib\nfrom mrcnn import visualize\n#from mrcnn.model import log\n#import keras.engine.topology as KE\nfrom tensorflow.keras.layers import Layer","metadata":{"execution":{"iopub.status.busy":"2022-03-19T18:45:52.181853Z","iopub.execute_input":"2022-03-19T18:45:52.182193Z","iopub.status.idle":"2022-03-19T18:45:52.580255Z","shell.execute_reply.started":"2022-03-19T18:45:52.182159Z","shell.execute_reply":"2022-03-19T18:45:52.579383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5","metadata":{"execution":{"iopub.status.busy":"2022-03-19T18:46:07.889579Z","iopub.execute_input":"2022-03-19T18:46:07.889906Z","iopub.status.idle":"2022-03-19T18:46:53.830392Z","shell.execute_reply.started":"2022-03-19T18:46:07.889861Z","shell.execute_reply":"2022-03-19T18:46:53.829409Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"COCO_MODEL_PATH = 'mask_rcnn_coco.h5'","metadata":{"execution":{"iopub.status.busy":"2022-03-19T18:47:15.588906Z","iopub.execute_input":"2022-03-19T18:47:15.589845Z","iopub.status.idle":"2022-03-19T18:47:15.593783Z","shell.execute_reply.started":"2022-03-19T18:47:15.589786Z","shell.execute_reply":"2022-03-19T18:47:15.592837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#StackOverflow trying to fix AttributeError: module 'keras.engine' has no attribute 'Layer'","metadata":{}},{"cell_type":"code","source":"import tensorflow\ntensorflow.__version__","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-19T19:07:43.324588Z","iopub.execute_input":"2022-03-19T19:07:43.324955Z","iopub.status.idle":"2022-03-19T19:07:43.333587Z","shell.execute_reply.started":"2022-03-19T19:07:43.324918Z","shell.execute_reply":"2022-03-19T19:07:43.332922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip uninstall keras -y\n!pip uninstall keras-nightly -y\n!pip uninstall keras-Preprocessing -y\n!pip uninstall keras-vis -y\n!pip uninstall tensorflow -y\n!pip uninstall h5py -y","metadata":{"execution":{"iopub.status.busy":"2022-03-19T19:08:38.824318Z","iopub.execute_input":"2022-03-19T19:08:38.824647Z","iopub.status.idle":"2022-03-19T19:08:51.267729Z","shell.execute_reply.started":"2022-03-19T19:08:38.824611Z","shell.execute_reply":"2022-03-19T19:08:51.266455Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tensorflow==1.13.1\n!pip install keras==2.0.8\n!pip install h5py==2.10.0","metadata":{"execution":{"iopub.status.busy":"2022-03-19T19:09:29.125825Z","iopub.execute_input":"2022-03-19T19:09:29.126190Z","iopub.status.idle":"2022-03-19T19:10:10.089108Z","shell.execute_reply.started":"2022-03-19T19:09:29.126149Z","shell.execute_reply":"2022-03-19T19:10:10.087837Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport keras.layers as KE\n\nimport keras.engine.topology as KE","metadata":{"execution":{"iopub.status.busy":"2022-03-19T19:19:09.703095Z","iopub.execute_input":"2022-03-19T19:19:09.703457Z","iopub.status.idle":"2022-03-19T19:19:09.714857Z","shell.execute_reply.started":"2022-03-19T19:19:09.703422Z","shell.execute_reply":"2022-03-19T19:19:09.713877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from tensorflow.keras.layers import Layer\n\n# Import COCO config\nsys.path.append(os.path.join(ROOT_DIR, \"Mask_RCNN/samples/coco/\"))  # To find local version\nimport coco","metadata":{"execution":{"iopub.status.busy":"2022-03-19T19:19:16.171896Z","iopub.execute_input":"2022-03-19T19:19:16.172630Z","iopub.status.idle":"2022-03-19T19:19:16.206119Z","shell.execute_reply.started":"2022-03-19T19:19:16.172575Z","shell.execute_reply":"2022-03-19T19:19:16.204671Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#After AttributeError: module 'keras.engine' has no attribute 'Layer'\n\nMany answers on StackOverflow that doesn't give any result.\n\nI wasn't able to proceed with Masks RCNN ","metadata":{}},{"cell_type":"code","source":"#Code by Volody  https://www.kaggle.com/code/volody/threshold-segmentation\n\nimg_list = ['000011451.jpg', '000011444.jpg', '000011447.jpg', '000011456.jpg']\nfig = plt.figure(figsize=(16, 16))\nfor i in range(4):\n    x = fig.add_subplot(2, 2, i+1)\n    image = plt.imread('/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images/10290/'+img_list[i])\n    x.set_title(\"{image} ({shape[0]},{shape[1]})\".format(image=img_list[i],shape=image.shape))\n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-03-19T19:38:42.019597Z","iopub.execute_input":"2022-03-19T19:38:42.020470Z","iopub.status.idle":"2022-03-19T19:38:46.225965Z","shell.execute_reply.started":"2022-03-19T19:38:42.020411Z","shell.execute_reply":"2022-03-19T19:38:46.225361Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Convert it into grayscale, that allows using simple treshold processing","metadata":{}},{"cell_type":"code","source":"#Code by Volody  https://www.kaggle.com/code/volody/threshold-segmentation\n\nfig = plt.figure(figsize=(16, 16))\nfor i in range(4):\n    x = fig.add_subplot(2, 2, i+1)\n    image = plt.imread('/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images/10290/'+img_list[i])\n    gray = rgb2gray(image)\n    x.set_title(\"{image} ({shape[0]},{shape[1]})\".format(image=img_list[i],shape=gray.shape))\n    plt.imshow(gray, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-03-19T19:42:21.952308Z","iopub.execute_input":"2022-03-19T19:42:21.952894Z","iopub.status.idle":"2022-03-19T19:42:25.831438Z","shell.execute_reply.started":"2022-03-19T19:42:21.952858Z","shell.execute_reply":"2022-03-19T19:42:25.830426Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Volody  https://www.kaggle.com/code/volody/threshold-segmentation\n\ndef to_2regions(image):\n    gray = rgb2gray(image)\n    m = gray.mean()\n    for i in range(image.shape[0]):\n        for j in range(image.shape[1]):    \n            gray[i,j] = int(gray[i,j] > m)\n    return gray","metadata":{"execution":{"iopub.status.busy":"2022-03-19T19:43:00.291511Z","iopub.execute_input":"2022-03-19T19:43:00.292527Z","iopub.status.idle":"2022-03-19T19:43:00.297595Z","shell.execute_reply.started":"2022-03-19T19:43:00.292482Z","shell.execute_reply":"2022-03-19T19:43:00.296724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Volody  https://www.kaggle.com/code/volody/threshold-segmentation\n\nfig = plt.figure(figsize=(16, 16))\nfor i in range(4):\n    x = fig.add_subplot(2, 2, i+1)\n    image = plt.imread('/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images/10290/'+img_list[i])\n    r2_gray = to_2regions(image)\n    x.set_title(\"{image} ({shape[0]},{shape[1]})\".format(image=img_list[i],shape=r2_gray.shape))\n    plt.imshow(r2_gray, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-03-19T19:44:07.406074Z","iopub.execute_input":"2022-03-19T19:44:07.406964Z","iopub.status.idle":"2022-03-19T19:44:21.037367Z","shell.execute_reply.started":"2022-03-19T19:44:07.406919Z","shell.execute_reply":"2022-03-19T19:44:21.036419Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Thresholds to detect multiple objects:","metadata":{}},{"cell_type":"code","source":"#Code by Volody  https://www.kaggle.com/code/volody/threshold-segmentation\n\ndef to_4regions(image):\n    g_image = rgb2gray(image)\n    m = g_image.mean()\n    m1 = 0.5 * m\n    m2 = 0.25 * m\n    for i in range(image.shape[0]):\n        for j in range(image.shape[1]):\n            pxl = g_image[i,j]\n            if pxl > m:\n                g_image[i,j] = 3\n            elif pxl > m1:\n                g_image[i,j] = 2\n            elif pxl > m2:\n                g_image[i,j] = 1\n            else:\n                g_image[i,j] = 0            \n    return g_image","metadata":{"execution":{"iopub.status.busy":"2022-03-19T19:44:33.775680Z","iopub.execute_input":"2022-03-19T19:44:33.775987Z","iopub.status.idle":"2022-03-19T19:44:33.783503Z","shell.execute_reply.started":"2022-03-19T19:44:33.775955Z","shell.execute_reply":"2022-03-19T19:44:33.782154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Volody  https://www.kaggle.com/code/volody/threshold-segmentation\n\nfig = plt.figure(figsize=(16, 16))\nfor i in range(4):\n    x = fig.add_subplot(2, 2, i+1)\n    image = plt.imread('/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images/10290/'+img_list[i])\n    r2_gray = to_4regions(image)\n    x.set_title(\"{image} ({shape[0]},{shape[1]})\".format(image=img_list[i],shape=r2_gray.shape))\n    plt.imshow(r2_gray, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-03-19T19:45:39.064918Z","iopub.execute_input":"2022-03-19T19:45:39.065937Z","iopub.status.idle":"2022-03-19T19:45:49.187979Z","shell.execute_reply.started":"2022-03-19T19:45:39.065890Z","shell.execute_reply":"2022-03-19T19:45:49.187269Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Unfortunately I didn't work with the masks, yet.","metadata":{}},{"cell_type":"markdown","source":"#Acknowledgements:\n\nCode by Volody  https://www.kaggle.com/code/volody/threshold-segmentation\n\nS_Shohei https://www.kaggle.com/code/iiyamaiiyama/how-to-submit-prediction","metadata":{}}]}