MCPcopy Create free account
hub / github.com/DragonisCV/RAM / forward

Method forward

ram/utils/dino_feature_extractor.py:96–139  ·  view source on GitHub ↗
(self, inp_img)

Source from the content-addressed store, hash-verified

94 return x
95
96 def forward(self, inp_img):
97
98 device = inp_img.device
99
100 mean = torch.tensor([0.485, 0.456, 0.406], device=device).view(1, 3, 1, 1)
101 std = torch.tensor([0.229, 0.224, 0.225], device=device).view(1, 3, 1, 1)
102
103
104 denormalized_img = inp_img * std + mean
105 denormalized_img = self.check_image_size(denormalized_img)
106 h_denormalized, w_denormalized = denormalized_img.shape[2], denormalized_img.shape[3]
107 # To ensure minimal changes and maintain code generality, the image size is directly scaled here to guarantee spatial alignment.
108
109 target_h = (h_denormalized // 8) * 14
110 target_w = (w_denormalized // 8) * 14
111
112 shortest_edge = min(target_h, target_w)
113 processor = AutoImageProcessor.from_pretrained(
114 r'pretrained_model/facebookdinov2_giant',
115 local_files_only=False,
116 do_rescale=False,
117 do_center_crop=False,
118 use_fast=True,
119 size={"shortest_edge": shortest_edge}
120 )
121
122 inputs = processor(
123 images=denormalized_img,
124 return_tensors="pt"
125 ).to(device)
126
127
128 shallow_feat1, mid_feat1, deep_feat1, shallow_feat2, mid_feat2, deep_feat2 = self.get_dino_features(inputs['pixel_values'])
129
130 dino_features = {
131 'shallow_feat1': shallow_feat1,
132 'mid_feat1': mid_feat1,
133 'deep_feat1': deep_feat1,
134 'shallow_feat2': shallow_feat2,
135 'mid_feat2': mid_feat2,
136 'deep_feat2': deep_feat2
137 }
138
139 return dino_features

Callers

nothing calls this directly

Calls 2

check_image_sizeMethod · 0.95
get_dino_featuresMethod · 0.95

Tested by

no test coverage detected