<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          使用Python分析姿態(tài)估計(jì)數(shù)據(jù)集COCO的教程

          共 23045字,需瀏覽 47分鐘

           ·

          2021-06-24 19:15

          點(diǎn)擊上方小白學(xué)視覺”,選擇加"星標(biāo)"或“置頂

          重磅干貨,第一時(shí)間送達(dá)

          本文轉(zhuǎn)自:AI算法與圖像處理

          當(dāng)我們訓(xùn)練姿勢(shì)估計(jì)模型,比較常用的數(shù)據(jù)集包括像COCO、MPII和CrowdPose這樣的公共數(shù)據(jù)集,但如果我們將其與不同計(jì)算機(jī)視覺任務(wù)(如對(duì)象檢測(cè)或分類)的公共可用數(shù)據(jù)集的數(shù)量進(jìn)行比較,就會(huì)發(fā)現(xiàn)可用的數(shù)據(jù)集并不多。
          姿態(tài)估計(jì)問題屬于一類比較復(fù)雜的問題,為神經(jīng)網(wǎng)絡(luò)模型建立一個(gè)合適的數(shù)據(jù)集是很困難的,圖像中每個(gè)人的每個(gè)關(guān)節(jié)都必須定位和標(biāo)記,這是一項(xiàng)瑣碎而費(fèi)時(shí)的任務(wù)。
          最流行的姿態(tài)估計(jì)數(shù)據(jù)集是COCO數(shù)據(jù)集,它有大約80類圖像和大約250000個(gè)人物實(shí)例。
          如果你檢查此數(shù)據(jù)集中的一些隨機(jī)圖像,你可能會(huì)遇到一些與要解決的問題無關(guān)的實(shí)例。學(xué)術(shù)界希望達(dá)到最高的精度,但在實(shí)際生產(chǎn)環(huán)境中并不總是如此。
          在現(xiàn)實(shí)世界中,我們可能更感興趣的是在非常特定的環(huán)境中工作良好的模型,例如行人、籃球運(yùn)動(dòng)員、健身房等。
          讓我們從COCO數(shù)據(jù)集中查看此圖像:
          你看到紅點(diǎn)了嗎?這是關(guān)鍵點(diǎn):鼻子。
          有時(shí),你可能不希望網(wǎng)絡(luò)看到僅包含頭部一部分的示例,尤其是在幀的底部。
          在這篇文章中,我會(huì)向你展示COCO數(shù)據(jù)集的一個(gè)示例分析
          COCO數(shù)據(jù)集


          COCO數(shù)據(jù)集是用于許多計(jì)算機(jī)視覺任務(wù)的大規(guī)模通用數(shù)據(jù)集。150萬個(gè)對(duì)象實(shí)例,80個(gè)對(duì)象類別,25萬人——這些都讓這個(gè)數(shù)據(jù)集令人印象深刻。你可以在源站點(diǎn)上找到更多詳細(xì)信息,在那里你還可以下載所有必需的文件:https://cocodataset.org/
          數(shù)據(jù)集由圖像文件和注釋文件組成。注釋文件是一個(gè)JSON,包含關(guān)于一個(gè)人(或其他一些類別)的所有元數(shù)據(jù)。在這里我們會(huì)找到邊界框的位置和大小,區(qū)域,關(guān)鍵點(diǎn),源圖像的文件名等。
          我們不必手動(dòng)解析JSON。有一個(gè)方便的Python庫(kù)可用使用,即pycocotools(https://github.com/cocodataset/cocoapi/tree/master/PythonAPI)
          我們需要train2017.zip(https://cocodataset.org/#download),val2017.zip(https://cocodataset.org/#download),annotations_trainval2017.zip(https://cocodataset.org/#download)
          具體來說,我們只需要人的注釋。zip中有兩個(gè)文件:annotations_trainval2017.zip:person_keypoints_train2017.json和person_keypoints_val2017.json
          我建議將文件放在以下這個(gè)文件夾層次結(jié)構(gòu)中:
          dataset_coco
             |---annotations
                   |---person_keypoints_train2017.json
                   |---person_keypoints_val2017.json
             |---train2017
                   |---*.jpg
             |---val2017
                   |---*.jpg
          下面是顯示如何加載注釋的代碼:
          from pycocotools.coco import COCO
          ...

          train_annot_path = 'dataset_coco/annotations  /person_keypoints_train2017.json'
          val_annot_path = 'dataset_coco/annotations/person_keypoints_val2017.json'
          train_coco = COCO(train_annot_path) # 加載訓(xùn)練集的注釋
          val_coco = COCO(val_annot_path) # 加載驗(yàn)證集的注釋
          ...
          # 函數(shù)遍歷一個(gè)人的所有數(shù)據(jù)庫(kù)并逐行返回相關(guān)數(shù)據(jù)
          def get_meta(coco):
              ids = list(coco.imgs.keys())
              for i, img_id in enumerate(ids):
                  img_meta = coco.imgs[img_id]
                  ann_ids = coco.getAnnIds(imgIds=img_id)
                  # 圖像的基本參數(shù)
                  img_file_name = img_meta['file_name']
                  w = img_meta['width']
                  h = img_meta['height']
                  # 檢索當(dāng)前圖像中所有人的元數(shù)據(jù)
                  anns = coco.loadAnns(ann_ids)

                  yield [img_id, img_file_name, w, h, anns]

          ...

          # 迭代圖像
          for img_id, img_fname, w, h, meta in get_meta(train_coco):
              ...
              # 遍歷圖像的所有注釋
              for m in meta:
                  # m是字典
                  keypoints = m['keypoints']
                  ...
          ...
          首先,我們必須加載COCO對(duì)象,它是json數(shù)據(jù)的包裝器(第6-7行)
          在第11行,我們加載所有圖像標(biāo)識(shí)符。
          在接下來的幾行中,我們?yōu)槊總€(gè)圖像加載元數(shù)據(jù),這是一個(gè)包含圖像寬度、高度、名稱、許可證等一般信息的詞典。
          在第14行,我們加載給定圖像的注釋元數(shù)據(jù),這是一個(gè)字典列表,每個(gè)字典代表一個(gè)人。
          第27-32行顯示了如何加載整個(gè)訓(xùn)練集(train_coco),類似地,我們可以加載驗(yàn)證集(val_coco)
          將COCO轉(zhuǎn)換為Pandas數(shù)據(jù)幀


          讓我們將COCO元數(shù)據(jù)轉(zhuǎn)換為pandas數(shù)據(jù)幀,我們使用如matplotlib、sklearn 和pandas。

          這可用使得數(shù)據(jù)的過濾、可視化和操作變得更加容易,此外,我們還可以將數(shù)據(jù)導(dǎo)出為csv或parquet等。
          def convert_to_df(coco):
              images_data = []
              persons_data = []
              
              # 遍歷所有圖像
              for img_id, img_fname, w, h, meta in get_meta(coco):
                  images_data.append({
                      'image_id': int(img_id),
                      'path': img_fname,
                      'width': int(w),
                      'height': int(h)
                  })
                  
                  # 遍歷所有元數(shù)據(jù)
                  for m in meta:
                      persons_data.append({
                          'image_id': m['image_id'],
                          'is_crowd': m['iscrowd'],
                          'bbox': m['bbox'],
                          'area': m['area'],
                          'num_keypoints': m['num_keypoints'],
                          'keypoints': m['keypoints'],
                      })
                      
              # 創(chuàng)建帶有圖像路徑的數(shù)據(jù)幀
              images_df = pd.DataFrame(images_data)
              images_df.set_index('image_id', inplace=True)
              
              # 創(chuàng)建與人相關(guān)的數(shù)據(jù)幀
              persons_df = pd.DataFrame(persons_data)
              persons_df.set_index('image_id', inplace=True)
              return images_df, persons_df
          我們使用get_meta函數(shù)構(gòu)造兩個(gè)數(shù)據(jù)幀—一個(gè)用于圖像路徑,另一個(gè)用于人的元數(shù)據(jù)。在一個(gè)圖像中可能有多個(gè)人,因此是一對(duì)多的關(guān)系。
          在下一步中,我們合并兩個(gè)表(left join操作)并將訓(xùn)練集和驗(yàn)證集組合,另外,我們添加了一個(gè)新列source,值為0表示訓(xùn)練集,值為1表示驗(yàn)證集。
          這樣的信息是必要的,因?yàn)槲覀冃枰缿?yīng)該在哪個(gè)文件夾中搜索圖像。如你所知,這些圖像位于兩個(gè)文件夾中:train2017/和val2017/
          images_df, persons_df = convert_to_df(train_coco)
          train_coco_df = pd.merge(images_df, persons_df, right_index=True, left_index=True)
          train_coco_df['source'] = 0

          images_df, persons_df = convert_to_df(val_coco)
          val_coco_df = pd.merge(images_df, persons_df, right_index=True, left_index=True)
          val_coco_df['source'] = 1

          coco_df = pd.concat([train_coco_df, val_coco_df], ignore_index=True)
          最后,我們有一個(gè)表示整個(gè)COCO數(shù)據(jù)集的數(shù)據(jù)幀。
          圖像中有多少人


          現(xiàn)在我們可以執(zhí)行第一個(gè)分析。
          COCO數(shù)據(jù)集包含多個(gè)人的圖像,我們想知道有多少圖像只包含一個(gè)人。
          代碼如下:
          # 計(jì)數(shù)

          annotated_persons_df = coco_df[coco_df['is_crowd'] == 0]
          crowd_df = coco_df[coco_df['is_crowd'] == 1]

          print("Number of people in total: " + str(len(annotated_persons_df)))
          print("Number of crowd annotations: " + str(len(crowd_df)))

          persons_in_img_df = pd.DataFrame({
              'cnt': annotated_persons_df['path'].value_counts()
          })
          persons_in_img_df.reset_index(level=0, inplace=True)
          persons_in_img_df.rename(columns = {'index':'path'}, inplace = True)

          # 按cnt分組,這樣我們就可以在一張圖片中得到帶有注釋人數(shù)的數(shù)據(jù)幀

          persons_in_img_df = persons_in_img_df.groupby(['cnt']).count()

          # 提取數(shù)組

          x_occurences = persons_in_img_df.index.values
          y_images = persons_in_img_df['path'].values

          # 繪圖

          plt.bar(x_occurences, y_images)
          plt.title('People on a single image ')
          plt.xticks(x_occurences, x_occurences)
          plt.xlabel('Number of people in a single image')
          plt.ylabel('Number of images')
          plt.show()
          結(jié)果圖表:
          如你所見,大多數(shù)COCO圖片都包含一個(gè)人。
          但也有相當(dāng)多的13個(gè)人的照片,讓我們舉幾個(gè)例子:
          好吧,甚至有一張圖片有19個(gè)注解(非人群):
          這個(gè)圖像的頂部區(qū)域不應(yīng)該標(biāo)記為一個(gè)人群?jiǎn)幔?/span>
          是的,應(yīng)該,但是,我們有多個(gè)沒有關(guān)鍵點(diǎn)的邊界框!這樣的注釋應(yīng)該像對(duì)待人群一樣對(duì)待,這意味著它們應(yīng)該被屏蔽。
          在這張圖片中,只有中間的3個(gè)方框有一些關(guān)鍵點(diǎn)。
          讓我們來優(yōu)化查詢,以獲取包含有/沒有關(guān)鍵點(diǎn)的人圖像的統(tǒng)計(jì)信息,以及有/沒有關(guān)鍵點(diǎn)的人的總數(shù):
          annotated_persons_nokp_df = coco_df[(coco_df['is_crowd'] == 0) & (coco_df['num_keypoints'] == 0)]
          annotated_persons_kp_df = coco_df[(coco_df['is_crowd'] == 0) & (coco_df['num_keypoints'] > 0)]

          print("Number of people (with keypoints) in total: " +
                  str(len(annotated_persons_kp_df)))
          print("Number of people without any keypoints in total: " +
                  str(len(annotated_persons_nokp_df)))

          persons_in_img_kp_df = pd.DataFrame({
              'cnt': annotated_persons_kp_df[['path','source']].value_counts()
          })
          persons_in_img_kp_df.reset_index(level=[0,1], inplace=True)
          persons_in_img_cnt_df = persons_in_img_kp_df.groupby(['cnt']).count()
          x_occurences_kp = persons_in_img_cnt_df.index.values
          y_images_kp = persons_in_img_cnt_df['path'].values

          f = plt.figure(figsize=(148))
          width = 0.4
          plt.bar(x_occurences_kp, y_images_kp, width=width, label='with keypoints')
          plt.bar(x_occurences + width, y_images, width=width, label='no keypoints')

          plt.title('People on a single image ')
          plt.xticks(x_occurences + width/2, x_occurences)
          plt.xlabel('Number of people in a single image')
          plt.ylabel('Number of images')
          plt.legend(loc = 'best')
          plt.show()
          現(xiàn)在我們可以看到區(qū)別是明顯的。
          雖然COCO官方頁面上描述有25萬人擁有關(guān)鍵點(diǎn),而我們只有156165個(gè)這樣的例子。
          他們可能應(yīng)該刪除了“帶關(guān)鍵點(diǎn)”這幾個(gè)字。
          添加額外列
          一旦我們將COCO轉(zhuǎn)換成pandas數(shù)據(jù)幀,我們就可以很容易地添加額外的列,從現(xiàn)有的列中計(jì)算出來。
          我認(rèn)為最好將所有的關(guān)鍵點(diǎn)坐標(biāo)提取到單獨(dú)的列中,此外,我們可以添加一個(gè)具有比例因子的列。
          特別是,關(guān)于一個(gè)人的邊界框的規(guī)模信息是非常有用的,例如,我們可能希望丟棄所有太小規(guī)模的人,或者執(zhí)行放大操作。
          為了實(shí)現(xiàn)這個(gè)目標(biāo),我們使用Python庫(kù)sklearn中的transformer對(duì)象。
          一般來說,sklearn transformers是用于清理、減少、擴(kuò)展和生成數(shù)據(jù)科學(xué)模型中的特征表示的強(qiáng)大工具。我們只會(huì)用一小部分的api。
          代碼如下:
          from sklearn.base import BaseEstimator, TransformerMixin

          class AttributesAdder(BaseEstimator, TransformerMixin):
              def __init__(self, num_keypoints, w_ix, h_ix, bbox_ix, kp_ix):
                  """
                  :param num_keypoints: 關(guān)鍵點(diǎn)的數(shù)量
                  :param w_ix: 包含圖像寬度的列索引
                  :param h_ix: 包含圖像高度的列索引
                  :param bbox_ix: 包含邊框數(shù)據(jù)的列索引
                  :param kp_ix: 包含關(guān)鍵點(diǎn)數(shù)據(jù)的列索引
                  """

                  self.num_keypoints = num_keypoints
                  self.w_ix = w_ix
                  self.h_ix = h_ix
                  self.bbox_ix = bbox_ix
                  self.kp_ix = kp_ix
                  
              def fit(self, X, y=None):
                  return self 
                
              def transform(self, X):
                
                  # 檢索特定列
                  
                  w = X[:, self.w_ix]
                  h = X[:, self.h_ix]
                  bbox = np.array(X[:, self.bbox_ix].tolist())  # to matrix
                  keypoints = np.array(X[:, self.kp_ix].tolist()) # to matrix
                  
                  # 計(jì)算邊框的比例因子
                  
                  scale_x = bbox[:,2] / w
                  scale_y = bbox[:,3] / h
                  aspect_ratio = w / h
                  
                  # 計(jì)算規(guī)模類別
                  
                  scale_cat = pd.cut(scale_y,
                      bins=[0.0.40.60.8, np.inf],
                      labels=['S''M''L''XL'])
                  
                  return np.c_[X, scale_x, scale_y, scale_cat, aspect_ratio, keypoints]
                
                
          # 用于添加新列的transformer對(duì)象

          attr_adder = AttributesAdder(num_keypoints=17, ...)
          coco_extra_attribs = attr_adder.transform(coco_df.values)

          # 創(chuàng)建列發(fā)新列表

          keypoints_cols = [['x'+str(idx), 'y'+str(idx), 'v'+str(idx)]
                                  for idx, k in enumerate(range(num_keypoints))]
          keypoints_cols = np.concatenate(keypoints_cols).tolist()

          # 創(chuàng)建新的更豐富的數(shù)據(jù)z幀

          coco_extra_attribs_df = pd.DataFrame(
              coco_extra_attribs,
              columns=list(coco_df.columns) +
                  ["scale_x""scale_y""scale_cat""aspect_ratio"] +
                  keypoints_cols,
              index=coco_df.index)
          38行代碼,我們?yōu)槊恳恍兄付ㄒ?guī)模類別(S、M、L或XL)。計(jì)算方法如下:
          • 如果scale_y在[0–0.4)范圍內(nèi),則類別為S
          • 如果scale_y在[0.4–0.6)范圍內(nèi),則類別為M
          • 如果scale_y在[0.6–0.8)范圍內(nèi),則類別為L(zhǎng)
          • 如果scale_y在[0.8–1.0)范圍內(nèi),則類別為XL
          在第42行中,我們將原始列與新列進(jìn)行合并。
          第28行我們將關(guān)鍵點(diǎn)擴(kuò)展到單獨(dú)的列中。COCO數(shù)據(jù)集中的關(guān)鍵點(diǎn)數(shù)據(jù)由一個(gè)一維列表表示:[x0,y0,v0,x1,y1,…],我們可以把這個(gè)列轉(zhuǎn)換成一個(gè)矩陣:[num of rows]x[num of keypoints*3],然后,我們可以不需要任何額外的努力就可以返回它(第42行)。
          最后,我們創(chuàng)建一個(gè)新的數(shù)據(jù)幀(第58-63行)
          鼻子在哪里?


          我們通過檢查圖像中頭部位置的分布來找到鼻子的坐標(biāo),然后在標(biāo)準(zhǔn)化的二維圖表中畫一個(gè)點(diǎn)。
          呈現(xiàn)此圖表的代碼如下:
          # 對(duì)水平圖像進(jìn)行關(guān)鍵點(diǎn)坐標(biāo)標(biāo)準(zhǔn)化

          horiz_imgs_df = coco_extra_attribs_df[coco_extra_attribs_df['aspect_ratio'] >= 1.]

          # 獲取平均寬度和高度-用于縮放關(guān)鍵點(diǎn)坐標(biāo)

          avg_w = int(horiz_imgs_df['width'].mean())
          avg_h = int(horiz_imgs_df['height'].mean())

          class NoseAttributesAdder(BaseEstimator, TransformerMixin):
              def __init__(self, avg_w, avg_h, w_ix, h_ix, x1_ix, y1_ix, v1_ix):
                  self.avg_w = avg_w
                  self.avg_h = avg_h
                  self.w_ix = w_ix
                  self.h_ix = h_ix
                  self.x1_ix = x1_ix
                  self.y1_ix = y1_ix
                  self.v1_ix = v1_ix

                  def fit(self, X, y=None):
                  return self 

                def transform(self, X):
                  w = X[:, self.w_ix]
                  h = X[:, self.h_ix]
                  x1 = X[:, self.x1_ix]
                  y1 = X[:, self.y1_ix]

                  # 標(biāo)準(zhǔn)化鼻子坐標(biāo),提供平均寬度和高度

                  scale_x = self.avg_w / w
                  scale_y = self.avg_h / h
                  nose_x = x1 * scale_x
                  nose_y = y1 * scale_y
                  
                  return np.c_[X, nose_x, nose_y]

                # 用于標(biāo)準(zhǔn)化鼻子坐標(biāo)列的transformer對(duì)象

          w_ix = horiz_imgs_df.columns.get_loc('width')
          h_ix = horiz_imgs_df.columns.get_loc('height')
          x1_ix = horiz_imgs_df.columns.get_loc('x0')  # 鼻子的x坐標(biāo)在'x0'列中
          y1_ix = horiz_imgs_df.columns.get_loc('y0')  # 鼻子的y坐標(biāo)在'y0'列中
          v1_ix = horiz_imgs_df.columns.get_loc('v0')  # 鼻頭的可見性

          attr_adder = NoseAttributesAdder(avg_w, avg_h, w_ix, h_ix, x1_ix, y1_ix, v1_ix)
          coco_noses = attr_adder.transform(horiz_imgs_df.values)

          # 使用標(biāo)準(zhǔn)化的數(shù)據(jù)創(chuàng)建新數(shù)據(jù)幀

          coco_noses_df = pd.DataFrame(
              coco_noses,
              columns=list(horiz_imgs_df.columns) + ["normalized_nose_x""normalized_nose_y"],
              index=horiz_imgs_df.index)

          # 過濾-只有可見的鼻子

          coco_noses_df = coco_noses_df[coco_noses_df["v0"] == 2]

          coco_noses_df.plot(kind="scatter", x="normalized_nose_x",
                             y="normalized_nose_y", alpha=0.3).invert_yaxis()
          與前面一樣,我們使用一個(gè)轉(zhuǎn)換器來添加新列。
          COCO數(shù)據(jù)集包含不同寬度和高度的圖像,我們必須標(biāo)準(zhǔn)化每個(gè)圖像中鼻子的x,y坐標(biāo),這樣我們就能在輸出圖表中畫出代表鼻子的點(diǎn)。
          我們首先確定所有圖像的平均寬度和高度(第7-8行)這里我們可以使用任何值,因?yàn)樗挥糜诖_定比例因子。
          在第40-44行,我們從dataframe中找到所需列的索引。
          隨后,我們執(zhí)行轉(zhuǎn)換(第46-47行)并創(chuàng)建一個(gè)新的數(shù)據(jù)幀,其中包含新的列normalized_nose_x和normalized_nose_y(第51-55行)
          最后一行繪制二維圖表。
          現(xiàn)在我們可以檢查一些圖像,例如,我們想檢查一些頭部位置非常接近圖像底邊的圖像,為了實(shí)現(xiàn)這一點(diǎn),我們通過列normalized_nose_y來過濾數(shù)據(jù)幀
          low_noses_df = coco_noses_df[coco_noses_df['normalized_nose_y'] > 430 ]
          low_noses_df
          以下是滿足此條件的示例圖像:


          關(guān)鍵點(diǎn)數(shù)量


          具有特定數(shù)量關(guān)鍵點(diǎn)的邊界框的數(shù)量是附加的有用信息。
          為什么要邊界框?
          邊界框有一個(gè)特殊的標(biāo)志iscrowd,用來確定內(nèi)容是應(yīng)該作為一個(gè)群組(沒有關(guān)鍵點(diǎn))還是一個(gè)人(應(yīng)該有關(guān)鍵點(diǎn))。一般來說,iscrowd是為包含許多人的小實(shí)例(例如網(wǎng)球比賽中的觀眾)的邊界框設(shè)置的。
          y_images = coco_extra_attribs_df['num_keypoints'].value_counts()
          x_keypoints = y_images.index.values

          # 繪圖

          plt.figsize=(10,5)
          plt.bar(x_keypoints, y_images)
          plt.title('Histogram of keypoints')
          plt.xticks(x_keypoints)
          plt.xlabel('Number of keypoints')
          plt.ylabel('Number of bboxes')
          plt.show()

          # 帶有若干關(guān)鍵點(diǎn)(行)的bboxes(列)百分比

          kp_df = pd.DataFrame({
              "Num keypoints %": coco_extra_attribs_df[
                                     "num_keypoints"].value_counts() / len(coco_extra_attribs_df)
          }).sort_index()
          如你所見,在表中顯示相同的信息非常容易:
          規(guī)模


          這是迄今為止最有價(jià)值的指標(biāo)。
          訓(xùn)練姿態(tài)估計(jì)深度神經(jīng)網(wǎng)絡(luò)模型對(duì)樣本中人的規(guī)模變化非常敏感,提供一個(gè)平衡的數(shù)據(jù)集是非常關(guān)鍵的,否則,模型可能會(huì)偏向于一個(gè)更具優(yōu)勢(shì)的規(guī)模。
          你還記得一個(gè)額外的屬性scale_cat嗎?現(xiàn)在我們要好好利用它。
          代碼:
          persons_df = coco_extra_attribs_df[coco_extra_attribs_df['num_keypoints'] > 0]
          persons_df['scale_cat'].hist()
          可以呈現(xiàn)以下圖表:
          我們清楚地看到,COCO數(shù)據(jù)集包含了很多小人物——不到圖像總高度的40%。我們把它放到表格中:
          scales_props_df = pd.DataFrame({
              "Scales": persons_df["scale_cat"].value_counts() / len(persons_df)
          })
          scales_props_df


          COCO數(shù)據(jù)集的分層抽樣


          首先,分層抽樣定義為當(dāng)我們將整個(gè)數(shù)據(jù)集劃分為訓(xùn)練集/驗(yàn)證集等時(shí),我們希望確保每個(gè)子集包含相同比例的特定數(shù)據(jù)組。
          假設(shè)我們有1000人,男性占57%,女性占43%。我們不能只為訓(xùn)練集和驗(yàn)證集選取隨機(jī)數(shù)據(jù),因?yàn)樵谶@些數(shù)據(jù)子集中,一個(gè)組可能會(huì)被低估。,我們必須從57%的男性和43%的女性中按比例選擇。
          換句話說,分層抽樣在訓(xùn)練集和驗(yàn)證集中保持了57%的男性/43%的女性的比率。
          同樣,我們可以檢查COCO訓(xùn)練集和驗(yàn)證集中是否保持了不同規(guī)模的比率。
          persons_df = coco_extra_attribs_df[coco_extra_attribs_df['num_keypoints'] > 0]
          train_df = persons_df[persons_df['source'] == 0]
          val_df = persons_df[persons_df['source'] == 1]

          scales_props_df = pd.DataFrame({
              "Scales in train set %": train_df["scale_cat"].value_counts() / len(train_df),
              "Scales in val set %": val_df["scale_cat"].value_counts() / len(val_df)
          })
          scales_props_df["Diff 100%"] = 100 * \
              np.absolute(scales_props_df["Scales in train set %"] -
                          scales_props_df["Scales in val set %"])
          在第2-3行,我們將數(shù)據(jù)幀拆分為訓(xùn)練集和驗(yàn)證集的單獨(dú)數(shù)據(jù)幀,這與我們分別從person_keypoints_train2017.json和person_keypoints_val2017.json加載數(shù)據(jù)幀相同。
          接下來,我們用訓(xùn)練集和驗(yàn)證集中每個(gè)規(guī)模組的基數(shù)創(chuàng)建一個(gè)新的數(shù)據(jù)幀,此外,我們添加了一個(gè)列,其中包含兩個(gè)數(shù)據(jù)集之間差異的百分比。
          結(jié)果如下:
          如我們所見,COCO數(shù)據(jù)集的分層非常好,訓(xùn)練集和驗(yàn)證集中的規(guī)模組之間只有很小的差異(1-2%)。
          現(xiàn)在,讓我們檢查不同的組-邊界框中關(guān)鍵點(diǎn)的數(shù)量。
          train_df = coco_extra_attribs_df[coco_extra_attribs_df['source'] == 0]
          val_df = coco_extra_attribs_df[coco_extra_attribs_df['source'] == 1]

          kp_props_df = pd.DataFrame({
              "Num keypoints in train set %": train_df["num_keypoints"].value_counts() / 
              len(train_df),
              "Num keypoints in val set %": val_df["num_keypoints"].value_counts() /
              len(val_df)
          }).sort_index()

          kp_props_df["Diff 100%"] = 100 * \
              np.absolute(kp_props_df["Num keypoints in train set %"] -
                          kp_props_df["Num keypoints in val set %"])
          類似地,我們看到關(guān)鍵點(diǎn)的數(shù)量在COCO訓(xùn)練和驗(yàn)證集中是相等的,這很好!
          現(xiàn)在,你可以將所有數(shù)據(jù)集(MPII、COCO)合并到一個(gè)包中,然后自己進(jìn)行拆分,有一個(gè)很好的sklearn類:StratifiedShuffleSplit可用做這個(gè)事情。

          總結(jié)

          在本文中,分析了COCO數(shù)據(jù)集的結(jié)構(gòu),了解其中的內(nèi)容可以幫助你更好地決定增加或丟棄一些不相關(guān)的樣本。
          分析可以在Jupyter notebook上進(jìn)行。
          從COCO數(shù)據(jù)集中展示了一些或多或少有用的指標(biāo),比如圖像中人的分布、人的邊界框的規(guī)模、某些特定身體部位的位置。
          最后,描述了驗(yàn)證集分層的過程。
          github倉(cāng)庫(kù)鏈接:https://github.com/michalfaber/dataset_toolkit
          下載1:OpenCV-Contrib擴(kuò)展模塊中文版教程
          在「小白學(xué)視覺」公眾號(hào)后臺(tái)回復(fù):擴(kuò)展模塊中文教程,即可下載全網(wǎng)第一份OpenCV擴(kuò)展模塊教程中文版,涵蓋擴(kuò)展模塊安裝、SFM算法、立體視覺、目標(biāo)跟蹤、生物視覺、超分辨率處理等二十多章內(nèi)容。

          下載2:Python視覺實(shí)戰(zhàn)項(xiàng)目52講
          小白學(xué)視覺公眾號(hào)后臺(tái)回復(fù):Python視覺實(shí)戰(zhàn)項(xiàng)目,即可下載包括圖像分割、口罩檢測(cè)、車道線檢測(cè)、車輛計(jì)數(shù)、添加眼線、車牌識(shí)別、字符識(shí)別、情緒檢測(cè)、文本內(nèi)容提取、面部識(shí)別等31個(gè)視覺實(shí)戰(zhàn)項(xiàng)目,助力快速學(xué)校計(jì)算機(jī)視覺。

          下載3:OpenCV實(shí)戰(zhàn)項(xiàng)目20講
          小白學(xué)視覺公眾號(hào)后臺(tái)回復(fù):OpenCV實(shí)戰(zhàn)項(xiàng)目20講,即可下載含有20個(gè)基于OpenCV實(shí)現(xiàn)20個(gè)實(shí)戰(zhàn)項(xiàng)目,實(shí)現(xiàn)OpenCV學(xué)習(xí)進(jìn)階。

          交流群


          歡迎加入公眾號(hào)讀者群一起和同行交流,目前有SLAM、三維視覺、傳感器、自動(dòng)駕駛、計(jì)算攝影、檢測(cè)、分割、識(shí)別、醫(yī)學(xué)影像、GAN、算法競(jìng)賽等微信群(以后會(huì)逐漸細(xì)分),請(qǐng)掃描下面微信號(hào)加群,備注:”昵稱+學(xué)校/公司+研究方向“,例如:”張三 + 上海交大 + 視覺SLAM“。請(qǐng)按照格式備注,否則不予通過。添加成功后會(huì)根據(jù)研究方向邀請(qǐng)進(jìn)入相關(guān)微信群。請(qǐng)勿在群內(nèi)發(fā)送廣告,否則會(huì)請(qǐng)出群,謝謝理解~



          瀏覽 34
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产日韩视频 | 国产人成午夜免电影费观看 | 精品欧美一区二区三区成人片 | 免费插逼| 天天日日夜夜爱 |