forked from google-deepmind/deepmind-research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_loop_test.py
97 lines (77 loc) · 3.56 KB
/
main_loop_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
# 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
#
# https://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 BYOL's main training loop."""
from absl import flags
from absl.testing import absltest
import tensorflow_datasets as tfds
from byol import byol_experiment
from byol import eval_experiment
from byol import main_loop
from byol.configs import byol as byol_config
from byol.configs import eval as eval_config
FLAGS = flags.FLAGS
class MainLoopTest(absltest.TestCase):
def test_pretrain(self):
config = byol_config.get_config(num_epochs=40, batch_size=4)
temp_dir = self.create_tempdir().full_path
# Override some config fields to make test lighter.
config['network_config']['encoder_class'] = 'TinyResNet'
config['network_config']['projector_hidden_size'] = 256
config['network_config']['predictor_hidden_size'] = 256
config['checkpointing_config']['checkpoint_dir'] = temp_dir
config['evaluation_config']['batch_size'] = 16
config['max_steps'] = 16
with tfds.testing.mock_data(num_examples=64):
experiment_class = byol_experiment.ByolExperiment
main_loop.train_loop(experiment_class, config)
main_loop.eval_loop(experiment_class, config)
def test_linear_eval(self):
config = eval_config.get_config(checkpoint_to_evaluate=None, batch_size=4)
temp_dir = self.create_tempdir().full_path
# Override some config fields to make test lighter.
config['network_config']['encoder_class'] = 'TinyResNet'
config['allow_train_from_scratch'] = True
config['checkpointing_config']['checkpoint_dir'] = temp_dir
config['evaluation_config']['batch_size'] = 16
config['max_steps'] = 16
with tfds.testing.mock_data(num_examples=64):
experiment_class = eval_experiment.EvalExperiment
main_loop.train_loop(experiment_class, config)
main_loop.eval_loop(experiment_class, config)
def test_pipeline(self):
b_config = byol_config.get_config(num_epochs=40, batch_size=4)
temp_dir = self.create_tempdir().full_path
# Override some config fields to make test lighter.
b_config['network_config']['encoder_class'] = 'TinyResNet'
b_config['network_config']['projector_hidden_size'] = 256
b_config['network_config']['predictor_hidden_size'] = 256
b_config['checkpointing_config']['checkpoint_dir'] = temp_dir
b_config['evaluation_config']['batch_size'] = 16
b_config['max_steps'] = 16
with tfds.testing.mock_data(num_examples=64):
main_loop.train_loop(byol_experiment.ByolExperiment, b_config)
e_config = eval_config.get_config(
checkpoint_to_evaluate=f'{temp_dir}/pretrain.pkl',
batch_size=4)
# Override some config fields to make test lighter.
e_config['network_config']['encoder_class'] = 'TinyResNet'
e_config['allow_train_from_scratch'] = True
e_config['checkpointing_config']['checkpoint_dir'] = temp_dir
e_config['evaluation_config']['batch_size'] = 16
e_config['max_steps'] = 16
with tfds.testing.mock_data(num_examples=64):
main_loop.train_loop(eval_experiment.EvalExperiment, e_config)
if __name__ == '__main__':
absltest.main()