Skip to content

No gradient defined for operation 'ExtractImagePatches' #2921

Closed

Description

@jyshee

I tried to use tf.extract_image_patches to implement a locally layer as #2605 said. However an error occurred:
LookupError: No gradient defined for operation 'local/ExtractImagePatches' (op type: ExtractImagePatches)

I checked that tf.space_to_depth has the gradient definition but tf.extract_image_patches does not. And it seems not easy for me to implement the gradient. Any advice?
@martinwicke @benoitsteiner @mrry

Activity

  1. sherrym commented on Jun 17, 2016

    @sherrym
    Contributor

    @jyshee go for it if you need it.

  2. added
    stat:contribution welcome
    on Jun 17, 2016
  3. jyshee commented on Jun 21, 2016

    @jyshee
    Author

    I tried to implement the gradient of tf.extract_image_patches using tf.reshape and tf.pad Ops as:

       @ops.RegisterGradient("ExtractImagePatches")
       def _ExtractImagePatchesGrad(op,grad):
          ksize=op.get_attr("ksizes")[1]
          im=grad
          batch_size = im.get_shape()[0].value
          h = im.get_shape()[1].value
          w = im.get_shape()[2].value
          ch = im.get_shape()[3].value
          h_o = h + (ksize - 1)
          w_o = w + (ksize - 1)
          ch_o = ch / ksize / ksize
          im_o = tf.Variable(tf.zeros([batch_size, h_o, w_o, ch_o]), trainable=False)
          for i in xrange(h):
             for j in xrange(w):
                res=tf.reshape(im[:,i,j,:],[-1,ksize,ksize,int(ch/ksize/ksize)])
                padres=tf.pad(res,[[0,0],[i,h_o-i-ksize],[j,w_o-j-ksize],[0,0]])
                im_o+=padres
          return tf.identity(im_o)```
    
    
    And it seems work...
    
  4. hughsalimbeni commented on Jun 27, 2016

    @hughsalimbeni

    I've hit this problem also. A quick test of the @jyshee solution fails in my example in the reshape line due to a mismatched number of elements

  5. jyshee commented on Jun 28, 2016

    @jyshee
    Author

    @hughsalimbeni could you show more details about your error when you test the codes above?

  6. hughsalimbeni commented on Jun 28, 2016

    @hughsalimbeni

    My code is rather messy and very far from a minimal example, but here it is:

        def make_x_hat(self, x):
            L, H = self.L, self.H
            #put two cols of zeros
            # TODO sort out messy tranpose
            padded = tf.pad(tf.transpose(x), [[0, 0], [1, 1]], 'CONSTANT')
            #make 4d and swap to make indexing work more nicely
            x4 = tf.expand_dims(tf.expand_dims(tf.transpose(padded), 0), 3)
            #rectangular patches of 2 (cols) by L+1 rows
            ksizes = [1, 2, L + 1, 1]
            strides = [1, ]*4
            rates = [1, ]*4
            patches = tf.extract_image_patches(x4, 'VALID', ksizes, strides, rates)
            #first layer: [0, :, 0, -L:] 
            #middle layers [0, :, 0,  1:-1]
            #final layer [0, :, 4, 1:L+1]
    
            raw = tf.squeeze(tf.transpose(patches, (0, 2, 1, 3)))
    
            ret = []
            ret.append(raw[:, 0, L+1:2*L+1])
            for h in range(1, H):
                ret.append(raw[:, h, 1:(2*L+1)])
            ret.append(raw[:, H, 1:L+1])
            return ret
    

    I was intending to use extract_image_patches to avoid a loop over h (I was going to combine the middle layers to a single array, if it had worked), but the performance gain is so minimal I just used this instead:

        def make_x_hat(self, x):
            x = tf.transpose(x)
            N, L, H = self.N, self.L, self.H    
            n = N + L 
            ret = []        
            a = []
            for l in range(L):
                a.append(x[l:n-L+l, 0])
            ret.append(tf.transpose(tf.pack(a)))
    
            for h in range(1, H):
                a = []
                b = []
                for l in range(L):
                    a.append(x[l:n-L+l, h])
                    b.append(x[l+1:n-L+l+1, h-1])
    
                A = tf.transpose(tf.pack(a))
                B = tf.transpose(tf.pack(b))
                ret.append(tf.concat(1, (B, A)))
    
            a = []
            for l in range(L):
                a.append(x[l+1:n-L+l+1, H-1])
            ret.append(tf.transpose(tf.pack(a)))
    
            return ret
    

    which works fine.

  7. nikhilmishra000 commented on Aug 4, 2016

    @nikhilmishra000
    Contributor

    I have an implementation of this gradient -- should I create a pull request?

  8. martinwicke commented on Aug 5, 2016

    @martinwicke
    Member

    Yes please, that would be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stat:contribution welcome

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions