forked from open-mmlab/mmpretrain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Docs] Add code-spell pre-commit hook and fix a large mount of typos. (…
…open-mmlab#470) * Add code spell check hook * Add codespell config * Fix a lot of typos. * Add formating.py to keep compatibility.
- Loading branch information
Showing
33 changed files
with
247 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,180 +1,9 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
from collections.abc import Sequence | ||
# flake8: noqa | ||
import warnings | ||
|
||
import mmcv | ||
import numpy as np | ||
import torch | ||
from mmcv.parallel import DataContainer as DC | ||
from PIL import Image | ||
from .formatting import * | ||
|
||
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 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) | ||
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_label". | ||
Args: | ||
keys (Sequence[str]): Keys of results to be collected in ``data``. | ||
meta_keys (Sequence[str], optional): Meta keys to be converted to | ||
``mmcv.DataContainer`` and collected in ``data[img_metas]``. | ||
Default: ('filename', 'ori_shape', 'img_shape', 'flip', | ||
'flip_direction', 'img_norm_cfg') | ||
Returns: | ||
dict: The result dict contains the following keys | ||
- keys in ``self.keys`` | ||
- ``img_metas`` if avaliable | ||
""" | ||
|
||
def __init__(self, | ||
keys, | ||
meta_keys=('filename', 'ori_filename', 'ori_shape', | ||
'img_shape', 'flip', 'flip_direction', | ||
'img_norm_cfg')): | ||
self.keys = keys | ||
self.meta_keys = meta_keys | ||
|
||
def __call__(self, results): | ||
data = {} | ||
img_meta = {} | ||
for key in self.meta_keys: | ||
if key in results: | ||
img_meta[key] = results[key] | ||
data['img_metas'] = DC(img_meta, cpu_only=True) | ||
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__}()' | ||
warnings.warn('DeprecationWarning: mmcls.datasets.pipelines.formating will be ' | ||
'deprecated in 2021, please replace it with ' | ||
'mmcls.datasets.pipelines.formatting.') |
Oops, something went wrong.