From 158bb1bee9951875dbe3474d84c6663431e18301 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Tue, 21 Oct 2014 21:49:18 +0000
Subject: [PATCH] softmax on gpu
---
src/cnn.c | 130 +++++++++++++++++++++++++++++++------------
1 files changed, 93 insertions(+), 37 deletions(-)
diff --git a/src/cnn.c b/src/cnn.c
index 0cd6da3..7e90a80 100644
--- a/src/cnn.c
+++ b/src/cnn.c
@@ -37,42 +37,104 @@
void test_convolutional_layer()
{
int i;
- image dog = load_image("data/dog.jpg",256,256);
+ image dog = load_image("data/dog.jpg",224,224);
network net = parse_network_cfg("cfg/convolutional.cfg");
// data test = load_cifar10_data("data/cifar10/test_batch.bin");
// float *X = calloc(net.batch*test.X.cols, sizeof(float));
// float *y = calloc(net.batch*test.y.cols, sizeof(float));
int in_size = get_network_input_size(net)*net.batch;
+ int del_size = get_network_output_size_layer(net, 0)*net.batch;
int size = get_network_output_size(net)*net.batch;
-float *X = calloc(in_size, sizeof(float));
+ float *X = calloc(in_size, sizeof(float));
+ float *y = calloc(size, sizeof(float));
for(i = 0; i < in_size; ++i){
X[i] = dog.data[i%get_network_input_size(net)];
}
// get_batch(test, net.batch, X, y);
clock_t start, end;
cl_mem input_cl = cl_make_array(X, in_size);
+ cl_mem truth_cl = cl_make_array(y, size);
- forward_network_gpu(net, input_cl, 1);
+ forward_network_gpu(net, input_cl, truth_cl, 1);
start = clock();
- forward_network_gpu(net, input_cl, 1);
+ forward_network_gpu(net, input_cl, truth_cl, 1);
end = clock();
float gpu_sec = (float)(end-start)/CLOCKS_PER_SEC;
+ printf("forward gpu: %f sec\n", gpu_sec);
+ start = clock();
+ backward_network_gpu(net, input_cl);
+ end = clock();
+ gpu_sec = (float)(end-start)/CLOCKS_PER_SEC;
+ printf("backward gpu: %f sec\n", gpu_sec);
+ //float gpu_cost = get_network_cost(net);
float *gpu_out = calloc(size, sizeof(float));
memcpy(gpu_out, get_network_output(net), size*sizeof(float));
+ float *gpu_del = calloc(del_size, sizeof(float));
+ memcpy(gpu_del, get_network_delta_layer(net, 0), del_size*sizeof(float));
+
+/*
start = clock();
- forward_network(net, X, 1);
+ forward_network(net, X, y, 1);
+ backward_network(net, X);
+ float cpu_cost = get_network_cost(net);
end = clock();
float cpu_sec = (float)(end-start)/CLOCKS_PER_SEC;
float *cpu_out = calloc(size, sizeof(float));
memcpy(cpu_out, get_network_output(net), size*sizeof(float));
+ float *cpu_del = calloc(del_size, sizeof(float));
+ memcpy(cpu_del, get_network_delta_layer(net, 0), del_size*sizeof(float));
float sum = 0;
- for(i = 0; i < size; ++i) {
- //printf("%f, %f\n", gpu_out[i], cpu_out[i]);
- sum += pow(gpu_out[i] - cpu_out[i], 2);
+ float del_sum = 0;
+ for(i = 0; i < size; ++i) sum += pow(gpu_out[i] - cpu_out[i], 2);
+ for(i = 0; i < del_size; ++i) {
+ //printf("%f %f\n", cpu_del[i], gpu_del[i]);
+ del_sum += pow(cpu_del[i] - gpu_del[i], 2);
}
- printf("gpu: %f sec, cpu: %f sec, diff: %f, size: %d\n", gpu_sec, cpu_sec, sum, size);
+ printf("GPU cost: %f, CPU cost: %f\n", gpu_cost, cpu_cost);
+ printf("gpu: %f sec, cpu: %f sec, diff: %f, delta diff: %f, size: %d\n", gpu_sec, cpu_sec, sum, del_sum, size);
+ */
+}
+
+void test_col2im()
+{
+ float col[] = {1,2,1,2,
+ 1,2,1,2,
+ 1,2,1,2,
+ 1,2,1,2,
+ 1,2,1,2,
+ 1,2,1,2,
+ 1,2,1,2,
+ 1,2,1,2,
+ 1,2,1,2};
+ float im[16] = {0};
+ int batch = 1;
+ int channels = 1;
+ int height=4;
+ int width=4;
+ int ksize = 3;
+ int stride = 1;
+ int pad = 0;
+ col2im_gpu(col, batch,
+ channels, height, width,
+ ksize, stride, pad, im);
+ int i;
+ for(i = 0; i < 16; ++i)printf("%f,", im[i]);
+ printf("\n");
+ /*
+ float data_im[] = {
+ 1,2,3,4,
+ 5,6,7,8,
+ 9,10,11,12
+ };
+ float data_col[18] = {0};
+ im2col_cpu(data_im, batch,
+ channels, height, width,
+ ksize, stride, pad, data_col) ;
+ for(i = 0; i < 18; ++i)printf("%f,", data_col[i]);
+ printf("\n");
+ */
}
#endif
@@ -216,29 +278,22 @@
free_data(train);
}
-void train_full()
+void train_assira()
{
- network net = parse_network_cfg("cfg/imagenet.cfg");
+ network net = parse_network_cfg("cfg/assira.cfg");
+ int imgs = 1000/net.batch+1;
+ //imgs = 1;
srand(2222222);
int i = 0;
char *labels[] = {"cat","dog"};
- float lr = .00001;
- float momentum = .9;
- float decay = 0.01;
while(1){
i += 1000;
- data train = load_data_image_pathfile_random("images/assira/train.list", 1000, labels, 2, 256, 256);
- //image im = float_to_image(256, 256, 3,train.X.vals[0]);
- //visualize_network(net);
- //cvWaitKey(100);
- //show_image(im, "input");
- //cvWaitKey(100);
- //scale_data_rows(train, 1./255.);
+ data train = load_data_image_pathfile_random("data/assira/train.list", imgs*net.batch, labels, 2, 256, 256);
normalize_data_rows(train);
clock_t start = clock(), end;
- float loss = train_network_sgd(net, train, 1000);
+ float loss = train_network_sgd_gpu(net, train, imgs);
end = clock();
- printf("%d: %f, Time: %lf seconds, LR: %f, Momentum: %f, Decay: %f\n", i, loss, (float)(end-start)/CLOCKS_PER_SEC, lr, momentum, decay);
+ printf("%d: %f, Time: %lf seconds\n", i, loss, (float)(end-start)/CLOCKS_PER_SEC );
free_data(train);
if(i%10000==0){
char buff[256];
@@ -274,7 +329,7 @@
normalize_data_rows(test);
for(j = 0; j < test.X.rows; ++j){
float *x = test.X.vals[j];
- forward_network(net, x, 0);
+ forward_network(net, x, 0, 0);
int class = get_predicted_class_network(net);
fprintf(fp, "%d\n", class);
}
@@ -285,7 +340,6 @@
void test_cifar10()
{
-
network net = parse_network_cfg("cfg/cifar10_part5.cfg");
data test = load_cifar10_data("data/cifar10/test_batch.bin");
clock_t start = clock(), end;
@@ -308,8 +362,8 @@
clock_t start = clock(), end;
float loss = train_network_sgd(net, train, iters);
end = clock();
- visualize_network(net);
- cvWaitKey(5000);
+ //visualize_network(net);
+ //cvWaitKey(5000);
//float test_acc = network_accuracy(net, test);
//printf("%d: Loss: %f, Test Acc: %f, Time: %lf seconds, LR: %f, Momentum: %f, Decay: %f\n", count, loss, test_acc,(float)(end-start)/CLOCKS_PER_SEC, net.learning_rate, net.momentum, net.decay);
@@ -317,7 +371,7 @@
float test_acc = network_accuracy(net, test);
printf("%d: Loss: %f, Test Acc: %f, Time: %lf seconds, LR: %f, Momentum: %f, Decay: %f\n", count, loss, test_acc,(float)(end-start)/CLOCKS_PER_SEC, net.learning_rate, net.momentum, net.decay);
char buff[256];
- sprintf(buff, "/home/pjreddie/cifar/cifar2_%d.cfg", count);
+ sprintf(buff, "/home/pjreddie/cifar/cifar10_2_%d.cfg", count);
save_network(net, buff);
}else{
printf("%d: Loss: %f, Time: %lf seconds, LR: %f, Momentum: %f, Decay: %f\n", count, loss, (float)(end-start)/CLOCKS_PER_SEC, net.learning_rate, net.momentum, net.decay);
@@ -383,7 +437,7 @@
int iters = 10000/net.batch;
while(++count <= 2000){
clock_t start = clock(), end;
- float loss = train_network_sgd(net, train, iters);
+ float loss = train_network_sgd_gpu(net, train, iters);
end = clock();
float test_acc = network_accuracy(net, test);
//float test_acc = 0;
@@ -457,7 +511,7 @@
int index = rand()%m.rows;
//image p = float_to_image(1690,1,1,m.vals[index]);
//normalize_image(p);
- forward_network(net, m.vals[index], 1);
+ forward_network(net, m.vals[index], 0, 1);
float *out = get_network_output(net);
float *delta = get_network_delta(net);
//printf("%f\n", out[0]);
@@ -478,7 +532,7 @@
matrix test = csv_to_matrix("test.csv");
truth = pop_column(&test, 0);
for(i = 0; i < test.rows; ++i){
- forward_network(net, test.vals[i], 0);
+ forward_network(net, test.vals[i],0, 0);
float *out = get_network_output(net);
if(fabs(out[0]) < .5) fprintf(fp, "0\n");
else fprintf(fp, "1\n");
@@ -578,7 +632,7 @@
//normalize_array(im.data, im.h*im.w*im.c);
translate_image(im, -144);
resize_network(net, im.h, im.w, im.c);
- forward_network(net, im.data, 0);
+ forward_network(net, im.data, 0, 0);
image out = get_network_image(net);
free_image(im);
cvReleaseImage(&sized);
@@ -630,7 +684,7 @@
resize_network(net, im.h, im.w, im.c);
//scale_image(im, 1./255);
translate_image(im, -144);
- forward_network(net, im.data, 0);
+ forward_network(net, im.data, 0, 0);
image out = get_network_image(net);
int dh = (im.h - h)/(out.h-1);
@@ -692,7 +746,7 @@
image im = load_image(image_path, 0, 0);
printf("Processing %dx%d image\n", im.h, im.w);
resize_network(net, im.h, im.w, im.c);
- forward_network(net, im.data, 0);
+ forward_network(net, im.data, 0, 0);
image out = get_network_image(net);
int dh = (im.h - h)/h;
@@ -725,7 +779,7 @@
image im = load_image("data/cat.png", 0, 0);
printf("Processing %dx%d image\n", im.h, im.w);
resize_network(net, im.h, im.w, im.c);
- forward_network(net, im.data, 0);
+ forward_network(net, im.data, 0, 0);
visualize_network(net);
cvWaitKey(0);
@@ -841,7 +895,8 @@
int main(int argc, char *argv[])
{
- //train_full();
+ //test_blas();
+ train_assira();
//test_distribution();
//feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
@@ -855,8 +910,9 @@
//test_ensemble();
//test_nist_single();
//test_nist();
- train_nist();
+ //train_nist();
//test_convolutional_layer();
+ //test_col2im();
//test_cifar10();
//train_cifar10();
//test_vince();
--
Gitblit v1.10.0