{"cells":[{"metadata":{},"cell_type":"markdown","source":"\"developed by : aipythoner@gmail.com\" \n\nI'm a tensorflow(keras) user and try to learn how fastai library work, but prefer to do it on realworld datasets\n\nDeveloped model and results can be found on my [Github](https://github.com/pykeras?tab=repositories)"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport glob, os, json, cv2, skimage, shutil\nfrom fastai import *\nfrom fastai.vision import * \nfrom progressbar import ProgressBar\nfrom skimage import io","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_image_path = glob.glob('../input/imaterialist-fashion-2020-fgvc7/train/*.jpg')\ntrain_csv = pd.read_csv('../input/imaterialist-fashion-2020-fgvc7/train.csv', index_col=['ImageId'])\ntrain_csv.head(1)\n# train_image_path = train_image_path[0:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('../input/imaterialist-fashion-2020-fgvc7/label_descriptions.json') as f:\n    label_descriptions = json.load(f)\n    \nlabel_names = [x['name'] for x in label_descriptions['categories']]\n\n # 1 for background","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"46 apparel objects (27 main apparel items and 19 apparel parts) from description"},{"metadata":{"trusted":true},"cell_type":"code","source":"num_categories = 27 + 1 #(add 1 for background)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_mask(df):\n    mask_h = df.at[0, 'Height']\n    mask_w = df.at[0, 'Width']\n    mask = np.full(mask_w*mask_h, num_categories-1, dtype=np.int32)\n    \n    for encode_pixels, encode_labels in zip(df.EncodedPixels.values, df.ClassId.values):\n        pixels = list(map(int, encode_pixels.split(' ')))\n        for i in range(0,len(pixels), 2):\n            start_pixel = pixels[i]-1 #index from 0\n            len_mask = pixels[i+1]-1\n            end_pixel = start_pixel + len_mask\n            if int(encode_labels) < num_categories - 1:\n                mask[start_pixel:end_pixel] = int(encode_labels)\n            \n    mask = mask.reshape((mask_h, mask_w), order='F')\n    return mask","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if not os.path.exists('./masks'):\n    os.makedirs('./masks')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for file in ProgressBar()(train_image_path):\n    file_name = file.split('/')[-1]\n    file_id = file_name.split('.')[0]\n    df = train_csv.loc[file_id]\n    if \"Series\" in str(type(df)):\n        df = DataFrame([df.to_list()],  columns=['EncodedPixels', 'Height', \\\n                                                  'Width','ClassId',  'AttributesIds'])\n        \n    try:\n        mask = create_mask(df.reset_index())\n    except:\n        print(file_id)\n    \n    mask_rgb = np.dstack((mask, mask, mask))\n    cv2.imwrite('./masks/'+file_id+'.png', mask_rgb)\n\n#     plt.imsave('./masks/'+file_name, mask)\n\n#     io.imsave('./masks/'+file_name, mask)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_path = '../input/imaterialist-fashion-2020-fgvc7/train/'\nlabel_path = '../input/fashion-segmentation-preprocessing/masks/'\nget_label = lambda x: label_path + (str(x).split('/')[-1]).split('.')[0] + '.png'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = open_image(train_image_path[1])\nmask = open_mask(get_label(train_image_path[1]))\n_,axs = plt.subplots(1,3, figsize=(10,10))\nimg.show(ax=axs[0], title='no mask')\nimg.show(ax=axs[1], y=mask, title='masked')\nmask.show(ax=axs[2], title='mask only', alpha=1.)","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}