Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into prepare-model-param
Browse files Browse the repository at this point in the history
  • Loading branch information
knorth55 committed May 29, 2019
2 parents c71d7c4 + 76e7201 commit 4e102f0
Show file tree
Hide file tree
Showing 19 changed files with 124 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,12 @@ def add_getter(self, keys, getter):
"""Register a getter function
Args:
keys (int or string or tuple of strings): The number or name(s) of
data that the getter function returns.
keys (string or tuple of strings): The name(s) of data
that the getter function returns.
getter (callable): A getter function that takes an index and
returns data of the corresponding example.
"""
self._getters.append(getter)
if isinstance(keys, int):
if keys == 1:
keys = None
else:
keys = (None,) * keys
if _is_iterable(keys):
for key_index, key in enumerate(keys):
self._keys.append((key, len(self._getters) - 1, key_index))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class TransformDataset(GetterDataset):
Args:
dataset: The underlying dataset.
This dataset should have :meth:`__len__` and :meth:`__getitem__`.
keys (int or string or tuple of strings): The number or name(s) of
data that the transform function returns.
keys (string or tuple of strings): The name(s) of data
that the transform function returns.
If this parameter is omitted, :meth:`__init__` fetches a sample
from the underlying dataset to determine the number of data.
transform (callable): A function that is called to transform values
Expand All @@ -38,12 +38,7 @@ def __init__(self, dataset, keys, transform=None):
self._dataset = dataset
self._transform = transform

if isinstance(keys, int):
if keys == 1:
keys = None
else:
keys = (None,) * keys
elif keys is None:
if keys is None:
sample = self._get(0)
if isinstance(sample, tuple):
keys = (None,) * len(sample)
Expand Down
29 changes: 16 additions & 13 deletions chainercv/visualizations/vis_bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,11 @@ def vis_bbox(img, bbox, label=None, score=None, label_names=None,
>>> plt.show()
Args:
img (~numpy.ndarray): An array of shape :math:`(3, height, width)`.
This is in RGB format and the range of its value is
:math:`[0, 255]`. If this is :obj:`None`, no image is displayed.
bbox (~numpy.ndarray): An array of shape :math:`(R, 4)`, where
:math:`R` is the number of bounding boxes in the image.
Each element is organized
by :math:`(y_{min}, x_{min}, y_{max}, x_{max})` in the second axis.
label (~numpy.ndarray): An integer array of shape :math:`(R,)`.
The values correspond to id for label names stored in
:obj:`label_names`. This is optional.
score (~numpy.ndarray): A float array of shape :math:`(R,)`.
Each value indicates how confident the prediction is.
This is optional.
img (~numpy.ndarray): See the table below. If this is :obj:`None`,
no image is displayed.
bbox (~numpy.ndarray): See the table below.
label (~numpy.ndarray): See the table below. This is optional.
score (~numpy.ndarray): See the table below. This is optional.
label_names (iterable of strings): Name of labels ordered according
to label ids. If this is :obj:`None`, labels will be skipped.
instance_colors (iterable of tuples): List of colors.
Expand All @@ -66,6 +58,17 @@ def vis_bbox(img, bbox, label=None, score=None, label_names=None,
ax (matplotlib.axes.Axis): The visualization is displayed on this
axis. If this is :obj:`None` (default), a new axis is created.
.. csv-table::
:header: name, shape, dtype, format
:obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \
"RGB, :math:`[0, 255]`"
:obj:`bbox`, ":math:`(R, 4)`", :obj:`float32`, \
":math:`(y_{min}, x_{min}, y_{max}, x_{max})`"
:obj:`label`, ":math:`(R,)`", :obj:`int32`, \
":math:`[0, \#fg\_class - 1]`"
:obj:`score`, ":math:`(R,)`", :obj:`float32`, --
Returns:
~matploblib.axes.Axes:
Returns the Axes object with the plot for further tweaking.
Expand Down
11 changes: 8 additions & 3 deletions chainercv/visualizations/vis_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ def vis_image(img, ax=None):
"""Visualize a color image.
Args:
img (~numpy.ndarray): An array of shape :math:`(3, height, width)`.
This is in RGB format and the range of its value is
:math:`[0, 255]`. If this is :obj:`None`, no image is displayed.
img (~numpy.ndarray): See the table below.
If this is :obj:`None`, no image is displayed.
ax (matplotlib.axes.Axis): The visualization is displayed on this
axis. If this is :obj:`None` (default), a new axis is created.
.. csv-table::
:header: name, shape, dtype, format
:obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \
"RGB, :math:`[0, 255]`"
Returns:
~matploblib.axes.Axes:
Returns the Axes object with the plot for further tweaking.
Expand Down
28 changes: 15 additions & 13 deletions chainercv/visualizations/vis_instance_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,11 @@ def vis_instance_segmentation(
>>> plt.show()
Args:
img (~numpy.ndarray): An array of shape :math:`(3, H, W)`.
This is in RGB format and the range of its value is
:math:`[0, 255]`. If this is :obj:`None`, no image is displayed.
mask (~numpy.ndarray): A bool array of shape
:math`(R, H, W)`.
If there is an object, the value of the pixel is :obj:`True`,
and otherwise, it is :obj:`False`.
label (~numpy.ndarray): An integer array of shape :math:`(R, )`.
The values correspond to id for label names stored in
:obj:`label_names`.
score (~numpy.ndarray): A float array of shape :math:`(R,)`.
Each value indicates how confident the prediction is.
This is optional.
img (~numpy.ndarray): See the table below. If this is :obj:`None`,
no image is displayed.
mask (~numpy.ndarray): See the table below.
label (~numpy.ndarray): See the table below. This is optional.
score (~numpy.ndarray): See the table below. This is optional.
label_names (iterable of strings): Name of labels ordered according
to label ids.
instance_colors (iterable of tuple): List of colors.
Expand All @@ -82,6 +74,16 @@ def vis_instance_segmentation(
ax (matplotlib.axes.Axis): The visualization is displayed on this
axis. If this is :obj:`None` (default), a new axis is created.
.. csv-table::
:header: name, shape, dtype, format
:obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \
"RGB, :math:`[0, 255]`"
:obj:`mask`, ":math:`(R, H, W)`", :obj:`bool`, --
:obj:`label`, ":math:`(R,)`", :obj:`int32`, \
":math:`[0, \#fg\_class - 1]`"
:obj:`score`, ":math:`(R,)`", :obj:`float32`, --
Returns:
matploblib.axes.Axes: Returns :obj:`ax`.
:obj:`ax` is an :class:`matploblib.axes.Axes` with the plot.
Expand Down
18 changes: 11 additions & 7 deletions chainercv/visualizations/vis_semantic_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,9 @@ def vis_semantic_segmentation(
>>> plt.show()
Args:
img (~numpy.ndarray): An array of shape :math:`(3, height, width)`.
This is in RGB format and the range of its value is
:math:`[0, 255]`. If this is :obj:`None`, no image is displayed.
label (~numpy.ndarray): An integer array of shape
:math:`(height, width)`.
The values correspond to id for label names stored in
:obj:`label_names`.
img (~numpy.ndarray): See the table below. If this is :obj:`None`,
no image is displayed.
label (~numpy.ndarray): See the table below.
label_names (iterable of strings): Name of labels ordered according
to label ids.
label_colors: (iterable of tuple): An iterable of colors for regular
Expand All @@ -63,6 +59,14 @@ def vis_semantic_segmentation(
ax (matplotlib.axes.Axis): The visualization is displayed on this
axis. If this is :obj:`None` (default), a new axis is created.
.. csv-table::
:header: name, shape, dtype, format
:obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \
"RGB, :math:`[0, 255]`"
:obj:`label`, ":math:`(H, W)`", :obj:`int32`, \
":math:`[-1, \#class - 1]`"
Returns:
matploblib.axes.Axes and list of matplotlib.patches.Patch:
Returns :obj:`ax` and :obj:`legend_handles`.
Expand Down
4 changes: 2 additions & 2 deletions examples/deeplab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ So we evaluated the pretrained graph using `eval_semantic_segmentation` in Chain
Although we could generate a graph the biggest image can be input, it resulted 40.13% in official evaluation code.

## Demo
This demo downloads Cityscapes pretrained model automatically if a pretrained model path is not given.
This demo downloads a pretrained model automatically if a pretrained model path is not given.
```
$ python demo.py [--gpu <gpu>] [--pretrained-model <model_path>] [--input-size <size>] <image>.jpg
$ python demo.py [--dataset cityscapes|ade20k|voc] [--gpu <gpu>] [--pretrained-model <model_path>] [--min-input-size <size>] <image>.jpg
```


Expand Down
12 changes: 7 additions & 5 deletions examples/deeplab/demo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import argparse

import copy
import matplotlib.pyplot as plt

import chainer
Expand All @@ -19,7 +19,8 @@
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=int, default=-1)
parser.add_argument('--pretrained-model', default=None)
parser.add_argument('--pretrained-model')
parser.add_argument('--min-input-size', type=int, default=None)
parser.add_argument(
'--dataset', choices=('cityscapes', 'ade20k', 'voc'),
default='cityscapes')
Expand All @@ -42,9 +43,10 @@ def main():
label_names = voc_semantic_segmentation_label_names
colors = voc_semantic_segmentation_label_colors

params = copy.deepcopy(DeepLabV3plusXception65.preset_params[args.dataset])
params['min_input_size'] = args.min_input_size
model = DeepLabV3plusXception65(
pretrained_model=args.pretrained_model,
**DeepLabV3plusXception65.preset_params[args.dataset])
pretrained_model=args.pretrained_model, **params)

if args.gpu >= 0:
chainer.cuda.get_device_from_id(args.gpu).use()
Expand All @@ -59,7 +61,7 @@ def main():
vis_image(img, ax=ax1)
ax2 = fig.add_subplot(1, 2, 2)
# Do not overlay the label image on the color image
vis_semantic_segmentation(
vis_semantic_segmentation(>>>>>>> master
None, label, label_names, colors, ax=ax2)
plt.show()

Expand Down
2 changes: 1 addition & 1 deletion examples/faster_rcnn/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def main():
parser.add_argument('--gpu', type=int, default=-1)
parser.add_argument('--pretrained-model')
parser.add_argument(
'--dataset', choices=('voc'), default='voc')
'--dataset', choices=('voc',), default='voc')
parser.add_argument('image')
args = parser.parse_args()

Expand Down
2 changes: 1 addition & 1 deletion examples/fcis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Segment objects in an given image. This demo downloads SBD pretrained model automatically if a pretrained model path is not given.

```bash
python demo.py [--gpu <gpu>] [--pretrained-model <model_path>] [--dataset <sbd, coco>] <image.jpg>
python demo.py [--dataset sbd|coco] [--gpu <gpu>] [--pretrained-model <model_path>] <image.jpg>
```

## Evaluation
Expand Down
2 changes: 1 addition & 1 deletion examples/fpn/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def main():
parser.add_argument('--gpu', type=int, default=-1)
parser.add_argument('--pretrained-model')
parser.add_argument(
'--dataset', choices=('coco'), default='coco')
'--dataset', choices=('coco',), default='coco')
parser.add_argument('image')
args = parser.parse_args()

Expand Down
2 changes: 1 addition & 1 deletion examples/pspnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Demo
This demo downloads a pretrained model automatically if a pretrained model path is not given.
```
$ python demo.py [--gpu <gpu>] [--pretrained-model <model_path>] [--input-size <size>] <image>.jpg
$ python demo.py [--dataset cityscapes|ade20k] [--gpu <gpu>] [--pretrained-model <model_path>] [--input-size <size>] <image>.jpg
```

## Weight Covnersion
Expand Down
10 changes: 7 additions & 3 deletions examples/pspnet/demo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import argparse

import copy
import matplotlib.pyplot as plt

import chainer

from chainercv.datasets import ade20k_semantic_segmentation_label_colors
from chainercv.datasets import ade20k_semantic_segmentation_label_names
from chainercv.datasets import cityscapes_semantic_segmentation_label_colors
Expand All @@ -17,6 +18,7 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', '-g', type=int, default=-1)
parser.add_argument('--pretrained-model')
parser.add_argument('--input-size', type=int, default=713)
parser.add_argument(
'--dataset', choices=('cityscapes', 'ade20k'), default='cityscapes')
parser.add_argument('image')
Expand All @@ -33,9 +35,11 @@ def main():
label_names = ade20k_semantic_segmentation_label_names
colors = ade20k_semantic_segmentation_label_colors

input_size = (args.input_size, args.input_size)
params = copy.deepcopy(PSPNetResNet101.preset_params[args.dataset])
params['input_size'] = input_size
model = PSPNetResNet101(
pretrained_model=args.pretrained_model,
**PSPNetResNet101.preset_params[args.dataset])
pretrained_model=args.pretrained_model, **params)

if args.gpu >= 0:
chainer.cuda.get_device_from_id(args.gpu).use()
Expand Down
2 changes: 1 addition & 1 deletion examples/segnet/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=int, default=-1)
parser.add_argument('--pretrained-model')
parser.add_argument('--dataset', choices=('camvid'), default='camvid')
parser.add_argument('--dataset', choices=('camvid',), default='camvid')
parser.add_argument('image')
args = parser.parse_args()

Expand Down
2 changes: 1 addition & 1 deletion examples/ssd/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def main():
parser.add_argument('--gpu', type=int, default=-1)
parser.add_argument('--pretrained-model')
parser.add_argument(
'--dataset', choices=('voc'), default='voc')
'--dataset', choices=('voc',), default='voc')
parser.add_argument('image')
args = parser.parse_args()

Expand Down
8 changes: 4 additions & 4 deletions examples/yolo/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ def main():
parser.add_argument('--gpu', type=int, default=-1)
parser.add_argument('--pretrained-model')
parser.add_argument(
'--dataset', choices=('voc'), default='voc')
'--dataset', choices=('voc',), default='voc')
parser.add_argument('image')
args = parser.parse_args()

if args.model == 'yolo_v2':
cls = YOLOv2
elif args.model == 'yolo_v2_tiny':
model = YOLOv2Tiny
cls = YOLOv2Tiny
elif args.model == 'yolo_v3':
model = YOLOv3
cls = YOLOv3

if args.dataset == 'voc':
if args.pretrained_model is None:
args.pretrained_model = 'voc0712'
label_names = voc_bbox_label_names
label_names = voc_bbox_label_name

model = cls(pretrained_model=args.pretrained_model,
**cls.preset_params[args.dataset])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, iterable=tuple):

self.add_getter('item0', self.get_item0)
self.add_getter(iterable(('item1', 'item2')), self.get_item1_item2)
self.add_getter(1, self.get_item3)
self.add_getter(('item3',), self.get_item3)

self.count = 0

Expand All @@ -28,7 +28,7 @@ def get_item1_item2(self, i):

def get_item3(self, i):
self.count += 1
return 'item3({:d})'.format(i)
return ('item3({:d})'.format(i),)


@testing.parameterize(
Expand All @@ -43,7 +43,7 @@ def setUp(self):

def test_keys(self):
self.assertEqual(
self.dataset.keys, ('item0', 'item1', 'item2', None))
self.dataset.keys, ('item0', 'item1', 'item2', 'item3'))

def test_get_example_by_keys(self):
example = self.dataset.get_example_by_keys(1, (1, 2, 3))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,6 @@ def test_transform(self):
dataset = TransformDataset(self.dataset, keys, self.func)
self._check(dataset, self.keys)

def test_transform_with_n_keys(self):
if isinstance(self.keys, tuple):
n_keys = len(self.keys)
if n_keys == 1:
self.skipTest(
'tuple of single element is not supported '
'when the number of keys is specified')
expected_keys = (None,) * n_keys
else:
n_keys = 1
expected_keys = None
dataset = TransformDataset(self.dataset, n_keys, self.func)
self._check(dataset, expected_keys)

def test_transform_compat(self):
if isinstance(self.keys, tuple):
expected_keys = (None,) * len(self.keys)
Expand Down
Loading

0 comments on commit 4e102f0

Please sign in to comment.