13 files modified
1 files added
| | |
| | | |
| | | 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 darknet.o detection_layer.o imagenet.o captcha.o route_layer.o writing.o box.o nightmare.o normalization_layer.o avgpool_layer.o coco.o dice.o yolo.o layer.o compare.o classifier.o local_layer.o |
| | | ifeq ($(GPU), 1) |
| | | OBJ+=convolutional_kernels.o deconvolutional_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 softmax_layer_kernels.o network_kernels.o avgpool_layer_kernels.o yolo_kernels.o |
| | | OBJ+=convolutional_kernels.o deconvolutional_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 softmax_layer_kernels.o network_kernels.o avgpool_layer_kernels.o yolo_kernels.o coco_kernels.o |
| | | endif |
| | | |
| | | OBJS = $(addprefix $(OBJDIR), $(OBJ)) |
| New file |
| | |
| | | [net] |
| | | batch=64 |
| | | subdivisions=4 |
| | | height=448 |
| | | width=448 |
| | | channels=3 |
| | | momentum=0.9 |
| | | decay=0.0005 |
| | | |
| | | learning_rate=0.0001 |
| | | policy=steps |
| | | steps=100,200,300,100000,150000 |
| | | scales=2.5,2,2,.1,.1 |
| | | max_batches = 300000 |
| | | |
| | | [crop] |
| | | crop_width=448 |
| | | crop_height=448 |
| | | flip=0 |
| | | angle=0 |
| | | saturation = 1.5 |
| | | exposure = 1.5 |
| | | |
| | | [convolutional] |
| | | filters=64 |
| | | size=7 |
| | | stride=2 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [maxpool] |
| | | size=2 |
| | | stride=2 |
| | | |
| | | [convolutional] |
| | | filters=192 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [maxpool] |
| | | size=2 |
| | | stride=2 |
| | | |
| | | [convolutional] |
| | | filters=128 |
| | | size=1 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=256 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=256 |
| | | size=1 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=512 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [maxpool] |
| | | size=2 |
| | | stride=2 |
| | | |
| | | [convolutional] |
| | | filters=256 |
| | | size=1 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=512 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=256 |
| | | size=1 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=512 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=256 |
| | | size=1 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=512 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=256 |
| | | size=1 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=512 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=512 |
| | | size=1 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=1024 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [maxpool] |
| | | size=2 |
| | | stride=2 |
| | | |
| | | [convolutional] |
| | | filters=512 |
| | | size=1 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=1024 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=512 |
| | | size=1 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | filters=1024 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | |
| | | ################################# |
| | | |
| | | |
| | | [convolutional] |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | filters=1024 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | size=3 |
| | | stride=2 |
| | | pad=1 |
| | | filters=1024 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | filters=1024 |
| | | activation=leaky |
| | | |
| | | [convolutional] |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | filters=1024 |
| | | activation=leaky |
| | | |
| | | [local] |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | filters=192 |
| | | activation=leaky |
| | | |
| | | [dropout] |
| | | probability=.5 |
| | | |
| | | [connected] |
| | | output= 4410 |
| | | activation=linear |
| | | |
| | | [detection] |
| | | classes=80 |
| | | coords=4 |
| | | rescore=1 |
| | | side=7 |
| | | num=2 |
| | | softmax=0 |
| | | sqrt=1 |
| | | jitter=.2 |
| | | |
| | | object_scale=1 |
| | | noobject_scale=.5 |
| | | class_scale=1 |
| | | coord_scale=5 |
| | | |
| | |
| | | __device__ float linear_activate_kernel(float x){return x;} |
| | | __device__ float logistic_activate_kernel(float x){return 1./(1. + exp(-x));} |
| | | __device__ float relu_activate_kernel(float x){return x*(x>0);} |
| | | __device__ float elu_activate_kernel(float x){return (x >= 0)*x + (x < 0)*(exp(x)-1);} |
| | | __device__ float relie_activate_kernel(float x){return x*(x>0);} |
| | | __device__ float ramp_activate_kernel(float x){return x*(x>0)+.1*x;} |
| | | __device__ float leaky_activate_kernel(float x){return (x>0) ? x : .1*x;} |
| | |
| | | __device__ float linear_gradient_kernel(float x){return 1;} |
| | | __device__ float logistic_gradient_kernel(float x){return (1-x)*x;} |
| | | __device__ float relu_gradient_kernel(float x){return (x>0);} |
| | | __device__ float elu_gradient_kernel(float x){return (x >= 0) + (x < 0)*(x + 1);} |
| | | __device__ float relie_gradient_kernel(float x){return (x>0) ? 1 : .01;} |
| | | __device__ float ramp_gradient_kernel(float x){return (x>0)+.1;} |
| | | __device__ float leaky_gradient_kernel(float x){return (x>0) ? 1 : .1;} |
| | |
| | | return logistic_activate_kernel(x); |
| | | case RELU: |
| | | return relu_activate_kernel(x); |
| | | case ELU: |
| | | return elu_activate_kernel(x); |
| | | case RELIE: |
| | | return relie_activate_kernel(x); |
| | | case RAMP: |
| | |
| | | return logistic_gradient_kernel(x); |
| | | case RELU: |
| | | return relu_gradient_kernel(x); |
| | | case ELU: |
| | | return elu_gradient_kernel(x); |
| | | case RELIE: |
| | | return relie_gradient_kernel(x); |
| | | case RAMP: |
| | |
| | | return "logistic"; |
| | | case RELU: |
| | | return "relu"; |
| | | case ELU: |
| | | return "elu"; |
| | | case RELIE: |
| | | return "relie"; |
| | | case RAMP: |
| | |
| | | { |
| | | if (strcmp(s, "logistic")==0) return LOGISTIC; |
| | | if (strcmp(s, "relu")==0) return RELU; |
| | | if (strcmp(s, "elu")==0) return ELU; |
| | | if (strcmp(s, "relie")==0) return RELIE; |
| | | if (strcmp(s, "plse")==0) return PLSE; |
| | | if (strcmp(s, "linear")==0) return LINEAR; |
| | |
| | | return logistic_activate(x); |
| | | case RELU: |
| | | return relu_activate(x); |
| | | case ELU: |
| | | return elu_activate(x); |
| | | case RELIE: |
| | | return relie_activate(x); |
| | | case RAMP: |
| | |
| | | return logistic_gradient(x); |
| | | case RELU: |
| | | return relu_gradient(x); |
| | | case ELU: |
| | | return elu_gradient(x); |
| | | case RELIE: |
| | | return relie_gradient(x); |
| | | case RAMP: |
| | |
| | | #include "math.h" |
| | | |
| | | typedef enum{ |
| | | LOGISTIC, RELU, RELIE, LINEAR, RAMP, TANH, PLSE, LEAKY |
| | | LOGISTIC, RELU, RELIE, LINEAR, RAMP, TANH, PLSE, LEAKY, ELU |
| | | }ACTIVATION; |
| | | |
| | | ACTIVATION get_activation(char *s); |
| | |
| | | static inline float linear_activate(float x){return x;} |
| | | static inline float logistic_activate(float x){return 1./(1. + exp(-x));} |
| | | static inline float relu_activate(float x){return x*(x>0);} |
| | | static inline float elu_activate(float x){return (x >= 0)*x + (x < 0)*(exp(x)-1);} |
| | | static inline float relie_activate(float x){return x*(x>0);} |
| | | static inline float ramp_activate(float x){return x*(x>0)+.1*x;} |
| | | static inline float leaky_activate(float x){return (x>0) ? x : .1*x;} |
| | |
| | | static inline float linear_gradient(float x){return 1;} |
| | | static inline float logistic_gradient(float x){return (1-x)*x;} |
| | | static inline float relu_gradient(float x){return (x>0);} |
| | | static inline float elu_gradient(float x){return (x >= 0) + (x < 0)*(x + 1);} |
| | | static inline float relie_gradient(float x){return (x>0) ? 1 : .01;} |
| | | static inline float ramp_gradient(float x){return (x>0)+.1;} |
| | | static inline float leaky_gradient(float x){return (x>0) ? 1 : .1;} |
| | |
| | | |
| | | int coco_ids[] = {1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,23,24,25,27,28,31,32,33,34,35,36,37,38,39,40,41,42,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,70,72,73,74,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90}; |
| | | |
| | | void draw_coco(image im, int num, float thresh, box *boxes, float **probs) |
| | | { |
| | | int classes = 80; |
| | | int i; |
| | | |
| | | for(i = 0; i < num; ++i){ |
| | | int class = max_index(probs[i], classes); |
| | | float prob = probs[i][class]; |
| | | if(prob > thresh){ |
| | | int width = sqrt(prob)*5 + 1; |
| | | printf("%f %s\n", prob, coco_classes[class]); |
| | | float red = get_color(0,class,classes); |
| | | float green = get_color(1,class,classes); |
| | | float blue = get_color(2,class,classes); |
| | | box b = boxes[i]; |
| | | |
| | | int left = (b.x-b.w/2.)*im.w; |
| | | int right = (b.x+b.w/2.)*im.w; |
| | | int top = (b.y-b.h/2.)*im.h; |
| | | int bot = (b.y+b.h/2.)*im.h; |
| | | draw_box_width(im, left, top, right, bot, width, red, green, blue); |
| | | } |
| | | } |
| | | } |
| | | image coco_labels[80]; |
| | | |
| | | void train_coco(char *cfgfile, char *weightfile) |
| | | { |
| | |
| | | detection_layer l = net.layers[net.n-1]; |
| | | set_batch_network(&net, 1); |
| | | srand(2222222); |
| | | float nms = .4; |
| | | clock_t time; |
| | | char buff[256]; |
| | | char *input = buff; |
| | |
| | | float *predictions = network_predict(net, X); |
| | | printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); |
| | | convert_coco_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, thresh, probs, boxes, 0); |
| | | draw_coco(im, l.side*l.side*l.n, thresh, boxes, probs); |
| | | if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, l.classes, nms); |
| | | draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, coco_classes, coco_labels, 80); |
| | | show_image(im, "predictions"); |
| | | |
| | | show_image(sized, "resized"); |
| | |
| | | } |
| | | } |
| | | |
| | | #ifdef OPENCV |
| | | #ifdef GPU |
| | | void demo_coco(char *cfgfile, char *weightfile, float thresh, int cam_index); |
| | | #endif |
| | | #endif |
| | | |
| | | void run_coco(int argc, char **argv) |
| | | { |
| | | int i; |
| | | for(i = 0; i < 80; ++i){ |
| | | char buff[256]; |
| | | sprintf(buff, "data/labels/%s.png", coco_classes[i]); |
| | | coco_labels[i] = load_image_color(buff, 0, 0); |
| | | } |
| | | float thresh = find_float_arg(argc, argv, "-thresh", .2); |
| | | int cam_index = find_int_arg(argc, argv, "-c", 0); |
| | | |
| | | if(argc < 4){ |
| | | fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]); |
| | | return; |
| | |
| | | else if(0==strcmp(argv[2], "train")) train_coco(cfg, weights); |
| | | else if(0==strcmp(argv[2], "valid")) validate_coco(cfg, weights); |
| | | else if(0==strcmp(argv[2], "recall")) validate_coco_recall(cfg, weights); |
| | | #ifdef OPENCV |
| | | #ifdef GPU |
| | | else if(0==strcmp(argv[2], "demo")) demo_coco(cfg, weights, thresh, cam_index); |
| | | #endif |
| | | #endif |
| | | } |
| | |
| | | #include "parser.h" |
| | | #include "box.h" |
| | | #include "image.h" |
| | | #include <sys/time.h> |
| | | } |
| | | |
| | | #ifdef OPENCV |
| | |
| | | #include "opencv2/imgproc/imgproc.hpp" |
| | | extern "C" image ipl_to_image(IplImage* src); |
| | | extern "C" void convert_coco_detections(float *predictions, int classes, int num, int square, int side, int w, int h, float thresh, float **probs, box *boxes, int only_objectness); |
| | | extern "C" void draw_coco(image im, int num, float thresh, box *boxes, float **probs); |
| | | |
| | | extern "C" char *coco_classes[]; |
| | | extern "C" image *coco_labels; |
| | | |
| | | static float **probs; |
| | | static box *boxes; |
| | |
| | | static image det ; |
| | | static image det_s; |
| | | static image disp ; |
| | | static cv::VideoCapture cap(0); |
| | | static cv::VideoCapture cap; |
| | | static float fps = 0; |
| | | |
| | | void *fetch_in_thread(void *ptr) |
| | | void *fetch_in_thread_coco(void *ptr) |
| | | { |
| | | cv::Mat frame_m; |
| | | cap >> frame_m; |
| | |
| | | return 0; |
| | | } |
| | | |
| | | void *detect_in_thread(void *ptr) |
| | | void *detect_in_thread_coco(void *ptr) |
| | | { |
| | | float nms = .4; |
| | | float thresh = .2; |
| | |
| | | if (nms > 0) do_nms(boxes, probs, l.side*l.side*l.n, l.classes, nms); |
| | | printf("\033[2J"); |
| | | printf("\033[1;1H"); |
| | | printf("\nObjects:\n\n"); |
| | | draw_coco(det, l.side*l.side*l.n, thresh, boxes, probs); |
| | | printf("\nFPS:%.0f\n",fps); |
| | | printf("Objects:\n\n"); |
| | | draw_detections(det, l.side*l.side*l.n, thresh, boxes, probs, coco_classes, coco_labels, 80); |
| | | return 0; |
| | | } |
| | | |
| | | extern "C" void demo_coco(char *cfgfile, char *weightfile, float thresh) |
| | | extern "C" void demo_coco(char *cfgfile, char *weightfile, float thresh, int cam_index) |
| | | { |
| | | printf("YOLO demo\n"); |
| | | net = parse_network_cfg(cfgfile); |
| | |
| | | |
| | | srand(2222222); |
| | | |
| | | cv::VideoCapture cam(cam_index); |
| | | cap = cam; |
| | | if(!cap.isOpened()) error("Couldn't connect to webcam.\n"); |
| | | |
| | | detection_layer l = net.layers[net.n-1]; |
| | |
| | | pthread_t fetch_thread; |
| | | pthread_t detect_thread; |
| | | |
| | | fetch_in_thread(0); |
| | | fetch_in_thread_coco(0); |
| | | det = in; |
| | | det_s = in_s; |
| | | |
| | | fetch_in_thread(0); |
| | | detect_in_thread(0); |
| | | fetch_in_thread_coco(0); |
| | | detect_in_thread_coco(0); |
| | | disp = det; |
| | | det = in; |
| | | det_s = in_s; |
| | | |
| | | while(1){ |
| | | if(pthread_create(&fetch_thread, 0, fetch_in_thread, 0)) error("Thread creation failed"); |
| | | if(pthread_create(&detect_thread, 0, detect_in_thread, 0)) error("Thread creation failed"); |
| | | struct timeval tval_before, tval_after, tval_result; |
| | | gettimeofday(&tval_before, NULL); |
| | | if(pthread_create(&fetch_thread, 0, fetch_in_thread_coco, 0)) error("Thread creation failed"); |
| | | if(pthread_create(&detect_thread, 0, detect_in_thread_coco, 0)) error("Thread creation failed"); |
| | | show_image(disp, "YOLO"); |
| | | free_image(disp); |
| | | cvWaitKey(1); |
| | |
| | | disp = det; |
| | | det = in; |
| | | det_s = in_s; |
| | | |
| | | gettimeofday(&tval_after, NULL); |
| | | timersub(&tval_after, &tval_before, &tval_result); |
| | | float curr = 1000000.f/((long int)tval_result.tv_usec); |
| | | fps = .9*fps + .1*curr; |
| | | } |
| | | } |
| | | #else |
| | | extern "C" void demo_coco(char *cfgfile, char *weightfile, float thresh){ |
| | | extern "C" void demo_coco(char *cfgfile, char *weightfile, float thresh, int cam_index){ |
| | | fprintf(stderr, "YOLO-COCO demo needs OpenCV for webcam images.\n"); |
| | | } |
| | | #endif |
| | |
| | | return r; |
| | | } |
| | | |
| | | void draw_label(image a, int r, int c, image label, const float *rgb) |
| | | { |
| | | float ratio = (float) label.w / label.h; |
| | | int h = label.h; |
| | | int w = ratio * h; |
| | | image rl = resize_image(label, w, h); |
| | | if (r - h >= 0) r = r - h; |
| | | |
| | | int i, j, k; |
| | | for(j = 0; j < h && j + r < a.h; ++j){ |
| | | for(i = 0; i < w && i + c < a.w; ++i){ |
| | | for(k = 0; k < label.c; ++k){ |
| | | float val = get_pixel(rl, i, j, k); |
| | | set_pixel(a, i+c, j+r, k, rgb[k] * val); |
| | | } |
| | | } |
| | | } |
| | | free_image(rl); |
| | | } |
| | | |
| | | void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b) |
| | | { |
| | | //normalize_image(a); |
| | |
| | | if(y2 < 0) y2 = 0; |
| | | if(y2 >= a.h) y2 = a.h-1; |
| | | |
| | | for(i = x1; i < x2; ++i){ |
| | | a.data[i + y1*a.w + 0*a.w*a.h] = b; |
| | | a.data[i + y2*a.w + 0*a.w*a.h] = b; |
| | | for(i = x1; i <= x2; ++i){ |
| | | a.data[i + y1*a.w + 0*a.w*a.h] = r; |
| | | a.data[i + y2*a.w + 0*a.w*a.h] = r; |
| | | |
| | | a.data[i + y1*a.w + 1*a.w*a.h] = g; |
| | | a.data[i + y2*a.w + 1*a.w*a.h] = g; |
| | | |
| | | a.data[i + y1*a.w + 2*a.w*a.h] = r; |
| | | a.data[i + y2*a.w + 2*a.w*a.h] = r; |
| | | a.data[i + y1*a.w + 2*a.w*a.h] = b; |
| | | a.data[i + y2*a.w + 2*a.w*a.h] = b; |
| | | } |
| | | for(i = y1; i < y2; ++i){ |
| | | a.data[x1 + i*a.w + 0*a.w*a.h] = b; |
| | | a.data[x2 + i*a.w + 0*a.w*a.h] = b; |
| | | for(i = y1; i <= y2; ++i){ |
| | | a.data[x1 + i*a.w + 0*a.w*a.h] = r; |
| | | a.data[x2 + i*a.w + 0*a.w*a.h] = r; |
| | | |
| | | a.data[x1 + i*a.w + 1*a.w*a.h] = g; |
| | | a.data[x2 + i*a.w + 1*a.w*a.h] = g; |
| | | |
| | | a.data[x1 + i*a.w + 2*a.w*a.h] = r; |
| | | a.data[x2 + i*a.w + 2*a.w*a.h] = r; |
| | | a.data[x1 + i*a.w + 2*a.w*a.h] = b; |
| | | a.data[x2 + i*a.w + 2*a.w*a.h] = b; |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image *labels, int classes) |
| | | { |
| | | int i; |
| | | |
| | | for(i = 0; i < num; ++i){ |
| | | int class = max_index(probs[i], classes); |
| | | float prob = probs[i][class]; |
| | | if(prob > thresh){ |
| | | int width = pow(prob, 1./2.)*10+1; |
| | | printf("%s: %.2f\n", names[class], prob); |
| | | int offset = class*17 % classes; |
| | | float red = get_color(0,offset,classes); |
| | | float green = get_color(1,offset,classes); |
| | | float blue = get_color(2,offset,classes); |
| | | float rgb[3]; |
| | | rgb[0] = red; |
| | | rgb[1] = green; |
| | | rgb[2] = blue; |
| | | box b = boxes[i]; |
| | | |
| | | int left = (b.x-b.w/2.)*im.w; |
| | | int right = (b.x+b.w/2.)*im.w; |
| | | int top = (b.y-b.h/2.)*im.h; |
| | | int bot = (b.y+b.h/2.)*im.h; |
| | | |
| | | if(left < 0) left = 0; |
| | | if(right > im.w-1) right = im.w-1; |
| | | if(top < 0) top = 0; |
| | | if(bot > im.h-1) bot = im.h-1; |
| | | |
| | | draw_box_width(im, left, top, right, bot, width, red, green, blue); |
| | | if (labels) draw_label(im, top + width, left, labels[class], rgb); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | void flip_image(image a) |
| | | { |
| | | int i,j,k; |
| | |
| | | void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b); |
| | | void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, float g, float b); |
| | | void draw_bbox(image a, box bbox, int w, float r, float g, float b); |
| | | void draw_label(image a, int r, int c, image label, const float *rgb); |
| | | void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image *labels, int classes); |
| | | image image_distance(image a, image b); |
| | | void scale_image(image m, float s); |
| | | image crop_image(image im, int dx, int dy, int w, int h); |
| | |
| | | float * x_gpu; |
| | | float * x_norm_gpu; |
| | | float * weights_gpu; |
| | | float * biases_gpu; |
| | | float * scales_gpu; |
| | | |
| | | float * weight_updates_gpu; |
| | | |
| | | float * biases_gpu; |
| | | float * bias_updates_gpu; |
| | | |
| | | float * scales_gpu; |
| | | float * scale_updates_gpu; |
| | | |
| | | float * output_gpu; |
| | |
| | | return batch_num; |
| | | } |
| | | |
| | | void reset_momentum(network net) |
| | | { |
| | | if (net.momentum == 0) return; |
| | | net.learning_rate = 0; |
| | | net.momentum = 0; |
| | | net.decay = 0; |
| | | #ifdef GPU |
| | | if(gpu_index >= 0) update_network_gpu(net); |
| | | #endif |
| | | } |
| | | |
| | | float get_current_rate(network net) |
| | | { |
| | | int batch_num = get_current_batch(net); |
| | |
| | | for(i = 0; i < net.num_steps; ++i){ |
| | | if(net.steps[i] > batch_num) return rate; |
| | | rate *= net.scales[i]; |
| | | if(net.steps[i] > batch_num - 1) reset_momentum(net); |
| | | } |
| | | return rate; |
| | | case EXP: |
| | |
| | | float *get_network_output_gpu(network net); |
| | | void forward_network_gpu(network net, network_state state); |
| | | void backward_network_gpu(network net, network_state state); |
| | | void update_network_gpu(network net); |
| | | #endif |
| | | |
| | | float get_current_rate(network net); |
| | |
| | | |
| | | char *voc_names[] = {"aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"}; |
| | | |
| | | void draw_yolo(image im, int num, float thresh, box *boxes, float **probs) |
| | | { |
| | | int classes = 20; |
| | | int i; |
| | | |
| | | for(i = 0; i < num; ++i){ |
| | | int class = max_index(probs[i], classes); |
| | | float prob = probs[i][class]; |
| | | if(prob > thresh){ |
| | | int width = pow(prob, 1./2.)*10+1; |
| | | width = 8; |
| | | printf("%s: %.2f\n", voc_names[class], prob); |
| | | class = class * 7 % 20; |
| | | float red = get_color(0,class,classes); |
| | | float green = get_color(1,class,classes); |
| | | float blue = get_color(2,class,classes); |
| | | //red = green = blue = 0; |
| | | box b = boxes[i]; |
| | | |
| | | int left = (b.x-b.w/2.)*im.w; |
| | | int right = (b.x+b.w/2.)*im.w; |
| | | int top = (b.y-b.h/2.)*im.h; |
| | | int bot = (b.y+b.h/2.)*im.h; |
| | | |
| | | if(left < 0) left = 0; |
| | | if(right > im.w-1) right = im.w-1; |
| | | if(top < 0) top = 0; |
| | | if(bot > im.h-1) bot = im.h-1; |
| | | |
| | | draw_box_width(im, left, top, right, bot, width, red, green, blue); |
| | | } |
| | | } |
| | | } |
| | | |
| | | void train_yolo(char *cfgfile, char *weightfile) |
| | | { |
| | | char *train_images = "data/voc.0712.trainval"; |
| | |
| | | printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); |
| | | convert_yolo_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, thresh, probs, boxes, 0); |
| | | if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, l.classes, nms); |
| | | draw_yolo(im, l.side*l.side*l.n, thresh, boxes, probs); |
| | | draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, voc_names, 0, 20); |
| | | show_image(im, "predictions"); |
| | | |
| | | show_image(sized, "resized"); |
| | |
| | | extern "C" void convert_yolo_detections(float *predictions, int classes, int num, int square, int side, int w, int h, float thresh, float **probs, box *boxes, int only_objectness); |
| | | extern "C" void draw_yolo(image im, int num, float thresh, box *boxes, float **probs); |
| | | |
| | | extern "C" char *voc_names[]; |
| | | |
| | | static float **probs; |
| | | static box *boxes; |
| | | static network net; |
| | |
| | | printf("\033[1;1H"); |
| | | printf("\nFPS:%.0f\n",fps); |
| | | printf("Objects:\n\n"); |
| | | draw_yolo(det, l.side*l.side*l.n, thresh, boxes, probs); |
| | | draw_detections(det, l.side*l.side*l.n, thresh, boxes, probs, voc_names, 0, 20); |
| | | return 0; |
| | | } |
| | | |