濮阳杆衣贸易有限公司

主頁 > 知識庫 > python 使用Yolact訓(xùn)練自己的數(shù)據(jù)集

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

熱門標簽:電話機器人貸款詐騙 廣東旅游地圖標注 打印谷歌地圖標注 京華圖書館地圖標注 蘇州人工外呼系統(tǒng)軟件 淮安呼叫中心外呼系統(tǒng)如何 佛山通用400電話申請 看懂地圖標注方法 電話外呼系統(tǒng)招商代理

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

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

使用labelme來制作分割數(shù)據(jù)集,但是得到的是一個個單獨的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)

 # 把驗證集轉(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ù)準備已經(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

剛開始:

因為我是租的云服務(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ù)集的詳細內(nèi)容,更多關(guān)于python 訓(xùn)練數(shù)據(jù)集的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • 如何用 Python 處理不平衡數(shù)據(jù)集
  • python實現(xiàn)將兩個文件夾合并至另一個文件夾(制作數(shù)據(jù)集)
  • python實現(xiàn)提取COCO,VOC數(shù)據(jù)集中特定的類
  • python KNN算法實現(xiàn)鳶尾花數(shù)據(jù)集分類
  • python Pandas如何對數(shù)據(jù)集隨機抽樣
  • python調(diào)用攝像頭拍攝數(shù)據(jù)集
  • python實現(xiàn)多層感知器MLP(基于雙月數(shù)據(jù)集)
  • Python 統(tǒng)計數(shù)據(jù)集標簽的類別及數(shù)目操作

標簽:江蘇 湖州 衡水 中山 畢節(jié) 駐馬店 股票 呼和浩特

巨人網(wǎng)絡(luò)通訊聲明:本文標題《python 使用Yolact訓(xùn)練自己的數(shù)據(jù)集》,本文關(guān)鍵詞  python,使用,Yolact,訓(xùn)練,自己的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python 使用Yolact訓(xùn)練自己的數(shù)據(jù)集》相關(guān)的同類信息!
  • 本頁收集關(guān)于python 使用Yolact訓(xùn)練自己的數(shù)據(jù)集的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    丰原市| 福安市| 枣强县| 中方县| 虞城县| 肥城市| 东源县| 光泽县| 桦南县| 襄垣县| 无锡市| 高雄县| 栖霞市| 孟连| 钦州市| 肇源县| 罗田县| 仁布县| 固安县| 奈曼旗| 榆树市| 贡嘎县| 信丰县| 通山县| 岳阳县| 扎囊县| 称多县| 高阳县| 广河县| 嘉义市| 鲁甸县| 德格县| 灵山县| 郑州市| 景泰县| 嘉峪关市| 茌平县| 赞皇县| 屯留县| 玉环县| 武胜县|