Enable HF pretrained backbones (#31145)
* Enable load HF or tim backbone checkpoints * Fix up * Fix test - pass in proper out_indices * Update docs * Fix tvp tests * Fix doc examples * Fix doc examples * Try to resolve DPT backbone param init * Don't conditionally set to None * Add condition based on whether backbone is defined * Address review comments
This commit is contained in:
@@ -23,6 +23,7 @@ import numpy as np
|
||||
from tests.test_modeling_common import floats_tensor
|
||||
from transformers import OneFormerConfig, is_torch_available, is_vision_available
|
||||
from transformers.testing_utils import (
|
||||
require_timm,
|
||||
require_torch,
|
||||
require_torch_accelerator,
|
||||
require_torch_fp16,
|
||||
@@ -446,6 +447,37 @@ class OneFormerModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCas
|
||||
self.assertIsNotNone(transformer_decoder_mask_predictions.grad)
|
||||
self.assertIsNotNone(attentions.grad)
|
||||
|
||||
@require_timm
|
||||
def test_backbone_selection(self):
|
||||
config, inputs = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
config.backbone_config = None
|
||||
config.backbone_kwargs = {"out_indices": [1, 2, 3]}
|
||||
config.use_pretrained_backbone = True
|
||||
|
||||
# Load a timm backbone
|
||||
# We can't load transformer checkpoint with timm backbone, as we can't specify features_only and out_indices
|
||||
config.backbone = "resnet18"
|
||||
config.use_timm_backbone = True
|
||||
|
||||
for model_class in self.all_model_classes:
|
||||
model = model_class(config).to(torch_device).eval()
|
||||
if model.__class__.__name__ == "OneFormerModel":
|
||||
self.assertEqual(model.pixel_level_module.encoder.out_indices, [1, 2, 3])
|
||||
elif model.__class__.__name__ == "OneFormerForUniversalSegmentation":
|
||||
self.assertEqual(model.model.pixel_level_module.encoder.out_indices, [1, 2, 3])
|
||||
|
||||
# Load a HF backbone
|
||||
config.backbone = "microsoft/resnet-18"
|
||||
config.use_timm_backbone = False
|
||||
|
||||
for model_class in self.all_model_classes:
|
||||
model = model_class(config).to(torch_device).eval()
|
||||
if model.__class__.__name__ == "OneFormerModel":
|
||||
self.assertEqual(model.pixel_level_module.encoder.out_indices, [1, 2, 3])
|
||||
elif model.__class__.__name__ == "OneFormerForUniversalSegmentation":
|
||||
self.assertEqual(model.model.pixel_level_module.encoder.out_indices, [1, 2, 3])
|
||||
|
||||
|
||||
TOLERANCE = 1e-4
|
||||
|
||||
|
||||
Reference in New Issue
Block a user