{"action":"created","issue":{"url":"https://api.github.com/repos/caffe2/caffe2/issues/1840","repository_url":"https://api.github.com/repos/caffe2/caffe2","labels_url":"https://api.github.com/repos/caffe2/caffe2/issues/1840/labels{/name}","comments_url":"https://api.github.com/repos/caffe2/caffe2/issues/1840/comments","events_url":"https://api.github.com/repos/caffe2/caffe2/issues/1840/events","html_url":"https://github.com/caffe2/caffe2/issues/1840","id":292521067,"number":1840,"title":"How to compute gradient of loss with respect to input image?","user":{"login":"MatthewInkawhich","id":21960679,"avatar_url":"https://avatars0.githubusercontent.com/u/21960679?v=4","gravatar_id":"","url":"https://api.github.com/users/MatthewInkawhich","html_url":"https://github.com/MatthewInkawhich","followers_url":"https://api.github.com/users/MatthewInkawhich/followers","following_url":"https://api.github.com/users/MatthewInkawhich/following{/other_user}","gists_url":"https://api.github.com/users/MatthewInkawhich/gists{/gist_id}","starred_url":"https://api.github.com/users/MatthewInkawhich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MatthewInkawhich/subscriptions","organizations_url":"https://api.github.com/users/MatthewInkawhich/orgs","repos_url":"https://api.github.com/users/MatthewInkawhich/repos","events_url":"https://api.github.com/users/MatthewInkawhich/events{/privacy}","received_events_url":"https://api.github.com/users/MatthewInkawhich/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2018-01-29T18:57:57Z","updated_at":"2018-02-09T22:19:25Z","closed_at":null,"author_association":"NONE","body":"In Caffe (the original), I was able to obtain the gradient of the loss w.r.t. an input image using the net.backward() function.\r\n\r\n\t\t\r\n```\r\n### Load image into net's data layer\r\nimg = caffe.io.load_image(jpg_file)\r\ntransformed_image = transformer.preprocess('data', img)\r\nnet.blobs['data'].data[...] = transformed_image    # copy image data into memory for net\r\n\r\n...\r\n\r\n### Form the final label vector (one hot vector)\r\nfinalLabelVector = np.zeros((1,num_classes))\r\nfinalLabelVector[0,ground_truth_label] = 1\r\n\r\n### Do a backward pass using finalLabelVector\r\n# must have 'force_backward: true' set in deploy prototxt\r\nbackwardpassData = net.backward(**{net.outputs[0]: finalLabelVector})\r\n\r\n# Get the derivative with respect to the input (data layer)\r\ndelta = backwardpassData['data']\r\n```\r\n\r\nAfter executing this portion, delta contained the gradient of the loss with respect to the input image (and its label).\r\n\r\n**How can I achieve this in Caffe2?**\r\n\r\nThe workspace.RunNet() handles both forward and backward passes in the tutorials that I have read.\r\n\r\nThanks,\r\nMatt\r\n"},"comment":{"url":"https://api.github.com/repos/caffe2/caffe2/issues/comments/364585810","html_url":"https://github.com/caffe2/caffe2/issues/1840#issuecomment-364585810","issue_url":"https://api.github.com/repos/caffe2/caffe2/issues/1840","id":364585810,"user":{"login":"MatthewInkawhich","id":21960679,"avatar_url":"https://avatars0.githubusercontent.com/u/21960679?v=4","gravatar_id":"","url":"https://api.github.com/users/MatthewInkawhich","html_url":"https://github.com/MatthewInkawhich","followers_url":"https://api.github.com/users/MatthewInkawhich/followers","following_url":"https://api.github.com/users/MatthewInkawhich/following{/other_user}","gists_url":"https://api.github.com/users/MatthewInkawhich/gists{/gist_id}","starred_url":"https://api.github.com/users/MatthewInkawhich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MatthewInkawhich/subscriptions","organizations_url":"https://api.github.com/users/MatthewInkawhich/orgs","repos_url":"https://api.github.com/users/MatthewInkawhich/repos","events_url":"https://api.github.com/users/MatthewInkawhich/events{/privacy}","received_events_url":"https://api.github.com/users/MatthewInkawhich/received_events","type":"User","site_admin":false},"created_at":"2018-02-09T22:19:25Z","updated_at":"2018-02-09T22:19:25Z","author_association":"NONE","body":"@CriCL I am not sure if I understand your question. I was not trying to input or inject data into any specified layer, rather, I was trying to obtain a specific gradient blob.\r\n\r\nThat said, once you run a forward pass on alexnet, it would seem that you would be able to extract the features that you want using `workspace.FetchBlob(<relublob>)`"}}
{"actor":{"display_login":"MatthewInkawhich"}}