{"cells":[{"metadata":{},"cell_type":"markdown","source":"After predicting the class id of images we will predict the labels.\n\nWe take 5k images labeled & 1k images with no labels.\n\nWe train the model to predict the labels. No label is `BG`\n\n**LOGIC TO CLEAN BG FROM DF IS PENDING**"},{"metadata":{},"cell_type":"markdown","source":"# Import Code"},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nfrom pathlib import Path\nfrom fastai.vision import *\nfrom fastai.callbacks.hooks import *\nfrom fastai.utils.mem import *\n\nfrom itertools import groupby\nfrom progressbar import ProgressBar\nimport cv2\nfrom multiprocessing import Pool\nimport os\nimport json\nimport torchvision\nfrom datetime import datetime","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Create Dir"},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"\npath = Path(\"../input\")\npath_img = path/'train'\npath_lbl = Path(\"../labels\")\npath_lbl_bw = Path(\"../labels_bw\")\n\nSIZE_LBL = 512\n# only the 27 apparel items, plus 1 for background\n# model image size 224x224\ncategory_num = 46 + 1\nsize = 512\nEXT_LBL = 'jpg'\n\n# create a folder for the mask images\nif  not os.path.isdir(path_lbl):\n    os.makedirs(path_lbl)\n    \n\nif  not os.path.isdir(path_lbl_bw):\n    os.makedirs(path_lbl_bw)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# train dataframe\ndf = pd.read_csv(path/'train.csv')\n# get and show categories\nwith open(path/\"label_descriptions.json\") as f:\n    label_descriptions = json.load(f)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Some Code"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"masks = {}\n\ndef make_attr_img(segment_df):\n#     import pdb; pdb.set_trace()\n    seg_width = segment_df.Width\n    seg_height = segment_df.Height\n\n    seg_img = np.copy(masks.get((seg_width, seg_height)))\n    try:\n        if not seg_img:\n            seg_img = np.full(seg_width*seg_height, category_num-1, dtype=np.int32)\n            masks[(seg_width, seg_height)] = np.copy(seg_img)\n    except:\n        # seg_img exists\n        pass\n    \n    encoded_pixels = segment_df[\"EncodedPixels\"]\n    class_id = segment_df[\"ClassId\"].split(\"_\")\n    class_id, attr_id = class_id[0], class_id[1:]\n#     new_label = [\"{}:{}\".format(class_id, a) for a in attr_id]\n    new_label = \" \".join(attr_id) or \"BG\"\n    \n    if not new_label: \n        return seg_img, class_id, new_label\n    \n    pixel_list = list(map(int, encoded_pixels.split(\" \")))\n    \n    for i in range(0, len(pixel_list), 2):\n        start_index = pixel_list[i] - 1\n        index_len = pixel_list[i+1] - 1\n        seg_img[start_index:start_index+index_len] = int(class_id)\n            \n    seg_img = seg_img.reshape((seg_height, seg_width), order='F')\n    return seg_img, class_id, new_label\n        \n\ndef create_attribute_label(df, images, path_lbl_bw):\n    \"\"\"\n    img_name = \"000aac3870ea7c59ca0333ffa5327323.jpg\"\n    tdf = create_attribute_label(tdf, [img_name], path_lbl_bw)\n    \"\"\"\n    print(\"Start creating label\")\n    for idx,img in enumerate(images):\n        img_df = df[df.ImageId == img]\n        for i, row in img_df.iterrows():\n            fname = path_lbl_bw.joinpath(\n                \"{}_{}.{}\".format(os.path.splitext(img)[0] ,row.ClassId.split('_')[0],EXT_LBL)).as_posix()\n            mask, kid, lbl = make_attr_img(row)\n            if not os.path.isfile(fname): # if not exist, create\n                img_mask_3_chn = np.dstack((mask, mask, mask))\n                cv2.imwrite(fname, img_mask_3_chn)\n                \n            df.loc[i, \"AttrId\"] = lbl\n            df.loc[i, \"ClassId\"] = kid\n        if idx % 40 ==0 : print(idx, end=\" \")\n    print(\"Finish creating label\")\n    return df\n            ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Create df with 5000 + 1000 images\n# Next create label_bw"},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"!rm ../labels_bw/*\n\n# img_name = \"000aac3870ea7c59ca0333ffa5327323.jpg\"\n# tdf = create_attribute_label(tdf, [img_name], path_lbl_bw)\n\nt1df = df[df.apply(lambda x: len(x.ClassId.split('_'))>1, axis=1)]\nt1df[\"AttrId\"] = \"\"\nt2df = df[~df.ImageId.isin((t1df.ImageId.unique()))]   \nt2df = t2df.sample(10000)\nt2df[\"AttrId\"] = \"\"\ntdf = pd.concat([t1df[:50], t2df[:10]])                                #<---------- HERE\ndel t1df, t2df #, df\n\ntdf = create_attribute_label(tdf, tdf.ImageId.unique(), path_lbl_bw)     #<---------- HERE\ntdf = tdf[tdf.ImageId.isin(tdf.ImageId.unique()) ]                       #<---------- HERE\n\n##\ntdf[\"ImageId\"] = tdf.apply(lambda x: ( x.ImageId.split(\".\")[0]  + '_' + x[\"ClassId\"] ), axis=1)\ntdf = tdf[['ImageId','AttrId']]\ntdf.to_csv('multilabel.csv',index=False)\ntdf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.random.seed(42)\n\ntfms = get_transforms(flip_vert=False, max_lighting=0.1, max_zoom=1.05, max_warp=0.) \nsrc = (ImageList.from_csv(path='.', csv_name='multilabel.csv', folder='../labels_bw', suffix=\".\"+EXT_LBL, )\n       .split_by_rand_pct(0.2)\n       .label_from_df(label_delim=' ')\n      )\n\ndata = (src.transform(tfms, size=SIZE_LBL)\n        .databunch(bs=8).normalize()\n#       .normalize(imagenet_stats)\n       )\ndata.show_batch(rows=3, figsize=(12,9))\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Create Learner"},{"metadata":{"trusted":true},"cell_type":"code","source":"arch = models.resnet50\nacc_02 = partial(accuracy_thresh, thresh=0.2)\nf_score = partial(fbeta, thresh=0.2)\nlearn = cnn_learner(data, arch, metrics=[acc_02, f_score])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def f():\n#     import pdb; pdb.set_trace()\n    learn.lr_find()\n    learn.recorder.plot()\n    \n# f()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Learn 5 cycle"},{"metadata":{"trusted":true},"cell_type":"code","source":"lr = .05\nlearn.fit_one_cycle(5, slice(lr))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('stage-1-multilabel-rn50')\nt=learn.unfreeze()\n# learn.lr_find()\n# learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(5, slice(1e-5, lr/5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('stage-1-multilabel-rn50')\nlearn.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Learn one cycle Unfreeze"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(5, slice(1e-5, lr/5))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Export the models"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('stage-2-multilabel-rn50')\nlearn.export()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nlearn.recorder.plot_losses()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}