欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 使用Yolact訓(xùn)練自己的數(shù)據(jù)集

 更新時間:2021年04月06日 14:39:19   作者:無窮升高的卡農(nóng)  
這篇文章主要介紹了python 使用Yolact訓(xùn)練自己的數(shù)據(jù)集,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下

可能是由于yolact官方更新過其項(xiàng)目代碼,所以網(wǎng)上其他人的yolact訓(xùn)練使用的config文件和我的稍微有區(qū)別。但總體還是差不多的。

1:提前準(zhǔn)備好自己的數(shù)據(jù)集

使用labelme來制作分割數(shù)據(jù)集,但是得到的是一個個單獨(dú)的json文件。需要將其轉(zhuǎn)換成coco。
labelme2coco.py如下所示(代碼來源:github鏈接):

import os
import json
import numpy as np
import glob
import shutil
from sklearn.model_selection import train_test_split
np.random.seed(41)

#0為背景,此處根據(jù)你數(shù)據(jù)集的類別來修改key
classname_to_id = {"1": 1}

class Lableme2CoCo:

 def __init__(self):
  self.images = []
  self.annotations = []
  self.categories = []
  self.img_id = 0
  self.ann_id = 0

 def save_coco_json(self, instance, save_path):
  json.dump(instance, open(save_path, 'w', encoding='utf-8'), ensure_ascii=False, indent=1) # indent=2 更加美觀顯示

 # 由json文件構(gòu)建COCO
 def to_coco(self, json_path_list):
  self._init_categories()
  for json_path in json_path_list:
   obj = self.read_jsonfile(json_path)
   self.images.append(self._image(obj, json_path))
   shapes = obj['shapes']
   for shape in shapes:
    annotation = self._annotation(shape)
    self.annotations.append(annotation)
    self.ann_id += 1
   self.img_id += 1
  instance = {}
  instance['info'] = 'spytensor created'
  instance['license'] = ['license']
  instance['images'] = self.images
  instance['annotations'] = self.annotations
  instance['categories'] = self.categories
  return instance

 # 構(gòu)建類別
 def _init_categories(self):
  for k, v in classname_to_id.items():
   category = {}
   category['id'] = v
   category['name'] = k
   self.categories.append(category)

 # 構(gòu)建COCO的image字段
 def _image(self, obj, path):
  image = {}
  from labelme import utils
  img_x = utils.img_b64_to_arr(obj['imageData'])
  h, w = img_x.shape[:-1]
  image['height'] = h
  image['width'] = w
  image['id'] = self.img_id
  image['file_name'] = os.path.basename(path).replace(".json", ".jpg")
  return image

 # 構(gòu)建COCO的annotation字段
 def _annotation(self, shape):
  label = shape['label']
  points = shape['points']
  annotation = {}
  annotation['id'] = self.ann_id
  annotation['image_id'] = self.img_id
  annotation['category_id'] = int(classname_to_id[label])
  annotation['segmentation'] = [np.asarray(points).flatten().tolist()]
  annotation['bbox'] = self._get_box(points)
  annotation['iscrowd'] = 0
  annotation['area'] = 1.0
  return annotation

 # 讀取json文件,返回一個json對象
 def read_jsonfile(self, path):
  with open(path, "r", encoding='utf-8') as f:
   return json.load(f)

 # COCO的格式: [x1,y1,w,h] 對應(yīng)COCO的bbox格式
 def _get_box(self, points):
  min_x = min_y = np.inf
  max_x = max_y = 0
  for x, y in points:
   min_x = min(min_x, x)
   min_y = min(min_y, y)
   max_x = max(max_x, x)
   max_y = max(max_y, y)
  return [min_x, min_y, max_x - min_x, max_y - min_y]


if __name__ == '__main__':
 labelme_path = "labelme/" # 此處根據(jù)你的數(shù)據(jù)集地址來修改
 saved_coco_path = "./"
 # 創(chuàng)建文件
 if not os.path.exists("%scoco/annotations/"%saved_coco_path):
  os.makedirs("%scoco/annotations/"%saved_coco_path)
 if not os.path.exists("%scoco/images/train2017/"%saved_coco_path):
  os.makedirs("%scoco/images/train2017"%saved_coco_path)
 if not os.path.exists("%scoco/images/val2017/"%saved_coco_path):
  os.makedirs("%scoco/images/val2017"%saved_coco_path)
 # 獲取images目錄下所有的joson文件列表
 json_list_path = glob.glob(labelme_path + "/*.json")
 # 數(shù)據(jù)劃分,這里沒有區(qū)分val2017和tran2017目錄,所有圖片都放在images目錄下
 train_path, val_path = train_test_split(json_list_path, test_size=0.12)
 print("train_n:", len(train_path), 'val_n:', len(val_path))

 # 把訓(xùn)練集轉(zhuǎn)化為COCO的json格式
 l2c_train = Lableme2CoCo()
 train_instance = l2c_train.to_coco(train_path)
 l2c_train.save_coco_json(train_instance, '%scoco/annotations/instances_train2017.json'%saved_coco_path)
 for file in train_path:
  shutil.copy(file.replace("json","jpg"),"%scoco/images/train2017/"%saved_coco_path)
 for file in val_path:
  shutil.copy(file.replace("json","jpg"),"%scoco/images/val2017/"%saved_coco_path)

 # 把驗(yàn)證集轉(zhuǎn)化為COCO的json格式
 l2c_val = Lableme2CoCo()
 val_instance = l2c_val.to_coco(val_path)
 l2c_val.save_coco_json(val_instance, '%scoco/annotations/instances_val2017.json'%saved_coco_path)

只需要修改兩個地方即可,然后放到data文件夾下。
最后,得到的coco格式的數(shù)據(jù)集如下所示:

至此,數(shù)據(jù)準(zhǔn)備已經(jīng)結(jié)束。

2:下載github存儲庫

網(wǎng)址:YOLACT

之后解壓,但是我解壓的時候不知道為啥沒有yolact.py這個文件。后來又建了一個py文件,復(fù)制了里面的代碼。

下載權(quán)重文件,把權(quán)重文件放到y(tǒng)olact-master下的weights文件夾里(沒有就新建):

3:修改config.py

文件所在位置:

修改類別,把原本的coco的類別全部注釋掉,修改成自己的(如紅色框),注意COCO_CLASSES里有一個逗號。

修改數(shù)據(jù)集地址dataset_base

修改coco_base_config(下面第二個橫線max_iter并不是控制訓(xùn)練輪數(shù)的,第二張圖中的max_iter才是)

4:訓(xùn)練

cd到指定路徑下,執(zhí)行下面命令即可

python train.py --config=yolact_base_config

剛開始:

因?yàn)槲沂亲獾脑品?wù)器,在jupyter notebook里訓(xùn)練的。輸出的訓(xùn)練信息比較亂。

訓(xùn)練幾分鐘后:

主要看T后面的數(shù)字即可,好像他就是總的loss,如果它收斂了,按下Ctrl+C,即可中止訓(xùn)練,保存模型權(quán)重。

第一個問題:

PytorchStreamReader failed reading zip archive: failed finding central directory

第二個問題:
(但是不知道為啥,我訓(xùn)練時如果中斷,保存的模型不能用來測試,會爆出下面的錯誤)

RuntimeError: unexpected EOF, expected *** more bytes. The file might be corruptrd

沒辦法解決,所以只能跑完,自動結(jié)束之后保存的模型拿來測試(自動保存的必中斷保存的要大十幾兆)

模型保存的格式:<config>_<epoch>_<iter>.pth。如果是中斷的:<config>_<epoch>_<iter>_interrupt.pth

5:測試

使用官網(wǎng)的測試命令即可

以上就是python 使用Yolact訓(xùn)練自己的數(shù)據(jù)集的詳細(xì)內(nèi)容,更多關(guān)于python 訓(xùn)練數(shù)據(jù)集的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • keras使用Sequence類調(diào)用大規(guī)模數(shù)據(jù)集進(jìn)行訓(xùn)練的實(shí)現(xiàn)

    keras使用Sequence類調(diào)用大規(guī)模數(shù)據(jù)集進(jìn)行訓(xùn)練的實(shí)現(xiàn)

    這篇文章主要介紹了keras使用Sequence類調(diào)用大規(guī)模數(shù)據(jù)集進(jìn)行訓(xùn)練的實(shí)現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python字典和集合講解

    Python字典和集合講解

    這篇文章主要給大家假關(guān)節(jié)的是Python字典和集合,字典是Python內(nèi)置的數(shù)據(jù)結(jié)構(gòu)之一,是一個無序的序列;而集合是python語言提供的內(nèi)置數(shù)據(jù)結(jié)構(gòu),沒有value的字典,集合類型與其他類型最大的區(qū)別在于,它不包含重復(fù)元素。想具體了解有關(guān)python字典與集合,請看下面文章內(nèi)容
    2021-10-10
  • Python相互導(dǎo)入的問題解決

    Python相互導(dǎo)入的問題解決

    大家好,本篇文章主要講的是Python相互導(dǎo)入的問題解決,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • python 匿名函數(shù)與三元運(yùn)算學(xué)習(xí)筆記

    python 匿名函數(shù)與三元運(yùn)算學(xué)習(xí)筆記

    這篇文章主要介紹了python 匿名函數(shù)與三元運(yùn)算的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python 編程,感興趣的朋友可以了解下
    2020-10-10
  • python實(shí)現(xiàn)根據(jù)窗口標(biāo)題調(diào)用窗口的方法

    python實(shí)現(xiàn)根據(jù)窗口標(biāo)題調(diào)用窗口的方法

    這篇文章主要介紹了python實(shí)現(xiàn)根據(jù)窗口標(biāo)題調(diào)用窗口的方法,涉及Python操作窗口的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • 關(guān)于pytorch訓(xùn)練分類器

    關(guān)于pytorch訓(xùn)練分類器

    這篇文章主要介紹了關(guān)于pytorch訓(xùn)練分類器問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • python中必要的名詞解釋

    python中必要的名詞解釋

    在本篇文章里小編給大家整理的是關(guān)于python中必要的名詞解釋以及相關(guān)知識點(diǎn),有興趣的朋友們學(xué)習(xí)下。
    2019-11-11
  • Pandas讀取并修改excel的示例代碼

    Pandas讀取并修改excel的示例代碼

    這篇文章主要介紹了Pandas讀取并修改excel的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • python爬蟲系列網(wǎng)絡(luò)請求案例詳解

    python爬蟲系列網(wǎng)絡(luò)請求案例詳解

    這篇文章主要介紹了【Python從零到壹】python爬蟲系列-網(wǎng)絡(luò)請求,從零開始學(xué)習(xí)Python網(wǎng)絡(luò)爬蟲,如何從中獲取需要的數(shù)據(jù)信息,現(xiàn)整理出零基礎(chǔ)如何學(xué)爬蟲技術(shù)以供學(xué)習(xí)
    2021-04-04
  • 在scrapy中使用phantomJS實(shí)現(xiàn)異步爬取的方法

    在scrapy中使用phantomJS實(shí)現(xiàn)異步爬取的方法

    今天小編就為大家分享一篇在scrapy中使用phantomJS實(shí)現(xiàn)異步爬取的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12

最新評論