点击查看代码
#以下代码是在编码点云的特征后进行的,即在maxpool之后的结构
import torch.nn as nn
import torch.utils.data
import torch.nn.functional as F
from models.pointnet_utils import PointNetEncoder,feature_transform_reguliarzer
class get_model(nn.Module):
def __init__(self, k=40, normal_channel=True):
super(get_model, self).__init__()
if normal_channel:
channel = 6
else:
channel = 3
self.feat = PointNetEncoder(global_feat=True, feature_transform=True, channel=channel)
self.fc1 = nn.Linear(1024, 512)
self.fc2 = nn.Linear(512, 256)
self.fc3 = nn.Linear(256, k)
self.dropout = nn.Dropout(p=0.4)
self.bn1 = nn.BatchNorm1d(512)
self.bn2 = nn.BatchNorm1d(256)
self.relu = nn.ReLU()
def forward(self, x):
x, trans, trans_feat = self.feat(x)
x = F.relu(self.bn1(self.fc1(x)))
x = F.relu(self.bn2(self.dropout(self.fc2(x)))) #nn.Dropout-使每个位置的元素都有一定概率归0,以此来模拟现实生活中的某些频道的数据缺失,以达到数据增强的目的
x = self.fc3(x)
x = F.log_softmax(x, dim=1) #F.softmax-按照行(1)或者列(0)来做归一化,F.log_softmax-在softmax的结果上做一次log运算
return x, trans_feat
class get_loss(torch.nn.Module):
def __init__(self, mat_diff_loss_scale=0.001):
super(get_loss, self).__init__()
self.mat_diff_loss_scale = mat_diff_loss_scale
def forward(self, pred, target, trans_feat):
loss = F.nll_loss(pred, target) #F.nll_loss()-nn.CrossEntropyLoss()与NLLLoss()相同,唯一不同的是前者为我们去做log_softmax
mat_diff_loss = feature_transform_reguliarzer(trans_feat)
total_loss = loss + mat_diff_loss * self.mat_diff_loss_scale
return total_loss