def compute_object_fabrication_gradient(self, x, detections=None): x_local = x.copy() * 255 x_tensor = t.from_numpy(preprocess(x_local[0].transpose( (2, 0, 1))))[None].cuda().float() x_tensor.requires_grad = True _bboxes = t.from_numpy(np.zeros((1, 1, 4))).float() _labels = t.from_numpy(np.zeros((1, 1))).int() _scale = at.scalar(np.asarray([1.])) losses = self.forward(x_tensor, _bboxes, _labels, _scale) self.optimizer.zero_grad() self.faster_rcnn.zero_grad() losses.object_fabrication_loss.backward() return x_tensor.grad.data.cpu().numpy().transpose((0, 2, 3, 1))
def compute_object_mislabeling_gradient(self, x, detections): x_local = x.copy() * 255 x_tensor = t.from_numpy(preprocess(x_local[0].transpose( (2, 0, 1))))[None].cuda().float() x_tensor.requires_grad = True _bboxes = t.from_numpy(detections[np.newaxis, :, [-3, -4, -1, -2]]).float() _labels = t.from_numpy(detections[np.newaxis, :, 0]).int() _scale = at.scalar(np.asarray([1.])) losses = self.forward(x_tensor, _bboxes, _labels, _scale) self.optimizer.zero_grad() self.faster_rcnn.zero_grad() losses.object_mislabeling_loss.backward() return x_tensor.grad.data.cpu().numpy().transpose((0, 2, 3, 1))
def compute_object_untargeted_gradient(self, x, detections): x_local = x.copy() * 255 x_tensor = t.from_numpy(preprocess(x_local[0].transpose( (2, 0, 1))))[None].cuda().float() x_tensor.requires_grad = True if detections is not None and len(detections) > 0: _bboxes = t.from_numpy(detections[np.newaxis, :, [-3, -4, -1, -2]]).float() _labels = t.from_numpy(detections[np.newaxis, :, 0]).int() else: _bboxes = t.from_numpy(np.zeros((1, 1, 4))).float() _labels = t.from_numpy(np.zeros((1, 1))).int() _scale = at.scalar(np.asarray([1.])) losses = self.forward(x_tensor, _bboxes, _labels, _scale) self.optimizer.zero_grad() self.faster_rcnn.zero_grad() if len(detections) > 0: losses.object_untargeted_loss.backward() else: losses.object_fabrication_loss.backward() return x_tensor.grad.data.cpu().numpy().transpose((0, 2, 3, 1))