1 | /** |
2 | * Copyright (c) Glow Contributors. See CONTRIBUTORS file. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #ifndef GLOW_TOOLS_LOADER_UTILS_H |
18 | #define GLOW_TOOLS_LOADER_UTILS_H |
19 | |
20 | #include "glow/Support/Support.h" |
21 | #include "llvm/ADT/ArrayRef.h" |
22 | #include "llvm/ADT/StringRef.h" |
23 | #include "llvm/Support/FileSystem.h" |
24 | #include "llvm/Support/Format.h" |
25 | #include "llvm/Support/Path.h" |
26 | #include "llvm/Support/raw_ostream.h" |
27 | |
28 | #include <chrono> |
29 | #include <cstddef> |
30 | #include <cstdint> |
31 | #include <fstream> |
32 | #include <sstream> |
33 | |
34 | namespace glow { |
35 | |
36 | /// Print message and exit with error code. |
37 | void exitWithErr(llvm::StringRef errMsg); |
38 | |
39 | /// Check condition and if false print message and exit with error code. |
40 | void checkCond(bool cond, llvm::StringRef errMsg); |
41 | |
42 | /// Typedefs for unlabeled datasets consisting only in data paths. |
43 | using UnlabeledData = std::string; |
44 | using UnlabeledDataSet = std::vector<UnlabeledData>; |
45 | |
46 | /// Function to read an unlabeled data set as an array of paths. The |
47 | /// \p dataSetFile is the path of the dataset description text file which |
48 | /// contains on each line a file name. The rest of the line content apart |
49 | /// from what is needed is ignored. An example might look like this (space |
50 | /// separated): |
51 | /// image0.png |
52 | /// image1.png this will be ignored |
53 | /// ............................... |
54 | /// Another example might look like this (comma separated): |
55 | /// image0.png, |
56 | /// image1.png,this,will,be,ignored, |
57 | /// ............................... |
58 | /// The file names are concatenated (prepended) with a common directory path |
59 | /// given by \p dataSetDirPath. If the \p dataSetDirPath is empty then it will |
60 | /// not be used. This function validates that all the data paths are valid and |
61 | /// \returns the unlabeled data set. |
62 | UnlabeledDataSet readUnlabeledDataSetFromFile(llvm::StringRef dataSetFile, |
63 | llvm::StringRef dataSetDirPath); |
64 | |
65 | /// Read an unlabeled data set as all the files from the given directory |
66 | /// \p dataSetDirPath. |
67 | UnlabeledDataSet readUnlabeledDataSetFromDir(llvm::StringRef dataSetDirPath); |
68 | |
69 | /// Typedefs for labeled datasets consisting in pairs of data paths and integer |
70 | /// labels. |
71 | using LabeledData = std::pair<std::string, unsigned>; |
72 | using LabeledDataSet = std::vector<LabeledData>; |
73 | |
74 | /// Function to read a labeled data set as pairs of {data_path, data_label}. |
75 | /// The \p dataSetFile is the path of the dataset description text file which |
76 | /// contains on each line a file name and an integer label separated by space |
77 | /// or comma. The integer labels start with 0 (0,1,...). The rest of the line |
78 | /// content apart from what is needed is ignored. An example might look like |
79 | /// this (space separated): |
80 | /// image0.png 0 |
81 | /// image1.png 13 |
82 | /// image1.png 15 this will be ignored |
83 | /// .................................. |
84 | /// Another example might look like this (comma separated): |
85 | /// image0.png,0, |
86 | /// image1.png,13, |
87 | /// image1.png,15,this,will,be,ignored, |
88 | /// .................................. |
89 | /// The file names are concatenated (prepended) with a common directory path |
90 | /// given by \p dataSetDirPath. This function validates that all the data paths |
91 | /// are valid and \returns the labeled data set. |
92 | LabeledDataSet readLabeledDataSet(llvm::StringRef dataSetFile, |
93 | llvm::StringRef dataSetDirPath); |
94 | |
95 | /// Typedefs for time measurement. |
96 | using TimeStamp = std::chrono::time_point<std::chrono::high_resolution_clock>; |
97 | |
98 | /// Get current time stamp. |
99 | TimeStamp getTimeStamp(); |
100 | |
101 | /// Get time duration (seconds) between current moment and a reference starting |
102 | /// time stamp. |
103 | unsigned getDurationSec(TimeStamp &startTime); |
104 | |
105 | /// Get time duration (seconds, minutes, hours) between current moment and a |
106 | /// reference starting time stamp. |
107 | void getDuration(TimeStamp &startTime, unsigned &sec, unsigned &min, |
108 | unsigned &hrs); |
109 | |
110 | } // namespace glow |
111 | |
112 | #endif // GLOW_TOOLS_LOADER_UTILS_H |
113 | |