thtrieu / darkflow

Translate darknet to tensorflow. Load trained weights, retrain/fine-tune using tensorflow, export constant graph def to mobile devices
GNU General Public License v3.0
6.13k stars 2.08k forks source link

Training YOLOv2 correct number of filters #414

Open AndresPMD opened 6 years ago

AndresPMD commented 6 years ago

Hello @thtrieu

As it is stated on the guide, if I want to use the tiny-yolo-voc.cfg I need to set up the formula according to the classes as:

num (classes + 5), being num = 5 and classes are 3 so 5 (3 + 5) = 40

But I am working in the YOLOv2 model (yolo.cfg). Can you help me out with the formula of the number of filters on the model for let say 1 class ?

I am working on text detection with some modifications to the YOLOv2 model. Thanks in advance and I will let you know any news.

AndresPMD commented 6 years ago

I have been checking the YOLOv1 and YOLOv2 paper, and the formula is as you described:

Bounding Boxes per each cell * (Coordinates + #Classes )

In YOLOv2 the number of bounding box predictions is 5, and the coordinates is 5 as well. But I am still having an issue of matrix shaping when training YOLOv2 with my own data.

Let me know if you can help.

The Error is the following:

File "/home/amafla/Documents/darkflow-master/darkflow/net/help.py", line 15, in build_train_op self.framework.loss(self.out) File "/home/amafla/Documents/darkflow-master/darkflow/net/yolov2/train.py", line 56, in loss net_out_reshape = tf.reshape(net_out, [-1, H, W, B, (4 + 1 + C)]) File "/home/amafla/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2619, in reshape name=name) File "/home/amafla/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op op_def=op_def) File "/home/amafla/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op original_op=self._default_original_op, op_def=op_def) File "/home/amafla/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1204, in init self._traceback = self._graph._extract_stack() # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 144400 values, but the requested shape requires a multiple of 10830 [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](output, Reshape/shape)]]

rlan commented 6 years ago

@andresmafladelgado Would you share your command-line and cfg files? Did you modify the code base?

AndresPMD commented 6 years ago

Thank you for your answer but I made it work. For now I am trying to understand the code in a deeper way.

theseus-david commented 6 years ago

@andresmafladelgado please explain how you made it work for other readers having the same issue

AndresPMD commented 6 years ago

@theseus-david Let me know how many classes you want to detect and some parameters so I can give you an explanation.

isaacdchan commented 6 years ago

@DreadPiratePsyopus I have the same problem trying to train on my own dataset. How do you know it's a problem with the filters? I am wishing to train 6 classes. What other parameters do you need?

jinchuika commented 6 years ago

@DreadPiratePsyopus How did you make it work? I'm trying to train using one class and I'm having the exact same issue.

AndresPMD commented 6 years ago

@jinchuika @isaacdchan Just follow the basic explanations on the main page. Basically: The formula: num (classes + 5); being num = 5 and classes are 3 so 5 (3 + 5) = 40

If you want to train one class: 5 * (1+5) = 30 So 30 will be your filter number and change the class to 1 in the cfg file that you are using.

I changed the code to suit my purpose, however I found that the original implementation is way faster to train but it is written on C. Let me know any other issue I can help you with.

jinchuika commented 6 years ago

Thanks for the response @DreadPiratePsyopus . Being a little noob here, how do I know what the num is?

My tiny-yolo-voc-p.cfg has 9 conv layers, and looks like this:

# first conv layer, filters=6
[convolutional]
batch_normalize=1
filters=6
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

# second conv layer, filters=12
[convolutional]
batch_normalize=1
filters=12
size=3
stride=1
pad=1
activation=leaky
....
#last conv layer, filters = 54
[convolutional]
size=1
stride=1
pad=1
filters=54
activation=linear
# classes changed to 1
[region]
anchors = 1.08,1.19,  3.42,4.41,  6.63,11.38,  9.42,5.11,  16.62,10.52
bias_match=1
classes=1
coords=4
num=5
softmax=1
jitter=.2
rescore=1

And so con until my final conv layer where: num (classes + 5) 9 (1 + 5) = 54

Thank you in advance.

sfarkya commented 6 years ago

@DreadPiratePsyopus What was the size of the image in your dataset? Did you change anything regarding that?

AndresPMD commented 6 years ago

The image size gets converted to 608x608 before it gets to the YOLO model. Also in the data augmentation section of the code the same idea is used. I didn't change anything about the image size, but you should consider that the YOLO model downsamples a image by a factor of 32 (5 max pooling layers) so small images or too big may be a problem.

AndresPMD commented 6 years ago

@jinchuika The num represents the number of bounding boxes proposed per grid cell. It does not represent the number of conv layers.

sfarkya commented 6 years ago

@DreadPiratePsyopus Yeah I was able to figure it out later after digging into the discussions, it's working fine now. Thanks for the confirmation.

pdhruv93 commented 5 years ago

@DreadPiratePsyopus I am using yolov2-tiny-voc_10000.weights and yolov2-tiny-voc.cfg in combination. Both of them I have downloaded from official darkflow site.

The deafult yolov2-tiny-voc.cfg file has filters=35 But for my custom dataset classes=1 so filters should be 30. Making it 30 straightaway gives error: AssertionError: expect 63082060 bytes, found 63102560

Here is my repo link: https://github.com/pdhruv93/RoadCrackDetector

Daniel-Peace commented 4 months ago

Hello! So I've been reading through this forum and have gathered that if I have 5 classes I will need 50 filters if my num value is 5. I am a bit confused, however, on how to modify the rest of the filter values in the config file for YOLOv2 tiny. There appears to be several convolutional layers each with their own filter value. How would I modify these values, if at all, based on my my number of classes? When I modify just the first layer, the training crashes. So I am guessing I am missing something. I am very new to this, so patience would be appreciated!

Daniel-Peace commented 4 months ago

Hello! So I've been reading through this forum and have gathered that if I have 5 classes I will need 50 filters if my num value is 5. I am a bit confused, however, on how to modify the rest of the filter values in the config file for YOLOv2 tiny. There appears to be several convolutional layers each with their own filter value. How would I modify these values, if at all, based on my my number of classes? When I modify just the first layer, the training crashes. So I am guessing I am missing something. I am very new to this, so patience would be appreciated!

Figured out my own mistake

LNG89811 commented 3 months ago

Hello! So I've been reading through this forum and have gathered that if I have 5 classes I will need 50 filters if my num value is 5. I am a bit confused, however, on how to modify the rest of the filter values in the config file for YOLOv2 tiny. There appears to be several convolutional layers each with their own filter value. How would I modify these values, if at all, based on my my number of classes? When I modify just the first layer, the training crashes. So I am guessing I am missing something. I am very new to this, so patience would be appreciated!

Figured out my own mistake

hi @Daniel-Peace , how did you fix the problem with filter and custom yolov2. I am doing custom yolov2 with resnet50 as backbone. And I got this problem, which i think it related to filter size and convolutional layer. image

Daniel-Peace commented 3 months ago

@LNG89811 I believe you want to leave all of the filter values as they are except for the last layer. The last layer is where you will change it to 50 filters. I misinterpreted a tutorial I was following and changed the number of filters in the first layer rather than the last layer and this caused the error for me. I also changed the number of classes towards the bottom of the file to the number of classes I have.

I am using yolov2-tiny-voc.cfg, just to make sure we are on the same page. I would imagine the process is similar though for other YOLOv2 cfgs.

[net]
# Testing
batch=64
subdivisions=8
# Training
# batch=64
# subdivisions=2
width=416
height=416
channels=3
momentum=0.9
decay=0.0005
angle=0
saturation = 1.5
exposure = 1.5
hue=.1

learning_rate=0.001
max_batches = 40200
policy=steps
steps=-1,100,20000,30000
scales=.1,10,.1,.1

[convolutional]
batch_normalize=1
filters=16
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=32
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=64
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=512
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=1

[convolutional]
batch_normalize=1
filters=1024
size=3
stride=1
pad=1
activation=leaky

###########

[convolutional]
batch_normalize=1
size=3
stride=1
pad=1
filters=1024
activation=leaky

[convolutional]
size=1
stride=1
pad=1
filters=45
activation=linear

[region]
anchors = 1.08,1.19,  3.42,4.41,  6.63,11.38,  9.42,5.11,  16.62,10.52
bias_match=1
classes=4
coords=4
num=5
softmax=1
jitter=.2
rescore=1

object_scale=5
noobject_scale=1
class_scale=1
coord_scale=1

absolute=1
thresh = .6
random=1