helpers.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import numpy as np
  2. import tensorflow as tf
  3. def kernel_initializer(kernels):
  4. """ Wrapper for an initializer of convolution weights.
  5. :return: Callable initializer object.
  6. """
  7. assert len(kernels.shape) == 3
  8. kernels = kernels.astype(np.float32)
  9. def _initializer(shape, dtype=tf.float32, partition_info=None):
  10. """Initializer function which is called from tensorflow internally.
  11. :param shape: Runtime / Construction time shape of the tensor.
  12. :param dtype: Data type of the resulting tensor.
  13. :param partition_info: Placeholder for internal tf call.
  14. :return: 4D numpy array with weights [filter_height, filter_width, in_channels, out_channels].
  15. """
  16. if shape:
  17. # second last dimension is input, last dimension is output
  18. fan_in = float(shape[-2]) if len(shape) > 1 else float(shape[-1])
  19. fan_out = float(shape[-1])
  20. else:
  21. fan_in = 1.0
  22. fan_out = 1.0
  23. assert fan_out == 1 and fan_in == kernels.shape[-1]
  24. # define weight matrix (set dtype always to float32)
  25. # weights = np.expand_dims(kernels, axis=2)
  26. weights = np.expand_dims(kernels, axis=-1)
  27. return weights
  28. return _initializer
  29. def bilinear_filter(filter_size=(4, 4)):
  30. """
  31. Make a 2D bilinear kernel suitable for upsampling of the given (h, w) size.
  32. Also allows asymmetric kernels.
  33. :param filter_size: Tuple defining the filter size in width and height.
  34. :return: 2D numpy array containing bilinear weights.
  35. """
  36. assert isinstance(filter_size, (list, tuple)) and len(filter_size) == 2
  37. factor = [(size + 1) // 2 for size in filter_size]
  38. # define first center dimension
  39. if filter_size[0] % 2 == 1:
  40. center_x = factor[0] - 1
  41. else:
  42. center_x = factor[0] - 0.5
  43. # define second center dimension
  44. if filter_size[1] % 2 == 1:
  45. center_y = factor[1] - 1
  46. else:
  47. center_y = factor[1] - 0.5
  48. og = np.ogrid[:filter_size[0], :filter_size[1]]
  49. kernel = (1 - abs(og[0] - center_x) / float(factor[0])) * (1 - abs(og[1] - center_y) / float(factor[1]))
  50. return kernel
  51. def get_default_session_config(memory_fraction=0.9):
  52. """ Returns default session configuration
  53. :param memory_fraction: percentage of the memory which should be kept free (growing is allowed).
  54. :return: tensorflow session configuration object
  55. """
  56. conf = tf.ConfigProto()
  57. conf.gpu_options.per_process_gpu_memory_fraction = memory_fraction
  58. conf.gpu_options.allocator_type = 'BFC'
  59. conf.gpu_options.allow_growth = True
  60. conf.allow_soft_placement = True