ManishSoni1908 / Mobilenet-ssd-keras

A Keras port of Single Shot MultiBox Detector as Mobilenet as a Backbone
62 stars 24 forks source link

how do I infer using the mobilenet? #9

Open skywo1f opened 4 years ago

skywo1f commented 4 years ago

If I try to use mobilenet.h5 to do inference, it tells me ValueError: Dimension 0 in both shapes must be equal, but are 1 and 84. Shapes are [1,1,512,63] and [84,512,1,1]. for 'Assign_175' (op: 'Assign') with input shapes: [1,1,512,63], [84,512,1,1].

skywo1f commented 4 years ago

thats for line 80: File "inference/infer_mobilenet_ssd.py", line 80, in

skywo1f commented 4 years ago

it seems to have something to do with training. Once I tried traiining my own net starting with the mobilenet.h5, thats when it started having problems. Similarly, when I tried the default converted_model.h5, it ran fine, but when I trained on my own dataset, thats when it broke. Is it because I am using a different number of classes (just 1 vs 20)? If so, the problem should occur on the training side not the inference side. Once I am on the inference side, there is just the architecture and the weights. The net knows nothing about the number of objects

skywo1f commented 4 years ago

Even If I just use the base example to train the voc mobilenet I still get that error... this guide is deprecated

aviziskind commented 4 years ago

I had the same error. I discovered that the weight size discrepancy is because the aspect_ratios variable is different between the train_mobilenet_ssd.py script and infer_mobilenet_ssd_py script. The first difference is that in the train script, the last two sets of aspect ratios have 3 elements ([1.0, 2.0, 0.5]] instead of 5 ([1.0, 2.0, 0.5, 3.0, 1.0 / 3.0]), (which causes a different error later on). the other difference is that the first set of aspect ratios is [1.001, 2.0, 0.5] instead of [1.0, 2.0, 0.5]. This seems to be a quick hack by the author, which, in essence, does the same thing as two_boxes_for_ar1=False for that lowest scale (since the code adds an extra box for aspect ratio 1 only if there is already a value of 1 in the list of aspect ratios, but the list of aspect ratios now has 1.001 instead of 1, so no extra box is added). the net result is that there are 3 boxes for aspect ratio 1 instead of 4, so with the pascal dataset (num_classes = 20+1 = 21), the weight size is 3num_classes = 63 instead of 4num_classes = 84, resulting in the observed weight size discrepancy ([1,1,512,63] vs [84,512,1,1]... it appears that keras or the h5py loading does some dimension reordering so that [1,1,512,84] becomes [84,512,1,1])

SmituSSupp commented 4 years ago

I had the same error. I discovered that the weight size discrepancy is because the aspect_ratios variable is different between the train_mobilenet_ssd.py script and infer_mobilenet_ssd_py script. The first difference is that in the train script, the last two sets of aspect ratios have 3 elements ([1.0, 2.0, 0.5]] instead of 5 ([1.0, 2.0, 0.5, 3.0, 1.0 / 3.0]), (which causes a different error later on). the other difference is that the first set of aspect ratios is [1.001, 2.0, 0.5] instead of [1.0, 2.0, 0.5]. This seems to be a quick hack by the author, which, in essence, does the same thing as two_boxes_for_ar1=False for that lowest scale (since the code adds an extra box for aspect ratio 1 only if there is already a value of 1 in the list of aspect ratios, but the list of aspect ratios now has 1.001 instead of 1, so no extra box is added). the net result is that there are 3 boxes for aspect ratio 1 instead of 4, so with the pascal dataset (num_classes = 20+1 = 21), the weight size is 3_num_classes = 63 instead of 4_num_classes = 84, resulting in the observed weight size discrepancy ([1,1,512,63] vs [84,512,1,1]... it appears that keras or the h5py loading does some dimension reordering so that [1,1,512,84] becomes [84,512,1,1])

looks like I`m facing similar problem So how did u solved this, and have u tried inference model on given examples?

aviziskind commented 4 years ago

make sure to use these aspect ratios if you're trying to load the ported caffe weights (converted_model.h5) aspect ratios = [[1.001, 2.0, 0.5], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0]].

SmituSSupp commented 4 years ago

make sure to use these aspect ratios if you're trying to load the ported caffe weights (converted_model.h5) aspect ratios = [[1.001, 2.0, 0.5], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0]].

Im sure what I use these aspect ratios. I just get 0 in y_pred_decoded[0]. So 0 objects found

SmituSSupp commented 4 years ago

I also get results of inference only if confidence_thresh = 0.0 and iou_threshold = 0.0

SmituSSupp commented 4 years ago

And can u call ur usage of this model succsessful?

make sure to use these aspect ratios if you're trying to load the ported caffe weights (converted_model.h5) aspect ratios = [[1.001, 2.0, 0.5], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0], [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0]].

aviziskind commented 4 years ago

When running the training script, i do not get the performance claimed in the Readme (I might be missing something, but in fact the process for loading mobilenet imagenet weights in the base network fails silently -- no weights are actually loaded, since none of the layer names match the names in the .h5 file). When running the evaluation script with the ported caffe weights, I only get 0.60 mAP instead of the 0.72 performance of the model. In general, I've had some success borrowing code from the original ssd-keras repo (vgg base network instead of mobilenet) (https://github.com/pierluigiferrari/ssd_keras/), since the code there seems better tested and documented.

SmituSSupp commented 4 years ago

When running the training script, i do not get the performance claimed in the Readme (I might be missing something, but in fact the process for loading mobilenet imagenet weights in the base network fails silently -- no weights are actually loaded, since none of the layer names match the names in the .h5 file). When running the evaluation script with the ported caffe weights, I only get 0.60 mAP instead of the 0.72 performance of the model. In general, I've had some success borrowing code from the original ssd-keras repo (vgg base network instead of mobilenet) (https://github.com/pierluigiferrari/ssd_keras/), since the code there seems better tested and documented.

Have u tried inference script instead of evaluating? The question is : “does this repo worth the time, spent on issues and troubleshooting” Because I need Keras ssd_mobilenet

aviziskind commented 4 years ago

I would recommend starting with https://github.com/pierluigiferrari/ssd_keras/ and getting that code running. Then copy over the mobilenet_v1.py code from this repo for an example of how to implement the mobilenet code as the base network.

SmituSSupp commented 4 years ago

I would recommend starting with https://github.com/pierluigiferrari/ssd_keras/ and getting that code running. Then copy over the mobilenet_v1.py code from this repo for an example of how to implement the mobilenet code as the base network.

Thank you for advice, will try it as soon as possible

SmituSSupp commented 4 years ago

I would recommend starting with https://github.com/pierluigiferrari/ssd_keras/ and getting that code running. Then copy over the mobilenet_v1.py code from this repo for an example of how to implement the mobilenet code as the base network.

But how to deal with pretrained model weights for mobilenet_ssd?

aviziskind commented 4 years ago

If you really want to use the ported weights, you could try build the caffe repo (https://github.com/chuanqi305/MobileNet-SSD) and inspect the outputs of each layer, and making sure it matches with the outputs of the corresponding keras model (they should be pretty close, since it gets 0.60 mAP, but there are obviously some minor differences in implementation that result in slightly degraded performance). Might just be easier to just train a new network, though.

aviziskind commented 4 years ago

Update: I tried running the evaluation script from the https://github.com/pierluigiferrari/ssd_keras/ repo (using code from this notebook: https://github.com/pierluigiferrari/ssd_keras/blob/master/ssd300_evaluation.ipynb) using the ported mobilenet-ssd weights, and get mAP of 0.68, which is a bit closer to the 0.72, so it seems as if at least some of the discrepancy is due to the evaluation script from this repo.

SmituSSupp commented 4 years ago

Big thanks for that, will try to run it on my data and examples too, for reproducing same results

SmituSSupp commented 4 years ago

@aviziskind Sorry for bothering again, but how do u load model weights in mentioned notebook? because I got next error: ValueError Traceback (most recent call last)

in () 9 'L2Normalization': L2Normalization, 10 'DecodeDetections': DecodeDetections, ---> 11 'compute_loss': ssd_loss.compute_loss}) 2 frames /usr/local/lib/python3.6/dist-packages/keras/utils/io_utils.py in __getitem__(self, attr) 300 else: 301 if self.read_only: --> 302 raise ValueError('Cannot create group in read only mode.') 303 val = H5Dict(self.data.create_group(attr)) 304 return val ValueError: Cannot create group in read only mode.
aviziskind commented 4 years ago

I'm not sure.. i didn't encounter that error. Looks like you're trying to run the code under section "1.2. Load a trained model" instead of "1.1. Build the model and load trained weights into it". Did you import the L2Normalization, etc. libraries from the first code block? (maybe keras isn't recognizing those L2Normalization or DecodeDetections modules which need to be imported) Did you try building the model using section 1.1 instead of loading it as in 1.2?

fader111 commented 4 years ago

Gentlemen, didn't u get any success with combining Mobilenet & SSD under keras? Heve you got the desired 0.72mAP from It?

SmituSSupp commented 4 years ago

Gentlemen, didn't u get any success with combining Mobilenet & SSD under keras? Heve you got the desired 0.72mAP from It?

Если Вы не возражаете, отвечу на русском. Говоря о совмещении Mobilenet&SSD, следует сказать, что моей целью не было получение указанного mAP. Требовалось получить рабочую модель. Вся проблема заключается в том, что прикрепленные в репозитории веса не совпадают с моделью, из-за чего приходится обучать модель с 0 на каком-либо датасете. Так что если Вашей целью является получение указанных метрик, то приблизительные им значения можно получить, обучив модель с самого начала. Надеюсь, мой ответ как-то помог.

fader111 commented 4 years ago

Спасибо.