forked from google-deepmind/deepmind-research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_test.py
159 lines (142 loc) · 5.58 KB
/
model_test.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Copyright 2020 Deepmind Technologies Limited.
#
# 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.
"""Tests for the PolyGen open-source version."""
from modules import FaceModel
from modules import VertexModel
import numpy as np
import tensorflow as tf
_BATCH_SIZE = 4
_TRANSFORMER_CONFIG = {
'num_layers': 2,
'hidden_size': 64,
'fc_size': 256
}
_CLASS_CONDITIONAL = True
_NUM_CLASSES = 4
_NUM_INPUT_VERTS = 50
_NUM_PAD_VERTS = 10
_NUM_INPUT_FACE_INDICES = 200
_QUANTIZATION_BITS = 8
_VERTEX_MODEL_USE_DISCRETE_EMBEDDINGS = True
_FACE_MODEL_DECODER_CROSS_ATTENTION = True
_FACE_MODEL_DISCRETE_EMBEDDINGS = True
_MAX_SAMPLE_LENGTH_VERTS = 10
_MAX_SAMPLE_LENGTH_FACES = 10
def _get_vertex_model_batch():
"""Returns batch with placeholders for vertex model inputs."""
return {
'class_label': tf.range(_BATCH_SIZE),
'vertices_flat': tf.placeholder(
dtype=tf.int32, shape=[_BATCH_SIZE, None]),
}
def _get_face_model_batch():
"""Returns batch with placeholders for face model inputs."""
return {
'vertices': tf.placeholder(
dtype=tf.float32, shape=[_BATCH_SIZE, None, 3]),
'vertices_mask': tf.placeholder(
dtype=tf.float32, shape=[_BATCH_SIZE, None]),
'faces': tf.placeholder(
dtype=tf.int32, shape=[_BATCH_SIZE, None]),
}
class VertexModelTest(tf.test.TestCase):
def setUp(self):
"""Defines a vertex model."""
super(VertexModelTest, self).setUp()
self.model = VertexModel(
decoder_config=_TRANSFORMER_CONFIG,
class_conditional=_CLASS_CONDITIONAL,
num_classes=_NUM_CLASSES,
max_num_input_verts=_NUM_INPUT_VERTS,
quantization_bits=_QUANTIZATION_BITS,
use_discrete_embeddings=_VERTEX_MODEL_USE_DISCRETE_EMBEDDINGS)
def test_model_runs(self):
"""Tests if the model runs without crashing."""
batch = _get_vertex_model_batch()
pred_dist = self.model(batch, is_training=False)
logits = pred_dist.logits
with self.session() as sess:
sess.run(tf.global_variables_initializer())
vertices_flat = np.random.randint(
2**_QUANTIZATION_BITS + 1,
size=[_BATCH_SIZE, _NUM_INPUT_VERTS * 3 + 1])
sess.run(logits, {batch['vertices_flat']: vertices_flat})
def test_sample_outputs_range(self):
"""Tests if the model produces samples in the correct range."""
context = {'class_label': tf.zeros((_BATCH_SIZE,), dtype=tf.int32)}
sample_dict = self.model.sample(
_BATCH_SIZE, max_sample_length=_MAX_SAMPLE_LENGTH_VERTS,
context=context)
with self.session() as sess:
sess.run(tf.global_variables_initializer())
sample_dict_np = sess.run(sample_dict)
in_range = np.logical_and(
0 <= sample_dict_np['vertices'],
sample_dict_np['vertices'] <= 2**_QUANTIZATION_BITS).all()
self.assertTrue(in_range)
class FaceModelTest(tf.test.TestCase):
def setUp(self):
"""Defines a face model."""
super(FaceModelTest, self).setUp()
self.model = FaceModel(
encoder_config=_TRANSFORMER_CONFIG,
decoder_config=_TRANSFORMER_CONFIG,
class_conditional=False,
max_seq_length=_NUM_INPUT_FACE_INDICES,
decoder_cross_attention=_FACE_MODEL_DECODER_CROSS_ATTENTION,
use_discrete_vertex_embeddings=_FACE_MODEL_DISCRETE_EMBEDDINGS,
quantization_bits=_QUANTIZATION_BITS)
def test_model_runs(self):
"""Tests if the model runs without crashing."""
batch = _get_face_model_batch()
pred_dist = self.model(batch, is_training=False)
logits = pred_dist.logits
with self.session() as sess:
sess.run(tf.global_variables_initializer())
vertices = np.random.rand(_BATCH_SIZE, _NUM_INPUT_VERTS, 3) - 0.5
vertices_mask = np.ones([_BATCH_SIZE, _NUM_INPUT_VERTS])
faces = np.random.randint(
_NUM_INPUT_VERTS + 2, size=[_BATCH_SIZE, _NUM_INPUT_FACE_INDICES])
sess.run(
logits,
{batch['vertices']: vertices,
batch['vertices_mask']: vertices_mask,
batch['faces']: faces}
)
def test_sample_outputs_range(self):
"""Tests if the model produces samples in the correct range."""
context = _get_face_model_batch()
del context['faces']
sample_dict = self.model.sample(
context, max_sample_length=_MAX_SAMPLE_LENGTH_FACES)
with self.session() as sess:
sess.run(tf.global_variables_initializer())
# Pad the vertices in order to test that the face model only outputs
# vertex indices in the unpadded range
vertices = np.pad(
np.random.rand(_BATCH_SIZE, _NUM_INPUT_VERTS, 3) - 0.5,
[[0, 0], [0, _NUM_PAD_VERTS], [0, 0]], mode='constant')
vertices_mask = np.pad(
np.ones([_BATCH_SIZE, _NUM_INPUT_VERTS]),
[[0, 0], [0, _NUM_PAD_VERTS]], mode='constant')
sample_dict_np = sess.run(
sample_dict,
{context['vertices']: vertices,
context['vertices_mask']: vertices_mask})
in_range = np.logical_and(
0 <= sample_dict_np['faces'],
sample_dict_np['faces'] <= _NUM_INPUT_VERTS + 1).all()
self.assertTrue(in_range)
if __name__ == '__main__':
tf.test.main()