Skip to content

Commit 1ee80e9

Browse files
committed
fix size_index bug (issue longcw#72)
1 parent dfdd822 commit 1ee80e9

File tree

6 files changed

+65
-50
lines changed

6 files changed

+65
-50
lines changed

README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,10 @@ and set the path in `yolo2-pytorch/cfgs/exps/darknet19_exp1.py`.
8181

8282
7. (optional) Training with TensorBoard.
8383

84-
To use the TensorBoard, install Crayon (https://github.com/torrvision/crayon)
85-
How to use the crayon
86-
```
87-
docker pull alband/crayon
88-
docker run -d -p 8888:8888 -p 8889:8889 --name crayon alband/crayon
89-
```
90-
91-
and set `use_tensorboard = True` in `yolo2-pytorch/cfgs/config.py`.
84+
To use the TensorBoard,
85+
set `use_tensorboard = True` in `yolo2-pytorch/cfgs/config.py`
86+
and install TensorboardX (https://github.com/lanpa/tensorboard-pytorch).
87+
Tensorboard log will be saved in `training/runs`.
9288

9389

9490
6. Run the training program: `python train.py`.

datasets/imdb.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ def mkdir(path, max_depth=3):
1515
os.mkdir(path)
1616

1717

18-
def image_resize(im, size_index):
19-
w, h = cfg.multi_scale_inp_size[size_index]
20-
im = cv2.resize(im, (w, h))
21-
return im
22-
23-
2418
class ImageDataset(object):
2519
def __init__(self, name, datadir, batch_size, im_processor,
2620
processes=3, shuffle=True, dst_size=None):
@@ -51,22 +45,42 @@ def next_batch(self, size_index):
5145
batch = {'images': [], 'gt_boxes': [], 'gt_classes': [],
5246
'dontcare': [], 'origin_im': []}
5347
i = 0
48+
if self.gen is None:
49+
indexes = np.arange(len(self.image_names), dtype=np.int)
50+
if self._shuffle:
51+
np.random.shuffle(indexes)
52+
self.gen = self.pool.imap(partial(self._im_processor,
53+
size_index=None),
54+
([self.image_names[i],
55+
self.get_annotation(i),
56+
self.dst_size] for i in indexes),
57+
chunksize=self.batch_size)
58+
self._epoch += 1
59+
print(('epoch {} start...'.format(self._epoch)))
60+
5461
while i < self.batch_size:
5562
try:
5663
images, gt_boxes, classes, dontcare, origin_im = next(self.gen)
57-
images = image_resize(images, size_index)
64+
65+
# multi-scale
66+
w, h = cfg.multi_scale_inp_size[size_index]
67+
gt_boxes = gt_boxes.astype(float)
68+
gt_boxes[:, 0::2] *= float(w) / images.shape[1]
69+
gt_boxes[:, 1::2] *= float(h) / images.shape[0]
70+
images = cv2.resize(images, (w, h))
71+
5872
batch['images'].append(images)
5973
batch['gt_boxes'].append(gt_boxes)
6074
batch['gt_classes'].append(classes)
6175
batch['dontcare'].append(dontcare)
6276
batch['origin_im'].append(origin_im)
6377
i += 1
64-
except (StopIteration, AttributeError, TypeError):
78+
except (StopIteration,):
6579
indexes = np.arange(len(self.image_names), dtype=np.int)
6680
if self._shuffle:
6781
np.random.shuffle(indexes)
6882
self.gen = self.pool.imap(partial(self._im_processor,
69-
size_index=size_index),
83+
size_index=None),
7084
([self.image_names[i],
7185
self.get_annotation(i),
7286
self.dst_size] for i in indexes),

demo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
from utils.timer import Timer
1010
import cfgs.config as cfg
1111

12+
# This prevents deadlocks in the data loader, caused by
13+
# some incompatibility between pytorch and cv2 multiprocessing.
14+
# See https://github.com/pytorch/pytorch/issues/1355.
15+
cv2.setNumThreads(0)
16+
1217

1318
def preprocess(fname):
1419
# return fname

test.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
import cfgs.config as cfg
1313

1414

15-
def preprocess(fname):
16-
# return fname
17-
image = cv2.imread(fname)
18-
im_data = np.expand_dims(yolo_utils.preprocess_test(image, cfg.multi_scale_inp_size), 0) # noqa
19-
return image, im_data
20-
21-
2215
parser = argparse.ArgumentParser(description='PyTorch Yolo')
2316
parser.add_argument('--image_size_index', type=int, default=0,
2417
metavar='image_size_index',

train.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
from random import randint
1313

1414
try:
15-
from pycrayon import CrayonClient
15+
from tensorboardX import SummaryWriter
1616
except ImportError:
17-
CrayonClient = None
17+
SummaryWriter = None
1818

1919

2020
# data loader
@@ -42,22 +42,12 @@
4242
weight_decay=cfg.weight_decay)
4343

4444
# tensorboad
45-
use_tensorboard = cfg.use_tensorboard and CrayonClient is not None
45+
use_tensorboard = cfg.use_tensorboard and SummaryWriter is not None
4646
# use_tensorboard = False
47-
remove_all_log = False
4847
if use_tensorboard:
49-
cc = CrayonClient(hostname='127.0.0.1')
50-
if remove_all_log:
51-
print('remove all experiments')
52-
cc.remove_all_experiments()
53-
if start_epoch == 0:
54-
try:
55-
cc.remove_experiment(cfg.exp_name)
56-
except ValueError:
57-
pass
58-
exp = cc.create_experiment(cfg.exp_name)
59-
else:
60-
exp = cc.open_experiment(cfg.exp_name)
48+
summary_writer = SummaryWriter(os.path.join(cfg.TRAIN_DIR, 'runs', cfg.exp_name))
49+
else:
50+
summary_writer = None
6151

6252
batch_per_epoch = imdb.batch_per_epoch
6353
train_loss = 0
@@ -81,7 +71,7 @@
8171
im_data = net_utils.np_to_variable(im,
8272
is_cuda=True,
8373
volatile=False).permute(0, 3, 1, 2)
84-
net(im_data, gt_boxes, gt_classes, dontcare, size_index)
74+
bbox_pred, iou_pred, prob_pred = net(im_data, gt_boxes, gt_classes, dontcare, size_index)
8575

8676
# backward
8777
loss = net.loss
@@ -106,12 +96,22 @@
10696
iou_loss, cls_loss, duration,
10797
str(datetime.timedelta(seconds=int((batch_per_epoch - step_cnt) * duration)))))) # noqa
10898

109-
if use_tensorboard and step % cfg.log_interval == 0:
110-
exp.add_scalar_value('loss_train', train_loss, step=step)
111-
exp.add_scalar_value('loss_bbox', bbox_loss, step=step)
112-
exp.add_scalar_value('loss_iou', iou_loss, step=step)
113-
exp.add_scalar_value('loss_cls', cls_loss, step=step)
114-
exp.add_scalar_value('learning_rate', lr, step=step)
99+
if summary_writer and step % cfg.log_interval == 0:
100+
summary_writer.add_scalar('loss_train', train_loss, step)
101+
summary_writer.add_scalar('loss_bbox', bbox_loss, step)
102+
summary_writer.add_scalar('loss_iou', iou_loss, step)
103+
summary_writer.add_scalar('loss_cls', cls_loss, step)
104+
summary_writer.add_scalar('learning_rate', lr, step)
105+
106+
# plot results
107+
bbox_pred = bbox_pred.data[0:1].cpu().numpy()
108+
iou_pred = iou_pred.data[0:1].cpu().numpy()
109+
prob_pred = prob_pred.data[0:1].cpu().numpy()
110+
image = im[0]
111+
bboxes, scores, cls_inds = yolo_utils.postprocess(
112+
bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh=0.3, size_index=size_index)
113+
im2show = yolo_utils.draw_detection(image, bboxes, scores, cls_inds, cfg)
114+
summary_writer.add_image('predict', im2show, step)
115115

116116
train_loss = 0
117117
bbox_loss, iou_loss, cls_loss = 0., 0., 0.

utils/yolo.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
from utils.cython_yolo import yolo_to_bbox
77

88

9+
# This prevents deadlocks in the data loader, caused by
10+
# some incompatibility between pytorch and cv2 multiprocessing.
11+
# See https://github.com/pytorch/pytorch/issues/1355.
12+
cv2.setNumThreads(0)
13+
14+
915
def clip_boxes(boxes, im_shape):
1016
"""
1117
Clip boxes to image boundaries.
@@ -50,7 +56,7 @@ def _offset_boxes(boxes, im_shape, scale, offs, flip):
5056

5157
def preprocess_train(data, size_index):
5258
im_path, blob, inp_size = data
53-
inp_size = inp_size[size_index]
59+
5460
boxes, gt_classes = blob['boxes'], blob['gt_classes']
5561

5662
im = cv2.imread(im_path)
@@ -60,7 +66,8 @@ def preprocess_train(data, size_index):
6066
scale, offs, flip = trans_param
6167
boxes = _offset_boxes(boxes, im.shape, scale, offs, flip)
6268

63-
if inp_size is not None:
69+
if inp_size is not None and size_index is not None:
70+
inp_size = inp_size[size_index]
6471
w, h = inp_size
6572
boxes[:, 0::2] *= float(w) / im.shape[1]
6673
boxes[:, 1::2] *= float(h) / im.shape[0]
@@ -126,7 +133,7 @@ def postprocess(bbox_pred, iou_pred, prob_pred, im_shape, cfg, thresh=0.05,
126133
prob_pred = prob_pred[(np.arange(prob_pred.shape[0]), cls_inds)]
127134
scores = iou_pred * prob_pred
128135
# scores = iou_pred
129-
136+
assert len(scores) == len(bbox_pred), '{}, {}'.format(scores.shape, bbox_pred.shape)
130137
# threshold
131138
keep = np.where(scores >= thresh)
132139
bbox_pred = bbox_pred[keep]

0 commit comments

Comments
 (0)