Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
517858ad
Commit
517858ad
authored
Feb 27, 2016
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
changed to run on image net
parent
fe70bd12
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
31 deletions
+39
-31
dnn_mit67_ex.cpp
examples/dnn_mit67_ex.cpp
+39
-31
No files found.
examples/dnn_mit67_ex.cpp
View file @
517858ad
...
...
@@ -41,7 +41,9 @@ void randomly_crop_image (
)
{
// figure out what rectangle we want to crop from the image
auto
scale
=
1
-
rnd
.
get_random_double
()
*
0.2
;
//auto scale = 1-rnd.get_random_double()*0.2;
double
mins
=
0.466666666
,
maxs
=
0.875
;
auto
scale
=
mins
+
rnd
.
get_random_double
()
*
(
maxs
-
mins
);
auto
size
=
scale
*
std
::
min
(
img
.
nr
(),
img
.
nc
());
rectangle
rect
(
size
,
size
);
// randomly shift the box around
...
...
@@ -49,8 +51,8 @@ void randomly_crop_image (
rnd
.
get_random_32bit_number
()
%
(
img
.
nr
()
-
rect
.
height
()));
rect
=
move_rect
(
rect
,
offset
);
// now crop it out as a 2
50x250
image.
extract_image_chip
(
img
,
chip_details
(
rect
,
chip_dims
(
2
50
,
250
)),
crop
);
// now crop it out as a 2
24x224
image.
extract_image_chip
(
img
,
chip_details
(
rect
,
chip_dims
(
2
24
,
224
)),
crop
);
// Also randomly flip the image
if
(
rnd
.
get_random_double
()
>
0.5
)
...
...
@@ -71,7 +73,9 @@ void randomly_crop_images (
for
(
long
i
=
0
;
i
<
num_crops
;
++
i
)
{
// figure out what rectangle we want to crop from the image
auto
scale
=
1
-
rnd
.
get_random_double
()
*
0.2
;
//auto scale = 1-rnd.get_random_double()*0.2;
double
mins
=
0.466666666
,
maxs
=
0.875
;
auto
scale
=
mins
+
rnd
.
get_random_double
()
*
(
maxs
-
mins
);
auto
size
=
scale
*
std
::
min
(
img
.
nr
(),
img
.
nc
());
rectangle
rect
(
size
,
size
);
// randomly shift the box around
...
...
@@ -79,7 +83,7 @@ void randomly_crop_images (
rnd
.
get_random_32bit_number
()
%
(
img
.
nr
()
-
rect
.
height
()));
rect
=
move_rect
(
rect
,
offset
);
dets
.
push_back
(
chip_details
(
rect
,
chip_dims
(
2
50
,
250
)));
dets
.
push_back
(
chip_details
(
rect
,
chip_dims
(
2
24
,
224
)));
}
extract_image_chips
(
img
,
dets
,
crops
);
...
...
@@ -104,7 +108,7 @@ struct image_info
unsigned
long
numeric_label
;
};
std
::
vector
<
image_info
>
get_
mit67
_listing
(
std
::
vector
<
image_info
>
get_
imagenet
_listing
(
const
std
::
string
&
images_folder
)
{
...
...
@@ -147,9 +151,10 @@ int main(int argc, char** argv) try
return
1
;
}
auto
listing
=
get_
mit67
_listing
(
argv
[
1
]);
auto
listing
=
get_
imagenet
_listing
(
argv
[
1
]);
cout
<<
"images in dataset: "
<<
listing
.
size
()
<<
endl
;
if
(
listing
.
size
()
==
0
||
listing
.
back
().
numeric_label
!=
66
)
const
auto
number_of_classes
=
listing
.
back
().
numeric_label
+
1
;
if
(
listing
.
size
()
==
0
||
number_of_classes
!=
1000
)
{
cout
<<
"Didn't find the MIT 67 scene dataset. Are you sure you gave the correct folder?"
<<
endl
;
cout
<<
"Give the Images folder as an argument to this program."
<<
endl
;
...
...
@@ -161,21 +166,21 @@ int main(int argc, char** argv) try
const
double
weight_decay
=
sa
=
argv
[
2
];
typedef
loss_multiclass_log
<
fc
<
avg_pool
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
res
<
max_pool
<
relu
<
bn
<
con
<
input
<
matrix
<
rgb_pixel
>
>>>>>>>>>>>>>>>>
net_type
;
>>>>>>>>>>>>>>>>
>>>>>>>>
net_type
;
net_type
net
(
fc_
(
67
),
net_type
net
(
fc_
(
number_of_classes
),
avg_pool_
(
1000
,
1000
,
1000
,
1000
),
res_
(
512
),
res_
(
512
,
2
),
res_
(
256
),
res_
(
256
,
2
),
res_
(
128
),
res_
(
128
,
2
),
res_
(
64
),
res_
(
64
),
res_
(
512
),
res_
(
512
),
res_
(
512
,
2
),
res_
(
256
),
res_
(
256
),
res_
(
256
),
res_
(
256
),
res_
(
256
),
res_
(
256
,
2
),
res_
(
128
),
res_
(
128
),
res_
(
128
),
res_
(
128
,
2
),
res_
(
64
),
res_
(
64
),
res_
(
64
),
max_pool_
(
3
,
3
,
2
,
2
),
relu_
(),
bn_
(
CONV_MODE
),
con_
(
64
,
7
,
7
,
2
,
2
)
);
...
...
@@ -185,12 +190,13 @@ int main(int argc, char** argv) try
dnn_trainer
<
net_type
>
trainer
(
net
,
sgd
(
initial_step_size
,
weight_decay
));
trainer
.
be_verbose
();
trainer
.
set_synchronization_file
(
"mit67_sync3_"
+
cast_to_string
(
weight_decay
),
std
::
chrono
::
minutes
(
5
));
trainer
.
set_synchronization_file
(
"sync_imagenet_full_training_set_40000_minstep_"
+
cast_to_string
(
weight_decay
),
std
::
chrono
::
minutes
(
5
));
trainer
.
set_iterations_between_step_size_adjust
(
40000
);
std
::
vector
<
matrix
<
rgb_pixel
>>
samples
;
std
::
vector
<
unsigned
long
>
labels
;
randomize_samples
(
listing
);
const
size_t
training_part
=
listing
.
size
()
*
0.7
;
const
size_t
training_part
=
listing
.
size
()
*
1.0
;
dlib
::
rand
rnd
;
...
...
@@ -198,14 +204,14 @@ int main(int argc, char** argv) try
const
bool
do_training
=
true
;
if
(
do_training
)
{
while
(
trainer
.
get_step_size
()
>=
1e-
4
)
while
(
trainer
.
get_step_size
()
>=
1e-
3
)
{
samples
.
clear
();
labels
.
clear
();
// make a
64
image mini-batch
// make a
128
image mini-batch
matrix
<
rgb_pixel
>
img
,
crop
;
while
(
samples
.
size
()
<
64
)
while
(
samples
.
size
()
<
128
)
{
auto
l
=
listing
[
rnd
.
get_random_32bit_number
()
%
training_part
];
load_image
(
img
,
l
.
filename
);
...
...
@@ -222,25 +228,25 @@ int main(int argc, char** argv) try
net
.
clean
();
cout
<<
"saving network"
<<
endl
;
serialize
(
"
mit67_network3
_"
+
cast_to_string
(
weight_decay
)
+
".dat"
)
<<
net
;
serialize
(
"
imagenet_full_training_set_40000_minstep
_"
+
cast_to_string
(
weight_decay
)
+
".dat"
)
<<
net
;
}
const
bool
test_network
=
tru
e
;
const
bool
test_network
=
fals
e
;
if
(
test_network
)
{
typedef
loss_multiclass_log
<
fc
<
avg_pool
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
ares
<
max_pool
<
relu
<
affine
<
con
<
input
<
matrix
<
rgb_pixel
>
>>>>>>>>>>>>>>>>
anet_type
;
>>>>>>>>>>>>>>>>
>>>>>>>>
anet_type
;
anet_type
net
;
deserialize
(
"
mit67
_network3_"
+
cast_to_string
(
weight_decay
)
+
".dat"
)
>>
net
;
deserialize
(
"
imagenet
_network3_"
+
cast_to_string
(
weight_decay
)
+
".dat"
)
>>
net
;
dlib
::
array
<
matrix
<
rgb_pixel
>>
images
;
std
::
vector
<
unsigned
long
>
labels
;
...
...
@@ -249,6 +255,7 @@ int main(int argc, char** argv) try
int
num_right
=
0
;
int
num_wrong
=
0
;
console_progress_indicator
pbar
(
training_part
);
/*
for (size_t i = 0; i < training_part; ++i)
{
pbar.print_status(i);
...
...
@@ -261,6 +268,7 @@ int main(int argc, char** argv) try
else
++num_wrong;
}
*/
cout
<<
"
\n
training num_right: "
<<
num_right
<<
endl
;
cout
<<
"training num_wrong: "
<<
num_wrong
<<
endl
;
...
...
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