火腿肠项目地址 https://github.com/qixuxiang/Hybrid_Task_Cascade
首先把Pytorch1.0装好
下载项目文件 确保777权限 不然会有编译失败
./compile.sh
python setup.py develop
或者
python setup.py install
mydemo.py
import mmcv
from mmcv.runner import load_checkpoint
from mmdet.models import build_detector
from mmdet.apis import inference_detector, show_result
cfg = mmcv.Config.fromfile('/media/yang/56c81da5-109b-42e1-9e51-0f9e1396fa9d/yang/Hybrid_Task_Cascade-master/configs/cascade_mask_rcnn_x101_64x4d_fpn_1x.py')
cfg.model.pretrained = None
# construct the model and load checkpoint
model = build_detector(cfg.model, test_cfg=cfg.test_cfg)
_ = load_checkpoint(model, 'checkpoints/cascade_mask_rcnn_x101_64x4d_fpn_20e_20181218-630773a7.pth')
# test a single image
img = mmcv.imread('demo/33823288584_1d21cf0a26_k.jpg')
result = inference_detector(model, img, cfg)
show_result(img, result)
'''
# test a list of images
imgs = ['test1.jpg', 'test2.jpg']
for i, result in enumerate(inference_detector(model, imgs, cfg, device='cuda:0')):
print(i, imgs[i])
show_result(imgs[i], result)
'''
权重下载地址 https://github.com/qixuxiang/Hybrid_Task_Cascade/blob/master/MODEL_ZOO.md
作者试验了很多模型 什么都有 看自己情况下载
作者把权重放在了亚马逊云 下载起来很慢 要挂代理
500M大概半小时左右
主要改3个地方 cfg路径 model路径 图片路径
python mydemo.py
我是用coco-annotator标注的
格式和coco2014一样
但是这个模型需要coco2017格式的数据集
也就是多了一组图片 stuffmaps这个东西
下面提供转换工具
https://github.com/nightrome/cocostuffapi 下载这个东西
2、进入PythonAPI/pycocotools/cocostuffhelper.py,改动:
(1) Line141:
from:
labelMap = cocoSegmentationToSegmentationMap(coco, imgId, includeCrowd=includeCrowd)
to:
labelMap = cocoSegmentationToSegmentationMap(coco, imgId, checkUniquePixelLabel=False, includeCrowd=includeCrowd)
(2) Line142 后面加上:
labelMap = labelMap + 91
(3)进入 PythonAPI/ :
make
4、使用 PythonAPI/cocostuff/cocoSegmentationToPngDemo.py ,设置好 annPath 为你自己的 coco/annotations/instances_train2014.json 路径;然后把 Line75-Line88 注释掉
4.5
中途报错:ValueError: invalid palette size,
解决方案:
引用cmap的时候改成 np.uint8(cmap).tolist()
即可~
这句话加在cocostuffapi-master/PythonAPI/pycocotools/cocostuffhelper.py
大约153行 assert len(cmap) == 768, 'Error: Color map must have exactly 256*3 elements!' 前面
5、把cocostuff/cocoSegmentationToPngDemo.py 放到cocostuff这个文件夹外面
修改成自己的路径 下面是我的
#!/usr/bin/python
__author__ = 'hcaesar'
# Converts COCO segmentation .json files (GT or results) to one .png file per image.
#
# This script can be used for visualization of ground-truth and result files.
# Furthermore it can convert the ground-truth annotations to a more easily
# accessible .png format that is supported by many semantic segmentation methods.
#
# Note: To convert a result file to .png, we need to have both a valid GT file
# and the result file and set isAnnotation=False.
#
# The .png images are stored as indexed images, which means they contain both the
# segmentation map, as well as a color palette for visualization.
#
# Microsoft COCO Toolbox. version 2.0
# Data, paper, and tutorials available at: http://mscoco.org/
# Code written by Piotr Dollar and Tsung-Yi Lin, 2015.
# Licensed under the Simplified BSD License [see coco/license.txt]
import os
from pycocotools import mask
from pycocotools.cocostuffhelper import cocoSegmentationToPng
from pycocotools.coco import COCO
import skimage.io
import matplotlib.pyplot as plt
def cocoSegmentationToPngDemo(dataDir='coco/', dataTypeAnn='train2014', dataTypeRes='examples', \
pngFolderName='export_png', isAnnotation=True, exportImageLimit=10000):
'''
Converts COCO segmentation .json files (GT or results) to one .png file per image.
:param dataDir: location of the COCO root folder
:param dataTypeAnn: identifier of the ground-truth annotation file
:param dataTypeRes: identifier of the result annotation file (if any)
:param pngFolderName: the name of the subfolder where we store .png images
:param isAnnotation: whether the COCO file is a GT annotation or a result file
:return: None
'''
# Define paths
annPath = '%s/annotations/instances_%s.json' % (dataDir, dataTypeAnn)
if isAnnotation:
pngFolder = '%s/annotationss/%s' % (dataDir, dataTypeAnn)
else:
pngFolder = '%s/results/%s' % (dataDir, pngFolderName)
resPath = '%s/results/stuff_%s_results.json' % (dataDir, dataTypeRes)
# Create output folder
if not os.path.exists(pngFolder):
os.makedirs(pngFolder)
# Initialize COCO ground-truth API
coco = COCO(annPath)
imgIds = coco.getImgIds()
# Initialize COCO result
if not isAnnotation:
coco = coco.loadRes(resPath)
imgIds = sorted(set([a['image_id'] for a in coco.anns.values()]))
# Limit number of images
if exportImageLimit < len(imgIds):
imgIds = imgIds[0:exportImageLimit]
# Convert each image to a png
imgCount = len(imgIds)
for i in range(0, imgCount):
imgId = imgIds[i]
imgName = coco.loadImgs(ids=imgId)[0]['file_name'].replace('.jpg', '')
print('Exporting image %d of %d: %s' % (i+1, imgCount, imgName))
segmentationPath = '%s/%s.png' % (pngFolder, imgName)
cocoSegmentationToPng(coco, imgId, segmentationPath)
# # Visualize the last image
# originalImage = skimage.io.imread(coco.loadImgs(imgId)[0]['coco_url'])
# segmentationImage = skimage.io.imread(segmentationPath)
# plt.figure()
# plt.subplot(121)
# plt.imshow(originalImage)
# plt.axis('off')
# plt.title('original image')
# plt.subplot(122)
# plt.imshow(segmentationImage)
# plt.axis('off')
# plt.title('annotated image')
# plt.show()
if __name__ == "__main__":
cocoSegmentationToPngDemo()
然后执行
坑爹的是这个图像是彩色的 必须是单通道的灰度图才行
转换工具
import cv2
import numpy as np
import os
IMAGE_DIR= '/media/yang/56c81da5-109b-42e1-9e51-0f9e1396fa9d/yang/Hybrid_Task_Cascade-master/data/coco/stuffthingmaps/val2014/'
out = '/media/yang/56c81da5-109b-42e1-9e51-0f9e1396fa9d/yang/Hybrid_Task_Cascade-master/data/coco/stuffthingmaps/out/'
file_names = next(os.walk(IMAGE_DIR))[2]
for x in range(len(file_names)):
img = cv2.imread(os.path.join(IMAGE_DIR, file_names[x]))
#img = cv2.imread(img_path)
#获取图片的宽和高
#width,height = img.shape[:2][::-1]
# #将图片缩小便于显示观看
# img_resize = cv2.resize(img,
# (int(width*0.5),int(height*0.5)),interpolation=cv2.INTER_CUBIC)
# cv2.imshow("img",img_resize)
# print("img_reisze shape:{}".format(np.shape(img_resize)))
#将图片转为灰度图
img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
cv2.imwrite(out+str(file_names[x]),img_gray)
#cv2.waitKey()
把这个图像包合并到之前的数据集
yang@yang:/media/yang/56c81da5-109b-42e1-9e51-0f9e1396fa9d/yang/maskrcnn/Mask_RCNN-master/datasets/coco$ tree
.
├── annotations
│ ├── instances_train2014.json
│ ├── instances_val2014.json
├── stuffthingmaps
│ ├── train2014
│ │ ├── 1000.png
...
│ └── val2014
│ ├── 10.png
...
└── val2014
├── 10.jpg
...
└── 2.jpg
这个样子的
需要修改的地方有
Hybrid_Task_Cascade-master/configs/htc/htc_x101_64x4d_fpn_20e_16gpu.py这个文件中 因为我做语义分割 所有要用这个模型
第5行 填写预训练模型地址 我知道你没有 第一次运行会自动下载
第44/54/64/79行设为你自己的分类数
第191行设为你自己的图像尺寸
第224行记着修改学习率 不然会梯度爆炸
第243行 修改训练的Epoch数 默认的就没问题
第246行 存储log和模型的路径 有需要自己改 我没变
第239行取消注释 就会保存tfbroad文件
tensorboard --logdir event文件的上一级路径 要等一会才会显示
python tools/train.py --config configs/htc/htc_x101_64x4d_fpn_20e_16gpu.py 就可以了
意外中断 恢复训练
python tools/train.py --config configs/htc/htc_x101_64x4d_fpn_20e_16gpu.py --resume_from work_dirs/htc_x101_64x4d_fpn_20e/epoch_16.pth