pointnet-pytorch代码运行

参考知乎:
https://zhuanlan.zhihu.com/p/86331508

训练时:

参考:https://github.com/fxia22/pointnet.pytorch

python train_classification.py --dataset <dataset path> --nepoch=<number epochs> --dataset_type <modelnet40 | shapenet>
python train_segmentation.py --dataset <dataset path> --nepoch=<number epochs> 

以如下方式训练:

训练 train_classification.py

python train_classification.py --dataset '/home/dengjie/dengjie/Paper/PointNet/pointnet.pytorch/shapenetcore_partanno_segmentation_benchmark_v0/' --nepoch='5' --dataset_type 'shapenet'

需要把<>改为‘ ’,否则会报以下错误:

-bash: 未预期的符号 `newline' 附近有语法错误

在这里插入图片描述
在这里插入图片描述
训练结果:
在这里插入图片描述
会生成cls文件夹以及model

训练 train_segmentation.py

python train_segmentation.py --dataset '/home/dengjie/dengjie/Paper/PointNet/pointnet.pytorch/shapenetcore_partanno_segmentation_benchmark_v0/' --nepoch='5'

在这里插入图片描述
报错:

RuntimeError: CUDA out of memory. Tried to allocate 40.00 MiB (GPU 0; 5.81 GiB total capacity; 4.73 GiB already allocated; 39.25 MiB free; 34.15 MiB cached)

看其他博客说可能是pytorch版本的问题,但classification都能跑这个没理由会出现版本问题。将batchsize降低到8后可以运行(默认为32):

python train_segmentation.py --dataset '/home/dengjie/dengjie/Paper/PointNet/pointnet.pytorch/shapenetcore_partanno_segmentation_benchmark_v0/' --nepoch='5' --batchSize='8'

在这里插入图片描述
结果:
在这里插入图片描述
生成seg文件夹及model。

可视化

~/dengjie/Paper/PointNet/pointnet.pytorch/utils$ python show3d_balls.py

此处调用render_balls_so.so
在这里插入图片描述

运行show_cls.py

python show_cls.py

在这里插入图片描述
这里提示:

FileNotFoundError: [Errno 2] No such file or directory: 'shapenetcore_partanno_segmentation_benchmark_v0/synsetoffset2category.txt'

但实际上是有这个文件的。查看show_cls.py,发现此处root位置为:
root='shapenetcore_partanno_segmentation_benchmark_v0',
需要将路径改为绝对路径:

root='/home/dengjie/dengjie/Paper/PointNet/pointnet.pytorch/shapenetcore_partanno_segmentation_benchmark_v0',

再次运行show_cls.py时报错:

python show_cls.py
Traceback (most recent call last):
  File "show_cls.py", line 36, in <module>
    classifier.load_state_dict(torch.load(opt.model))
  File "/home/dengjie/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/serialization.py", line 381, in load
    f = open(f, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: ''

在这里插入图片描述
后面要加参数才能运行成功:

python show_cls.py --model cls/cls_model_0.pth

在这里插入图片描述
但此时的accuracy都是0。需要修改show_cls.py中的代码:

#correct = pred_choice.eq(target.data).cpu().sum()
 correct = target.eq(pred_choice.data).cpu().sum().data.numpy()

此处参考issue:https://github.com/fxia22/pointnet.pytorch/issues/29
运行结果:
在这里插入图片描述

运行show_seg.py

在这里插入图片描述
错误为;

Traceback (most recent call last):
  File "show_seg.py", line 30, in <module>
    data_augmentation=False)
  File "/home/dengjie/dengjie/Paper/PointNet/pointnet.pytorch/pointnet/dataset.py", line 76, in __init__
    with open(self.catfile, 'r') as f:
FileNotFoundError: [Errno 2] 没有那个文件或目录: 'synsetoffset2category.txt'

加参数后仍然报错:

python show_seg.py --dataset '~/dengjie/Paper/PointNet/pointnet.pytorch/shapenetcore_partanno_segmentation_benchmark_v0/'

在这里插入图片描述

FileNotFoundError: [Errno 2] 没有那个文件或目录: '~/dengjie/Paper/PointNet/pointnet.pytorch/shapenetcore_partanno_segmentation_benchmark_v0/synsetoffset2category.txt'

修改如下:

#root=opt.dataset,   
root='/home/dengjie/dengjie/Paper/PointNet/pointnet.pytorch/shapenetcore_partanno_segmentation_benchmark_v0',

又有新的错误出现:

IndexError: list index out of range

在这里插入图片描述
/pointnet.pytorch/pointnet/dataset.py中打印出self.seg_classes这个字典
在这里插入图片描述
在这里插入图片描述
self.seg_classes是有值的,所以问题不出在这。self.cat.keys()这个字典转换为列表后超出索引,所以考虑是不是self.cat字典是空字典。在代码中加log判断问题出在哪一步
在这里插入图片描述
在这里插入图片描述
发现列表class_choice是空的,在语句:

self.cat = {k: v for k, v in self.cat.items() if k in class_choice}

之前self.cat是有值的,但在这条语句之后就变成空的了,而之后再调用self.cat时自然也是空的。所以问题就出在这条语句上。
这句话的含义是,以k和v遍历字典self.cat的所有键值对并赋给k,v,如果键k在提供的参数class_choice中,则将k,v赋值给self.cat,若不在就为空,即self.cat被赋值为空。(个人理解)
那么在show_seg.py文件中有以下可选参数:

parser.add_argument('--model', type=str, default='', help='model path')
parser.add_argument('--idx', type=int, default=0, help='model index')                                                                                     parser.add_argument('--dataset', type=str, default='', help='dataset path')
parser.add_argument('--class_choice', type=str, default='', help='class choice')

这里–class_choice参数默认为空,所以直接运行此文件时就会使k无法遍历而使self.cat被赋值为空从而导致报错。
所以在运行show_seg.py时应加上参数如下:

python show_seg.py --model seg/seg_model_Chair_3.pth --class_choice Airplane

–class_choice 后可跟16种类别的任意一种。
可视化结果如下:
在这里插入图片描述

  • 19
    点赞
  • 90
    收藏
    觉得还不错? 一键收藏
  • 25
    评论
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值