20 #include "caffe2/core/blob_serialization.h" 21 #include "caffe2/core/init.h" 22 #include "caffe2/core/logging.h" 23 #include "caffe2/core/net.h" 24 #include "caffe2/core/operator.h" 25 #include "caffe2/utils/string_utils.h" 26 #include "c10/util/string_utils.h" 29 using std::shared_ptr;
33 template <
typename ContextType,
typename TensorType>
36 const string& output_prefix,
40 if (index >= num_blobs) {
43 string filename = name;
44 std::replace(filename.begin(), filename.end(),
'/',
'_');
45 string output_name = output_prefix +
"/" + filename +
".txt";
47 caffe2::BlobProto blob_proto;
50 *tensor, output_name, blob_proto.mutable_tensor(), 0, tensor->numel());
51 blob_proto.set_name(output_name);
52 blob_proto.set_type(
"Tensor");
53 CAFFE_ENFORCE(blob_proto.has_tensor());
54 caffe2::TensorProto tensor_proto = blob_proto.tensor();
55 int dims_size = tensor_proto.dims_size();
56 long long elem_dim_size =
57 dims_size > 1 ? tensor_proto.dims(1) : tensor_proto.dims(0);
58 for (
int i = 2; i < dims_size; i++) {
59 elem_dim_size *= tensor_proto.dims(i);
61 std::vector<std::string> lines;
63 for (
int i = 0; i < dims_size; i++) {
64 int dim = tensor_proto.dims(i);
68 dims += c10::to_string(dim);
70 lines.push_back(dims);
71 std::stringstream line;
72 if (tensor_proto.data_type() == caffe2::TensorProto::FLOAT) {
73 auto start = tensor_proto.float_data().begin();
74 auto end = tensor_proto.float_data().end();
75 copy(start, end, std::ostream_iterator<float>(line,
","));
76 }
else if (tensor_proto.data_type() == caffe2::TensorProto::INT32) {
77 auto start = tensor_proto.int32_data().begin();
78 auto end = tensor_proto.int32_data().end();
79 copy(start, end, std::ostream_iterator<int>(line,
","));
81 CAFFE_THROW(
"Unimplemented Blob type.");
84 string str = line.str();
85 if(str.length() != 0) {
91 auto flags =
static_cast<std::ios_base::openmode
>(std::ios::out);
93 flags |=
static_cast<std::ios_base::openmode
>(std::ios::app);
95 flags |=
static_cast<std::ios_base::openmode
>(std::ios::trunc);
97 std::ofstream output_file(output_name, flags);
98 std::ostream_iterator<std::string> output_iterator(output_file,
"\n");
99 std::copy(lines.begin(), lines.end(), output_iterator);
102 void observerConfig();
103 bool backendCudaSet(
const string&);
104 void setDeviceType(caffe2::NetDef*, caffe2::DeviceType&);
105 void setOperatorEngine(caffe2::NetDef*,
const string&);
107 shared_ptr<caffe2::Workspace> workspace,
108 const bool run_on_gpu,
109 map<string, caffe2::TensorProtos>& tensor_protos_map,
111 const string& input_file,
112 const string& input_dims,
113 const string& input_type);
115 shared_ptr<caffe2::Workspace> workspace,
116 map<string, caffe2::TensorProtos>& tensor_protos_map,
119 shared_ptr<caffe2::Workspace> workspace,
120 const bool run_on_gpu,
121 const string& output,
122 const string& output_folder,
123 const bool text_output,
125 const int num_blobs);
127 shared_ptr<caffe2::Workspace> workspace,
128 caffe2::NetDef& net_def,
129 map<string, caffe2::TensorProtos>& tensor_protos_map,
130 const bool wipe_cache,
131 const bool run_individual,
132 const bool run_on_gpu,
133 const bool text_output,
137 const int sleep_before_run,
138 const int sleep_between_iteration,
139 const int sleep_between_net_and_operator,
140 const std::string& output,
141 const std::string& output_folder);
145 const string& FLAGS_backend,
146 const string& FLAGS_init_net,
147 const string& FLAGS_input,
148 const string& FLAGS_input_dims,
149 const string& FLAGS_input_file,
150 const string& FLAGS_input_type,
152 const string& FLAGS_net,
153 const string& FLAGS_output,
154 const string& FLAGS_output_folder,
155 bool FLAGS_run_individual,
156 int FLAGS_sleep_before_run,
157 int FLAGS_sleep_between_iteration,
158 int FLAGS_sleep_between_net_and_operator,
159 bool FLAGS_text_output,
161 bool FLAGS_wipe_cache);
TensorSerializer is the serializer for Tensors.
void Serialize(const void *pointer, TypeMeta typeMeta, const string &name, SerializationAcceptor acceptor) override
Serializes a Blob.