ExtremeNet 实现

代码:https://github.com/xingyizhou/ExtremeNet

1、 创建虚拟环境

在当前工程中:

$ conda create -- name ExtremeNet -- file conda_packagelist.txt

过程中下载了 file conda_packagelist.txt中的所有库包,都下载完全后,激活环境。

$ source activate ExtremeNet

这时命令行最前面会出现一个(CornerNet),代表在当前的虚拟环境中。

2、编译 NMS(来自于 Faster RCNN 和soft_NMS)

$ cd external
$ make

报错:
在这里插入图片描述
安装上Cython即可。pip install Cython

3、安装 MS COCO APIs

$ cd data
$ git clone https://github.com/cocodataset/cocoapi/git coco
$ cd data/coco/PythonAPI
$ make
$ python setup.py install --user

4、准备数据集

用的制作的数据集,如下格式:

annotationsinstances_train2014.json
ExtremeNetdatacocoinstances_val2014.json
imagestrain2014
val2014

5.1修改tools中的gen_coco_extrem_points.py

①原来为2017改为2014

ANN_PATH = '../data/coco/annotations/instances_{}2014.json'
OUT_PATH = '../data/coco/annotations/instances_extreme_{}2014.json'
IMG_DIR = '../data/coco/{}2014/'

②修改种类

if __name__ == '__main__':  
   for split in SPLITS:     
     data = json.load(open(ANN_PATH.format(split), 'r'))     
     coco = cocoapi.COCO(ANN_PATH.format(split))     
     img_ids = coco.getImgIds()     
     num_images = len(img_ids)     
     num_classes = 1  #修改称自己的种类

5.2修改db/coco_extreme.py

class MSCOCOExtreme(DETECTION):
      def __init__(self, db_config, split):
          super(MSCOCOExtreme, self).__init__(db_config)
          data_dir   = system_configs.data_dir
          cache_dir  = system_configs.cache_dir

          self._split = split
          self._dataset = {
              "train": "train2014",
              "val": "val2014",
              #"testdev": "test2017"
        }[self._split]

原来的train和val是2017,我这里改成了2014。
②self._cat_ids = [1] #修改类别id。

5.3修改模型文件ExtremeNet.py 输出的类别数

class model(exkp):     
       def __init__(self, db):        
              n       = 5         
             dims    = [256, 256, 384, 384, 384, 512]        
             modules = [2, 2, 2, 2, 2, 4]         
             out_dim = 1   #改为自己的类别数  

5.4修改db/detection.py文件中的类别数

https://github.com/princeton-vl/CornerNet/issues/23
self._configs[“categories”] =1 #改为自己的类别数

5.5(不修改也没有影响)修改sample/coco_extreme.py文件中的类别数

  if debug:
        debugger = Debugger(num_classes=1)
5.6修改models/py_utils/exkp.py
 def  _debug(image, t_heat, l_heat, b_heat, r_heat, ct_heat):
        debugger = Debuger(num_classes=1)

6、修改ExtremeNet.json配送文件

我只有一个GPU因此要修改batchsize和chunk_sizes,修改max_iter ,stepsize等

6.1 修改ExtremeNet.json:

{
    "system": {
        "dataset": "MSCOCOExtreme",
        "batch_size": 1,
        "sampling_function": "kp_detection",

        "train_split": "train",
        "val_split": "val",

        "learning_rate": 0.00025,
        "decay_rate": 10,

        "val_iter": 100,

        "opt_algo": "adam",
        "prefetch_size": 10,

        "max_iter": 5000,
        "stepsize": 3500,
        "snapshot": 500,

        "chunk_sizes": [1],

        "data_dir": "./data",

        "pretrain": "./cache/ExtremeNet_250000.pkl"
    },
    
    "db": {
        "rand_scale_min": 0.6,
        "rand_scale_max": 1.4,
        "rand_scale_step": 0.1,
        "rand_scales": null,

        "rand_crop": true,
        "rand_color": true,

        "border": 128,
        "gaussian_bump": true,

        "input_size": [511, 511],
        "output_sizes": [[128, 128]],

        "test_scales": [1],

        "top_k": 40,
        "categories": 1,
        "aggr_weight": 0.1,
        "scores_thresh": 0.1,
        "center_thresh": 0.1,
        "nms_threshold": 0.5,
        "suppres_ghost": true,

        "max_per_image": 100
    }
}

6.2 在train.py中添加一句指定GPU语句。

https://blog.csdn.net/LLyj_/article/details/88900547

torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
os.environ["CUDA_VISIBLE_DEVICES"] = "0" #加上这句

7、下载预训练模型:

pre-trained model 下载到cache文件夹下

8、从分割中产生生成极点的标签文件:

$ cd tools
$ python gen_coco_extreme_points.py

在data/coco/annotations/生成了 instances_extreme_train2014.json 和instances_extreme_val2014.json
在这里插入图片描述

9、训练

运行命令:

$ python train.py ExtremeNet

报错:(OpenCV安装的有问题)https://blog.csdn.net/qq_26815239/article/details/89312033

Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you
are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then
re-run cmake or configure script

重新安装opencv-python:

$ pip install opencv-python

之后重新运行命令.
1)报错:
在这里插入图片描述
这是因为加载的预训练权重是训练好的80类,我的是1类,于是我直接把加载预训练权重部分的程序去掉,不加载了。
2)训练过程
在这里插入图片描述
训练过程中产生的文件都存放在cache/nnet/EtremeNet中。
在这里插入图片描述

10、测试

运行命令:

$ python test.py EtremeNet

1)不加载预训练权重的测试结果:
在这里插入图片描述
2)加载预训练权重的测试结果(加载预训练权重时要把5.1②gen_coco_extrem_points.py中的num_classes改为80;5.3ExtremeNet.py中的out_dim改为80; 5.4和5.5以及5.6都改为80; ExtremeNet.json 6.1中 “categories”: 80 在测试的时候才改为1):
在这里插入图片描述

11、运行demo验证

修改demo.py

class_name = ['__background__','apple']
修改加载的权重:改成自己训练好的权重
def parse_args():
    parser = argparse.ArgumentParser(description="Demo CornerNet")
    parser.add_argument("--cfg_file", help="config file",
                        default='ExtremeNet', type=str)
    parser.add_argument("--demo", help="demo image path or folders",
                        default="", type=str)
    parser.add_argument("--model_path",
                        default='cache/ExtremeNet_5000.pkl')#改成自己训练好的权重路径

运行命令:

$ python demo.py --demo timg8.jpg  --show_mask

报错找不到 dextr_pacal-sbd.pth文件
下载 PASCAL + SBD pertained model 放到cache文件夹中
重新运行命令,得到结果:
在这里插入图片描述
原图:
在这里插入图片描述
center
不加载权重:
在这里插入图片描述
加载权重:
在这里插入图片描述
extreme:
不加载权重:
在这里插入图片描述
加载权重:
在这里插入图片描述
mask:
不加载权重:
在这里插入图片描述
加载权重:
在这里插入图片描述
out:
不加载权重:
在这里插入图片描述
加载权重:
在这里插入图片描述
参考博客:
https://blog.csdn.net/qq_26815239/article/details/89313064
https://blog.csdn.net/LLyj_/article/details/88900547
https://blog.csdn.net/qq_26815239/article/details/89312033

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值