Skip to content

Commit

Permalink
Fixing formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Kieran Fraser <Kieran.Fraser@ibm.com>
  • Loading branch information
kieranfraser committed Jun 14, 2023
1 parent e0fe372 commit cb49cdd
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 46 deletions.
48 changes: 23 additions & 25 deletions art/estimators/object_detection/pytorch_detection_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,10 +832,13 @@ def compute_loss( # type: ignore

return loss.detach().cpu().numpy()

def _apply_resizing(self, x: Union[np.ndarray, "torch.Tensor"],
y: List[Dict[str, Union[np.ndarray, "torch.Tensor"]]],
height: int = 800,
width: int = 800):
def _apply_resizing(
self,
x: Union[np.ndarray, "torch.Tensor"],
y: List[Dict[str, Union[np.ndarray, "torch.Tensor"]]],
height: int = 800,
width: int = 800,
):
"""
Resize the input and targets to dimensions expected by DETR.
Expand All @@ -856,45 +859,39 @@ def _apply_resizing(self, x: Union[np.ndarray, "torch.Tensor"],
):
resized_imgs = []
if isinstance(x, torch.Tensor):
x = T.Resize(size = (height, width))(x)
x = T.Resize(size=(height, width))(x)
else:
for i, _ in enumerate(x):
resized = cv2.resize(
(x)[i].transpose(1, 2, 0),
dsize=(height, width),
interpolation=cv2.INTER_CUBIC,
)
resized = resized.transpose(2, 0, 1)
resized_imgs.append(
resized
(x)[i].transpose(1, 2, 0),
dsize=(height, width),
interpolation=cv2.INTER_CUBIC,
)
resized = resized.transpose(2, 0, 1)
resized_imgs.append(resized)
x = np.array(resized_imgs)

elif self._input_shape[1] != self._input_shape[2]:
rescale_dim = max(self._input_shape[1], self._input_shape[2])
resized_imgs = []
if isinstance(x, torch.Tensor):
x = T.Resize(size = (rescale_dim,rescale_dim))(x)
x = T.Resize(size=(rescale_dim, rescale_dim))(x)
else:
for i, _ in enumerate(x):
resized = cv2.resize(
(x)[i].transpose(1, 2, 0),
dsize=(rescale_dim, rescale_dim),
interpolation=cv2.INTER_CUBIC,
)
resized = resized.transpose(2, 0, 1)
resized_imgs.append(
resized
(x)[i].transpose(1, 2, 0),
dsize=(rescale_dim, rescale_dim),
interpolation=cv2.INTER_CUBIC,
)
resized = resized.transpose(2, 0, 1)
resized_imgs.append(resized)
x = np.array(resized_imgs)

targets = []
if y is not None:
if isinstance(y[0]['boxes'], torch.Tensor):
if isinstance(y[0]["boxes"], torch.Tensor):
for target in y:
cxcy_norm = revert_rescale_bboxes(
target["boxes"], (self.input_shape[2], self.input_shape[1])
)
cxcy_norm = revert_rescale_bboxes(target["boxes"], (self.input_shape[2], self.input_shape[1]))
targets.append(
{
"labels": target["labels"].type(torch.int64).to(self.device),
Expand All @@ -917,6 +914,7 @@ def _apply_resizing(self, x: Union[np.ndarray, "torch.Tensor"],

return x, targets


class NestedTensor:
"""
From DETR source: https://github.com/facebookresearch/detr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,29 @@ def test_predict(get_pytorch_detr):
assert list(result[0].keys()) == ["boxes", "labels", "scores"]

assert result[0]["boxes"].shape == (100, 4)
expected_detection_boxes = np.asarray([-5.9490204e-03, 1.1947733e+01, 3.1993944e+01, 3.1925127e+01])
expected_detection_boxes = np.asarray([-5.9490204e-03, 1.1947733e01, 3.1993944e01, 3.1925127e01])
np.testing.assert_array_almost_equal(result[0]["boxes"][2, :], expected_detection_boxes, decimal=3)

assert result[0]["scores"].shape == (100,)
expected_detection_scores = np.asarray(
[0.00679839, 0.0250559 , 0.07205943, 0.01115368, 0.03321039,
0.10407761, 0.00113309, 0.01442852, 0.00527624, 0.01240906]
[
0.00679839,
0.0250559,
0.07205943,
0.01115368,
0.03321039,
0.10407761,
0.00113309,
0.01442852,
0.00527624,
0.01240906,
]
)
np.testing.assert_array_almost_equal(result[0]["scores"][:10], expected_detection_scores, decimal=6)
np.testing.assert_array_almost_equal(result[0]["scores"][:10], expected_detection_scores, decimal=5)

assert result[0]["labels"].shape == (100,)
expected_detection_classes = np.asarray([17, 17, 33, 17, 17, 17, 74, 17, 17, 17])
np.testing.assert_array_almost_equal(result[0]["labels"][:10], expected_detection_classes, decimal=6)
np.testing.assert_array_almost_equal(result[0]["labels"][:10], expected_detection_classes, decimal=5)


@pytest.mark.only_with_platform("pytorch")
Expand All @@ -98,26 +108,79 @@ def test_loss_gradient(get_pytorch_detr):
assert grads.shape == (2, 3, 800, 800)

expected_gradients1 = np.asarray(
[-0.00061366, 0.00322502, -0.00039866, -0.00807413, -0.00476555,
0.00181204, 0.01007765, 0.00415828, -0.00073114, 0.00018387,
-0.00146992, -0.00119636, -0.00098966, -0.00295517, -0.0024271 ,
-0.00131314, -0.00149217, -0.00104926, -0.00154239, -0.00110989,
0.00092887, 0.00049146, -0.00292508, -0.00124526, 0.00140347,
0.00019833, 0.00191074, -0.00117537, -0.00080604, 0.00057427,
-0.00061728, -0.00206535]
[
-0.00061366,
0.00322502,
-0.00039866,
-0.00807413,
-0.00476555,
0.00181204,
0.01007765,
0.00415828,
-0.00073114,
0.00018387,
-0.00146992,
-0.00119636,
-0.00098966,
-0.00295517,
-0.0024271,
-0.00131314,
-0.00149217,
-0.00104926,
-0.00154239,
-0.00110989,
0.00092887,
0.00049146,
-0.00292508,
-0.00124526,
0.00140347,
0.00019833,
0.00191074,
-0.00117537,
-0.00080604,
0.00057427,
-0.00061728,
-0.00206535,
]
)

np.testing.assert_array_almost_equal(grads[0, 0, 10, :32], expected_gradients1, decimal=2)

expected_gradients2 = np.asarray(
[-1.1787530e-03, -2.8500680e-03, 5.0884970e-03, 6.4504531e-04,
-6.8841036e-05, 2.8184296e-03, 3.0257765e-03, 2.8565727e-04,
-1.0701057e-04, 1.2945699e-03, 7.3593057e-04, 1.0177144e-03,
-2.4692707e-03, -1.3801848e-03, 6.3182280e-04, -4.2305476e-04,
4.4307750e-04, 8.5821096e-04, -7.1204413e-04, -3.1404425e-03,
-1.5964351e-03, -1.9222996e-03, -5.3157361e-04, -9.9202688e-04,
-1.5815455e-03, 2.0060266e-04, -2.0584739e-03, 6.6960667e-04,
9.7393827e-04, -1.6040013e-03, -6.9741381e-04, 1.4657658e-04]
[
-1.1787530e-03,
-2.8500680e-03,
5.0884970e-03,
6.4504531e-04,
-6.8841036e-05,
2.8184296e-03,
3.0257765e-03,
2.8565727e-04,
-1.0701057e-04,
1.2945699e-03,
7.3593057e-04,
1.0177144e-03,
-2.4692707e-03,
-1.3801848e-03,
6.3182280e-04,
-4.2305476e-04,
4.4307750e-04,
8.5821096e-04,
-7.1204413e-04,
-3.1404425e-03,
-1.5964351e-03,
-1.9222996e-03,
-5.3157361e-04,
-9.9202688e-04,
-1.5815455e-03,
2.0060266e-04,
-2.0584739e-03,
6.6960667e-04,
9.7393827e-04,
-1.6040013e-03,
-6.9741381e-04,
1.4657658e-04,
]
)
np.testing.assert_array_almost_equal(grads[1, 0, 10, :32], expected_gradients2, decimal=2)

Expand Down Expand Up @@ -235,7 +298,7 @@ def test_pgd(get_pytorch_detr):

imgs = []
for i in x_test:
img = Image.fromarray((i*255).astype(np.uint8).transpose(1,2,0))
img = Image.fromarray((i * 255).astype(np.uint8).transpose(1, 2, 0))
img = img.resize(size=(800, 800))
imgs.append(np.array(img))
x_test = np.array(imgs).transpose(0, 3, 1, 2)
Expand Down

0 comments on commit cb49cdd

Please sign in to comment.