tensoflow-trained ssd model not working after converting to tensorflow-lite for raspi

梦想的初衷 提交于 2021-01-29 09:22:43

问题


System information Laptop: Linux Ubuntu Tensorflow 1.15.0

Raspi: Raspberry Pi 4 tflite-runtime 2.5.0 tensorflow-estimator 1.14.0 Coral Edge TPU

Hello everybody, I am stuck at getting my trained model running on raspi. I trained ssd_mobilenet_v2_coco model from tensorflow 1 modelzoo with my own custom dataset on google cloud with this config file where I did few changes:

model {
  ssd {
    num_classes: 3
    image_resizer {
      fixed_shape_resizer {
        height: 720
        width: 1280
      }
    }
    feature_extractor {
      type: "ssd_mobilenet_v2"
      depth_multiplier: 1.0
      min_depth: 16
      conv_hyperparams {
        regularizer {
          l2_regularizer {
            weight: 3.99999989895e-05
          }
        }
        initializer {
          truncated_normal_initializer {
            mean: 0.0
            stddev: 0.0299999993294
          }
        }
        activation: RELU_6
        batch_norm {
          decay: 0.999700009823
          center: true
          scale: true
          epsilon: 0.0010000000475
          train: true
        }
      }
      #batch_norm_trainable: true
      use_depthwise: 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
      }
    }
    similarity_calculator {
      iou_similarity {
      }
    }
    box_predictor {
      convolutional_box_predictor {
        conv_hyperparams {
          regularizer {
            l2_regularizer {
              weight: 3.99999989895e-05
            }
          }
          initializer {
            truncated_normal_initializer {
              mean: 0.0
              stddev: 0.0299999993294
            }
          }
          activation: RELU_6
          batch_norm {
            decay: 0.999700009823
            center: true
            scale: true
            epsilon: 0.0010000000475
            train: true
          }
        }
        min_depth: 0
        max_depth: 0
        num_layers_before_predictor: 0
        use_dropout: false
        dropout_keep_probability: 0.800000011921
        kernel_size: 3
        box_code_size: 4
        apply_sigmoid_to_scores: false
      }
    }
    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 {
          alpha: 0.25
          gamma: 2.0
        }
      }
      classification_weight: 1.0
      localization_weight: 1.0
    }
  }
}
train_config {
  batch_size: 4
  data_augmentation_options {
    random_horizontal_flip {
    }
  }
  data_augmentation_options {
    ssd_random_crop {
    }
  }
  optimizer {
    rms_prop_optimizer {
      learning_rate {
        exponential_decay_learning_rate {
          initial_learning_rate: 0.00400000018999
          decay_steps: 800720
          decay_factor: 0.949999988079
        }
      }
      momentum_optimizer_value: 0.899999976158
      decay: 0.899999976158
      epsilon: 1.0
    }
  }
  fine_tune_checkpoint: "gs://objects1119/model.ckpt"
  num_steps: 200000
  fine_tune_checkpoint_type: "detection"
}
train_input_reader {
  label_map_path: "gs://objects1119/labelmap.pbtxt"
  tf_record_input_reader {
    input_path: "gs://objects1119/train.record"
  }
  shuffle: true
}
eval_config {
  num_examples: 150
  max_evals: 10
  use_moving_averages: false
}
eval_input_reader {
  label_map_path: "gs://objects1119/labelmap.pbtxt"
  shuffle: true
  num_readers: 1
  tf_record_input_reader {
    input_path: "gs://objects1119/test.record"
  }
}

After training I exported the frozen_inference_graph which worked pretty well on my laptop and prediction was good, so I exported the tflite graph with the following command:

python export_tflite_ssd_graph.py \ --pipeline_config_path=gs://objects1119/ssd_mobilenet_v2_coco.config \ --trained_checkpoint_prefix=gs://objects1119/model.ckpt-189879 \ --output_directory=tflite \ --add_postprocessing_op=true

after that I converted it to detect.tflite file with:

tflite_convert --graph_def_file=tflite/tflite_graph.pb --output_file=tflite/detect.tflite --output_format=TFLITE --input_arrays=normalized_input_image_tensor --output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3' --inference_type=QUANTIZED_UINT8 --mean_values=128 --std_dev_values=127 --change_concat_input_ranges=false --allow_custom_ops

After that I converted it to edgetpu-file with this google colab: https://colab.research.google.com/drive/1o6cNNNgGhoT7_DR4jhpMKpq3mZZ6Of4N?usp=sharing while using the EdjeElectronics Tutorial: https://github.com/EdjeElectronics/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi/blob/master/Raspberry_Pi_Guide.md everything worked without errors.

But when I finally try to run it on the raspi I am always facing the same output, no matter what input. There is always Class Zero predicted on the same areas of the image with prediction of 50%. What am I doing wrong? Is it because of the changed input shapes in the config file for training? In my case it would be really complicated to train again so I try to avoid this if its possible. Here is also the link to the Isuue in github https://github.com/tensorflow/tensorflow/issues/45148

Thank you very much in advance.

来源:https://stackoverflow.com/questions/65108043/tensoflow-trained-ssd-model-not-working-after-converting-to-tensorflow-lite-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!