2

I am trying to obtain the gradient of the loss with respect to an input image. In Caffe (the original), the following code achieved my goal:

### Load image into net's data layer
img = caffe.io.load_image(jpg_file)
transformed_image = transformer.preprocess('data', img)
net.blobs['data'].data[...] = transformed_image    # copy image data into memory for net

...

### Form the final label vector (one hot vector)
finalLabelVector = np.zeros((1,num_classes))
finalLabelVector[0,ground_truth_label] = 1

### Do a backward pass using finalLabelVector
# must have 'force_backward: true' set in deploy prototxt
backwardpassData = net.backward(**{net.outputs[0]: finalLabelVector})

# Get the derivative with respect to the input (data layer)
delta = backwardpassData['data']

What must I do using the Caffe2 Python interface to calculate the same delta as above?

Mink
  • 857
  • 2
  • 11
  • 24

0 Answers0