Skip to content

Commit

Permalink
accidentally broke multigpu a while ago :-/
Browse files Browse the repository at this point in the history
  • Loading branch information
pjreddie committed Jun 26, 2017
1 parent 63ebd69 commit 59ed171
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 46 deletions.
2 changes: 1 addition & 1 deletion cfg/gru.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ adam=1

policy=constant
power=4
max_batches=400000
max_batches=1000000

[gru]
output = 1024
Expand Down
1 change: 1 addition & 0 deletions examples/classifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
cuda_set_device(gpus[i]);
#endif
nets[i] = load_network(cfgfile, weightfile, clear);
nets[i].learning_rate *= ngpus;
}
srand(time(0));
network net = nets[0];
Expand Down
2 changes: 1 addition & 1 deletion examples/darknet.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void denormalize_net(char *cfgfile, char *weightfile, char *outfile)
int i;
for (i = 0; i < net.n; ++i) {
layer l = net.layers[i];
if (l.type == CONVOLUTIONAL && l.batch_normalize) {
if ((l.type == DECONVOLUTIONAL || l.type == CONVOLUTIONAL) && l.batch_normalize) {
denormalize_convolutional_layer(l);
net.layers[i].batch_normalize=0;
}
Expand Down
1 change: 1 addition & 0 deletions examples/detector.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i
cuda_set_device(gpus[i]);
#endif
nets[i] = load_network(cfgfile, weightfile, clear);
nets[i].learning_rate *= ngpus;
}
srand(time(0));
network net = nets[0];
Expand Down
3 changes: 2 additions & 1 deletion examples/rnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ char **read_tokens(char *filename, size_t *read)
size = size*2;
d = realloc(d, size*sizeof(char *));
}
if(0==strcmp(line, "<NEWLINE>")) line = "\n";
d[count-1] = line;
}
fclose(fp);
Expand Down Expand Up @@ -190,7 +191,7 @@ void train_char_rnn(char *cfgfile, char *weightfile, char *filename, int clear,

for(j = 0; j < streams; ++j){
//printf("%d\n", j);
if(rand()%10 == 0){
if(rand()%64 == 0){
//fprintf(stderr, "Reset\n");
offsets[j] = rand_size_t()%size;
reset_rnn_state(net, j);
Expand Down
33 changes: 18 additions & 15 deletions examples/segmenter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <sys/time.h>
#include <assert.h>

void train_segmenter(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear)
void train_segmenter(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear, int display)
{
int i;

Expand Down Expand Up @@ -95,9 +95,9 @@ void train_segmenter(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
#else
loss = train_network(net, train);
#endif
if(1){
image tr = float_to_image(net.w/div, net.h/div, 80, train.y.vals[net.batch]);
image im = float_to_image(net.w, net.h, net.c, train.X.vals[net.batch]);
if(display){
image tr = float_to_image(net.w/div, net.h/div, 80, train.y.vals[net.batch*(net.subdivisions-1)]);
image im = float_to_image(net.w, net.h, net.c, train.X.vals[net.batch*(net.subdivisions-1)]);
image mask = mask_to_rgb(tr);
image prmask = mask_to_rgb(pred);
show_image(im, "input");
Expand Down Expand Up @@ -163,18 +163,18 @@ void predict_segmenter(char *datafile, char *cfgfile, char *weightfile, char *fi
float *X = sized.data;
time=clock();
float *predictions = network_predict(net, X);
image m = float_to_image(sized.w, sized.h, 81, predictions);
image rgb = mask_to_rgb(m);
image pred = get_network_image(net);
image prmask = mask_to_rgb(pred);
show_image(sized, "orig");
show_image(rgb, "pred");
show_image(prmask, "pred");
#ifdef OPENCV
cvWaitKey(0);
#endif
printf("Predicted: %f\n", predictions[0]);
printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
free_image(im);
free_image(sized);
free_image(rgb);
free_image(prmask);
if (filename) break;
}
}
Expand All @@ -183,7 +183,7 @@ void predict_segmenter(char *datafile, char *cfgfile, char *weightfile, char *fi
void demo_segmenter(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
{
#ifdef OPENCV
printf("Regressor Demo\n");
printf("Classifier Demo\n");
network net = parse_network_cfg(cfgfile);
if(weightfile){
load_weights(&net, weightfile);
Expand All @@ -200,8 +200,8 @@ void demo_segmenter(char *datacfg, char *cfgfile, char *weightfile, int cam_inde
}

if(!cap) error("Couldn't connect to webcam.\n");
cvNamedWindow("Regressor", CV_WINDOW_NORMAL);
cvResizeWindow("Regressor", 512, 512);
cvNamedWindow("Segmenter", CV_WINDOW_NORMAL);
cvResizeWindow("Segmenter", 512, 512);
float fps = 0;

while(1){
Expand All @@ -210,18 +210,20 @@ void demo_segmenter(char *datacfg, char *cfgfile, char *weightfile, int cam_inde

image in = get_image_from_stream(cap);
image in_s = letterbox_image(in, net.w, net.h);
show_image(in, "Regressor");

float *predictions = network_predict(net, in_s.data);

printf("\033[2J");
printf("\033[1;1H");
printf("\nFPS:%.0f\n",fps);

printf("People: %f\n", predictions[0]);

image pred = get_network_image(net);
image prmask = mask_to_rgb(pred);
show_image(prmask, "Segmenter");

free_image(in_s);
free_image(in);
free_image(prmask);

cvWaitKey(10);

Expand Down Expand Up @@ -266,12 +268,13 @@ void run_segmenter(int argc, char **argv)

int cam_index = find_int_arg(argc, argv, "-c", 0);
int clear = find_arg(argc, argv, "-clear");
int display = find_arg(argc, argv, "-display");
char *data = argv[3];
char *cfg = argv[4];
char *weights = (argc > 5) ? argv[5] : 0;
char *filename = (argc > 6) ? argv[6]: 0;
if(0==strcmp(argv[2], "test")) predict_segmenter(data, cfg, weights, filename);
else if(0==strcmp(argv[2], "train")) train_segmenter(data, cfg, weights, gpus, ngpus, clear);
else if(0==strcmp(argv[2], "train")) train_segmenter(data, cfg, weights, gpus, ngpus, clear, display);
else if(0==strcmp(argv[2], "demo")) demo_segmenter(data, cfg, weights, cam_index, filename);
}

Expand Down
48 changes: 48 additions & 0 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,54 @@ data load_data_seg(int n, char **paths, int m, int w, int h, int classes, int mi
d.X.cols = h*w*3;


d.y.rows = n;
d.y.cols = h*w*classes/div/div;
d.y.vals = calloc(d.X.rows, sizeof(float*));

for(i = 0; i < n; ++i){
image orig = load_image_color(random_paths[i], 0, 0);
augment_args a = random_augment_args(orig, angle, aspect, min, max, w, h);
image sized = rotate_crop_image(orig, a.rad, a.scale, a.w, a.h, a.dx, a.dy, a.aspect);

int flip = rand()%2;
if(flip) flip_image(sized);
random_distort_image(sized, hue, saturation, exposure);
d.X.vals[i] = sized.data;

image mask = get_segmentation_image(random_paths[i], orig.w, orig.h, classes);
//image mask = make_image(orig.w, orig.h, classes+1);
image sized_m = rotate_crop_image(mask, a.rad, a.scale/div, a.w/div, a.h/div, a.dx/div, a.dy/div, a.aspect);

if(flip) flip_image(sized_m);
d.y.vals[i] = sized_m.data;

free_image(orig);
free_image(mask);

/*
image rgb = mask_to_rgb(sized_m, classes);
show_image(rgb, "part");
show_image(sized, "orig");
cvWaitKey(0);
free_image(rgb);
*/
}
free(random_paths);
return d;
}

data load_data_iseg(int n, char **paths, int m, int w, int h, int classes, int min, int max, float angle, float aspect, float hue, float saturation, float exposure, int div)
{
char **random_paths = get_random_paths(paths, n, m);
int i;
data d = {0};
d.shallow = 0;

d.X.rows = n;
d.X.vals = calloc(d.X.rows, sizeof(float*));
d.X.cols = h*w*3;


d.y.rows = n;
d.y.cols = h*w*classes/div/div;
d.y.vals = calloc(d.X.rows, sizeof(float*));
Expand Down
35 changes: 25 additions & 10 deletions src/deconvolutional_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,20 @@ layer make_deconvolutional_layer(int batch, int h, int w, int c, int n, int size
l.output_gpu = cuda_make_array(l.output, l.batch*l.out_h*l.out_w*n);

if(batch_normalize){
l.mean_gpu = cuda_make_array(l.mean, n);
l.variance_gpu = cuda_make_array(l.variance, n);
l.mean_gpu = cuda_make_array(0, n);
l.variance_gpu = cuda_make_array(0, n);

l.rolling_mean_gpu = cuda_make_array(l.mean, n);
l.rolling_variance_gpu = cuda_make_array(l.variance, n);
l.rolling_mean_gpu = cuda_make_array(0, n);
l.rolling_variance_gpu = cuda_make_array(0, n);

l.mean_delta_gpu = cuda_make_array(l.mean, n);
l.variance_delta_gpu = cuda_make_array(l.variance, n);
l.mean_delta_gpu = cuda_make_array(0, n);
l.variance_delta_gpu = cuda_make_array(0, n);

l.scales_gpu = cuda_make_array(l.scales, n);
l.scale_updates_gpu = cuda_make_array(l.scale_updates, n);
l.scales_gpu = cuda_make_array(0, n);
l.scale_updates_gpu = cuda_make_array(0, n);

l.x_gpu = cuda_make_array(l.output, l.batch*l.out_h*l.out_w*n);
l.x_norm_gpu = cuda_make_array(l.output, l.batch*l.out_h*l.out_w*n);
l.x_gpu = cuda_make_array(0, l.batch*l.out_h*l.out_w*n);
l.x_norm_gpu = cuda_make_array(0, l.batch*l.out_h*l.out_w*n);
}
}
#ifdef CUDNN
Expand All @@ -144,6 +144,21 @@ layer make_deconvolutional_layer(int batch, int h, int w, int c, int n, int size
return l;
}

void denormalize_deconvolutional_layer(layer l)
{
int i, j;
for(i = 0; i < l.n; ++i){
float scale = l.scales[i]/sqrt(l.rolling_variance[i] + .00001);
for(j = 0; j < l.c*l.size*l.size; ++j){
l.weights[i*l.c*l.size*l.size + j] *= scale;
}
l.biases[i] -= l.rolling_mean[i] * scale;
l.scales[i] = 1;
l.rolling_mean[i] = 0;
l.rolling_variance[i] = 1;
}
}

void resize_deconvolutional_layer(layer *l, int h, int w)
{
l->h = h;
Expand Down
2 changes: 1 addition & 1 deletion src/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void draw_detections(image im, int num, float thresh, box *boxes, float **probs,
float prob = probs[i][class];
if(prob > thresh){

int width = im.h * .012;
int width = im.h * .006;

if(0){
width = pow(prob, 1./2.)*10+1;
Expand Down
5 changes: 5 additions & 0 deletions src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ void set_batch_network(network *net, int b)
if(net->layers[i].type == CONVOLUTIONAL){
cudnn_convolutional_setup(net->layers + i);
}
if(net->layers[i].type == DECONVOLUTIONAL){
layer *l = net->layers + i;
cudnnSetTensor4dDescriptor(l->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, 1, l->out_c, l->out_h, l->out_w);
cudnnSetTensor4dDescriptor(l->normTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, 1, l->out_c, 1, 1);
}
#endif
}
}
Expand Down
1 change: 0 additions & 1 deletion src/network_kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ float train_networks(network *nets, int n, data d, int interval)

float sum = 0;
for(i = 0; i < n; ++i){
nets[i].learning_rate *= n;
data p = get_data_part(d, i, n);
threads[i] = train_network_in_thread(nets[i], p, errors + i);
}
Expand Down
Loading

0 comments on commit 59ed171

Please sign in to comment.