-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbearembed.cpp
1399 lines (1254 loc) · 50.6 KB
/
bearembed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// The contents of this file are licensed under the MIT license.
// See LICENSE.txt for more information.
/*
This program takes a set of face chips and trains a network for face
embedding.
This program is based on the following example from dlib:
https://github.com/davisking/dlib/blob/master/examples/dnn_metric_learning_ex.cpp
The program had 3 usage modes:
1. train: using a set of face chips, train an embedding network
2. test: using a set of face chips, test the network for accuracy
3. embed: use the trained network to create embeddings fro each provided
face chip.
*/
#include <iostream>
#include <ctime>
#include <dlib/dnn.h>
#include <dlib/image_io.h>
#include <dlib/misc_api.h>
#include <dlib/cmd_line_parser.h>
#include <dlib/matrix.h>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include "boost/date_time/local_time_adjustor.hpp"
#include "boost/date_time/c_local_time_adjustor.hpp"
#include <map>
using namespace dlib;
using namespace std;
using namespace boost::posix_time;
using namespace boost::gregorian;
using namespace boost::algorithm;
using boost::property_tree::ptree;
// ----------------------------------------------------------------------------------------
// program functionality:
// list of training set & test set
// network produced
// results
// any parameters used as input (size of vector)
//
// log error rate(s) through iterations.
// ----------------------------------------------------------------------------------------
// We will need to create some functions for loading data. This program will
// expect to be given a directory structured as follows:
// top_level_directory/
// person1/
// image1.jpg
// image2.jpg
// image3.jpg
// person2/
// image4.jpg
// image5.jpg
// image6.jpg
// person3/
// image7.jpg
// image8.jpg
// image9.jpg
//
// The specific folder and image names don't matter, nor does the number of folders or
// images. What does matter is that there is a top level folder, which contains
// subfolders, and each subfolder contains images of a single person.
ptree g_xml_tree;
std::string g_mode; // one of {train,test,embed}
std::vector <ptree> g_chips;
//--------------------------------------------------
// initialize xml
//--------------------------------------------------
int xml_add_headers ()
{
g_xml_tree.add("dataset.name", "bearid dataset");
g_xml_tree.add("dataset.comment", "Created by bearembed");
return 0;
}
//--------------------------------------------------
// This function spiders the top level directory and obtains a list of all the
// image files.
std::vector<std::vector<string>> load_objects_list (
const string& dir
)
{
std::vector<std::vector<string>> objects;
for (auto subdir : directory(dir).get_dirs())
{
std::vector<string> imgs;
for (auto img : subdir.get_files())
imgs.push_back(img);
if (imgs.size() != 0)
objects.push_back(imgs);
}
return objects;
}
/*!
jitter image for augmentation.
requires
- image_type == an image object that implements the interface defined in
dlib/image_processing/generic_image.h
- pixel_traits<typename image_traits<image_type>::pixel_type>::has_alpha == false
- img.size() > 0
- img.nr() == img.nc()
ensures
- Randomly jitters the image a little bit and returns this new jittered image.
To be specific, the returned image has the same size as img and will look
generally similar. The difference is that the returned image will have been
slightly rotated, zoomed, and translated. There is also a 50% chance it will
be mirrored left to right.
!*/
template <
typename image_type
>
image_type my_jitter_image(
const image_type& img,
dlib::rand& rnd
)
{
DLIB_CASSERT(num_rows(img)*num_columns(img) != 0);
DLIB_CASSERT(num_rows(img)==num_columns(img));
const double max_rotation_degrees = 3;
const double min_object_height = 0.97;
const double max_object_height = 0.99999;
const double translate_amount = 0.02;
const auto rect = shrink_rect(get_rect(img),3);
// perturb the location of the crop by a small fraction of the object's size.
const point rand_translate = dpoint(rnd.get_double_in_range(-translate_amount,translate_amount)*rect.width(),
rnd.get_double_in_range(-translate_amount,translate_amount)*rect.height());
// perturb the scale of the crop by a fraction of the object's size
const double rand_scale_perturb = rnd.get_double_in_range(min_object_height, max_object_height);
const long box_size = rect.height()/rand_scale_perturb;
const auto crop_rect = centered_rect(center(rect)+rand_translate, box_size, box_size);
const double angle = rnd.get_double_in_range(-max_rotation_degrees, max_rotation_degrees)*pi/180;
image_type crop;
extract_image_chip(img, chip_details(crop_rect, chip_dims(num_rows(img),num_columns(img)), angle), crop);
if (rnd.get_random_double() > 0.5)
flip_image_left_right(crop);
return crop;
}
//-----------------------------------------------------------------
// Grab matched and unmatched pairs of chip files and labels
//-----------------------------------------------------------------
std::vector<std::vector<string>> load_pairs_map (
const std::string& xml_file)
{
//std::map<string,std::vector<std::string>> chips_map;
ptree tree;
boost::property_tree::read_xml (xml_file, tree,
boost::property_tree::xml_parser::trim_whitespace);
cout << "load_pairs_map" << endl;
std::vector<std::vector<string>> objects; // return object
std::vector<string> matched_chips;
std::vector<string> unmatched_chips;
std::vector<string> matched_labels;
std::vector<string> unmatched_labels;
int count = 0;
BOOST_FOREACH(ptree::value_type& child, tree.get_child("dataset.pairs"))
{
std::string child_name = child.first;
std::vector<string> pairFiles;
std::vector<string> pairLabels;
//cout << child_name << " " << count << endl;
if ((child_name == "pair_matched") || (child_name == "pair_unmatched"))
{
ptree pairtree = (ptree) child.second;
int ccount = 0;
BOOST_FOREACH(ptree::value_type& pchild, pairtree)
{
//cout << pchild.first << endl;
ccount += 1;
std::string chipfile = pchild.second.get<std::string>("<xmlattr>.file");
pairFiles.push_back (chipfile);
std::string bearID = pchild.second.get<std::string>("label");
pairLabels.push_back (bearID);
//cout << "ID: " << bearID << " File: " << chipfile << endl;
}
if (ccount == 2)
{
// Add pair to map
if (child_name == "pair_matched")
{
matched_chips.push_back (pairFiles[0]);
matched_chips.push_back (pairFiles[1]);
matched_labels.push_back (pairLabels[0]);
matched_labels.push_back (pairLabels[1]);
}
else{
unmatched_chips.push_back (pairFiles[0]);
unmatched_chips.push_back (pairFiles[1]);
unmatched_labels.push_back (pairLabels[0]);
unmatched_labels.push_back (pairLabels[1]);
}
}
else
{
cout << "BAD PAIR: " << child_name << " " << count << " has " << ccount << " chip(s)" << endl;
}
}
count += 1;
//if (count >= 3) break; //TODO Remove ME
}
objects.push_back (matched_chips);
objects.push_back (unmatched_chips);
objects.push_back (matched_labels);
objects.push_back (unmatched_labels);
return objects;
}
//-----------------------------------------------------------------
// Grab all the chip files from xml and store each under its label.
// When doing infer/embed, chips will not have label so return
// vector of 1 list and empty string.
// Generate warning for chips with no label when test and train.
//-----------------------------------------------------------------
std::vector<std::vector<string>> load_chips_map (
const std::string& xml_file, std::vector<std::string>& obj_labels)
{
std::map<string,std::vector<std::string>> chips_map;
ptree tree;
boost::property_tree::read_xml (xml_file, tree,
boost::property_tree::xml_parser::trim_whitespace);
std::string mode = g_mode;
std::vector<std::vector<string>> objects; // return object
// for traing and test, add all chip filenames to map by bearID
BOOST_FOREACH(ptree::value_type& child, tree.get_child("dataset.chips"))
{
std::string child_name = child.first;
if (child_name == "chip")
{
ptree chip = child.second;
std::string chipfile = child.second.get<std::string>("<xmlattr>.file");
std::string bearID = child.second.get<std::string>("label");
if (bearID.empty() && g_mode != "embed")
{
std::cout << "Error: ignoring chipfile " << chipfile << " wwith no bearID.\n" << endl;
continue;
}
if (bearID.empty())
{
bearID = " ";
// TODO: need to support for unknown images
}
g_chips.push_back (chip);
chips_map[bearID].push_back (chipfile);
}
}
// massage map of vector to return vector of vector
std::string key;
std::vector<std::string> value;
obj_labels.clear ();
std::map<std::string, std::vector<std::string>>::iterator it;
for ( it = chips_map.begin(); it != chips_map.end(); it++ )
{
objects.push_back (it->second);
obj_labels.push_back (it->first);
}
return objects;
}
//-----------------------------------------------------------------
//-----------------------------------------------------------------
std::vector<std::vector<string>> load_chips_xml (
const std::string& xml_file, bool pair, std::vector<std::string>& obj_labels)
{
std::vector<std::vector<string>> objects; // return object
if (pair)
{
cout << "Pair file" << endl;
objects = load_pairs_map(xml_file);
}
else
{
cout << "Normal file" << endl;
objects = load_chips_map(xml_file, obj_labels);
}
return objects;
}
//-----------------------------------------------------------------
// This function takes the output of load_objects_list() as input and randomly
// selects images for training. It should also be pointed out that it's really
// important that each mini-batch contain multiple images of each person. This
// is because the metric learning algorithm needs to consider pairs of images
// that should be close (i.e. images of the same person) as well as pairs of
// images that should be far apart (i.e. images of different people) during each
// training step.
void load_mini_batch (
const size_t num_people, // how many different people to include
const size_t samples_per_id, // how many images per person to select.
dlib::rand& rnd,
const std::vector<std::vector<string>>& objs,
std::vector<matrix<rgb_pixel>>& images,
std::vector<unsigned long>& labels
)
{
images.clear();
labels.clear();
DLIB_CASSERT(num_people <= objs.size(), "The dataset doesn't have that many people in it.");
std::vector<bool> already_selected(objs.size(), false);
matrix<rgb_pixel> image;
for (size_t i = 0; i < num_people; ++i)
{
size_t id = rnd.get_random_32bit_number()%objs.size();
// don't pick a person we already added to the mini-batch
while(already_selected[id])
id = rnd.get_random_32bit_number()%objs.size();
already_selected[id] = true;
//cout << "Rnd ID: " << id << endl;
for (size_t j = 0; j < samples_per_id; ++j)
{
const auto& obj = objs[id][rnd.get_random_32bit_number()%objs[id].size()];
load_image(image, obj);
images.push_back(std::move(image));
labels.push_back(id);
}
}
// You might want to do some data augmentation at this point. Here we do some simple
// color augmentation.
for (auto&& crop : images)
{
disturb_colors(crop,rnd);
// Jitter most crops
if (rnd.get_random_double() > 0.1)
crop = my_jitter_image(crop,rnd);
}
// All the images going into a mini-batch have to be the same size. And really, all
// the images in your entire training dataset should be the same size for what we are
// doing to make the most sense.
DLIB_CASSERT(images.size() > 0);
for (auto&& img : images)
{
DLIB_CASSERT(img.nr() == images[0].nr() && img.nc() == images[0].nc(),
"All the images in a single mini-batch must be the same size.");
}
}
// ----------------------------------------------------------------------------------------
// The next page of code defines a ResNet network. It's basically copied
// and pasted from the dnn_imagenet_ex.cpp example, except we replaced the loss
// layer with loss_metric and make the network somewhat smaller.
template <template <int,template<typename>class,int,typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual = add_prev1<block<N,BN,1,tag1<SUBNET>>>;
template <template <int,template<typename>class,int,typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual_down = add_prev2<avg_pool<2,2,2,2,skip1<tag2<block<N,BN,2,tag1<SUBNET>>>>>>;
template <int N, template <typename> class BN, int stride, typename SUBNET>
using block = BN<con<N,3,3,1,1,relu<BN<con<N,3,3,stride,stride,SUBNET>>>>>;
template <int N, typename SUBNET> using res = relu<residual<block,N,bn_con,SUBNET>>;
template <int N, typename SUBNET> using ares = relu<residual<block,N,affine,SUBNET>>;
template <int N, typename SUBNET> using res_down = relu<residual_down<block,N,bn_con,SUBNET>>;
template <int N, typename SUBNET> using ares_down = relu<residual_down<block,N,affine,SUBNET>>;
// ----------------------------------------------------------------------------------------
template <typename SUBNET> using level0 = res_down<256,SUBNET>;
template <typename SUBNET> using level1 = res<256,res<256,res_down<256,SUBNET>>>;
template <typename SUBNET> using level2 = res<128,res<128,res_down<128,SUBNET>>>;
template <typename SUBNET> using level3 = res<64,res<64,res<64,res_down<64,SUBNET>>>>;
template <typename SUBNET> using level4 = res<32,res<32,res<32,SUBNET>>>;
template <typename SUBNET> using alevel0 = ares_down<256,SUBNET>;
template <typename SUBNET> using alevel1 = ares<256,ares<256,ares_down<256,SUBNET>>>;
template <typename SUBNET> using alevel2 = ares<128,ares<128,ares_down<128,SUBNET>>>;
template <typename SUBNET> using alevel3 = ares<64,ares<64,ares<64,ares_down<64,SUBNET>>>>;
template <typename SUBNET> using alevel4 = ares<32,ares<32,ares<32,SUBNET>>>;
// training network type
using net_type = loss_metric<fc_no_bias<128,avg_pool_everything<
level0<
level1<
level2<
level3<
level4<
max_pool<3,3,2,2,relu<bn_con<con<32,7,7,2,2,
input_rgb_image
>>>>>>>>>>>>;
// training network type of size 150
using net_type_150 = loss_metric<fc_no_bias<128,avg_pool_everything<
level0<
level1<
level2<
level3<
level4<
max_pool<3,3,2,2,relu<bn_con<con<32,7,7,2,2,
input_rgb_image_sized<150>
>>>>>>>>>>>>;
// testing network type (replaced batch normalization with fixed affine transforms)
using anet_type = loss_metric<fc_no_bias<128,avg_pool_everything<
alevel0<
alevel1<
alevel2<
alevel3<
alevel4<
max_pool<3,3,2,2,relu<affine<con<32,7,7,2,2,
input_rgb_image
>>>>>>>>>>>>;
// testing network type of size 150
using anet_type_150 = loss_metric<fc_no_bias<128,avg_pool_everything<
alevel0<
alevel1<
alevel2<
alevel3<
alevel4<
max_pool<3,3,2,2,relu<affine<con<32,7,7,2,2,
input_rgb_image_sized<150>
>>>>>>>>>>>>;
int find_chip_index (
std::vector <ptree> chips, std::string chip_name)
{
for (int i=0 ; i < chips.size (); ++i)
{
std::string chipfile = chips[i].get<std::string>("<xmlattr>.file");
if (chip_name == chipfile)
return i;
}
return -1;
}
// ----------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
try
{
time_t timeStart = time(NULL);
command_line_parser parser;
cout << "Start time: " << timeStart << endl;
parser.add_option("h","Display this help message.");
parser.add_option("train","Train the face embedding network. Writes to network file.");
// --test <network>
parser.add_option("test","Test the face embedding network. Takes trained network", 1);
parser.add_option("pair","Interpret xml_file as a pair file, used for testing");
// --embed <network>
parser.add_option("embed","Create the face embedding for each image.", 1);
// --pretrain only used with --train
parser.add_option("pretrain","Specifies data to initialize the network.",1);
// --output: <trained_network> with --train; <embed_directory> with --embed
parser.add_option("output","Used with train, specifies trained weights file. Use with -embed, specifies directory to put embeddings. Defaults to local.",1);
// --bn not used with --pretrain
parser.add_option("bn","Use batch norm, not affine net");
// --network_steps <num_iterations>. defaults to 2000
parser.add_option("network_steps","Steps before writing to new network file [default = 2000]",1);
// --threshold <num_iterations>. defaults to 10000
parser.add_option("threshold","Iterations without progess before stopping [default = 10000]",1);
// --numid <count_per_minibatch> . defaults to 5
parser.add_option("numid","Number if IDs used per mini batch [default = 5]",1);
parser.add_option("numface","Number of face images used per ID [default = 5]",1);
// --root <chip_root_path> . defaults to /home/data/bears/faceDogHip
parser.add_option("root","Root of chip files, used in extracting embed file path.", 1);
parser.add_option("roc","Generate ROC Curve data");
parser.parse(argc, argv);
// Now we do a little command line validation. Each of the following functions
// checks something and throws an exception if the test fails.
const char* one_time_opts[] = {"h", "train", "test", "embed"};
parser.check_one_time_options(one_time_opts); // Can't give an option more than once
if (parser.option("h"))
{
cout << "Usage: bearembed [options] <xml_file>\n";
parser.print_options();
return EXIT_SUCCESS;
}
if (parser.number_of_arguments() == 0)
{
cout << "Give a xml chip file as input. It should list chips and labels to be" << endl;
cout << "used to learn to distinguish between these labels with metric learning. " << endl;
cout << "For example:" << endl;
cout << " ./bearembed <xml_file>" << endl;
return 1;
}
cout << "\nXML chip file.... : " << parser[0] << endl;
std::vector<string> obj_labels;
// if train or test, load chips with labels
// if infer, has no labels, load list of chips
if (parser.option ("embed"))
g_mode = "embed";
else if (parser.option ("test"))
g_mode = "test";
else if (parser.option ("train"))
g_mode = "train";
auto objs = load_chips_xml (parser[0], parser.option("pair"), obj_labels);
cout << "objs.size(): "<< objs.size() << endl;
// auto objs = load_objects_list(parser[0]);
// cout << "objs.size(): "<< objs.size() << endl;
// Id x Face parameters: example 5x5, face recognition 35x15
int numid = get_option (parser, "numid", 5); // how many different people to include
int numface = get_option (parser, "numface", 5); // how many images per person to select
cout << "numid: " << numid << endl;
cout << "numface: " << numface << endl;
std::vector<matrix<rgb_pixel>> images;
std::vector<unsigned long> labels;
// Training
if (parser.option("train"))
{
anet_type_150 anet_150;
net_type_150 net_150;
net_type net;
cout << "Start training..." << endl;
std::string sync_file;
std::string trained_network;
if (parser.option ("output"))
{
trained_network = parser.option("output").argument();
}
else
{
trained_network = "bears.dat";
}
cout << "Output: " << trained_network << endl;
int threshold = get_option (parser, "threshold", 10000);
cout << "Threshold: " << threshold << endl;
if (parser.option("pretrain")) // assumes size 150
{
cout << ".. using pretrain weights ..." << endl;
std::string pretrained_file = parser.option("pretrain").argument();
cout << "Load network: " << pretrained_file << endl;
if (parser.option("bn"))
{ // ------- BATCHNORM, NO -ANET ---------------
deserialize(pretrained_file) >> net_150;
dnn_trainer<net_type_150> trainer(net_150, sgd(0.0001, 0.9));
cout << "...... no anet ......." << endl;
trainer.set_learning_rate(0.001);
sync_file= "pretrained_no_anet_sync";
trainer.be_verbose();
trainer.set_synchronization_file(sync_file, std::chrono::minutes(5));
trainer.set_iterations_without_progress_threshold(threshold);
dlib::pipe<std::vector<matrix<rgb_pixel>>> qimages(4);
dlib::pipe<std::vector<unsigned long>> qlabels(4);
auto data_loader = [&numid, &numface, &qimages, &qlabels, &objs](time_t seed)
{
dlib::rand rnd(time(0)+seed);
std::vector<matrix<rgb_pixel>> images;
std::vector<unsigned long> labels;
while(qimages.is_enabled())
{
try
{
load_mini_batch(numid, numface, rnd, objs, images, labels);
qimages.enqueue(images);
qlabels.enqueue(labels);
}
catch(std::exception& e)
{
cout << "EXCEPTION IN LOADING DATA" << endl;
cout << e.what() << endl;
}
}
};
std::thread data_loader1([data_loader](){ data_loader(1); });
std::thread data_loader2([data_loader](){ data_loader(2); });
std::thread data_loader3([data_loader](){ data_loader(3); });
std::thread data_loader4([data_loader](){ data_loader(4); });
std::thread data_loader5([data_loader](){ data_loader(5); });
while(trainer.get_learning_rate() >= 1e-4)
{
qimages.dequeue(images);
qlabels.dequeue(labels);
trainer.train_one_step(images, labels);
cout << "Step: " << trainer.get_train_one_step_calls() << " Loss: " << trainer.get_average_loss() << endl;
}
trainer.get_net();
cout << "done training" << endl;
net_150.clean();
anet_150 = net_150;
serialize(trained_network) << anet_150;
qimages.disable();
qlabels.disable();
data_loader1.join();
data_loader2.join();
data_loader3.join();
data_loader4.join();
data_loader5.join();
} // ------- BATCHNORM, NO -ANET ---------------
else
{ // ------- ANET ---------------
cout << "...... using anet ......." << endl;
deserialize(pretrained_file) >> anet_150;
dnn_trainer<anet_type_150> trainer(anet_150, sgd(0.0001, 0.9));
trainer.set_learning_rate(0.001);
sync_file= "pretrained_anet_sync";
trainer.be_verbose();
trainer.set_synchronization_file(sync_file, std::chrono::minutes(5));
trainer.set_iterations_without_progress_threshold(threshold);
dlib::pipe<std::vector<matrix<rgb_pixel>>> qimages(4);
dlib::pipe<std::vector<unsigned long>> qlabels(4);
auto data_loader = [&numid, &numface, &qimages, &qlabels, &objs](time_t seed)
{
dlib::rand rnd(time(0)+seed);
std::vector<matrix<rgb_pixel>> images;
std::vector<unsigned long> labels;
while(qimages.is_enabled())
{
try
{
load_mini_batch(numid, numface, rnd, objs, images, labels);
qimages.enqueue(images);
qlabels.enqueue(labels);
}
catch(std::exception& e)
{
cout << "EXCEPTION IN LOADING DATA" << endl;
cout << e.what() << endl;
}
}
};
std::thread data_loader1([data_loader](){ data_loader(1); });
std::thread data_loader2([data_loader](){ data_loader(2); });
std::thread data_loader3([data_loader](){ data_loader(3); });
std::thread data_loader4([data_loader](){ data_loader(4); });
std::thread data_loader5([data_loader](){ data_loader(5); });
int step_count=1;
int network_count=1;
int network_step_size = 2000;
if (parser.option("network_steps"))
network_step_size = std::stoi (parser.option("network_steps").argument());
while(trainer.get_learning_rate() >= 1e-4)
{
qimages.dequeue(images);
qlabels.dequeue(labels);
trainer.train_one_step(images, labels);
cout << "Step: " << trainer.get_train_one_step_calls() << " Loss: " << trainer.get_average_loss() << endl;
// generating occasional networks to create learning curve
if (parser.option("network_steps"))
{
if (step_count % network_step_size == 0)
{
trainer.get_net();
std::string intermediate_network = trained_network+std::to_string(network_count);
cout << "writing network " << intermediate_network << endl;
anet_150.clean();
serialize(intermediate_network) << anet_150;
network_count++;
}
}
step_count++;
}
trainer.get_net();
cout << "done training" << endl;
anet_150.clean();
serialize(trained_network) << anet_150;
qimages.disable();
qlabels.disable();
data_loader1.join();
data_loader2.join();
data_loader3.join();
data_loader4.join();
data_loader5.join();
} // ------- ANET ---------------
}
else //------------ NO PRETRAINED DATA ------
{
cout << "... using random weights ....." << endl;
dnn_trainer<net_type> trainer(net, sgd(0.0001, 0.9));
trainer.set_learning_rate(0.1);
sync_file= "rand_sync";
trainer.be_verbose();
trainer.set_synchronization_file(sync_file, std::chrono::minutes(5));
trainer.set_iterations_without_progress_threshold(threshold);
dlib::pipe<std::vector<matrix<rgb_pixel>>> qimages(4);
dlib::pipe<std::vector<unsigned long>> qlabels(4);
auto data_loader = [&numid, &numface, &qimages, &qlabels, &objs](time_t seed)
{
dlib::rand rnd(time(0)+seed);
std::vector<matrix<rgb_pixel>> images;
std::vector<unsigned long> labels;
while(qimages.is_enabled())
{
try
{
load_mini_batch(numid, numface, rnd, objs, images, labels);
qimages.enqueue(images);
qlabels.enqueue(labels);
}
catch(std::exception& e)
{
cout << "EXCEPTION IN LOADING DATA" << endl;
cout << e.what() << endl;
}
}
};
std::thread data_loader1([data_loader](){ data_loader(1); });
std::thread data_loader2([data_loader](){ data_loader(2); });
std::thread data_loader3([data_loader](){ data_loader(3); });
std::thread data_loader4([data_loader](){ data_loader(4); });
std::thread data_loader5([data_loader](){ data_loader(5); });
while(trainer.get_learning_rate() >= 1e-4)
{
qimages.dequeue(images);
qlabels.dequeue(labels);
trainer.train_one_step(images, labels);
cout << "Step: " << trainer.get_train_one_step_calls() << " Loss: " << trainer.get_average_loss() << endl;
}
trainer.get_net();
cout << "done training" << endl;
net.clean();
serialize(trained_network) << net;
qimages.disable();
qlabels.disable();
data_loader1.join();
data_loader2.join();
data_loader3.join();
data_loader4.join();
data_loader5.join();
}
/*
// ----------- START SECTION REPLICATED 3X ABOVE --------------------
// I've set this to something really small to make the example terminate
// sooner. But when you really want to train a good model you should set
// this to something like 10000 so training doesn't terminate too early.
//trainer.set_iterations_without_progress_threshold(300);
// -- trainer.set_iterations_without_progress_threshold(10000);
// If you have a lot of data then it might not be reasonable to load it all
// into RAM. So you will need to be sure you are decompressing your images
// and loading them fast enough to keep the GPU occupied. I like to do this
// using the following coding pattern: create a bunch of threads that dump
// mini-batches into dlib::pipes.
dlib::pipe<std::vector<matrix<rgb_pixel>>> qimages(4);
dlib::pipe<std::vector<unsigned long>> qlabels(4);
auto data_loader = [&qimages, &qlabels, &objs](time_t seed)
{
dlib::rand rnd(time(0)+seed);
std::vector<matrix<rgb_pixel>> images;
std::vector<unsigned long> labels;
while(qimages.is_enabled())
{
try
{
load_mini_batch(numid, numface, rnd, objs, images, labels);
// Tried 35x10, trained much slower and did worse on test (86%; training was 100%)
qimages.enqueue(images);
qlabels.enqueue(labels);
}
catch(std::exception& e)
{
cout << "EXCEPTION IN LOADING DATA" << endl;
cout << e.what() << endl;
}
}
};
// Run the data_loader from 5 threads. You should set the number of threads
// relative to the number of CPU cores you have.
std::thread data_loader1([data_loader](){ data_loader(1); });
std::thread data_loader2([data_loader](){ data_loader(2); });
std::thread data_loader3([data_loader](){ data_loader(3); });
std::thread data_loader4([data_loader](){ data_loader(4); });
std::thread data_loader5([data_loader](){ data_loader(5); });
// Here we do the training. We keep passing mini-batches to the trainer until the
// learning rate has dropped low enough.
while(trainer.get_learning_rate() >= 1e-4)
{
qimages.dequeue(images);
qlabels.dequeue(labels);
trainer.train_one_step(images, labels);
cout << "Step: " << trainer.get_train_one_step_calls() << " Loss: " << trainer.get_average_loss() << endl;
}
// Wait for training threads to stop
trainer.get_net();
cout << "done training" << endl;
// Save the network to disk
if (parser.option("pretrained_file"))
{
if (parser.option("anet"))
{
anet_150.clean();
serialize(trained_network) << anet_150;
}
else
{
net_150.clean();
serialize(trained_network) << net_150;
}
}
else
{
net.clean();
serialize(trained_network) << net;
}
// stop all the data loading threads and wait for them to terminate.
qimages.disable();
qlabels.disable();
data_loader1.join();
data_loader2.join();
data_loader3.join();
data_loader4.join();
data_loader5.join();
// ----------- END SECTION REPLICATED 3X ABOVE --------------------
*/
}
// Testing pairs
else if (parser.option("test") && parser.option("pair"))
{
anet_type_150 anet_150;
anet_type_150 testing_net;
net_type net;
// anet_type testing_net;
std::string test_network = (parser.option("test").argument());
// boost::filesystem::path test_network(parser.option("train").argument());
if (parser.option("bn"))
{ // ------- BATCHNORM, NOT ANET ----------
cout << "... not anet ..." << endl;
deserialize(test_network) >> net;
testing_net = net;
} // ------- BATCHNORM, NOT ANET ----------
else
{ // ------- ANET ----------
cout << "... using anet ..." << endl;
deserialize(test_network) >> testing_net;
} // ------- ANET ----------
cout << "Start testing pairs..." << endl;
std::vector<string> matchedPairs = objs[0];
std::vector<string> unmatchedPairs = objs[1];
std::vector<string> matchedLabels = objs[2];
std::vector<string> unmatchedLabels = objs[3];
std::vector<double> true_dets, false_dets;
int num_right = 0;
int num_wrong = 0;
int num_true_pos = 0;
int num_true_neg = 0;
int num_false_pos = 0;
int num_false_neg = 0;
matrix<rgb_pixel> image;
std::vector<matrix<rgb_pixel>> images;
// set up file name for result file
time_t rawtime;
struct tm * timeinfo;
char time_buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(time_buffer,sizeof(time_buffer),"%Y%m%d%I%M",timeinfo);
std::string result_filename = "test_result_";
result_filename.append (time_buffer);
ofstream result_file;
result_file.open (result_filename);
// Matching Pairs
double dist_max = testing_net.loss_details().get_distance_threshold();
for (size_t i = 0; i < matchedPairs.size(); i+=2)
{
result_file << matchedLabels[i] << "," << matchedLabels[i+1];
images.clear();
//cout << "Matched Pair " << i/2 << endl;
//cout << matchedPairs[i] << endl;
load_image(image, matchedPairs[i]);
images.push_back(std::move(image));
//cout << matchedPairs[i+1] << endl;
load_image(image, matchedPairs[i+1]);
images.push_back(std::move(image));
std::vector<matrix<float,0,1>> matchedEmbedded = testing_net(images);
double images_dist = length(matchedEmbedded[0]-matchedEmbedded[1]);
true_dets.push_back(images_dist);
if (images_dist < dist_max)
{
++num_right;
++num_true_pos;
result_file << ",1,1,";
//cout << "RIGHT (same) " << matchedPairs[i] << " to " << matchedPairs[i+1] << " distance is " << to_string(length(matchedEmbedded[0]-matchedEmbedded[1])) << endl;
}
else
{
++num_wrong;
++num_false_neg;
result_file << ",0,1,";
//cout << "WRONG (same) " << matchedPairs[i] << " to " << matchedPairs[i+1] << " distance is " << to_string(length(matchedEmbedded[0]-matchedEmbedded[1])) << endl;
}
result_file << matchedPairs[i] << "," << matchedPairs[i+1];
result_file << "," << images_dist << "," << dist_max << endl;
}
// Unmatching Pairs
for (size_t i = 0; i < unmatchedPairs.size(); i+=2)
{
result_file << unmatchedLabels[i] << "," << unmatchedLabels[i+1];
images.clear();
//cout << "Unmatched Pair " << i/2 << endl;
//cout << unmatchedPairs[i] << endl;
load_image(image, unmatchedPairs[i]);
images.push_back(std::move(image));
//cout << unmatchedPairs[i+1] << endl;
load_image(image, unmatchedPairs[i+1]);
images.push_back(std::move(image));
std::vector<matrix<float,0,1>> unmatchedEmbedded = testing_net(images);
double images_dist = length(unmatchedEmbedded[0]-unmatchedEmbedded[1]);
false_dets.push_back(images_dist);
if (images_dist >= dist_max)
{
++num_right;
++num_true_neg;
result_file << ",0,0,";
//cout << "RIGHT (same) " << unmatchedPairs[i] << " to " << unmatchedPairs[i+1] << " distance is " << to_string(length(unmatchedEmbedded[0]-unmatchedEmbedded[1])) << endl;
}
else
{
++num_wrong;
++num_false_pos;
result_file << ",1,0,";
//cout << "WRONG (same) " << unmatchedPairs[i] << " to " << unmatchedPairs[i+1] << " distance is " << to_string(length(unmatchedEmbedded[0]-unmatchedEmbedded[1])) << endl;
}
result_file << unmatchedPairs[i] << "," << unmatchedPairs[i+1];
result_file << "," << images_dist << "," << dist_max << endl;
}
result_file.close ();
cout << "num_right: "<< num_right << endl;
cout << "num_wrong: "<< num_wrong << endl;
cout << "num_true_pos: "<< num_true_pos << endl;
cout << "num_true_neg: "<< num_true_neg << endl;
cout << "num_false_pos: "<< num_false_pos << endl;
cout << "num_false_neg: "<< num_false_neg << endl;