Ink
Ink

Reputation: 963

Any channel wise pooling implementation with caffe?

I got nothing from google or github.

Up to now, I have to slice two blobs with shape [N,C,H,W] to 2*C blobs with shape [N,1,H,W], and permute new blobs to shape [N, H, W, 1], then pooling with kernel size=1 on the new blobs. And concatenate to [N,H,W,C] and permute to [N,C,H,W] finally.

Any good channel wise pooling implementation?

Upvotes: 0

Views: 215

Answers (1)

Dmytro Prylipko
Dmytro Prylipko

Reputation: 5084

For me it sounds like not a channel-wise pooling (which must produce a single output channel), but element-wise MAX operation:

layer {
  name: "input_1"
  type: "Input"
  top: "input_1"
  input_param {
    shape {
      dim: 1
      dim: 2
      dim: 3
      dim: 3
    }
  }
}

layer {
  name: "input_2"
  type: "Input"
  top: "input_2"
  input_param {
    shape {
      dim: 1
      dim: 2
      dim: 3
      dim: 3
    }
  }  
}

layer {
   name: "channel_max"
   type: "Eltwise"
   bottom: "input_1"
   bottom: "input_2"
   top: "channel_max"
   eltwise_param {
      operation: MAX
   }
}

The following code:

import caffe
import numpy as np


caffe.set_mode_cpu()
net = caffe.Net('net.prototxt', 1)

net.blobs['input_1'].data[...] = np.random.randint(10, size=(1, 2, 3, 3))
net.blobs['input_2'].data[...] = np.random.randint(10, size=(1, 2, 3, 3))

net.forward()
print('Blob #1:')
print(net.blobs['input_1'].data)
print('Blob #2:')
print(net.blobs['input_2'].data)

print('Result:')
print(net.blobs['channel_max'].data)

Merges the two blobs into one with the same number of channels filled with max values of the feature maps:

Blob #1:
[[[[5. 6. 5.]
   [1. 6. 1.]
   [4. 7. 6.]]

  [[9. 8. 3.]
   [8. 8. 8.]
   [6. 9. 9.]]]]
Blob #2:
[[[[4. 1. 1.]
   [2. 1. 3.]
   [6. 1. 0.]]

  [[3. 8. 7.]
   [8. 2. 4.]
   [2. 8. 1.]]]]
Result:
[[[[5. 6. 5.]
   [2. 6. 3.]
   [6. 7. 6.]]

  [[9. 8. 7.]
   [8. 8. 8.]
   [6. 9. 9.]]]]

Upvotes: 0

Related Questions