2
$\begingroup$

According to the original paper, the input size of the YOLO network layer is 448x448x3 and after the filter (7x7x64-s-2) is applied the output shape is to be 221x221x192 as I suppose. Some sources assert that the output shape is 224x224x192 but how is it possible if we don't use the kernel (2x2x64-s-2)?

And I want to implement it using keras. But my code doesn't allow to obtain correct size of the next layer it gives (None, 221, 221, 64)

model = Sequential()
# The 1st layer
model.add(Conv2D(filters=64, kernel_size=7,
                 strides=2, input_shape=(448,448,3)))
model.add(LeakyReLU(alpha=0.1))
$\endgroup$
8
  • $\begingroup$ 224x224 is because of the stride of the convolution which is 2 (I think in the paper, that's why the convolutional layer is 7x7x64-s-2. I guess it uses padding to have size 224x224 and not 221x221 $\endgroup$ Nov 13 '18 at 13:52
  • $\begingroup$ @JérémyBlain, but but when the 7x7 kernels with stride=2 moves over the 448x448 image it yields the 221x221 image, then the maxpool layer gives the 110x110 image. Am I wrong? $\endgroup$ Nov 13 '18 at 13:59
  • $\begingroup$ if you don't apply padding in your image, I think yes. usually we apply this padding, that way, activation maps have the same size as the inputs. Look at this for padding (it's an arugment for Conv2d object) : keras.io/layers/convolutional $\endgroup$ Nov 13 '18 at 14:02
  • $\begingroup$ maybe look at this video to understand padding : fr.coursera.org/lecture/convolutional-neural-networks/… $\endgroup$ Nov 13 '18 at 14:06
  • $\begingroup$ Oh, thanks I didn't know there is the same padding in the paper $\endgroup$ Nov 13 '18 at 14:10
1
$\begingroup$

Usually, when we use a CNN, we apply padding with convolution, that way, activation maps have the same size as the inputs.

Look at this video to understand how padding works : Andrew Ng course on Coursera about padding (you need an account to watch the full video )

In Keras, Conv2D layer have an argument called 'padding', here is a link to the documentation : Convolutional layer documentation of Keras

Be aware because they say that setting padding to 'same' when using a stride different than 1 (it's your case here) can be inconsistent depending on the backends you use. I let you try to see if the second layers have the right shape.

That way, your conv layer output should be 224x224 like in the paper (and 112x112 after the maxpool layer)

Note : Be careful with your filters number. You set the number of filters to 64, but in the paper, the filters number is 192 (I guess it's 64*3 as there are 3 channels ?)

$\endgroup$
1
$\begingroup$

You will have to look into the sources. From below, if pad != 0 then padding = size/2 = 7/2 = 3. If pad = 0, then whatever padding was passed through cfg/yolov1.cfg. Phew!

Breakpoint 1, parse_convolutional (options=0x5555557f18c0, params=...) at ./src/parser.c:180
180 {
(gdb) n
181     int n = option_find_int(options, "filters",1);
(gdb) n
182     int size = option_find_int(options, "size",1);
(gdb) n
183     int stride = option_find_int(options, "stride",1);
(gdb) n
184     int pad = option_find_int_quiet(options, "pad",0);
(gdb) n
185     int padding = option_find_int_quiet(options, "padding",0);
(gdb) print pad
$1 = 1
(gdb) print padding
$2 = 1436698242
(gdb) n
186     int groups = option_find_int_quiet(options, "groups", 1);
(gdb) print padding
$3 = 0
(gdb) n
187     if(pad) padding = size/2;
(gdb) n
189     char *activation_s = option_find_str(options, "activation", "logistic");
(gdb) print padding
$4 = 3
(gdb) print size
$5 = 7
(gdb) 
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.