[6d389a]: / tests / test_runtime / test_inference.py

Download this file

150 lines (125 with data), 5.8 kB

  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
# Copyright (c) OpenMMLab. All rights reserved.
import mmcv
import numpy as np
import pytest
import torch
import torch.nn as nn
from mmaction.apis import inference_recognizer, init_recognizer
video_config_file = 'configs/recognition/tsn/tsn_r50_video_inference_1x1x3_100e_kinetics400_rgb.py' # noqa: E501
frame_config_file = 'configs/recognition/tsn/tsn_r50_inference_1x1x3_100e_kinetics400_rgb.py' # noqa: E501
flow_frame_config_file = 'configs/recognition/tsn/tsn_r50_320p_1x1x3_110e_kinetics400_flow.py' # noqa: E501
video_path = 'demo/demo.mp4'
frames_path = 'tests/data/imgs'
def test_init_recognizer():
with pytest.raises(TypeError):
# config must be a filename or Config object
init_recognizer(dict(config_file=None))
if torch.cuda.is_available():
device = 'cuda:0'
else:
device = 'cpu'
model = init_recognizer(video_config_file, None, device)
config = mmcv.Config.fromfile(video_config_file)
config.model.backbone.pretrained = None
isinstance(model, nn.Module)
if torch.cuda.is_available():
assert next(model.parameters()).is_cuda is True
else:
assert next(model.parameters()).is_cuda is False
assert model.cfg.model.backbone.pretrained is None
def test_video_inference_recognizer():
if torch.cuda.is_available():
device = 'cuda:0'
else:
device = 'cpu'
model = init_recognizer(video_config_file, None, device)
with pytest.raises(RuntimeError):
# video path doesn't exist
inference_recognizer(model, 'missing.mp4')
for ops in model.cfg.data.test.pipeline:
if ops['type'] in ('TenCrop', 'ThreeCrop'):
# Use CenterCrop to reduce memory in order to pass CI
ops['type'] = 'CenterCrop'
top5_label = inference_recognizer(model, video_path)
scores = [item[1] for item in top5_label]
assert len(top5_label) == 5
assert scores == sorted(scores, reverse=True)
_, feat = inference_recognizer(
model, video_path, outputs=('backbone', 'cls_head'), as_tensor=False)
assert isinstance(feat, dict)
assert 'backbone' in feat and 'cls_head' in feat
assert isinstance(feat['backbone'], np.ndarray)
assert isinstance(feat['cls_head'], np.ndarray)
assert feat['backbone'].shape == (25, 2048, 7, 7)
assert feat['cls_head'].shape == (1, 400)
_, feat = inference_recognizer(
model,
video_path,
outputs=('backbone.layer3', 'backbone.layer3.1.conv1'))
assert 'backbone.layer3.1.conv1' in feat and 'backbone.layer3' in feat
assert isinstance(feat['backbone.layer3.1.conv1'], torch.Tensor)
assert isinstance(feat['backbone.layer3'], torch.Tensor)
assert feat['backbone.layer3'].size() == (25, 1024, 14, 14)
assert feat['backbone.layer3.1.conv1'].size() == (25, 256, 14, 14)
cfg_file = 'configs/recognition/slowfast/slowfast_r50_video_inference_4x16x1_256e_kinetics400_rgb.py' # noqa: E501
sf_model = init_recognizer(cfg_file, None, device)
for ops in sf_model.cfg.data.test.pipeline:
# Changes to reduce memory in order to pass CI
if ops['type'] in ('TenCrop', 'ThreeCrop'):
ops['type'] = 'CenterCrop'
if ops['type'] == 'SampleFrames':
ops['num_clips'] = 1
_, feat = inference_recognizer(
sf_model, video_path, outputs=('backbone', 'cls_head'))
assert isinstance(feat, dict) and isinstance(feat['backbone'], tuple)
assert 'backbone' in feat and 'cls_head' in feat
assert len(feat['backbone']) == 2
assert isinstance(feat['backbone'][0], torch.Tensor)
assert isinstance(feat['backbone'][1], torch.Tensor)
assert feat['backbone'][0].size() == (1, 2048, 4, 8, 8)
assert feat['backbone'][1].size() == (1, 256, 32, 8, 8)
assert feat['cls_head'].size() == (1, 400)
def test_frames_inference_recognizer():
if torch.cuda.is_available():
device = 'cuda:0'
else:
device = 'cpu'
rgb_model = init_recognizer(frame_config_file, None, device)
flow_model = init_recognizer(flow_frame_config_file, None, device)
with pytest.raises(RuntimeError):
# video path doesn't exist
inference_recognizer(rgb_model, 'missing_path')
for ops in rgb_model.cfg.data.test.pipeline:
if ops['type'] in ('TenCrop', 'ThreeCrop'):
# Use CenterCrop to reduce memory in order to pass CI
ops['type'] = 'CenterCrop'
ops['crop_size'] = 224
for ops in flow_model.cfg.data.test.pipeline:
if ops['type'] in ('TenCrop', 'ThreeCrop'):
# Use CenterCrop to reduce memory in order to pass CI
ops['type'] = 'CenterCrop'
ops['crop_size'] = 224
top5_label = inference_recognizer(rgb_model, frames_path)
scores = [item[1] for item in top5_label]
assert len(top5_label) == 5
assert scores == sorted(scores, reverse=True)
_, feat = inference_recognizer(
flow_model,
frames_path,
outputs=('backbone', 'cls_head'),
as_tensor=False)
assert isinstance(feat, dict)
assert 'backbone' in feat and 'cls_head' in feat
assert isinstance(feat['backbone'], np.ndarray)
assert isinstance(feat['cls_head'], np.ndarray)
assert feat['backbone'].shape == (25, 2048, 7, 7)
assert feat['cls_head'].shape == (1, 400)
_, feat = inference_recognizer(
rgb_model,
frames_path,
outputs=('backbone.layer3', 'backbone.layer3.1.conv1'))
assert 'backbone.layer3.1.conv1' in feat and 'backbone.layer3' in feat
assert isinstance(feat['backbone.layer3.1.conv1'], torch.Tensor)
assert isinstance(feat['backbone.layer3'], torch.Tensor)
assert feat['backbone.layer3'].size() == (25, 1024, 14, 14)
assert feat['backbone.layer3.1.conv1'].size() == (25, 256, 14, 14)