Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
seminar-breakout
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shashank Suhas
seminar-breakout
Commits
81a1cb92
Commit
81a1cb92
authored
Aug 07, 2019
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add balloon demo
parent
ab229670
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
175 additions
and
10 deletions
+175
-10
examples/FasterRCNN/BALLOON.md
examples/FasterRCNN/BALLOON.md
+45
-0
examples/FasterRCNN/NOTES.md
examples/FasterRCNN/NOTES.md
+2
-0
examples/FasterRCNN/README.md
examples/FasterRCNN/README.md
+2
-2
examples/FasterRCNN/config.py
examples/FasterRCNN/config.py
+1
-0
examples/FasterRCNN/data.py
examples/FasterRCNN/data.py
+1
-1
examples/FasterRCNN/dataset/__init__.py
examples/FasterRCNN/dataset/__init__.py
+1
-0
examples/FasterRCNN/dataset/balloon.py
examples/FasterRCNN/dataset/balloon.py
+70
-0
examples/FasterRCNN/predict.py
examples/FasterRCNN/predict.py
+10
-3
examples/FasterRCNN/train.py
examples/FasterRCNN/train.py
+5
-3
examples/FasterRCNN/viz.py
examples/FasterRCNN/viz.py
+33
-0
tensorpack/tfutils/sessinit.py
tensorpack/tfutils/sessinit.py
+5
-1
No files found.
examples/FasterRCNN/BALLOON.md
0 → 100644
View file @
81a1cb92
## Balloon Demo
This is a demo on how to train tensorpack's Mask R-CNN on a custom dataset.
We use the
[
balloon dataset
](
https://github.com/matterport/Mask_RCNN/tree/master/samples/balloon
)
as an example.
1.
Download and unzip the dataset:
```
wget https://github.com/matterport/Mask_RCNN/releases/download/v2.1/balloon_dataset.zip
unzip balloon_dataset.zip
```
2.
(included already) Since this dataset is not in COCO format, we add a new file
[
dataset/balloon.py
](
dataset/balloon.py
)
to load the dataset.
Refer to
[
dataset/dataset.py
](
dataset/dataset.py
)
on the required interface of a new dataset.
3.
(included already) Register the names of the new dataset in
`train.py`
and
`predict.py`
, by calling
`register_balloon("/path/to/balloon_dataset")`
4.
Download a model pretrained on COCO from tensorpack model zoo:
```
wget http://models.tensorpack.com/FasterRCNN/COCO-MaskRCNN-R50FPN2x.npz
```
5.
Start fine-tuning on the new dataset:
```
./train.py --config DATA.BASEDIR=~/data/balloon MODE_FPN=True \
"DATA.VAL=('balloon_val',)" "DATA.TRAIN=('balloon_train',)" \
TRAIN.BASE_LR=1e-3 TRAIN.EVAL_PERIOD=0 "TRAIN.LR_SCHEDULE=[1000]" \
"PREPROC.TRAIN_SHORT_EDGE_SIZE=[600,1200]" TRAIN.CHECKPOINT_PERIOD=1 \
--load COCO-MaskRCNN-R50FPN2x.npz --logdir train_log/balloon
```
6.
You can train as long as you want, but it only takes __a few minutes__ to produce nice results.
You can visualize the results of the latest model by:
```
./predict.py --config DATA.BASEDIR=~/data/balloon MODE_FPN=True \
"DATA.VAL=('balloon_val',)" "DATA.TRAIN=('balloon_train',)" \
--load train_log/balloon/checkpoint --predict ~/data/balloon/val/*.jpg
```
This command will produce images like this in your window:

examples/FasterRCNN/NOTES.md
View file @
81a1cb92
...
@@ -31,6 +31,8 @@ Data:
...
@@ -31,6 +31,8 @@ Data:
In this class you'll implement the logic to load your dataset and evaluate predictions.
In this class you'll implement the logic to load your dataset and evaluate predictions.
The documentation is in the docstring of
`DatasetSplit.
The documentation is in the docstring of
`DatasetSplit.
See [BALLOON.md](BALLOON.md) for an example of fine-tuning on a different dataset.
1. If you load a COCO-trained model on a different dataset, you may see error messages
1. If you load a COCO-trained model on a different dataset, you may see error messages
complaining about unmatched number of categories for certain weights in the checkpoint.
complaining about unmatched number of categories for certain weights in the checkpoint.
You can either remove those weights in checkpoint, or rename them in the model.
You can either remove those weights in checkpoint, or rename them in the model.
...
...
examples/FasterRCNN/README.md
View file @
81a1cb92
...
@@ -121,6 +121,6 @@ Performance in [Detectron](https://github.com/facebookresearch/Detectron/) can b
...
@@ -121,6 +121,6 @@ Performance in [Detectron](https://github.com/facebookresearch/Detectron/) can b
Note that our training strategy is slightly different: we enable cascade throughout the entire training.
Note that our training strategy is slightly different: we enable cascade throughout the entire training.
As far as I know, this model is the __best open source TF model__ on COCO dataset.
As far as I know, this model is the __best open source TF model__ on COCO dataset.
##
Notes
##
Other Datasets / Implementation Details / Speed:
[
NOTES.md
](
NOTES.md
)
has some notes about implementation details & speed
.
See
[
BALLOON.md
](
BALLOON.md
)
and
[
NOTES.md
](
NOTES.md
)
for more details
.
examples/FasterRCNN/config.py
View file @
81a1cb92
...
@@ -141,6 +141,7 @@ _C.TRAIN.STARTING_EPOCH = 1 # the first epoch to start with, useful to continue
...
@@ -141,6 +141,7 @@ _C.TRAIN.STARTING_EPOCH = 1 # the first epoch to start with, useful to continue
_C
.
TRAIN
.
LR_SCHEDULE
=
"1x"
# "1x" schedule in detectron
_C
.
TRAIN
.
LR_SCHEDULE
=
"1x"
# "1x" schedule in detectron
_C
.
TRAIN
.
EVAL_PERIOD
=
25
# period (epochs) to run evaluation
_C
.
TRAIN
.
EVAL_PERIOD
=
25
# period (epochs) to run evaluation
_C
.
TRAIN
.
CHECKPOINT_PERIOD
=
20
# period (epochs) to save model
# preprocessing --------------------
# preprocessing --------------------
# Alternative old (worse & faster) setting: 600
# Alternative old (worse & faster) setting: 600
...
...
examples/FasterRCNN/data.py
View file @
81a1cb92
...
@@ -49,8 +49,8 @@ def print_class_histogram(roidbs):
...
@@ -49,8 +49,8 @@ def print_class_histogram(roidbs):
gt_inds
=
np
.
where
((
entry
[
"class"
]
>
0
)
&
(
entry
[
"is_crowd"
]
==
0
))[
0
]
gt_inds
=
np
.
where
((
entry
[
"class"
]
>
0
)
&
(
entry
[
"is_crowd"
]
==
0
))[
0
]
gt_classes
=
entry
[
"class"
][
gt_inds
]
gt_classes
=
entry
[
"class"
][
gt_inds
]
gt_hist
+=
np
.
histogram
(
gt_classes
,
bins
=
hist_bins
)[
0
]
gt_hist
+=
np
.
histogram
(
gt_classes
,
bins
=
hist_bins
)[
0
]
COL
=
6
data
=
list
(
itertools
.
chain
(
*
[[
class_names
[
i
+
1
],
v
]
for
i
,
v
in
enumerate
(
gt_hist
[
1
:])]))
data
=
list
(
itertools
.
chain
(
*
[[
class_names
[
i
+
1
],
v
]
for
i
,
v
in
enumerate
(
gt_hist
[
1
:])]))
COL
=
max
(
6
,
len
(
data
))
total_instances
=
sum
(
data
[
1
::
2
])
total_instances
=
sum
(
data
[
1
::
2
])
data
.
extend
([
None
]
*
(
COL
-
len
(
data
)
%
COL
))
data
.
extend
([
None
]
*
(
COL
-
len
(
data
)
%
COL
))
data
.
extend
([
"total"
,
total_instances
])
data
.
extend
([
"total"
,
total_instances
])
...
...
examples/FasterRCNN/dataset/__init__.py
View file @
81a1cb92
from
.dataset
import
*
from
.dataset
import
*
from
.coco
import
*
from
.coco
import
*
from
.balloon
import
*
examples/FasterRCNN/dataset/balloon.py
0 → 100644
View file @
81a1cb92
import
os
import
numpy
as
np
import
json
from
dataset
import
DatasetSplit
,
DatasetRegistry
__all__
=
[
"register_balloon"
]
class
BalloonDemo
(
DatasetSplit
):
def
__init__
(
self
,
base_dir
,
split
):
assert
split
in
[
"train"
,
"val"
]
base_dir
=
os
.
path
.
expanduser
(
base_dir
)
self
.
imgdir
=
os
.
path
.
join
(
base_dir
,
split
)
assert
os
.
path
.
isdir
(
self
.
imgdir
),
self
.
imgdir
def
training_roidbs
(
self
):
json_file
=
os
.
path
.
join
(
self
.
imgdir
,
"via_region_data.json"
)
with
open
(
json_file
)
as
f
:
obj
=
json
.
load
(
f
)
ret
=
[]
for
_
,
v
in
obj
.
items
():
fname
=
v
[
"filename"
]
fname
=
os
.
path
.
join
(
self
.
imgdir
,
fname
)
roidb
=
{
"file_name"
:
fname
}
annos
=
v
[
"regions"
]
boxes
=
[]
segs
=
[]
for
_
,
anno
in
annos
.
items
():
assert
not
anno
[
"region_attributes"
]
anno
=
anno
[
"shape_attributes"
]
px
=
anno
[
"all_points_x"
]
py
=
anno
[
"all_points_y"
]
poly
=
np
.
stack
((
px
,
py
),
axis
=
1
)
+
0.5
maxxy
=
poly
.
max
(
axis
=
0
)
minxy
=
poly
.
min
(
axis
=
0
)
boxes
.
append
([
minxy
[
0
],
minxy
[
1
],
maxxy
[
0
],
maxxy
[
1
]])
segs
.
append
([
poly
])
N
=
len
(
annos
)
roidb
[
"boxes"
]
=
np
.
asarray
(
boxes
,
dtype
=
np
.
float32
)
roidb
[
"segmentation"
]
=
segs
roidb
[
"class"
]
=
np
.
ones
((
N
,
),
dtype
=
np
.
int32
)
roidb
[
"is_crowd"
]
=
np
.
zeros
((
N
,
),
dtype
=
np
.
int8
)
ret
.
append
(
roidb
)
return
ret
def
register_balloon
(
basedir
):
for
split
in
[
"train"
,
"val"
]:
name
=
"balloon_"
+
split
DatasetRegistry
.
register
(
name
,
lambda
x
=
split
:
BalloonDemo
(
basedir
,
x
))
DatasetRegistry
.
register_metadata
(
name
,
"class_names"
,
[
"BG"
,
"balloon"
])
if
__name__
==
'__main__'
:
basedir
=
'~/data/balloon'
roidbs
=
BalloonDemo
(
basedir
,
"train"
)
.
training_roidbs
()
print
(
"#images:"
,
len
(
roidbs
))
from
viz
import
draw_annotation
from
tensorpack.utils.viz
import
interactive_imshow
as
imshow
import
cv2
for
r
in
roidbs
:
im
=
cv2
.
imread
(
r
[
"file_name"
])
vis
=
draw_annotation
(
im
,
r
[
"boxes"
],
r
[
"class"
],
r
[
"segmentation"
])
imshow
(
vis
)
examples/FasterRCNN/predict.py
View file @
81a1cb92
...
@@ -18,13 +18,15 @@ from tensorpack.tfutils import get_model_loader, get_tf_version_tuple
...
@@ -18,13 +18,15 @@ from tensorpack.tfutils import get_model_loader, get_tf_version_tuple
from
tensorpack.tfutils.export
import
ModelExporter
from
tensorpack.tfutils.export
import
ModelExporter
from
tensorpack.utils
import
fs
,
logger
from
tensorpack.utils
import
fs
,
logger
from
dataset
import
DatasetRegistry
,
register_coco
from
dataset
import
DatasetRegistry
,
register_coco
,
register_balloon
from
config
import
config
as
cfg
from
config
import
config
as
cfg
from
config
import
finalize_configs
from
config
import
finalize_configs
from
data
import
get_eval_dataflow
,
get_train_dataflow
from
data
import
get_eval_dataflow
,
get_train_dataflow
from
eval
import
DetectionResult
,
multithread_predict_dataflow
,
predict_image
from
eval
import
DetectionResult
,
multithread_predict_dataflow
,
predict_image
from
modeling.generalized_rcnn
import
ResNetC4Model
,
ResNetFPNModel
from
modeling.generalized_rcnn
import
ResNetC4Model
,
ResNetFPNModel
from
viz
import
draw_annotation
,
draw_final_outputs
,
draw_predictions
,
draw_proposal_recall
from
viz
import
(
draw_annotation
,
draw_final_outputs
,
draw_predictions
,
draw_proposal_recall
,
draw_final_outputs_blackwhite
)
def
do_visualize
(
model
,
model_path
,
nr_visualize
=
100
,
output_dir
=
'output'
):
def
do_visualize
(
model
,
model_path
,
nr_visualize
=
100
,
output_dir
=
'output'
):
...
@@ -97,6 +99,9 @@ def do_evaluate(pred_config, output_file):
...
@@ -97,6 +99,9 @@ def do_evaluate(pred_config, output_file):
def
do_predict
(
pred_func
,
input_file
):
def
do_predict
(
pred_func
,
input_file
):
img
=
cv2
.
imread
(
input_file
,
cv2
.
IMREAD_COLOR
)
img
=
cv2
.
imread
(
input_file
,
cv2
.
IMREAD_COLOR
)
results
=
predict_image
(
img
,
pred_func
)
results
=
predict_image
(
img
,
pred_func
)
if
cfg
.
MODE_MASK
:
final
=
draw_final_outputs_blackwhite
(
img
,
results
)
else
:
final
=
draw_final_outputs
(
img
,
results
)
final
=
draw_final_outputs
(
img
,
results
)
viz
=
np
.
concatenate
((
img
,
final
),
axis
=
1
)
viz
=
np
.
concatenate
((
img
,
final
),
axis
=
1
)
cv2
.
imwrite
(
"output.png"
,
viz
)
cv2
.
imwrite
(
"output.png"
,
viz
)
...
@@ -122,6 +127,8 @@ if __name__ == '__main__':
...
@@ -122,6 +127,8 @@ if __name__ == '__main__':
if
args
.
config
:
if
args
.
config
:
cfg
.
update_args
(
args
.
config
)
cfg
.
update_args
(
args
.
config
)
register_coco
(
cfg
.
DATA
.
BASEDIR
)
# add COCO datasets to the registry
register_coco
(
cfg
.
DATA
.
BASEDIR
)
# add COCO datasets to the registry
register_balloon
(
cfg
.
DATA
.
BASEDIR
)
MODEL
=
ResNetFPNModel
()
if
cfg
.
MODE_FPN
else
ResNetC4Model
()
MODEL
=
ResNetFPNModel
()
if
cfg
.
MODE_FPN
else
ResNetC4Model
()
if
not
tf
.
test
.
is_gpu_available
():
if
not
tf
.
test
.
is_gpu_available
():
...
...
examples/FasterRCNN/train.py
View file @
81a1cb92
...
@@ -10,7 +10,7 @@ from tensorpack import *
...
@@ -10,7 +10,7 @@ from tensorpack import *
from
tensorpack.tfutils
import
collect_env_info
from
tensorpack.tfutils
import
collect_env_info
from
tensorpack.tfutils.common
import
get_tf_version_tuple
from
tensorpack.tfutils.common
import
get_tf_version_tuple
from
dataset
import
register_coco
from
dataset
import
register_coco
,
register_balloon
from
config
import
config
as
cfg
from
config
import
config
as
cfg
from
config
import
finalize_configs
from
config
import
finalize_configs
from
data
import
get_train_dataflow
from
data
import
get_train_dataflow
...
@@ -43,6 +43,7 @@ if __name__ == '__main__':
...
@@ -43,6 +43,7 @@ if __name__ == '__main__':
if
args
.
config
:
if
args
.
config
:
cfg
.
update_args
(
args
.
config
)
cfg
.
update_args
(
args
.
config
)
register_coco
(
cfg
.
DATA
.
BASEDIR
)
# add COCO datasets to the registry
register_coco
(
cfg
.
DATA
.
BASEDIR
)
# add COCO datasets to the registry
register_balloon
(
cfg
.
DATA
.
BASEDIR
)
# add the demo balloon datasets to the registry
# Setup logger ...
# Setup logger ...
is_horovod
=
cfg
.
TRAINER
==
'horovod'
is_horovod
=
cfg
.
TRAINER
==
'horovod'
...
@@ -82,7 +83,7 @@ if __name__ == '__main__':
...
@@ -82,7 +83,7 @@ if __name__ == '__main__':
callbacks
=
[
callbacks
=
[
PeriodicCallback
(
PeriodicCallback
(
ModelSaver
(
max_to_keep
=
10
,
keep_checkpoint_every_n_hours
=
1
),
ModelSaver
(
max_to_keep
=
10
,
keep_checkpoint_every_n_hours
=
1
),
every_k_epochs
=
20
),
every_k_epochs
=
cfg
.
TRAIN
.
CHECKPOINT_PERIOD
),
# linear warmup
# linear warmup
ScheduledHyperParamSetter
(
ScheduledHyperParamSetter
(
'learning_rate'
,
warmup_schedule
,
interp
=
'linear'
,
step_based
=
True
),
'learning_rate'
,
warmup_schedule
,
interp
=
'linear'
,
step_based
=
True
),
...
@@ -105,7 +106,8 @@ if __name__ == '__main__':
...
@@ -105,7 +106,8 @@ if __name__ == '__main__':
session_init
=
None
session_init
=
None
else
:
else
:
if
args
.
load
:
if
args
.
load
:
session_init
=
get_model_loader
(
args
.
load
)
# ignore mismatched values, so you can `--load` a model for fine-tuning
session_init
=
get_model_loader
(
args
.
load
,
ignore_mismatch
=
True
)
else
:
else
:
session_init
=
get_model_loader
(
cfg
.
BACKBONE
.
WEIGHTS
)
if
cfg
.
BACKBONE
.
WEIGHTS
else
None
session_init
=
get_model_loader
(
cfg
.
BACKBONE
.
WEIGHTS
)
if
cfg
.
BACKBONE
.
WEIGHTS
else
None
...
...
examples/FasterRCNN/viz.py
View file @
81a1cb92
...
@@ -97,6 +97,39 @@ def draw_final_outputs(img, results):
...
@@ -97,6 +97,39 @@ def draw_final_outputs(img, results):
return
ret
return
ret
def
draw_final_outputs_blackwhite
(
img
,
results
):
"""
Args:
results: [DetectionResult]
"""
if
len
(
results
)
==
0
:
return
img
# Display in largest to smallest order to reduce occlusion
boxes
=
np
.
asarray
([
r
.
box
for
r
in
results
])
areas
=
np_area
(
boxes
)
sorted_inds
=
np
.
argsort
(
-
areas
)
img_bw
=
img
.
mean
(
axis
=
2
)
img_bw
=
np
.
stack
([
img_bw
]
*
3
,
axis
=
2
)
tags
=
[]
all_masks
=
[
results
[
rid
]
.
mask
for
rid
in
sorted_inds
]
if
all_masks
[
0
]
is
not
None
:
m
=
all_masks
[
0
]
>
0
for
m2
in
all_masks
[
1
:]:
m
=
m
|
(
m2
>
0
)
print
(
m
,
m
.
sum
())
img_bw
[
m
]
=
img
[
m
]
for
r
in
results
:
tags
.
append
(
"{},{:.2f}"
.
format
(
cfg
.
DATA
.
CLASS_NAMES
[
r
.
class_id
],
r
.
score
))
ret
=
viz
.
draw_boxes
(
img_bw
,
boxes
,
tags
)
return
ret
def
draw_mask
(
im
,
mask
,
alpha
=
0.5
,
color
=
None
):
def
draw_mask
(
im
,
mask
,
alpha
=
0.5
,
color
=
None
):
"""
"""
Overlay a mask on top of the image.
Overlay a mask on top of the image.
...
...
tensorpack/tfutils/sessinit.py
View file @
81a1cb92
...
@@ -172,14 +172,18 @@ class SaverRestoreRelaxed(SaverRestore):
...
@@ -172,14 +172,18 @@ class SaverRestoreRelaxed(SaverRestore):
logger
.
info
(
logger
.
info
(
"Restoring checkpoint from {} ..."
.
format
(
self
.
path
))
"Restoring checkpoint from {} ..."
.
format
(
self
.
path
))
matched_pairs
=
[]
def
f
(
reader
,
name
,
v
):
def
f
(
reader
,
name
,
v
):
val
=
reader
.
get_tensor
(
name
)
val
=
reader
.
get_tensor
(
name
)
val
=
SessionUpdate
.
relaxed_value_for_var
(
val
,
v
,
ignore_mismatch
=
True
)
val
=
SessionUpdate
.
relaxed_value_for_var
(
val
,
v
,
ignore_mismatch
=
True
)
if
val
is
not
None
:
if
val
is
not
None
:
v
.
load
(
val
)
matched_pairs
.
append
((
v
,
val
)
)
with
sess
.
as_default
():
with
sess
.
as_default
():
self
.
_match_vars
(
f
)
self
.
_match_vars
(
f
)
upd
=
SessionUpdate
(
sess
,
[
x
[
0
]
for
x
in
matched_pairs
])
upd
.
update
({
x
[
0
]
.
name
:
x
[
1
]
for
x
in
matched_pairs
})
class
DictRestore
(
SessionInit
):
class
DictRestore
(
SessionInit
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment