From 1c0fd9bb4726f28b5ccf4491b8d108b00c884ec3 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Thu, 30 Oct 2014 06:26:41 +0000
Subject: [PATCH] im2col slightly faster
---
src/network.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 55 insertions(+), 3 deletions(-)
diff --git a/src/network.c b/src/network.c
index 6696769..0a72a19 100644
--- a/src/network.c
+++ b/src/network.c
@@ -38,6 +38,7 @@
//printf("start\n");
int i;
for(i = 0; i < net.n; ++i){
+ clock_t time = clock();
if(net.types[i] == CONVOLUTIONAL){
convolutional_layer layer = *(convolutional_layer *)net.layers[i];
forward_convolutional_layer_gpu(layer, input);
@@ -62,6 +63,7 @@
forward_softmax_layer_gpu(layer, input);
input = layer.output_cl;
}
+ printf("%d %f\n", i, sec(clock()-time));
/*
else if(net.types[i] == CROP){
crop_layer layer = *(crop_layer *)net.layers[i];
@@ -83,6 +85,7 @@
cl_mem prev_input;
cl_mem prev_delta;
for(i = net.n-1; i >= 0; --i){
+ clock_t time = clock();
if(i == 0){
prev_input = input;
prev_delta = 0;
@@ -110,6 +113,7 @@
softmax_layer layer = *(softmax_layer *)net.layers[i];
backward_softmax_layer_gpu(layer, prev_delta);
}
+ printf("back: %d %f\n", i, sec(clock()-time));
}
}
@@ -384,6 +388,7 @@
{
int x_size = get_network_input_size(net)*net.batch;
int y_size = get_network_output_size(net)*net.batch;
+ clock_t time = clock();
if(!*net.input_cl){
*net.input_cl = cl_make_array(x, x_size);
*net.truth_cl = cl_make_array(y, y_size);
@@ -391,10 +396,18 @@
cl_write_array(*net.input_cl, x, x_size);
cl_write_array(*net.truth_cl, y, y_size);
}
+ //printf("trans %f\n", sec(clock()-time));
+ time = clock();
forward_network_gpu(net, *net.input_cl, *net.truth_cl, 1);
+ //printf("forw %f\n", sec(clock()-time));
+ time = clock();
backward_network_gpu(net, *net.input_cl);
+ //printf("back %f\n", sec(clock()-time));
+ time = clock();
float error = get_network_cost(net);
update_network_gpu(net);
+ //printf("updt %f\n", sec(clock()-time));
+ time = clock();
return error;
}
@@ -407,7 +420,25 @@
int i;
float sum = 0;
for(i = 0; i < n; ++i){
- get_batch(d, batch, X, y);
+ get_random_batch(d, batch, X, y);
+ float err = train_network_datum_gpu(net, X, y);
+ sum += err;
+ }
+ free(X);
+ free(y);
+ return (float)sum/(n*batch);
+}
+
+float train_network_data_gpu(network net, data d, int n)
+{
+ int batch = net.batch;
+ float *X = calloc(batch*d.X.cols, sizeof(float));
+ float *y = calloc(batch*d.y.cols, sizeof(float));
+
+ int i;
+ float sum = 0;
+ for(i = 0; i < n; ++i){
+ get_next_batch(d, batch, i*batch, X, y);
float err = train_network_datum_gpu(net, X, y);
sum += err;
}
@@ -438,7 +469,7 @@
int i;
float sum = 0;
for(i = 0; i < n; ++i){
- get_batch(d, batch, X, y);
+ get_random_batch(d, batch, X, y);
float err = train_network_datum(net, X, y);
sum += err;
}
@@ -621,7 +652,7 @@
image *prev = 0;
int i;
char buff[256];
- show_image(get_network_image_layer(net, 0), "Crop");
+ //show_image(get_network_image_layer(net, 0), "Crop");
for(i = 0; i < net.n; ++i){
sprintf(buff, "Layer %d", i);
if(net.types[i] == CONVOLUTIONAL){
@@ -635,6 +666,27 @@
}
}
+void top_predictions(network net, int n, int *index)
+{
+ int i,j;
+ int k = get_network_output_size(net);
+ float *out = get_network_output(net);
+ float thresh = FLT_MAX;
+ for(i = 0; i < n; ++i){
+ float max = -FLT_MAX;
+ int max_i = -1;
+ for(j = 0; j < k; ++j){
+ float val = out[j];
+ if(val > max && val < thresh){
+ max = val;
+ max_i = j;
+ }
+ }
+ index[i] = max_i;
+ thresh = max;
+ }
+}
+
float *network_predict(network net, float *input)
{
forward_network(net, input, 0, 0);
--
Gitblit v1.10.0