Thastp commited on
Commit
eb3b2c9
·
verified ·
1 Parent(s): 6ae798d

Upload model

Browse files
Files changed (3) hide show
  1. config.json +1 -0
  2. configuration_rf_detr.py +3 -3
  3. modeling_rf_detr.py +6 -12
config.json CHANGED
@@ -11,6 +11,7 @@
11
  "ca_nheads": 16,
12
  "dec_layers": 3,
13
  "dec_n_points": 2,
 
14
  "encoder": "dinov2_windowed_small",
15
  "gradient_checkpointing": false,
16
  "group_detr": 13,
 
11
  "ca_nheads": 16,
12
  "dec_layers": 3,
13
  "dec_n_points": 2,
14
+ "device": "cpu",
15
  "encoder": "dinov2_windowed_small",
16
  "gradient_checkpointing": false,
17
  "group_detr": 13,
configuration_rf_detr.py CHANGED
@@ -6,7 +6,7 @@ from optimum.exporters.onnx.model_configs import ViTOnnxConfig
6
 
7
  ### modified from https://github.com/roboflow/rf-detr/blob/main/rfdetr/config.py
8
 
9
- #DEVICE = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
10
 
11
  class RFDetrConfig(PretrainedConfig):
12
  model_type = 'rf-detr'
@@ -24,7 +24,7 @@ class RFDetrConfig(PretrainedConfig):
24
  amp: bool = True,
25
  num_classes: int = 90,
26
  num_queries: int = 300,
27
- # device: Literal["cpu", "cuda", "mps"] = DEVICE,
28
  resolution: int = 560,
29
  group_detr: int = 13,
30
  gradient_checkpointing: bool = False,
@@ -40,7 +40,7 @@ class RFDetrConfig(PretrainedConfig):
40
  self.layer_norm = layer_norm
41
  self.amp = amp
42
  self.num_classes = num_classes
43
- # self.device = device
44
  self.resolution = resolution
45
  self.group_detr = group_detr
46
  self.gradient_checkpointing = gradient_checkpointing
 
6
 
7
  ### modified from https://github.com/roboflow/rf-detr/blob/main/rfdetr/config.py
8
 
9
+ DEVICE = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
10
 
11
  class RFDetrConfig(PretrainedConfig):
12
  model_type = 'rf-detr'
 
24
  amp: bool = True,
25
  num_classes: int = 90,
26
  num_queries: int = 300,
27
+ device: Literal["cpu", "cuda", "mps"] = DEVICE,
28
  resolution: int = 560,
29
  group_detr: int = 13,
30
  gradient_checkpointing: bool = False,
 
40
  self.layer_norm = layer_norm
41
  self.amp = amp
42
  self.num_classes = num_classes
43
+ self.device = device
44
  self.resolution = resolution
45
  self.group_detr = group_detr
46
  self.gradient_checkpointing = gradient_checkpointing
modeling_rf_detr.py CHANGED
@@ -40,7 +40,7 @@ class RFDetrModelForObjectDetection(PreTrainedModel):
40
  layer_norm = config.layer_norm,
41
  amp = config.amp,
42
  num_classes = config.num_classes,
43
- #device = config.device,
44
  resolution = config.resolution,
45
  group_detr = config.group_detr,
46
  gradient_checkpointing = config.gradient_checkpointing,
@@ -106,19 +106,15 @@ class RFDetrModelForObjectDetection(PreTrainedModel):
106
  wr = self.config.resolution / float(w)
107
 
108
  for label in labels:
109
- boxes = label["boxes"]#.to(device=self.config.device, dtype=torch.float32)
110
  # resize boxes to model's resolution
111
  boxes[:, [0, 2]] *= wr
112
  boxes[:, [1, 3]] *= hr
113
- # boxes[:, 0] *= wr
114
- # boxes[:, 1] *= hr
115
- # boxes[:, 2] *= wr
116
- # boxes[:, 3] *= hr
117
  # normalize to [0, 1] by model's resolution
118
  boxes[:] /= self.config.resolution
119
  label["boxes"] = boxes
120
- # if "labels" in label:
121
- # label["labels"] = label["labels"].to(self.config.device)
122
 
123
  def forward(self, pixel_values: torch.Tensor, pixel_mask: torch.Tensor, labels=None, **kwargs) -> ModelOutput:
124
  resize = Resize((self.config.resolution, self.config.resolution))
@@ -135,16 +131,14 @@ class RFDetrModelForObjectDetection(PreTrainedModel):
135
  self.criterion.training = False
136
 
137
  # resize pixel values and mask to model's resolution
138
- # pixel_values = pixel_values.to(self.config.device)
139
- # pixel_mask = pixel_mask.to(self.config.device)
140
  pixel_values = resize(pixel_values)
141
  pixel_mask = resize(pixel_mask)
142
 
143
  samples = NestedTensor(pixel_values, pixel_mask)
144
  outputs = self.model(samples)
145
 
146
- #TODO: check format of pred_boxes
147
-
148
  # compute loss, return none and empty dict if not training
149
  loss, loss_dict = self.compute_loss(labels, outputs)
150
 
 
40
  layer_norm = config.layer_norm,
41
  amp = config.amp,
42
  num_classes = config.num_classes,
43
+ device = config.device,
44
  resolution = config.resolution,
45
  group_detr = config.group_detr,
46
  gradient_checkpointing = config.gradient_checkpointing,
 
106
  wr = self.config.resolution / float(w)
107
 
108
  for label in labels:
109
+ boxes = label["boxes"].to(device=self.config.device, dtype=torch.float32)
110
  # resize boxes to model's resolution
111
  boxes[:, [0, 2]] *= wr
112
  boxes[:, [1, 3]] *= hr
 
 
 
 
113
  # normalize to [0, 1] by model's resolution
114
  boxes[:] /= self.config.resolution
115
  label["boxes"] = boxes
116
+ if "labels" in label:
117
+ label["labels"] = label["labels"].to(self.config.device)
118
 
119
  def forward(self, pixel_values: torch.Tensor, pixel_mask: torch.Tensor, labels=None, **kwargs) -> ModelOutput:
120
  resize = Resize((self.config.resolution, self.config.resolution))
 
131
  self.criterion.training = False
132
 
133
  # resize pixel values and mask to model's resolution
134
+ pixel_values = pixel_values.to(self.config.device)
135
+ pixel_mask = pixel_mask.to(self.config.device)
136
  pixel_values = resize(pixel_values)
137
  pixel_mask = resize(pixel_mask)
138
 
139
  samples = NestedTensor(pixel_values, pixel_mask)
140
  outputs = self.model(samples)
141
 
 
 
142
  # compute loss, return none and empty dict if not training
143
  loss, loss_dict = self.compute_loss(labels, outputs)
144