google-coral / tflite

Examples using TensorFlow Lite API to run inference on Coral devices
https://coral.withgoogle.com
Apache License 2.0
182 stars 68 forks source link

Getting only 10 detections #44

Closed hannes56a closed 3 years ago

hannes56a commented 3 years ago

Hi all, I retrained the ssd_mobilNet_v1 model with own data (4 classes) (following the google tutorial for the coral chip: https://coral.ai/docs/edgetpu/retrain-detection/#using-the-coral-usb-accelerator).

Now I´m testing the new model on my test images with the "detect_image.py" script. It basically works, but I got maximum 10 detections (boundingboxes). There are many more objects to find. I tested some things and there are every time maximum 10 detections in all (not 10 detections per class). If i "overpaint" these detections in the testimage, the model finds another 10 detections...

Perhaps some usefull information:

"max_detections_per_class" and "max_total_detections" in the "pipeline.config" for the retraining are both 100.

I do not find any parameter or reason for this behavior. Can please someone help me?

Greeting, Hannes

hannes56a commented 3 years ago

Nobody has an idea??

scottamain commented 3 years ago

When calling the detect_image.py script, did you try setting --threshold=0?

hannes56a commented 3 years ago

Hi, i will check it, but if this is only the threshold for detection output, I tried with 0.2 and the "not detected" objects have a score above 0.5 ...

hannes56a commented 3 years ago

I tried with --threshold=0, but it doesn´t help...

Namburger commented 3 years ago

@hannes56a can post your full pipeline.conf?

hannes56a commented 3 years ago
[pipeline.config.txt](https://github.com/google-coral/tflite/files/6149571/pipeline.config.txt)

# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

model {
  ssd {
    num_classes: 4
    image_resizer {
      fixed_shape_resizer {
        height: 300
        width: 300
      }
    }
    feature_extractor {
      type: "ssd_mobilenet_v1"
      depth_multiplier: 1.0
      min_depth: 16
      conv_hyperparams {
        regularizer {
          l2_regularizer {
            weight: 3.99999989895e-05
          }
        }
        initializer {
          random_normal_initializer {
            mean: 0.0
            stddev: 0.00999999977648
          }
        }
        activation: RELU_6
        batch_norm {
          decay: 0.97000002861
          center: true
          scale: true
          epsilon: 0.0010000000475
        }
      }
      override_base_feature_extractor_hyperparams: true
    }
    box_coder {
      faster_rcnn_box_coder {
        y_scale: 10.0
        x_scale: 10.0
        height_scale: 5.0
        width_scale: 5.0
      }
    }
    matcher {
      argmax_matcher {
        matched_threshold: 0.5
        unmatched_threshold: 0.5
        ignore_thresholds: false
        negatives_lower_than_unmatched: true
        force_match_for_each_row: true
        use_matmul_gather: true
      }
    }
    similarity_calculator {
      iou_similarity {
      }
    }
    box_predictor {
      convolutional_box_predictor {
        conv_hyperparams {
          regularizer {
            l2_regularizer {
              weight: 3.99999989895e-05
            }
          }
          initializer {
            random_normal_initializer {
              mean: 0.0
              stddev: 0.00999999977648
            }
          }
          activation: RELU_6
          batch_norm {
            decay: 0.97000002861
            center: true
            scale: true
            epsilon: 0.0010000000475
          }
        }
        min_depth: 0
        max_depth: 0
        num_layers_before_predictor: 0
        use_dropout: false
        dropout_keep_probability: 0.800000011921
        kernel_size: 1
        box_code_size: 4
        apply_sigmoid_to_scores: false
        class_prediction_bias_init: -4.59999990463
      }
    }
    anchor_generator {
      ssd_anchor_generator {
        num_layers: 6
        min_scale: 0.20000000298
        max_scale: 0.949999988079
        aspect_ratios: 1.0
        aspect_ratios: 2.0
        aspect_ratios: 0.5
        aspect_ratios: 3.0
        aspect_ratios: 0.333299994469
      }
    }
    post_processing {
      batch_non_max_suppression {
        score_threshold: 0.300000011921
        iou_threshold: 0.600000023842
        max_detections_per_class: 100
        max_total_detections: 100
      }
      score_converter: SIGMOID
    }
    normalize_loss_by_num_matches: true
    loss {
      localization_loss {
        weighted_smooth_l1 {
        }
      }
      classification_loss {
        weighted_sigmoid_focal {
          gamma: 2.0
          alpha: 0.75
        }
      }
      classification_weight: 1.0
      localization_weight: 1.0
    }
    encode_background_as_zeros: true
    normalize_loc_loss_by_codesize: true
    inplace_batchnorm_update: true
    freeze_batchnorm: false
  }
}
train_config {
  batch_size: 64
  data_augmentation_options {
    random_horizontal_flip {
    }
  }
  data_augmentation_options {
    random_vertical_flip {
    }
  }
  data_augmentation_options {
    ssd_random_crop {
    }
  }
  data_augmentation_options {
    random_rotation90  {
    }
  }
  data_augmentation_options {
    random_adjust_contrast  {
    }
  }
  #data_augmentation_options {
  #  rgb_to_gray  {
  #  }
  #}
  data_augmentation_options {
    random_image_scale  {
    }
  }
  data_augmentation_options {
    random_adjust_brightness  {
    }
  }
  data_augmentation_options {
    random_adjust_hue  {
    }
  }
  data_augmentation_options {
    random_adjust_saturation  {
    }
  }
  sync_replicas: true
  optimizer {
    momentum_optimizer {
      learning_rate {
        cosine_decay_learning_rate {
          learning_rate_base: 0.40000000298
          total_steps: 60000
          warmup_learning_rate: 0.0599999986589
          warmup_steps: 500
        }
      }
      momentum_optimizer_value: 0.899999976158
    }
    use_moving_average: false
  }
  fine_tune_checkpoint: "/tensorflow/models/research/learn_pet/ckpt/model.ckpt"
  from_detection_checkpoint: true
  load_all_detection_checkpoint_vars: true
  num_steps: 50000
  startup_delay_steps: 0.0
  replicas_to_aggregate: 8
  max_number_of_boxes: 100
  unpad_groundtruth_tensors: false
}
train_input_reader {
  label_map_path: "/tensorflow/models/research/learn_pet/muster_cobot/muster_cobot_label_map.pbtxt"
  tf_record_input_reader {
    input_path: "/tensorflow/models/research/learn_pet/muster_cobot/train.tfrecords"
  }
}
eval_config {
  num_examples: 8000
  metrics_set: "coco_detection_metrics"
  use_moving_averages: false
  num_visualizations: 15
}
eval_input_reader {
  label_map_path: "/tensorflow/models/research/learn_pet/muster_cobot/muster_cobot_label_map.pbtxt"
  shuffle: false
  num_readers: 1
  tf_record_input_reader {
    input_path: "/tensorflow/models/research/learn_pet/muster_cobot/eval.tfrecords"
  }
}
graph_rewriter {
  quantization {
    delay: 48000
    weight_bits: 8
    activation_bits: 8
  }
}
Namburger commented 3 years ago

Actually, that pipeline looks good, on the step where you run the export_tflite_ssd_graph.py make sure you also add this flag:

--max_detections=100

since for what ever odd reason, tensorflow decided that it'd default to 10: https://github.com/tensorflow/models/blob/master/research/object_detection/export_tflite_ssd_graph.py#L107

Namburger commented 3 years ago

I guess for you, since you are following the tutorial, that means you need to modify this scipt: https://github.com/google-coral/tutorials/blob/master/docker/object_detection/scripts/convert_checkpoint_to_edgetpu_tflite.sh#L55

hannes56a commented 3 years ago

Thanks a lot! Looks like this is the reason for sure. I cant test it now but I will report, when i have test it! Again: Thanks a lot!

hannes56a commented 3 years ago

It works! Thanks a lot again!