Skip to content

Refactoring all the code to simplify things #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 79 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
1f37294
Initial version of Fast-RCNN. Work in progress
fmassa Jul 9, 2015
344a9bb
Add ROIDataLayer, still incomplete
fmassa Aug 7, 2015
c5d456c
Commiting what we have for the moment
fmassa Aug 13, 2015
ea4dcd3
Updating BatchProviderROI
fmassa Aug 13, 2015
6ac59e8
Basics of Fast-RCNN seems to be working
fmassa Aug 15, 2015
f9b514c
Almost working
fmassa Aug 15, 2015
22e7b0e
Basic test for FRCNN
fmassa Aug 16, 2015
bc6c681
Fix bug in ROIPooling
fmassa Aug 17, 2015
f6d5381
Improve training script for frcnn
fmassa Aug 18, 2015
590cd80
Cleaning up and fix in test
fmassa Aug 19, 2015
19a8e7f
Add gitignore
fmassa Aug 20, 2015
e9e55e7
Fix unitialized memory in BatchProviderROI
fmassa Aug 22, 2015
077c169
Fix bug in BatchProviderROI
fmassa Aug 22, 2015
d1d9f9c
Add flip in training and test code
fmassa Aug 23, 2015
b810d29
Force rois to be in cpu
fmassa Aug 23, 2015
e32c8d4
Fix wrong batch-size
fmassa Aug 30, 2015
639af95
Modifications in the training procedure
fmassa Aug 31, 2015
da0dc4a
Started refactoring code to unify all frameworks. Doesn't work yet
fmassa Sep 13, 2015
417e082
Continue refactoring
fmassa Sep 14, 2015
b5a0223
Factorized RCNN and FRCNN as feature providers, and modified BatchPro…
fmassa Sep 14, 2015
0adcd67
Start cleaning data.lua and add a config file with parameters of the …
fmassa Sep 14, 2015
086da4d
Add batched cropping in SPP. Still needs some clean up
fmassa Sep 17, 2015
e36f6be
The basics of RCNN and Fast-RCNN seems to be working after refactorin…
fmassa Sep 18, 2015
39becec
Add train/test option to feature providers
fmassa Sep 19, 2015
e6e5e18
Fix bug in ROIPooling
fmassa Sep 19, 2015
757f901
Continue refactoring. SPP is not working ATM
fmassa Sep 19, 2015
b3b12d1
Fixed SPP. Gives the same result as before
fmassa Sep 19, 2015
bb053cb
Batch providers are working again.
fmassa Sep 20, 2015
ab435d3
Adding argcheck for most of the classes
fmassa Sep 20, 2015
c9545ae
Reduce memory usage during detection for RCNN and SPP
fmassa Sep 20, 2015
48037f5
ImageDetect also outputs bbox, enabling implementing bbox regression
fmassa Sep 21, 2015
5c289d1
Add simple tests for training
fmassa Sep 21, 2015
20a3b8e
Basic test comprising training and testing for FRCNN
fmassa Sep 21, 2015
f932bd0
Fix super constructors in BatchProviders. Thanks @0wu
fmassa Sep 23, 2015
295724f
Started cleaning a bit DataSetPascal
fmassa Sep 23, 2015
1ac5175
Add utility concat, allows concatenating void tensors
fmassa Sep 24, 2015
b9e5bdb
Avoid memory explosion in RCNN/SPP. Loads images as float
fmassa Sep 25, 2015
aabd917
Fix concat when dim is not provided
fmassa Sep 25, 2015
1bb94ee
Fixing ambiguous constructor in FRCNN and upvalue function in Tester.…
fmassa Sep 25, 2015
6f76215
Fix ambiguous call to constructor in RCNN
fmassa Sep 29, 2015
56d6b72
Fix problem with argcheck in SPP
fmassa Sep 30, 2015
a99dcf4
Refactoring a bit more DataSetPascal
fmassa Sep 30, 2015
f9b53a7
Depreceating old Tester and renaming the new one
fmassa Oct 1, 2015
274d8ff
Add visualization function
fmassa Oct 1, 2015
d17c500
Fix bug with DataSetPascal and rename Tester class
fmassa Oct 1, 2015
569684b
Adapt test utils to new constructors
fmassa Oct 1, 2015
25c2bff
Add DataSetCOCO
fmassa Oct 4, 2015
c02a6b4
Generalizing argument checks
fmassa Oct 7, 2015
376a3c4
Improve visualization tool
fmassa Oct 8, 2015
a7f3b99
Started updating README
fmassa Oct 14, 2015
9f6abf1
Improve README
fmassa Oct 18, 2015
771df8f
Add example on how to train and test RCNN
fmassa Oct 18, 2015
af3a622
Renaming files
fmassa Oct 18, 2015
5b64965
Finish renaming files
fmassa Oct 18, 2015
eb579f6
Update README and examples
fmassa Oct 18, 2015
b4ef606
Fix example
fmassa Oct 18, 2015
e66b532
Tweak example
fmassa Oct 18, 2015
ddabe6b
Doc tweak
fmassa Oct 19, 2015
cfba0e8
Fix readme
fmassa Oct 19, 2015
7d5950a
Add threads to RCNN
fmassa Nov 9, 2015
a84e16a
Force image to be float in RCNN
fmassa Nov 9, 2015
8c906e9
Avoid modifying original bbox in RCNN SPP
fmassa Nov 10, 2015
0962e70
Updating readme and examples with threads
fmassa Nov 14, 2015
a8479f4
Remove old Tester
fmassa Nov 15, 2015
c0d474e
Adding pretty-print to feature transformers
fmassa Nov 15, 2015
d1075c6
Doc tweak
fmassa Nov 15, 2015
91691c9
Doc fix
fmassa Nov 15, 2015
8b86477
Updating SPP with cache function
fmassa Nov 15, 2015
b664985
Remove depreceated file
fmassa Nov 15, 2015
42d3ae2
Preliminary changes to SVMTrainer
fmassa Nov 16, 2015
412ff8e
Improve visualization tool
fmassa Nov 16, 2015
d557865
Improve example in README
fmassa Nov 16, 2015
786b7fb
Fix generic arguments in BatchProvider
fmassa Dec 10, 2015
b890a4e
Updating main script. Needs further testing
fmassa Dec 10, 2015
db30250
README tweak
fmassa Dec 10, 2015
3f072d2
Update Fast R-CNN model
fmassa Jan 30, 2016
b949c6a
Fix link in README
fmassa Jan 30, 2016
83da6a5
Update frcnn_alexnet.lua
fmassa Mar 8, 2016
a0a4a51
Explicitly require modelpath in example
fmassa Apr 13, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*~
*.swp
cachedir/*
149 changes: 149 additions & 0 deletions BatchProviderBase.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
local argcheck = require 'argcheck'

local function createWindowBase(rec,i,j,is_bg)
local label = is_bg == true and 0+1 or rec.label[j]+1
local window = {i,rec.boxes[j][1],rec.boxes[j][2],
rec.boxes[j][3],rec.boxes[j][4],
label}
return window
end

local function createWindowAngle(rec,i,j,is_bg)
local label = is_bg == true and 0+1 or rec.label[j]+1
--local ang = ( is_bg == false and rec.objects[rec.correspondance[j] ] ) and
-- rec.objects[rec.correspondance[j] ].viewpoint.azimuth or 0
local ang
if is_bg == false and rec.objects[rec.correspondance[j] ] then
if rec.objects[rec.correspondance[j] ].viewpoint.distance == '0' then
ang = rec.objects[rec.correspondance[j] ].viewpoint.azimuth_coarse
else
ang = rec.objects[rec.correspondance[j] ].viewpoint.azimuth
end
else
ang = 0
end
local window = {i,rec.boxes[j][1],rec.boxes[j][2],
rec.boxes[j][3],rec.boxes[j][4],
label,ang}
return window
end

--[[
local argcheck = require 'argcheck'
local initcheck = argcheck{
pack=true,
noordered=true,
{name="dataset",
type="nnf.DataSetPascal",
help="A dataset class"
},
{name="batch_size",
type="number",
default=128,
help="batch size"},
{name="fg_fraction",
type="number",
default=0.25,
help="foreground fraction in batch"
},
{name="fg_threshold",
type="number",
default=0.5,
help="foreground threshold"
},
{name="bg_threshold",
type="table",
default={0,0.5},
help="background threshold, in the form {LO,HI}"
},
{name="createWindow",
type="function",
default=createWindowBase,
help=""
},
{name="do_flip",
type="boolean",
default=true,
help="sample batches with random flips"
},
}
--]]

local BatchProviderBase = torch.class('nnf.BatchProviderBase')

function BatchProviderBase:__init(...)

self.dataset = nil
self.batch_size = 128
self.fg_fraction = 0.25
self.fg_threshold = 0.5
self.bg_threshold = {0,0.5}
self.createWindow = createWindowBase
self.do_flip = true

--local opts = initcheck(...)
--for k,v in pairs(opts) do self[k] = v end

end

-- allow changing the way self.bboxes are formatted
function BatchProviderBase:setCreateWindow(createWindow)
self.createWindow = createWindow
end

function BatchProviderBase:setupData()
local dataset = self.dataset
local bb = {}
local bbT = {}

for i=0,dataset.num_classes do -- 0 because of background
bb[i] = {}
end

for i=1,dataset.num_imgs do
bbT[i] = {}
end

for i = 1,dataset.num_imgs do
if dataset.num_imgs > 10 then
xlua.progress(i,dataset.num_imgs)
end

local rec = dataset:attachProposals(i)

for j=1,rec:size() do
local id = rec.label[j]
local is_fg = (rec.overlap[j] >= self.fg_threshold)
local is_bg = (not is_fg) and (rec.overlap[j] >= self.bg_threshold[1] and
rec.overlap[j] < self.bg_threshold[2])
if is_fg then
local window = self.createWindow(rec,i,j,is_bg)
table.insert(bb[1], window) -- could be id instead of 1
elseif is_bg then
local window = self.createWindow(rec,i,j,is_bg)
table.insert(bb[0], window)
end

end

for j=0,dataset.num_classes do -- 0 because of background
if #bb[j] > 0 then
bbT[i][j] = torch.FloatTensor(bb[j])
end
end

bb = {}
for i=0,dataset.num_classes do -- 0 because of background
bb[i] = {}
end
collectgarbage()
end
self.bboxes = bbT
--return bbT
end

function BatchProviderBase:getBatch()
error("You can't use BatchProviderBase")
return input,target
end

164 changes: 164 additions & 0 deletions BatchProviderIC.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
local BatchProvider, parent = torch.class('nnf.BatchProviderIC','nnf.BatchProviderBase')

local argcheck = require 'argcheck'

local env = require 'argcheck.env' -- retrieve argcheck environement
-- this is the default type function
-- which can be overrided by the user
function env.istype(obj, typename)
if typename == 'DataSet' then
return obj and obj._isDataSet
end
if typename == 'FeatureProvider' then
return obj and obj._isFeatureProvider
end
return torch.type(obj) == typename
end


local initcheck = argcheck{
pack=true,
noordered=true,
{name="dataset",
type="DataSet",
help="A dataset class"
},
{name="feat_provider",
type="nnf.FRCNN",
help="A feat provider class"
},
{name="batch_size",
type="number",
opt=true,
help="batch size"},
{name="imgs_per_batch",
type="number",
default=2,
help="number of images to sample in a batch"},
{name="fg_fraction",
type="number",
default=0.25,
help="foreground fraction in batch"
},
{name="fg_threshold",
type="number",
default=0.5,
help="foreground threshold"
},
{name="bg_threshold",
type="table",
default={0.1,0.5},
help="background threshold, in the form {LO,HI}"
},
{name="do_flip",
type="boolean",
default=true,
help="sample batches with random flips"
},
}

function BatchProvider:__init(...)
parent.__init(self)

local opts = initcheck(...)
for k,v in pairs(opts) do self[k] = v end
end

-- setup is the same

function BatchProvider:permuteIdx()
local total_img = self.dataset:size()
local imgs_per_batch = self.imgs_per_batch

self._cur = self._cur or math.huge

if self._cur + imgs_per_batch > total_img then
self._perm = torch.randperm(total_img)
self._cur = 1
end

local img_idx = self._perm[{{self._cur,self._cur + self.imgs_per_batch - 1}}]
self._cur = self._cur + self.imgs_per_batch

local img_idx_end = imgs_per_batch

local fg_windows = {}
local bg_windows = {}
for i=1,img_idx_end do
local curr_idx = img_idx[i]
bg_windows[i] = {}
if self.bboxes[curr_idx][0] then
for j=1,self.bboxes[curr_idx][0]:size(1) do
table.insert(bg_windows[i],{curr_idx,j})
end
end
fg_windows[i] = {}
if self.bboxes[curr_idx][1] then
for j=1,self.bboxes[curr_idx][1]:size(1) do
table.insert(fg_windows[i],{curr_idx,j})
end
end
end
local do_flip = torch.FloatTensor(imgs_per_batch):random(0,1)
local opts = {img_idx=img_idx,img_idx_end=img_idx_end,do_flip=do_flip}
return fg_windows,bg_windows,opts

end

function BatchProvider:selectBBoxes(fg_windows,bg_windows)
local fg_num_each = torch.round(self.fg_num_each/self.imgs_per_batch)
local bg_num_each = torch.round(self.bg_num_each/self.imgs_per_batch)

local bboxes = {}
local labels = {}
for im=1,self.imgs_per_batch do
local window_idx = torch.randperm(#bg_windows[im])
local end_idx = math.min(bg_num_each,#bg_windows[im])
local bbox = {}
for i=1,end_idx do
local curr_idx = bg_windows[im][window_idx[i] ][1]
local position = bg_windows[im][window_idx[i] ][2]
local dd = self.bboxes[curr_idx][0][position][{{2,6}}]
table.insert(bbox,{dd[1],dd[2],dd[3],dd[4]})
table.insert(labels,dd[5])
end

window_idx = torch.randperm(#fg_windows[im])
local end_idx = math.min(fg_num_each,#fg_windows[im])
for i=1,end_idx do
local curr_idx = fg_windows[im][window_idx[i] ][1]
local position = fg_windows[im][window_idx[i] ][2]
local dd = self.bboxes[curr_idx][1][position][{{2,6}}]
table.insert(bbox,{dd[1],dd[2],dd[3],dd[4]})
table.insert(labels,dd[5])
end
table.insert(bboxes,torch.FloatTensor(bbox))
end
labels = torch.IntTensor(labels)
return bboxes, labels
end

function BatchProvider:getBatch()
local dataset = self.dataset

self.fg_num_each = self.fg_fraction * self.batch_size
self.bg_num_each = self.batch_size - self.fg_num_each

local fg_windows,bg_windows,opts = self:permuteIdx()

self.targets = self.targets or torch.FloatTensor()

local batches = self.batches
local targets = self.targets

local imgs = {}
for i=1,opts.img_idx:size(1) do
table.insert(imgs,dataset:getImage(opts.img_idx[i]))
end
local boxes,labels = self:selectBBoxes(fg_windows,bg_windows)
self.batches = self.feat_provider:getFeature(imgs,boxes,opts.do_flip)

targets:resize(labels:size()):copy(labels)

return self.batches, self.targets
end
Loading