11#include " visualdl/logic/sdk.h"
22
33#include " visualdl/logic/histogram.h"
4+ #include " visualdl/storage/binary_record.h"
45#include " visualdl/utils/image.h"
56#include " visualdl/utils/macro.h"
67
78namespace visualdl {
89
10+ // global log dir, a hack solution to pass accross all the components.
11+ // One process of VDL backend can only process a single logdir, so this
12+ // is OK.
13+ std::string g_log_dir;
14+
15+ LogWriter LogWriter::AsMode (const std::string& mode) {
16+ LogWriter writer = *this ;
17+ storage_.AddMode (mode);
18+ writer.mode_ = mode;
19+ return writer;
20+ }
21+
22+ Tablet LogWriter::AddTablet (const std::string& tag) {
23+ // TODO(ChunweiYan) add string check here.
24+ auto tmp = mode_ + " /" + tag;
25+ string::TagEncode (tmp);
26+ auto res = storage_.AddTablet (tmp);
27+ res.SetCaptions (std::vector<std::string>({mode_}));
28+ res.SetTag (mode_, tag);
29+ return res;
30+ }
31+
32+ LogReader::LogReader (const std::string& dir) : reader_(dir) { g_log_dir = dir; }
33+
34+ LogReader LogReader::AsMode (const std::string& mode) {
35+ auto tmp = *this ;
36+ tmp.mode_ = mode;
37+ return tmp;
38+ }
39+
40+ TabletReader LogReader::tablet (const std::string& tag) {
41+ auto tmp = mode_ + " /" + tag;
42+ string::TagEncode (tmp);
43+ return reader_.tablet (tmp);
44+ }
45+
46+ std::vector<std::string> LogReader::all_tags () {
47+ auto tags = reader_.all_tags ();
48+ auto it =
49+ std::remove_if (tags.begin (), tags.end (), [&](const std::string& tag) {
50+ return !TagMatchMode (tag, mode_);
51+ });
52+ tags.erase (it + 1 );
53+ return tags;
54+ }
55+
56+ std::vector<std::string> LogReader::tags (const std::string& component) {
57+ auto type = Tablet::type (component);
58+ auto tags = reader_.tags (type);
59+ CHECK (!tags.empty ()) << " component " << component << " has no taged records" ;
60+ std::vector<std::string> res;
61+ for (const auto & tag : tags) {
62+ if (TagMatchMode (tag, mode_)) {
63+ res.push_back (GenReadableTag (mode_, tag));
64+ }
65+ }
66+ return res;
67+ }
68+
69+ std::string LogReader::GenReadableTag (const std::string& mode,
70+ const std::string& tag) {
71+ auto tmp = tag;
72+ string::TagDecode (tmp);
73+ return tmp.substr (mode.size () + 1 ); // including `/`
74+ }
75+
76+ bool LogReader::TagMatchMode (const std::string& tag, const std::string& mode) {
77+ if (tag.size () <= mode.size ()) return false ;
78+ return tag.substr (0 , mode.size ()) == mode;
79+ }
80+
981namespace components {
1082
1183template <typename T>
@@ -103,8 +175,10 @@ void Image::SetSample(int index,
103175 new_shape.emplace_back (1 );
104176 }
105177 // production
106- int size = std::accumulate (
107- new_shape.begin (), new_shape.end (), 1 ., [](int a, int b) { return a * b; });
178+ int size =
179+ std::accumulate (new_shape.begin (), new_shape.end (), 1 ., [](int a, int b) {
180+ return a * b;
181+ });
108182 CHECK_GT (size, 0 );
109183 CHECK_LE (new_shape.size (), 3 )
110184 << " shape should be something like (width, height, num_channel)" ;
@@ -114,30 +188,28 @@ void Image::SetSample(int index,
114188 CHECK_LT (index, num_samples_);
115189 CHECK_LE (index, num_records_);
116190
117- auto entry = step_.MutableData <std::vector<byte_t >>(index);
118191 // trick to store int8 to protobuf
119192 std::vector<byte_t > data_str (data.size ());
120193 for (int i = 0 ; i < data.size (); i++) {
121194 data_str[i] = data[i];
122195 }
123196 Uint8Image image (new_shape[2 ], new_shape[0 ] * new_shape[1 ]);
124197 NormalizeImage (&image, &data[0 ], new_shape[0 ] * new_shape[1 ], new_shape[2 ]);
125- // entry.SetRaw(std::string(data_str.begin(), data_str.end()));
126- entry.SetRaw (
198+
199+ BinaryRecord brcd (
200+ GenBinaryRecordDir (step_.parent ()->dir ()),
127201 std::string (image.data (), image.data () + image.rows () * image.cols ()));
202+ brcd.tofile ();
203+
204+ auto entry = step_.MutableData <std::vector<byte_t >>(index);
205+ entry.SetRaw (brcd.hash ());
128206
129207 static_assert (
130208 !is_same_type<value_t , shape_t >::value,
131209 " value_t should not use int64_t field, this type is used to store shape" );
132210
133211 // set meta.
134212 entry.SetMulti (new_shape);
135-
136- // // set meta with hack
137- // Entry<shape_t> meta;
138- // meta.set_parent(entry.parent());
139- // meta.entry = entry.entry;
140- // meta.SetMulti(shape);
141213}
142214
143215std::string ImageReader::caption () {
@@ -154,9 +226,13 @@ ImageReader::ImageRecord ImageReader::record(int offset, int index) {
154226 ImageRecord res;
155227 auto record = reader_.record (offset);
156228 auto entry = record.data (index);
157- auto data_str = entry.GetRaw ();
158- std::transform (data_str.begin (),
159- data_str.end (),
229+ auto data_hash = entry.GetRaw ();
230+ CHECK (!g_log_dir.empty ())
231+ << " g_log_dir should be set in LogReader construction" ;
232+ BinaryRecordReader brcd (GenBinaryRecordDir (g_log_dir), data_hash);
233+
234+ std::transform (brcd.data .begin (),
235+ brcd.data .end (),
160236 std::back_inserter (res.data ),
161237 [](byte_t i) { return (int )(i); });
162238 res.shape = entry.GetMulti <shape_t >();
0 commit comments