{"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n#Libraries for plotting\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set(style=\"whitegrid\")\n\n\n#to count occurences of attributes in data\nfrom collections import Counter\n\n# Input data files are available in the \"../input/\" directory.\n\nimport os\n\n#to look at test and train images import\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Key insights:\n1. There are 3474 labels in total that are organized into following subclasses:     \n\n    * Countries: labels 0 to 99\n    * Cultures: labels 99 to 780\n    * Dimension: labels 781 to 785\n    * Medium: labels 786 to 2705\n    * Tag: 2706 to 3473\n   \n   Of these 3474 labels 3 aren't used on any of the training data     \n\n2. Not every artwork was assigned a country or culture by the experts and some artworks were assigned 2-3 cultures or countries\n\n3. The occurence of individual labels out of the subclasses grows exponentially towards some individual labels like \"*female*\" and \"*male*\" within *Tags*. However, the individual labels of the Dimension subclass were used with a similar frequency","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/imet-2020-fgvc7/train.csv\")\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"labels_df = pd.read_csv(\"../input/imet-2020-fgvc7/labels.csv\")\nlabels_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_path = \"../input/imet-2020-fgvc7/train/\" #all images are .png","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['Number of tags']= train_df['attribute_ids'].apply(lambda x:len(x.split(' ')))\n\ntags = sns.countplot(x='Number of tags',data=train_df ,palette='rocket')\nplt.ylabel('Number of images')\nplt.title(\"How many tags were used to describe image?\")\nsns.despine()\n\nfig_3 = tags.get_figure()\nfig_3.savefig(\"tag_num.png\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#this code line to separate the original attributes which where attribute_type::attribute_name\n#throws an error if run more than once so comment it out once the column has been split\n\nattributes = labels_df[\"attribute_name\"]\nlabels_df[[\"attribute_type\", \"attribute_name\"]] = attributes.str.split(\"::\", expand=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#this is meant to connect the attribute names with the numerical ids of the attributes in the training data for visualization purposes\n\n#this flattens the train.csv into a one dimensional array of all ids assigned to all images\nid_perImg = train_df[\"attribute_ids\"].to_numpy()\nids = []\nfor id_list in id_perImg:\n    id_list = id_list.split(\" \")\n    for id in id_list:\n        id = int(id)\n        ids.append(id)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#counts how many times each id appears in the flattened list\noccurences = Counter(ids)\nocc = sorted(occurences.items())\n\n#created a dataframe of the ids and count so you can merge it with the df containing meaningful names\nocc_df = pd.DataFrame(occ, columns=['attribute_id','count'])\ncounts_df = pd.merge(occ_df, labels_df, on='attribute_id', how= \"right\")\ncounts_df = counts_df.sort_values(by='attribute_id', ascending=True).set_index(\"attribute_id\")\n\n#occ_df has 3471 rows × 2 columns meaning that 3 attribute ids haven't been used to identify artworks\ncounts_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"top30_att = counts_df.sort_values(by=\"count\",ascending=False)\ntop30_att.iloc[0:30,0]\n\nsns.set(font_scale = 3)\nsns.set_style(\"whitegrid\")\n\nplt.subplots(figsize=(60,25))\nax = sns.barplot(y=top30_att.iloc[0:30,1],x=top30_att.iloc[0:30,0], palette='rocket')\nplt.ylabel('Attributes')\nplt.xlabel('Count')\nplt.title(\"The top 25 out of 3474 attributes used to label the images\")\nsns.despine()\nfig_all = ax.get_figure()\nfig_all.savefig(\"allLabels.png\",transparent=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Code segment to show how many times the attribute types where used to describe the artwork in the train data\n\nmedium_occ = counts_df.loc[counts_df['attribute_type'] == \"medium\", 'count'].sum()\ntag_occ = counts_df.loc[counts_df['attribute_type'] == \"tags\", 'count'].sum()\nculture_occ = counts_df.loc[counts_df['attribute_type'] == \"culture\", 'count'].sum()\ncountry_occ = counts_df.loc[counts_df['attribute_type'] == \"country\", 'count'].sum()\ndimension_occ = counts_df.loc[counts_df['attribute_type'] == \"dimension\", 'count'].sum()\n\noccurences_atypes = [[\"medium\",\"tags\", \"culture\",\"country\",\"dimension\"],[medium_occ, tag_occ, culture_occ, country_occ, dimension_occ]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.set(font_scale = 1)\nsns.set_style(\"whitegrid\")\n\n#2 plots to understand the attributes and their distribution better\ndist_att = sns.barplot(labels_df[\"attribute_type\"].value_counts().index, labels_df[\"attribute_type\"].value_counts(), palette='rocket')\ndist_att.set(xlabel=\"Grouped by attribute types\", ylabel='Number of existing attributes')\nplt.title(\"How many distinct attributes exist per attribute group?\")\nplt.show()\n\nfig_2 = dist_att.get_figure()\nfig_2.savefig(\"attribs.png\",transparent=True)\n\nocc = sns.barplot(occurences_atypes[0] ,occurences_atypes[1], palette='rocket')\nocc.set(xlabel=\"Grouped by attribute types\", ylabel='Occurence of attribute type in train set')\nplt.title(\"How often did experts assign different attribute types to the train set?\")\nplt.show()\nfig_3 = occ.get_figure()\nfig_3.savefig(\"occ_attribs.png\",transparent=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#This segment splits the ids into their subgroups for later visualization\n#Also in the previous year it was mentioned that the culture and country labels tend to be noisy and it might be good to train them seperately\n\ncountry_df = counts_df[counts_df.attribute_type.values == 'country']\nculture_df = counts_df[counts_df.attribute_type.values == 'culture']\ndimension_df = counts_df[counts_df.attribute_type.values == 'dimension']\nmedium_df = counts_df[counts_df.attribute_type.values == 'medium']\ntags_df = counts_df[counts_df.attribute_type.values == 'tags']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#explore if every artwork has a culture or country associated with it \n# countries are all ids between 0 and 99\n# cultures are all ids between 100 and 780\n\nall_ids = train_df.iloc[:,1].to_numpy()\nid_lists = [elem.split(\" \") for elem in all_ids]\ncountry_culture = [[el for el in id_list if int(el) < 781] for id_list in id_lists]\ncountry_culture[0:10]\n\n#judging by this not every artwork was assigned a culture or country label and some were assigned 2 or more culture/country labels therefore they are not mutually exclusive","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# This will be the segment for some bar charts so we can see how the labels are distributed\n# Ideally we could figure out how to display the count next to the bar so that it makes the scale difference from one image to the next more clear\nsns.set_style(\"whitegrid\")\n\ncountry_vis = country_df.sort_values(by=\"count\", ascending=False) \n\nplt.figure(figsize=(15, 5))\ntop20 = country_vis.head(n=10)\ncountry = sns.barplot(top20[\"count\"],top20[\"attribute_name\"], palette='rocket')\ncountry.set(xlabel=\"Count per label\", ylabel='Country label')\nplt.title(\"Top 10 assigned country labels out of 100 total\")\nplt.show()\n\nfig_4 = country.get_figure()\nfig_4.savefig(\"occ_countries.png\",transparent=True)\n\n# I separated it into 2 segments so you could see what counts the bottom 50 have\n# Since they were so much smaller quantities than in the top 30 there seemed to be no occurences at all in a plot showing all at once\n#rest = country_vis.tail(n=80)\n#plt.figure(figsize=(16, 20))\n#rest = sns.barplot(rest[\"count\"],rest[\"attribute_name\"] )\n#rest.set(xlabel=\"Count of label\", ylabel='Country label')\n#rest.xaxis.set_label_position('top') \n#rest.xaxis.tick_top()\n#plt.title(\"The rest of the counts are far smaller \")\n#plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"culture_vis = culture_df.sort_values(by=\"count\", ascending=False)\nplt.figure(figsize=(16, 7))\ntop20 = culture_vis.head(n=15)\nculture = sns.barplot(top20[\"count\"],top20[\"attribute_name\"], palette='rocket')\nculture.set(xlabel=\"Count per label\", ylabel='Culture label')\nplt.title(\"Top 15 assigned culture labels out of 681 cultures\")\nplt.show()\n\nfig_5 = culture.get_figure()\nfig_5.savefig(\"occ_cultures.png\",transparent=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dimension_vis = dimension_df.sort_values(by=\"count\", ascending=False)\n\ndim = sns.barplot(dimension_vis[\"attribute_name\"],dimension_vis[\"count\"], palette='rocket')\ndim.set(xlabel='Dimension labels', ylabel=\"Count per Dimension\")\nplt.title(\"All Assigned dimension labels\")\nplt.show()\n\nfig_6 = dim.get_figure()\nfig_6.savefig(\"occ_dim.png\",transparent=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"medium_vis = medium_df.sort_values(by=\"count\", ascending=False)\nplt.figure(figsize=(16, 7))\ntop20_m = medium_vis.head(n=15)\nmed = sns.barplot(top20_m[\"count\"],top20_m[\"attribute_name\"], palette='rocket')\nmed.set(xlabel=\"Count per label\", ylabel='Medium label')\nplt.title(\"Top 15 assigned medium labels from 1920 labels\")\nplt.show()\n\nfig_7 = med.get_figure()\nfig_7.savefig(\"occ_med.png\",transparent=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tags_vis = tags_df.sort_values(by=\"count\", ascending=False)\nplt.figure(figsize=(16, 7))\ntop20_t = tags_vis.head(n=15)\ntags = sns.barplot(top20_t[\"count\"],top20_t[\"attribute_name\"], palette='rocket')\ntags.set(xlabel=\"Count of label\", ylabel='Tag label')\nplt.title(\"Top 15 assigned tags from 768 total tags\")\nplt.show()\n\nfig_8 = tags.get_figure()\nfig_8.savefig(\"occ_tags.png\",transparent=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#visualize test images\ncount = 1\nplt.figure(figsize=[16,16])\n\nfor img_name in os.listdir(\"../input/imet-2020-fgvc7/test/\")[:16]:\n    img = cv2.imread(\"../input/imet-2020-fgvc7/test/{}\".format(img_name))[...,[2,1,0]]\n    plt.subplot(4,4,count)\n    plt.imshow(img)\n    plt.title(\"test image {}\".format(count))\n    count += 1\nplt.show();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#visualize train images\nsns.set_style('white')\nplt.figure(figsize=[22,20])\ncount=1\nfor img_name in os.listdir('../input/imet-2020-fgvc7/train/')[:36]:\n    img = cv2.imread('../input/imet-2020-fgvc7/train/%s'%img_name)\n    plt.subplot(6,6,count)\n    plt.imshow(img)\n    plt.title('Item %s'%count)\n    count+=1","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}