Wednesday, 8 August 2018

Edit image as tensorflow tensor python

I'll do my best to provide a reproducible example here.

I have an image:

img

This image of Aaron Eckhart is (150, 150)

My goal is to perturb a ROI of this image by doing mathematical operations on the pixels, however, the issue is that the math must be done as a tensorflow tensor because the mathematical operation to be done is to multiply the tensor by it's scaled gradient (which is also a tensor of size (row_pixels, column_pixels, 3))

So here's the process I imagine:

  1. Read in image as numpy array RGB size: (1, 150, 150, 3) (1 is batch size)

    w, h = img.shape

    ret = np.empty((w, h, 3), dtype=np.uint8)

    ret[:, :, 0] = ret[:, :, 1] = ret[:, :, 2] = img

  2. Make pixel values between 0 and 1

    img = (faces1 - min_pixel) / (max_pixel - min_pixel)

  3. for i in range(steps):

(a) extract ROI of image this is the part I don't understand how to do

(b) calculate gradient of smaller img ROI tensor's loss

loss = utils_tf.model_loss(y, preds, mean=False)
grad, = tf.gradients(loss, x)

(c) multiply img ROI tensor by gradient of loss

scaled_grad = eps * normalized_grad
adv_img = img + scaled_grad

(d) place this newly perturbed ROI tensor back into the same positions it was in the original tensor this is the other part I don't understand how to do

This will result in an image where only some of the pixel values have been perturbed and the rest remain the same



from Edit image as tensorflow tensor python

No comments:

Post a Comment