<jupyter_start><jupyter_text>DescriptionsThis notebook collects statistical distribution of all cropped images.<jupyter_code># Load libraries
import os
import json 
import random
import urllib
import time

from pycocotools.coco import COCO
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import PIL

import utils

%matplotlib inline
# Define image directory
projectDir=os.getcwd()
dataDir='..'
dataType='val2017'
imageDir='{}/images/'.format(dataDir)
annFile='{}/images/{}_selected/annotations/instances_{}.json'.format(dataDir,dataType,dataType)

coco=COCO(annFile)
obj_ids = coco.getAnnIds()<jupyter_output>loading annotations into memory...
Done (t=0.73s)
creating index...
index created!<jupyter_text>Load results<jupyter_code>def get_category_dictionary():
    catLblFile='../images/val2017_selected/annotations/coco-labels-paper.txt'
    f = open(catLblFile, "r")
    category_name = list()
    for cat in f:
        category_name.append(cat)
    cat_dict = {i: category_name[i - 1].strip("\n").replace(" ","") for i in range(1, len(category_name)+1)}
    return cat_dict
print(get_category_dictionary()[56])
print(get_category_dictionary())
def remove_space(c):
    if isinstance(c,str):
        return c.replace(" ","")
    pass

def convert_label(v):
    cat_dict = get_category_dictionary()
    return cat_dict[v]
df = pd.read_csv("new_result.csv")
df = df.drop(columns=['Unnamed: 0'])#, 'Unnamed: 0.1'])
df['pred_class'] = df['pred_class'].apply(remove_space)
df['true_class_label'] = df['true_class'].apply(convert_label)
df.info()
df.head()<jupyter_output><empty_output><jupyter_text>**Get all unique classes**<jupyter_code>print(len(df['true_class'].unique()))
print(sorted(df['true_class'].unique()))
a = pd.DataFrame()
for c in get_category_dictionary().keys():
    _df = df[df['true_class']==c]
    a = a.append({
        "true_class": get_category_dictionary()[c],
        "count": _df.shape[0]
    }, ignore_index=True)
a.sort_values('count', ascending=False)<jupyter_output><empty_output><jupyter_text>**relative true bbox size**<jupyter_code>def calculate_relative_bbox(bbox, img_size):
    bbox_x, bbox_y, bbox_w, bbox_h = map(int,bbox.strip('[]').split(', '))
    img_w, img_h = img_size
    return (bbox_w*bbox_h)/(img_w*img_h)*100

relative_size_true = []
for idx, item in df.iterrows():
    obj_ann = coco.loadAnns(int(item.obj_id))[0]
    img_ann = coco.loadImgs(obj_ann["image_id"])[0]
    im_width = img_ann['width']
    im_height = img_ann['height']
    relative_size = calculate_relative_bbox(item.true_bbox, (im_width, im_height))
    relative_size_true.append(relative_size)

df['relative_size_true'] = relative_size_true<jupyter_output><empty_output><jupyter_text>**relative predicted bbox size**<jupyter_code>def calculate_relative_bbox_predict(bbox_pred, img_size):
    if type(bbox_pred) is not str:
        return np.nan
    bbox_x, bbox_y, bbox_w, bbox_h = map(int,bbox_pred.strip('[]').split(', '))
    img_w, img_h = img_size
    return (bbox_w*bbox_h)/(img_w*img_h)*100

relative_size_pred = []
for idx, item in df.iterrows():
    obj_ann = coco.loadAnns(int(item.obj_id))[0]
    img_ann = coco.loadImgs(obj_ann["image_id"])[0]
    im_width = img_ann['width']
    im_height = img_ann['height']
    relative_size = calculate_relative_bbox_predict(item.pred_bbox, (im_width, im_height))
    relative_size_pred.append(relative_size)
df['relative_size_pred'] = relative_size_pred
df_centr = df[df.crop_type.str.contains("centr")]
df_top_L = df[df.crop_type.str.contains("top_L")]
df_top_R = df[df.crop_type.str.contains("top_R")]
df_bot_L = df[df.crop_type.str.contains("bot_L")]
df_bot_R = df[df.crop_type.str.contains("bot_R")]<jupyter_output><empty_output><jupyter_text>Analysis1. Count number of unidentified object2. Count number of misclassified object3. Top 5 object class per crop_type<jupyter_code># Count number of misclassified object
print("Count number of misclassified objects / detected objects")
print("top L:  ", df_top_L.shape[0] - df_top_L[df_top_L.pred_class == df_top_L.true_class_label].shape[0]-df_top_L.pred_class.isnull().sum(), "/", df_top_L.shape[0]-df_top_L.pred_class.isnull().sum())
print("bot L:  ", df_bot_L.shape[0] - df_bot_L[df_bot_L.pred_class == df_bot_L.true_class_label].shape[0]-df_bot_L.pred_class.isnull().sum(), "/", df_bot_L.shape[0]-df_bot_L.pred_class.isnull().sum())
print("center: ", df_centr.shape[0] - df_centr[df_centr.pred_class == df_centr.true_class_label].shape[0]-df_centr.pred_class.isnull().sum(), "/", df_centr.shape[0]-df_centr.pred_class.isnull().sum())
print("bot R:  ", df_bot_R.shape[0] - df_bot_R[df_bot_R.pred_class == df_bot_R.true_class_label].shape[0]-df_bot_R.pred_class.isnull().sum(), "/", df_bot_R.shape[0]-df_bot_R.pred_class.isnull().sum())
print("top R:  ", df_top_R.shape[0] - df_top_R[df_top_R.pred_class == df_top_R.true_class_label].shape[0]-df_top_R.pred_class.isnull().sum(), "/", df_top_R.shape[0]-df_top_R.pred_class.isnull().sum())
print("="*10); print()

# Percentage of misclassified objects
print("Percentage of misclassified objects")
print("top L:  ", 100.*(df_top_L.shape[0] - df_top_L[df_top_L.pred_class == df_top_L.true_class_label].shape[0]-df_top_L.pred_class.isnull().sum())/(df_top_L.shape[0]-df_top_L.pred_class.isnull().sum()), "%")
print("bot L:  ", 100.*(df_bot_L.shape[0] - df_bot_L[df_bot_L.pred_class == df_bot_L.true_class_label].shape[0]-df_bot_L.pred_class.isnull().sum())/(df_bot_L.shape[0]-df_bot_L.pred_class.isnull().sum()), "%")
print("center: ", 100.*(df_centr.shape[0] - df_centr[df_centr.pred_class == df_centr.true_class_label].shape[0]-df_centr.pred_class.isnull().sum())/(df_centr.shape[0]-df_centr.pred_class.isnull().sum()), "%")
print("bot R:  ", 100.*(df_bot_R.shape[0] - df_bot_R[df_bot_R.pred_class == df_bot_R.true_class_label].shape[0]-df_bot_R.pred_class.isnull().sum())/(df_bot_R.shape[0]-df_bot_R.pred_class.isnull().sum()), "%")
print("top R:  ", 100.*(df_top_R.shape[0] - df_top_R[df_top_R.pred_class == df_top_R.true_class_label].shape[0]-df_top_R.pred_class.isnull().sum())/(df_top_R.shape[0]-df_top_R.pred_class.isnull().sum()), "%")
# Count number of identified object
print("Count number of identified object")
print("top L:  ", df_top_L.shape[0]-df_top_L.pred_class.isnull().sum(),"/",df_top_L.shape[0])
print("bot L:  ", df_bot_L.shape[0]-df_bot_L.pred_class.isnull().sum(),"/",df_bot_L.shape[0])
print("center: ", df_centr.shape[0]-df_centr.pred_class.isnull().sum(),"/",df_centr.shape[0])
print("bot R:  ", df_bot_R.shape[0]-df_bot_R.pred_class.isnull().sum(),"/",df_bot_R.shape[0])
print("top R:  ", df_top_R.shape[0]-df_top_R.pred_class.isnull().sum(),"/",df_top_R.shape[0])
print("="*10); print()

# Percentage of recognizable objects
print("Percentage of recognizable objects")
print("top L:  ", 100.*(df_top_L.shape[0]-df_top_L.pred_class.isnull().sum())/df_top_L.shape[0],"%")
print("bot L:  ", 100.*(df_bot_L.shape[0]-df_bot_L.pred_class.isnull().sum())/df_bot_L.shape[0],"%")
print("center: ", 100.*(df_centr.shape[0]-df_centr.pred_class.isnull().sum())/df_centr.shape[0],"%")
print("bot R:  ", 100.*(df_bot_R.shape[0]-df_bot_R.pred_class.isnull().sum())/df_bot_R.shape[0],"%")
print("top R:  ", 100.*(df_top_R.shape[0]-df_top_R.pred_class.isnull().sum())/df_top_R.shape[0],"%")
print("="*10); print()
def top_5_class(df):
    """cat: "pred_class" or "true_class_label"
    """
    c_dict = get_category_dictionary()
    _df = pd.DataFrame()
    for c in c_dict.values():
        count1 = sum(df['pred_class']==c)
        count2 = sum(df['true_class_label']==c)
        _df = _df.append({
            'class': c,
            'count': count1,
            'total': count2
        }, ignore_index=True)
    return _df
crop_type = ['top_L','bot_L','centr','bot_R','top_R']
df_list = [df_top_L, df_bot_L, df_centr, df_bot_R, df_top_R]
for crop_type_label, df_type in zip(crop_type, df_list):
    print(crop_type_label)
    print(top_5_class(df_type).sort_values('count', ascending=False).reset_index().drop(columns='index').head(10))
    print("="*10)
crop_type = ['top_L','bot_L','centr','bot_R','top_R']
df_list = [df_top_L, df_bot_L, df_centr, df_bot_R, df_top_R]
for crop_type_label, df_type in zip(crop_type, df_list):
    print(crop_type_label)
    print(top_5_class(df_type).sort_values('total', ascending=False).reset_index().drop(columns='index').head(10))
    print("="*10)<jupyter_output>top_L
          class  count  total
0        person  203.0  322.0
1           car   25.0   41.0
2          bird    9.0   29.0
3         sheep    5.0   26.0
4  trafficlight   13.0   25.0
5        bottle    6.0   25.0
6        carrot    1.0   25.0
7         donut    3.0   24.0
8      elephant    8.0   24.0
9           cow    4.0   21.0
==========
bot_L
          class  count  total
0        person  225.0  322.0
1           car   25.0   41.0
2          bird    9.0   29.0
3         sheep   12.0   26.0
4  trafficlight    9.0   25.0
5        bottle    4.0   25.0
6        carrot    3.0   25.0
7         donut    2.0   24.0
8      elephant    8.0   24.0
9           cow    4.0   21.0
==========
centr
          class  count  total
0        person  227.0  322.0
1           car   25.0   41.0
2          bird   13.0   29.0
3         sheep   14.0   26.0
4  trafficlight   12.0   25.0
5        bottle    8.0   25.0
6        carrot    5.0   25.0
7         donut   10.0   24.0
8      elephant   12.0   24.0
[...]<jupyter_text>Prediction confidence comparison<jupyter_code>plt.rcParams['figure.figsize']=(8,8)
sns.boxplot(x='crop_type', y='pred', data=df, order=['top_L','bot_L','centr','bot_R','top_R'], showmeans=True, meanprops={"marker":"o","markerfacecolor":"white","markeredgecolor":"black","markersize":"10"})
sns.set(font_scale=2)
plt.title("Comparison in prediction confidence")
plt.ylabel("Prediction confidence")
plt.xlabel("Corner type")
plt.show()
df.dropna().groupby('crop_type')['pred','relative_size_pred','relative_size_true'].agg(['mean','median'])<jupyter_output><empty_output><jupyter_text>Ground truth vs. prediction bounding box size<jupyter_code>_df1 = df.dropna()

_df_groundtruth = pd.DataFrame()
_df_groundtruth['relative_size'] = _df1['relative_size_true']
_df_groundtruth['crop_type'] = _df1['crop_type']
_df_groundtruth['class'] = 'ground truth'

_df_prediction = pd.DataFrame()
_df_prediction['relative_size'] = _df1['relative_size_pred']
_df_prediction['crop_type'] = _df1['crop_type']
_df_prediction['class'] = 'prediction'

_df_analysis1 = pd.concat([_df_groundtruth, _df_prediction], axis=0, ignore_index=True)

plt.rcParams['figure.figsize']=(12,8)
sns.boxplot(x='crop_type', y='relative_size', data=_df_analysis1, order=['top_L','bot_L','centr','bot_R','top_R'], hue='class', showmeans=True, meanprops={"marker":"o","markerfacecolor":"white", "markeredgecolor":"black","markersize":"10"})
plt.legend(loc='upper right')
plt.xlabel('Corner type')
plt.ylabel('Bounding box size / Image resolution (%)')
plt.title('Bounding box size comparison')
plt.show()<jupyter_output><empty_output><jupyter_text>Classified vs. Misclassified objects<jupyter_code>is_NaN = df.isnull()
row_has_NaN = is_NaN.any(axis=1)
rows_with_NaN = df[row_has_NaN]
rows_with_value = df[~row_has_NaN]

_df_NaN = pd.DataFrame()
_df_NaN['relative_size'] = rows_with_NaN['relative_size_true']
_df_NaN['type'] = rows_with_NaN['crop_type']
_df_NaN['class'] = 'unidentified'

_df_value = pd.DataFrame()
_df_value['relative_size'] = rows_with_value['relative_size_true']
_df_value['type'] = rows_with_value['crop_type']
_df_value['class'] = 'identified'

_df_analysis2 = pd.concat([_df_value, _df_NaN], axis=0, ignore_index=True)

plt.rcParams['figure.figsize']=(12,8)
sns.boxplot(x='type', y='relative_size', data=_df_analysis2, order=['top_L','bot_L','centr','bot_R','top_R'], hue='class', showmeans=True, meanprops={"marker":"o","markerfacecolor":"white", "markeredgecolor":"black","markersize":"10"})
plt.legend(loc='upper right')
plt.xlabel('Corner type')
plt.ylabel('Bounding box size / Image resolution (%)')
plt.title('Bounding box size comparison')
plt.show()<jupyter_output><empty_output><jupyter_text>HTML visualisation<jupyter_code>def generate_html(name, df, skip_rows):
    html_file_name = f'{name}.html'
    f = open(html_file_name, 'wt', encoding='utf8')

    f.write('<!DOCTYPE html>\n')
    f.write('<html lang="en">\n')

    f.write('<head>\n')
    f.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n')
    f.write('</head>')

    f.write('<style>')
    f.write('.img-overlay-wrap {position: relative; display: inline-block; transition: transform 150ms ease-in-out;}')
    f.write('.img-overlay-wrap svg {position: absolute; top: 0; left: 0;}')
    f.write('</style>')

    f.write("<body>")

    f.write("<table border=1>")
    f.write("<tr>")
    f.write("<th>#obj_id</th>")
    f.write("<th>coco image</th>")
    f.write("</tr>")

    n_rows = df.shape[0]
    for i in range(0, n_rows, skip_rows):
        obj_id = int(df['obj_id'].values[i])
        obj_ann = coco.loadAnns(obj_id)[0]
        img_ann = coco.loadImgs(obj_ann['image_id'])[0]
        coco_url = img_ann['coco_url']
        cell_height = 200
        cell_width = img_ann['width']*1.0/img_ann['height']*cell_height
        bbox_x, bbox_y, bbox_w, bbox_h = obj_ann['bbox']
        rect_x = bbox_x/img_ann['width']*cell_width
        rect_y = bbox_y/img_ann['height']*cell_height
        rect_w = bbox_w/img_ann['width']*cell_width
        rect_h = bbox_h/img_ann['height']*cell_height

        s = "<tr>\n"
        s += "<td>[%d]</td>\n" % obj_id
        
        s += "<td width=250>\n"
        s += "<div class='img-overlay-wrap'>\n"
        s += "<img id='%d_imgf' src='%s' alt='%s' height='%d'/>" % (obj_id, coco_url, coco_url, cell_height)
        s += "<svg style='width:100%;height:100%;'>\n"
        s += "<rect x='%f' y='%f' width='%f' height='%f' style='fill:none;stroke:rgb(255,255,255);stroke-width:2'/>\n" % (rect_x, rect_y, rect_w, rect_h)
        s += "<text x='%s' y='%s' text-anchor='top' fill='white' font-size='14' font-color='rgb(255,255,255)'>%s</text>\n" % (rect_x, rect_y-5, get_category_dictionary()[obj_ann['category_id']])
        s += "</svg>\n"
        s += "</div>\n"
        s += "</td>\n"
        s += "</tr>\n"
        f.write(s)

    f.write("</table>")

    f.write("</body>")

    f.write("</html>")
    f.close()

    print(f"html link: file:///Users/haiho/PycharmProjects/yolov3_huynhngocanh/paper_followup/{html_file_name}")
generate_html("identified_centre", df_centr.dropna(), 1)
generate_html("identified_top_L", df_top_L.dropna(), 1)
generate_html("identified_top_R", df_top_R.dropna(), 1)
generate_html("identified_bot_L", df_bot_L.dropna(), 1)
generate_html("identified_bot_R", df_bot_R.dropna(), 1)
generate_html("unidentified_centre", df_centr[df_centr['pred'].isnull()], 1)
generate_html("unidentified_top_L", df_top_L[df_top_L['pred'].isnull()], 1)
generate_html("unidentified_top_R", df_top_R[df_top_R['pred'].isnull()], 1)
generate_html("unidentified_bot_L", df_bot_L[df_bot_L['pred'].isnull()], 1)
generate_html("unidentified_bot_R", df_bot_R[df_bot_R['pred'].isnull()], 1)<jupyter_output>html link: file:///Users/haiho/PycharmProjects/yolov3_huynhngocanh/paper_followup/unidentified_centre.html
html link: file:///Users/haiho/PycharmProjects/yolov3_huynhngocanh/paper_followup/unidentified_top_L.html
html link: file:///Users/haiho/PycharmProjects/yolov3_huynhngocanh/paper_followup/unidentified_top_R.html
html link: file:///Users/haiho/PycharmProjects/yolov3_huynhngocanh/paper_followup/unidentified_bot_L.html
html link: file:///Users/haiho/PycharmProjects/yolov3_huynhngocanh/paper_followup/unidentified_bot_R.html
{'fraction_non_alphanumeric': 0.17979102956167176, 'fraction_numerical': 0.026694699286442407, 'mean_word_length': 5.002676864244742, 'pattern_counts': {'":': 14, '<': 70, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '482767', 'license': 'MIT', 'n_tokens_mistral': 6479, 'n_tokens_neox': 6219, 'n_words': 874, 'path': 'paper_followup/method1_0_eval.ipynb', 'repo_name': 'hhodac/keras-yolo3', 'size': '15696'}