gberton commited on
Commit
eaca790
·
1 Parent(s): 7df8807

Use relative imports for sibling modules (fixes local loading, save_pretrained, pickling)

Browse files
Files changed (1) hide show
  1. modeling_dpt.py +4 -26
modeling_dpt.py CHANGED
@@ -1,32 +1,13 @@
1
  """TIPSv2 DPT dense prediction model for HuggingFace."""
2
 
3
- import importlib
4
- import os
5
  from dataclasses import dataclass
6
- from pathlib import Path
7
  from typing import Optional
8
 
9
  import torch
10
- from huggingface_hub import hf_hub_download
11
  from transformers import AutoConfig, AutoModel, PreTrainedModel
12
 
13
  from .configuration_dpt import TIPSv2DPTConfig
14
-
15
- _this_dir = Path(__file__).parent
16
- _sibling_cache = {}
17
-
18
-
19
- def _load_sibling(name, repo_id=None):
20
- if name in _sibling_cache:
21
- return _sibling_cache[name]
22
- path = _this_dir / f"{name}.py"
23
- if not path.exists() and repo_id:
24
- path = Path(hf_hub_download(repo_id, f"{name}.py"))
25
- spec = importlib.util.spec_from_file_location(name, str(path))
26
- mod = importlib.util.module_from_spec(spec)
27
- spec.loader.exec_module(mod)
28
- _sibling_cache[name] = mod
29
- return mod
30
 
31
 
32
  @dataclass
@@ -69,26 +50,23 @@ class TIPSv2DPTModel(PreTrainedModel):
69
  def __init__(self, config: TIPSv2DPTConfig):
70
  super().__init__(config)
71
 
72
- repo_id = getattr(config, "_name_or_path", None)
73
- dpt_mod = _load_sibling("dpt_head", repo_id)
74
-
75
  ppc = tuple(config.post_process_channels)
76
 
77
  backbone_config = AutoConfig.from_pretrained(config.backbone_repo, trust_remote_code=True)
78
  backbone = AutoModel.from_config(backbone_config, trust_remote_code=True)
79
  self.vision_encoder = backbone.vision_encoder
80
 
81
- self.depth_head = dpt_mod.DPTDepthHead(
82
  input_embed_dim=config.embed_dim, channels=config.channels,
83
  post_process_channels=ppc, readout_type=config.readout_type,
84
  num_depth_bins=config.num_depth_bins,
85
  min_depth=config.min_depth, max_depth=config.max_depth,
86
  )
87
- self.normals_head = dpt_mod.DPTNormalsHead(
88
  input_embed_dim=config.embed_dim, channels=config.channels,
89
  post_process_channels=ppc, readout_type=config.readout_type,
90
  )
91
- self.segmentation_head = dpt_mod.DPTSegmentationHead(
92
  input_embed_dim=config.embed_dim, channels=config.channels,
93
  post_process_channels=ppc, readout_type=config.readout_type,
94
  num_classes=config.num_seg_classes,
 
1
  """TIPSv2 DPT dense prediction model for HuggingFace."""
2
 
 
 
3
  from dataclasses import dataclass
 
4
  from typing import Optional
5
 
6
  import torch
 
7
  from transformers import AutoConfig, AutoModel, PreTrainedModel
8
 
9
  from .configuration_dpt import TIPSv2DPTConfig
10
+ from .dpt_head import DPTDepthHead, DPTNormalsHead, DPTSegmentationHead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
 
13
  @dataclass
 
50
  def __init__(self, config: TIPSv2DPTConfig):
51
  super().__init__(config)
52
 
 
 
 
53
  ppc = tuple(config.post_process_channels)
54
 
55
  backbone_config = AutoConfig.from_pretrained(config.backbone_repo, trust_remote_code=True)
56
  backbone = AutoModel.from_config(backbone_config, trust_remote_code=True)
57
  self.vision_encoder = backbone.vision_encoder
58
 
59
+ self.depth_head = DPTDepthHead(
60
  input_embed_dim=config.embed_dim, channels=config.channels,
61
  post_process_channels=ppc, readout_type=config.readout_type,
62
  num_depth_bins=config.num_depth_bins,
63
  min_depth=config.min_depth, max_depth=config.max_depth,
64
  )
65
+ self.normals_head = DPTNormalsHead(
66
  input_embed_dim=config.embed_dim, channels=config.channels,
67
  post_process_channels=ppc, readout_type=config.readout_type,
68
  )
69
+ self.segmentation_head = DPTSegmentationHead(
70
  input_embed_dim=config.embed_dim, channels=config.channels,
71
  post_process_channels=ppc, readout_type=config.readout_type,
72
  num_classes=config.num_seg_classes,