test_optflow.py 10.8 KB
Newer Older
1
# Copyright (c) OpenMMLab. All rights reserved.
Kai Chen's avatar
Kai Chen committed
2
3
4
5
import os
import os.path as osp
import tempfile

6
import cv2
Kai Chen's avatar
Kai Chen committed
7
8
import numpy as np
import pytest
Kai Chen's avatar
Kai Chen committed
9
10
11
from numpy.testing import assert_array_almost_equal, assert_array_equal

import mmcv
Kai Chen's avatar
Kai Chen committed
12
13


Kai Chen's avatar
Kai Chen committed
14
def test_flowread():
Kai Chen's avatar
Kai Chen committed
15
    data_dir = osp.join(osp.dirname(__file__), '../data')
Kai Chen's avatar
Kai Chen committed
16
17
18
    flow_shape = (60, 80, 2)

    # read .flo file
Kai Chen's avatar
Kai Chen committed
19
    flow = mmcv.flowread(osp.join(data_dir, 'optflow.flo'))
Kai Chen's avatar
Kai Chen committed
20
21
22
23
    assert flow.shape == flow_shape

    # pseudo read
    flow_same = mmcv.flowread(flow)
Kai Chen's avatar
Kai Chen committed
24
    assert_array_equal(flow, flow_same)
Kai Chen's avatar
Kai Chen committed
25
26

    # read quantized flow concatenated vertically
Kai Chen's avatar
Kai Chen committed
27
    flow = mmcv.flowread(
Kai Chen's avatar
Kai Chen committed
28
        osp.join(data_dir, 'optflow_concat0.jpg'), quantize=True, denorm=True)
Kai Chen's avatar
Kai Chen committed
29
30
31
    assert flow.shape == flow_shape

    # read quantized flow concatenated horizontally
Kai Chen's avatar
Kai Chen committed
32
    flow = mmcv.flowread(
Kai Chen's avatar
Kai Chen committed
33
        osp.join(data_dir, 'optflow_concat1.jpg'),
Kai Chen's avatar
Kai Chen committed
34
35
36
        quantize=True,
        concat_axis=1,
        denorm=True)
Kai Chen's avatar
Kai Chen committed
37
38
39
    assert flow.shape == flow_shape

    # test exceptions
Kai Chen's avatar
Kai Chen committed
40
    notflow_file = osp.join(data_dir, 'color.jpg')
Kai Chen's avatar
Kai Chen committed
41
42
43
44
    with pytest.raises(TypeError):
        mmcv.flowread(1)
    with pytest.raises(IOError):
        mmcv.flowread(notflow_file)
Kai Chen's avatar
Kai Chen committed
45
    with pytest.raises(IOError):
Kai Chen's avatar
Kai Chen committed
46
        mmcv.flowread(notflow_file, quantize=True)
Kai Chen's avatar
Kai Chen committed
47
    with pytest.raises(ValueError):
Kai Chen's avatar
Kai Chen committed
48
        mmcv.flowread(np.zeros((100, 100, 1)))
Kai Chen's avatar
Kai Chen committed
49
50


Kai Chen's avatar
Kai Chen committed
51
def test_flowwrite():
Kai Chen's avatar
Kai Chen committed
52
    flow = np.random.rand(100, 100, 2).astype(np.float32)
Kai Chen's avatar
Kai Chen committed
53

Kai Chen's avatar
Kai Chen committed
54
    # write to a .flo file
55
    tmp_filehandler, filename = tempfile.mkstemp()
Kai Chen's avatar
Kai Chen committed
56
57
    mmcv.flowwrite(flow, filename)
    flow_from_file = mmcv.flowread(filename)
Kai Chen's avatar
Kai Chen committed
58
    assert_array_equal(flow, flow_from_file)
59
    os.close(tmp_filehandler)
Kai Chen's avatar
Kai Chen committed
60
    os.remove(filename)
Kai Chen's avatar
Kai Chen committed
61

Kai Chen's avatar
Kai Chen committed
62
    # write to two .jpg files
Kai Chen's avatar
Kai Chen committed
63
64
    tmp_filename = osp.join(tempfile.gettempdir(), 'mmcv_test_flow.jpg')
    for concat_axis in range(2):
Kai Chen's avatar
Kai Chen committed
65
66
        mmcv.flowwrite(
            flow, tmp_filename, quantize=True, concat_axis=concat_axis)
Kai Chen's avatar
Kai Chen committed
67
68
69
70
71
72
73
74
        shape = (200, 100) if concat_axis == 0 else (100, 200)
        assert osp.isfile(tmp_filename)
        assert mmcv.imread(tmp_filename, flag='unchanged').shape == shape
        os.remove(tmp_filename)

    # test exceptions
    with pytest.raises(AssertionError):
        mmcv.flowwrite(flow, tmp_filename, quantize=True, concat_axis=2)
Kai Chen's avatar
Kai Chen committed
75
76
77
78
79
80
81
82
83
84
85
86


def test_quantize_flow():
    flow = (np.random.rand(10, 8, 2).astype(np.float32) - 0.5) * 15
    max_val = 5.0
    dx, dy = mmcv.quantize_flow(flow, max_val=max_val, norm=False)
    ref = np.zeros_like(flow, dtype=np.uint8)
    for i in range(ref.shape[0]):
        for j in range(ref.shape[1]):
            for k in range(ref.shape[2]):
                val = flow[i, j, k] + max_val
                val = min(max(val, 0), 2 * max_val)
Kai Chen's avatar
Kai Chen committed
87
                ref[i, j, k] = min(np.floor(255 * val / (2 * max_val)), 254)
Kai Chen's avatar
Kai Chen committed
88
89
90
91
92
93
94
95
96
97
98
    assert_array_equal(dx, ref[..., 0])
    assert_array_equal(dy, ref[..., 1])
    max_val = 0.5
    dx, dy = mmcv.quantize_flow(flow, max_val=max_val, norm=True)
    ref = np.zeros_like(flow, dtype=np.uint8)
    for i in range(ref.shape[0]):
        for j in range(ref.shape[1]):
            for k in range(ref.shape[2]):
                scale = flow.shape[1] if k == 0 else flow.shape[0]
                val = flow[i, j, k] / scale + max_val
                val = min(max(val, 0), 2 * max_val)
Kai Chen's avatar
Kai Chen committed
99
                ref[i, j, k] = min(np.floor(255 * val / (2 * max_val)), 254)
Kai Chen's avatar
Kai Chen committed
100
101
102
103
104
105
106
107
108
109
110
111
    assert_array_equal(dx, ref[..., 0])
    assert_array_equal(dy, ref[..., 1])


def test_dequantize_flow():
    dx = np.random.randint(256, size=(10, 8), dtype=np.uint8)
    dy = np.random.randint(256, size=(10, 8), dtype=np.uint8)
    max_val = 5.0
    flow = mmcv.dequantize_flow(dx, dy, max_val=max_val, denorm=False)
    ref = np.zeros_like(flow, dtype=np.float32)
    for i in range(ref.shape[0]):
        for j in range(ref.shape[1]):
Kai Chen's avatar
Kai Chen committed
112
113
            ref[i, j, 0] = float(dx[i, j] + 0.5) * 2 * max_val / 255 - max_val
            ref[i, j, 1] = float(dy[i, j] + 0.5) * 2 * max_val / 255 - max_val
Kai Chen's avatar
Kai Chen committed
114
115
116
117
118
119
120
    assert_array_almost_equal(flow, ref)
    max_val = 0.5
    flow = mmcv.dequantize_flow(dx, dy, max_val=max_val, denorm=True)
    h, w = dx.shape
    ref = np.zeros_like(flow, dtype=np.float32)
    for i in range(ref.shape[0]):
        for j in range(ref.shape[1]):
121
122
123
124
            ref[i, j,
                0] = (float(dx[i, j] + 0.5) * 2 * max_val / 255 - max_val) * w
            ref[i, j,
                1] = (float(dy[i, j] + 0.5) * 2 * max_val / 255 - max_val) * h
Kai Chen's avatar
Kai Chen committed
125
126
127
128
    assert_array_almost_equal(flow, ref)


def test_flow2rgb():
129
130
    flow = np.array([[[0, 0], [0.5, 0.5], [1, 1], [2, 1], [3, np.inf]]],
                    dtype=np.float32)
Kai Chen's avatar
Kai Chen committed
131
132
133
134
135
136
137
138
139
140
141
142
143
    flow_img = mmcv.flow2rgb(flow)
    # yapf: disable
    assert_array_almost_equal(
        flow_img,
        np.array([[[1., 1., 1.],
                   [1., 0.826074731, 0.683772236],
                   [1., 0.652149462, 0.367544472],
                   [1., 0.265650552, 5.96046448e-08],
                   [0., 0., 0.]]],
                 dtype=np.float32))
    # yapf: enable


144
def test_flow_warp():
145
146
147
    img = np.zeros((5, 5, 3))
    img[2, 2, 0] = 1
    flow = np.ones((5, 5, 2))
148

149
150
    res_nn = mmcv.flow_warp(img, flow, interpolate_mode='nearest')
    res_bi = mmcv.flow_warp(img, flow, interpolate_mode='bilinear')
151

152
    assert_array_almost_equal(res_nn, res_bi, decimal=5)
153

154
155
156
157
158
    img = np.zeros((5, 5, 1))
    img[2, 2, 0] = 1
    img[2, 3, 0] = 0.75
    flow = np.zeros((5, 5, 2))
    flow[2, 2, :] = [0.5, 0.7]
159

160
161
162
163
    res_ = np.copy(img)
    res_[2, 2] = 0.5 * 0.3 + 0.75 * 0.5 * 0.3
    res_bi = mmcv.flow_warp(img, flow, interpolate_mode='bilinear')
    assert_array_almost_equal(res_, res_bi, decimal=5)
164

165
166
    with pytest.raises(NotImplementedError):
        _ = mmcv.flow_warp(img, flow, interpolate_mode='xxx')
167

168
169
    with pytest.raises(AssertionError):
        _ = mmcv.flow_warp(img, flow[:, :, 0], interpolate_mode='xxx')
170
171


Kai Chen's avatar
Kai Chen committed
172
173
174
175
176
def test_make_color_wheel():
    default_color_wheel = mmcv.make_color_wheel()
    color_wheel = mmcv.make_color_wheel([2, 2, 2, 2, 2, 2])
    # yapf: disable
    assert_array_equal(default_color_wheel, np.array(
Kai Chen's avatar
Kai Chen committed
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
        [[1.       , 0.        , 0.        ],  # noqa
        [1.        , 0.06666667, 0.        ],  # noqa
        [1.        , 0.13333334, 0.        ],  # noqa
        [1.        , 0.2       , 0.        ],  # noqa
        [1.        , 0.26666668, 0.        ],  # noqa
        [1.        , 0.33333334, 0.        ],  # noqa
        [1.        , 0.4       , 0.        ],  # noqa
        [1.        , 0.46666667, 0.        ],  # noqa
        [1.        , 0.53333336, 0.        ],  # noqa
        [1.        , 0.6       , 0.        ],  # noqa
        [1.        , 0.6666667 , 0.        ],  # noqa
        [1.        , 0.73333335, 0.        ],  # noqa
        [1.        , 0.8       , 0.        ],  # noqa
        [1.        , 0.8666667 , 0.        ],  # noqa
        [1.        , 0.93333334, 0.        ],  # noqa
        [1.        , 1.        , 0.        ],  # noqa
        [0.8333333 , 1.        , 0.        ],  # noqa
        [0.6666667 , 1.        , 0.        ],  # noqa
        [0.5       , 1.        , 0.        ],  # noqa
        [0.33333334, 1.        , 0.        ],  # noqa
        [0.16666667, 1.        , 0.        ],  # noqa
        [0.        , 1.        , 0.        ],  # noqa
        [0.        , 1.        , 0.25      ],  # noqa
        [0.        , 1.        , 0.5       ],  # noqa
        [0.        , 1.        , 0.75      ],  # noqa
        [0.        , 1.        , 1.        ],  # noqa
        [0.        , 0.90909094, 1.        ],  # noqa
        [0.        , 0.8181818 , 1.        ],  # noqa
        [0.        , 0.72727275, 1.        ],  # noqa
        [0.        , 0.6363636 , 1.        ],  # noqa
        [0.        , 0.54545456, 1.        ],  # noqa
        [0.        , 0.45454547, 1.        ],  # noqa
        [0.        , 0.36363637, 1.        ],  # noqa
        [0.        , 0.27272728, 1.        ],  # noqa
        [0.        , 0.18181819, 1.        ],  # noqa
        [0.        , 0.09090909, 1.        ],  # noqa
        [0.        , 0.        , 1.        ],  # noqa
        [0.07692308, 0.        , 1.        ],  # noqa
        [0.15384616, 0.        , 1.        ],  # noqa
        [0.23076923, 0.        , 1.        ],  # noqa
        [0.30769232, 0.        , 1.        ],  # noqa
        [0.3846154 , 0.        , 1.        ],  # noqa
        [0.46153846, 0.        , 1.        ],  # noqa
        [0.53846157, 0.        , 1.        ],  # noqa
        [0.61538464, 0.        , 1.        ],  # noqa
        [0.6923077 , 0.        , 1.        ],  # noqa
        [0.7692308 , 0.        , 1.        ],  # noqa
        [0.84615386, 0.        , 1.        ],  # noqa
        [0.9230769 , 0.        , 1.        ],  # noqa
        [1.        , 0.        , 1.        ],  # noqa
        [1.        , 0.        , 0.8333333 ],  # noqa
        [1.        , 0.        , 0.6666667 ],  # noqa
        [1.        , 0.        , 0.5       ],  # noqa
        [1.        , 0.        , 0.33333334],  # noqa
        [1.        , 0.        , 0.16666667]], dtype=np.float32))  # noqa
Kai Chen's avatar
Kai Chen committed
232
233
234

    assert_array_equal(
        color_wheel,
Kai Chen's avatar
Kai Chen committed
235
236
237
238
239
240
241
242
243
244
245
246
        np.array([[1., 0. , 0. ],  # noqa
                 [1. , 0.5, 0. ],  # noqa
                 [1. , 1. , 0. ],  # noqa
                 [0.5, 1. , 0. ],  # noqa
                 [0. , 1. , 0. ],  # noqa
                 [0. , 1. , 0.5],  # noqa
                 [0. , 1. , 1. ],  # noqa
                 [0. , 0.5, 1. ],  # noqa
                 [0. , 0. , 1. ],  # noqa
                 [0.5, 0. , 1. ],  # noqa
                 [1. , 0. , 1. ],  # noqa
                 [1. , 0. , 0.5]], dtype=np.float32))  # noqa
Kai Chen's avatar
Kai Chen committed
247
    # yapf: enable
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


def test_flow_from_bytes():
    data_dir = osp.join(osp.dirname(__file__), '../data')
    flow_shape = (60, 80, 2)
    flow_file = osp.join(data_dir, 'optflow.flo')

    # read .flo file
    flow_fromfile = mmcv.flowread(flow_file)

    with open(flow_file, 'rb') as f:
        flow_bytes = f.read()
    flow_frombytes = mmcv.flow_from_bytes(flow_bytes)

    assert flow_frombytes.shape == flow_shape
    assert np.all(flow_frombytes == flow_fromfile)


def test_sparse_flow_from_bytes():
    data_dir = osp.join(osp.dirname(__file__), '../data')
    flow_file = osp.join(data_dir, 'sparse_flow.png')

    with open(flow_file, 'rb') as f:
        flow_bytes = f.read()
    # read flow from bytes
    flow_frombytes, valid_frombytes = mmcv.sparse_flow_from_bytes(flow_bytes)

    # test flow shape is [H, W, 2] and valid shape is [H, W]
    assert flow_frombytes.shape[:2] == valid_frombytes.shape
    assert flow_frombytes.shape[2] == 2

    def read_sparse_flow_from_file():
        flow = cv2.imread(flow_file, cv2.IMREAD_ANYDEPTH | cv2.IMREAD_COLOR)
        flow = flow[:, :, ::-1].astype(np.float32)
        flow, valid = flow[:, :, :2], flow[:, :, 2]
        flow = (flow - 2**15) / 64.0
        return flow, valid

    # read flow from file
    flow_flowfile, valid_fromfile = read_sparse_flow_from_file()

    assert np.all(flow_frombytes == flow_flowfile)
    assert np.all(valid_frombytes == valid_fromfile)