13 #include <caffe/caffe.hpp>
14 using namespace caffe
;
22 static shared_ptr
<Net
<float> > net
;
25 using_dcnn(struct board
*b
)
27 return (real_board_size(b
) == 19 && net
);
30 /* Make caffe quiet */
32 dcnn_quiet_caffe(int argc
, char *argv
[])
34 if (DEBUGL(7) || getenv("GLOG_minloglevel"))
37 setenv("GLOG_minloglevel", "2", 1);
38 execvp(argv
[0], argv
); /* Sucks that we have to do this */
48 const char *model_file
= "movepredict.prototxt";
49 const char *trained_file
= "movepredict.caffemodel";
50 if (stat(model_file
, &s
) != 0 || stat(trained_file
, &s
) != 0) {
52 fprintf(stderr
, "No dcnn files found, will not use dcnn code.\n");
56 Caffe::set_mode(Caffe::CPU
);
58 /* Load the network. */
59 net
.reset(new Net
<float>(model_file
, TEST
));
60 net
->CopyTrainedLayersFrom(trained_file
);
63 fprintf(stderr
, "Initialized dcnn.\n");
67 dcnn_get_moves(struct board
*b
, enum stone color
, float result
[])
69 assert(real_board_size(b
) == 19);
71 float *data
= new float[2 * size
* size
];
74 for (int j
= 0; j
< size
; j
++)
75 for (int k
= 0; k
< size
; k
++) {
76 coord_t c
= coord_xy(b
, j
+ 1, k
+ 1);
77 int offsetb
= j
* size
+ k
;
78 int offsetw
= size
* size
+ size
* j
+ k
;
79 data
[offsetb
] = (board_at(b
, c
) == S_BLACK
? 1.0 : 0.0);
80 data
[offsetw
] = (board_at(b
, c
) == S_WHITE
? 1.0 : 0.0);
83 if (color
== S_WHITE
) {
84 for (int j
= 0; j
< size
; j
++)
85 for (int k
= 0; k
< size
; k
++) {
86 coord_t c
= coord_xy(b
, j
+ 1, k
+ 1);
87 int offsetb
= j
* size
+ k
;
88 int offsetw
= size
* size
+ size
* j
+ k
;
89 data
[offsetb
] = (board_at(b
, c
) == S_WHITE
? 1.0 : 0.0);
90 data
[offsetw
] = (board_at(b
, c
) == S_BLACK
? 1.0 : 0.0);
94 Blob
<float> *blob
= new Blob
<float>(1,2,size
,size
);
95 blob
->set_cpu_data(data
);
96 vector
<Blob
<float>*> bottom
;
97 bottom
.push_back(blob
);
99 const vector
<Blob
<float>*>& rr
= net
->Forward(bottom
);
101 for (int i
= 0; i
< size
* size
; i
++) {
102 result
[i
] = rr
[0]->cpu_data()[i
];
103 if (result
[i
] < 0.00001)