tf_average.py 910 B

123456789101112131415161718192021222324252627282930
  1. import chainer
  2. from chainer.functions.pooling.average_pooling_2d import AveragePooling2D
  3. from chainer import cuda
  4. class TFAveragePooling2D(AveragePooling2D):
  5. """
  6. The only thing, that changes is the CuDNN poolgin option from
  7. CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING
  8. to
  9. CUDNN_POOLING_AVERAGE_COUNT_EXCLUDE_PADDING
  10. Note: CuDNN is required for this Function
  11. """
  12. def __init__(self, *args, **kwargs):
  13. super(TFAveragePooling2D, self).__init__(*args, **kwargs)
  14. def create_pool_desc(self):
  15. assert chainer.should_use_cudnn('>=auto'), \
  16. "This function works only with CuDNN!"
  17. return cuda.cudnn.create_pooling_descriptor(
  18. (self.kh, self.kw), (self.sy, self.sx), (self.ph, self.pw),
  19. cuda.cuda.cudnn.CUDNN_POOLING_AVERAGE_COUNT_EXCLUDE_PADDING)
  20. def tf_average_pooling_2d(x, ksize, stride=None, pad=0):
  21. return TFAveragePooling2D(ksize, stride, pad, cover_all=False).apply((x,))[0]