使用TensorFlow对象检测API进行转移学习

时间:2019-07-18 08:58:15

标签: tensorflow object-detection

我正在尝试使用预训练的faster_rcnn_inception_v2_coco训练模型。我正在使用以下配置文件:

model {
  faster_rcnn {
    num_classes: 37
    image_resizer {
      keep_aspect_ratio_resizer {
        min_dimension: 1080
        max_dimension: 1365
      }
    }
    feature_extractor {
      type: "faster_rcnn_inception_v2"
      first_stage_features_stride: 8
    }
    first_stage_anchor_generator {
      grid_anchor_generator {
        height_stride: 16
        width_stride: 16
        scales: 0.25
        scales: 0.5
        scales: 1.0
        scales: 2.0
        aspect_ratios: 0.5
        aspect_ratios: 1.0
        aspect_ratios: 2.0
      }
    }
    first_stage_box_predictor_conv_hyperparams {
      op: CONV
      regularizer {
        l2_regularizer {
          weight: 0.0001
        }
      }
      initializer {
        truncated_normal_initializer {
          stddev: 0.00999999977648
        }
      }
    }
    first_stage_nms_score_threshold: 0.0
    first_stage_nms_iou_threshold: 0.699999988079
    first_stage_max_proposals: 31
    second_stage_batch_size: 30
    first_stage_localization_loss_weight: 2.0
    first_stage_objectness_loss_weight: 1.0
    initial_crop_size: 14
    maxpool_kernel_size: 2
    maxpool_stride: 2
    second_stage_box_predictor {
      mask_rcnn_box_predictor {
        fc_hyperparams {
          op: FC
          regularizer {
            l2_regularizer {
              weight: 0.0001
            }
          }
          initializer {
            variance_scaling_initializer {
              factor: 1.0
              uniform: true
              mode: FAN_AVG
            }
          }
        }
        use_dropout: true
        dropout_keep_probability: 0.20
      }
    }
    second_stage_post_processing {
      batch_non_max_suppression {
        score_threshold: 0.0
        iou_threshold: 0.600000023842
        max_detections_per_class: 100
        max_total_detections: 100
      }
      score_converter: SOFTMAX
    }
    second_stage_classification_loss{
      weighted_sigmoid_focal{
        gamma:2
        alpha:0.5
      }
    }
    second_stage_localization_loss_weight: 2.0
    second_stage_classification_loss_weight: 1.0
  }
}
train_config {
  batch_size: 1
  data_augmentation_options {
    random_jitter_boxes {
    }
  }
  optimizer {
    adam_optimizer {
      learning_rate {
        manual_step_learning_rate {
          initial_learning_rate: 4.99999987369e-05
          schedule {
            step: 160000
            learning_rate: 1e-05
          }
          schedule {
            step: 175000
            learning_rate: 1e-06
          }
        }
      }
    }
    use_moving_average: true
  }
  gradient_clipping_by_norm: 10.0
  fine_tune_checkpoint: "/home/deploy/tensorflow/models/research/object_detection/ved/model.ckpt"
  from_detection_checkpoint: true
  num_steps: 400000
}
train_input_reader {
  label_map_path: "/home/deploy/tensorflow/models/research/object_detection/ved/tomato.pbtxt"
  tf_record_input_reader {
    input_path: "/home/deploy/tensorflow/models/research/object_detection/ved/train.record"
  }
}
eval_config {
  num_visualizations: 4
  max_evals: 5
  num_examples: 4
  max_num_boxes_to_visualize : 100
  metrics_set: "coco_detection_metrics"
  eval_interval_secs: 600
}
eval_input_reader {
  label_map_path: "/home/deploy/tensorflow/models/research/object_detection/ved/tomato.pbtxt"
  shuffle: true
  num_epochs: 1
  num_readers: 1
  tf_record_input_reader {
    input_path: "/home/deploy/tensorflow/models/research/object_detection/ved/val.record"
  }
  sample_1_of_n_examples: 2
}

但是我遇到以下错误:

InvalidArgumentError (see above for traceback): Restoring from checkpoint failed. This is most likely due to a mismatch between the current graph and the graph from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Assign requires shapes of both tensors to match. lhs shape= [148] rhs shape= [40]
         [[node save/Assign_728 (defined at /home/deploy/tensorflow/models/research/object_detection/model_lib.py:490)  = Assign[T=DT_FLOAT, _class=["loc:@SecondStageBoxPredictor/BoxEncodingPredictor/biases"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](SecondStageBoxPredictor/BoxEncodingPredictor/biases, save/RestoreV2/_1457)]]
         [[{{node save/RestoreV2/_1768}} = _Send[T=DT_FLOAT, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_1773_save/RestoreV2", _device="/job:localhost/replica:0/task:0/device:CPU:0"](save/RestoreV2:884)]]

我不知道为什么会这样。我已经更改了num_classesfirst_stage_max_proposalssecond_stage_batch_size

1 个答案:

答案 0 :(得分:0)

尝试更正检查点文件路径。检查点文件应来自用于培训的相同模型。通常,它带有从TensorFlow Model Zoo下载的预先训练好的模型包。

尝试在此行修复:

  fine_tune_checkpoint: "/home/deploy/tensorflow/models/research/object_detection/ved/model.ckpt"