This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
/
io.cpp
834 lines (777 loc) · 28.3 KB
/
io.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
/*
All modification made by Intel Corporation: © 2016 Intel Corporation
All contributions by the University of California:
Copyright (c) 2014, 2015, The Regents of the University of California (Regents)
All rights reserved.
All other contributions:
Copyright (c) 2014, 2015, the respective contributors
All rights reserved.
For the list of contributors go to https://github.com/BVLC/caffe/blob/master/CONTRIBUTORS.md
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <fcntl.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc.hpp>
#endif // USE_OPENCV
#include <stdint.h>
#include <algorithm>
#include <fstream> // NOLINT(readability/streams)
#include <map>
#include <string>
#include <vector>
#if defined(_MSC_EXTENSIONS)
#include <io.h>
#endif
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/io.hpp"
const int kProtoReadBytesLimit = INT_MAX; // Max size of 2 GB minus 1 byte.
namespace caffe {
using namespace boost::property_tree; // NOLINT(build/namespaces)
using google::protobuf::io::FileInputStream;
using google::protobuf::io::FileOutputStream;
using google::protobuf::io::ZeroCopyInputStream;
using google::protobuf::io::CodedInputStream;
using google::protobuf::io::ZeroCopyOutputStream;
using google::protobuf::io::CodedOutputStream;
using google::protobuf::Message;
bool ReadProtoFromTextFile(const char* filename, Message* proto) {
#if defined(_MSC_EXTENSIONS)
int fd = _open(filename, O_RDONLY);
#else
int fd = open(filename, O_RDONLY);
#endif
CHECK_NE(fd, -1) << "File not found: " << filename;
FileInputStream* input = new FileInputStream(fd);
bool success = google::protobuf::TextFormat::Parse(input, proto);
delete input;
#if defined(_MSC_EXTENSIONS)
_close(fd);
#else
close(fd);
#endif
return success;
}
void WriteProtoToTextFile(const Message& proto, const char* filename) {
#if defined(_MSC_EXTENSIONS)
int fd = _open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
#else
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
#endif
FileOutputStream* output = new FileOutputStream(fd);
CHECK(google::protobuf::TextFormat::Print(proto, output));
delete output;
#if defined(_MSC_EXTENSIONS)
_close(fd);
#else
close(fd);
#endif
}
bool ReadProtoFromBinaryFile(const char* filename, Message* proto) {
#if defined(_MSC_EXTENSIONS)
int fd = _open(filename, O_RDONLY);
#else
int fd = open(filename, O_RDONLY);
#endif
CHECK_NE(fd, -1) << "File not found: " << filename;
ZeroCopyInputStream* raw_input = new FileInputStream(fd);
CodedInputStream* coded_input = new CodedInputStream(raw_input);
coded_input->SetTotalBytesLimit(kProtoReadBytesLimit, 536870912);
bool success = proto->ParseFromCodedStream(coded_input);
delete coded_input;
delete raw_input;
#if defined(_MSC_EXTENSIONS)
_close(fd);
#else
close(fd);
#endif
return success;
}
void WriteProtoToBinaryFile(const Message& proto, const char* filename) {
fstream output(filename, ios::out | ios::trunc | ios::binary);
CHECK(proto.SerializeToOstream(&output));
}
#ifdef USE_OPENCV
cv::Mat ReadImageToCVMat(const string& filename,
const int height, const int width, const bool is_color) {
cv::Mat cv_img;
int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat cv_img_origin = cv::imread(filename, cv_read_flag);
if (!cv_img_origin.data) {
LOG(ERROR) << "Could not open or find file " << filename;
return cv_img_origin;
}
if (height > 0 && width > 0) {
cv::resize(cv_img_origin, cv_img, cv::Size(width, height));
} else {
cv_img = cv_img_origin;
}
return cv_img;
}
cv::Mat ReadImageToCVMat(const string& filename,
const int height, const int width, const bool is_color,
const int min_height, const int min_width) {
cv::Mat cv_img;
int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat cv_img_origin = cv::imread(filename, cv_read_flag);
if (!cv_img_origin.data) {
LOG(ERROR) << "Could not open or find file " << filename;
return cv_img_origin;
}
if (height > 0 && width > 0) {
cv::resize(cv_img_origin, cv_img, cv::Size(width, height));
} else if (cv_img_origin.rows < min_height || cv_img_origin.cols < min_width){
bool resize_width = (min_width - cv_img_origin.cols > min_height - cv_img_origin.rows)
? true: false;
if (resize_width) {
int height_aspect_ratio = float(min_width)*(float(cv_img_origin.rows)/float(cv_img_origin.cols)) + 0.5;
cv::resize(cv_img_origin, cv_img, cv::Size(min_width, height_aspect_ratio));
} else {
int width_aspect_ratio = float(min_height)*(float(cv_img_origin.cols)/float(cv_img_origin.rows)) + 0.5;
cv::resize(cv_img_origin, cv_img, cv::Size(width_aspect_ratio , min_height));
}
} else {
cv_img = cv_img_origin;
}
return cv_img;
}
cv::Mat ReadImageToCVMat(const string& filename,
const int height, const int width) {
return ReadImageToCVMat(filename, height, width, true);
}
cv::Mat ReadImageToCVMat(const string& filename,
const bool is_color) {
return ReadImageToCVMat(filename, 0, 0, is_color);
}
cv::Mat ReadImageToCVMat(const string& filename) {
return ReadImageToCVMat(filename, 0, 0, true);
}
// Do the file extension and encoding match?
static bool matchExt(const std::string & fn,
std::string en) {
size_t p = fn.rfind('.') + 1;
std::string ext = p != fn.npos ? fn.substr(p) : fn;
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
std::transform(en.begin(), en.end(), en.begin(), ::tolower);
if ( ext == en )
return true;
if ( en == "jpg" && ext == "jpeg" )
return true;
return false;
}
bool ReadImageToDatum(const string& filename, const int label,
const int height, const int width, const bool is_color,
const std::string & encoding, Datum* datum) {
if (!encoding.size()) {
cv::Mat cv_img = ReadImageToCVMat(filename, height, width, is_color);
if (cv_img.data) {
CVMatToDatum(cv_img, datum);
datum->set_label(label);
return true;
} else {
return false;
}
} else {
cv::Mat cv_img = cv::imread(filename, -1);
if (!cv_img.data) {
LOG(ERROR) << "Could not open or find file " << filename;
return false;
}
bool is_img_grayscale = cv_img.channels() == 1;
bool is_img_bgr = cv_img.channels() == 3;
bool is_img_bgra = cv_img.channels() == 4;
if ( !(is_img_grayscale || is_img_bgr || is_img_bgra) ) {
LOG(ERROR) << "Images with " << cv_img.channels() <<
" channels unsupported: " << filename;
return false;
}
bool need_convert = is_img_bgra || (is_img_grayscale == is_color);
bool need_resize = height > 0 && width > 0;
if (!need_convert && !need_resize && matchExt(filename, encoding)) {
datum->set_channels(cv_img.channels());
datum->set_height(cv_img.rows);
datum->set_width(cv_img.cols);
return ReadFileToDatum(filename, label, datum);
}
if (need_resize)
cv::resize(cv_img, cv_img, cv::Size(width, height));
if (need_convert) {
int conv_code =
(is_img_grayscale && is_color) ? cv::COLOR_GRAY2BGR
: (is_img_bgr && !is_color) ? cv::COLOR_BGR2GRAY
: (is_img_bgra && is_color) ? cv::COLOR_BGRA2BGR
: (is_img_bgra && !is_color) ? cv::COLOR_BGRA2GRAY
: -1;
cv::cvtColor(cv_img, cv_img, conv_code);
}
std::vector<uchar> buf;
cv::imencode("."+encoding, cv_img, buf);
datum->set_data(std::string(reinterpret_cast<char*>(&buf[0]),
buf.size()));
datum->set_label(label);
datum->set_encoded(true);
return true;
}
}
void GetImageSize(const string& filename, int* height, int* width) {
cv::Mat cv_img = cv::imread(filename);
if (!cv_img.data) {
LOG(ERROR) << "Could not open or find file " << filename;
return;
}
*height = cv_img.rows;
*width = cv_img.cols;
}
bool ReadRichImageToAnnotatedDatum(const string& filename,
const string& labelfile, const int height, const int width,
const int min_dim, const int max_dim, const bool is_color,
const string& encoding, const AnnotatedDatum_AnnotationType type,
const string& labeltype, const std::map<string, int>& name_to_label,
AnnotatedDatum* anno_datum) {
// Read image to datum.
bool status = ReadImageToDatum(filename, -1, height, width,
is_color, encoding,
anno_datum->mutable_datum());
if (status == false) {
return status;
}
anno_datum->clear_annotation_group();
if (!boost::filesystem::exists(labelfile)) {
return true;
}
switch (type) {
case AnnotatedDatum_AnnotationType_BBOX:
int ori_height, ori_width;
GetImageSize(filename, &ori_height, &ori_width);
if (labeltype == "xml") {
return ReadXMLToAnnotatedDatum(labelfile, ori_height, ori_width,
name_to_label, anno_datum);
} else if (labeltype == "json") {
return ReadJSONToAnnotatedDatum(labelfile, ori_height, ori_width,
name_to_label, anno_datum);
} else if (labeltype == "txt") {
return ReadTxtToAnnotatedDatum(labelfile, ori_height, ori_width,
anno_datum);
} else {
LOG(FATAL) << "Unknown label file type.";
return false;
}
break;
default:
LOG(FATAL) << "Unknown annotation type.";
return false;
}
}
#endif // USE_OPENCV
bool ReadFileToDatum(const string& filename, const int label,
Datum* datum) {
std::streampos size;
fstream file(filename.c_str(), ios::in|ios::binary|ios::ate);
if (file.is_open()) {
size = file.tellg();
std::string buffer(size, ' ');
file.seekg(0, ios::beg);
file.read(&buffer[0], size);
file.close();
datum->set_data(buffer);
datum->set_label(label);
datum->set_encoded(true);
return true;
} else {
return false;
}
}
// Parse VOC/ILSVRC detection annotation.
bool ReadXMLToAnnotatedDatum(const string& labelfile, const int img_height,
const int img_width, const std::map<string, int>& name_to_label,
AnnotatedDatum* anno_datum) {
ptree pt;
read_xml(labelfile, pt);
// Parse annotation.
int width = 0, height = 0;
try {
height = pt.get<int>("annotation.size.height");
width = pt.get<int>("annotation.size.width");
} catch (const ptree_error &e) {
LOG(WARNING) << "When parsing " << labelfile << ": " << e.what();
height = img_height;
width = img_width;
}
LOG_IF(WARNING, height != img_height) << labelfile <<
" inconsistent image height.";
LOG_IF(WARNING, width != img_width) << labelfile <<
" inconsistent image width.";
CHECK(width != 0 && height != 0) << labelfile <<
" no valid image width/height.";
int instance_id = 0;
BOOST_FOREACH(ptree::value_type &v1, pt.get_child("annotation")) {
ptree pt1 = v1.second;
if (v1.first == "object") {
Annotation* anno = NULL;
bool difficult = false;
ptree object = v1.second;
BOOST_FOREACH(ptree::value_type &v2, object.get_child("")) {
ptree pt2 = v2.second;
if (v2.first == "name") {
string name = pt2.data();
if (name_to_label.find(name) == name_to_label.end()) {
LOG(FATAL) << "Unknown name: " << name;
}
int label = name_to_label.find(name)->second;
bool found_group = false;
for (int g = 0; g < anno_datum->annotation_group_size(); ++g) {
AnnotationGroup* anno_group =
anno_datum->mutable_annotation_group(g);
if (label == anno_group->group_label()) {
if (anno_group->annotation_size() == 0) {
instance_id = 0;
} else {
instance_id = anno_group->annotation(
anno_group->annotation_size() - 1).instance_id() + 1;
}
anno = anno_group->add_annotation();
found_group = true;
}
}
if (!found_group) {
// If there is no such annotation_group, create a new one.
AnnotationGroup* anno_group = anno_datum->add_annotation_group();
anno_group->set_group_label(label);
anno = anno_group->add_annotation();
instance_id = 0;
}
anno->set_instance_id(instance_id++);
} else if (v2.first == "difficult") {
difficult = pt2.data() == "1";
} else if (v2.first == "bndbox") {
int xmin = pt2.get("xmin", 0);
int ymin = pt2.get("ymin", 0);
int xmax = pt2.get("xmax", 0);
int ymax = pt2.get("ymax", 0);
CHECK_NOTNULL(anno);
LOG_IF(WARNING, xmin > width) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, ymin > height) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, xmax > width) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, ymax > height) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, xmin < 0) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, ymin < 0) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, xmax < 0) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, ymax < 0) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, xmin > xmax) << labelfile <<
" bounding box irregular.";
LOG_IF(WARNING, ymin > ymax) << labelfile <<
" bounding box irregular.";
// Store the normalized bounding box.
NormalizedBBox* bbox = anno->mutable_bbox();
CHECK_NOTNULL(bbox);
bbox->set_xmin(static_cast<float>(xmin) / width);
bbox->set_ymin(static_cast<float>(ymin) / height);
bbox->set_xmax(static_cast<float>(xmax) / width);
bbox->set_ymax(static_cast<float>(ymax) / height);
bbox->set_difficult(difficult);
}
}
}
}
return true;
}
// Parse MSCOCO detection annotation.
bool ReadJSONToAnnotatedDatum(const string& labelfile, const int img_height,
const int img_width, const std::map<string, int>& name_to_label,
AnnotatedDatum* anno_datum) {
ptree pt;
read_json(labelfile, pt);
// Get image info.
int width = 0, height = 0;
try {
height = pt.get<int>("image.height");
width = pt.get<int>("image.width");
} catch (const ptree_error &e) {
LOG(WARNING) << "When parsing " << labelfile << ": " << e.what();
height = img_height;
width = img_width;
}
LOG_IF(WARNING, height != img_height) << labelfile <<
" inconsistent image height.";
LOG_IF(WARNING, width != img_width) << labelfile <<
" inconsistent image width.";
CHECK(width != 0 && height != 0) << labelfile <<
" no valid image width/height.";
// Get annotation info.
int instance_id = 0;
BOOST_FOREACH(ptree::value_type& v1, pt.get_child("annotation")) {
Annotation* anno = NULL;
bool iscrowd = false;
ptree object = v1.second;
// Get category_id.
string name = object.get<string>("category_id");
if (name_to_label.find(name) == name_to_label.end()) {
LOG(FATAL) << "Unknown name: " << name;
}
int label = name_to_label.find(name)->second;
bool found_group = false;
for (int g = 0; g < anno_datum->annotation_group_size(); ++g) {
AnnotationGroup* anno_group =
anno_datum->mutable_annotation_group(g);
if (label == anno_group->group_label()) {
if (anno_group->annotation_size() == 0) {
instance_id = 0;
} else {
instance_id = anno_group->annotation(
anno_group->annotation_size() - 1).instance_id() + 1;
}
anno = anno_group->add_annotation();
found_group = true;
}
}
if (!found_group) {
// If there is no such annotation_group, create a new one.
AnnotationGroup* anno_group = anno_datum->add_annotation_group();
anno_group->set_group_label(label);
anno = anno_group->add_annotation();
instance_id = 0;
}
anno->set_instance_id(instance_id++);
// Get iscrowd.
iscrowd = object.get<int>("iscrowd", 0);
// Get bbox.
vector<float> bbox_items;
BOOST_FOREACH(ptree::value_type& v2, object.get_child("bbox")) {
bbox_items.push_back(v2.second.get_value<float>());
}
CHECK_EQ(bbox_items.size(), 4);
float xmin = bbox_items[0];
float ymin = bbox_items[1];
float xmax = bbox_items[0] + bbox_items[2];
float ymax = bbox_items[1] + bbox_items[3];
CHECK_NOTNULL(anno);
LOG_IF(WARNING, xmin > width) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, ymin > height) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, xmax > width) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, ymax > height) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, xmin < 0) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, ymin < 0) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, xmax < 0) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, ymax < 0) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, xmin > xmax) << labelfile <<
" bounding box irregular.";
LOG_IF(WARNING, ymin > ymax) << labelfile <<
" bounding box irregular.";
// Store the normalized bounding box.
NormalizedBBox* bbox = anno->mutable_bbox();
bbox->set_xmin(xmin / width);
bbox->set_ymin(ymin / height);
bbox->set_xmax(xmax / width);
bbox->set_ymax(ymax / height);
bbox->set_difficult(iscrowd);
}
return true;
}
// Parse plain txt detection annotation: label_id, xmin, ymin, xmax, ymax.
bool ReadTxtToAnnotatedDatum(const string& labelfile, const int height,
const int width, AnnotatedDatum* anno_datum) {
std::ifstream infile(labelfile.c_str());
if (!infile.good()) {
LOG(INFO) << "Cannot open " << labelfile;
return false;
}
int label;
float xmin, ymin, xmax, ymax;
while (infile >> label >> xmin >> ymin >> xmax >> ymax) {
Annotation* anno = NULL;
int instance_id = 0;
bool found_group = false;
for (int g = 0; g < anno_datum->annotation_group_size(); ++g) {
AnnotationGroup* anno_group = anno_datum->mutable_annotation_group(g);
if (label == anno_group->group_label()) {
if (anno_group->annotation_size() == 0) {
instance_id = 0;
} else {
instance_id = anno_group->annotation(
anno_group->annotation_size() - 1).instance_id() + 1;
}
anno = anno_group->add_annotation();
found_group = true;
}
}
if (!found_group) {
// If there is no such annotation_group, create a new one.
AnnotationGroup* anno_group = anno_datum->add_annotation_group();
anno_group->set_group_label(label);
anno = anno_group->add_annotation();
instance_id = 0;
}
anno->set_instance_id(instance_id++);
LOG_IF(WARNING, xmin > width) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, ymin > height) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, xmax > width) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, ymax > height) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, xmin < 0) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, ymin < 0) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, xmax < 0) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, ymax < 0) << labelfile <<
" bounding box exceeds image boundary.";
LOG_IF(WARNING, xmin > xmax) << labelfile <<
" bounding box irregular.";
LOG_IF(WARNING, ymin > ymax) << labelfile <<
" bounding box irregular.";
// Store the normalized bounding box.
NormalizedBBox* bbox = anno->mutable_bbox();
bbox->set_xmin(xmin / width);
bbox->set_ymin(ymin / height);
bbox->set_xmax(xmax / width);
bbox->set_ymax(ymax / height);
bbox->set_difficult(false);
}
return true;
}
bool ReadLabelFileToLabelMap(const string& filename, bool include_background,
const string& delimiter, LabelMap* map) {
// cleanup
map->Clear();
std::ifstream file(filename.c_str());
string line;
// Every line can have [1, 3] number of fields.
// The delimiter between fields can be one of " :;".
// The order of the fields are:
// name [label] [display_name]
// ...
int field_size = -1;
int label = 0;
LabelMapItem* map_item;
// Add background (none_of_the_above) class.
if (include_background) {
map_item = map->add_item();
map_item->set_name("none_of_the_above");
map_item->set_label(label++);
map_item->set_display_name("background");
}
while (std::getline(file, line)) {
vector<string> fields;
fields.clear();
boost::split(fields, line, boost::is_any_of(delimiter));
if (field_size == -1) {
field_size = fields.size();
} else {
CHECK_EQ(field_size, fields.size())
<< "Inconsistent number of fields per line.";
}
map_item = map->add_item();
map_item->set_name(fields[0]);
switch (field_size) {
case 1:
map_item->set_label(label++);
map_item->set_display_name(fields[0]);
break;
case 2:
label = std::atoi(fields[1].c_str());
map_item->set_label(label);
map_item->set_display_name(fields[0]);
break;
case 3:
label = std::atoi(fields[1].c_str());
map_item->set_label(label);
map_item->set_display_name(fields[2]);
break;
default:
LOG(FATAL) << "The number of fields should be [1, 3].";
break;
}
}
return true;
}
bool MapNameToLabel(const LabelMap& map, const bool strict_check,
std::map<string, int>* name_to_label) {
// cleanup
name_to_label->clear();
for (int i = 0; i < map.item_size(); ++i) {
const string& name = map.item(i).name();
const int label = map.item(i).label();
if (strict_check) {
if (!name_to_label->insert(std::make_pair(name, label)).second) {
LOG(FATAL) << "There are many duplicates of name: " << name;
return false;
}
} else {
(*name_to_label)[name] = label;
}
}
return true;
}
bool MapLabelToName(const LabelMap& map, const bool strict_check,
std::map<int, string>* label_to_name) {
// cleanup
label_to_name->clear();
for (int i = 0; i < map.item_size(); ++i) {
const string& name = map.item(i).name();
const int label = map.item(i).label();
if (strict_check) {
if (!label_to_name->insert(std::make_pair(label, name)).second) {
LOG(FATAL) << "There are many duplicates of label: " << label;
return false;
}
} else {
(*label_to_name)[label] = name;
}
}
return true;
}
bool MapLabelToDisplayName(const LabelMap& map, const bool strict_check,
std::map<int, string>* label_to_display_name) {
// cleanup
label_to_display_name->clear();
for (int i = 0; i < map.item_size(); ++i) {
const string& display_name = map.item(i).display_name();
const int label = map.item(i).label();
if (strict_check) {
if (!label_to_display_name->insert(
std::make_pair(label, display_name)).second) {
LOG(FATAL) << "There are many duplicates of label: " << label;
return false;
}
} else {
(*label_to_display_name)[label] = display_name;
}
}
return true;
}
#ifdef USE_OPENCV
cv::Mat DecodeDatumToCVMatNative(const Datum& datum) {
cv::Mat cv_img;
CHECK(datum.encoded()) << "Datum not encoded";
const string& data = datum.data();
std::vector<char> vec_data(data.c_str(), data.c_str() + data.size());
cv_img = cv::imdecode(vec_data, -1);
if (!cv_img.data) {
LOG(ERROR) << "Could not decode datum ";
}
return cv_img;
}
cv::Mat DecodeDatumToCVMat(const Datum& datum, bool is_color) {
cv::Mat cv_img;
CHECK(datum.encoded()) << "Datum not encoded";
const string& data = datum.data();
std::vector<char> vec_data(data.c_str(), data.c_str() + data.size());
int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
CV_LOAD_IMAGE_GRAYSCALE);
cv_img = cv::imdecode(vec_data, cv_read_flag);
if (!cv_img.data) {
LOG(ERROR) << "Could not decode datum ";
}
return cv_img;
}
// If Datum is encoded will decoded using DecodeDatumToCVMat and CVMatToDatum
// If Datum is not encoded will do nothing
bool DecodeDatumNative(Datum* datum) {
if (datum->encoded()) {
cv::Mat cv_img = DecodeDatumToCVMatNative((*datum));
CVMatToDatum(cv_img, datum);
return true;
} else {
return false;
}
}
bool DecodeDatum(Datum* datum, bool is_color) {
if (datum->encoded()) {
cv::Mat cv_img = DecodeDatumToCVMat((*datum), is_color);
CVMatToDatum(cv_img, datum);
return true;
} else {
return false;
}
}
void EncodeCVMatToDatum(const cv::Mat& cv_img, const string& encoding,
Datum* datum) {
std::vector<uchar> buf;
cv::imencode("."+encoding, cv_img, buf);
datum->set_data(std::string(reinterpret_cast<char*>(&buf[0]),
buf.size()));
datum->set_channels(cv_img.channels());
datum->set_height(cv_img.rows);
datum->set_width(cv_img.cols);
datum->set_encoded(true);
}
void CVMatToDatum(const cv::Mat& cv_img, Datum* datum) {
CHECK(cv_img.depth() == CV_8U) << "Image data type must be unsigned byte";
datum->set_channels(cv_img.channels());
datum->set_height(cv_img.rows);
datum->set_width(cv_img.cols);
datum->clear_data();
datum->clear_float_data();
datum->set_encoded(false);
int datum_channels = datum->channels();
int datum_height = datum->height();
int datum_width = datum->width();
int datum_size = datum_channels * datum_height * datum_width;
std::string buffer(datum_size, ' ');
for (int h = 0; h < datum_height; ++h) {
const uchar* ptr = cv_img.ptr<uchar>(h);
int img_index = 0;
for (int w = 0; w < datum_width; ++w) {
for (int c = 0; c < datum_channels; ++c) {
int datum_index = (c * datum_height + h) * datum_width + w;
buffer[datum_index] = static_cast<char>(ptr[img_index++]);
}
}
}
datum->set_data(buffer);
}
#endif // USE_OPENCV
} // namespace caffe