参考知乎:
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种类别的任意一种。
可视化结果如下: