[c1b1c5]: / ViTPose / tests / test_models / test_layer.py

Download this file

69 lines (56 with data), 1.7 kB

 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
# Copyright (c) OpenMMLab. All rights reserved.
import numpy as np
import torch
import torch.nn as nn
from mmcv.cnn import build_conv_layer, build_upsample_layer
def test_build_upsample_layer():
layer1 = nn.ConvTranspose2d(
in_channels=3,
out_channels=10,
kernel_size=3,
stride=2,
padding=1,
output_padding=1,
bias=False)
layer2 = build_upsample_layer(
dict(type='deconv'),
in_channels=3,
out_channels=10,
kernel_size=3,
stride=2,
padding=1,
output_padding=1,
bias=False)
layer2.load_state_dict(layer1.state_dict())
input_shape = (1, 3, 32, 32)
inputs = _demo_inputs(input_shape)
out1 = layer1(inputs)
out2 = layer2(inputs)
assert torch.equal(out1, out2)
def test_build_conv_layer():
layer1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, stride=1, padding=1)
layer2 = build_conv_layer(
cfg=dict(type='Conv2d'),
in_channels=3,
out_channels=10,
kernel_size=3,
stride=1,
padding=1)
layer2.load_state_dict(layer1.state_dict())
input_shape = (1, 3, 32, 32)
inputs = _demo_inputs(input_shape)
out1 = layer1(inputs)
out2 = layer2(inputs)
assert torch.equal(out1, out2)
def _demo_inputs(input_shape=(1, 3, 64, 64)):
"""Create a superset of inputs needed to run backbone.
Args:
input_shape (tuple): input batch dimensions.
Default: (1, 3, 64, 64).
Returns:
Random input tensor with the size of input_shape.
"""
inps = np.random.random(input_shape)
inps = torch.FloatTensor(inps)
return inps