{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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\n# import os\n# for 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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import json","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open(\"/kaggle/input/imaterialist-fashion-2019-FGVC6/label_descriptions.json\",\"r\") as file:\n    data = json.load(file)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dir = \"../input/imaterialist-fashion-2019-FGVC6/train\"\ntrain_csv = \"../input/imaterialist-fashion-2019-FGVC6/train.csv\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(train_csv)\ndf.head(15)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.ClassId.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#checking if there are attributes are 91\n\n# test_dataframe = df[\"ClassId\"].values\n# for x in test_dataframe:\n# #     if \"91\" in x\n#         print(\"91\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom random import randint,choice","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_dataframe = df[\"ClassId\"].values\ntest_dataframe","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def function_to_print_attributes_by_class_id(dataframe,train_dir,class_id,name,num_examples=2):\n#     byClass_Dataframe = dataframe[dataframe[\"ClassId\"]==class_id]\n#     to_plt = byClass_Dataframe.head(randint(1,10))[\"ImageId\"].values[0]\n#     img = plt.imread(os.path.join(train_dir,to_plt))\n#     plt.imshow(img)\n    test_dataframe = dataframe[\"ClassId\"].values\n    for i in range(num_examples):\n        random_num = choice([ i for (i,data) in enumerate(test_dataframe) if class_id in data])\n        to_plt = dataframe[random_num:random_num+1][\"ImageId\"].values[0]\n        img = cv2.imread(os.path.join(train_dir,to_plt))\n        cv2.imwrite(\"./\"+name +str(i)+\".jpg\",img)\n        plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"attrib = data[\"attributes\"]\nfor i,key in enumerate(attrib):\n    if key[\"id\"]==i:\n        string = key[\"name\"]\n        function_to_print_attributes_by_class_id(df,train_dir,str(i),string,num_examples=2)\n#         print(i,string)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"attributes = {}\nfor x in data[\"attributes\"]:\n    if x[\"supercategory\"] not in attributes:\n        attributes[x[\"supercategory\"]] = []\n        attributes[x[\"supercategory\"]].append(x[\"name\"])\n    else:\n        attributes[x[\"supercategory\"]].append(x[\"name\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(attributes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"categories = {}\nfor x in data[\"categories\"]:\n    if x[\"supercategory\"] not in categories:\n        categories[x[\"supercategory\"]] = []\n        categories[x[\"supercategory\"]].append(x[\"name\"])\n    else:\n        categories[x[\"supercategory\"]].append(x[\"name\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(categories)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_id = df[\"ClassId\"].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_id.head(30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def print_by_class_id(dataframe,train_dir,class_id):\n    byClass_Dataframe = dataframe[dataframe[\"ClassId\"]==class_id]\n    rand = randint(0,byClass_Dataframe.shape[0])\n    to_plt = byClass_Dataframe[rand:rand+1][\"ImageId\"].values[0]\n#     to_plt = choice(byClass_Dataframe[0])\n#     print(to_plt)\n    img = plt.imread(os.path.join(train_dir,to_plt))\n    plt.imshow(img)\n\nprint_by_class_id(df,train_dir,\"32\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def count(dataframe,class_id):\n#     byClass_Dataframe = dataframe[dataframe[\"ClassId\"]==class_id]\n#     to_plt = byClass_Dataframe.head(randint(1,10))[\"ImageId\"].values[0]\n#     img = plt.imread(os.path.join(train_dir,to_plt))\n#     plt.imshow(img)\n    test_dataframe = dataframe[\"ClassId\"].values\n    total_data = [ i for (i,data) in enumerate(test_dataframe) if class_id in data]\n#     random_num = choice(total_data)\n#     to_plt = dataframe[random_num:random_num+1][\"ImageId\"].values[0]\n    return len(total_data)\ncount(df,\"0\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lists = []\nnames = []\nfor i in data[\"attributes\"]:\n    ids = i['id']\n    lists.append(count(df,str(ids)))\n    \n    name = i[\"name\"]\n    names.append(name)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"counting_dict = {}\nfor a,b in zip(names,lists):\n    counting_dict[a] = b","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for sub_categories in attributes:\n    count = 0\n    for sub_sub in attributes[sub_categories]:\n        count+=counting_dict[sub_sub]\n    print(count,sub_categories)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}