pjreddie / darknet

Convolutional Neural Networks
http://pjreddie.com/darknet/
Other
25.78k stars 21.33k forks source link

reorg_cpu seems unusual #344

Open hughsando opened 6 years ago

hughsando commented 6 years ago

I have been studying the reorg_cpu code, and it seem a little odd. My interpretation of it is (ignoring batch, 'reverse' (inference) only):

  void reorg_cpu(int *x, int smallW, int smallH, int smallChannels, int stride, int *out)
   {
      bool forward = false;

      int bigW = smallW * stride;
      int bigH = smallH * stride;
      int bigChannels = smallChannels/(stride*stride);

          for(int ch = 0; ch < smallChannels; ++ch){
              for(int sy = 0; sy < smallH; ++sy){
                  for(int sx = 0; sx < smallW; ++sx){
                      int in_index  = sx + smallW*(sy + smallH*ch);

                      // Pull consecutive channels ...
                      int bigChannel = ch % bigChannels;
                      int offset = ch / bigChannels;
                      // from a pattern like:  0 1
                      //                       2 3
                      int bigX = sx*stride + offset % stride;
                      int bigY = sy*stride + offset / stride;

                      int out_index = bigX + bigW*(bigY + bigH*bigChannel);

                      // not forwards
                      out[in_index] = x[out_index];
                  }
              }
          }
   }

Where the number of channels on the 'big' image is smaller than the number of channels on the 'small' image. However, the w,h,c passed in are the values from the 'big' image (26,26,64), not the 'small' image (13,13,256). So the 'out_c' becomes 16, not 256 and the components are distributed over several pixels. There is a 1-1 relation between input and output pixels, so there not actually a bug, but is seems to me you might get a better result by passing in the small (output) size rather than the large (input) size to maintain the convolutional nature of the algorithm. Of course, this would need the weights to change and retraining.

AlexeyAB commented 6 years ago

@hughsando Yes, the code for the reorg-layer on CPU/GPU makes strange permutations. Obviously, Joseph had in mind other logic of this function. There is required a new model with a fixed reorg2.0-layer. Or you can use models without reorg: tiny-yolo, yolo9000.

Also some review and discussions were here: https://github.com/opencv/opencv/pull/9705#discussion_r143136536

hughsando commented 6 years ago

Ok, thanks. Hopefully next next version will have even better performance. Feel free to close this one if you have moved on.

ysh329 commented 6 years ago

differ from Reshape of Caffe

Grabber commented 6 years ago

So... re-implement and share with community...

On Sun, Jan 28, 2018 at 10:59 AM, Shuai Yuan notifications@github.com wrote:

differ from Reshape of Caffe

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pjreddie/darknet/issues/344#issuecomment-361061199, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA9cxsWLAEKhaOAfoI_4-THi_PcZI9aks5tPG8rgaJpZM4QvLeM .

-- Regards,

Luiz Vitor Martinez Cardoso

"The only limits are the ones you place upon yourself"