{"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":"import numpy as np\nimport pandas as pd\nimport os\nimport json\nimport random\nimport seaborn as sns\n\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport matplotlib.patches as patches\nimport tensorflow as tf\nimport pylab as pl\nfrom PIL import Image\nimport math\n\n#from tensorflow.keras.utils import to_categorical\n#from tensorflow.keras.preprocessing.image import load_img, img_to_array\n#from tensorflow.python.keras.preprocessing.image import ImageDataGenerator\n\n#from sklearn.metrics import classification_report, log_loss, accuracy_score\n#from sklearn.model_selection import train_test_split","metadata":{"papermill":{"duration":6.733234,"end_time":"2021-05-12T15:15:05.808862","exception":false,"start_time":"2021-05-12T15:14:59.075628","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check Data Structure","metadata":{"papermill":{"duration":0.037154,"end_time":"2021-05-12T15:15:05.883454","exception":false,"start_time":"2021-05-12T15:15:05.8463","status":"completed"},"tags":[]}},{"cell_type":"code","source":"data_dir = '../input/iwildcam2021-fgvc8/train'\ntest_dir = '../input/iwildcam2021-fgvc8/test'","metadata":{"papermill":{"duration":0.045214,"end_time":"2021-05-12T15:15:05.965411","exception":false,"start_time":"2021-05-12T15:15:05.920197","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample=pd.read_csv('../input/iwildcam2021-fgvc8/sample_submission.csv')\nsample[0:3]","metadata":{"papermill":{"duration":0.338669,"end_time":"2021-05-12T15:15:06.341686","exception":false,"start_time":"2021-05-12T15:15:06.003017","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('../input/iwildcam2021-fgvc8/metadata/iwildcam2021_train_annotations.json') as json_data:\n    train_anno = json.load(json_data)\n    print(train_anno.keys())","metadata":{"papermill":{"duration":1.915191,"end_time":"2021-05-12T15:15:08.404978","exception":false,"start_time":"2021-05-12T15:15:06.489787","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('../input/iwildcam2021-fgvc8/metadata/iwildcam2021_test_information.json') as json_data:\n    test_anno = json.load(json_data)\n    print(test_anno.keys())","metadata":{"papermill":{"duration":0.43446,"end_time":"2021-05-12T15:15:08.882443","exception":false,"start_time":"2021-05-12T15:15:08.447983","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('../input/iwildcam2021-fgvc8/metadata/iwildcam2021_megadetector_results.json') as json_data:\n    megadetect = json.load(json_data)\n    print(megadetect.keys())","metadata":{"papermill":{"duration":3.285427,"end_time":"2021-05-12T15:15:12.20752","exception":false,"start_time":"2021-05-12T15:15:08.922093","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_detect = pd.DataFrame(megadetect[\"images\"])\ndf_detect\n#### YOLO info for train and test","metadata":{"papermill":{"duration":0.422917,"end_time":"2021-05-12T15:15:12.678363","exception":false,"start_time":"2021-05-12T15:15:12.255446","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('../input/iwildcam2021-fgvc8/metadata/iwildcam2021_test_information.json') as json_data:\n    test_info = json.load(json_data)\n    print(test_info.keys())","metadata":{"papermill":{"duration":0.309057,"end_time":"2021-05-12T15:15:13.03444","exception":false,"start_time":"2021-05-12T15:15:12.725383","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_info=pd.DataFrame(test_info['images'])\ndf_test_info[0:3]","metadata":{"papermill":{"duration":0.281865,"end_time":"2021-05-12T15:15:13.358086","exception":false,"start_time":"2021-05-12T15:15:13.076221","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_anno=pd.DataFrame(train_anno['annotations'])\ndf_anno[0:3]","metadata":{"papermill":{"duration":0.309573,"end_time":"2021-05-12T15:15:13.709606","exception":false,"start_time":"2021-05-12T15:15:13.400033","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat = pd.DataFrame(train_anno[\"categories\"])\ndf_cat[0:3]","metadata":{"papermill":{"duration":0.05499,"end_time":"2021-05-12T15:15:13.806693","exception":false,"start_time":"2021-05-12T15:15:13.751703","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N=[]\nfor i in range(205):\n    N+=[i]\n    \ncat_id=df_cat['id']\nName=df_cat['name']\n\nNM2NU_mapping=dict(zip(Name,N))\nNU2NM_mapping=dict(zip(N,Name))\nNM2ID_mapping=dict(zip(Name,cat_id)) \nID2NM_mapping=dict(zip(cat_id,Name))\nNU2ID_mapping=dict(zip(N,cat_id))\nID2NU_mapping=dict(zip(cat_id,N))  ","metadata":{"papermill":{"duration":0.053494,"end_time":"2021-05-12T15:15:13.902821","exception":false,"start_time":"2021-05-12T15:15:13.849327","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Function to show image and YOLO rectangle","metadata":{"papermill":{"duration":0.043023,"end_time":"2021-05-12T15:15:13.988453","exception":false,"start_time":"2021-05-12T15:15:13.94543","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# for train data\n\ndef draw_bbox(img_path):\n    \n    img_id=img_path.split('/')[-1].split('.')[0] \n    img=mpimg.imread(img_path)\n    detects=df_detect[df_detect.id==img_id].detections.values[0]   \n    if detects==[]:\n        return None\n    else:\n        anno=df_anno[df_anno.image_id==img_id]    ### name\n        cat_id=anno.category_id\n        name=ID2NM_mapping[int(cat_id)]\n        _ = plt.figure(figsize = (10,10))\n        _ = plt.axis('off')\n        ax = plt.gca()\n        ax.text(10,100,f'{name}',fontsize=20,color='fuchsia')\n        for detect in detects:\n            x0,y0,w0,h0=detect['bbox']    \n            H,W,_=img.shape\n            x=x0*W\n            y=y0*H\n            w=w0*W\n            h=h0*H\n            cat='animal' if detect['category']==\"1\" else 'human'\n            bbox=patches.FancyBboxPatch((x,y),w,h,alpha=0.8,capstyle='projecting',edgecolor='fuchsia',facecolor=\"none\")\n            ax.text(x+1.5,y-8,f'{cat} {detect[\"conf\"]}',fontsize=10,bbox=dict(facecolor='fuchsia',alpha=0.8,edgecolor=\"none\"))\n            ax.add_patch(bbox)\n\n    _ = plt.imshow(img)","metadata":{"papermill":{"duration":0.056972,"end_time":"2021-05-12T15:15:14.089213","exception":false,"start_time":"2021-05-12T15:15:14.032241","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for test data\n\ndef draw_test_bbox(img_path):\n    \n    img_id=img_path.split('/')[-1].split('.')[0] \n    img=mpimg.imread(img_path)\n    detects=df_detect[df_detect.id==img_id].detections.values[0]   \n    if detects==[]:\n        return None\n    else:\n        _ = plt.figure(figsize = (10,10))\n        _ = plt.axis('off')\n        ax = plt.gca()\n        ax.text(10,100,f'test',fontsize=20,color='fuchsia')\n        for detect in detects:\n            x0,y0,w0,h0=detect['bbox']    \n            H,W,_=img.shape\n            x=x0*W\n            y=y0*H\n            w=w0*W\n            h=h0*H\n            cat='animal' if detect['category']==\"1\" else 'human'\n            bbox=patches.FancyBboxPatch((x,y),w,h,alpha=0.8,capstyle='projecting',edgecolor='fuchsia',facecolor=\"none\")\n            ax.text(x+1.5,y-8,f'{cat} {detect[\"conf\"]}',fontsize=10,bbox=dict(facecolor='fuchsia',alpha=0.8,edgecolor=\"none\"))\n            ax.add_patch(bbox)\n\n    _ = plt.imshow(img)","metadata":{"papermill":{"duration":0.056662,"end_time":"2021-05-12T15:15:14.19","exception":false,"start_time":"2021-05-12T15:15:14.133338","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = \"../input/iwildcam2021-fgvc8/train/86760c00-21bc-11ea-a13a-137349068a90.jpg\"\n_ = plt.figure(figsize = (10,10))\n_ = plt.axis('off')\n_ = plt.imshow(mpimg.imread(img)[100:-100])","metadata":{"papermill":{"duration":0.473643,"end_time":"2021-05-12T15:15:14.707515","exception":false,"start_time":"2021-05-12T15:15:14.233872","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_bbox('../input/iwildcam2021-fgvc8/train/86760c00-21bc-11ea-a13a-137349068a90.jpg')","metadata":{"papermill":{"duration":0.614723,"end_time":"2021-05-12T15:15:15.37349","exception":false,"start_time":"2021-05-12T15:15:14.758767","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Function to extract rectangle image","metadata":{"papermill":{"duration":0.059386,"end_time":"2021-05-12T15:15:15.493609","exception":false,"start_time":"2021-05-12T15:15:15.434223","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# for train data\n\nobjects=[]\nobjlabels=[]\n\ndef extract_obj(img_path, show=False):\n\n    img_id = img_path.split('/')[-1].split('.')[0]\n    img = mpimg.imread(img_path)\n    if df_detect[df_detect.id==img_id].detections.values.size!=0:      ######     \n        detects = df_detect[df_detect.id==img_id].detections.values[0]\n        if detects==[]:\n            return None\n        else:\n            anno=df_anno[df_anno.image_id==img_id]\n            cat_id=anno.category_id\n            name=ID2NM_mapping[int(cat_id)]\n            for idx,detect in enumerate(detects):\n                x0,y0,w0,h0=detects[idx]['bbox']    \n                H,W,_=img.shape\n                if w0*W>100 and h0*H>100:           \n                    x=math.floor(x0*W)\n                    y=math.floor(y0*H)\n                    w=math.floor(w0*W)\n                    h=math.floor(h0*H)\n                    obj=img[int(y):int(y+h),int(x):int(x+w)]\n                    obj1 = Image.fromarray(np.uint8(obj))\n                    obj2 = np.asarray(obj1.resize((32,32))) \n                    objects.append(obj2/255.0)\n                    objlabels.append(name)\n\n                    if show:\n                        _ = plt.figure(figsize=(3,3))\n                        _ = plt.xticks([])\n                        _ = plt.yticks([])\n                        _ = plt.imshow(obj)\n\n    return None","metadata":{"papermill":{"duration":0.075465,"end_time":"2021-05-12T15:15:15.629667","exception":false,"start_time":"2021-05-12T15:15:15.554202","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for test data\n\ntobjects=[]\ntestid=[]\n\ndef extract_test_obj(img_path, show=False):\n\n    img_id = img_path.split('/')[-1].split('.')[0]\n    img = mpimg.imread(img_path)\n    if df_detect[df_detect.id==img_id].detections.values.size!=0:      ###### \n        detects = df_detect[df_detect.id==img_id].detections.values[0]\n        if detects==[]:\n            return None\n        else:\n            for idx,detect in enumerate(detects):\n                x0,y0,w0,h0=detects[idx]['bbox']    \n                H,W,_=img.shape\n                if w0*W>100 and h0*H>100:\n                    x=math.floor(x0*W)\n                    y=math.floor(y0*H)\n                    w=math.floor(w0*W)\n                    h=math.floor(h0*H)\n                    obj=img[int(y):int(y+h),int(x):int(x+w)]\n                    obj1 = Image.fromarray(np.uint8(obj))\n                    obj2 = np.asarray(obj1.resize((32,32))) \n                    tobjects.append(obj2/255.0)\n                    testid.append(img_id)\n\n                    if show:\n                        _ = plt.figure(figsize=(3,3))\n                        _ = plt.xticks([])\n                        _ = plt.yticks([])\n                        _ = plt.imshow(obj)\n            \n    return None","metadata":{"papermill":{"duration":0.074505,"end_time":"2021-05-12T15:15:15.764012","exception":false,"start_time":"2021-05-12T15:15:15.689507","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_bbox('../input/iwildcam2021-fgvc8/train/86760c00-21bc-11ea-a13a-137349068a90.jpg')","metadata":{"papermill":{"duration":0.592668,"end_time":"2021-05-12T15:15:16.416455","exception":false,"start_time":"2021-05-12T15:15:15.823787","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extract_obj('../input/iwildcam2021-fgvc8/train/86760c00-21bc-11ea-a13a-137349068a90.jpg', show=True)","metadata":{"papermill":{"duration":0.413923,"end_time":"2021-05-12T15:15:16.899722","exception":false,"start_time":"2021-05-12T15:15:16.485799","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_test_bbox('../input/iwildcam2021-fgvc8/test/8676fd72-21bc-11ea-a13a-137349068a90.jpg')","metadata":{"papermill":{"duration":0.539401,"end_time":"2021-05-12T15:15:17.512993","exception":false,"start_time":"2021-05-12T15:15:16.973592","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extract_test_obj('../input/iwildcam2021-fgvc8/test/8676fd72-21bc-11ea-a13a-137349068a90.jpg', show=True)","metadata":{"papermill":{"duration":0.45443,"end_time":"2021-05-12T15:15:18.050978","exception":false,"start_time":"2021-05-12T15:15:17.596548","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.111306,"end_time":"2021-05-12T17:42:05.907152","exception":false,"start_time":"2021-05-12T17:42:05.795846","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.10722,"end_time":"2021-05-12T17:42:06.121217","exception":false,"start_time":"2021-05-12T17:42:06.013997","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}