diff --git a/Makefile b/Makefile index 25a85f81..41e5fc8d 100644 --- a/Makefile +++ b/Makefile @@ -118,7 +118,7 @@ LDFLAGS+= -L/usr/local/zed/lib -lsl_core -lsl_input -lsl_zed #-lstdc++ -D_GLIBCXX_USE_CXX11_ABI=0 endif -OBJ=image_opencv.o http_stream.o gemm.o utils.o dark_cuda.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 darknet.o detection_layer.o captcha.o route_layer.o writing.o box.o nightmare.o normalization_layer.o avgpool_layer.o coco.o dice.o yolo.o detector.o layer.o compare.o classifier.o local_layer.o swag.o shortcut_layer.o activation_layer.o rnn_layer.o gru_layer.o rnn.o rnn_vid.o crnn_layer.o demo.o tag.o cifar.o go.o batchnorm_layer.o art.o region_layer.o reorg_layer.o reorg_old_layer.o super.o voxel.o tree.o yolo_layer.o upsample_layer.o lstm_layer.o conv_lstm_layer.o scale_channels_layer.o sam_layer.o +OBJ=image_opencv.o http_stream.o gemm.o utils.o dark_cuda.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 darknet.o detection_layer.o captcha.o route_layer.o writing.o box.o nightmare.o normalization_layer.o avgpool_layer.o coco.o dice.o yolo.o detector.o layer.o compare.o classifier.o local_layer.o swag.o shortcut_layer.o activation_layer.o rnn_layer.o gru_layer.o rnn.o rnn_vid.o crnn_layer.o demo.o tag.o cifar.o go.o batchnorm_layer.o art.o region_layer.o reorg_layer.o reorg_old_layer.o super.o voxel.o tree.o yolo_layer.o gaussian_yolo_layer.o upsample_layer.o lstm_layer.o conv_lstm_layer.o scale_channels_layer.o sam_layer.o ifeq ($(GPU), 1) LDFLAGS+= -lstdc++ OBJ+=convolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o network_kernels.o avgpool_layer_kernels.o diff --git a/build/darknet/darknet.vcxproj b/build/darknet/darknet.vcxproj index d7dc9159..b685bebd 100644 --- a/build/darknet/darknet.vcxproj +++ b/build/darknet/darknet.vcxproj @@ -199,6 +199,7 @@ + @@ -263,6 +264,7 @@ + diff --git a/include/darknet.h b/include/darknet.h index e78abe6a..00b49921 100644 --- a/include/darknet.h +++ b/include/darknet.h @@ -149,6 +149,7 @@ typedef enum { XNOR, REGION, YOLO, + GAUSSIAN_YOLO, ISEG, REORG, REORG_OLD, @@ -728,6 +729,7 @@ typedef struct detection{ float *mask; float objectness; int sort_class; + float *uc; // Gaussian_YOLOv3 - tx,ty,tw,th uncertainty } detection; // matrix.h diff --git a/src/box.c b/src/box.c index 1b5c4998..c6a27ed5 100644 --- a/src/box.c +++ b/src/box.c @@ -13,6 +13,16 @@ box float_to_box(float *f) return b; } +box float_to_box_stride(float *f, int stride) +{ + box b = { 0 }; + b.x = f[0]; + b.y = f[1 * stride]; + b.w = f[2 * stride]; + b.h = f[3 * stride]; + return b; +} + dbox derivative(box a, box b) { dbox d; diff --git a/src/box.h b/src/box.h index 2392fedd..172c1352 100644 --- a/src/box.h +++ b/src/box.h @@ -31,6 +31,7 @@ typedef struct detection_with_class { extern "C" { #endif box float_to_box(float *f); +box float_to_box_stride(float *f, int stride); float box_iou(box a, box b); float box_rmse(box a, box b); dxrep dx_box_iou(box a, box b, IOU_LOSS iou_loss); diff --git a/src/convolutional_kernels.cu b/src/convolutional_kernels.cu index edfb03b8..23005ccb 100644 --- a/src/convolutional_kernels.cu +++ b/src/convolutional_kernels.cu @@ -10,6 +10,7 @@ #include "col2im.h" #include "utils.h" #include "dark_cuda.h" +#include "box.h" __global__ void binarize_kernel(float *x, int n, float *binary) @@ -892,16 +893,6 @@ void backward_convolutional_layer_gpu(convolutional_layer l, network_state state } } -static box float_to_box_stride(float *f, int stride) -{ - box b = { 0 }; - b.x = f[0]; - b.y = f[1 * stride]; - b.w = f[2 * stride]; - b.h = f[3 * stride]; - return b; -} - __global__ void calc_avg_activation_kernel(float *src, float *dst, int size, int channels, int batches) { int i = blockIdx.x * blockDim.x + threadIdx.x; diff --git a/src/convolutional_layer.c b/src/convolutional_layer.c index 8bce5aa6..6818b603 100644 --- a/src/convolutional_layer.c +++ b/src/convolutional_layer.c @@ -5,6 +5,7 @@ #include "col2im.h" #include "blas.h" #include "gemm.h" +#include "box.h" #include #include @@ -1171,16 +1172,6 @@ void forward_convolutional_layer(convolutional_layer l, network_state state) } } -static box float_to_box_stride(float *f, int stride) -{ - box b = { 0 }; - b.x = f[0]; - b.y = f[1 * stride]; - b.w = f[2 * stride]; - b.h = f[3 * stride]; - return b; -} - void assisted_excitation_forward(convolutional_layer l, network_state state) { const int iteration_num = (*state.net.seen) / (state.net.batch*state.net.subdivisions); diff --git a/src/data.c b/src/data.c index 7cb7bf0a..c0af1ab8 100644 --- a/src/data.c +++ b/src/data.c @@ -2,6 +2,7 @@ #include "utils.h" #include "image.h" #include "dark_cuda.h" +#include "box.h" #include #include @@ -779,16 +780,6 @@ data load_data_swag(char **paths, int n, int classes, float jitter) return d; } -static box float_to_box_stride(float *f, int stride) -{ - box b = { 0 }; - b.x = f[0]; - b.y = f[1 * stride]; - b.w = f[2 * stride]; - b.h = f[3 * stride]; - return b; -} - void blend_truth(float *new_truth, int boxes, float *old_truth) { const int t_size = 4 + 1; diff --git a/src/gaussian_yolo_layer.c b/src/gaussian_yolo_layer.c new file mode 100644 index 00000000..32083401 --- /dev/null +++ b/src/gaussian_yolo_layer.c @@ -0,0 +1,445 @@ +// Gaussian YOLOv3 implementation +// Author: Jiwoong Choi +// ICCV 2019 Paper: http://openaccess.thecvf.com/content_ICCV_2019/html/Choi_Gaussian_YOLOv3_An_Accurate_and_Fast_Object_Detector_Using_Localization_ICCV_2019_paper.html +// arxiv.org: https://arxiv.org/abs/1904.04620v2 +// source code: https://github.com/jwchoi384/Gaussian_YOLOv3 + +#include "gaussian_yolo_layer.h" +#include "activations.h" +#include "blas.h" +#include "box.h" +#include "dark_cuda.h" +#include "utils.h" + +#include +#include +#include +#include + +#ifndef M_PI +#define M_PI 3.141592 +#endif + +layer make_gaussian_yolo_layer(int batch, int w, int h, int n, int total, int *mask, int classes) +{ + int i; + layer l = {0}; + l.type = GAUSSIAN_YOLO; + + l.n = n; + l.total = total; + l.batch = batch; + l.h = h; + l.w = w; + l.c = n*(classes + 8 + 1); + l.out_w = l.w; + l.out_h = l.h; + l.out_c = l.c; + l.classes = classes; + l.cost = calloc(1, sizeof(float)); + l.biases = calloc(total*2, sizeof(float)); + if(mask) l.mask = mask; + else{ + l.mask = calloc(n, sizeof(int)); + for(i = 0; i < n; ++i){ + l.mask[i] = i; + } + } + l.bias_updates = calloc(n*2, sizeof(float)); + l.outputs = h*w*n*(classes + 8 + 1); + l.inputs = l.outputs; + l.truths = 90*(4 + 1); + l.delta = calloc(batch*l.outputs, sizeof(float)); + l.output = calloc(batch*l.outputs, sizeof(float)); + for(i = 0; i < total*2; ++i){ + l.biases[i] = .5; + } + + l.forward = forward_gaussian_yolo_layer; + l.backward = backward_gaussian_yolo_layer; +#ifdef GPU + l.forward_gpu = forward_gaussian_yolo_layer_gpu; + l.backward_gpu = backward_gaussian_yolo_layer_gpu; + l.output_gpu = cuda_make_array(l.output, batch*l.outputs); + l.delta_gpu = cuda_make_array(l.delta, batch*l.outputs); +#endif + + fprintf(stderr, "Gaussian_yolo\n"); + srand(0); + + return l; +} + +void resize_gaussian_yolo_layer(layer *l, int w, int h) +{ + l->w = w; + l->h = h; + + l->outputs = h*w*l->n*(l->classes + 8 + 1); + l->inputs = l->outputs; + + l->output = realloc(l->output, l->batch*l->outputs*sizeof(float)); + l->delta = realloc(l->delta, l->batch*l->outputs*sizeof(float)); + +#ifdef GPU + cuda_free(l->delta_gpu); + cuda_free(l->output_gpu); + + l->delta_gpu = cuda_make_array(l->delta, l->batch*l->outputs); + l->output_gpu = cuda_make_array(l->output, l->batch*l->outputs); +#endif +} + +box get_gaussian_yolo_box(float *x, float *biases, int n, int index, int i, int j, int lw, int lh, int w, int h, int stride) +{ + box b; + b.x = (i + x[index + 0*stride]) / lw; + b.y = (j + x[index + 2*stride]) / lh; + b.w = exp(x[index + 4*stride]) * biases[2*n] / w; + b.h = exp(x[index + 6*stride]) * biases[2*n+1] / h; + return b; +} + +float delta_gaussian_yolo_box(box truth, float *x, float *biases, int n, int index, int i, int j, int lw, int lh, int w, int h, float *delta, float scale, int stride) +{ + box pred = get_gaussian_yolo_box(x, biases, n, index, i, j, lw, lh, w, h, stride); + float iou = box_iou(pred, truth); + + float tx = (truth.x*lw - i); + float ty = (truth.y*lh - j); + float tw = log(truth.w*w / biases[2*n]); + float th = log(truth.h*h / biases[2*n + 1]); + + float sigma_const = 0.3; + float epsi = pow(10,-9); + + float in_exp_x = (tx - x[index + 0*stride])/x[index+1*stride]; + float in_exp_x_2 = pow(in_exp_x, 2); + float normal_dist_x = exp(in_exp_x_2*(-1./2.))/(sqrt(M_PI * 2.0)*(x[index+1*stride]+sigma_const)); + + float in_exp_y = (ty - x[index + 2*stride])/x[index+3*stride]; + float in_exp_y_2 = pow(in_exp_y, 2); + float normal_dist_y = exp(in_exp_y_2*(-1./2.))/(sqrt(M_PI * 2.0)*(x[index+3*stride]+sigma_const)); + + float in_exp_w = (tw - x[index + 4*stride])/x[index+5*stride]; + float in_exp_w_2 = pow(in_exp_w, 2); + float normal_dist_w = exp(in_exp_w_2*(-1./2.))/(sqrt(M_PI * 2.0)*(x[index+5*stride]+sigma_const)); + + float in_exp_h = (th - x[index + 6*stride])/x[index+7*stride]; + float in_exp_h_2 = pow(in_exp_h, 2); + float normal_dist_h = exp(in_exp_h_2*(-1./2.))/(sqrt(M_PI * 2.0)*(x[index+7*stride]+sigma_const)); + + float temp_x = (1./2.) * 1./(normal_dist_x+epsi) * normal_dist_x * scale; + float temp_y = (1./2.) * 1./(normal_dist_y+epsi) * normal_dist_y * scale; + float temp_w = (1./2.) * 1./(normal_dist_w+epsi) * normal_dist_w * scale; + float temp_h = (1./2.) * 1./(normal_dist_h+epsi) * normal_dist_h * scale; + + delta[index + 0*stride] = temp_x * in_exp_x * (1./x[index+1*stride]); + delta[index + 2*stride] = temp_y * in_exp_y * (1./x[index+3*stride]); + delta[index + 4*stride] = temp_w * in_exp_w * (1./x[index+5*stride]); + delta[index + 6*stride] = temp_h * in_exp_h * (1./x[index+7*stride]); + + delta[index + 1*stride] = temp_x * (in_exp_x_2/x[index+1*stride] - 1./(x[index+1*stride]+sigma_const)); + delta[index + 3*stride] = temp_y * (in_exp_y_2/x[index+3*stride] - 1./(x[index+3*stride]+sigma_const)); + delta[index + 5*stride] = temp_w * (in_exp_w_2/x[index+5*stride] - 1./(x[index+5*stride]+sigma_const)); + delta[index + 7*stride] = temp_h * (in_exp_h_2/x[index+7*stride] - 1./(x[index+7*stride]+sigma_const)); + return iou; +} + + +void delta_gaussian_yolo_class(float *output, float *delta, int index, int class, int classes, int stride, float *avg_cat) +{ + int n; + if (delta[index]){ + delta[index + stride*class] = 1 - output[index + stride*class]; + if(avg_cat) *avg_cat += output[index + stride*class]; + return; + } + for(n = 0; n < classes; ++n){ + delta[index + stride*n] = ((n == class)?1 : 0) - output[index + stride*n]; + if(n == class && avg_cat) *avg_cat += output[index + stride*n]; + } +} + +static int entry_gaussian_index(layer l, int batch, int location, int entry) +{ + int n = location / (l.w*l.h); + int loc = location % (l.w*l.h); + return batch*l.outputs + n*l.w*l.h*(8+l.classes+1) + entry*l.w*l.h + loc; +} + +void forward_gaussian_yolo_layer(const layer l, network net) +{ + int i,j,b,t,n; + memcpy(l.output, net.input, l.outputs*l.batch*sizeof(float)); + +#ifndef GPU + for (b = 0; b < l.batch; ++b){ + for(n = 0; n < l.n; ++n){ + // x : mu, sigma + int index = entry_gaussian_index(l, b, n*l.w*l.h, 0); + activate_array(l.output + index, 2*l.w*l.h, LOGISTIC); + // y : mu, sigma + index = entry_gaussian_index(l, b, n*l.w*l.h, 2); + activate_array(l.output + index, 2*l.w*l.h, LOGISTIC); + // w : sigma + index = entry_gaussian_index(l, b, n*l.w*l.h, 5); + activate_array(l.output + index, l.w*l.h, LOGISTIC); + // h : sigma + index = entry_gaussian_index(l, b, n*l.w*l.h, 7); + activate_array(l.output + index, l.w*l.h, LOGISTIC); + // objectness & class + index = entry_gaussian_index(l, b, n*l.w*l.h, 8); + activate_array(l.output + index, (1+l.classes)*l.w*l.h, LOGISTIC); + } + } +#endif + + memset(l.delta, 0, l.outputs * l.batch * sizeof(float)); + if(!net.train) return; + float avg_iou = 0; + float recall = 0; + float recall75 = 0; + float avg_cat = 0; + float avg_obj = 0; + float avg_anyobj = 0; + int count = 0; + int class_count = 0; + *(l.cost) = 0; + for (b = 0; b < l.batch; ++b) { + for (j = 0; j < l.h; ++j) { + for (i = 0; i < l.w; ++i) { + for (n = 0; n < l.n; ++n) { + int box_index = entry_gaussian_index(l, b, n*l.w*l.h + j*l.w + i, 0); + box pred = get_gaussian_yolo_box(l.output, l.biases, l.mask[n], box_index, i, j, l.w, l.h, net.w, net.h, l.w*l.h); + float best_iou = 0; + int best_t = 0; + for(t = 0; t < l.max_boxes; ++t){ + box truth = float_to_box_stride(net.truth + t*(4 + 1) + b*l.truths, 1); + if(!truth.x) break; + float iou = box_iou(pred, truth); + if (iou > best_iou) { + best_iou = iou; + best_t = t; + } + } + int obj_index = entry_gaussian_index(l, b, n*l.w*l.h + j*l.w + i, 8); + avg_anyobj += l.output[obj_index]; + l.delta[obj_index] = 0 - l.output[obj_index]; + if (best_iou > l.ignore_thresh) { + l.delta[obj_index] = 0; + } + if (best_iou > l.truth_thresh) { + l.delta[obj_index] = 1 - l.output[obj_index]; + + int class = net.truth[best_t*(4 + 1) + b*l.truths + 4]; + if (l.map) class = l.map[class]; + int class_index = entry_gaussian_index(l, b, n*l.w*l.h + j*l.w + i, 9); + delta_gaussian_yolo_class(l.output, l.delta, class_index, class, l.classes, l.w*l.h, 0); + box truth = float_to_box_stride(net.truth + best_t*(4 + 1) + b*l.truths, 1); + delta_gaussian_yolo_box(truth, l.output, l.biases, l.mask[n], box_index, i, j, l.w, l.h, net.w, net.h, l.delta, (2-truth.w*truth.h), l.w*l.h); + } + } + } + } + for(t = 0; t < l.max_boxes; ++t){ + box truth = float_to_box_stride(net.truth + t*(4 + 1) + b*l.truths, 1); + + if(!truth.x) break; + float best_iou = 0; + int best_n = 0; + i = (truth.x * l.w); + j = (truth.y * l.h); + box truth_shift = truth; + truth_shift.x = truth_shift.y = 0; + for(n = 0; n < l.total; ++n){ + box pred = {0}; + pred.w = l.biases[2*n]/net.w; + pred.h = l.biases[2*n+1]/net.h; + float iou = box_iou(pred, truth_shift); + if (iou > best_iou){ + best_iou = iou; + best_n = n; + } + } + + int mask_n = int_index(l.mask, best_n, l.n); + if(mask_n >= 0){ + int box_index = entry_gaussian_index(l, b, mask_n*l.w*l.h + j*l.w + i, 0); + float iou = delta_gaussian_yolo_box(truth, l.output, l.biases, best_n, box_index, i, j, l.w, l.h, net.w, net.h, l.delta, (2-truth.w*truth.h), l.w*l.h); + + int obj_index = entry_gaussian_index(l, b, mask_n*l.w*l.h + j*l.w + i, 8); + avg_obj += l.output[obj_index]; + l.delta[obj_index] = 1 - l.output[obj_index]; + + int class = net.truth[t*(4 + 1) + b*l.truths + 4]; + if (l.map) class = l.map[class]; + int class_index = entry_gaussian_index(l, b, mask_n*l.w*l.h + j*l.w + i, 9); + delta_gaussian_yolo_class(l.output, l.delta, class_index, class, l.classes, l.w*l.h, &avg_cat); + + ++count; + ++class_count; + if(iou > .5) recall += 1; + if(iou > .75) recall75 += 1; + avg_iou += iou; + } + } + } + *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2); + printf("Region %d Avg IOU: %f, Class: %f, Obj: %f, No Obj: %f, .5R: %f, .75R: %f, count: %d\n", net.index, avg_iou/count, avg_cat/class_count, avg_obj/count, avg_anyobj/(l.w*l.h*l.n*l.batch), recall/count, recall75/count, count); +} + +void backward_gaussian_yolo_layer(const layer l, network net) +{ + axpy_cpu(l.batch*l.inputs, 1, l.delta, 1, net.delta, 1); +} + +void correct_gaussian_yolo_boxes(detection *dets, int n, int w, int h, int netw, int neth, int relative) +{ + int i; + int new_w=0; + int new_h=0; + if (((float)netw/w) < ((float)neth/h)) { + new_w = netw; + new_h = (h * netw)/w; + } else { + new_h = neth; + new_w = (w * neth)/h; + } + for (i = 0; i < n; ++i){ + box b = dets[i].bbox; + b.x = (b.x - (netw - new_w)/2./netw) / ((float)new_w/netw); + b.y = (b.y - (neth - new_h)/2./neth) / ((float)new_h/neth); + b.w *= (float)netw/new_w; + b.h *= (float)neth/new_h; + if(!relative){ + b.x *= w; + b.w *= w; + b.y *= h; + b.h *= h; + } + dets[i].bbox = b; + } +} + +int gaussian_yolo_num_detections(layer l, float thresh) +{ + int i, n; + int count = 0; + for (i = 0; i < l.w*l.h; ++i){ + for(n = 0; n < l.n; ++n){ + int obj_index = entry_gaussian_index(l, 0, n*l.w*l.h + i, 8); + if(l.output[obj_index] > thresh){ + ++count; + } + } + } + return count; +} + +/* +void avg_flipped_gaussian_yolo(layer l) +{ + int i,j,n,z; + float *flip = l.output + l.outputs; + for (j = 0; j < l.h; ++j) { + for (i = 0; i < l.w/2; ++i) { + for (n = 0; n < l.n; ++n) { + for(z = 0; z < l.classes + 8 + 1; ++z){ + int i1 = z*l.w*l.h*l.n + n*l.w*l.h + j*l.w + i; + int i2 = z*l.w*l.h*l.n + n*l.w*l.h + j*l.w + (l.w - i - 1); + float swap = flip[i1]; + flip[i1] = flip[i2]; + flip[i2] = swap; + if(z == 0){ + flip[i1] = -flip[i1]; + flip[i2] = -flip[i2]; + } + } + } + } + } + for(i = 0; i < l.outputs; ++i){ + l.output[i] = (l.output[i] + flip[i])/2.; + } +} +*/ + +int get_gaussian_yolo_detections(layer l, int w, int h, int netw, int neth, float thresh, int *map, int relative, detection *dets) +{ + int i,j,n; + float *predictions = l.output; + //if (l.batch == 2) avg_flipped_gaussian_yolo(l); + int count = 0; + for (i = 0; i < l.w*l.h; ++i){ + int row = i / l.w; + int col = i % l.w; + for(n = 0; n < l.n; ++n){ + int obj_index = entry_gaussian_index(l, 0, n*l.w*l.h + i, 8); + float objectness = predictions[obj_index]; + if(objectness <= thresh) continue; + int box_index = entry_gaussian_index(l, 0, n*l.w*l.h + i, 0); + dets[count].bbox = get_gaussian_yolo_box(predictions, l.biases, l.mask[n], box_index, col, row, l.w, l.h, netw, neth, l.w*l.h); + dets[count].objectness = objectness; + dets[count].classes = l.classes; + + dets[count].uc[0] = predictions[entry_gaussian_index(l, 0, n*l.w*l.h + i, 1)]; // tx uncertainty + dets[count].uc[1] = predictions[entry_gaussian_index(l, 0, n*l.w*l.h + i, 3)]; // ty uncertainty + dets[count].uc[2] = predictions[entry_gaussian_index(l, 0, n*l.w*l.h + i, 5)]; // tw uncertainty + dets[count].uc[3] = predictions[entry_gaussian_index(l, 0, n*l.w*l.h + i, 7)]; // th uncertainty + + for(j = 0; j < l.classes; ++j){ + int class_index = entry_gaussian_index(l, 0, n*l.w*l.h + i, 9 + j); + float uc_aver = (dets[count].uc[0] + dets[count].uc[1] + dets[count].uc[2] + dets[count].uc[3])/4.0; + float prob = objectness*predictions[class_index]*(1.0-uc_aver); + dets[count].prob[j] = (prob > thresh) ? prob : 0; + } + ++count; + } + } + correct_gaussian_yolo_boxes(dets, count, w, h, netw, neth, relative); + return count; +} + +#ifdef GPU + +void forward_gaussian_yolo_layer_gpu(const layer l, network net) +{ + copy_ongpu(l.batch*l.inputs, net.input_gpu, 1, l.output_gpu, 1); + int b, n; + for (b = 0; b < l.batch; ++b) + { + for(n = 0; n < l.n; ++n) + { + // x : mu, sigma + int index = entry_gaussian_index(l, b, n*l.w*l.h, 0); + activate_array_ongpu(l.output_gpu + index, 2*l.w*l.h, LOGISTIC); + // y : mu, sigma + index = entry_gaussian_index(l, b, n*l.w*l.h, 2); + activate_array_ongpu(l.output_gpu + index, 2*l.w*l.h, LOGISTIC); + // w : sigma + index = entry_gaussian_index(l, b, n*l.w*l.h, 5); + activate_array_ongpu(l.output_gpu + index, l.w*l.h, LOGISTIC); + // h : sigma + index = entry_gaussian_index(l, b, n*l.w*l.h, 7); + activate_array_ongpu(l.output_gpu + index, l.w*l.h, LOGISTIC); + // objectness & class + index = entry_gaussian_index(l, b, n*l.w*l.h, 8); + activate_array_ongpu(l.output_gpu + index, (1+l.classes)*l.w*l.h, LOGISTIC); + } + } + if(!net.train || l.onlyforward){ + cuda_pull_array(l.output_gpu, l.output, l.batch*l.outputs); + return; + } + + cuda_pull_array(l.output_gpu, net.input, l.batch*l.inputs); + forward_gaussian_yolo_layer(l, net); + cuda_push_array(l.delta_gpu, l.delta, l.batch*l.outputs); +} + +void backward_gaussian_yolo_layer_gpu(const layer l, network net) +{ + axpy_ongpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1); +} +#endif diff --git a/src/gaussian_yolo_layer.h b/src/gaussian_yolo_layer.h new file mode 100644 index 00000000..96cb2a8f --- /dev/null +++ b/src/gaussian_yolo_layer.h @@ -0,0 +1,20 @@ +//Gaussian YOLOv3 implementation +#ifndef GAUSSIAN_YOLO_LAYER_H +#define GAUSSIAN_YOLO_LAYER_H + +#include "darknet.h" +#include "layer.h" +#include "network.h" + +layer make_gaussian_yolo_layer(int batch, int w, int h, int n, int total, int *mask, int classes); +void forward_gaussian_yolo_layer(const layer l, network net); +void backward_gaussian_yolo_layer(const layer l, network net); +void resize_gaussian_yolo_layer(layer *l, int w, int h); +int gaussian_yolo_num_detections(layer l, float thresh); + +#ifdef GPU +void forward_gaussian_yolo_layer_gpu(const layer l, network net); +void backward_gaussian_yolo_layer_gpu(layer l, network net); +#endif + +#endif diff --git a/src/network.c b/src/network.c index 82dc4d53..06587880 100644 --- a/src/network.c +++ b/src/network.c @@ -34,6 +34,7 @@ #include "shortcut_layer.h" #include "scale_channels_layer.h" #include "yolo_layer.h" +#include "gaussian_yolo_layer.h" #include "upsample_layer.h" #include "parser.h" @@ -202,6 +203,10 @@ char *get_layer_string(LAYER_TYPE a) return "detection"; case REGION: return "region"; + case YOLO: + return "yolo"; + case GAUSSIAN_YOLO: + return "Gaussian_yolo"; case DROPOUT: return "dropout"; case CROP: @@ -524,6 +529,8 @@ int resize_network(network *net, int w, int h) resize_region_layer(&l, w, h); }else if (l.type == YOLO) { resize_yolo_layer(&l, w, h); + }else if (l.type == GAUSSIAN_YOLO) { + resize_gaussian_yolo_layer(&l, w, h); }else if(l.type == ROUTE){ resize_route_layer(&l, net); }else if (l.type == SHORTCUT) { @@ -687,6 +694,9 @@ int num_detections(network *net, float thresh) if (l.type == YOLO) { s += yolo_num_detections(l, thresh); } + if (l.type == GAUSSIAN_YOLO) { + s += gaussian_yolo_num_detections(l, thresh); + } if (l.type == DETECTION || l.type == REGION) { s += l.w*l.h*l.n; } @@ -703,6 +713,8 @@ detection *make_network_boxes(network *net, float thresh, int *num) detection* dets = (detection*)calloc(nboxes, sizeof(detection)); for (i = 0; i < nboxes; ++i) { dets[i].prob = (float*)calloc(l.classes, sizeof(float)); + // tx,ty,tw,th uncertainty + dets[i].uc = calloc(4, sizeof(float)); // Gaussian_YOLOv3 if (l.coords > 4) { dets[i].mask = (float*)calloc(l.coords - 4, sizeof(float)); } @@ -749,6 +761,10 @@ void fill_network_boxes(network *net, int w, int h, float thresh, float hier, in prev_classes, l.classes); } } + if (l.type == GAUSSIAN_YOLO) { + int count = get_gaussian_yolo_detections(l, w, h, net->w, net->h, thresh, map, relative, dets); + dets += count; + } if (l.type == REGION) { custom_get_region_detections(l, w, h, net->w, net->h, thresh, map, hier, relative, dets, letter); //get_region_detections(l, w, h, net->w, net->h, thresh, map, hier, relative, dets); diff --git a/src/parser.c b/src/parser.c index 829134d1..b31c7673 100644 --- a/src/parser.c +++ b/src/parser.c @@ -38,6 +38,7 @@ #include "upsample_layer.h" #include "version.h" #include "yolo_layer.h" +#include "gaussian_yolo_layer.h" typedef struct{ char *type; @@ -57,6 +58,7 @@ LAYER_TYPE string_to_layer_type(char * type) if (strcmp(type, "[detection]")==0) return DETECTION; if (strcmp(type, "[region]")==0) return REGION; if (strcmp(type, "[yolo]") == 0) return YOLO; + if (strcmp(type, "[Gaussian_yolo]") == 0) return GAUSSIAN_YOLO; if (strcmp(type, "[local]")==0) return LOCAL; if (strcmp(type, "[conv]")==0 || strcmp(type, "[convolutional]")==0) return CONVOLUTIONAL; @@ -390,6 +392,67 @@ layer parse_yolo(list *options, size_params params) return l; } + +int *parse_gaussian_yolo_mask(char *a, int *num) // Gaussian_YOLOv3 +{ + int *mask = 0; + if (a) { + int len = strlen(a); + int n = 1; + int i; + for (i = 0; i < len; ++i) { + if (a[i] == ',') ++n; + } + mask = calloc(n, sizeof(int)); + for (i = 0; i < n; ++i) { + int val = atoi(a); + mask[i] = val; + a = strchr(a, ',') + 1; + } + *num = n; + } + return mask; +} + + +layer parse_gaussian_yolo(list *options, size_params params) // Gaussian_YOLOv3 +{ + int classes = option_find_int(options, "classes", 20); + int total = option_find_int(options, "num", 1); + int num = total; + + char *a = option_find_str(options, "mask", 0); + int *mask = parse_gaussian_yolo_mask(a, &num); + layer l = make_gaussian_yolo_layer(params.batch, params.w, params.h, num, total, mask, classes); + assert(l.outputs == params.inputs); + + l.max_boxes = option_find_int_quiet(options, "max", 90); + l.jitter = option_find_float(options, "jitter", .2); + + l.ignore_thresh = option_find_float(options, "ignore_thresh", .5); + l.truth_thresh = option_find_float(options, "truth_thresh", 1); + l.random = option_find_int_quiet(options, "random", 0); + + char *map_file = option_find_str(options, "map", 0); + if (map_file) l.map = read_map(map_file); + + a = option_find_str(options, "anchors", 0); + if (a) { + int len = strlen(a); + int n = 1; + int i; + for (i = 0; i < len; ++i) { + if (a[i] == ',') ++n; + } + for (i = 0; i < n; ++i) { + float bias = atof(a); + l.biases[i] = bias; + a = strchr(a, ',') + 1; + } + } + return l; +} + layer parse_region(list *options, size_params params) { int coords = option_find_int(options, "coords", 4); @@ -923,6 +986,8 @@ network parse_network_cfg_custom(char *filename, int batch, int time_steps) l = parse_region(options, params); }else if (lt == YOLO) { l = parse_yolo(options, params); + }else if (lt == GAUSSIAN_YOLO) { + l = parse_gaussian_yolo(options, params); }else if(lt == DETECTION){ l = parse_detection(options, params); }else if(lt == SOFTMAX){ diff --git a/src/yolo_layer.c b/src/yolo_layer.c index 2006f4b8..424811df 100644 --- a/src/yolo_layer.c +++ b/src/yolo_layer.c @@ -242,16 +242,6 @@ static int entry_index(layer l, int batch, int location, int entry) return batch*l.outputs + n*l.w*l.h*(4+l.classes+1) + entry*l.w*l.h + loc; } -static box float_to_box_stride(float *f, int stride) -{ - box b = { 0 }; - b.x = f[0]; - b.y = f[1 * stride]; - b.w = f[2 * stride]; - b.h = f[3 * stride]; - return b; -} - void forward_yolo_layer(const layer l, network_state state) { int i, j, b, t, n;