def forward(self, p, img_size, var=None): #bs, ny, nx = p.shape[0], p.shape[-2], p.shape[-1] bs, _, ny, nx = p.shape # bs, 255, 13, 13 if (self.nx, self.ny) != (nx, ny): create_grids(self, img_size, (nx, ny), p.device, p.dtype) # p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes + xywh) p = p.view(bs, self.na, self.nc + 5, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction # https://discuss.pytorch.org/t/in-pytorch-0-4-is-it-recommended-to-use-reshape-than-view-when-it-is-possible/17034 #p = p.reshape(bs, self.na, self.nc + 5, self.ny, self.nx).permute(0, 1, 3, 4, 2) # prediction if self.training: return p else: # inference # s = 1.5 # scale_xy (pxy = pxy * s - (s - 1) / 2) io = p.clone() # inference output io[..., 0:2] = torch.sigmoid(io[..., 0:2]) + self.grid_xy # xy io[..., 2:4] = torch.exp( io[..., 2:4]) * self.anchor_wh # wh yolo method # io[..., 2:4] = ((torch.sigmoid(io[..., 2:4]) * 2) ** 3) * self.anchor_wh # wh power method io[..., :4] *= self.stride #if 'default' in self.arc: # seperate obj and cls #torch.sigmoid_(io[..., 4:]) #torch.sigmoid_(io[..., 4]) torch.sigmoid_(io[..., 4:]) if self.nc == 1: io[..., 5] = 1 # single-class model https://github.com/ultralytics/yolov3/issues/235 # reshape from [1, 3, 13, 13, 85] to [1, 507, 85] return io.view(bs, -1, 5 + self.nc), p
def forward(self, p, img_size): bs, _, ny, nx = p.shape # bs, 255, 13, 13 if (self.nx, self.ny) != (nx, ny): create_grids(self, img_size, (nx, ny), p.device, p.dtype) # p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes + xywh) p = p.view(bs, self.na, self.no, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction if self.training: return p else: # inference # s = 1.5 # scale_xy (pxy = pxy * s - (s - 1) / 2) io = p.clone() # inference output io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid_xy # xy io[..., 2:4] = torch.exp( io[..., 2:4]) * self.anchor_wh # wh yolo method # io[..., 2:4] = ((torch.sigmoid(io[..., 2:4]) * 2) ** 3) * self.anchor_wh # wh power method io[..., :4] *= self.stride # 原始像素尺度 torch.sigmoid_(io[..., 4:]) return io.view(bs, -1, self.no), p
def forward(self, p): p = p[self.in_feature_id] p = self.conv(p) bs, ny, nx = p.shape[0], p.shape[-2], p.shape[-1] if (self.nx, self.ny) != (nx, ny): self.create_grids((nx, ny), p.device, p.dtype) # p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes + xywh) p = p.view(bs, self.na, self.nc + 5, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction if self.training: return p else: # inference # s = 1.5 # scale_xy (pxy = pxy * s - (s - 1) / 2) io = p.clone() # inference output io[..., 0:2] = torch.sigmoid(io[..., 0:2]) + self.grid_xy # xy io[..., 2:4] = torch.exp( io[..., 2:4]) * self.anchor_wh # wh yolo method # io[..., 2:4] = ((torch.sigmoid(io[..., 2:4]) * 2) ** 3) * self.anchor_wh # wh power method io[..., :4] *= self.stride torch.sigmoid_(io[..., 4:]) if self.nc == 1: io[..., 5] = 1 # single-class model https://github.com/ultralytics/yolov3/issues/235 # reshape from [1, 3, 13, 13, 85] to [1, 507, 85] return io.view(bs, -1, 5 + self.nc), p
def forward(self, p, img_size): bs, ny, nx = p.shape[0], p.shape[-2], p.shape[-1] if (self.nx, self.ny) != (nx, ny): create_grids(self, img_size, (nx, ny), p.device, p.dtype) # p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes + xywh) p = p.view(bs, self.na, self.nc + 6, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction if self.training: return p else: # inference io = p.clone() # inference output io[..., 0:2] = torch.sigmoid(io[..., 0:2]) + self.grid_xy # xy io[..., 2:4] = torch.exp( io[..., 2:4]) * self.anchor_wh # wh yolo method io[..., :4] *= self.stride torch.sigmoid_(io[..., 4:-1]) if self.nc == 1: io[..., 5] = 1 # single-class model https://github.com/ultralytics/yolov3/issues/235 return io.view(bs, -1, 6 + self.nc), p
def swish_func(x, beta=1.0, inplace=False): """ "Swish: a Self-Gated Activation Function" Searching for Activation Functions (https://arxiv.org/abs/1710.05941) If beta=1 applies the Sigmoid Linear Unit (SiLU) function element-wise If beta=0, Swish becomes the scaled linear function (identity activation) f(x) = x/2 As beta -> ∞, the sigmoid component converges to approach a 0-1 function (unit step), and multiplying that by x gives us f(x)=2max(0,x), which is the ReLU multiplied by a constant factor of 2, so Swish becomes like the ReLU function. Including beta, Swish can be loosely viewed as a smooth function that nonlinearly interpolate between identity (linear) and ReLU function. The degree of interpolation can be controlled by the model if beta is set as a trainable parameter. Alt: 1.78718727865 * (x * sigmoid(x) - 0.20662096414) """ if inplace: # In-place implementation, may consume less GPU memory: result = x.clone() torch.sigmoid_(beta * x) x *= result return x # Normal out-of-place implementation: return x * torch.sigmoid(beta * x)
def forward(self, p, img_size, var=None): bs, _, ny, nx = p.shape # bs, 255, 13, 13 if (self.nx, self.ny) != (nx, ny): create_grids(self, img_size, (nx, ny), p.device, p.dtype) # p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes + xywh) p = p.view(bs, self.na, self.no, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction if self.training: return p else: # inference # s = 1.5 # scale_xy (pxy = pxy * s - (s - 1) / 2) io = p.clone() # inference output io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid_xy # xy io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh # wh yolo method # io[..., 2:4] = ((torch.sigmoid(io[..., 2:4]) * 2) ** 3) * self.anchor_wh # wh power method io[..., :4] *= self.stride if 'default' in self.arc: # seperate obj and cls torch.sigmoid_(io[..., 4]) elif 'BCE' in self.arc: # unified BCE (80 classes) torch.sigmoid_(io[..., 5:]) io[..., 4] = 1 elif 'CE' in self.arc: # unified CE (1 background + 80 classes) io[..., 4:] = F.softmax(io[..., 4:], dim=4) io[..., 4] = 1 if self.nc == 1: io[..., 5] = 1 # single-class model https://github.com/ultralytics/yolov3/issues/235 # reshape from [1, 3, 13, 13, 85] to [1, 507, 84], remove obj_conf return io.view(bs, -1, self.no), p
def forward(self, x, img_max_side): batch_size, _, grid_num_y, grid_num_x = x.shape # last layer output shape : [batch_size, 255, w, h] if (self.grid_num_x, self.grid_num_y) != (grid_num_x, grid_num_y): create_grids(self, img_max_side, (grid_num_x, grid_num_y), x.device, x.dtype) x = x.view( batch_size, self.num_anchors, self.num_outputs, self.grid_num_y, self.grid_num_x ).permute(0, 1, 3, 4, 2).contiguous( ) # from [batch_size, 255, y, x] -> [batch_size, 3(number of anchors), y,x, 85] # self.training is member of nn.Module if self.training: return x else: # testing outputs = x.clone() # location outputs[:, :, :, :, :2] = torch.sigmoid( outputs[:, :, :, :, :2]) + self.grid_xy outputs[:, :, :, :, 2:4] = torch.exp(outputs[:, :, :, :, 2:4]) * self.anchor_wh outputs[:, :, :, :, :4] *= self.grid_stride # classificaion torch.sigmoid_(outputs[:, :, :, :, 4:]) # from [batch_size, num_anchors, nx, ny, num_outputs] to [batch_size, -1, num_outputs] return outputs.view(batch_size, -1, self.num_outputs)
def forward(self, x): # (bs, 255, 13, 13) -> (bs, 3, 85, 13, 13) -> (bs, 3, 13, 13, 85) # (bs, anchors, gridx, gridy, xywh+conf+classes) bs, _, ny, nx = x.shape if self.grid is None or self.grid.shape[-3:-1] != (ny, nx): dev = x.device gy, gx = torch.meshgrid( [torch.arange(ny, device=dev), torch.arange(nx, device=dev)]) self.grid = torch.stack((gx, gy), 2).view(1, 1, ny, nx, 2).float() self.anchor_wh = self.anchor_wh.to(dev) # logging.debug(f"Created inference grid {tuple(self.grid.shape)}") x = x.view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() if self.training: return x else: y = x.clone() # inference output y[..., :2] = torch.sigmoid(y[..., :2]) + self.grid # xy y[..., 2:4] = torch.exp(y[..., 2:4]) * self.anchor_wh # wh y[..., :4] *= self.stride # scale back torch.sigmoid_(y[..., 4:]) # anchor and class scores? # return y.view(bs, -1, self.no), x # view [1, 3, 13, 13, 85] as [1, 507, 85] return y.view(bs, -1, self.no) # view [1, 3, 13, 13, 85] as [1, 507, 85]
def forward(self, x, out): bool_ASFF = False # https://arxiv.org/abs/1911.09516 if bool_ASFF: i, n = self.index, self.num_layers # index in layers, number of layers x = out[self.layers[i]] bs, _, ny, nx = x.shape # bs, 255, 13, 13 if (self.num_x, self.num_y) != (nx, ny): self.create_grids((nx, ny), x.device) # outputs and weights w = torch.sigmoid(x[:, -n:]) * (2 / n) # sigmoid weights (faster) # weighted bool_ASFF sum x = out[self.layers[i]][:, :-n] * w[:, i:i + 1] for j in range(n): if j != i: x += w[:, j:j + 1] * \ F.interpolate(out[self.layers[j]][:, :-n], size=[ny, nx], mode='bilinear', align_corners=False) elif ONNX_EXPORT: bs = 1 # batch size else: bs, _, ny, nx = x.shape # batch_size, predict_param(255), grid(13), grid(13) if (self.num_x, self.num_y) != (nx, ny) or hasattr( self, "grid") is False: # fix num_outputs grid bug self.create_grids((nx, ny), x.device) # p.view(batch_size, 255, 13, 13) -> (batch_size, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes + xywh) x = x.view(bs, self.num_anchors, self.num_outputs, self.num_y, self.num_x).permute(0, 1, 3, 4, 2).contiguous() # prediction if self.training: return x elif ONNX_EXPORT: # Avoid broadcasting for ANE operations m = self.num_anchors * self.num_x * self.num_y # 3* ng = 1. / self.ng.repeat(m, 1) grid = self.grid.repeat(1, self.num_anchors, 1, 1, 1).view(m, 2) anchor_wh = self.anchor_wh.repeat(1, 1, self.num_x, self.num_y, 1).view(m, 2) * ng x = x.view(m, self.num_outputs) x[:, :2] = (torch.sigmoid(x[:, 0:2]) + grid) * ng # x, y x[:, 2:4] = torch.exp(x[:, 2:4]) * anchor_wh # width, height x[:, 4:] = torch.sigmoid(x[:, 4:]) x[:, 5:] = x[:, 5:self.num_outputs] * x[:, 4:5] return x else: # inference io = x.clone() # inference output io[..., :2] = torch.sigmoid( io[..., :2]) + self.grid # xy 计算在feature map上的xy坐标 io[..., 2:4] = torch.exp( io[..., 2:4]) * self.anchor_wh # wh yolo method 计算在feature map上的wh io[..., :4] *= self.stride # 换算映射回原图尺度 torch.sigmoid_(io[..., 4:]) return io.view( bs, -1, self.num_outputs), x # view [1, 3, 13, 13, 85] as [1, 507, 85]
def forward(self, p, out): ASFF = False # https://arxiv.org/abs/1911.09516 if ASFF: i, n = self.index, self.nl # index in layers, number of layers p = out[self.layers[i]] bs, _, ny, nx = p.shape # bs, 255, 13, 13 if (self.nx, self.ny) != (nx, ny): self.create_grids((nx, ny), p.device) # outputs and weights # w = F.softmax(p[:, -n:], 1) # normalized weights w = torch.sigmoid(p[:, -n:]) * (2 / n) # sigmoid weights (faster) # w = w / w.sum(1).unsqueeze(1) # normalize across layer dimension # weighted ASFF sum p = out[self.layers[i]][:, :-n] * w[:, i:i + 1] for j in range(n): if j != i: p += w[:, j:j + 1] * \ F.interpolate(out[self.layers[j]][:, :-n], size=[ny, nx], mode='bilinear', align_corners=False) elif ONNX_EXPORT: bs = 1 # batch size else: bs, _, ny, nx = p.shape # bs, 255, 13, 13 if (self.nx, self.ny) != (nx, ny): self.create_grids((nx, ny), p.device) # p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes + xywh) p = p.view(bs, self.na, self.no, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction if self.training: return p elif ONNX_EXPORT: # Avoid broadcasting for ANE operations m = self.na * self.nx * self.ny ng = 1. / self.ng.repeat(m, 1) grid = self.grid.repeat(1, self.na, 1, 1, 1).view(m, 2) anchor_wh = self.anchor_wh.repeat(1, 1, self.nx, self.ny, 1).view( m, 2) * ng p = p.view(m, self.no) xy = torch.sigmoid(p[:, 0:2]) + grid # x, y wh = torch.exp(p[:, 2:4]) * anchor_wh # width, height p_cls = torch.sigmoid(p[:, 4:5]) if self.nc == 1 else \ torch.sigmoid(p[:, 5:self.no]) * torch.sigmoid(p[:, 4:5]) # conf return p_cls, xy * ng, wh else: # inference io = p.clone() # inference output io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid # xy io[..., 2:4] = torch.exp( io[..., 2:4]) * self.anchor_wh # wh yolo method io[..., :4] *= self.stride torch.sigmoid_(io[..., 4:]) return io.view( bs, -1, self.no), p # view [1, 3, 13, 13, 85] as [1, 507, 85]
def forward(self, pred, img_size): """forward""" if ONNX_EXPORT: batch_size = 1 else: batch_size, num_y, num_x = pred.shape[0], pred.shape[ -2], pred.shape[-1] if (self.num_x, self.num_y) != (num_x, num_y): self.create_grids(img_size, (num_x, num_y), pred.device, pred.dtype) # pred.view(batch_size, 255, 13, 13) -- > (bs, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes+xywh) pred = pred.view(batch_size, self.num_anchors, self.num_classes + 5, self.num_y, self.num_x) pred = pred.permute(0, 1, 3, 4, 2).contiguous() if self.training: return pred if ONNX_EXPORT: # Constants can not be broadcast, ensure correct shape! num_gu = self.num_grid.repeat( (1, self.num_anchors * self.num_x * self.num_y, 1)) grid_xy = self.grid_xy.repeat((1, self.num_anchors, 1, 1, 1)).view( (1, -1, 2)) anchor_wh = self.anchor_wh.repeat( (1, 1, self.num_x, self.num_y, 1)).view((1, -1, 2)) / num_gu pred = pred.view(-1, 5 + self.num_classes) center_xy = torch.sigmoid(pred[..., 0:2]) + grid_xy[0] # x, y w_h = torch.exp(pred[..., 2:4]) * anchor_wh[0] # width, height pred_conf = torch.sigmoid(pred[:, 4:5]) # conf pred_cls = F.softmax(pred[:, 5:85], 1) * pred_conf # SSD-like conf return torch.cat((center_xy / num_gu[0], w_h, pred_conf, pred_cls), 1).t() # inference # s = 1.5 , scale_xy (pxy = pxy * s - (s - 1) / 2) infer_output = pred.clone() # inference output infer_output[..., 0:2] = torch.sigmoid( infer_output[..., 0:2]) + self.grid_xy # xy infer_output[..., 2:4] = torch.exp( infer_output[..., 2:4]) * self.anchor_wh # wh yolo method infer_output[..., :4] *= self.stride if 'default' in self.arc: # seperate obj and cls torch.sigmoid_(infer_output[..., 4:]) elif 'BCE' in self.arc: # unified BCE (80 classes) torch.sigmoid_(infer_output[..., 5:]) infer_output[..., 4] = 1 elif 'CE' in self.arc: # unified CE (1 background + 80 classes) infer_output[..., 4:] = F.softmax(infer_output[..., 4:], dim=4) infer_output[..., 4] = 1 if self.num_classes == 1: infer_output[ ..., 5] = 1 # single-class model https://github.com/ultralytics/yolov3/issues/235 # reshape from [1, 3, 13, 13, 85] to [1, 507, 85] return infer_output.view(batch_size, -1, 5 + self.num_classes), pred
def forward(self, g, g_prime): concat = torch.cat([g, g_prime], dim=-1) lin1_res = self.lin1(concat) torch.sigmoid_(lin1_res) lin2_res = self.lin2(concat) pred = lin1_res * lin2_res return pred
def __call__(self, epoch): for i in range(epoch): sumLoss = 0 # last_loss = 10 for j, (img, lable) in enumerate(self.dataloader): img, lable = img.to(DEVICE), lable.to(DEVICE) predict = self.net(img) if self.imgsize == 12: predict = predict.reshape(-1, 5) torch.sigmoid_(predict[:, 0]) # 置信度损失 mask = lable[:, 0] < 2 indexC = mask.nonzero()[:, 0] preCond = predict[indexC][:, 0] actCond = lable[indexC][:, 0] condLoss = torch.mean((preCond - actCond)**2) # 标注框偏移量损失 mask = lable[:, 0] > 0 indexO = mask.nonzero()[:, 0] preBoxOf = predict[indexO][:, 1:5] actBoxOf = lable[indexO][:, 1:5] boxLoss = torch.mean((preBoxOf - actBoxOf)**2) #只有O网络训练五官偏移量 # 计算五官偏移量的损失 ofLdMLoss = 0 if self.imgsize == 48: ofLdMPre = predict[indexO][:, 5:] ofLdMAct = lable[indexO][:, 5:] ofLdMLoss = torch.mean((ofLdMPre - ofLdMAct)**2) loss = condLoss + boxLoss + ofLdMLoss self.opt.zero_grad() loss.backward() self.opt.step() sumLoss += loss.cpu().detach().item() print("批次:", j, "损失:", loss.detach().item(), condLoss.detach().item(), boxLoss.detach().item(), ofLdMLoss) avg_loss = sumLoss / len(self.dataloader) print("批次:", i, "损失:", avg_loss) # if last_loss > avg_loss: # last_loss = avg_loss if self.imgsize == 12: torch.save(self.net.state_dict(), "./param/pnet.pt") elif self.imgsize == 24: torch.save(self.net.state_dict(), "./param/rnet.pt") else: torch.save(self.net.state_dict(), "./param/onet.pt")
def forward(self, p, img_size, var=None): if ONNX_EXPORT: bs = 1 # batch size else: bs, _, ny, nx = p.shape # bs, 255, 13, 13 if (self.nx, self.ny) != (nx, ny): create_grids(self, img_size, (nx, ny), p.device, p.dtype) # p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes + xywh) p = p.view(bs, self.na, self.no, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction if self.training: return p elif ONNX_EXPORT: # Constants CAN NOT BE BROADCAST, ensure correct shape! m = self.na * self.nx * self.ny grid_xy = self.grid_xy.repeat((1, self.na, 1, 1, 1)).view(m, 2) anchor_wh = self.anchor_wh.repeat( (1, 1, self.nx, self.ny, 1)).view(m, 2) / self.ng p = p.view(m, self.no) xy = torch.sigmoid(p[:, 0:2]) + grid_xy # x, y wh = torch.exp(p[:, 2:4]) * anchor_wh # width, height p_cls = torch.sigmoid(p[:, 5:self.no]) * torch.sigmoid( p[:, 4:5]) # conf return p_cls, xy / self.ng, wh else: # inference # s = 1.5 # scale_xy (pxy = pxy * s - (s - 1) / 2) io = p.clone() # inference output io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid_xy # xy io[..., 2:4] = torch.exp( io[..., 2:4]) * self.anchor_wh # wh yolo method # io[..., 2:4] = ((torch.sigmoid(io[..., 2:4]) * 2) ** 3) * self.anchor_wh # wh power method io[..., :4] *= self.stride if 'default' in self.arc: # seperate obj and cls # class dist should be a probability dist io[..., 5:] = F.softmax(io[..., 5:], dim=4) # objectness is between 0 and 1 torch.sigmoid_(io[..., 4]) elif 'BCE' in self.arc: # unified BCE (80 classes) torch.sigmoid_(io[..., 5:]) io[..., 4] = 1 elif 'CE' in self.arc: # unified CE (1 background + 80 classes) io[..., 4:] = F.softmax(io[..., 4:], dim=4) io[..., 4] = 1 if self.nc == 1: io[..., 5] = 1 # single-class model https://github.com/ultralytics/yolov3/issues/235 # reshape from [1, 3, 13, 13, 85] to [1, 507, 85] return io.view(bs, -1, self.no), p
def forward(self, g, g_prime): # print('g : ', g.shape) # print('g_prime : ', g_prime.shape) concat = torch.cat([g, g_prime], dim=-1) # print(concat.shape) lin1_res = self.lin1(concat) torch.sigmoid_(lin1_res) lin2_res = self.lin2(concat) pred = lin1_res * lin2_res return pred
def test_sigmoid(self): x = torch.randn(4, 5, dtype=torch.float32) * 10 mkldnn_x = x.to_mkldnn() self.assertEqual( torch.sigmoid(x), torch.sigmoid(mkldnn_x).to_dense(), ) # inplace torch.sigmoid_(x) torch.sigmoid_(mkldnn_x) self.assertEqual(x, mkldnn_x.to_dense())
def filter(self, output, clsm): output = output.permute(0, 2, 3, 1) output = output.reshape(output.size(0), output.size(1), output.size(2), 3, -1) output = output.cpu().data torch.sigmoid_(output[..., 0]) # 置信度加sigmoid激活 torch.sigmoid_(output[..., 1:3]) # 中心点加sigmoid激活 mask = output[..., 0] > clsm idxs = mask.nonzero() vecs = output[mask] return idxs, vecs
def _filter(output, thresh): output = output.permute(0, 2, 3, 1) output = output.reshape(*output.shape[:3], 3, -1) output = output.cpu() torch.sigmoid_(output[..., 4]) torch.sigmoid_(output[..., :2]) mask = output[..., 4] > thresh idxs = mask.nonzero() vecs = output[mask] return idxs, vecs
def test_sigmoid(self): ipex.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) x_cpu = torch.randn(4, 5, dtype=torch.float32) * 10 x_dpcpp = x_cpu.to(device=device) self.assertEqual(torch.sigmoid(x_cpu), torch.sigmoid(x_dpcpp)) # inplace torch.sigmoid_(x_cpu) torch.sigmoid_(x_dpcpp) self.assertEqual(x_cpu, x_dpcpp)
def forward(self, x): residual = x basicblock = self.conv_a(x) basicblock = self.bn_a(basicblock) basicblock = torch.sigmoid_(basicblock) basicblock = self.conv_b(basicblock) basicblock = self.bn_b(basicblock) if self.downsample is not None: residual = self.downsample(x) return torch.sigmoid_(residual + basicblock)
def forward(self, p, img_size): # print(p.shape) bs, _, ny, nx = p.shape if (self.nx, self.ny) != (nx, ny): create_grids(self, img_size, (nx, ny), p.device, p.dtype) p = p.view(bs, self.na, self.no, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction io = p.clone() # inference output io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid_xy # xy io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh # wh yolo method io[..., :4] *= self.stride torch.sigmoid_(io[..., 4:]) return io.view(bs, -1, self.no), p
def filter(self, input, threshold): input = input.permute(0, 2, 3, 1) input = input.reshape(input.size(0), input.size(1), input.size(2), 3, -1) # 置信度加sigmoid激活,将值压缩在0.5到0.7311之间,加快调置信度筛选框的速度 torch.sigmoid_(input[..., 4]) # 求出符合要求框所在的索引 mask = torch.gt(input[..., 4], threshold) # 1*13*13*3 # 获取使用建议框的索引值,以及中心点的索引值 indexs = torch.nonzero(mask) # n*4 # 根据索引获取详细值(中心点偏移量,宽高偏移量,置信度以及类别) outputs = input[mask] # n*15 return indexs, outputs
def test_sigmoid_(self): rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) x_auto_mix = torch.randn(4, 5, dtype=torch.float32, device=device) * 10 x_man_bf16 = x_auto_mix.to(torch.bfloat16) with AutoDNNL(True), AutoMixPrecision(False): torch.sigmoid_(x_man_bf16) with AutoMixPrecision(True): torch.sigmoid_(x_auto_mix) self.assertEqual(x_auto_mix.dtype, torch.float) self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix)) self.assertEqual(x_auto_mix, x_man_bf16.float())
def _filter(self, output, thresh): output = output.permute(0,2,3,1) output = output.reshape(output.shape[0],output.shape[1],output.shape[2],3,-1) output = output.cpu() #计算置信度损失 torch.sigmoid_(output[...,4]) torch.sigmoid_(output[...,0:2]) mask = output[...,4] > thresh idxs = mask.nonzero() vecs = output[mask] return idxs, vecs
def sdp(self, hidden: dict, graph=True): # 语义依存 sdp_arc, sdp_label, _ = self.model.sdp_decoder(hidden['word_cls_input'], hidden['word_length']) sdp_arc = torch.sigmoid_(sdp_arc) if graph: # 语义依存图 sdp_arc.transpose_(-1, -2) sdp_root_mask = sdp_arc[:, 0].argmax(dim=-1).unsqueeze_(-1).expand_as(sdp_arc[:, 0]) sdp_arc[:, 0] = 0 sdp_arc[:, 0].scatter_(dim=-1, index=sdp_root_mask, value=1) sdp_arc_T = sdp_arc.transpose(-1, -2) sdp_arc_fix = sdp_arc_T.argmax(dim=-1).unsqueeze_(-1).expand_as(sdp_arc) sdp_arc = ((sdp_arc_T > 0.5) & (sdp_arc_T > sdp_arc)). \ scatter_(dim=-1, index=sdp_arc_fix, value=True) else: # 语义依存树 sdp_arc_fix = eisner(sdp_arc, hidden['word_cls_mask']).unsqueeze_(-1).expand_as(sdp_arc) sdp_arc = torch.zeros_like(sdp_arc, dtype=torch.bool).scatter_(dim=-1, index=sdp_arc_fix, value=True) sdp_label = torch.argmax(sdp_label, dim=-1) word_cls_mask = hidden['word_cls_mask'] word_cls_mask = word_cls_mask.unsqueeze(-1).expand(-1, -1, word_cls_mask.size(1)) sdp_arc = sdp_arc & word_cls_mask sdp_label = self._get_graph_entities(sdp_arc, sdp_label, self.sdp_vocab) return sdp_label
def silu(input, inplace = False): ''' Applies the Sigmoid Linear Unit (SiLU) function element-wise: .. math:: SiLU(x) = x * sigmoid(x) See additional documentation for :mod:`echoAI.Activation.Torch.silu`. ''' if inplace: result = input.clone() torch.sigmoid_(input) input *= result else: return input * torch.sigmoid(input)
def forward(self, x): x_compress = self.compress(x) x_out = self.spatial(x_compress) scale = torch.sigmoid_(x_out) # print('scale') # print(scale.size()) return x * scale
def forward(self, x): t0, t1 = torch.chunk(x, ngates, dim=1) t0 = torch.tanh_(t0) t1.sub_(2) t1 = torch.sigmoid_(t1) return t1 * t0
def forward(self, x: Tensor, style: Tensor) -> Tensor: x = self.encoder(x) for layer in self.layers: x = layer(x, style) return torch.sigmoid_(x)
def sdp(self, hidden: dict, graph=True): """ 语义依存图(树) Args: hidden: 分词时所得到的中间表示 graph: 选择是语义依存图还是语义依存树结果 Returns: 语义依存图(树)结果 """ word_attention_mask = hidden['word_cls_mask'] sdp_arc, sdp_label = self.model.sdp_classifier( input=hidden['word_cls_input'], word_attention_mask=word_attention_mask[:, 1:])[0] sdp_arc[:, 0, 1:] = float('-inf') sdp_arc.diagonal(0, 1, 2)[1:].fill_(float('-inf')) # 避免自指 sdp_label = torch.argmax(sdp_label, dim=-1) if graph: # 语义依存图 sdp_arc = torch.sigmoid_(sdp_arc) > 0.5 else: # 语义依存树 sdp_arc_idx = eisner( sdp_arc, word_attention_mask).unsqueeze_(-1).expand_as(sdp_arc) sdp_arc = torch.zeros_like(sdp_arc, dtype=torch.bool).scatter_( -1, sdp_arc_idx, True) sdp_arc[~word_attention_mask] = False sdp_label = get_graph_entities(sdp_arc, sdp_label, self.sdp_vocab) return sdp_label