Skip to content

Commit

Permalink
[Docs] Add code-spell pre-commit hook and fix a large mount of typos. (
Browse files Browse the repository at this point in the history
…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
mzr1996 authored Oct 13, 2021
1 parent 7f5bfef commit 75fc2c4
Show file tree
Hide file tree
Showing 33 changed files with 247 additions and 231 deletions.
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ repos:
- id: markdownlint
args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034",
"-t", "allow_different_nesting"]
- repo: https://github.com/codespell-project/codespell
rev: v2.1.0
hooks:
- id: codespell
- repo: https://github.com/myint/docformatter
rev: v1.3.1
hooks:
Expand Down
4 changes: 2 additions & 2 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

- Add `hparams` argument in `AutoAugment` and `RandAugment` and some other improvement. ([#398](https://github.com/open-mmlab/mmclassification/pull/398))
- Support classwise weight in losses. ([#388](https://github.com/open-mmlab/mmclassification/pull/388))
- Enhence `SELayer` to support custom squeeze channels. ([#417](https://github.com/open-mmlab/mmclassification/pull/417))
- Enhance `SELayer` to support custom squeeze channels. ([#417](https://github.com/open-mmlab/mmclassification/pull/417))

### Code Refactor

Expand Down Expand Up @@ -118,7 +118,7 @@
- Improve test tools and add some new tools. (#322)
- Correct MobilenetV3 backbone structure and add pretained models. (#291)
- Refactor `PatchEmbed` and `HybridEmbed` as independent components. (#330)
- Refactor mixup and cutmix as `Augments` to support more funtions. (#278)
- Refactor mixup and cutmix as `Augments` to support more functions. (#278)
- Refactor weights initialization method. (#270, #318, #319)
- Refactor `LabelSmoothLoss` to support multiple calculation formulas. (#285)

Expand Down
2 changes: 1 addition & 1 deletion docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ PyTorch 1.3.1., you need to install the prebuilt PyTorch with CUDA 9.2.
conda install pytorch=1.3.1 cudatoolkit=9.2 torchvision=0.4.2 -c pytorch
```

If you build PyTorch from source instead of installing the prebuilt pacakge,
If you build PyTorch from source instead of installing the prebuilt package,
you can use more CUDA versions such as 9.0.

c. Install MMClassification repository.
Expand Down
2 changes: 1 addition & 1 deletion docs/tools/model_serving.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ curl -O https://raw.githubusercontent.com/pytorch/serve/master/docs/images/3dogs
curl http://127.0.0.1:8080/predictions/${MODEL_NAME} -T 3dogs.jpg
```

You should obtain a respose similar to:
You should obtain a response similar to:

```json
{
Expand Down
6 changes: 3 additions & 3 deletions docs/tools/onnx2tensorrt.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- [How to convert models from ONNX to TensorRT](#how-to-convert-models-from-onnx-to-tensorrt)
- [Prerequisite](#prerequisite)
- [Usage](#usage)
- [List of supported models convertable to TensorRT](#list-of-supported-models-convertable-to-tensorrt)
- [List of supported models convertible to TensorRT](#list-of-supported-models-convertible-to-tensorrt)
- [Reminders](#reminders)
- [FAQs](#faqs)

Expand Down Expand Up @@ -55,9 +55,9 @@ python tools/deployment/onnx2tensorrt.py \
--verify \
```

## List of supported models convertable to TensorRT
## List of supported models convertible to TensorRT

The table below lists the models that are guaranteed to be convertable to TensorRT.
The table below lists the models that are guaranteed to be convertible to TensorRT.

| Model | Config | Status |
| :----------: | :--------------------------------------------------------------------------: | :----: |
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/new_dataset.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Note: The value of ground-truth labels should fall in range `[0, num_classes - 1

You can write a new Dataset class inherited from `BaseDataset`, and overwrite `load_annotations(self)`,
like [CIFAR10](https://github.com/open-mmlab/mmclassification/blob/master/mmcls/datasets/cifar.py) and [ImageNet](https://github.com/open-mmlab/mmclassification/blob/master/mmcls/datasets/imagenet.py).
Typically, this function returns a list, where each sample is a dict, containing necessary data informations, e.g., `img` and `gt_label`.
Typically, this function returns a list, where each sample is a dict, containing necessary data information, e.g., `img` and `gt_label`.

Assume we are going to implement a `Filelist` dataset, which takes filelists for both training and testing. The format of annotation list is as follows:

Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/new_modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ model = dict(

To add a new loss function, we mainly implement the `forward` function in the loss module.
In addition, it is helpful to leverage the decorator `weighted_loss` to weight the loss for each element.
Assuming that we want to mimic a probablistic distribution generated from another classification model, we implement a L1Loss to fulfil the purpose as below.
Assuming that we want to mimic a probabilistic distribution generated from another classification model, we implement a L1Loss to fulfil the purpose as below.

1. Create a new file in `mmcls/models/losses/l1_loss.py`.

Expand Down
4 changes: 2 additions & 2 deletions mmcls/core/visualization/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def put_img_infos(self,
show=True,
wait_time=0,
out_file=None):
"""Show image with extra infomation.
"""Show image with extra information.
Args:
img (str | ndarray): The image to be displayed.
Expand Down Expand Up @@ -293,7 +293,7 @@ def imshow_infos(img,
fig_size=(15, 10),
wait_time=0,
out_file=None):
"""Show image with extra infomation.
"""Show image with extra information.
Args:
img (str | ndarray): The image to be displayed.
Expand Down
4 changes: 2 additions & 2 deletions mmcls/datasets/pipelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Posterize, RandAugment, Rotate, Sharpness, Shear,
Solarize, SolarizeAdd, Translate)
from .compose import Compose
from .formating import (Collect, ImageToTensor, ToNumpy, ToPIL, ToTensor,
Transpose, to_tensor)
from .formatting import (Collect, ImageToTensor, ToNumpy, ToPIL, ToTensor,
Transpose, to_tensor)
from .loading import LoadImageFromFile
from .transforms import (CenterCrop, ColorJitter, Lighting, RandomCrop,
RandomErasing, RandomFlip, RandomGrayscale,
Expand Down
183 changes: 6 additions & 177 deletions mmcls/datasets/pipelines/formating.py
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.')
Loading

0 comments on commit 75fc2c4

Please sign in to comment.