小言_互联网的博客

论文复现|Panoptic Deeplab(全景分割PyTorch)

276人阅读  评论(0)
摘要:这是发表于CVPR 2020的一篇论文的复现模型。

本文分享自华为云社区《Panoptic Deeplab(全景分割PyTorch)》,作者:HWCloudAI 。

这是发表于CVPR 2020的一篇论文的复现模型,B. Cheng et al, “Panoptic-DeepLab: A Simple, Strong, and Fast Baseline for Bottom-Up Panoptic Segmentation”, CVPR 2020,此模型在原论文的基础上,使用HRNet作为backbone,得到了高于原论文的精度,PQ达到了63.7%,mIoU达到了80.3%,AP达到了37.3%。该算法会载入Cityscapes上的预训练模型(HRNet),我们提供了训练代码和可用于训练的模型,用于实际场景的微调训练。训练后生成的模型可直接在ModelArts平台部署成在线服务。

具体算法介绍:AI Gallery_算法_模型_云市场-华为云

注意事项:

1.本案例使用框架:PyTorch1.4.0

2.本案例使用硬件:GPU: 1*NVIDIA-V100NV32(32GB) | CPU: 8 核 64GB

3.运行代码方法: 点击本页面顶部菜单栏的三角形运行按钮或按Ctrl+Enter键 运行每个方块中的代码

4.JupyterLab的详细用法: 请参考《ModelAtrs JupyterLab使用指导》

5.碰到问题的解决办法: 请参考《ModelAtrs JupyterLab常见问题解决办法》

1.下载数据和代码

运行下面代码,进行数据和代码的下载

本案例使用cityscapes数据集。


  
  1. import os
  2. import moxing as mox
  3. # 数据代码下载
  4. mox.file.copy_parallel('s3://obs-aigallery-zc/algorithm/panoptic-deeplab','./panoptic-deeplab')

2.模型训练

2.1依赖库加载


  
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from __future__ import print_function
  4. import os
  5. root_path = './panoptic-deeplab/'
  6. os.chdir(root_path)
  7. # 获取当前目录结构信息,以便进行代码调试
  8. print('os.getcwd():', os.getcwd())
  9. import time
  10. import argparse
  11. import time
  12. import datetime
  13. import math
  14. import sys
  15. import shutil
  16. import moxing as mox # ModelArts上专用的moxing模块,可用于与OBS的数据交互,API文档请查看:https://github.com/huaweicloud/ModelArts-Lab/tree/master/docs/moxing_api_doc
  17. from PIL import ImageFile
  18. ImageFile. LOAD_TRUNCATED_IMAGES = True

2.2训练参数设置


  
  1. parser = argparse.ArgumentParser(description= 'Panoptic Deeplab')
  2. parser.add_argument( '--training_dataset', default= '/home/ma-user/work/panoptic-deeplab/', help= 'Training dataset directory') # 在ModelArts中创建算法时,必须进行输入路径映射配置,输入映射路径的前缀必须是/home/work/modelarts/inputs/,作用是在启动训练时,将OBS的数据拷贝到这个本地路径中供本地代码使用。
  3. parser.add_argument( '--train_url', default= './output', help= 'the path to save training outputs') # 在ModelArts中创建训练作业时,必须指定OBS上的一个训练输出位置,训练结束时,会将输出映射路径拷贝到该位置
  4. parser.add_argument( '--num_gpus', default= 1, type= int, help= 'num of GPUs to train')
  5. parser.add_argument( '--eval', default= 'False', help= 'whether to eval')
  6. parser.add_argument( '--load_weight', default= 'trained_model/model/model_final.pth', type= str) # obs路径 断点模型 pth文件 如果是评估 则是相对于src的路径
  7. parser.add_argument( '--iteration', default= 100, type= int)
  8. parser.add_argument( '--learning_rate', default= 0.001, type= float)
  9. parser.add_argument( '--ims_per_batch', default= 8, type= int)
  10. args, unknown = parser.parse_known_args() # 必须将parse_args改成parse_known_args,因为在ModelArts训练作业中运行时平台会传入一个额外的init_method的参数
  11. # dir
  12. fname = os.getcwd()
  13. project_dir = os.path.join(fname, "panoptic-deeplab")
  14. detectron2_dir = os.path.join(fname, "detectron2-0.3+cu102-cp36-cp36m-linux_x86_64.whl")
  15. panopticapi_dir = os.path.join(fname, "panopticapi-0.1-py3-none-any.whl")
  16. cityscapesscripts_dir = os.path.join(fname, "cityscapesScripts-2.1.7-py3-none-any.whl")
  17. requirements_dir = os.path.join(project_dir, "requirements.txt")
  18. output_dir = "/home/work/modelarts/outputs/train_output"
  19. # config strings
  20. evalpath = ''
  21. MAX_ITER = 'SOLVER.MAX_ITER ' + str(args.iteration+ 90000)
  22. BASE_LR = 'SOLVER.BASE_LR ' + str(args.learning_rate)
  23. IMS_PER_BATCH = 'SOLVER.IMS_PER_BATCH ' + str(args.ims_per_batch)
  24. SCRIPT_PATH = os.path.join(project_dir, "tools_d2/train_panoptic_deeplab.py")
  25. CONFIG_PATH = os.path.join(fname, "configs/config.yaml")
  26. CONFIG_CMD = '--config-file ' + CONFIG_PATH
  27. EVAL_CMD = ''
  28. GPU_CMD = ''
  29. OPTS_CMD = MAX_ITER + ' ' + BASE_LR + ' ' + IMS_PER_BATCH
  30. RESUME_CMD = ''
  31. #functions
  32. def merge_cmd( scirpt_path, config_cmd, gpu_cmd, eval_cmd, resume_cmd, opts_cmd):
  33. return "python " + scirpt_path + " "+ config_cmd + " " + gpu_cmd + " " + eval_cmd + " " + resume_cmd + " " + OPTS_CMD
  34. if args. eval == 'True':
  35. assert args.load_weight, 'load_weight empty when trying to evaluate' # 如果评估时为空,则报错
  36. if args.load_weight != 'trained_model/model/model_final.pth':
  37. #将model拷贝到本地,并获取模型路径
  38. modelpath, modelname = os.path.split(args.load_weight)
  39. mox.file.copy_parallel(args.load_weight, os.path.join(fname, modelname))
  40. evalpath = os.path.join(fname,modelname)
  41. else:
  42. evalpath = os.path.join(fname, 'trained_model/model/model_final.pth')
  43. EVAL_CMD = '--eval-only MODEL.WEIGHTS ' + evalpath
  44. else:
  45. GPU_CMD = '--num-gpus ' + str(args.num_gpus)
  46. if args.load_weight:
  47. RESUME_CMD = '--resume'
  48. if args.load_weight != 'trained_model/model/model_final.pth':
  49. modelpath, modelname = os.path.split(args.load_weight)
  50. mox.file.copy_parallel(args.load_weight, os.path.join( '/cache',modelname))
  51. with open( '/cache/last_checkpoint', 'w') as f: #创建last_checkpoint文件
  52. f.write(modelname)
  53. f.close()
  54. else:
  55. os.system( 'cp ' + os.path.join(fname, 'trained_model/model/model_final.pth') + ' /cache/model_final.pth')
  56. with open( '/cache/last_checkpoint', 'w') as f: #创建last_checkpoint文件
  57. f.write( 'model_final.pth')
  58. f.close()
  59. os.environ[ 'DETECTRON2_DATASETS'] = args.training_dataset #添加数据库路径环境变量
  60. cmd = merge_cmd(SCRIPT_PATH, CONFIG_CMD, GPU_CMD, EVAL_CMD, RESUME_CMD, OPTS_CMD)
  61. # os.system('mkdir -p ' + args.train_url)
  62. print( '*********Train Information*********')
  63. print( 'Run Command: ' + cmd)
  64. print( 'Num of GPUs: ' + str(args.num_gpus))
  65. print( 'Evaluation: ' + args. eval)
  66. if args.load_weight:
  67. print( 'Load Weight: ' + args.load_weight)
  68. else:
  69. print( 'Load Weight: None (train from scratch)')
  70. print( 'Iteration: ' + str(args.iteration))
  71. print( 'Learning Rate: ' + str(args.learning_rate))
  72. print( 'Images Per Batch: ' + str(args.ims_per_batch))

2.3安装依赖库

安装依赖库需要几分钟,请耐心等待


  
  1. def install_dependecies(r,d, p, c):
  2. os. system( 'pip uninstall pytorch> out1.txt')
  3. os. system( 'pip install torch==1.7.0> out2.txt')
  4. os. system( 'pip install --upgrade pip')
  5. os. system( 'pip install --upgrade numpy')
  6. os. system( 'pip install torchvision==1.7.0> out3.txt')
  7. os. system( 'pip install pydot')
  8. os. system( 'pip install --upgrade pycocotools')
  9. os. system( 'pip install tensorboard')
  10. os. system( 'pip install -r ' + r + ' --ignore-installed PyYAML')
  11. os. system( 'pip install ' + d)
  12. os. system( 'pip install ' + p)
  13. os. system( 'pip install ' + c)
  14. os. system( 'pip install pyyaml ==5.1.0')
  15. # 安装依赖
  16. print( '*********Installing Dependencies*********')
  17. install_dependecies(requirements_dir,detectron2_dir, panopticapi_dir, cityscapesscripts_dir)
  18. *********Installing Dependencies*********

2.4开始训练


  
  1. print( '*********Training Begin*********')
  2. print(cmd)
  3. start = time. time()
  4. ret = os.system(cmd+ " >out.txt")
  5. if ret == 0:
  6. print( "success")
  7. else:
  8. print( 'fail')
  9. end_time= time. time()
  10. print( 'done')
  11. print(end_time-start)
  12. if args.eval == 'False':
  13. os.system( 'mv /cache/model_final.pth ' + os. path.join(fname, 'output/model_final.pth')) #/cache模型移动到输出文件夹
  14. if os. path.exists( os. path.join(fname, 'pred_results')):
  15. os.system( 'mv ' + os. path.join(fname, 'pred_results') + ' ' + args.train_url)

训练完成之后,可以在out.txt中看运行日志
在./panoptic-deeplab/output/pred_results/文件目录下,有该模型全景分割,实例分割,语义分割的评估结果

3.模型测试

3.1加载测试函数

from test import *

3.2开始预测


  
  1. if __name__ == '__main__':
  2. img_path = r'/home/ma-user/work/panoptic-deeplab/cityscapes/leftImg8bit/val/frankfurt/frankfurt_000000_003920_leftImg8bit.png' # TODO 修改测试图片路径
  3. model_path = r'/home/ma-user/work/panoptic-deeplab/output/model_final.pth' # TODO 修改模型路径
  4. my_model = ModelClass(model_path)
  5. result = my_model.predict(img_path)
  6. print(result)

点击关注,第一时间了解华为云新鲜技术~


转载:https://blog.csdn.net/devcloud/article/details/128013436
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场