Search code examples
pytorchobject-detectionfeature-extractionhuggingface-transformerspytorch-lightning

Is there any way to change parameters in pretrained DetrFeatureExtractor?


For my model, I load pretrained version of DetrFeatureExtractor:

feature_extractor = DetrFeatureExtractor(return_tensors="pt"
                                        ,do_normalize = True
                                        ,size = 400).from_pretrained("facebook/detr-resnet-50")

But when I output parameters of this variable, I get:

DetrFeatureExtractor {
  "do_normalize": true,
  "do_resize": true,
  "feature_extractor_type": "DetrFeatureExtractor",
  "format": "coco_detection",
  "image_mean": [
    0.485,
    0.456,
    0.406
  ],
  "image_std": [
    0.229,
    0.224,
    0.225
  ],
  "max_size": 1333,
  "size": 800
}

which still has size = 800. Is that possible to change parameters of pretrained feature extractor and, if yes, how can I change them?


Solution

  • Your values to the constructor are not taken, because you are calling .from_pretrained(), which loads all values from the respective config file of the pretrained model (in your case, the corresponding config file can be viewed here). Even if some values might not be specified in the config, they will be primarily taken from the default values, instead of whatever you're passing before.

    If you want to change attributes, you can do so after loading:

    from transformers import DetrFeatureExtractor
    
    model = DetrFeatureExtractor.from_pretrained("facebook/detr-resnet-50")
    model.size = 400
    print(model)  # will show the correct size
    

    I do want to point out that changing parameters of pretrained networks might lead to unexpected behavior - after all, the model wasn't originally trained with this particular setting. So just be aware that this might lead to detrimental performance, and experiment at your own risk ;-)