{"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\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","execution":{"iopub.status.busy":"2021-07-29T00:35:11.223407Z","iopub.execute_input":"2021-07-29T00:35:11.223931Z","iopub.status.idle":"2021-07-29T00:35:11.243046Z","shell.execute_reply.started":"2021-07-29T00:35:11.223818Z","shell.execute_reply":"2021-07-29T00:35:11.241898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\nimport sys\nimport json\nimport glob\nimport random\nfrom pathlib import Path\n\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport itertools\nfrom tqdm import tqdm\n\nfrom imgaug import augmenters as iaa\nfrom sklearn.model_selection import StratifiedKFold, KFold","metadata":{"execution":{"iopub.status.busy":"2021-07-31T14:25:52.552336Z","iopub.execute_input":"2021-07-31T14:25:52.552987Z","iopub.status.idle":"2021-07-31T14:25:54.925669Z","shell.execute_reply.started":"2021-07-31T14:25:52.552848Z","shell.execute_reply":"2021-07-31T14:25:54.924712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/imaterialist-fashion-2019-FGVC6/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-31T14:25:54.927358Z","iopub.execute_input":"2021-07-31T14:25:54.927645Z","iopub.status.idle":"2021-07-31T14:26:21.340431Z","shell.execute_reply.started":"2021-07-31T14:25:54.927616Z","shell.execute_reply":"2021-07-31T14:26:21.339368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2021-07-31T14:26:21.341821Z","iopub.execute_input":"2021-07-31T14:26:21.342115Z","iopub.status.idle":"2021-07-31T14:26:21.354007Z","shell.execute_reply.started":"2021-07-31T14:26:21.342085Z","shell.execute_reply":"2021-07-31T14:26:21.352960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['CategoryId'] = df['ClassId'].str.split('_').str[0]\ndf['AttributeId'] = df['ClassId'].str.split('_').str[1:]\n\nprint(\"Total segments: \", len(df))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-31T17:17:32.448058Z","iopub.execute_input":"2021-07-31T17:17:32.448404Z","iopub.status.idle":"2021-07-31T17:17:34.169632Z","shell.execute_reply.started":"2021-07-31T17:17:32.448376Z","shell.execute_reply":"2021-07-31T17:17:34.168537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_img(img):\n    I = cv2.imread(\"/kaggle/input/train/\" + img, cv2.IMREAD_COLOR)\n    I = cv2.cvtColor(I, cv2.COLOR_BGR2RGB)\n    I = cv2.resize(I, (IMAGE_SIZE, IMAGE_SIZE), interpolation=cv2.INTER_AREA)  \n    plt.imshow(I)","metadata":{"execution":{"iopub.status.busy":"2021-07-31T14:26:22.934213Z","iopub.execute_input":"2021-07-31T14:26:22.934567Z","iopub.status.idle":"2021-07-31T14:26:22.939826Z","shell.execute_reply.started":"2021-07-31T14:26:22.934536Z","shell.execute_reply":"2021-07-31T14:26:22.938780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.transform import resize\n\ndef show_img(img):\n    I = cv2.imread(\"/kaggle/input/imaterialist-fashion-2019-FGVC6/train/\" + img, cv2.IMREAD_COLOR)\n    I = cv2.cvtColor(I, cv2.COLOR_BGR2RGB)\n    I = cv2.resize(I, (256, 256), interpolation=cv2.INTER_AREA)  \n    plt.imshow(I)\n\ndef get_mask(df, img_id):\n    a = df[df.ImageId == img_id]\n    a = a.groupby('CategoryId', as_index=False).agg({'EncodedPixels':' '.join, 'Height':'first','Width':'first'})\n    H = a.iloc[0,2]\n    W = a.iloc[0,3]\n    masks =[]\n    categories =[]\n    for line in a[['EncodedPixels','CategoryId']].iterrows():\n        mask = np.full(H*W,dtype='int',fill_value = 0)\n        EncodedPixels = line[1][0]\n        pixel_loc = list(map(int,EncodedPixels.split(' ')[0::2]))\n        iter_num =  list(map(int,EncodedPixels.split(' ')[1::2]))\n        for p,i in zip(pixel_loc,iter_num):\n            mask[p:(p+i)] = line[1][1]\n        mask = mask.reshape(W,H).T\n        masks+=[mask]\n        categories+=[line[1][1]]\n    return masks, categories\n\ndef new_mask(mask):\n    matrix = [[0 for x in range(512)] for y in range(512)] \n    for i in range(0, len(mask)):\n        mask[i] = cv2.resize(mask[i], (512, 512), interpolation=cv2.INTER_NEAREST)\n    for m in mask:\n        for i in range(0, 512):\n            for j in range(0, 512):\n                if m[i][j] != 0:\n                    matrix[i][j]=m[i][j]\n    matrix = np.array(matrix)\n    return matrix\n\ndef masked_image(df, image_id):\n    masked_list, categories = get_mask(df, image_id)\n    plt.figure(figsize=[30,30])\n    plt.subplot(1,10,1)\n    I = cv2.imread(\"/kaggle/input/imaterialist-fashion-2019-FGVC6/train/\" + image_id, cv2.IMREAD_COLOR)\n    I = cv2.cvtColor(I, cv2.COLOR_BGR2RGB)\n    I = cv2.resize(I, (512, 512), interpolation=cv2.INTER_AREA)  \n    plt.imshow(I)    \n    plt.title('Input Image')\n    i=1\n    gray = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY)\n    \n    plt.imshow(I)    \n    for mask, cat in zip(masked_list, 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        plt.subplots_adjust(wspace=0.4, hspace=-0.65)\n        i+=1\n        if i ==10:\n            break\n    new_mask_ = new_mask(masked_list)\n    I[new_mask_ == 0] = 0\n    plt.imshow(I)    \n","metadata":{"execution":{"iopub.status.busy":"2021-07-31T17:37:27.989111Z","iopub.execute_input":"2021-07-31T17:37:27.989458Z","iopub.status.idle":"2021-07-31T17:37:28.006270Z","shell.execute_reply.started":"2021-07-31T17:37:27.989419Z","shell.execute_reply":"2021-07-31T17:37:28.004972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = df['ImageId'].unique()\nfor idx in ids[0:10]:\n    masked_image(df, idx)","metadata":{"execution":{"iopub.status.busy":"2021-07-31T17:37:30.946597Z","iopub.execute_input":"2021-07-31T17:37:30.946972Z","iopub.status.idle":"2021-07-31T17:38:04.945091Z","shell.execute_reply.started":"2021-07-31T17:37:30.946929Z","shell.execute_reply":"2021-07-31T17:38:04.944022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"working progress","metadata":{}}]}