Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. def _put_kernels_on_grid(self, kernel, grid_Y, grid_X, pad=1):
  2. '''
  3. Visualize conv. features as an image (mostly for the 1st layer).
  4. Place kernel into a grid, with some paddings between adjacent filters.
  5. Args:
  6. kernel: tensor of shape [Y, X, NumChannels, NumKernels]
  7. (grid_Y, grid_X): shape of the grid. Require: NumKernels == grid_Y * grid_X
  8. User is responsible of how to break into two multiples.
  9. pad: number of black pixels around each filter (between them)
  10.  
  11. Return:
  12. Tensor of shape [(Y+pad)*grid_Y, (X+pad)*grid_X, NumChannels, 1].
  13. '''
  14. # pad X and Y
  15. x1 = tf.pad(kernel, [[pad,0],[pad,0],[0,0],[0,0]] )
  16.  
  17. # X and Y dimensions, w.r.t. padding
  18. Y = kernel.get_shape()[0] + pad
  19. X = kernel.get_shape()[1] + pad
  20.  
  21. # put NumKernels to the 1st dimension
  22. x2 = tf.transpose(x1, (3, 0, 1, 2))
  23. # organize grid on Y axis
  24. x3 = tf.reshape(x2, tf.pack([grid_X, Y * grid_Y, X, 3]))
  25.  
  26. # switch X and Y axes
  27. x4 = tf.transpose(x3, (0, 2, 1, 3))
  28. # organize grid on X axis
  29. x5 = tf.reshape(x4, tf.pack([1, X * grid_X, Y * grid_Y, 3]))
  30.  
  31. # back to normal order (not combining with the next step for clarity)
  32. x6 = tf.transpose(x5, (2, 1, 3, 0))
  33.  
  34. # to tf.image_summary order [batch_size, height, width, channels],
  35. # where in this case batch_size == 1
  36. x7 = tf.transpose(x6, (3, 0, 1, 2))
  37.  
  38. # scale to [0, 1]
  39. x_min = tf.reduce_min(x7)
  40. x_max = tf.reduce_max(x7)
  41. x8 = (x7 - x_min) / (x_max - x_min)
  42.  
  43. return x8
  44.  
  45.  
  46. if i == 1: # if first channel
  47. grid = self._put_kernels_on_grid(kernel, 8, 8)
  48. tf.image_summary("filter"+str(i), grid)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement