def points2bbox(self, pts, y_first=True): """点集转换成包围框. :param pts: the input points sets (fields), each points set (fields) is represented as 2n scalar. :param y_first: if y_first=True, the point set is represented as [y1, x1, y2, x2 ... yn, xn], otherwise the point set is represented as [x1, y1, x2, y2 ... xn, yn]. :return: each points set is converting to a bbox [x1, y1, x2, y2]. """ pts_reshape = L.reshape(pts, (pts.shape[0], -1, 2, pts.shape[2], pts.shape[3])) pts_y = pts_reshape[:, :, 0, :, :] if y_first else pts_reshape[:, :, 1, :, :] pts_x = pts_reshape[:, :, 1, :, :] if y_first else pts_reshape[:, :, 0, :, :] if self.transform_method == 'minmax': # bbox_left = pts_x.min(dim=1, keepdim=True)[0] # bbox_right = pts_x.max(dim=1, keepdim=True)[0] # bbox_up = pts_y.min(dim=1, keepdim=True)[0] # bbox_bottom = pts_y.max(dim=1, keepdim=True)[0] # bbox = torch.cat([bbox_left, bbox_up, bbox_right, bbox_bottom], # dim=1) pass elif self.transform_method == 'partial_minmax': # pts_y = pts_y[:, :4, ...] # pts_x = pts_x[:, :4, ...] # bbox_left = pts_x.min(dim=1, keepdim=True)[0] # bbox_right = pts_x.max(dim=1, keepdim=True)[0] # bbox_up = pts_y.min(dim=1, keepdim=True)[0] # bbox_bottom = pts_y.max(dim=1, keepdim=True)[0] # bbox = torch.cat([bbox_left, bbox_up, bbox_right, bbox_bottom], # dim=1) pass elif self.transform_method == 'moment': pts_y_mean = L.reduce_mean(pts_y, dim=1, keep_dim=True) pts_x_mean = L.reduce_mean(pts_x, dim=1, keep_dim=True) pts_y_std = paddle.std(pts_y - pts_y_mean, axis=1, keepdim=True) pts_x_std = paddle.std(pts_x - pts_x_mean, axis=1, keepdim=True) moment_transfer = (self.moment_transfer * self.moment_mul) + ( self.moment_transfer.detach() * (1 - self.moment_mul)) moment_width_transfer = moment_transfer[0] moment_height_transfer = moment_transfer[1] half_width = pts_x_std * L.exp(moment_width_transfer) half_height = pts_y_std * L.exp(moment_height_transfer) bbox = L.concat([ pts_x_mean - half_width, pts_y_mean - half_height, pts_x_mean + half_width, pts_y_mean + half_height ], axis=1) else: raise NotImplementedError return bbox
def static(self): with paddle.static.program_guard(paddle.static.Program()): x = paddle.data('X', self.shape, self.dtype) out = paddle.std(x, self.axis, self.unbiased, self.keepdim) exe = paddle.static.Executor(self.place) res = exe.run(feed={'X': self.x}, fetch_list=[out]) return res[0]
def get_logic_from_model(name): x, _ = load_sample_audio_text() model = Wav2Vec2ForCTC(name, pretrained=True) model.eval() with paddle.no_grad(): logits = model(x) return float(paddle.mean(logits)), float(paddle.std(logits))
def dynamic(self, axis=None, keepdim=False, unbiased=True): with fluid.dygraph.guard(self._place): data = fluid.dygraph.to_variable(self._input) out = paddle.std(input=data, axis=axis, keepdim=keepdim, unbiased=unbiased) return out.numpy()
def static(self, axis=None, keepdim=False, unbiased=True): prog = fluid.Program() with fluid.program_guard(prog): data = fluid.data(name="data", dtype=self._dtype, shape=[None, 3, 4, 5]) out = prog.current_block().create_var(dtype=self._dtype, shape=[2, 3, 4, 5]) paddle.std(input=data, axis=axis, keepdim=keepdim, unbiased=unbiased, out=out) exe = fluid.Executor(self._place) return exe.run(feed={"data": self._input}, program=prog, fetch_list=[out])[0]
def test_alias(self): paddle.disable_static() x = paddle.to_tensor(np.array([10, 12], 'float32')) out1 = paddle.std(x).numpy() out2 = paddle.tensor.std(x).numpy() out3 = paddle.tensor.stat.std(x).numpy() self.assertTrue(np.allclose(out1, out2)) self.assertTrue(np.allclose(out1, out3)) paddle.enable_static()
def forward(self, x): # (b, c, h, w) b, c, h, w = x.shape x = paddle.reshape(x, [b * self.groups, -1, h, w]) xn = x * self.avg_pool(x) xn = paddle.sum(xn, axis=1, keepdim=True) t = paddle.reshape(xn, [b * self.groups, -1]) t = t - paddle.mean(t, axis=1, keepdim=True) std = paddle.std(t, axis=1, keepdim=True) + 1e-5 t = t / std t = paddle.reshape(t, [b, self.groups, h, w]) t = t * self.weight + self.bias t = paddle.reshape(t, (b * self.groups, 1, h, w)) x = x * self.sig(t) x = paddle.reshape(x, (b, c, h, w)) return x
def convertDistribution(self, x): mean, std = paddle.mean(x), paddle.std(x) y = (x - mean) * 0.2 / std return y.astype('float32')
def dygraph(self): paddle.disable_static() x = paddle.to_tensor(self.x) out = paddle.std(x, self.axis, self.unbiased, self.keepdim) paddle.enable_static() return out.numpy()