{"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\nimport shutil\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\n\n#Libraries:\nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing.image import ImageDataGenerator\nimport matplotlib.pyplot as plt\nfrom sklearn import metrics\nfrom PIL import Image\n\n#Set classes\nclass_names = ['cieling_light', 'lamp', 'wall_light', 'window', 'art', 'bed', 'sofa',\n               'rug', 'ottoman', 'mirror', 'toilet', 'tub', 'sink', 'cabinet', 'kitchen_table', 'oven', 'refrigerator',\n               'desk', 'shelves', 'table', 'dresser', 'desk_light', 'book', 'plant', 'laptop', 'chair']","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-24T13:59:17.738360Z","iopub.execute_input":"2022-04-24T13:59:17.739001Z","iopub.status.idle":"2022-04-24T13:59:17.745600Z","shell.execute_reply.started":"2022-04-24T13:59:17.738965Z","shell.execute_reply":"2022-04-24T13:59:17.744432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Use image metadata to get YOLO .txt files","metadata":{}},{"cell_type":"code","source":"from sklearn.cluster import DBSCAN\nimport matplotlib.patches as patches\n#meta_im - the semantic segmentation image to \n#class_min - the minimum pixel value for that class\n#class_max - the max pixel value\n#channel - which color the class is stored in: red: 0, green: 1, blue: 3\n#class_num - the number which this class corresponds too\ndef get_image_annotations(meta_im, class_min = 25, class_max = 28, channel = 0, class_num = 1, data_dict=None):\n    \n    #Draw Image code\n    image=meta_im\n\n    if data_dict:\n        class_min = data_dict['class_min']\n        class_max = data_dict['class_max']\n        channel = data_dict['channel']\n        class_num = data_dict['class_num']\n    \n    meta_im = np.array(meta_im)\n    single_color = meta_im[:,:,channel]\n\n    #calc where the array matches the class specifications\n    class_array = (single_color >= class_min)*(single_color < class_max)\n    class_locs = np.argwhere(class_array == True)\n    \n    if len(class_locs) == 0:\n        return pd.DataFrame()\n    \n    #build a clustering model \n    clustering = DBSCAN(eps = 15, min_samples=30).fit(class_locs)\n\n    label_list = clustering.labels_\n    \n    class_str = pd.DataFrame(columns=['class', 'x', 'y', 'w','h'])\n    \n    for i in range(len(set(label_list))):\n        final_labels = class_locs[label_list == i,:]\n\n        if len(final_labels) == 0:\n            continue\n\n        #get the min and max bounding box values\n        min_y = min(final_labels[:,0])\n        min_x = min(final_labels[:,1])\n\n        max_y = max(final_labels[:,0])\n        max_x = max(final_labels[:,1])\n\n        center_x = (min_x + max_x)/2/600\n        center_y = (min_y + max_y)/2/600\n\n        width = (max_x  - min_x)/600\n        height = (max_y  - min_y)/600\n\n        #each row is [class x_center y_center width height] with spaces between each double\n        data_str = pd.DataFrame(\n                    [[str(class_num-1), str(center_x), str(center_y), str(width), str(height)]], \n                    columns=['class', 'x', 'y', 'w','h']\n                    )\n        class_str = class_str.append(data_str, ignore_index=True)\n\n        '''\n        #Draw Boxes\n        # Create figure and axes\n        fig, ax = plt.subplots()\n\n        # Display the image\n        ax.imshow(image)\n\n        # Create a Rectangle patch\n        rect = patches.Rectangle((min_x, min_y), width*600, height*600, linewidth=1, edgecolor='w', facecolor='none')\n\n        # Add the patch to the Axes\n        ax.add_patch(rect)\n        plt.show()\n        '''\n    return class_str","metadata":{"execution":{"iopub.status.busy":"2022-04-24T13:59:15.114565Z","iopub.execute_input":"2022-04-24T13:59:15.114920Z","iopub.status.idle":"2022-04-24T13:59:15.130487Z","shell.execute_reply.started":"2022-04-24T13:59:15.114884Z","shell.execute_reply":"2022-04-24T13:59:15.129623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = dict()\nclasses[0] = {'class_min':9, 'class_max':11, 'channel':0, 'class_num':1}\nclasses[1] = {'class_min':25, 'class_max':28, 'channel':0, 'class_num':2}\nclasses[2] = {'class_min':43, 'class_max':46, 'channel':0, 'class_num':3}\nclasses[3] = {'class_min':61, 'class_max':64, 'channel':0, 'class_num':4}\nclasses[4] = {'class_min':78, 'class_max':81, 'channel':0, 'class_num':5}\nclasses[5] = {'class_min':94, 'class_max':96, 'channel':0, 'class_num':6}\nclasses[6] = {'class_min':108, 'class_max':111, 'channel':0, 'class_num':7}\nclasses[7] = {'class_min':121, 'class_max':124, 'channel':0, 'class_num':8}\nclasses[8] = {'class_min':133, 'class_max':136, 'channel':0, 'class_num':9}\nclasses[9] = {'class_min':143, 'class_max':146, 'channel':0, 'class_num':10}\nclasses[10] = {'class_min':153, 'class_max':156, 'channel':0, 'class_num':11}\nclasses[11] = {'class_min':161, 'class_max':164, 'channel':0, 'class_num':12}\nclasses[12] = {'class_min':169, 'class_max':172, 'channel':0, 'class_num':13}\nclasses[13] = {'class_min':176, 'class_max':179, 'channel':0, 'class_num':14}\nclasses[14] = {'class_min':182, 'class_max':185, 'channel':0, 'class_num':15}\nclasses[15] = {'class_min':188, 'class_max':190, 'channel':0, 'class_num':16}\nclasses[16] = {'class_min':193, 'class_max':195, 'channel':0, 'class_num':17}\nclasses[17] = {'class_min':197, 'class_max':200, 'channel':0, 'class_num':18}\nclasses[18] = {'class_min':202, 'class_max':204, 'channel':0, 'class_num':19}\nclasses[19] = {'class_min':9, 'class_max':11, 'channel':1, 'class_num':20}\nclasses[20] = {'class_min':25, 'class_max':28, 'channel':1, 'class_num':21}\nclasses[21] = {'class_min':43, 'class_max':46, 'channel':1, 'class_num':22}\nclasses[22] = {'class_min':61, 'class_max':64, 'channel':1, 'class_num':23}\nclasses[23] = {'class_min':78, 'class_max':81, 'channel':1, 'class_num':24}\nclasses[24] = {'class_min':94, 'class_max':96, 'channel':1, 'class_num':25}\nclasses[25] = {'class_min':108, 'class_max':111, 'channel':1, 'class_num':26}\nclasses[26] = {'class_min':121, 'class_max':124, 'channel':1, 'class_num':27}\nclasses[27] = {'class_min':133, 'class_max':136, 'channel':1, 'class_num':28}\nclasses[28] = {'class_min':143, 'class_max':146, 'channel':1, 'class_num':29}\nclasses[29] = {'class_min':153, 'class_max':156, 'channel':1, 'class_num':30}\nclasses[30] = {'class_min':161, 'class_max':164, 'channel':1, 'class_num':31}\nclasses[31] = {'class_min':169, 'class_max':172, 'channel':1, 'class_num':32}\nclasses[32] = {'class_min':41, 'class_max':43, 'channel':2, 'class_num':33}\nclasses[33] = {'class_min':89, 'class_max':92, 'channel':2, 'class_num':34}\nclasses[34] = {'class_min':128, 'class_max':131, 'channel':2, 'class_num':35}\nclasses[35] = {'class_min':157, 'class_max':159, 'channel':2, 'class_num':36}\nclasses[36] = {'class_min':178, 'class_max':180, 'channel':2, 'class_num':37}\nclasses[37] = {'class_min':193, 'class_max':196, 'channel':2, 'class_num':38}","metadata":{"execution":{"iopub.status.busy":"2022-04-24T13:59:22.943131Z","iopub.execute_input":"2022-04-24T13:59:22.943614Z","iopub.status.idle":"2022-04-24T13:59:22.966233Z","shell.execute_reply.started":"2022-04-24T13:59:22.943578Z","shell.execute_reply":"2022-04-24T13:59:22.965280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##TEST OUR FUNCTION\npath = '../input/MLDS-2021-22/Data_for_comp/Data_for_comp/metadata/metadata_2'\nmeta_im = Image.open(path+ '/'+ '0001.jpg')\n\n#Get annotation of image\nannotation = pd.DataFrame(columns=['class', 'x', 'y', 'w','h'])\nfor j in classes:\n    annote = get_image_annotations(meta_im, data_dict=classes[j])\n    if annote.empty == False:\n        print('Adding Feature')\n        annotation = annotation.append(annote)\n\nannotation","metadata":{"execution":{"iopub.status.busy":"2022-04-23T14:24:48.487491Z","iopub.execute_input":"2022-04-23T14:24:48.487682Z","iopub.status.idle":"2022-04-23T14:24:50.820888Z","shell.execute_reply.started":"2022-04-23T14:24:48.487659Z","shell.execute_reply":"2022-04-23T14:24:50.820197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Attempting YOLO","metadata":{}},{"cell_type":"code","source":"!mkdir val\n!mkdir train","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:22:11.551149Z","iopub.execute_input":"2022-04-24T14:22:11.551607Z","iopub.status.idle":"2022-04-24T14:22:12.890294Z","shell.execute_reply.started":"2022-04-24T14:22:11.551568Z","shell.execute_reply":"2022-04-24T14:22:12.889296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#run to go up directory\n#%cd ..","metadata":{"execution":{"iopub.status.busy":"2022-04-23T14:24:52.152365Z","iopub.execute_input":"2022-04-23T14:24:52.152624Z","iopub.status.idle":"2022-04-23T14:24:52.1583Z","shell.execute_reply.started":"2022-04-23T14:24:52.152595Z","shell.execute_reply":"2022-04-23T14:24:52.157134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\n# Get all classes of image folders\npath = '../input/MLDS-2021-22/Data_for_comp/Data_for_comp/metadata/metadata_2/'\ndir_list = os.listdir(path)\nimgs = []\nfor f in dir_list:\n    if int(f[0:-4])<10000:\n        imgs.append(path + f)\n    \n#imgs = [path + f if int(f[0:-4]) < 10000 for f in os.listdir(path)]  # batch of images\nall_class = [\"ceiling_fan\",\"cieling_light\",\"lamp\",\"wall_light\",\"kitchen fancy light\",\"window\",\"art\",\"bed\",\"sofa\",\n             \"ottoman\",\"rug\",\"lounge chair\",\"mirror\",\"toilet\",\"tub\",\"sink\",\"cabinet\",\"overhead cabinet\",\"kitchen_table\",\n             \"oven\",\"refrigerator\",\"desk\",\"shelves\",\"table\",\"dresser\",\"desk_light\",\"book\",\"plant\",\"laptop\",\"tv\",\"chair\",\"door\",\n             'hardwood','dark_hardwood','tile','hex tile','hex_marble_tile','marble_tile', 'bathroom', 'office','bedroom','kitchen','living room']\n\nfor im in imgs:\n    \n    i = random.uniform(0, 1)\n    if i > 0.8:\n        folder = 'val/'\n    else:\n        folder = 'train/'\n        \n\n    im_lab = im.split('/',-1)\n    im_lab = im_lab[-1]\n    im_lab = \"Image\" + im_lab\n    label_name = list(im_lab)\n    label_name[-3] = 't'\n    label_name[-2] = 'x'\n    label_name[-1] = 't'\n    label_name = \"\".join(label_name)\n    \n    #Copy image to testing folder\n    shutil.copy('../input/MLDS-2021-22/Data_for_comp/Data_for_comp/00_comp_pics/pics/'+ im_lab, '/kaggle/working/' + folder)\n    \n    #Get annotation of image\n    annotation = pd.DataFrame(columns=['class', 'x', 'y', 'w','h'])\n    meta_im = Image.open(im)\n    for j in classes:\n        annote = get_image_annotations(meta_im, data_dict=classes[j])\n        if annote.empty == False:\n            annotation = annotation.append(annote)\n    \n    #Create text file of annotation\n    with open('/kaggle/working/'+ folder + label_name, 'w') as f:\n        for index, row in annotation.iterrows():\n            f.write(str(row[0]) + ' ' + str(row[1]) + ' ' + str(row[2])+ ' ' + str(row[3])+ ' ' + str(row[4]) )\n            f.write('\\n')","metadata":{"execution":{"iopub.status.busy":"2022-04-24T19:10:39.381005Z","iopub.execute_input":"2022-04-24T19:10:39.381918Z","iopub.status.idle":"2022-04-24T21:10:32.049337Z","shell.execute_reply.started":"2022-04-24T19:10:39.381872Z","shell.execute_reply":"2022-04-24T21:10:32.048440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create .yaml file \n!git clone https://github.com/ultralytics/yolov5  # clone\n%cd yolov5\n%pip install -U -r requirements.txt\n%pip install Pillow==9.0.0\n#%pip install -r requirements.txt  # install\n\nimport yaml\nimport torch\nfrom yolov5 import utils\ndisplay = utils.notebook_init() \n\ndata_yaml = dict(\n    train = '/kaggle/working/train',\n    val = '/kaggle/working/val',\n    nc = len(all_class),\n    names = all_class\n)\n\n# Note that I am creating the file in the yolov5/data/ directory.\nwith open('/kaggle/working/yolov5/data/dataset.yaml', 'w') as outfile:\n    yaml.dump(data_yaml, outfile, default_flow_style=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:14:07.005229Z","iopub.execute_input":"2022-04-24T21:14:07.005874Z","iopub.status.idle":"2022-04-24T21:14:28.608806Z","shell.execute_reply.started":"2022-04-24T21:14:07.005832Z","shell.execute_reply":"2022-04-24T21:14:28.607992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"  !python train.py --img 640 --batch 16 --epochs 5 --data /kaggle/working/yolov5/data/dataset.yaml --weights yolov5s.pt","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:14:36.034873Z","iopub.execute_input":"2022-04-24T21:14:36.035371Z","iopub.status.idle":"2022-04-24T21:54:14.133389Z","shell.execute_reply.started":"2022-04-24T21:14:36.035332Z","shell.execute_reply":"2022-04-24T21:54:14.132549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Get model\n!python /kaggle/working/yolov5/export.py --weights /kaggle/working/yolov5/runs/train/exp/weights/best.pt --include torchscript","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:54:29.975537Z","iopub.execute_input":"2022-04-24T21:54:29.976297Z","iopub.status.idle":"2022-04-24T21:54:37.001556Z","shell.execute_reply.started":"2022-04-24T21:54:29.976255Z","shell.execute_reply":"2022-04-24T21:54:37.000754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!python detect.py --weights /kaggle/working/yolov5/runs/train/exp2/weights/best.pt --img 640 --conf 0.3 --source ../../input/MLDS-2021-22/Data_for_comp/Data_for_comp/00_comp_pics/pics/Image0001.jpg","metadata":{"execution":{"iopub.status.busy":"2022-04-23T17:21:18.113169Z","iopub.execute_input":"2022-04-23T17:21:18.113613Z","iopub.status.idle":"2022-04-23T17:21:18.119798Z","shell.execute_reply.started":"2022-04-23T17:21:18.113572Z","shell.execute_reply":"2022-04-23T17:21:18.118989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n#import torchnet as tnt\nimport numpy as np\n\n# Model\nmodel = torch.hub.load('ultralytics/yolov5', 'custom', path='/kaggle/working/yolov5/runs/train/exp/weights/best.pt', force_reload=True)\nmodel.conf=0.2\n\n# Images\npath = '../../input/MLDS-2021-22/Data_for_comp/Data_for_comp/00_comp_pics/pics/'\n#imgs = [path + f for f in os.listdir(path)]  # batch of images\ndir_list = os.listdir(path)\nimgs = []\nfor f in dir_list:\n    if int(f[5:-4])>10000:\n        imgs.append(path + f)\n\n        \npred_df = pd.DataFrame(columns=['image', 'item', 'conf'])\n# Inference\nfor img in imgs:\n    label_name = img.split('/',-1)\n    label_name = list(label_name[-1])\n    label_name = \"\".join(label_name[5:-4])\n\n    results = model(img)\n    \n    #results.print()  # or .show(), .save()\n    for i in results.pred[0]:\n        item = i[5].item()\n        conf = i[4].item()\n    \n        prediction = {'image':label_name, 'item':item, 'conf':conf}\n        pred_df = pred_df.append([prediction])\n    ","metadata":{"execution":{"iopub.status.busy":"2022-04-24T21:54:45.552431Z","iopub.execute_input":"2022-04-24T21:54:45.552991Z","iopub.status.idle":"2022-04-24T21:58:05.207381Z","shell.execute_reply.started":"2022-04-24T21:54:45.552951Z","shell.execute_reply":"2022-04-24T21:58:05.206554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Take predictions and put into DataFrame","metadata":{}},{"cell_type":"code","source":"final_df = pred_df.groupby(by=['image', 'item']).aggregate('max')\nfinal_df = final_df.reset_index()\nfinal_df = final_df.rename(columns={'item':'class_num'})\n\n#Issue with split method here\nfinal_df['id'] = final_df.apply(lambda x: str(x.image) + '_' + str(x.class_num).split('.',1)[0], axis=1)\nfinal_df['value'] = final_df[final_df.columns[0]]\nfinal_df['Id'] = final_df.id\nfinal_df['Predicted'] = final_df.conf\nfinal_df = final_df[['Id', 'Predicted']]\n\nfinal_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-24T22:17:21.423145Z","iopub.execute_input":"2022-04-24T22:17:21.423837Z","iopub.status.idle":"2022-04-24T22:17:21.466096Z","shell.execute_reply.started":"2022-04-24T22:17:21.423797Z","shell.execute_reply":"2022-04-24T22:17:21.465276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..\n#%cd working","metadata":{"execution":{"iopub.status.busy":"2022-04-24T22:17:32.856044Z","iopub.execute_input":"2022-04-24T22:17:32.856880Z","iopub.status.idle":"2022-04-24T22:17:32.865465Z","shell.execute_reply.started":"2022-04-24T22:17:32.856824Z","shell.execute_reply":"2022-04-24T22:17:32.864738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_wide = pd.read_csv('../input/MLDS-2021-22/Data_for_comp/Data_for_comp/training_wide.csv')\n\navgs = []\nfor column in training_wide.columns:\n    if column == 'Scene':\n        continue\n    mean = training_wide[column].mean()\n    avgs.append(mean)\n\n\nfor i in range(10001, 20001):\n    for j in range(43):\n        ID = str(i) + '_' + str(j+1)\n        if final_df[final_df.Id == ID].empty:\n            prediction = avgs[j]\n            final_df = final_df.append({'Id':ID,'Predicted': prediction}, ignore_index=True)\nfinal_df = final_df.groupby(by='Id').Predicted.max()\n#final_df = final_df.set_index('Id')\nfinal_df.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-24T22:17:35.966796Z","iopub.execute_input":"2022-04-24T22:17:35.967620Z","iopub.status.idle":"2022-04-24T22:53:56.368378Z","shell.execute_reply.started":"2022-04-24T22:17:35.967574Z","shell.execute_reply":"2022-04-24T22:53:56.367060Z"},"trusted":true},"execution_count":null,"outputs":[]}]}