forked from BR-IDL/PaddleViT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_resmlp.py
109 lines (90 loc) · 3.64 KB
/
test_resmlp.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
# Copyright (c) 2021 PPViT Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import numpy as np
import paddle
import paddle.nn as nn
from config import *
from resmlp import Identity
from resmlp import PatchEmbedding
from resmlp import ResMlp
from resmlp import Mlp
from resmlp import ResBlock
from resmlp import Affine
from resmlp import build_res_mlp
class MlpTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
paddle.set_device('cpu')
cls.config = get_config()
cls.dummy_img = np.random.randn(4, 3, 224, 224).astype('float32')
cls.dummy_tensor = paddle.to_tensor(cls.dummy_img)
cls.model = build_res_mlp(cls.config)
@classmethod
def tearDown(cls):
pass
#@unittest.skip('skip for debug')
def test_out_shape(self):
out = MlpTest.model(MlpTest.dummy_tensor)
self.assertEqual(out.shape, [4, 1000])
#@unittest.skip('skip for debug')
def test_all_parameters_updated(self):
optim = paddle.optimizer.SGD(
parameters=MlpTest.model.parameters(), learning_rate=0.1)
out = MlpTest.model(MlpTest.dummy_tensor)
loss = out.mean()
loss.backward()
optim.step()
for name, param in MlpTest.model.named_parameters():
if not param.stop_gradient:
self.assertIsNotNone(param.gradient())
self.assertNotEqual(0, np.sum(param.gradient()**2))
#@unittest.skip('skip for debug')
def test_embeddings(self):
embed = PatchEmbedding(embed_dim=768)
dummy_img = np.random.randn(4, 3, 224, 224).astype('float32')
dummy_tensor = paddle.to_tensor(dummy_img)
embed_out = embed(dummy_tensor)
self.assertEqual(embed_out.shape, [4, 3136, 768])
#@unittest.skip('skip for debug')
def test_mlp(self):
mlp_op = Mlp(768, 256, 0.0)
dummy_img = np.random.randn(4, 50, 768).astype('float32')
dummy_tensor = paddle.to_tensor(dummy_img)
out = mlp_op(dummy_tensor)
self.assertEqual(out.shape, [4, 50, 768])
#@unittest.skip('skip for debug')
def test_identity(self):
op = Identity()
dummy_img = np.random.randn(4, 50, 768).astype('float32')
dummy_tensor = paddle.to_tensor(dummy_img)
out = op(dummy_tensor)
self.assertEqual(out.shape, [4, 50, 768])
#@unittest.skip('skip for debug')
def test_mixer_block(self):
op = ResBlock(dim=768, seq_len=50)
dummy_img = np.random.randn(4, 50, 768).astype('float32')
dummy_tensor = paddle.to_tensor(dummy_img)
out = op(dummy_tensor)
self.assertEqual(out.shape, [4, 50, 768])
def test_affine(self):
op = Affine(dim=768)
dummy_tensor = paddle.ones([4, 50, 768])
dummy_alpha = paddle.ones([1, 1, 768]) * 0.5
dummy_beta = paddle.ones([1, 1, 768]) * 0.2
op.alpha.set_value(dummy_alpha)
op.beta.set_value(dummy_beta)
out = op(dummy_tensor)
self.assertEqual(out.shape, [4, 50, 768])
self.assertEqual(paddle.equal_all(out, paddle.ones([4, 50, 768]) * 0.7).numpy(), True)