Skip to content

Instantly share code, notes, and snippets.

@iopq
Created March 10, 2016 15:21
Show Gist options
  • Save iopq/884d3a7af33e31b39aac to your computer and use it in GitHub Desktop.
Save iopq/884d3a7af33e31b39aac to your computer and use it in GitHub Desktop.
//#include <cuda_runtime.h>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <string>
#include <iostream>
#include <stdio.h>
#include "caffe/caffe.hpp"
#include "caffe/util/io.hpp"
#include "caffe/blob.hpp"
using namespace caffe;
using namespace std;
const int BLACK = 1;
const int WHITE = 2;
int board[19*19] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,2,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,1,2,1,0,0,0,0,0,0,0,0,0,1,0,2,0,0,
0,1,2,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
const int B_SIZE = 19;
int xy2pos(int x,int y)
{
return y*B_SIZE+x;
}
Net<float> *p_caffe_net;
void initCNN()
{
//set_phase(TEST);
Caffe::set_mode(Caffe::CPU);
//Caffe::set_mode(Caffe::GPU);
string net_src = "movepredict.prototxt";
// Net<float> caffe_net(net_src, caffe::TEST); //get the net
// p_caffe_net = &caffe_net;
p_caffe_net = new Net<float>(net_src, caffe::TEST);
string traied_net = "movepredict.caffemodel";
p_caffe_net->CopyTrainedLayersFrom(traied_net);
}
void testCNN()
{
int col = BLACK;
const int size = B_SIZE;
float result[size*size];
float *data = new float[2*size*size];
//fprintf(stderr,"2\n");
if (col==BLACK) {
for (int j=0;j<size;j++) for (int k=0;k<size;k++) {
//fprintf(stderr,"%d %d %d\n",i,j,k);
if (board[xy2pos(j,k)]==BLACK) {
data[j*size+k]=1.0;
data[size*size+size*j+k]=0.0;
} else if (board[xy2pos(j,k)]==WHITE) {
data[j*size+k]=0.0;
data[size*size+size*j+k]=1.0;
} else {
data[j*size+k]=0.0;
data[size*size+size*j+k]=0.0;
}
}
}
if (col==WHITE) {
for (int j=0;j<size;j++) for (int k=0;k<size;k++) {
//fprintf(stderr,"%d %d %d\n",i,j,k);
if (board[xy2pos(j,k)]==BLACK) {
data[j*size+k]=0.0;
data[size*size+size*j+k]=1.0;
} else if (board[xy2pos(j,k)]==WHITE) {
data[j*size+k]=1.0;
data[size*size+size*j+k]=0.0;
} else {
data[j*size+k]=0.0;
data[size*size+size*j+k]=0.0;
}
}
}
Blob<float> *b=new Blob<float>(1,2,size,size);
b->set_cpu_data(data);
vector<Blob<float>*> bottom;
bottom.push_back(b);
const vector<Blob<float>*>& rr = p_caffe_net->Forward(bottom);
float sum = 0;
for (int j=0;j<B_SIZE;j++) {
for (int k=0;k<B_SIZE;k++) {
float a = rr[0]->cpu_data()[j*B_SIZE+k];
sum += a;
fprintf(stderr,"%5.3f ",a);
}
fprintf(stderr,"\n");
}
fprintf(stderr,"sum=%.3f\n",sum);
for (int i=0;i<size*size;i++) {
result[i]=rr[0]->cpu_data()[i];
if (result[i]<0.00001) result[i]=0.00001;
}
delete[] data;
delete b;
}
int main(int argc, char** argv) {
initCNN();
// testCNN();
double ct1 = clock();
int i;
for (i=0;i<10;i++) {
int x,y;
for (y=0;y<B_SIZE;y++) for (x=0;x<B_SIZE;x++) {
int c = rand() % 3;
board[xy2pos(x,y)] = c;
}
testCNN();
}
double t = (clock() - ct1)/CLOCKS_PER_SEC;
printf("i=%d,t=%.1f, %f\n",i,t, t/i);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment