前面我们通过可视化手段看到每个类别中训练集图片数量是不均匀的,我们需要将数目最多和最少的10个类别找出来,进行相应的处理!代码实现如下:
import sys, os, multiprocessing, urllib3, csv
from PIL import Image
from io import BytesIO
from tqdm import tqdm
import json
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np
from numpy import *
import pylab
import heapq
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
key_class_list = []
count = {}
j = json.load(open('train.json'))
images = j['images']
for item in images:
label = item['class']
key_class_list.append((label))
print(type(key_class_list),'\n',len(key_class_list)) #打印出来数据信息(类型,长度)
#统计列表中最多最少的10数
print(Counter(key_class_list).most_common(10))
print(Counter(key_class_list).most_common()[:-11:-1])
打印结果如下: