Commit 982f42b9 authored by Yuxin Wu's avatar Yuxin Wu

Remove FloatBox and IntBox

parent 95cdb963
...@@ -364,6 +364,7 @@ _DEPRECATED_NAMES = set([ ...@@ -364,6 +364,7 @@ _DEPRECATED_NAMES = set([
'dump_dataflow_to_lmdb', 'dump_dataflow_to_lmdb',
'dump_dataflow_to_tfrecord', 'dump_dataflow_to_tfrecord',
'pyplot2img', 'pyplot2img',
'IntBox', 'FloatBox',
# renamed stuff: # renamed stuff:
'DumpTensor', 'DumpTensor',
......
...@@ -8,7 +8,6 @@ from tabulate import tabulate ...@@ -8,7 +8,6 @@ from tabulate import tabulate
import tqdm import tqdm
from tensorpack.utils import logger from tensorpack.utils import logger
from tensorpack.utils.rect import FloatBox
from tensorpack.utils.timer import timed_operation from tensorpack.utils.timer import timed_operation
from tensorpack.utils.argtools import log_once from tensorpack.utils.argtools import log_once
...@@ -130,13 +129,15 @@ class COCODetection(object): ...@@ -130,13 +129,15 @@ class COCODetection(object):
x1, y1, w, h = obj['bbox'] x1, y1, w, h = obj['bbox']
# bbox is originally in float # bbox is originally in float
# x1/y1 means upper-left corner and w/h means true w/h. This can be verified by segmentation pixels. # x1/y1 means upper-left corner and w/h means true w/h. This can be verified by segmentation pixels.
# But we do assume that (0.0, 0.0) is upper-left corner of the first pixel # But we do make an assumption here that (0.0, 0.0) is upper-left corner of the first pixel
box = FloatBox(float(x1), float(y1),
float(x1 + w), float(y1 + h)) x1 = np.clip(float(x1), 0, width)
box.clip_by_shape([height, width]) y1 = np.clip(float(y1), 0, height)
w = np.clip(float(x1 + w), 0, width) - x1
h = np.clip(float(y1 + h), 0, height) - y1
# Require non-zero seg area and more than 1x1 box size # Require non-zero seg area and more than 1x1 box size
if obj['area'] > 1 and box.is_box() and box.area() >= 4: if obj['area'] > 1 and w > 0 and h > 0 and w * h >= 4:
obj['bbox'] = [box.x1, box.y1, box.x2, box.y2] obj['bbox'] = [x1, y1, x1 + w, y1 + h]
valid_objs.append(obj) valid_objs.append(obj)
if add_mask: if add_mask:
......
...@@ -23,7 +23,7 @@ which is better than other TensorFlow implementations. ...@@ -23,7 +23,7 @@ which is better than other TensorFlow implementations.
### Usage ### Usage
1. Download the pre-trained model: 1. Download the pre-trained model (converted from caffe):
```bash ```bash
wget http://models.tensorpack.com/OpticalFlow/flownet2.npz wget http://models.tensorpack.com/OpticalFlow/flownet2.npz
...@@ -36,9 +36,9 @@ wget http://models.tensorpack.com/OpticalFlow/flownet2-c.npz ...@@ -36,9 +36,9 @@ wget http://models.tensorpack.com/OpticalFlow/flownet2-c.npz
2. Run inference 2. Run inference
```bash ```bash
python flownet2.py python flownet2.py \
--left left.png --right right.png \ --left left.png --right right.png \
--load flownet2.npz --model flownet2 --load flownet2.npz --model flownet2
``` ```
3. Evaluate AEE (Average Endpoing Error) on Sintel dataset: 3. Evaluate AEE (Average Endpoing Error) on Sintel dataset:
......
...@@ -113,20 +113,20 @@ class ImageAugmentor(Augmentor): ...@@ -113,20 +113,20 @@ class ImageAugmentor(Augmentor):
floating point images in range [0, 1] or [0, 255]. floating point images in range [0, 1] or [0, 255].
""" """
def augment_coords(self, coords, param): def augment_coords(self, coords, param):
return self._augment_coords(coords, param)
def _augment_coords(self, coords, param):
""" """
Augment the coordinates given the param. Augment the coordinates given the param.
By default, keeps coordinates unchanged. By default, an augmentor keeps coordinates unchanged.
If a subclass changes coordinates but couldn't implement this method, If a subclass changes coordinates but couldn't implement this method,
it should ``raise NotImplementedError()``. it should ``raise NotImplementedError()``.
Args: Args:
coords: Nx2 floating point nparray where each row is (x, y) coords: Nx2 floating point numpy array where each row is (x, y)
Returns: Returns:
new coords new coords
""" """
return self._augment_coords(coords, param)
def _augment_coords(self, coords, param):
return coords return coords
......
...@@ -8,7 +8,6 @@ import sys ...@@ -8,7 +8,6 @@ import sys
import io import io
from .fs import mkdir_p from .fs import mkdir_p
from .argtools import shape2d from .argtools import shape2d
from .rect import BoxBase, IntBox
from .palette import PALETTE_RGB from .palette import PALETTE_RGB
try: try:
...@@ -367,8 +366,7 @@ def draw_boxes(im, boxes, labels=None, color=None): ...@@ -367,8 +366,7 @@ def draw_boxes(im, boxes, labels=None, color=None):
""" """
Args: Args:
im (np.ndarray): a BGR image in range [0,255]. It will not be modified. im (np.ndarray): a BGR image in range [0,255]. It will not be modified.
boxes (np.ndarray or list[BoxBase]): If an ndarray, boxes (np.ndarray): a numpy array of shape Nx4 where each row is [x1, y1, x2, y2].
must be of shape Nx4 where the second dimension is [x1, y1, x2, y2].
labels: (list[str] or None) labels: (list[str] or None)
color: a 3-tuple (in range [0, 255]). By default will choose automatically. color: a 3-tuple (in range [0, 255]). By default will choose automatically.
...@@ -377,14 +375,7 @@ def draw_boxes(im, boxes, labels=None, color=None): ...@@ -377,14 +375,7 @@ def draw_boxes(im, boxes, labels=None, color=None):
""" """
FONT = cv2.FONT_HERSHEY_SIMPLEX FONT = cv2.FONT_HERSHEY_SIMPLEX
FONT_SCALE = 0.4 FONT_SCALE = 0.4
if isinstance(boxes, list): boxes = np.asarray(boxes, dtype='int32')
arr = np.zeros((len(boxes), 4), dtype='int32')
for idx, b in enumerate(boxes):
assert isinstance(b, BoxBase), b
arr[idx, :] = [int(b.x1), int(b.y1), int(b.x2), int(b.y2)]
boxes = arr
else:
boxes = boxes.astype('int32')
if labels is not None: if labels is not None:
assert len(labels) == len(boxes), "{} != {}".format(len(labels), len(boxes)) assert len(labels) == len(boxes), "{} != {}".format(len(labels), len(boxes))
areas = (boxes[:, 2] - boxes[:, 0] + 1) * (boxes[:, 3] - boxes[:, 1] + 1) areas = (boxes[:, 2] - boxes[:, 0] + 1) * (boxes[:, 3] - boxes[:, 1] + 1)
...@@ -415,17 +406,18 @@ def draw_boxes(im, boxes, labels=None, color=None): ...@@ -415,17 +406,18 @@ def draw_boxes(im, boxes, labels=None, color=None):
if top_left[1] < 0: # out of image if top_left[1] < 0: # out of image
top_left[1] = box[3] - 1.3 * lineh top_left[1] = box[3] - 1.3 * lineh
bottom_left[1] = box[3] - 0.3 * lineh bottom_left[1] = box[3] - 0.3 * lineh
textbox = IntBox(int(top_left[0]), int(top_left[1]), x1, y1 = int(top_left[0]), int(top_left[1])
int(top_left[0] + linew), int(top_left[1] + lineh)) x2, y2 = int(top_left[0] + linew), int(top_left[1] + lineh)
textbox.clip_by_shape(im.shape[:2]) x1, x2 = [np.clip(x, 0, img.shape[1] - 1) for x in [x1, x2]]
y1, y2 = [np.clip(y, 0, img.shape[0] - 1) for y in [y1, y2]]
if color is None: if color is None:
# find the best color # find the best color
mean_color = textbox.roi(im).mean(axis=(0, 1)) mean_color = im[y1:y2 + 1, x1:x2 + 1].mean(axis=(0, 1))
best_color_ind = (np.square(COLOR_CANDIDATES - mean_color) * best_color_ind = (np.square(COLOR_CANDIDATES - mean_color) *
COLOR_DIFF_WEIGHT).sum(axis=1).argmax() COLOR_DIFF_WEIGHT).sum(axis=1).argmax()
best_color = COLOR_CANDIDATES[best_color_ind].tolist() best_color = COLOR_CANDIDATES[best_color_ind].tolist()
cv2.putText(im, label, (textbox.x1, textbox.y2), cv2.putText(im, label, (x1, y2),
FONT, FONT_SCALE, color=best_color, lineType=cv2.LINE_AA) FONT, FONT_SCALE, color=best_color, lineType=cv2.LINE_AA)
cv2.rectangle(im, (box[0], box[1]), (box[2], box[3]), cv2.rectangle(im, (box[0], box[1]), (box[2], box[3]),
color=best_color, thickness=1) color=best_color, thickness=1)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment