File size: 7,616 Bytes
3133fdb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import itertools
import unittest
import numpy as np
import torch
from pytorchvideo.layers.convolutions import (
Conv2plus1d,
ConvReduce3D,
create_conv_2plus1d,
)
from torch import nn
class TestConvReduce3D(unittest.TestCase):
def setUp(self):
super().setUp()
torch.set_rng_state(torch.manual_seed(42).get_state())
def test_create_stack_conv(self):
"""
Test ConvReduce3D.
"""
for input_dim, output_dim in itertools.product((2, 4), (4, 8, 16)):
model = ConvReduce3D(
in_channels=input_dim,
out_channels=output_dim,
kernel_size=((1, 1, 1), (3, 3, 3), (1, 3, 3)),
stride=((1, 1, 1), (1, 1, 1), None),
padding=((0, 0, 0), (1, 1, 1), (0, 1, 1)),
dilation=((2, 2, 2), (1, 1, 1), None),
groups=(1, 2, None),
bias=(True, False, None),
)
model_gt_list = [
nn.Conv3d(
in_channels=input_dim,
out_channels=output_dim,
kernel_size=(1, 1, 1),
stride=(1, 1, 1),
padding=(0, 0, 0),
dilation=(2, 2, 2),
groups=1,
bias=True,
),
nn.Conv3d(
in_channels=input_dim,
out_channels=output_dim,
kernel_size=(3, 3, 3),
stride=(1, 1, 1),
padding=(1, 1, 1),
dilation=(1, 1, 1),
groups=2,
bias=False,
),
nn.Conv3d(
in_channels=input_dim,
out_channels=output_dim,
kernel_size=(1, 3, 3),
padding=(0, 1, 1),
),
]
model.convs[0].load_state_dict(
model_gt_list[0].state_dict(), strict=True
) # explicitly use strict mode.
model.convs[1].load_state_dict(
model_gt_list[1].state_dict(), strict=True
) # explicitly use strict mode.
model.convs[2].load_state_dict(
model_gt_list[2].state_dict(), strict=True
) # explicitly use strict mode.
# Test forwarding.
for tensor in TestConvReduce3D._get_inputs(input_dim):
if tensor.shape[1] != input_dim:
with self.assertRaises(RuntimeError):
output_tensor = model(tensor)
continue
else:
output_tensor = model(tensor)
output_gt = []
for ind in range(3):
output_gt.append(model_gt_list[ind](tensor))
output_tensor_gt = torch.stack(output_gt, dim=0).sum(
dim=0, keepdim=False
)
self.assertEqual(
output_tensor.shape,
output_tensor_gt.shape,
"Output shape {} is different from expected shape {}".format(
output_tensor.shape, output_tensor_gt.shape
),
)
@staticmethod
def _get_inputs(input_dim: int = 3) -> torch.tensor:
"""
Provide different tensors as test cases.
Yield:
(torch.tensor): tensor as test case input.
"""
# Prepare random tensor as test cases.
shapes = (
# Forward succeeded.
(1, input_dim, 3, 7, 7),
(1, input_dim, 5, 7, 7),
(1, input_dim, 7, 7, 7),
(2, input_dim, 3, 7, 7),
(4, input_dim, 3, 7, 7),
(8, input_dim, 3, 7, 7),
(2, input_dim, 3, 7, 14),
(2, input_dim, 3, 14, 7),
(2, input_dim, 3, 14, 14),
# Forward failed.
(8, input_dim * 2, 3, 7, 7),
(8, input_dim * 4, 5, 7, 7),
)
for shape in shapes:
yield torch.rand(shape)
class TestConv2plus1d(unittest.TestCase):
def setUp(self):
super().setUp()
torch.set_rng_state(torch.manual_seed(42).get_state())
def test_create_2plus1d_conv(self):
"""
Test Conv2plus1d.
"""
for input_dim, output_dim in itertools.product((2, 4), (4, 8, 16)):
model = Conv2plus1d(
conv_t=nn.Conv3d(
in_channels=input_dim,
out_channels=output_dim,
kernel_size=(3, 1, 1),
stride=(2, 1, 1),
padding=(1, 0, 0),
bias=False,
),
norm=nn.BatchNorm3d(output_dim),
activation=nn.ReLU(),
conv_xy=nn.Conv3d(
in_channels=output_dim,
out_channels=output_dim,
kernel_size=(1, 3, 3),
stride=(1, 2, 2),
padding=(0, 1, 1),
bias=False,
),
)
model_gt = create_conv_2plus1d(
in_channels=input_dim,
out_channels=output_dim,
kernel_size=(3, 3, 3),
stride=(2, 2, 2),
padding=(1, 1, 1),
bias=False,
norm=nn.BatchNorm3d,
norm_eps=1e-5,
norm_momentum=0.1,
activation=nn.ReLU,
)
model.load_state_dict(
model_gt.state_dict(), strict=True
) # explicitly use strict mode.
# Test forwarding.
for input_tensor in TestConv2plus1d._get_inputs():
with torch.no_grad():
if input_tensor.shape[1] != input_dim:
with self.assertRaises(RuntimeError):
output_tensor = model(input_tensor)
continue
else:
output_tensor = model(input_tensor)
output_tensor_gt = model_gt(input_tensor)
self.assertEqual(
output_tensor.shape,
output_tensor_gt.shape,
"Output shape {} is different from expected shape {}".format(
output_tensor.shape, output_tensor_gt.shape
),
)
self.assertTrue(
np.allclose(output_tensor.numpy(), output_tensor_gt.numpy())
)
@staticmethod
def _get_inputs(input_dim: int = 3) -> torch.tensor:
"""
Provide different tensors as test cases.
Yield:
(torch.tensor): tensor as test case input.
"""
# Prepare random tensor as test cases.
shapes = (
# Forward succeeded.
(1, input_dim, 3, 7, 7),
(1, input_dim, 5, 7, 7),
(1, input_dim, 7, 7, 7),
(2, input_dim, 3, 7, 7),
(4, input_dim, 3, 7, 7),
(8, input_dim, 3, 7, 7),
(2, input_dim, 3, 7, 14),
(2, input_dim, 3, 14, 7),
(2, input_dim, 3, 14, 14),
# Forward failed.
(8, input_dim * 2, 3, 7, 7),
(8, input_dim * 4, 5, 7, 7),
)
for shape in shapes:
yield torch.rand(shape)
|