Skip to content

Commit f6762fa

Browse files
authored
Create intro.cc
1 parent ee3d75b commit f6762fa

File tree

1 file changed

+364
-0
lines changed

1 file changed

+364
-0
lines changed

intro.cc

+364
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,364 @@
1+
/*
2+
* asdf.cpp
3+
*
4+
* Created on: Oct 7, 2018
5+
* Author: ryan
6+
*/
7+
8+
// namespace caffe2
9+
10+
#include "ReadWriteNet.h"
11+
12+
#include <list>
13+
#include <caffe2/proto/caffe2_pb.h>
14+
15+
#include <caffe2/core/init.h>
16+
#include <caffe2/core/operator.h>
17+
#include <caffe2/core/operator_gradient.h>
18+
#include <caffe2/utils/proto_utils.h>
19+
20+
21+
22+
namespace caffe2 {
23+
24+
void print(Blob* blob, const std::string& name) {
25+
//auto tensor = blob->->Get<TensorCPU>();
26+
Tensor* tensor = BlobGetMutableTensor(blob, caffe2::DeviceType::CPU);
27+
const auto& data = tensor->data<float>();
28+
std::cout << name << "(" << tensor->dims()
29+
<< "): " << std::vector<float>(data, data + tensor->size())
30+
<< std::endl;
31+
}
32+
33+
void run() {
34+
std::cout << std::endl;
35+
std::cout << "## Caffe2 Intro Tutorial ##" << std::endl;
36+
std::cout << "https://caffe2.ai/docs/intro-tutorial.html" << std::endl;
37+
std::cout << std::endl;
38+
39+
// >>> from caffe2.python import workspace, model_helper
40+
// >>> import numpy as np
41+
Workspace workspace;
42+
43+
44+
45+
// >>> x = np.random.rand(4, 3, 2)
46+
std::vector<float> x(4 * 3 * 2);
47+
//for (auto& v : x) {
48+
// v = (float)rand() / RAND_MAX;
49+
//}
50+
51+
int count = 0;
52+
Tensor value = Tensor(x.size(), caffe2::DeviceType::CPU);
53+
for (auto &v: x) {
54+
v = (float)rand() / RAND_MAX;
55+
value.mutable_data<float>()[count]=v;
56+
count++;
57+
}
58+
59+
// >>> print(x)
60+
std::cout << x << std::endl;
61+
62+
// >>> workspace.FeedBlob("my_x", x)
63+
{
64+
Blob* my_xBlob = workspace.CreateBlob("my_x");
65+
Tensor* tensor = BlobGetMutableTensor(my_xBlob, caffe2::DeviceType::CPU);
66+
67+
68+
//tensor->ResizeLike(value); //still work
69+
//tensor->ShareData(value); //still work
70+
tensor->CopyFrom(value);
71+
}
72+
73+
// >>> x2 = workspace.FetchBlob("my_x")
74+
// >>> print(x2)
75+
{
76+
Blob* blob = workspace.GetBlob("my_x");
77+
print(blob, "my_x");
78+
}
79+
80+
81+
/*
82+
* the above just demonstrated how to get data data into a tensor thats in a blob which is in a workspace
83+
*/
84+
// >>> data = np.random.rand(16, 100).astype(np.float32)
85+
86+
87+
//std::vector<float> data(16 * 100);
88+
std::vector<float> data(16*10);
89+
std::vector<int> dim({16,10});
90+
count = 0;
91+
/*********************************************/
92+
/* Important note
93+
* From what I could gather the data in the tensor is stored as a one dimensional array
94+
* so if you have this
95+
*
96+
* a b c d e f
97+
* g h I j k l
98+
* m n o p k r
99+
*
100+
* you feed it into the Tensor something like this
101+
*
102+
* r
103+
* p
104+
* o
105+
* n
106+
* m
107+
* l
108+
* k
109+
* k
110+
* j
111+
* I
112+
* h
113+
* g
114+
* f
115+
* e
116+
* d
117+
* c
118+
* b
119+
* a
120+
*
121+
* then the dimensions tell the tensor how the data is should look, so in this example
122+
* the dimensions would be {3,6}
123+
*
124+
* This was not obvious to me at first
125+
*
126+
* */
127+
Tensor dataTen(dim, caffe2::DeviceType::CPU);
128+
129+
for (auto& v : data) {
130+
v = (float)rand() / RAND_MAX;
131+
dataTen.mutable_data<float>()[count] =v;
132+
count++;
133+
}
134+
135+
//just to show that the data is there
136+
for(int a =0; a<count; ++a)
137+
cout<<dataTen.mutable_data<float >()[a]<<endl;
138+
cout<<dataTen.DebugString()<<endl;
139+
140+
// >>> label = (np.random.rand(16) * 10).astype(np.int32)
141+
std::vector<int> label(16,1);
142+
count = 0;
143+
Tensor labelTen = Tensor(label.size(), caffe2::DeviceType::CPU);
144+
for (auto& v : label) {
145+
v = rand() %10;
146+
labelTen.mutable_data<int>()[count]=v;
147+
count++;
148+
}
149+
150+
151+
152+
// >>> workspace.FeedBlob("data", data)
153+
{
154+
//auto tensor = workspace.CreateBlob("data")->GetMutable<TensorCPU>();
155+
Blob* myBlob = workspace.CreateBlob("data");
156+
Tensor* tensor = caffe2::BlobGetMutableTensor(myBlob, caffe2::DeviceType::CPU);
157+
158+
//auto value = TensorCPU({16, 100}, data, NULL);
159+
//dataTen is used instead of value
160+
161+
//tensor->ResizeLike(value);
162+
//tensor->ShareData(value);
163+
tensor->CopyFrom(dataTen);//the above two lines works this is just a different way to do it you will see later that I do it this way
164+
}
165+
166+
// >>> workspace.FeedBlob("label", label)
167+
{
168+
//auto tensor = workspace.CreateBlob("label")->GetMutable<TensorCPU>();
169+
Blob* myBlob = workspace.CreateBlob("label");
170+
Tensor* tensor = caffe2::BlobGetMutableTensor(myBlob, caffe2::DeviceType::CPU);
171+
172+
//auto value = TensorCPU({16}, label, NULL);
173+
//labelTen is used instead of value
174+
175+
//tensor->ResizeLike(value);
176+
//tensor->ShareData(value);
177+
tensor->CopyFrom(labelTen);//the above two lines works this is just a different way to do it you will see later that I do it this way
178+
179+
}
180+
181+
// >>> m = model_helper.ModelHelper(name="my first net")
182+
NetDef initModel;
183+
initModel.set_name("my first net_init");
184+
NetDef predictModel;
185+
predictModel.set_name("my first net");
186+
187+
// >>> weight = m.param_initModel.XavierFill([], 'fc_w', shape=[10, 100])
188+
{
189+
auto op = initModel.add_op();
190+
op->set_type("XavierFill");
191+
auto arg = op->add_arg();
192+
arg->set_name("shape");
193+
arg->add_ints(16);
194+
arg->add_ints(10);
195+
op->add_output("fc_w");
196+
}
197+
198+
199+
// >>> bias = m.param_initModel.ConstantFill([], 'fc_b', shape=[10, ])
200+
{
201+
auto op = initModel.add_op();
202+
op->set_type("ConstantFill");
203+
auto arg = op->add_arg();
204+
arg->set_name("shape");
205+
arg->add_ints(16);
206+
op->add_output("fc_b");
207+
}
208+
209+
std::vector<OperatorDef*> gradient_ops;
210+
211+
// >>> fc_1 = m.net.FC(["data", "fc_w", "fc_b"], "fc1")
212+
{
213+
auto op = predictModel.add_op();
214+
op->set_type("FC");
215+
op->add_input("data");
216+
op->add_input("fc_w");
217+
op->add_input("fc_b");
218+
op->add_output("fc1");
219+
gradient_ops.push_back(op);
220+
}
221+
222+
// >>> pred = m.net.Sigmoid(fc_1, "pred")
223+
{
224+
auto op = predictModel.add_op();
225+
op->set_type("Sigmoid");
226+
op->add_input("fc1");
227+
op->add_output("pred");
228+
gradient_ops.push_back(op);
229+
}
230+
231+
// >>> [softmax, loss] = m.net.SoftmaxWithLoss([pred, "label"], ["softmax",
232+
// "loss"])
233+
{
234+
auto op = predictModel.add_op();
235+
op->set_type("SoftmaxWithLoss");
236+
op->add_input("pred");
237+
op->add_input("label");
238+
op->add_output("softmax");
239+
op->add_output("loss");
240+
gradient_ops.push_back(op);
241+
}
242+
243+
// >>> m.AddGradientOperators([loss])
244+
{
245+
auto op = predictModel.add_op();
246+
op->set_type("ConstantFill");
247+
auto arg = op->add_arg();
248+
arg->set_name("value");
249+
arg->set_f(1.0);
250+
op->add_input("loss");
251+
op->add_output("loss_grad");
252+
op->set_is_gradient_op(true);
253+
}
254+
std::reverse(gradient_ops.begin(), gradient_ops.end());
255+
for (auto op : gradient_ops) {
256+
vector<GradientWrapper> output(op->output_size());
257+
for (auto i = 0; i < output.size(); i++) {
258+
output[i].dense_ = op->output(i) + "_grad";
259+
}
260+
GradientOpsMeta meta = GetGradientForOp(*op, output);
261+
auto grad = predictModel.add_op();
262+
grad->CopyFrom(meta.ops_[0]);
263+
grad->set_is_gradient_op(true);
264+
}
265+
266+
// >>> print(str(m.net.Proto()))
267+
// std::cout << std::endl;
268+
// print(predictModel);
269+
270+
// >>> print(str(m.param_init_net.Proto()))
271+
// std::cout << std::endl;
272+
// print(initModel);
273+
274+
// >>> workspace.RunNetOnce(m.param_init_net)
275+
CAFFE_ENFORCE(workspace.RunNetOnce(initModel));
276+
277+
278+
// >>> workspace.CreateNet(m.net)
279+
CAFFE_ENFORCE(workspace.CreateNet(predictModel));
280+
281+
282+
// >>> for j in range(0, 100):
283+
for (auto i = 0; i < 100; i++) {
284+
// >>> data = np.random.rand(16, 100).astype(np.float32)
285+
//std::vector<float> data(16 * 100);
286+
std::vector<float> data(16*10);
287+
count=0;
288+
for (auto& v : data) {
289+
v = (float)rand() / RAND_MAX;
290+
dataTen.mutable_data<float>()[count] = v;
291+
count++;
292+
}
293+
294+
// >>> label = (np.random.rand(16) * 10).astype(np.int32)
295+
std::vector<int> label(16);
296+
count = 0;
297+
for (auto& v : label) {
298+
v = rand() %10;
299+
labelTen.mutable_data<int>()[count] = v;
300+
count++;
301+
302+
}
303+
304+
// >>> workspace.FeedBlob("data", data)
305+
{
306+
//auto tensor = workspace.GetBlob("data")->GetMutable<TensorCPU>();
307+
Blob* myBlob = workspace.GetBlob("data");
308+
Tensor* tensor = caffe2::BlobGetMutableTensor(myBlob, caffe2::DeviceType::CPU);
309+
//auto value = TensorCPU({16, 100}, data, NULL);
310+
//tensor->ShareData(value);
311+
tensor->ResizeLike(dataTen);
312+
tensor->ShareData(dataTen);
313+
314+
315+
}
316+
317+
// >>> workspace.FeedBlob("label", label)
318+
{
319+
//auto tensor = workspace.GetBlob("label")->GetMutable<TensorCPU>();
320+
Blob* myBlob = workspace.GetBlob("label");
321+
Tensor* tensor = caffe2::BlobGetMutableTensor(myBlob, caffe2::DeviceType::CPU);
322+
//auto value = TensorCPU({16}, label, NULL);
323+
//tensor->ShareData(value);
324+
tensor->ResizeLike(labelTen);
325+
tensor->ShareData(labelTen);
326+
327+
}
328+
329+
cout<<predictModel.DebugString()<<endl;
330+
cout<<predictModel.external_input_size()<<endl;
331+
predictModel.InitAsDefaultInstance();
332+
333+
334+
// >>> workspace.RunNet(m.name, 10) # run for 10 times
335+
for (auto j = 0; j < 10; j++) {
336+
predictModel.CheckInitialized();
337+
CAFFE_ENFORCE(workspace.RunNet(predictModel.name()));
338+
std::cout << "step: " << i << " loss: ";
339+
print(workspace.GetBlob("loss"),"loss");
340+
std::cout << std::endl;
341+
}
342+
}
343+
344+
std::cout << std::endl;
345+
346+
// >>> print(workspace.FetchBlob("softmax"))
347+
print(workspace.GetBlob("softmax"), "softmax");
348+
349+
std::cout << std::endl;
350+
351+
// >>> print(workspace.FetchBlob("loss"))
352+
print(workspace.GetBlob("loss"), "loss");
353+
}
354+
355+
}
356+
int main(int argc, char **argv) {
357+
358+
caffe2::GlobalInit(&argc, &argv);
359+
caffe2::run();
360+
google::protobuf::ShutdownProtobufLibrary();
361+
362+
363+
return 0;
364+
}

0 commit comments

Comments
 (0)