{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":13032,"databundleVersionId":862545,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os,json\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:41:03.594422Z","iopub.execute_input":"2024-12-18T16:41:03.594813Z","iopub.status.idle":"2024-12-18T16:41:03.600039Z","shell.execute_reply.started":"2024-12-18T16:41:03.594778Z","shell.execute_reply":"2024-12-18T16:41:03.598751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/imaterialist-fashion-2019-FGVC6/train.csv')\ntrain_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:41:05.193522Z","iopub.execute_input":"2024-12-18T16:41:05.193887Z","iopub.status.idle":"2024-12-18T16:41:35.822009Z","shell.execute_reply.started":"2024-12-18T16:41:05.193857Z","shell.execute_reply":"2024-12-18T16:41:35.820978Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['CategoryId'] = train_df['ClassId'].str.split('_').str[0]\ntrain_df['AttributeId'] = train_df['ClassId'].str.split('_').str[1:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:41:40.412019Z","iopub.execute_input":"2024-12-18T16:41:40.412411Z","iopub.status.idle":"2024-12-18T16:41:41.653736Z","shell.execute_reply.started":"2024-12-18T16:41:40.412360Z","shell.execute_reply":"2024-12-18T16:41:41.652629Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open(\"/kaggle/input/imaterialist-fashion-2019-FGVC6/label_descriptions.json\") as f:\n    label_descriptions = json.load(f)\n\nlabel_names = [x['name'] for x in label_descriptions['categories']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:41:41.655603Z","iopub.execute_input":"2024-12-18T16:41:41.656020Z","iopub.status.idle":"2024-12-18T16:41:41.668570Z","shell.execute_reply.started":"2024-12-18T16:41:41.655973Z","shell.execute_reply":"2024-12-18T16:41:41.667635Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# copied from https://ccshenyltw.medium.com/run-length-encode-and-decode-a33383142e6b\ndef rle_decode(mask_rle, shape):\n    '''\n    mask_rle: run-length as string formated: [start0] [length0] [start1] [length1]... in 1d array\n    shape: (height,width) of array to return\n    Returns numpy array according to the shape, 1 - mask, 0 - background\n    '''\n    shape = (shape[1], shape[0])\n    s = mask_rle.split()\n    # gets starts & lengths 1d arrays\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0::2], s[1::2])]\n    starts -= 1\n    # gets ends 1d array\n    ends = starts + lengths\n    # creates blank mask image 1d array\n    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)\n    # sets mark pixels\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    # reshape as a 2d mask image\n    return img.reshape(shape).T  # Needed to align to RLE direction","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:43:31.364409Z","iopub.execute_input":"2024-12-18T16:43:31.364812Z","iopub.status.idle":"2024-12-18T16:43:31.372419Z","shell.execute_reply.started":"2024-12-18T16:43:31.364780Z","shell.execute_reply":"2024-12-18T16:43:31.371051Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_masks(df, img_idx):\n    img_id = df.iloc[img_idx].ImageId\n    orig_height = df.iloc[img_idx].Height\n    orig_width = df.iloc[img_idx].Width\n\n    sub_df = df[df['ImageId'] == img_id]\n    mask_rles = sub_df.EncodedPixels.to_list()\n    categories = sub_df.CategoryId.to_list()\n\n    masks = list(map(lambda x: rle_decode(x, (orig_height,orig_width)), mask_rles))\n    \n    return masks, categories","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:43:31.615056Z","iopub.execute_input":"2024-12-18T16:43:31.615449Z","iopub.status.idle":"2024-12-18T16:43:31.622175Z","shell.execute_reply.started":"2024-12-18T16:43:31.615413Z","shell.execute_reply":"2024-12-18T16:43:31.620927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_image(df, img_idx):\n\n    image_id = df.iloc[img_idx].ImageId\n    img = cv2.imread(\"/kaggle/input/imaterialist-fashion-2019-FGVC6/train/\" + image_id, cv2.IMREAD_COLOR)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_AREA)  \n    plt.figure(figsize=[30,30])\n    plt.subplot(1,10,1)\n    plt.imshow(img)    \n    plt.title('Input Image')\n    \n    masks, categories = get_masks(df, img_idx)\n    i=1\n    for mask, cat in zip(masks, categories):\n        mask = cv2.resize(mask, (512, 512), interpolation=cv2.INTER_NEAREST)\n        plt.subplot(1,10,i+1)\n        plt.imshow(mask)\n        plt.title(label_names[int(cat)])\n        i+=1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:43:32.030150Z","iopub.execute_input":"2024-12-18T16:43:32.030553Z","iopub.status.idle":"2024-12-18T16:43:32.038790Z","shell.execute_reply.started":"2024-12-18T16:43:32.030521Z","shell.execute_reply":"2024-12-18T16:43:32.037556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_image(train_df, 199)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T16:44:11.420316Z","iopub.execute_input":"2024-12-18T16:44:11.420720Z","iopub.status.idle":"2024-12-18T16:44:12.909806Z","shell.execute_reply.started":"2024-12-18T16:44:11.420684Z","shell.execute_reply":"2024-12-18T16:44:12.908695Z"}},"outputs":[],"execution_count":null}]}