google / aiyprojects-raspbian

API libraries, samples, and system images for AIY Projects (Voice Kit and Vision Kit)
https://aiyprojects.withgoogle.com/
Apache License 2.0
1.63k stars 694 forks source link

Retrained embedded mobilenet v1 SSD causing AssertionError when running the object detection demo #563

Open di-jabil opened 5 years ago

di-jabil commented 5 years ago

Hi All,

I have retrained an embedded mobilenet v1 SSD following the AIY tutorial for training an object detection model (https://aiyprojects.withgoogle.com/vision/#makers-guide--tensorflow-model-compiler). The retrained model aims to detect one single object class.

The training went well. I was able to compile the model, load the binaryproto to the AIY kit, and test it with any_model_camera.py.

Then I decided to try this model with the object detection demo. I modified AIY-projects-python/src/aiy/vision/models/object_detection.py by changing _COMPUTE_GRAPH_NAME to my own binaryproto.

Then running the object detection demo ~/AIY-projects-python/src/examples/vision/object_detection.py --input input.jpg
got me the follow error: Traceback (most recent call last): File "/home/pi/AIY-projects-python/src/examples/vision/object_detection.py", line 73, in main() File "/home/pi/AIY-projects-python/src/examples/vision/object_detection.py", line 59, in main objects = object_detection.get_objects(result, args.threshold, offset) File "/opt/aiy/projects-python/src/aiy/vision/models/object_detection.py", line 269, in get_objects objs = _decode_detection_result(logit_scores, box_encodings, threshold, size, offset) File "/opt/aiy/projects-python/src/aiy/vision/models/object_detection.py", line 88, in _decode_detection_result assert len(box_encodings) == 4 * _NUM_ANCHORS AssertionError

Is there something wrong with my model? I would much appreciate your advice and comments. Please let me know if you need any further information. Thank you so much

The AIY image is AIY Kits Release 2018-11-16

di-jabil commented 5 years ago

Hi All,

Sorry for the vague question and little information provided yesterday. I just did more investigations and would like to provide more information.

Following the instructions here(https://cogint.ai/custom-vision-training-on-the-aiy-vision-kit/), I made the following changes to ~/AIY-projects-python/src/aiy/vision/models/object_detection.py 1, added a constant _NUM_LABELS

_ANCHORS = utils.load_ssd_anchors('mobilenet_ssd_256res_0.125_person_cat_dog_anchors.txt') _NUM_ANCHORS = len(_ANCHORS) _NUM_LABELS = 2 #added

2, changed the labels to match my model that only has a single object

class Object: """Object detection result.""" BACKGROUND = 0 TARGET = 1

_LABELS = {
    BACKGROUND: 'BACKGROUND',
    TARGET: 'TARGET',

3, changed the assertion statement at line 84; added a print statement to see their values

print(_NUM_ANCHORS, len(logit_scores), len(box_encodings) ) #added

assert len(logit_scores) == 4 * _NUM_ANCHORS

assert len(logit_scores) == _NUM_LABELS * _NUM_ANCHORS

4, changed the index at line 91

logits = logit_scores[4 i: 4 (i + 1)]

logits = logit_scores[_NUM_LABELS i: _NUM_LABELS (i + 1)]

Then I run the object detection demo ~/AIY-projects-python/src/examples/vision/object_detection.py --input target1s.jpg (500x500 jpg) and it still gave this error:

File "/opt/aiy/projects-python/src/aiy/vision/models/object_detection.py", line 90, in _decode_detection_result assert len(logit_scores) == _NUM_LABELS * _NUM_ANCHORS #di added AssertionError

The printed values are: _NUM_ANCHORS = 1278, len(logit_scores) = 5112, len(logit_scores) = 2556

I decided to try another binaryproto. So I downloaded the binaryproto from the pikachu detector (https://github.com/giacomobartoli/vision-kit/tree/master/pikachu-detector) and loaded it up.

With the pikachu detector binaryproto, the assertion error goes away and the printed values are _NUM_ANCHORS = 1278, len(logit_scores) = 2556, len(logit_scores) = 5112

So seems like the problem really is with my binaryproto. But I couldn't figure how & why. I followed the tutorial on the AIY homepage to train this embedded mobilenet v1 ssd model. I trained it on Google Cloud and everything went well. I was able to compile it with this following script

./bonnet_model_compiler.par \ --frozen_graph_path=my_detector.pb \ --output_graph_path=my_detector.binaryproto \ --input_tensor_name="Preprocessor/sub" \ --output_tensor_names="concat,concat_1" \ --input_tensor_size=256 \ --debug

So could you please help advise what could have gone wrong? I would much appreciate any comments. Thank you so much

My frozen graph is attached here target_detector.zip