forked from facebookresearch/pytorchvideo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_models_slowfast.py
144 lines (128 loc) · 4.91 KB
/
test_models_slowfast.py
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
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import itertools
import os
import unittest
from typing import Tuple
import torch
from pytorchvideo.models.slowfast import create_slowfast, create_slowfast_with_roi_head
from pytorchvideo.transforms.functional import uniform_temporal_subsample_repeated
from torch import nn
class TestSlowFast(unittest.TestCase):
def setUp(self):
super().setUp()
torch.set_rng_state(torch.manual_seed(42).get_state())
def test_load_hubconf(self):
path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
)
for model_name in ["slowfast_r50", "slowfast_r101"]:
model = torch.hub.load(
repo_or_dir=path, source="local", model=model_name, pretrained=False
)
self.assertIsNotNone(model)
input_clip_length = 32
input_crop_size = 224
input_channel = 3
# Test forwarding.
for tensor in TestSlowFast._get_inputs(
input_channel, input_clip_length, input_crop_size
):
with torch.no_grad():
if tensor[0].shape[1] != input_channel:
with self.assertRaises(RuntimeError):
model(tensor)
continue
def test_load_hubconf_detection(self):
path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
)
input_clip_length = 32
input_crop_size = 224
input_channel = 3
model = torch.hub.load(
repo_or_dir=path,
source="local",
model="slowfast_r50_detection",
pretrained=False,
)
self.assertIsNotNone(model)
# Test forwarding.
bbox_test_imputs = torch.tensor([[0.0, 10, 15, 20, 25], [0.0, 11, 16, 21, 26]])
for tensor in TestSlowFast._get_inputs(
input_channel, input_clip_length, input_crop_size
):
with torch.no_grad():
if tensor[0].shape[1] != input_channel:
with self.assertRaises(RuntimeError):
model(tensor, bbox_test_imputs)
continue
model(tensor, bbox_test_imputs)
def test_create_slowfast_with_roi_head_with_callable(self):
input_clip_length = 32
input_crop_size = 224
input_channel = 3
model = create_slowfast_with_roi_head()
self.assertIsNotNone(model)
# Test forwarding.
bbox_test_imputs = torch.tensor([[0.0, 10, 15, 20, 25], [0.0, 11, 16, 21, 26]])
for tensor in TestSlowFast._get_inputs(
input_channel, input_clip_length, input_crop_size
):
with torch.no_grad():
if tensor[0].shape[1] != input_channel:
with self.assertRaises(RuntimeError):
model(tensor, bbox_test_imputs)
continue
model(tensor, bbox_test_imputs)
def test_create_slowfast_with_callable(self):
"""
Test builder `create_slowfast` with callable inputs.
"""
for (norm, activation) in itertools.product(
(nn.BatchNorm3d, None), (nn.ReLU, nn.Sigmoid, None)
):
input_clip_length = 32
input_crop_size = 224
input_channel = 3
model = create_slowfast(
slowfast_channel_reduction_ratio=8,
slowfast_conv_channel_fusion_ratio=2,
slowfast_fusion_conv_kernel_size=(7, 1, 1),
slowfast_fusion_conv_stride=(4, 1, 1),
input_channels=(input_channel,) * 2,
model_depth=18,
model_num_class=400,
dropout_rate=0,
norm=norm,
activation=activation,
)
# Test forwarding.
for tensor in TestSlowFast._get_inputs(
input_channel, input_clip_length, input_crop_size
):
with torch.no_grad():
if tensor[0].shape[1] != input_channel:
with self.assertRaises(RuntimeError):
model(tensor)
continue
model(tensor)
@staticmethod
def _get_inputs(
channel: int = 3,
clip_length: int = 8,
crop_size: int = 224,
frame_ratios: Tuple[int] = (4, 1),
) -> torch.tensor:
"""
Provide different tensors as test cases.
Yield:
(torch.tensor): tensor as test case input.
"""
# Prepare random inputs as test cases.
shapes = ((1, channel, clip_length, crop_size, crop_size),)
for shape in shapes:
yield uniform_temporal_subsample_repeated(
torch.rand(shape), frame_ratios=frame_ratios, temporal_dim=2
)