File: test_tfinputshapes.py

package info (click to toggle)
python-dtcwt 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 8,588 kB
  • sloc: python: 6,287; sh: 29; makefile: 13
file content (173 lines) | stat: -rw-r--r-- 5,692 bytes parent folder | download
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
import os
import pytest

from importlib import import_module

from .util import skip_if_no_tf
from dtcwt.utils import unpack
import dtcwt
import dtcwt.compat

PRECISION_DECIMAL = 5


@skip_if_no_tf
def setup_module():
    global tf
    tf = import_module('tensorflow')
    dtcwt.push_backend('tf')

    # Make sure we run tests on cpu rather than gpus
    os.environ["CUDA_VISIBLE_DEVICES"] = ""


@skip_if_no_tf
@pytest.mark.parametrize("nlevels, include_scale", [
    (2,False),
    (2,True),
    (4,False),
    (3,True)
])
def test_scales(nlevels, include_scale):
    in_ = tf.placeholder(tf.float32, [512, 512])
    t = dtcwt.Transform2d()

    p = t.forward(in_, nlevels, include_scale)

    # At level 1, the lowpass output will be the same size as the input. At
    # levels above that, it will be half the size per level
    extent = 512 * 2**(-(nlevels-1))
    assert p.lowpass_op.get_shape().as_list() == [extent, extent]
    assert p.lowpass_op.dtype == tf.float32

    for i in range(nlevels):
        extent = 512 * 2**(-(i+1))
        assert (p.highpasses_ops[i].get_shape().as_list() ==
                [extent, extent, 6])
        assert (p.highpasses_ops[i].dtype ==
                tf.complex64)
        if include_scale:
            assert (p.scales_ops[i].get_shape().as_list() ==
                    [2*extent, 2*extent])
            assert p.scales_ops[i].dtype == tf.float32


@skip_if_no_tf
@pytest.mark.parametrize("nlevels, include_scale", [
    (2,False),
    (2,True),
    (4,False),
    (3,True)
])
def test_2d_input_tuple(nlevels, include_scale):
    in_ = tf.placeholder(tf.float32, [512, 512])
    t = dtcwt.Transform2d()
    if include_scale:
        Yl, Yh, Yscale = unpack(t.forward(in_, nlevels, include_scale), 'tf')
    else:
        Yl, Yh = unpack(t.forward(in_, nlevels, include_scale), 'tf')

    # At level 1, the lowpass output will be the same size as the input. At
    # levels above that, it will be half the size per level
    extent = 512 * 2**(-(nlevels-1))
    assert Yl.get_shape().as_list() == [extent, extent]
    assert Yl.dtype == tf.float32

    for i in range(nlevels):
        extent = 512 * 2**(-(i+1))
        assert Yh[i].get_shape().as_list() == [extent, extent, 6]
        assert Yh[i].dtype == tf.complex64
        if include_scale:
            assert Yscale[i].get_shape().as_list() == [2*extent, 2*extent]
            assert Yscale[i].dtype == tf.float32


@skip_if_no_tf
@pytest.mark.parametrize("nlevels, include_scale, batch_size", [
    (2,False,None),
    (2,True,10),
    (4,False,None),
    (3,True,2)
])
def test_batch_input(nlevels, include_scale, batch_size):
    in_ = tf.placeholder(tf.float32, [batch_size, 512, 512])
    t = dtcwt.Transform2d()
    p = t.forward_channels(in_, "nhw", nlevels, include_scale)

    # At level 1, the lowpass output will be the same size as the input. At
    # levels above that, it will be half the size per level
    extent = 512 * 2**(-(nlevels-1))
    assert p.lowpass_op.get_shape().as_list() == [batch_size, extent, extent]
    assert p.lowpass_op.dtype == tf.float32

    for i in range(nlevels):
        extent = 512 * 2**(-(i+1))
        assert (p.highpasses_ops[i].get_shape().as_list() ==
                [batch_size, extent, extent, 6])
        assert p.highpasses_ops[i].dtype == tf.complex64
        if include_scale:
            assert (p.scales_ops[i].get_shape().as_list() ==
                    [batch_size, 2*extent, 2*extent])
            assert p.scales_ops[i].dtype == tf.float32


@skip_if_no_tf
@pytest.mark.parametrize("nlevels, include_scale, batch_size", [
    (2,False,None),
    (2,True,10),
    (4,False,None),
    (3,True,2)
])
def test_batch_input_tuple(nlevels, include_scale, batch_size):
    in_ = tf.placeholder(tf.float32, [batch_size, 512, 512])
    t = dtcwt.Transform2d()

    if include_scale:
        Yl, Yh, Yscale = unpack(
            t.forward_channels(in_, "nhw", nlevels, include_scale), "tf")
    else:
        Yl, Yh = unpack(
            t.forward_channels(in_, "nhw", nlevels, include_scale), "tf")

    # At level 1, the lowpass output will be the same size as the input. At
    # levels above that, it will be half the size per level
    extent = 512 * 2**(-(nlevels-1))
    assert Yl.get_shape().as_list() == [batch_size, extent, extent]
    assert Yl.dtype == tf.float32

    for i in range(nlevels):
        extent = 512 * 2**(-(i+1))
        assert Yh[i].get_shape().as_list() == [batch_size, extent, extent, 6]
        assert Yh[i].dtype == tf.complex64
        if include_scale:
            assert (Yscale[i].get_shape().as_list() ==
                    [batch_size, 2*extent, 2*extent])
            assert Yscale[i].dtype == tf.float32


@skip_if_no_tf
@pytest.mark.parametrize("nlevels, channels", [
    (2,5),
    (2,2),
    (4,10),
    (3,6)
])
def test_multichannel(nlevels, channels):
    in_ = tf.placeholder(tf.float32, [None, 512, 512, channels])
    t = dtcwt.Transform2d()
    Yl, Yh, Yscale = unpack(
        t.forward_channels(in_, "nhwc", nlevels, include_scale=True), "tf")
    # At level 1, the lowpass output will be the same size as the input. At
    # levels above that, it will be half the size per level
    extent = 512 * 2**(-(nlevels-1))
    assert Yl.get_shape().as_list() == [None, extent, extent, channels]
    assert Yl.dtype == tf.float32

    for i in range(nlevels):
        extent = 512 * 2**(-(i+1))
        assert (Yh[i].get_shape().as_list() ==
                [None, extent, extent, channels, 6])
        assert Yh[i].dtype == tf.complex64
        assert Yscale[i].get_shape().as_list() == [
            None, 2*extent, 2*extent, channels]
        assert Yscale[i].dtype == tf.float32