Skip to content

Commit

Permalink
support Pytorch 0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
messi committed Jul 26, 2018
1 parent 6544e53 commit 0f5d9aa
Show file tree
Hide file tree
Showing 45 changed files with 38 additions and 45 deletions.
Binary file added .test_RFB.py.swp
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ By Songtao Liu, Di Huang, Yunhong Wang
### Introduction
Inspired by the structure of Receptive Fields (RFs) in human visual systems, we propose a novel RF Block (RFB) module, which takes the relationship between the size and eccentricity of RFs into account, to enhance the discriminability and robustness of features. We further assemble the RFB module to the top of SSD with a lightweight CNN model, constructing the RFB Net detector. You can use the code to train/evaluate the RFB Net for object detection. For more details, please refer to our [arXiv paper](https://arxiv.org/pdf/1711.07767.pdf).

<img align="right" src="https://github.com/ruinmessi/RFBNet/blob/master/doc/rfb.png">
<img align="right" src="https://github.com/ruinmessi/RFBNet/blob/master/doc/RFB.png">

&nbsp;
&nbsp;
Expand Down
Binary file modified data/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified data/__pycache__/coco.cpython-36.pyc
Binary file not shown.
Binary file added data/__pycache__/config.cpython-36.pyc
Binary file not shown.
Binary file added data/__pycache__/data_augment.cpython-36.pyc
Binary file not shown.
Binary file modified data/__pycache__/voc0712.cpython-36.pyc
Binary file not shown.
Binary file modified data/__pycache__/voc_eval.cpython-36.pyc
Binary file not shown.
4 changes: 0 additions & 4 deletions data/data_augment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
The data augmentation procedures were interpreted from @weiliu89's SSD paper
http://arxiv.org/abs/1512.02325
TODO: implement data_augment for training
Ellis Brown, Max deGroot
"""

import torch
Expand Down
Binary file modified doc/RFB.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed doc/rfb.png
Binary file not shown.
Binary file added layers/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added layers/functions/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
13 changes: 4 additions & 9 deletions layers/functions/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import torch.backends.cudnn as cudnn
from torch.autograd import Function
from torch.autograd import Variable
from utils.box_utils import decode, nms
from utils.box_utils import decode


class Detect(Function):
Expand All @@ -15,9 +15,7 @@ class Detect(Function):
def __init__(self, num_classes, bkg_label, cfg):
self.num_classes = num_classes
self.background_label = bkg_label
#self.thresh = thresh

# Parameters used in nms.
self.variance = cfg['variance']

def forward(self, predictions, prior):
Expand All @@ -40,6 +38,9 @@ def forward(self, predictions, prior):
self.num_priors = prior_data.size(0)
self.boxes = torch.zeros(1, self.num_priors, 4)
self.scores = torch.zeros(1, self.num_priors, self.num_classes)
if loc_data.is_cuda:
self.boxes = self.boxes.cuda()
self.scores = self.scores.cuda()

if num == 1:
# size batch x num_classes x num_priors
Expand All @@ -54,13 +55,7 @@ def forward(self, predictions, prior):
# Decode predictions into bboxes.
for i in range(num):
decoded_boxes = decode(loc_data[i], prior_data, self.variance)
# For each class, perform nms
conf_scores = conf_preds[i].clone()
'''
c_mask = conf_scores.gt(self.thresh)
decoded_boxes = decoded_boxes[c_mask]
conf_scores = conf_scores[c_mask]
'''

self.boxes[i] = decoded_boxes
self.scores[i] = conf_scores
Expand Down
2 changes: 0 additions & 2 deletions layers/functions/prior_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import torch.backends.cudnn as cudnn
from math import sqrt as sqrt
from itertools import product as product
if torch.cuda.is_available():
torch.set_default_tensor_type('torch.cuda.FloatTensor')


class PriorBox(object):
Expand Down
Binary file added layers/modules/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file not shown.
5 changes: 2 additions & 3 deletions layers/modules/multibox_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
GPU = False
if torch.cuda.is_available():
GPU = True
torch.set_default_tensor_type('torch.cuda.FloatTensor')


class MultiBoxLoss(nn.Module):
Expand Down Expand Up @@ -93,7 +92,7 @@ def forward(self, predictions, priors, targets):
loss_c = log_sum_exp(batch_conf) - batch_conf.gather(1, conf_t.view(-1,1))

# Hard Negative Mining
loss_c[pos.view(-1)] = 0 # filter out pos boxes for now
loss_c[pos.view(-1,1)] = 0 # filter out pos boxes for now
loss_c = loss_c.view(num, -1)
_,loss_idx = loss_c.sort(1, descending=True)
_,idx_rank = loss_idx.sort(1)
Expand All @@ -110,7 +109,7 @@ def forward(self, predictions, priors, targets):

# Sum of losses: L(x,c,l,g) = (Lconf(x, c) + αLloc(x,l,g)) / N

N = num_pos.data.sum()
N = max(num_pos.data.sum().float(), 1)
loss_l/=N
loss_c/=N
return loss_l,loss_c
2 changes: 1 addition & 1 deletion models/RFB_Net_E_vgg.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def __init__(self, phase, size, base, extras, head, num_classes):
self.loc = nn.ModuleList(head[0])
self.conf = nn.ModuleList(head[1])
if self.phase == 'test':
self.softmax = nn.Softmax()
self.softmax = nn.Softmax(dim=-1)

def forward(self, x):
"""Applies network layers and ops on input image(s) x.
Expand Down
2 changes: 1 addition & 1 deletion models/RFB_Net_mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def __init__(self, phase, size, base, extras, head, num_classes):
self.loc = nn.ModuleList(head[0])
self.conf = nn.ModuleList(head[1])
if self.phase == 'test':
self.softmax = nn.Softmax()
self.softmax = nn.Softmax(dim=-1)

def forward(self, x):
"""Applies network layers and ops on input image(s) x.
Expand Down
2 changes: 1 addition & 1 deletion models/RFB_Net_vgg.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def __init__(self, phase, size, base, extras, head, num_classes):
self.loc = nn.ModuleList(head[0])
self.conf = nn.ModuleList(head[1])
if self.phase == 'test':
self.softmax = nn.Softmax()
self.softmax = nn.Softmax(dim=-1)

def forward(self, x):
"""Applies network layers and ops on input image(s) x.
Expand Down
Binary file added models/__pycache__/RFB_Net_vgg.cpython-36.pyc
Binary file not shown.
Binary file added models/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
30 changes: 15 additions & 15 deletions test_RFB.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
help='Dir to save results')
parser.add_argument('--cuda', default=True, type=bool,
help='Use cuda to train model')
parser.add_argument('--cpu', default=False, type=bool,
help='Use cpu nms')
parser.add_argument('--retest', default=False, type=bool,
help='test cache results')
args = parser.parse_args()
Expand All @@ -54,9 +56,10 @@
print('Unkown version!')

priorbox = PriorBox(cfg)
priors = Variable(priorbox.forward(), volatile=True)
if not args.cuda:
priors = priors.cpu()
with torch.no_grad():
priors = priorbox.forward()
if args.cuda:
priors = priors.cuda()


def test_net(save_folder, net, detector, cuda, testset, transform, max_per_image=300, thresh=0.005):
Expand All @@ -82,9 +85,13 @@ def test_net(save_folder, net, detector, cuda, testset, transform, max_per_image

for i in range(num_images):
img = testset.pull_image(i)
x = Variable(transform(img).unsqueeze(0),volatile=True)
if cuda:
x = x.cuda()
scale = torch.Tensor([img.shape[1], img.shape[0],
img.shape[1], img.shape[0]])
with torch.no_grad():
x = transform(img).unsqueeze(0)
if cuda:
x = x.cuda()
scale = scale.cuda()

_t['im_detect'].tic()
out = net(x) # forward pass
Expand All @@ -93,12 +100,10 @@ def test_net(save_folder, net, detector, cuda, testset, transform, max_per_image
boxes = boxes[0]
scores=scores[0]

boxes *= scale
boxes = boxes.cpu().numpy()
scores = scores.cpu().numpy()
# scale each detection back up to the image
scale = torch.Tensor([img.shape[1], img.shape[0],
img.shape[1], img.shape[0]]).cpu().numpy()
boxes *= scale

_t['misc'].tic()

Expand All @@ -111,13 +116,8 @@ def test_net(save_folder, net, detector, cuda, testset, transform, max_per_image
c_scores = scores[inds, j]
c_dets = np.hstack((c_bboxes, c_scores[:, np.newaxis])).astype(
np.float32, copy=False)
if args.dataset == 'VOC':
cpu = True
else:
cpu = False

keep = nms(c_dets, 0.45, force_cpu=cpu)
keep = keep[:50]
keep = nms(c_dets, 0.45, force_cpu=args.cpu)
c_dets = c_dets[keep, :]
all_boxes[j][i] = c_dets
if max_per_image > 0:
Expand Down
18 changes: 11 additions & 7 deletions train_RFB.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def weights_init(m):
for key in m.state_dict():
if key.split('.')[-1] == 'weight':
if 'conv' in key:
init.kaiming_normal(m.state_dict()[key], mode='fan_out')
init.kaiming_normal_(m.state_dict()[key], mode='fan_out')
if 'bn' in key:
m.state_dict()[key][...] = 1
elif key.split('.')[-1] == 'bias':
Expand Down Expand Up @@ -146,7 +146,11 @@ def weights_init(m):

criterion = MultiBoxLoss(num_classes, 0.5, True, 0, True, 3, 0.5, False)
priorbox = PriorBox(cfg)
priors = Variable(priorbox.forward(), volatile=True)
with torch.no_grad():
priors = priorbox.forward()
if args.cuda:
priors = priors.cuda()



def train():
Expand Down Expand Up @@ -207,10 +211,10 @@ def train():

if args.cuda:
images = Variable(images.cuda())
targets = [Variable(anno.cuda(),volatile=True) for anno in targets]
targets = [Variable(anno.cuda()) for anno in targets]
else:
images = Variable(images)
targets = [Variable(anno, volatile=True) for anno in targets]
targets = [Variable(anno) for anno in targets]
# forward
t0 = time.time()
out = net(images)
Expand All @@ -221,14 +225,14 @@ def train():
loss.backward()
optimizer.step()
t1 = time.time()
loc_loss += loss_l.data[0]
conf_loss += loss_c.data[0]
loc_loss += loss_l.item()
conf_loss += loss_c.item()
load_t1 = time.time()
if iteration % 10 == 0:
print('Epoch:' + repr(epoch) + ' || epochiter: ' + repr(iteration % epoch_size) + '/' + repr(epoch_size)
+ '|| Totel iter ' +
repr(iteration) + ' || L: %.4f C: %.4f||' % (
loss_l.data[0],loss_c.data[0]) +
loss_l.item(),loss_c.item()) +
'Batch time: %.4f sec. ||' % (load_t1 - load_t0) + 'LR: %.8f' % (lr))

torch.save(net.state_dict(), args.save_folder +
Expand Down
Binary file added utils/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added utils/__pycache__/box_utils.cpython-36.pyc
Binary file not shown.
Binary file added utils/__pycache__/nms_wrapper.cpython-36.pyc
Binary file not shown.
Binary file added utils/__pycache__/timer.cpython-36.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion utils/box_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import numpy as np
if torch.cuda.is_available():
import torch.backends.cudnn as cudnn
torch.set_default_tensor_type('torch.cuda.FloatTensor')


def point_form(boxes):
Expand Down Expand Up @@ -296,3 +295,5 @@ def nms(boxes, scores, overlap=0.5, top_k=200):
# keep only elements with an IoU <= overlap
idx = idx[IoU.le(overlap)]
return keep, count


Binary file added utils/build/temp.linux-x86_64-3.6/nms/cpu_nms.o
Binary file not shown.
Binary file added utils/build/temp.linux-x86_64-3.6/nms/gpu_nms.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added utils/nms/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added utils/pycocotools/__pycache__/coco.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file added utils/pycocotools/__pycache__/mask.cpython-36.pyc
Binary file not shown.
Binary file not shown.

0 comments on commit 0f5d9aa

Please sign in to comment.