Skip to content

Commit

Permalink
NO FUCKING SPOILERS DOUG
Browse files Browse the repository at this point in the history
  • Loading branch information
pjreddie committed Jan 16, 2018
1 parent 6e79145 commit e3931c7
Show file tree
Hide file tree
Showing 23 changed files with 450 additions and 90 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ CFLAGS+= -DCUDNN
LDFLAGS+= -lcudnn
endif

OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o
OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o l2norm_layer.o
EXECOBJA=captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o darknet.o
ifeq ($(GPU), 1)
LDFLAGS+= -lstdc++
Expand Down
12 changes: 4 additions & 8 deletions cfg/darknet.cfg
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[net]
# Train
# batch=128
# subdivisions=1
batch=128
subdivisions=1
# Test
batch=1
subdivisions=1
#batch=1
#subdivisions=1
height=256
width=256
channels=3
Expand Down Expand Up @@ -88,7 +88,6 @@ activation=leaky
[maxpool]
size=2
stride=2
padding=1

[convolutional]
batch_normalize=1
Expand All @@ -110,6 +109,3 @@ activation=leaky
[softmax]
groups=1

[cost]
type=sse

26 changes: 20 additions & 6 deletions cfg/darknet19.cfg
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
[net]
batch=128
subdivisions=1
height=224
width=224
# Training
#batch=128
#subdivisions=2

# Testing
batch=1
subdivisions=1

height=256
width=256
min_crop=128
max_crop=448
channels=3
momentum=0.9
decay=0.0005
max_crop=448

burn_in=1000
learning_rate=0.1
policy=poly
power=4
max_batches=1600000
max_batches=800000

angle=7
hue=.1
saturation=.75
exposure=.75
aspect=.75

[convolutional]
batch_normalize=1
Expand Down
2 changes: 2 additions & 0 deletions examples/classifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *fi
}
image im = load_image_color(input, 0, 0);
image r = letterbox_image(im, net->w, net->h);
//image r = resize_min(im, 320);
//printf("%d %d\n", r.w, r.h);
//resize_network(net, r.w, r.h);
//printf("%d %d\n", r.w, r.h);

Expand Down
120 changes: 111 additions & 9 deletions examples/lsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,92 @@ void train_pix2pix(char *cfg, char *weight, char *acfg, char *aweight, int clear
}
*/

void slerp(float *start, float *end, float s, int n, float *out)
{
float omega = acos(dot_cpu(n, start, 1, end, 1));
float so = sin(omega);
fill_cpu(n, 0, out, 1);
axpy_cpu(n, sin((1-s)*omega)/so, start, 1, out, 1);
axpy_cpu(n, sin(s*omega)/so, end, 1, out, 1);

float mag = mag_array(out, n);
scale_array(out, n, 1./mag);
}

image random_unit_vector_image(w, h, c)
{
image im = make_image(w, h, c);
int i;
for(i = 0; i < im.w*im.h*im.c; ++i){
im.data[i] = rand_normal();
}
float mag = mag_array(im.data, im.w*im.h*im.c);
scale_array(im.data, im.w*im.h*im.c, 1./mag);
return im;
}

void inter_dcgan(char *cfgfile, char *weightfile)
{
network *net = load_network(cfgfile, weightfile, 0);
set_batch_network(net, 1);
srand(2222222);

clock_t time;
char buff[256];
char *input = buff;
int i, imlayer = 0;

for (i = 0; i < net->n; ++i) {
if (net->layers[i].out_c == 3) {
imlayer = i;
printf("%d\n", i);
break;
}
}
image start = random_unit_vector_image(net->w, net->h, net->c);
image end = random_unit_vector_image(net->w, net->h, net->c);
image im = make_image(net->w, net->h, net->c);
image orig = copy_image(start);

int c = 0;
int count = 0;
int max_count = 15;
while(1){
++c;

if(count == max_count){
count = 0;
free_image(start);
start = end;
end = random_unit_vector_image(net->w, net->h, net->c);
if(c > 300){
end = orig;
}
if(c>300 + max_count) return;
}
++count;

slerp(start.data, end.data, (float)count / max_count, im.w*im.h*im.c, im.data);

float *X = im.data;
time=clock();
network_predict(net, X);
image out = get_network_image_layer(net, imlayer);
//yuv_to_rgb(out);
normalize_image(out);
printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
//char buff[256];
sprintf(buff, "out%05d", c);
show_image(out, "out");
save_image(out, "out");
save_image(out, buff);
#ifdef OPENCV
//cvWaitKey(0);
#endif

}
}

void test_dcgan(char *cfgfile, char *weightfile)
{
network *net = load_network(cfgfile, weightfile, 0);
Expand All @@ -409,7 +495,7 @@ void test_dcgan(char *cfgfile, char *weightfile)
im.data[i] = rand_normal();
}
float mag = mag_array(im.data, im.w*im.h*im.c);
//scale_array(im.data, im.w*im.h*im.c, 1./mag);
scale_array(im.data, im.w*im.h*im.c, 1./mag);

float *X = im.data;
time=clock();
Expand All @@ -429,7 +515,7 @@ void test_dcgan(char *cfgfile, char *weightfile)
}


void train_dcgan(char *cfg, char *weight, char *acfg, char *aweight, int clear, int display, char *train_images)
void train_dcgan(char *cfg, char *weight, char *acfg, char *aweight, int clear, int display, char *train_images, int maxbatch)
{
#ifdef GPU
char *backup_directory = "/home/pjreddie/backup/";
Expand All @@ -441,7 +527,6 @@ void train_dcgan(char *cfg, char *weight, char *acfg, char *aweight, int clear,
network *anet = load_network(acfg, aweight, clear);
//float orig_rate = anet->learning_rate;

int start = 0;
int i, j, k;
layer imlayer = {0};
for (i = 0; i < gnet->n; ++i) {
Expand Down Expand Up @@ -488,8 +573,8 @@ void train_dcgan(char *cfg, char *weight, char *acfg, char *aweight, int clear,

//data generated = copy_data(train);

while (get_current_batch(gnet) < gnet->max_batches) {
start += 1;
if (maxbatch == 0) maxbatch = gnet->max_batches;
while (get_current_batch(gnet) < maxbatch) {
i += 1;
time=clock();
pthread_join(load_thread, 0);
Expand Down Expand Up @@ -519,6 +604,13 @@ void train_dcgan(char *cfg, char *weight, char *acfg, char *aweight, int clear,
float mag = mag_array(gnet->input + z*gnet->inputs, gnet->inputs);
scale_array(gnet->input + z*gnet->inputs, gnet->inputs, 1./mag);
}
/*
for(z = 0; z < 100; ++z){
printf("%f, ", gnet->input[z]);
}
printf("\n");
printf("input: %f %f\n", mean_array(gnet->input, x_size), variance_array(gnet->input, x_size));
*/

//cuda_push_array(gnet->input_gpu, gnet->input, x_size);
//cuda_push_array(gnet->truth_gpu, gnet->truth, y_size);
Expand All @@ -533,18 +625,26 @@ void train_dcgan(char *cfg, char *weight, char *acfg, char *aweight, int clear,
backward_network(anet);

float genaloss = *anet->cost / anet->batch;
printf("%f\n", genaloss);
//printf("%f\n", genaloss);

scal_gpu(imlayer.outputs*imlayer.batch, 1, imerror, 1);
scal_gpu(imlayer.outputs*imlayer.batch, 0, gnet->layers[gnet->n-1].delta_gpu, 1);

printf("realness %f\n", cuda_mag_array(imerror, imlayer.outputs*imlayer.batch));
printf("features %f\n", cuda_mag_array(gnet->layers[gnet->n-1].delta_gpu, imlayer.outputs*imlayer.batch));
//printf("realness %f\n", cuda_mag_array(imerror, imlayer.outputs*imlayer.batch));
//printf("features %f\n", cuda_mag_array(gnet->layers[gnet->n-1].delta_gpu, imlayer.outputs*imlayer.batch));

axpy_gpu(imlayer.outputs*imlayer.batch, 1, imerror, 1, gnet->layers[gnet->n-1].delta_gpu, 1);

backward_network(gnet);

/*
for(k = 0; k < gnet->n; ++k){
layer l = gnet->layers[k];
cuda_pull_array(l.output_gpu, l.output, l.outputs*l.batch);
printf("%d: %f %f\n", k, mean_array(l.output, l.outputs*l.batch), variance_array(l.output, l.outputs*l.batch));
}
*/

for(k = 0; k < gnet->batch; ++k){
int index = j*gnet->batch + k;
copy_cpu(gnet->outputs, gnet->output + k*gnet->outputs, 1, gen.X.vals[index], 1);
Expand Down Expand Up @@ -1104,6 +1204,7 @@ void run_lsd(int argc, char **argv)

int clear = find_arg(argc, argv, "-clear");
int display = find_arg(argc, argv, "-display");
int batches = find_int_arg(argc, argv, "-b", 0);
char *file = find_char_arg(argc, argv, "-file", "/home/pjreddie/data/imagenet/imagenet1k.train.list");

char *cfg = argv[3];
Expand All @@ -1115,9 +1216,10 @@ void run_lsd(int argc, char **argv)
//else if(0==strcmp(argv[2], "train2")) train_lsd2(cfg, weights, acfg, aweights, clear);
//else if(0==strcmp(argv[2], "traincolor")) train_colorizer(cfg, weights, acfg, aweights, clear);
//else if(0==strcmp(argv[2], "train3")) train_lsd3(argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], clear);
if(0==strcmp(argv[2], "traingan")) train_dcgan(cfg, weights, acfg, aweights, clear, display, file);
if(0==strcmp(argv[2], "traingan")) train_dcgan(cfg, weights, acfg, aweights, clear, display, file, batches);
else if(0==strcmp(argv[2], "traincolor")) train_colorizer(cfg, weights, acfg, aweights, clear, display);
else if(0==strcmp(argv[2], "gan")) test_dcgan(cfg, weights);
else if(0==strcmp(argv[2], "inter")) inter_dcgan(cfg, weights);
else if(0==strcmp(argv[2], "test")) test_lsd(cfg, weights, filename, 0);
else if(0==strcmp(argv[2], "color")) test_lsd(cfg, weights, filename, 1);
/*
Expand Down
6 changes: 5 additions & 1 deletion include/darknet.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ typedef enum {
REORG,
UPSAMPLE,
LOGXENT,
L2NORM,
BLANK
} LAYER_TYPE;

typedef enum{
SSE, MASKED, L1, SEG, SMOOTH
SSE, MASKED, L1, SEG, SMOOTH,WGAN
} COST_TYPE;

typedef struct{
Expand Down Expand Up @@ -162,6 +163,7 @@ struct layer{
float shift;
float ratio;
float learning_rate_scale;
float clip;
int softmax;
int classes;
int coords;
Expand Down Expand Up @@ -475,6 +477,7 @@ typedef struct network{
int train;
int index;
float *cost;
float clip;

#ifdef GPU
float *input_gpu;
Expand Down Expand Up @@ -604,6 +607,7 @@ void backward_network(network *net);
void update_network(network *net);


float dot_cpu(int N, float *X, int INCX, float *Y, int INCY);
void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY);
void copy_cpu(int N, float *X, int INCX, float *Y, int INCY);
void scal_cpu(int N, float ALPHA, float *X, int INCX);
Expand Down
39 changes: 39 additions & 0 deletions src/blas.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ void variance_cpu(float *x, float *mean, int batch, int filters, int spatial, fl
}
}

void l2normalize_cpu(float *x, float *dx, int batch, int filters, int spatial)
{
int b,f,i;
for(b = 0; b < batch; ++b){
for(i = 0; i < spatial; ++i){
float sum = 0;
for(f = 0; f < filters; ++f){
int index = b*filters*spatial + f*spatial + i;
sum += powf(x[index], 2);
}
sum = sqrtf(sum);
for(f = 0; f < filters; ++f){
int index = b*filters*spatial + f*spatial + i;
x[index] /= sum;
dx[index] = (1 - x[index]) / sum;
}
}
}
}


void normalize_cpu(float *x, float *mean, float *variance, int batch, int filters, int spatial)
{
int b, f, i;
Expand Down Expand Up @@ -310,3 +331,21 @@ void softmax_cpu(float *input, int n, int batch, int batch_offset, int groups, i
}
}

void upsample_cpu(float *in, int w, int h, int c, int batch, int stride, int forward, float *out)
{
int i, j, k, b;
for(b = 0; b < batch; ++b){
for(k = 0; k < c; ++k){
for(j = 0; j < h*stride; ++j){
for(i = 0; i < w*stride; ++i){
int in_index = b*w*h*c + k*w*h + (j/stride)*w + i/stride;
int out_index = b*w*h*c + k*w*h + j*w + i;
if(forward) out[out_index] = in[in_index];
else in[in_index] += out[out_index];
}
}
}
}
}


Loading

0 comments on commit e3931c7

Please sign in to comment.