CenterNet急切的少量射击对象检测合作实验室

问题描述

我正在使用Tensorflow对象检测API。最近,它已更新为Tensorflow2。 随之,作者提出了一个很棒的Colab https://github.com/tensorflow/models/blob/master/research/object_detection/colab_tutorials/eager_few_shot_od_training_tf2_colab.ipynb。他们在新数据集上微调了RetinaNet,但是我不知道如何使用它来微调CenterNet(和EfficientDet)。

他们具有用于初始化RetinaNet模型的以下代码

tf.keras.backend.clear_session()

print('Building model and restoring weights for fine-tuning...',flush=True)
num_classes = 1
pipeline_config = 'models/research/object_detection/configs/tf2/ssd_resnet50_v1_fpn_640x640_coco17_tpu-8.config'
checkpoint_path = 'models/research/object_detection/test_data/checkpoint/ckpt-0'

# Load pipeline config and build a detection model.
#
# Since we are working off of a COCO architecture which predicts 90
# class slots by default,we override the `num_classes` field here to be just
# one (for our new rubber ducky class).
configs = config_util.get_configs_from_pipeline_file(pipeline_config)
model_config = configs['model']
model_config.ssd.num_classes = num_classes
model_config.ssd.freeze_batchnorm = True
detection_model = model_builder.build(
      model_config=model_config,is_training=True)

# Set up object-based checkpoint restore --- RetinaNet has two prediction
# `heads` --- one for classification,the other for Box regression.  We will
# restore the Box regression head but initialize the classification head
# from scratch (we show the omission below by commenting out the line that
# we would add if we wanted to restore both heads)
fake_Box_predictor = tf.compat.v2.train.Checkpoint(
    _base_tower_layers_for_heads=detection_model._Box_predictor._base_tower_layers_for_heads,# _prediction_heads=detection_model._Box_predictor._prediction_heads,#    (i.e.,the classification head that we *will not* restore)
    _Box_prediction_head=detection_model._Box_predictor._Box_prediction_head,)
fake_model = tf.compat.v2.train.Checkpoint(
          _feature_extractor=detection_model._feature_extractor,_Box_predictor=fake_Box_predictor)
ckpt = tf.compat.v2.train.Checkpoint(model=fake_model)
ckpt.restore(checkpoint_path).expect_partial()

# Run model through a dummy image so that variables are created
image,shapes = detection_model.preprocess(tf.zeros([1,640,3]))
prediction_dict = detection_model.predict(image,shapes)
_ = detection_model.postprocess(prediction_dict,shapes)
print('Weights restored!')

我尝试对CenterNet模型做类似的事情(在本Colab教程https://github.com/tensorflow/models/blob/master/research/object_detection/colab_tutorials/inference_tf2_colab.ipynb中用于推理):

pipeline_config =  'models/research/object_detection/configs/tf2/centernet_hourglass104_512x512_coco17_tpu-8.config'
model_dir = 'models/research/object_detection/test_data/checkpoint/'
num_classes = 1
# Load pipeline config and build a detection model
configs = config_util.get_configs_from_pipeline_file(pipeline_config)
model_config = configs['model']

model_config.center_net.num_classes = num_classes
detection_model = model_builder.build(
      model_config=model_config,is_training=True)

# Restore checkpoint
ckpt = tf.compat.v2.train.Checkpoint(
      model=detection_model)
ckpt.restore(os.path.join(model_dir,'ckpt-0')).expect_partial()

但是,由于形状不兼容而引发了异常(因为我更改了类数)。在RetinaNet的示例中,据我所知,此技巧用于制作正确形状的张量:

fake_Box_predictor = tf.compat.v2.train.Checkpoint(
    _base_tower_layers_for_heads=detection_model._Box_predictor._base_tower_layers_for_heads,_Box_predictor=fake_Box_predictor)

但是如何发现应该在checkpoint函数内部编写什么? (例如,_base_tower_layers_for_heads=detection_model._Box_predictor._base_tower_layers_for_heads_Box_prediction_head=detection_model._Box_predictor._Box_prediction_head

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)