test_ddpm.py 22.7 KB
Newer Older
limm's avatar
limm committed
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# Copyright (c) OpenMMLab. All rights reserved.
from copy import deepcopy

import numpy as np
import pytest
import torch

from mmgen.models.builder import build_model
from mmgen.models.diffusions import (BasicGaussianDiffusion,
                                     UniformTimeStepSampler)
from mmgen.models.diffusions.utils import _get_label_batch, _get_noise_batch


class TestBasicGaussianDiffusion:

    @classmethod
    def setup_class(cls):
        cls.config = dict(
            type='BasicGaussianDiffusion',
            num_timesteps=10,
            betas_cfg=dict(type='cosine'),
            train_cfg=None,
            test_cfg=None)
        cls.denoising = dict(
            type='DenoisingUnet',
            image_size=32,
            in_channels=3,
            base_channels=128,
            resblocks_per_downsample=1,
            attention_res=[16, 8],
            use_scale_shift_norm=True,
            dropout=0.3,
            num_heads=1,
            use_rescale_timesteps=True,
            output_cfg=dict(mean='eps', var='learned_range'),
        )
        cls.sampler = dict(type='UniformTimeStepSampler')
        cls.ddpm_loss = [
            dict(
                type='DDPMVLBLoss',
                rescale_mode='constant',
                rescale_cfg=dict(scale=1),
                data_info=dict(
                    mean_pred='mean_pred',
                    mean_target='mean_posterior',
                    logvar_pred='logvar_pred',
                    logvar_target='logvar_posterior'),
                log_cfgs=[
                    dict(
                        type='quartile',
                        prefix_name='loss_vlb',
                        total_timesteps=1000),
                    dict(type='name')
                ]),
            dict(
                type='DDPMMSELoss',
                log_cfgs=dict(
                    type='quartile',
                    prefix_name='loss_mse',
                    total_timesteps=1000),
            )
        ]

    def test_diffusion(self):
        # test build model
        cfg = deepcopy(self.config)
        cfg['denoising'] = self.denoising
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        diffusion = build_model(cfg)
        assert isinstance(diffusion, BasicGaussianDiffusion)
        assert isinstance(diffusion.sampler, UniformTimeStepSampler)
        assert not diffusion.use_ema

        # test build model --> parse train_cfg with ema
        cfg = deepcopy(self.config)
        cfg['denoising'] = self.denoising
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        cfg['train_cfg'] = dict(use_ema=True)
        diffusion = build_model(cfg)
        assert isinstance(diffusion, BasicGaussianDiffusion)
        assert isinstance(diffusion.sampler, UniformTimeStepSampler)
        assert diffusion.use_ema

        # test build_model --> parse train_cfg, without ema
        cfg = deepcopy(self.config)
        cfg['denoising'] = self.denoising
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        cfg['train_cfg'] = dict(use_ema=False)
        diffusion = build_model(cfg)
        assert isinstance(diffusion, BasicGaussianDiffusion)
        assert isinstance(diffusion.sampler, UniformTimeStepSampler)
        assert not diffusion.use_ema

        # test build_model --> parse test_cfg, with ema
        cfg = deepcopy(self.config)
        cfg['denoising'] = self.denoising
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        cfg['test_cfg'] = dict(use_ema=True)
        diffusion = build_model(cfg)
        assert isinstance(diffusion, BasicGaussianDiffusion)
        assert isinstance(diffusion.sampler, UniformTimeStepSampler)
        assert diffusion.use_ema

        # test build_model --> parse test_cfg, without ema
        cfg = deepcopy(self.config)
        cfg['denoising'] = self.denoising
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        cfg['test_cfg'] = dict(use_ema=False)
        diffusion = build_model(cfg)
        assert isinstance(diffusion, BasicGaussianDiffusion)
        assert isinstance(diffusion.sampler, UniformTimeStepSampler)
        assert not diffusion.use_ema

        # test sampler is None
        cfg = deepcopy(self.config)
        cfg['denoising'] = self.denoising
        cfg['timestep_sampler'] = None
        cfg['ddpm_loss'] = None
        diffusion = build_model(cfg)
        assert diffusion.sampler is None

        # test build model --> betas type = linear
        cfg = deepcopy(self.config)
        cfg['betas_cfg'] = dict(type='linear')
        cfg['denoising'] = self.denoising
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        diffusion = build_model(cfg)
        assert isinstance(diffusion, BasicGaussianDiffusion)
        assert isinstance(diffusion.sampler, UniformTimeStepSampler)
        assert not diffusion.use_ema

        # test build model --> wrong beta cfgs
        cfg = deepcopy(self.config)
        cfg['betas_cfg'] = dict(type='sine')
        cfg['denoising'] = self.denoising
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        with pytest.raises(AttributeError):
            diffusion = build_model(cfg)

        # test forward train --> raise error
        with pytest.raises(NotImplementedError):
            diffusion(None, return_loss=True)

        # test forward test
        imgs = diffusion(
            None, return_loss=False, mode='sampling', num_batches=2)
        assert imgs.shape == (2, 3, 32, 32)

        # test forward test --> wrong mode
        with pytest.raises(NotImplementedError):
            diffusion(
                None, return_loss=False, mode='generation', num_batches=2)

        # test reconstruction step --> given timestep
        cfg = deepcopy(self.config)
        cfg['denoising'] = self.denoising
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        diffusion = build_model(cfg)
        data_batch = dict(real_img=torch.randn(2, 3, 32, 32))
        fake_imgs = diffusion(
            data_batch, timesteps=[0, 5], mode='reconstruction')
        assert fake_imgs.shape == (2, 3, 32, 32)

        # test reconstruction step --> timestep = None
        output_dict = diffusion(
            data_batch, mode='reconstruction', return_noise=True)
        assert output_dict['fake_img'].shape == (20, 3, 32, 32)
        timestep = torch.cat([torch.LongTensor([i, i]) for i in range(10)])
        assert (output_dict['timesteps'] == timestep).all()

        # test reconstruction step --> noise in input
        noise = torch.randn(2, 3, 32, 32)
        output_dict = diffusion(
            data_batch, noise=noise, mode='reconstruction', return_noise=True)
        assert output_dict['noise'].shape == (20, 3, 32, 32)
        assert (output_dict['noise'] == torch.cat([noise for _ in range(10)],
                                                  dim=0)).all()

        # test reconstruction step --> noise in data_batch
        data_batch = dict(real_img=torch.randn(2, 3, 32, 32), noise=noise)
        output_dict = diffusion(
            data_batch, mode='reconstruction', return_noise=True)
        assert output_dict['noise'].shape == (20, 3, 32, 32)
        assert (output_dict['noise'] == torch.cat([noise for _ in range(10)],
                                                  dim=0)).all()

        # test reconstruction step --> noise in data_batch and input (error)
        data_batch = dict(
            real_img=torch.randn(2, 3, 32, 32),
            noise=torch.randn(2, 3, 32, 32))
        with pytest.raises(AssertionError):
            output_dict = diffusion(
                data_batch,
                noise=torch.randn(2, 3, 32, 32),
                mode='reconstruction')

        # test sample from noise
        fake_imgs = diffusion.sample_from_noise(None, num_batches=2)
        assert fake_imgs.shape == (2, 3, 32, 32)

        # test sample from noise --> save_intermedia
        output_dict = diffusion.sample_from_noise(
            None, num_batches=2, save_intermedia=True)
        assert list(output_dict.keys()) == [i for i in range(10, -1, -1)]

        # test sample from noise -->
        # sample model == ema/orig and use_ema == False
        output_dict = diffusion.sample_from_noise(
            None, num_batches=2, save_intermedia=True)

        # test sample from noise --> sample model == ema but use_ema = False
        with pytest.raises(AssertionError):
            diffusion.sample_from_noise(
                None, num_batches=2, sample_model='ema')

        # test sample from noise --> wrong sample method
        diffusion.sample_method = 'dk_method'
        with pytest.raises(AttributeError):
            diffusion.sample_from_noise(
                None, num_batches=2, save_intermedia=True)

        # test sample from noise --> sample model == orig/ema
        cfg = deepcopy(self.config)
        denoising_cfg = deepcopy(self.denoising)
        denoising_cfg['output_cfg'] = dict(mean='start_x', var='learned')
        cfg['train_cfg'] = dict(use_ema=True)
        cfg['denoising'] = denoising_cfg
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        diffusion = build_model(cfg)
        output = diffusion.sample_from_noise(None, num_batches=2)
        assert output.shape == (4, 3, 32, 32)

        # test sample from noise --> ema
        fake_img = diffusion.sample_from_noise(
            None, num_batches=2, sample_model='ema')
        assert fake_img.shape == (2, 3, 32, 32)

        # test sample from noise --> orig/ema + save_intermedia
        output_dict = diffusion.sample_from_noise(
            None, num_batches=2, save_intermedia=True)
        assert list(output_dict.keys()) == [i for i in range(10, -1, -1)]
        assert all([v.shape == (4, 3, 32, 32) for v in output_dict.values()])

        # test sample from noise -->
        # sample model == ema/orig return noise = True
        output_dict = diffusion.sample_from_noise(
            None,
            num_batches=2,
            save_intermedia=True,
            return_noise=True,
            sample_model='ema/orig')
        assert list(output_dict.keys()) == [i for i in range(10, -1, -1)]
        assert all([v.shape == (4, 3, 32, 32) for v in output_dict.values()])

        # test sample from noise -->
        # sample model == ema/orig return noise = True, timesteps_noise
        output_dict = diffusion.sample_from_noise(
            None,
            num_batches=2,
            save_intermedia=True,
            return_noise=True,
            timesteps_noise=torch.randn(10, 3, 32, 32),
            sample_model='ema/orig')
        assert list(output_dict.keys()) == [i for i in range(10, -1, -1)]
        assert all([v.shape == (4, 3, 32, 32) for v in output_dict.values()])

        # test denoising_var_mode = 'LEARNED' & denoising_mean_mode = 'start_x'
        cfg = deepcopy(self.config)
        denoising_cfg = deepcopy(self.denoising)
        denoising_cfg['output_cfg'] = dict(mean='start_x', var='learned')
        cfg['denoising'] = denoising_cfg
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        diffusion = build_model(cfg)
        output_dict = diffusion.sample_from_noise(None, num_batches=2)

        # test denoising_var_mode = 'fixed_large' &
        # denoising_mean_mode = 'previous_x'
        cfg = deepcopy(self.config)
        denoising_cfg = deepcopy(self.denoising)
        denoising_cfg['output_cfg'] = dict(
            mean='previous_x', var='fixed_large')
        cfg['denoising'] = denoising_cfg
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        diffusion = build_model(cfg)
        output_dict = diffusion.sample_from_noise(None, num_batches=2)

        # test denoising_var_mode = 'fixed_small' &
        # denoising_mean_mode = 'previous_x'
        cfg = deepcopy(self.config)
        denoising_cfg = deepcopy(self.denoising)
        denoising_cfg['output_cfg'] = dict(
            mean='previous_x', var='fixed_small')
        cfg['denoising'] = denoising_cfg
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        diffusion = build_model(cfg)
        output_dict = diffusion.sample_from_noise(None, num_batches=2)

        # test output_cfg --> error denoising_mean_mode
        cfg = deepcopy(self.config)
        denoising_cfg = deepcopy(self.denoising)
        denoising_cfg['output_cfg'] = dict(mean='x_0', var='fixed_small')
        cfg['denoising'] = denoising_cfg
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        diffusion = build_model(cfg)
        with pytest.raises(AttributeError):
            output_dict = diffusion.sample_from_noise(None, num_batches=2)

        # test output_cfg --> error denoising_var_mode
        cfg = deepcopy(self.config)
        denoising_cfg = deepcopy(self.denoising)
        denoising_cfg['output_cfg'] = dict(mean='previous_x', var='fixex')
        cfg['denoising'] = denoising_cfg
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = None
        diffusion = build_model(cfg)
        with pytest.raises(AttributeError):
            output_dict = diffusion.sample_from_noise(None, num_batches=2)

        # test train step --> no running status but have diffusion.iteration
        cfg = deepcopy(self.config)
        cfg['denoising'] = deepcopy(self.denoising)
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = deepcopy(self.ddpm_loss)
        diffusion = build_model(cfg)
        setattr(diffusion, 'iteration', 1)
        data = dict(real_img=torch.randn(2, 3, 32, 32))
        optimizer = dict(
            denoising=torch.optim.SGD(
                diffusion.denoising.parameters(), lr=0.01))
        model_outputs = diffusion.train_step(data, optimizer)
        assert 'log_vars' in model_outputs
        assert 'results' in model_outputs

        # test train step --> running status
        cfg = deepcopy(self.config)
        cfg['denoising'] = deepcopy(self.denoising)
        cfg['timestep_sampler'] = self.sampler
        cfg['ddpm_loss'] = deepcopy(self.ddpm_loss)
        diffusion = build_model(cfg)
        data = dict(real_img=torch.randn(2, 3, 32, 32))
        optimizer = dict(
            denoising=torch.optim.SGD(
                diffusion.denoising.parameters(), lr=0.01))
        model_outputs = diffusion.train_step(
            data, optimizer, running_status=dict(iteration=1))
        assert 'log_vars' in model_outputs
        assert 'results' in model_outputs


def test_ddpm_noise_batch_utils():
    image_shape = (3, 32, 32)
    num_batches = 2

    # noise is None, timestep is False
    noise_out = _get_noise_batch(None, image_shape, num_batches=num_batches)
    assert noise_out.shape == (2, 3, 32, 32)
    print(noise_out.shape)

    # noise is None, timestep is True
    noise_out = _get_noise_batch(None, image_shape, 4, num_batches, True)
    assert noise_out.shape == (4, 2, 3, 32, 32)
    print(noise_out.shape)

    # noise is callable, timestep is False
    noise_out = _get_noise_batch(
        lambda shape: torch.randn(*shape),
        image_shape,
        num_batches=num_batches)
    print(noise_out.shape)
    assert noise_out.shape == (2, 3, 32, 32)

    # noise is callable, timestep is True
    noise_out = _get_noise_batch(lambda shape: torch.randn(*shape),
                                 image_shape, 4, num_batches, True)
    print(noise_out.shape)
    assert noise_out.shape == (4, 2, 3, 32, 32)

    # noise is Tensor, timestep is False, noise dim = 3
    noise_inp = torch.randn(3, 32, 32)
    noise_out = _get_noise_batch(noise_inp, image_shape)
    print(noise_out.shape)
    assert noise_out.shape == (1, 3, 32, 32)

    # noise is Tensor, timestep is False, noise dim = 4
    noise_inp = torch.randn(2, 3, 32, 32)
    noise_out = _get_noise_batch(noise_inp, image_shape)
    print(noise_out.shape)
    assert noise_out.shape == (2, 3, 32, 32)

    # noise is Tensor, timestep is False, noise dim = 5
    noise_inp = torch.randn(1, 2, 3, 32, 32)
    with pytest.raises(ValueError):
        _get_noise_batch(noise_inp, image_shape)

    # noise is Tensor, timestep is True, noise dim = 4
    # noise.size(0) == num_batches
    noise_inp = torch.randn(4, 3, 32, 32)
    noise_out = _get_noise_batch(
        noise_inp,
        image_shape,
        num_timesteps=6,
        num_batches=4,
        timesteps_noise=True)
    print(noise_out.shape)
    assert noise_out.shape == (6, 4, 3, 32, 32)
    assert all([(noise_inp == noise).all() for noise in noise_out])

    # noise is Tensor, timestep is True, noise dim = 4
    # noise.size(0) == num_timesteps
    noise_inp = torch.randn(6, 3, 32, 32)
    noise_out = _get_noise_batch(
        noise_inp,
        image_shape,
        num_timesteps=6,
        num_batches=4,
        timesteps_noise=True)
    print(noise_out.shape)
    assert noise_out.shape == (6, 4, 3, 32, 32)
    assert all([(noise_inp == noise_out[:, idx, ...]).all()
                for idx in range(4)])

    # noise is Tensor, timestep is True, noise dim = 4
    # noise.size(0) == num_timesteps * num_batches
    noise_inp = torch.randn(24, 3, 32, 32)
    noise_out = _get_noise_batch(
        noise_inp,
        image_shape,
        num_timesteps=6,
        num_batches=4,
        timesteps_noise=True)
    print(noise_out.shape)
    assert noise_out.shape == (6, 4, 3, 32, 32)
    assert all([(noise_inp[idx] == noise_out[idx // 4][idx % 4]).all()
                for idx in range(24)])

    # noise is Tensor, timestep is True, noise dim = 4
    # noise_out.size(0) != num_batches * num_timesteps
    noise_inp = torch.randn(25, 3, 32, 32)
    with pytest.raises(ValueError):
        _get_noise_batch(
            noise_inp,
            image_shape,
            num_timesteps=6,
            num_batches=4,
            timesteps_noise=True)

    # noise is Tensor, timestep is True, noise dim = 5
    noise_inp = torch.randn(6, 4, 3, 32, 32)
    noise_out = _get_noise_batch(
        noise_inp,
        image_shape,
        num_timesteps=6,
        num_batches=4,
        timesteps_noise=True)
    print(noise_out.shape)
    assert noise_out.shape == (6, 4, 3, 32, 32)
    assert (noise_out == noise_inp).all()

    # noise is Tensor, timestep is True, noise dim = 6
    noise_inp = torch.randn(1, 6, 4, 3, 32, 32)
    with pytest.raises(ValueError):
        noise_out = _get_noise_batch(
            noise_inp,
            image_shape,
            num_timesteps=6,
            num_batches=4,
            timesteps_noise=True)


def test_ddpm_label_batch_utils():
    # num_classes = 0
    label_out = _get_label_batch(
        label=None, num_timesteps=2, num_classes=0, num_batches=2)
    assert label_out is None

    # num_classes = 0, label is not None
    with pytest.raises(AssertionError):
        label_out = _get_label_batch(
            label=torch.randint(0, 10, (2, )),
            num_timesteps=2,
            num_classes=0,
            num_batches=2)

    # label is None, timestep is False
    label_out = _get_label_batch(None, num_classes=10, num_batches=2)
    assert label_out.shape == (2, )
    assert torch.logical_and(label_out >= 0, label_out < 10).all()

    # label is None, timestep is True
    label_out = _get_label_batch(
        None,
        num_classes=10,
        num_batches=2,
        num_timesteps=4,
        timesteps_noise=True)
    assert label_out.shape == (4, 2)
    assert torch.logical_and(label_out >= 0, label_out < 10).all()

    # label is callable, timestep is False
    label_out = _get_label_batch(
        lambda shape: torch.randint(0, 10, shape),
        num_classes=10,
        num_batches=2)
    assert label_out.shape == (2, )
    assert torch.logical_and(label_out >= 0, label_out < 10).all()

    # label is callable, timestep is True
    label_out = _get_label_batch(
        lambda shape: torch.randint(0, 10, shape),
        num_classes=10,
        num_timesteps=4,
        num_batches=2,
        timesteps_noise=True)
    assert label_out.shape == (4, 2)
    assert torch.logical_and(label_out >= 0, label_out < 10).all()

    # label is tensor, timestep is False, label dim = 1
    label_inp = torch.LongTensor([4, 3])
    label_out = _get_label_batch(label_inp, num_classes=10)
    assert label_out.shape == (2, )
    assert (label_out == label_inp).all()

    # label is tensor, timestep is False, label dim = 0
    label_inp = torch.from_numpy(np.array(10))
    label_out = _get_label_batch(label_inp, num_classes=10)
    assert label_out.shape == (1, )

    # label is tensor, timestep is False, label dim = 2
    label_inp = torch.randint(0, 10, (4, 2))
    with pytest.raises(ValueError):
        _get_label_batch(label_inp, num_classes=10, num_batches=2)

    # label is tensor, timestep is True, label dim = 1
    # label.size(0) == num_batches
    label_inp = torch.randint(0, 10, (2, ))
    label_out = _get_label_batch(
        label_inp,
        num_timesteps=4,
        num_batches=2,
        num_classes=10,
        timesteps_noise=True)
    assert label_out.shape == (4, 2)
    assert all([(label_out[idx] == label_inp).all() for idx in range(4)])

    # label is tensor, timestep is True, label dim = 1
    # label.size(0) == num_timesteps
    label_inp = torch.randint(0, 10, (4, ))
    label_out = _get_label_batch(
        label_inp,
        num_timesteps=4,
        num_batches=2,
        num_classes=10,
        timesteps_noise=True)
    assert label_out.shape == (4, 2)
    assert all([(label_inp == label_out[:, idx]).all() for idx in range(2)])

    # label is tensor, timestep is True, label dim = 1
    # label.size(0) == num_timesteps * num_batches
    label_inp = torch.randint(0, 10, (8, ))
    label_out = _get_label_batch(
        label_inp,
        num_timesteps=4,
        num_batches=2,
        num_classes=10,
        timesteps_noise=True)
    assert label_out.shape == (4, 2)
    assert all([(label_inp[idx] == label_out[idx // 2][idx % 2]).all()
                for idx in range(8)])

    # label is tensor, timestep is True, label dim = 1
    # label.size(0) != num_timesteps * num_batches
    label_inp = torch.randint(0, 10, (9, ))
    with pytest.raises(ValueError):
        _get_label_batch(
            label_inp,
            num_timesteps=4,
            num_batches=2,
            num_classes=10,
            timesteps_noise=True)

    # label is tensor, timestep is True, label dim = 2
    label_inp = torch.randint(0, 10, (4, 2))
    label_out = _get_label_batch(
        label_inp,
        num_timesteps=4,
        num_batches=2,
        num_classes=10,
        timesteps_noise=True)
    assert label_out.shape == (4, 2)
    assert (label_out == label_inp).all()

    # label is tensor, timestep is True, label dim = 3
    label_inp = torch.randint(0, 10, (4, 2, 1))
    with pytest.raises(ValueError):
        _get_label_batch(
            label_inp,
            num_timesteps=4,
            num_batches=2,
            num_classes=10,
            timesteps_noise=True)