Retraining faster rcnn from object detection 2 api on a custom dataste
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 20/100
- Issue 类型
- 缺陷
- 描述清晰度
- 需要澄清
- 活跃度
- 停滞
- 技术栈
- python, tensorflow
调研方向
从所引用的 Faster R-CNN 流水线配置以及 model_builder 和 config_util 入口点开始。检查 checkpoint 的恢复方式以及 classification head 的替换方式,然后运行所示的虚拟图像初始化和训练设置,以识别恢复变量、head 维度或损失行为方面的不一致。
由索引模型根据 Issue 内容生成。
描述
Dear all,
I am trying to retrain the classification head of faster rcnn while keeping other branches, I defined this code:
import tensorflow as tf
from object_detection.builders import model_builder
from object_detection.utils import config_util
Clear the current TensorFlow session
tf.keras.backend.clear_session()
print('Building model and restoring weights for fine-tuning...', flush=True)
Settings
num_classes = 2
pipeline_config = '/MyPath/models/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8.config'
checkpoint_path = 'MyPath/models/research/object_detection/test_data2/checkpoint/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8/checkpoint/ckpt-0'
Load pipeline config and build a detection model
configs = config_util.get_configs_from_pipeline_file(pipeline_config)
model_config = configs['model']
Update the model config to reflect the number of classes
model_config.faster_rcnn.num_classes = num_classes
model_config.faster_rcnn.feature_extractor.batch_norm_trainable = False
Build the detection model
detection_model = model_builder.build(model_config=model_config, is_training=False)
Restore the model weights from the checkpoint, excluding the classification and mask heads
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt.restore(checkpoint_path).expect_partial()
Reinitialize the class prediction head to match the new number of classes
class_prediction_head = detection_model._mask_rcnn_box_predictor._class_prediction_head
Access the Dense layer responsible for class prediction
dense_layer = class_prediction_head._class_predictor_layers[1]
num_units = num_classes + 1 # Add 1 for the background class
Create a new Dense layer with the correct number of classes
new_dense_layer = tf.keras.layers.Dense(
units=num_units,
activation=dense_layer.activation,
use_bias=dense_layer.use_bias,
kernel_initializer=tf.keras.initializers.RandomNormal(stddev=0.01), # Initialize new layer weights randomly
bias_initializer=dense_layer.bias_initializer,
kernel_regularizer=dense_layer.kernel_regularizer,
bias_regularizer=dense_layer.bias_regularizer,
activity_regularizer=dense_layer.activity_regularizer,
kernel_constraint=dense_layer.kernel_constraint,
bias_constraint=dense_layer.bias_constraint,
name=dense_layer.name # Keep the same name
)
Replace the old Dense layer with the new one in the class prediction head
class_prediction_head._class_predictor_layers[1] = new_dense_layer
Run the model through a dummy image to ensure all variables are properly initialized
image, shapes = detection_model.preprocess(tf.zeros([1, 640, 640, 3]))
prediction_dict = detection_model.predict(image, shapes)
_ = detection_model.postprocess(prediction_dict, shapes)
print('Weights restored for box predictor! Classification head initialized with the new number of classes.')
When establishing the training, my loss is around the 1. I choose the following optimizer:
learning_rate = 0.000001
optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate, momentum=0.9)
Is the restoration of checkpoint correct? Can Any help to detect any inconsistencies in the definition of model.
Thank you
- 主要语言
- Python
- 星标
- 77.6k
- 派生
- 44.8k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
tensorflow/models 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 72/100
tensorflow/models#13653 ·
-
object_detection_tutorial.ipynb pretrained model "resnet50 imagenet"可能重新可做 @laxmareddyp 于 203 天前认领,目前没有进行中的 PR。 未关闭type:support
tensorflow/models#13634 · 2 条评论 · 1 个 reaction · 已指派 1 人 ·
-
Tag version compatible with Tensorflow 2.20可能重新可做 @bharatjetti 于 277 天前认领,目前没有进行中的 PR。 未关闭models:official type:bug
tensorflow/models#13607 · 1 条评论 · 已指派 1 人 ·
-
models:research type:feature
难度 5/5 一周以上 新手友好度 10/100
tensorflow/models#13600 ·
-
Installation Issues on Python 3.13可能重新可做 @bharatjetti 于 246 天前认领,目前没有进行中的 PR。 未关闭models:official type:bug
tensorflow/models#13587 · 9 条评论 · 已指派 1 人 ·
查看 tensorflow/models 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
bytedance/agentkit-samples#277 ·
-
ai-driven-fuzzing bug claude Minor
难度 2/5 1-3 小时 新手友好度 83/100
维护者通常 2 天内回复
-
channels:edit check:passed
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
aws-samples/sample-aws-genai-db-modernizer#2097 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复