199 lines
5.6 KiB
Python
199 lines
5.6 KiB
Python
from collections.abc import Sequence
|
|
|
|
import mmcv
|
|
import numpy as np
|
|
import torch
|
|
from mmcv.parallel import DataContainer as DC
|
|
from PIL import Image
|
|
|
|
from ..builder import PIPELINES
|
|
|
|
|
|
def to_tensor(data):
|
|
"""Convert objects of various python types to :obj:`torch.Tensor`.
|
|
|
|
Supported types are: :class:`numpy.ndarray`, :class:`torch.Tensor`,
|
|
:class:`Sequence`, :class:`int` and :class:`float`.
|
|
"""
|
|
if isinstance(data, torch.Tensor):
|
|
return data
|
|
elif isinstance(data, np.ndarray):
|
|
return torch.from_numpy(data)
|
|
elif isinstance(data, Sequence) and not mmcv.is_str(data):
|
|
return torch.tensor(data)
|
|
elif isinstance(data, int):
|
|
return torch.LongTensor([data])
|
|
elif isinstance(data, float):
|
|
return torch.FloatTensor([data])
|
|
else:
|
|
raise TypeError(
|
|
f'Type {type(data)} cannot be converted to tensor.'
|
|
'Supported types are: `numpy.ndarray`, `torch.Tensor`, '
|
|
'`Sequence`, `int` and `float`')
|
|
|
|
|
|
@PIPELINES.register_module()
|
|
class ToTensor(object):
|
|
|
|
def __init__(self, keys):
|
|
self.keys = keys
|
|
|
|
def __call__(self, results):
|
|
for key in self.keys:
|
|
results[key] = to_tensor(results[key])
|
|
return results
|
|
|
|
def __repr__(self):
|
|
return self.__class__.__name__ + f'(keys={self.keys})'
|
|
|
|
|
|
@PIPELINES.register_module()
|
|
class ImageToTensor(object):
|
|
|
|
def __init__(self, keys):
|
|
self.keys = keys
|
|
|
|
def __call__(self, results):
|
|
for key in self.keys:
|
|
img = results[key]
|
|
if len(img.shape) < 3:
|
|
img = np.expand_dims(img, -1)
|
|
results[key] = to_tensor(img.transpose(2, 0, 1))
|
|
return results
|
|
|
|
def __repr__(self):
|
|
return self.__class__.__name__ + f'(keys={self.keys})'
|
|
|
|
|
|
@PIPELINES.register_module()
|
|
class Transpose(object):
|
|
|
|
def __init__(self, keys, order):
|
|
self.keys = keys
|
|
self.order = order
|
|
|
|
def __call__(self, results):
|
|
for key in self.keys:
|
|
results[key] = results[key].transpose(self.order)
|
|
return results
|
|
|
|
def __repr__(self):
|
|
return self.__class__.__name__ + \
|
|
f'(keys={self.keys}, order={self.order})'
|
|
|
|
|
|
@PIPELINES.register_module()
|
|
class ToDataContainer(object):
|
|
|
|
def __init__(self,
|
|
fields=(dict(key='img', stack=True), dict(key='gt_labels'))):
|
|
self.fields = fields
|
|
|
|
def __call__(self, results):
|
|
for field in self.fields:
|
|
field = field.copy()
|
|
key = field.pop('key')
|
|
results[key] = DC(results[key], **field)
|
|
return results
|
|
|
|
def __repr__(self):
|
|
return self.__class__.__name__ + f'(fields={self.fields})'
|
|
|
|
|
|
@PIPELINES.register_module()
|
|
class ToPIL(object):
|
|
|
|
def __init__(self):
|
|
pass
|
|
|
|
def __call__(self, results):
|
|
results['img'] = Image.fromarray(results['img'])
|
|
return results
|
|
|
|
|
|
@PIPELINES.register_module()
|
|
class ToNumpy(object):
|
|
|
|
def __init__(self):
|
|
pass
|
|
|
|
def __call__(self, results):
|
|
results['img'] = np.array(results['img'], dtype=np.float32)
|
|
# results['img'] = np.array(results['img'])
|
|
return results
|
|
|
|
|
|
@PIPELINES.register_module()
|
|
class Collect(object):
|
|
"""
|
|
Collect data from the loader relevant to the specific task.
|
|
|
|
This is usually the last stage of the data loader pipeline. Typically keys
|
|
is set to some subset of "img" and "gt_labels".
|
|
|
|
The "img_meta" item is always populated. The contents of the "img_meta"
|
|
dictionary depends on "meta_keys". By default this includes:
|
|
|
|
- "img_shape": shape of the image input to the network as a tuple
|
|
(h, w, c). Note that images may be zero padded on the bottom/right
|
|
if the batch tensor is larger than this shape.
|
|
|
|
- "filename": path to the image file
|
|
|
|
- "ori_shape": original shape of the image as a tuple (h, w, c)
|
|
|
|
- "img_norm_cfg": a dict of normalization information:
|
|
- mean - per channel mean subtraction
|
|
- std - per channel std divisor
|
|
- to_rgb - bool indicating if bgr was converted to rgb
|
|
"""
|
|
|
|
def __init__(self, keys, meta_keys=('filename', 'ori_shape', 'img_shape')):
|
|
self.keys = keys
|
|
self.meta_keys = meta_keys
|
|
|
|
def __call__(self, results):
|
|
data = {}
|
|
img_meta = {}
|
|
for key in self.meta_keys:
|
|
img_meta[key] = results[key]
|
|
# data['img_metas'] = DC(img_meta, cpu_only=True)
|
|
# data['img_metas'] = img_meta
|
|
for key in self.keys:
|
|
data[key] = results[key]
|
|
return data
|
|
|
|
def __repr__(self):
|
|
return self.__class__.__name__ + \
|
|
f'(keys={self.keys}, meta_keys={self.meta_keys})'
|
|
|
|
|
|
@PIPELINES.register_module()
|
|
class WrapFieldsToLists(object):
|
|
"""Wrap fields of the data dictionary into lists for evaluation.
|
|
|
|
This class can be used as a last step of a test or validation
|
|
pipeline for single image evaluation or inference.
|
|
|
|
Example:
|
|
>>> test_pipeline = [
|
|
>>> dict(type='LoadImageFromFile'),
|
|
>>> dict(type='Normalize',
|
|
mean=[123.675, 116.28, 103.53],
|
|
std=[58.395, 57.12, 57.375],
|
|
to_rgb=True),
|
|
>>> dict(type='ImageToTensor', keys=['img']),
|
|
>>> dict(type='Collect', keys=['img']),
|
|
>>> dict(type='WrapIntoLists')
|
|
>>> ]
|
|
"""
|
|
|
|
def __call__(self, results):
|
|
# Wrap dict fields into lists
|
|
for key, val in results.items():
|
|
results[key] = [val]
|
|
return results
|
|
|
|
def __repr__(self):
|
|
return f'{self.__class__.__name__}()'
|