BLOB: why use offset rather than computing index with n and c?

163 views
Skip to first unread message

Philip H

unread,
Jul 8, 2015, 3:00:37 AM7/8/15
to caffe...@googlegroups.com
Here's an example from the forward pass of the pooling layer:

    // The main loop
    for (int n = 0; n < bottom[0]->num(); ++n) {
      for (int c = 0; c < channels_; ++c) {
        for (int ph = 0; ph < pooled_height_; ++ph) {
          for (int pw = 0; pw < pooled_width_; ++pw) {
            int hstart = ph * stride_h_ - pad_h_;
            int wstart = pw * stride_w_ - pad_w_;
            int hend = min(hstart + kernel_h_, height_);
            int wend = min(wstart + kernel_w_, width_);
            hstart = max(hstart, 0);
            wstart = max(wstart, 0);
            const int pool_index = ph * pooled_width_ + pw;
            for (int h = hstart; h < hend; ++h) {
              for (int w = wstart; w < wend; ++w) {
                const int index = h * width_ + w;
                if (bottom_data[index] > top_data[pool_index]) {
                  top_data[pool_index] = bottom_data[index];
                  if (use_top_mask) {
                    top_mask[pool_index] = static_cast<Dtype>(index);
                  } else {
                    mask[pool_index] = index;
                  }
                }
              }
            }
          }
        }
        // compute offset
        bottom_data += bottom[0]->offset(0, 1);
        top_data += top[0]->offset(0, 1);
        if (use_top_mask) {
          top_mask += top[0]->offset(0, 1);
        } else {
          mask += top[0]->offset(0, 1);
        }
      }
    }


As you can see, the blobs are indexed with an index calculated using height and width. In order to catch the correct num and channel, the pointers bottom_data etc are shifted by "one channel" (offset(0, 1)) after each iteration over one channel.

What is the idea behind this? Why not do const int index = ((n * channels_ + c) * height_ + h) * width_ + w; rather than const int index = h * width_ + w; ?

Thanks,
Phil


Michael Wilber

unread,
Jul 8, 2015, 9:25:09 AM7/8/15
to caffe...@googlegroups.com
Do you mean to ask "Why does the loop process each batch and channel one by one when it could assume that all of the images are contiguous in memory"? I'd guess readability since neither way seems to save computation. This is also easier to modify if the format ever changes.

But I'm not sure.

Philip H

unread,
Jul 8, 2015, 9:43:31 AM7/8/15
to caffe...@googlegroups.com
Not exactly. It totally does make sense to run the loops this way because that's how data is organized in a blob. What I mean is, you could change const int index = h * width_ + w; to const int index = ((n * channels_ + c) * height_ + h) * width_ + w; and just omit the offsetting after the 2 innermost loops. That would get you to the same position in memory. 
Reply all
Reply to author
Forward
0 new messages