18 files modified
3 files added
1 files deleted
| | |
| | | GPU=1 |
| | | OPENCV=1 |
| | | GPU=0 |
| | | OPENCV=0 |
| | | DEBUG=0 |
| | | |
| | | ARCH= --gpu-architecture=compute_20 --gpu-code=compute_20 |
| | |
| | | LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand |
| | | 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 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 swag.o shortcut_layer.o activation_layer.o rnn_layer.o rnn.o rnn_vid.o crnn_layer.o coco_demo.o tag.o cifar.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 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 swag.o shortcut_layer.o activation_layer.o rnn_layer.o rnn.o rnn_vid.o crnn_layer.o coco_demo.o tag.o cifar.o yolo_demo.o go.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 |
| | | endif |
| | | |
| | | OBJS = $(addprefix $(OBJDIR), $(OBJ)) |
| New file |
| | |
| | | [net] |
| | | batch=1 |
| | | subdivisions=1 |
| | | height=19 |
| | | width=19 |
| | | channels=1 |
| | | momentum=0.9 |
| | | decay=0.0005 |
| | | |
| | | learning_rate=0.1 |
| | | max_batches = 0 |
| | | policy=steps |
| | | steps=50000, 90000 |
| | | scales=.1, .1 |
| | | |
| | | [convolutional] |
| | | filters=256 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | batch_normalize=1 |
| | | |
| | | [convolutional] |
| | | filters=256 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | batch_normalize=1 |
| | | |
| | | [convolutional] |
| | | filters=256 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | batch_normalize=1 |
| | | |
| | | [convolutional] |
| | | filters=256 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | batch_normalize=1 |
| | | |
| | | [convolutional] |
| | | filters=256 |
| | | size=3 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | batch_normalize=1 |
| | | |
| | | [convolutional] |
| | | filters=1 |
| | | size=1 |
| | | stride=1 |
| | | pad=1 |
| | | activation=leaky |
| | | |
| | | [softmax] |
| | | |
| | | [cost] |
| | | type=sse |
| | | |
| | |
| | | |
| | | void variance_cpu(float *x, float *mean, int batch, int filters, int spatial, float *variance) |
| | | { |
| | | float scale = 1./(batch * spatial); |
| | | float scale = 1./(batch * spatial - 1); |
| | | int i,j,k; |
| | | for(i = 0; i < filters; ++i){ |
| | | variance[i] = 0; |
| | |
| | | for(f = 0; f < filters; ++f){ |
| | | for(i = 0; i < spatial; ++i){ |
| | | int index = b*filters*spatial + f*spatial + i; |
| | | x[index] = (x[index] - mean[f])/(sqrt(variance[f]) + .00001f); |
| | | x[index] = (x[index] - mean[f])/(sqrt(variance[f]) + .000001f); |
| | | } |
| | | } |
| | | } |
| | |
| | | if (index >= N) return; |
| | | int f = (index/spatial)%filters; |
| | | |
| | | x[index] = (x[index] - mean[f])/(sqrt(variance[f]) + .00001f); |
| | | x[index] = (x[index] - mean[f])/(sqrt(variance[f]) + .000001f); |
| | | } |
| | | |
| | | __global__ void normalize_delta_kernel(int N, float *x, float *mean, float *variance, float *mean_delta, float *variance_delta, int batch, int filters, int spatial, float *delta) |
| | |
| | | if (index >= N) return; |
| | | int f = (index/spatial)%filters; |
| | | |
| | | delta[index] = delta[index] * 1./(sqrt(variance[f]) + .00001f) + variance_delta[f] * 2. * (x[index] - mean[f]) / (spatial * batch) + mean_delta[f]/(spatial*batch); |
| | | delta[index] = delta[index] * 1./(sqrt(variance[f]) + .000001f) + variance_delta[f] * 2. * (x[index] - mean[f]) / (spatial * batch) + mean_delta[f]/(spatial*batch); |
| | | } |
| | | |
| | | extern "C" void normalize_delta_gpu(float *x, float *mean, float *variance, float *mean_delta, float *variance_delta, int batch, int filters, int spatial, float *delta) |
| | |
| | | variance_delta[i] += delta[index]*(x[index] - mean[i]); |
| | | } |
| | | } |
| | | variance_delta[i] *= -.5 * pow(variance[i] + .00001f, (float)(-3./2.)); |
| | | variance_delta[i] *= -.5 * pow(variance[i] + .000001f, (float)(-3./2.)); |
| | | } |
| | | |
| | | __global__ void accumulate_kernel(float *x, int n, int groups, float *sum) |
| | |
| | | for(i = 0; i < threads; ++i){ |
| | | mean_delta[filter] += local[i]; |
| | | } |
| | | mean_delta[filter] *= (-1./sqrt(variance[filter] + .00001f)); |
| | | mean_delta[filter] *= (-1./sqrt(variance[filter] + .000001f)); |
| | | } |
| | | } |
| | | |
| | |
| | | for(i = 0; i < threads; ++i){ |
| | | variance_delta[filter] += local[i]; |
| | | } |
| | | variance_delta[filter] *= -.5 * pow(variance[filter] + .00001f, (float)(-3./2.)); |
| | | variance_delta[filter] *= -.5 * pow(variance[filter] + .000001f, (float)(-3./2.)); |
| | | } |
| | | } |
| | | |
| | |
| | | mean_delta[i] += delta[index]; |
| | | } |
| | | } |
| | | mean_delta[i] *= (-1./sqrt(variance[i] + .00001f)); |
| | | mean_delta[i] *= (-1./sqrt(variance[i] + .000001f)); |
| | | } |
| | | |
| | | extern "C" void mean_delta_gpu(float *delta, float *variance, int batch, int filters, int spatial, float *mean_delta) |
| | |
| | | |
| | | __global__ void variance_kernel(float *x, float *mean, int batch, int filters, int spatial, float *variance) |
| | | { |
| | | float scale = 1./(batch * spatial); |
| | | float scale = 1./(batch * spatial - 1); |
| | | int j,k; |
| | | int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
| | | if (i >= filters) return; |
| | |
| | | for(i = 0; i < threads; ++i){ |
| | | variance[filter] += local[i]; |
| | | } |
| | | variance[filter] /= spatial * batch; |
| | | variance[filter] /= (spatial * batch - 1); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | float loss = train_network_sgd(net, train, 1); |
| | | if(avg_loss == -1) avg_loss = loss; |
| | | avg_loss = avg_loss*.9 + loss*.1; |
| | | avg_loss = avg_loss*.95 + loss*.05; |
| | | printf("%d, %.3f: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), (float)(*net.seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen); |
| | | if(*net.seen/N > epoch){ |
| | | epoch = *net.seen/N; |
| | |
| | | free_data(train); |
| | | } |
| | | |
| | | void train_cifar_distill(char *cfgfile, char *weightfile) |
| | | { |
| | | data_seed = time(0); |
| | | srand(time(0)); |
| | | float avg_loss = -1; |
| | | char *base = basecfg(cfgfile); |
| | | printf("%s\n", base); |
| | | network net = parse_network_cfg(cfgfile); |
| | | if(weightfile){ |
| | | load_weights(&net, weightfile); |
| | | } |
| | | printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); |
| | | |
| | | char *backup_directory = "/home/pjreddie/backup/"; |
| | | int classes = 10; |
| | | int N = 50000; |
| | | |
| | | char **labels = get_labels("data/cifar/labels.txt"); |
| | | int epoch = (*net.seen)/N; |
| | | |
| | | data train = load_all_cifar10(); |
| | | matrix soft = csv_to_matrix("results/ensemble.csv"); |
| | | |
| | | float weight = .9; |
| | | scale_matrix(soft, weight); |
| | | scale_matrix(train.y, 1. - weight); |
| | | matrix_add_matrix(soft, train.y); |
| | | |
| | | while(get_current_batch(net) < net.max_batches || net.max_batches == 0){ |
| | | clock_t time=clock(); |
| | | |
| | | float loss = train_network_sgd(net, train, 1); |
| | | if(avg_loss == -1) avg_loss = loss; |
| | | avg_loss = avg_loss*.95 + loss*.05; |
| | | printf("%d, %.3f: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), (float)(*net.seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen); |
| | | if(*net.seen/N > epoch){ |
| | | epoch = *net.seen/N; |
| | | char buff[256]; |
| | | sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch); |
| | | save_weights(net, buff); |
| | | } |
| | | if(get_current_batch(net)%100 == 0){ |
| | | char buff[256]; |
| | | sprintf(buff, "%s/%s.backup",backup_directory,base); |
| | | save_weights(net, buff); |
| | | } |
| | | } |
| | | char buff[256]; |
| | | sprintf(buff, "%s/%s.weights", backup_directory, base); |
| | | save_weights(net, buff); |
| | | |
| | | free_network(net); |
| | | free_ptrs((void**)labels, classes); |
| | | free(base); |
| | | free_data(train); |
| | | } |
| | | |
| | | void test_cifar_multi(char *filename, char *weightfile) |
| | | { |
| | | network net = parse_network_cfg(filename); |
| | | if(weightfile){ |
| | | load_weights(&net, weightfile); |
| | | } |
| | | set_batch_network(&net, 1); |
| | | srand(time(0)); |
| | | |
| | | float avg_acc = 0; |
| | | data test = load_cifar10_data("data/cifar/cifar-10-batches-bin/test_batch.bin"); |
| | | |
| | | int i; |
| | | for(i = 0; i < test.X.rows; ++i){ |
| | | image im = float_to_image(32, 32, 3, test.X.vals[i]); |
| | | |
| | | float pred[10] = {0}; |
| | | |
| | | float *p = network_predict(net, im.data); |
| | | axpy_cpu(10, 1, p, 1, pred, 1); |
| | | flip_image(im); |
| | | p = network_predict(net, im.data); |
| | | axpy_cpu(10, 1, p, 1, pred, 1); |
| | | |
| | | int index = max_index(pred, 10); |
| | | int class = max_index(test.y.vals[i], 10); |
| | | if(index == class) avg_acc += 1; |
| | | free_image(im); |
| | | printf("%4d: %.2f%%\n", i, 100.*avg_acc/(i+1)); |
| | | } |
| | | } |
| | | |
| | | void test_cifar(char *filename, char *weightfile) |
| | | { |
| | | network net = parse_network_cfg(filename); |
| | |
| | | free_data(test); |
| | | } |
| | | |
| | | void test_cifar_csv(char *filename, char *weightfile) |
| | | { |
| | | network net = parse_network_cfg(filename); |
| | | if(weightfile){ |
| | | load_weights(&net, weightfile); |
| | | } |
| | | srand(time(0)); |
| | | |
| | | data test = load_cifar10_data("data/cifar/cifar-10-batches-bin/test_batch.bin"); |
| | | |
| | | matrix pred = network_predict_data(net, test); |
| | | |
| | | int i; |
| | | for(i = 0; i < test.X.rows; ++i){ |
| | | image im = float_to_image(32, 32, 3, test.X.vals[i]); |
| | | flip_image(im); |
| | | } |
| | | matrix pred2 = network_predict_data(net, test); |
| | | scale_matrix(pred, .5); |
| | | scale_matrix(pred2, .5); |
| | | matrix_add_matrix(pred2, pred); |
| | | |
| | | matrix_to_csv(pred); |
| | | fprintf(stderr, "Accuracy: %f\n", matrix_topk_accuracy(test.y, pred, 1)); |
| | | free_data(test); |
| | | } |
| | | |
| | | void test_cifar_csvtrain(char *filename, char *weightfile) |
| | | { |
| | | network net = parse_network_cfg(filename); |
| | | if(weightfile){ |
| | | load_weights(&net, weightfile); |
| | | } |
| | | srand(time(0)); |
| | | |
| | | data test = load_all_cifar10(); |
| | | |
| | | matrix pred = network_predict_data(net, test); |
| | | |
| | | int i; |
| | | for(i = 0; i < test.X.rows; ++i){ |
| | | image im = float_to_image(32, 32, 3, test.X.vals[i]); |
| | | flip_image(im); |
| | | } |
| | | matrix pred2 = network_predict_data(net, test); |
| | | scale_matrix(pred, .5); |
| | | scale_matrix(pred2, .5); |
| | | matrix_add_matrix(pred2, pred); |
| | | |
| | | matrix_to_csv(pred); |
| | | fprintf(stderr, "Accuracy: %f\n", matrix_topk_accuracy(test.y, pred, 1)); |
| | | free_data(test); |
| | | } |
| | | |
| | | void eval_cifar_csv() |
| | | { |
| | | data test = load_cifar10_data("data/cifar/cifar-10-batches-bin/test_batch.bin"); |
| | | |
| | | matrix pred = csv_to_matrix("results/combined.csv"); |
| | | fprintf(stderr, "%d %d\n", pred.rows, pred.cols); |
| | | |
| | | fprintf(stderr, "Accuracy: %f\n", matrix_topk_accuracy(test.y, pred, 1)); |
| | | free_data(test); |
| | | free_matrix(pred); |
| | | } |
| | | |
| | | |
| | | void run_cifar(int argc, char **argv) |
| | | { |
| | | if(argc < 4){ |
| | |
| | | char *cfg = argv[3]; |
| | | char *weights = (argc > 4) ? argv[4] : 0; |
| | | if(0==strcmp(argv[2], "train")) train_cifar(cfg, weights); |
| | | else if(0==strcmp(argv[2], "distill")) train_cifar_distill(cfg, weights); |
| | | else if(0==strcmp(argv[2], "test")) test_cifar(cfg, weights); |
| | | else if(0==strcmp(argv[2], "multi")) test_cifar_multi(cfg, weights); |
| | | else if(0==strcmp(argv[2], "csv")) test_cifar_csv(cfg, weights); |
| | | else if(0==strcmp(argv[2], "csvtrain")) test_cifar_csvtrain(cfg, weights); |
| | | else if(0==strcmp(argv[2], "eval")) eval_cifar_csv(); |
| | | } |
| | | |
| | | |
| | |
| | | #include "parser.h" |
| | | #include "option_list.h" |
| | | #include "blas.h" |
| | | #include <sys/time.h> |
| | | |
| | | #ifdef OPENCV |
| | | #include "opencv2/highgui/highgui_c.h" |
| | |
| | | } |
| | | int w = net.w; |
| | | int h = net.h; |
| | | image im = load_image_color(paths[i], w, h); |
| | | int shift = 32; |
| | | image im = load_image_color(paths[i], w+shift, h+shift); |
| | | image images[10]; |
| | | images[0] = crop_image(im, -shift, -shift, w, h); |
| | | images[1] = crop_image(im, shift, -shift, w, h); |
| | |
| | | float avg_topk = 0; |
| | | int *indexes = calloc(topk, sizeof(int)); |
| | | |
| | | int size = net.w; |
| | | for(i = 0; i < m; ++i){ |
| | | int class = -1; |
| | | char *path = paths[i]; |
| | |
| | | } |
| | | } |
| | | image im = load_image_color(paths[i], 0, 0); |
| | | resize_network(&net, im.w, im.h); |
| | | image resized = resize_min(im, size); |
| | | resize_network(&net, resized.w, resized.h); |
| | | //show_image(im, "orig"); |
| | | //show_image(crop, "cropped"); |
| | | //cvWaitKey(0); |
| | | float *pred = network_predict(net, im.data); |
| | | float *pred = network_predict(net, resized.data); |
| | | |
| | | free_image(im); |
| | | free_image(resized); |
| | | top_k(pred, classes, topk, indexes); |
| | | |
| | | if(indexes[0] == class) avg_acc += 1; |
| | |
| | | |
| | | char **labels = get_labels(label_list); |
| | | list *plist = get_paths(valid_list); |
| | | int scales[] = {224, 256, 384, 480, 512}; |
| | | int scales[] = {192, 224, 288, 320, 352}; |
| | | int nscales = sizeof(scales)/sizeof(scales[0]); |
| | | |
| | | char **paths = (char **)list_to_array(plist); |
| | |
| | | float *pred = calloc(classes, sizeof(float)); |
| | | image im = load_image_color(paths[i], 0, 0); |
| | | for(j = 0; j < nscales; ++j){ |
| | | int w, h; |
| | | if(im.w < im.h){ |
| | | w = scales[j]; |
| | | h = (im.h*w)/im.w; |
| | | } else { |
| | | h = scales[j]; |
| | | w = (im.w * h) / im.h; |
| | | } |
| | | resize_network(&net, w, h); |
| | | image r = resize_image(im, w, h); |
| | | image r = resize_min(im, scales[j]); |
| | | resize_network(&net, r.w, r.h); |
| | | float *p = network_predict(net, r.data); |
| | | axpy_cpu(classes, 1, p, 1, pred, 1); |
| | | flip_image(r); |
| | |
| | | } |
| | | |
| | | |
| | | void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename) |
| | | { |
| | | #ifdef OPENCV |
| | | printf("Classifier Demo\n"); |
| | | network net = parse_network_cfg(cfgfile); |
| | | if(weightfile){ |
| | | load_weights(&net, weightfile); |
| | | } |
| | | set_batch_network(&net, 1); |
| | | list *options = read_data_cfg(datacfg); |
| | | |
| | | srand(2222222); |
| | | CvCapture * cap; |
| | | |
| | | if(filename){ |
| | | cap = cvCaptureFromFile(filename); |
| | | }else{ |
| | | cap = cvCaptureFromCAM(cam_index); |
| | | } |
| | | |
| | | int top = option_find_int(options, "top", 1); |
| | | |
| | | char *name_list = option_find_str(options, "names", 0); |
| | | char **names = get_labels(name_list); |
| | | |
| | | int *indexes = calloc(top, sizeof(int)); |
| | | |
| | | if(!cap) error("Couldn't connect to webcam.\n"); |
| | | cvNamedWindow("Classifier", CV_WINDOW_NORMAL); |
| | | cvResizeWindow("Classifier", 512, 512); |
| | | float fps = 0; |
| | | int i; |
| | | |
| | | while(1){ |
| | | struct timeval tval_before, tval_after, tval_result; |
| | | gettimeofday(&tval_before, NULL); |
| | | |
| | | image in = get_image_from_stream(cap); |
| | | image in_s = resize_image(in, net.w, net.h); |
| | | show_image(in, "Classifier"); |
| | | |
| | | float *predictions = network_predict(net, in_s.data); |
| | | top_predictions(net, top, indexes); |
| | | |
| | | printf("\033[2J"); |
| | | printf("\033[1;1H"); |
| | | printf("\nFPS:%.0f\n",fps); |
| | | |
| | | for(i = 0; i < top; ++i){ |
| | | int index = indexes[i]; |
| | | printf("%.1f%%: %s\n", predictions[index]*100, names[index]); |
| | | } |
| | | |
| | | free_image(in_s); |
| | | free_image(in); |
| | | |
| | | cvWaitKey(10); |
| | | |
| | | 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; |
| | | } |
| | | #endif |
| | | } |
| | | |
| | | |
| | | void run_classifier(int argc, char **argv) |
| | | { |
| | | if(argc < 4){ |
| | |
| | | return; |
| | | } |
| | | |
| | | int cam_index = find_int_arg(argc, argv, "-c", 0); |
| | | char *data = argv[3]; |
| | | char *cfg = argv[4]; |
| | | char *weights = (argc > 5) ? argv[5] : 0; |
| | |
| | | int layer = layer_s ? atoi(layer_s) : -1; |
| | | if(0==strcmp(argv[2], "predict")) predict_classifier(data, cfg, weights, filename); |
| | | else if(0==strcmp(argv[2], "train")) train_classifier(data, cfg, weights); |
| | | else if(0==strcmp(argv[2], "demo")) demo_classifier(data, cfg, weights, cam_index, filename); |
| | | else if(0==strcmp(argv[2], "test")) test_classifier(data, cfg, weights, layer); |
| | | else if(0==strcmp(argv[2], "valid")) validate_classifier(data, cfg, weights); |
| | | else if(0==strcmp(argv[2], "valid10")) validate_classifier_10(data, cfg, weights); |
| | |
| | | void demo_coco(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename) |
| | | { |
| | | demo_thresh = thresh; |
| | | printf("YOLO demo\n"); |
| | | printf("COCO demo\n"); |
| | | net = parse_network_cfg(cfgfile); |
| | | if(weightfile){ |
| | | load_weights(&net, weightfile); |
| | |
| | | } |
| | | |
| | | if(!cap) error("Couldn't connect to webcam.\n"); |
| | | cvNamedWindow("YOLO", CV_WINDOW_NORMAL); |
| | | cvResizeWindow("YOLO", 512, 512); |
| | | cvNamedWindow("COCO", CV_WINDOW_NORMAL); |
| | | cvResizeWindow("COCO", 512, 512); |
| | | |
| | | detection_layer l = net.layers[net.n-1]; |
| | | int j; |
| | |
| | | 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"); |
| | | save_image(disp, "YOLO"); |
| | | show_image(disp, "COCO"); |
| | | //save_image(disp, "COCO"); |
| | | free_image(disp); |
| | | cvWaitKey(10); |
| | | pthread_join(fetch_thread, 0); |
| | |
| | | } |
| | | } |
| | | |
| | | __global__ void dot_kernel(float *output, float scale, int batch, int n, int size, float *delta) |
| | | { |
| | | int index = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
| | | int f1 = index / n; |
| | | int f2 = index % n; |
| | | if (f2 <= f1) return; |
| | | |
| | | float sum = 0; |
| | | float norm1 = 0; |
| | | float norm2 = 0; |
| | | int b, i; |
| | | for(b = 0; b < batch; ++b){ |
| | | for(i = 0; i < size; ++i){ |
| | | int i1 = b * size * n + f1 * size + i; |
| | | int i2 = b * size * n + f2 * size + i; |
| | | sum += output[i1] * output[i2]; |
| | | norm1 += output[i1] * output[i1]; |
| | | norm2 += output[i2] * output[i2]; |
| | | } |
| | | } |
| | | norm1 = sqrt(norm1); |
| | | norm2 = sqrt(norm2); |
| | | float norm = norm1 * norm2; |
| | | sum = sum / norm; |
| | | for(b = 0; b < batch; ++b){ |
| | | for(i = 0; i < size; ++i){ |
| | | int i1 = b * size * n + f1 * size + i; |
| | | int i2 = b * size * n + f2 * size + i; |
| | | delta[i1] += - scale * sum * output[i2] / norm; |
| | | delta[i2] += - scale * sum * output[i1] / norm; |
| | | } |
| | | } |
| | | } |
| | | |
| | | void dot_error_gpu(layer l) |
| | | { |
| | | dot_kernel<<<cuda_gridsize(l.n*l.n), BLOCK>>>(l.output_gpu, l.dot, l.batch, l.n, l.out_w * l.out_h, l.delta_gpu); |
| | | check_error(cudaPeekAtLastError()); |
| | | } |
| | | |
| | | void backward_bias_gpu(float *bias_updates, float *delta, int batch, int n, int size) |
| | | { |
| | | backward_bias_kernel<<<n, BLOCK>>>(bias_updates, delta, batch, n, size); |
| | |
| | | add_bias_gpu(l.output_gpu, l.biases_gpu, l.batch, l.n, n); |
| | | |
| | | activate_array_ongpu(l.output_gpu, m*n*l.batch, l.activation); |
| | | if(l.dot > 0) dot_error_gpu(l); |
| | | if(l.binary) swap_binary(&l); |
| | | } |
| | | |
| | |
| | | extern void run_vid_rnn(int argc, char **argv); |
| | | extern void run_tag(int argc, char **argv); |
| | | extern void run_cifar(int argc, char **argv); |
| | | extern void run_go(int argc, char **argv); |
| | | |
| | | void change_rate(char *filename, float scale, float add) |
| | | { |
| | |
| | | int i; |
| | | for(i = 0; i < net.n; ++i){ |
| | | layer l = net.layers[i]; |
| | | if(l.type == CONVOLUTIONAL){ |
| | | if (l.type == CONVOLUTIONAL && l.batch_normalize) { |
| | | denormalize_convolutional_layer(l); |
| | | net.layers[i].batch_normalize=0; |
| | | } |
| | |
| | | run_yolo(argc, argv); |
| | | } else if (0 == strcmp(argv[1], "cifar")){ |
| | | run_cifar(argc, argv); |
| | | } else if (0 == strcmp(argv[1], "go")){ |
| | | run_go(argc, argv); |
| | | } else if (0 == strcmp(argv[1], "rnn")){ |
| | | run_char_rnn(argc, argv); |
| | | } else if (0 == strcmp(argv[1], "vid")){ |
| | |
| | | image crop = random_crop_image(im, min, max, size); |
| | | int flip = rand_r(&data_seed)%2; |
| | | if (flip) flip_image(crop); |
| | | /* |
| | | show_image(im, "orig"); |
| | | show_image(crop, "crop"); |
| | | cvWaitKey(0); |
| | | */ |
| | | free_image(im); |
| | | X.vals[i] = crop.data; |
| | | X.cols = crop.h*crop.w*crop.c; |
| | |
| | | } |
| | | } |
| | | |
| | | void smooth_data(data d) |
| | | { |
| | | int i, j; |
| | | int scale = 1. / d.y.cols; |
| | | int eps = .1; |
| | | for(i = 0; i < d.y.rows; ++i){ |
| | | for(j = 0; j < d.y.cols; ++j){ |
| | | d.y.vals[i][j] = eps * scale + (1-eps) * d.y.vals[i][j]; |
| | | } |
| | | } |
| | | } |
| | | |
| | | data load_all_cifar10() |
| | | { |
| | |
| | | //normalize_data_rows(d); |
| | | //translate_data_rows(d, -128); |
| | | scale_data_rows(d, 1./255); |
| | | // smooth_data(d); |
| | | return d; |
| | | } |
| | | |
| | | data load_go(char *filename) |
| | | { |
| | | FILE *fp = fopen(filename, "rb"); |
| | | matrix X = make_matrix(128, 361); |
| | | matrix y = make_matrix(128, 361); |
| | | int row, col; |
| | | |
| | | if(!fp) file_error(filename); |
| | | char *label; |
| | | int count = 0; |
| | | while((label = fgetl(fp))){ |
| | | int i; |
| | | if(count == X.rows){ |
| | | X = resize_matrix(X, count*2); |
| | | y = resize_matrix(y, count*2); |
| | | } |
| | | sscanf(label, "%d %d", &row, &col); |
| | | char *board = fgetl(fp); |
| | | |
| | | int index = row*19 + col; |
| | | y.vals[count][index] = 1; |
| | | |
| | | for(i = 0; i < 19*19; ++i){ |
| | | float val = 0; |
| | | if(board[i] == '1') val = 1; |
| | | else if(board[i] == '2') val = -1; |
| | | X.vals[count][i] = val; |
| | | } |
| | | ++count; |
| | | } |
| | | X = resize_matrix(X, count); |
| | | y = resize_matrix(y, count); |
| | | |
| | | data d; |
| | | d.shallow = 0; |
| | | d.X = X; |
| | | d.y = y; |
| | | |
| | | |
| | | fclose(fp); |
| | | |
| | | return d; |
| | | } |
| | | |
| | | |
| | | void randomize_data(data d) |
| | | { |
| | | int i; |
| | |
| | | } |
| | | } |
| | | |
| | | data get_random_data(data d, int num) |
| | | { |
| | | data r = {0}; |
| | | r.shallow = 1; |
| | | |
| | | r.X.rows = num; |
| | | r.y.rows = num; |
| | | |
| | | r.X.cols = d.X.cols; |
| | | r.y.cols = d.y.cols; |
| | | |
| | | r.X.vals = calloc(num, sizeof(float *)); |
| | | r.y.vals = calloc(num, sizeof(float *)); |
| | | |
| | | int i; |
| | | for(i = 0; i < num; ++i){ |
| | | int index = rand()%d.X.rows; |
| | | r.X.vals[i] = d.X.vals[index]; |
| | | r.y.vals[i] = d.y.vals[index]; |
| | | } |
| | | return r; |
| | | } |
| | | |
| | | data *split_data(data d, int part, int total) |
| | | { |
| | | data *split = calloc(2, sizeof(data)); |
| | |
| | | data load_data_detection(int n, char **paths, int m, int classes, int w, int h, int num_boxes, int background); |
| | | data load_data_tag(char **paths, int n, int m, int k, int min, int max, int size); |
| | | data load_data_augment(char **paths, int n, int m, char **labels, int k, int min, int max, int size); |
| | | data load_go(char *filename); |
| | | |
| | | box_label *read_boxes(char *filename, int *n); |
| | | data load_cifar10_data(char *filename); |
| | |
| | | list *get_paths(char *filename); |
| | | char **get_labels(char *filename); |
| | | void get_random_batch(data d, int n, float *X, float *y); |
| | | data get_random_data(data d, int num); |
| | | void get_next_batch(data d, int n, int offset, float *X, float *y); |
| | | data load_categorical_data_csv(char *filename, int target, int k); |
| | | void normalize_data_rows(data d); |
| New file |
| | |
| | | #include "network.h" |
| | | #include "utils.h" |
| | | #include "parser.h" |
| | | #include "option_list.h" |
| | | #include "blas.h" |
| | | |
| | | #ifdef OPENCV |
| | | #include "opencv2/highgui/highgui_c.h" |
| | | #endif |
| | | |
| | | void train_go(char *cfgfile, char *weightfile) |
| | | { |
| | | data_seed = time(0); |
| | | srand(time(0)); |
| | | float avg_loss = -1; |
| | | char *base = basecfg(cfgfile); |
| | | printf("%s\n", base); |
| | | network net = parse_network_cfg(cfgfile); |
| | | if(weightfile){ |
| | | load_weights(&net, weightfile); |
| | | } |
| | | printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); |
| | | |
| | | char *backup_directory = "/home/pjreddie/backup/"; |
| | | |
| | | data train = load_go("/home/pjreddie/backup/go.train"); |
| | | int N = train.X.rows; |
| | | int epoch = (*net.seen)/N; |
| | | while(get_current_batch(net) < net.max_batches || net.max_batches == 0){ |
| | | clock_t time=clock(); |
| | | |
| | | data batch = get_random_data(train, net.batch); |
| | | int i; |
| | | for(i = 0; i < batch.X.rows; ++i){ |
| | | int flip = rand()%2; |
| | | int rotate = rand()%4; |
| | | image in = float_to_image(19, 19, 1, batch.X.vals[i]); |
| | | image out = float_to_image(19, 19, 1, batch.y.vals[i]); |
| | | //show_image_normalized(in, "in"); |
| | | //show_image_normalized(out, "out"); |
| | | if(flip){ |
| | | flip_image(in); |
| | | flip_image(out); |
| | | } |
| | | rotate_image_cw(in, rotate); |
| | | rotate_image_cw(out, rotate); |
| | | //show_image_normalized(in, "in2"); |
| | | //show_image_normalized(out, "out2"); |
| | | //cvWaitKey(0); |
| | | } |
| | | float loss = train_network(net, batch); |
| | | free_data(batch); |
| | | if(avg_loss == -1) avg_loss = loss; |
| | | avg_loss = avg_loss*.95 + loss*.05; |
| | | printf("%d, %.3f: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), (float)(*net.seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen); |
| | | if(*net.seen/N > epoch){ |
| | | epoch = *net.seen/N; |
| | | char buff[256]; |
| | | sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch); |
| | | save_weights(net, buff); |
| | | } |
| | | if(get_current_batch(net)%100 == 0){ |
| | | char buff[256]; |
| | | sprintf(buff, "%s/%s.backup",backup_directory,base); |
| | | save_weights(net, buff); |
| | | } |
| | | } |
| | | char buff[256]; |
| | | sprintf(buff, "%s/%s.weights", backup_directory, base); |
| | | save_weights(net, buff); |
| | | |
| | | free_network(net); |
| | | free(base); |
| | | free_data(train); |
| | | } |
| | | |
| | | void propagate_liberty(float *board, int *lib, int *visited, int row, int col, int num, int side) |
| | | { |
| | | if (!num) return; |
| | | if (row < 0 || row > 18 || col < 0 || col > 18) return; |
| | | int index = row*19 + col; |
| | | if (board[index] != side) return; |
| | | if (visited[index]) return; |
| | | visited[index] = 1; |
| | | lib[index] += num; |
| | | propagate_liberty(board, lib, visited, row+1, col, num, side); |
| | | propagate_liberty(board, lib, visited, row-1, col, num, side); |
| | | propagate_liberty(board, lib, visited, row, col+1, num, side); |
| | | propagate_liberty(board, lib, visited, row, col-1, num, side); |
| | | } |
| | | |
| | | int *calculate_liberties(float *board) |
| | | { |
| | | int *lib = calloc(19*19, sizeof(int)); |
| | | int visited[361]; |
| | | int i, j; |
| | | for(j = 0; j < 19; ++j){ |
| | | for(i = 0; i < 19; ++i){ |
| | | memset(visited, 0, 19*19*sizeof(int)); |
| | | int index = j*19 + i; |
| | | if(board[index]){ |
| | | printf("%d %d\n", j, i); |
| | | int side = board[index]; |
| | | int num = 0; |
| | | if (i > 0 && board[j*19 + i - 1] == 0) ++num; |
| | | if (i < 18 && board[j*19 + i + 1] == 0) ++num; |
| | | if (j > 0 && board[j*19 + i - 19] == 0) ++num; |
| | | if (j < 18 && board[j*19 + i + 19] == 0) ++num; |
| | | propagate_liberty(board, lib, visited, j, i, num, side); |
| | | } |
| | | } |
| | | } |
| | | return lib; |
| | | } |
| | | |
| | | void update_board(float *board) |
| | | { |
| | | int i; |
| | | int *l = calculate_liberties(board); |
| | | for(i = 0; i < 19*19; ++i){ |
| | | if (board[i] && !l[i]) board[i] = 0; |
| | | } |
| | | free(l); |
| | | } |
| | | |
| | | void print_board(float *board) |
| | | { |
| | | int i,j; |
| | | printf("\n\n"); |
| | | printf(" "); |
| | | for(i = 0; i < 19; ++i){ |
| | | printf("%c ", 'A' + i + 1*(i > 7)); |
| | | } |
| | | printf("\n"); |
| | | for(j = 0; j < 19; ++j){ |
| | | printf("%2d ", 19-j); |
| | | for(i = 0; i < 19; ++i){ |
| | | int index = j*19 + i; |
| | | if(board[index] > 0) printf("\u25C9 "); |
| | | else if(board[index] < 0) printf("\u25EF "); |
| | | else printf(" "); |
| | | } |
| | | printf("\n"); |
| | | } |
| | | } |
| | | |
| | | void flip_board(float *board) |
| | | { |
| | | int i; |
| | | for(i = 0; i < 19*19; ++i){ |
| | | board[i] = -board[i]; |
| | | } |
| | | } |
| | | |
| | | void test_go(char *filename, char *weightfile) |
| | | { |
| | | network net = parse_network_cfg(filename); |
| | | if(weightfile){ |
| | | load_weights(&net, weightfile); |
| | | } |
| | | srand(time(0)); |
| | | set_batch_network(&net, 1); |
| | | float *board = calloc(19*19, sizeof(float)); |
| | | float *move = calloc(19*19, sizeof(float)); |
| | | image bim = float_to_image(19, 19, 1, board); |
| | | while(1){ |
| | | float *output = network_predict(net, board); |
| | | copy_cpu(19*19, output, 1, move, 1); |
| | | int i; |
| | | for(i = 1; i < 8; ++i){ |
| | | rotate_image_cw(bim, i); |
| | | if(i >= 4) flip_image(bim); |
| | | |
| | | float *output = network_predict(net, board); |
| | | image oim = float_to_image(19, 19, 1, output); |
| | | |
| | | if(i >= 4) flip_image(oim); |
| | | rotate_image_cw(oim, -i); |
| | | |
| | | int index = max_index(output, 19*19); |
| | | int row = index / 19; |
| | | int col = index % 19; |
| | | printf("Suggested: %c %d, %.2f%%\n", col + 'A' + 1*(col > 7), 19 - row, output[index]*100); |
| | | |
| | | axpy_cpu(19*19, 1, output, 1, move, 1); |
| | | |
| | | if(i >= 4) flip_image(bim); |
| | | rotate_image_cw(bim, -i); |
| | | } |
| | | scal_cpu(19*19, 1./8., move, 1); |
| | | for(i = 0; i < 19*19; ++i){ |
| | | if(board[i]) move[i] = 0; |
| | | } |
| | | |
| | | int indexes[3]; |
| | | int row, col; |
| | | top_k(move, 19*19, 3, indexes); |
| | | print_board(board); |
| | | for(i = 0; i < 3; ++i){ |
| | | int index = indexes[i]; |
| | | row = index / 19; |
| | | col = index % 19; |
| | | printf("Suggested: %c %d, %.2f%%\n", col + 'A' + 1*(col > 7), 19 - row, move[index]*100); |
| | | } |
| | | int index = indexes[0]; |
| | | row = index / 19; |
| | | col = index % 19; |
| | | |
| | | printf("\u25C9 Enter move: "); |
| | | char c; |
| | | char *line = fgetl(stdin); |
| | | int num = sscanf(line, "%c %d", &c, &row); |
| | | if (c < 'A' || c > 'T'){ |
| | | if (c == 'p'){ |
| | | board[row*19 + col] = 1; |
| | | }else{ |
| | | char g; |
| | | num = sscanf(line, "%c %c %d", &g, &c, &row); |
| | | row = 19 - row; |
| | | col = c - 'A'; |
| | | if (col > 7) col -= 1; |
| | | board[row*19 + col] = 0; |
| | | } |
| | | } else { |
| | | row = 19 - row; |
| | | col = c - 'A'; |
| | | if (col > 7) col -= 1; |
| | | if(num == 2) board[row*19 + col] = 1; |
| | | } |
| | | update_board(board); |
| | | flip_board(board); |
| | | } |
| | | |
| | | } |
| | | |
| | | void run_go(int argc, char **argv) |
| | | { |
| | | if(argc < 4){ |
| | | fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]); |
| | | return; |
| | | } |
| | | |
| | | char *cfg = argv[3]; |
| | | char *weights = (argc > 4) ? argv[4] : 0; |
| | | if(0==strcmp(argv[2], "train")) train_go(cfg, weights); |
| | | else if(0==strcmp(argv[2], "test")) test_go(cfg, weights); |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | void transpose_image(image im) |
| | | { |
| | | assert(im.w == im.h); |
| | | int n, m; |
| | | int c; |
| | | for(c = 0; c < im.c; ++c){ |
| | | for(n = 0; n < im.w-1; ++n){ |
| | | for(m = n + 1; m < im.w; ++m){ |
| | | float swap = im.data[m + im.w*(n + im.h*c)]; |
| | | im.data[m + im.w*(n + im.h*c)] = im.data[n + im.w*(m + im.h*c)]; |
| | | im.data[n + im.w*(m + im.h*c)] = swap; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | void rotate_image_cw(image im, int times) |
| | | { |
| | | assert(im.w == im.h); |
| | | times = (times + 400) % 4; |
| | | int i, x, y, c; |
| | | int n = im.w; |
| | | for(i = 0; i < times; ++i){ |
| | | for(c = 0; c < im.c; ++c){ |
| | | for(x = 0; x < n/2; ++x){ |
| | | for(y = 0; y < (n-1)/2 + 1; ++y){ |
| | | float temp = im.data[y + im.w*(x + im.h*c)]; |
| | | im.data[y + im.w*(x + im.h*c)] = im.data[n-1-x + im.w*(y + im.h*c)]; |
| | | im.data[n-1-x + im.w*(y + im.h*c)] = im.data[n-1-y + im.w*(n-1-x + im.h*c)]; |
| | | im.data[n-1-y + im.w*(n-1-x + im.h*c)] = im.data[x + im.w*(n-1-y + im.h*c)]; |
| | | im.data[x + im.w*(n-1-y + im.h*c)] = temp; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | void flip_image(image a) |
| | | { |
| | |
| | | return filters; |
| | | } |
| | | |
| | | void show_image_normalized(image im, const char *name) |
| | | { |
| | | image c = copy_image(im); |
| | | normalize_image(c); |
| | | show_image(c, name); |
| | | free_image(c); |
| | | } |
| | | |
| | | void show_images(image *ims, int n, char *window) |
| | | { |
| | | image m = collapse_images_vert(ims, n); |
| | |
| | | void translate_image(image m, float s); |
| | | void normalize_image(image p); |
| | | image rotate_image(image m, float rad); |
| | | void rotate_image_cw(image im, int times); |
| | | void embed_image(image source, image dest, int dx, int dy); |
| | | void saturate_image(image im, float sat); |
| | | void exposure_image(image im, float sat); |
| | |
| | | image collapse_images_vert(image *ims, int n); |
| | | |
| | | void show_image(image p, const char *name); |
| | | void show_image_normalized(image im, const char *name); |
| | | void save_image(image p, const char *name); |
| | | void show_images(image *ims, int n, char *window); |
| | | void show_image_layers(image p, char *name); |
| | |
| | | int binary; |
| | | int steps; |
| | | int hidden; |
| | | float dot; |
| | | float angle; |
| | | float jitter; |
| | | float saturation; |
| | |
| | | return (float)correct/truth.rows; |
| | | } |
| | | |
| | | void scale_matrix(matrix m, float scale) |
| | | { |
| | | int i,j; |
| | | for(i = 0; i < m.rows; ++i){ |
| | | for(j = 0; j < m.cols; ++j){ |
| | | m.vals[i][j] *= scale; |
| | | } |
| | | } |
| | | } |
| | | |
| | | matrix resize_matrix(matrix m, int size) |
| | | { |
| | | int i; |
| | | if (m.rows == size) return m; |
| | | if (m.rows < size) { |
| | | m.vals = realloc(m.vals, size*sizeof(float*)); |
| | | for (i = m.rows; i < size; ++i) { |
| | | m.vals[i] = calloc(m.cols, sizeof(float)); |
| | | } |
| | | } else if (m.rows > size) { |
| | | for (i = size; i < m.rows; ++i) { |
| | | free(m.vals[i]); |
| | | } |
| | | m.vals = realloc(m.vals, size*sizeof(float*)); |
| | | } |
| | | m.rows = size; |
| | | return m; |
| | | } |
| | | |
| | | void matrix_add_matrix(matrix from, matrix to) |
| | | { |
| | | assert(from.rows == to.rows && from.cols == to.cols); |
| | |
| | | return m; |
| | | } |
| | | |
| | | void matrix_to_csv(matrix m) |
| | | { |
| | | int i, j; |
| | | |
| | | for(i = 0; i < m.rows; ++i){ |
| | | for(j = 0; j < m.cols; ++j){ |
| | | if(j > 0) printf(","); |
| | | printf("%.17g", m.vals[i][j]); |
| | | } |
| | | printf("\n"); |
| | | } |
| | | } |
| | | |
| | | void print_matrix(matrix m) |
| | | { |
| | | int i, j; |
| | |
| | | void print_matrix(matrix m); |
| | | |
| | | matrix csv_to_matrix(char *filename); |
| | | void matrix_to_csv(matrix m); |
| | | matrix hold_out_matrix(matrix *m, int n); |
| | | float matrix_topk_accuracy(matrix truth, matrix guess, int k); |
| | | void matrix_add_matrix(matrix from, matrix to); |
| | | void scale_matrix(matrix m, float scale); |
| | | matrix resize_matrix(matrix m, int size); |
| | | |
| | | float *pop_column(matrix *m, int c); |
| | | |
| | |
| | | |
| | | convolutional_layer layer = make_convolutional_layer(batch,h,w,c,n,size,stride,pad,activation, batch_normalize, binary); |
| | | layer.flipped = option_find_int_quiet(options, "flipped", 0); |
| | | layer.dot = option_find_float_quiet(options, "dot", 0); |
| | | |
| | | char *weights = option_find_str(options, "weights", 0); |
| | | char *biases = option_find_str(options, "biases", 0); |
| | |
| | | fread(l.scales, sizeof(float), l.n, fp); |
| | | fread(l.rolling_mean, sizeof(float), l.n, fp); |
| | | fread(l.rolling_variance, sizeof(float), l.n, fp); |
| | | /* |
| | | int i; |
| | | for(i = 0; i < l.n; ++i){ |
| | | if(l.rolling_mean[i] > 1 || l.rolling_mean[i] < -1 || l.rolling_variance[i] > 1 || l.rolling_variance[i] < -1) |
| | | printf("%f %f\n", l.rolling_mean[i], l.rolling_variance[i]); |
| | | } |
| | | */ |
| | | } |
| | | fflush(stdout); |
| | | fread(l.filters, sizeof(float), num, fp); |
| | | if (l.flipped) { |
| | | transpose_matrix(l.filters, l.c*l.size*l.size, l.n); |
| | |
| | | int indexes[10]; |
| | | char buff[256]; |
| | | char *input = buff; |
| | | int size = net.w; |
| | | while(1){ |
| | | if(filename){ |
| | | strncpy(input, filename, 256); |
| | |
| | | if(!input) return; |
| | | strtok(input, "\n"); |
| | | } |
| | | image im = load_image_color(input, net.w, net.h); |
| | | //resize_network(&net, im.w, im.h); |
| | | printf("%d %d\n", im.w, im.h); |
| | | image im = load_image_color(input, 0, 0); |
| | | image r = resize_min(im, size); |
| | | resize_network(&net, r.w, r.h); |
| | | printf("%d %d\n", r.w, r.h); |
| | | |
| | | float *X = im.data; |
| | | float *X = r.data; |
| | | time=clock(); |
| | | float *predictions = network_predict(net, X); |
| | | top_predictions(net, 10, indexes); |
| | |
| | | int index = indexes[i]; |
| | | printf("%.1f%%: %s\n", predictions[index]*100, names[index]); |
| | | } |
| | | free_image(r); |
| | | free_image(im); |
| | | if (filename) break; |
| | | } |
| | |
| | | #endif |
| | | */ |
| | | |
| | | void demo_yolo(char *cfgfile, char *weightfile, float thresh, int cam_index); |
| | | #ifndef GPU |
| | | void demo_yolo(char *cfgfile, char *weightfile, float thresh, int cam_index) |
| | | { |
| | | fprintf(stderr, "Darknet must be compiled with CUDA for YOLO demo.\n"); |
| | | } |
| | | #endif |
| | | void demo_yolo(char *cfgfile, char *weightfile, float thresh, int cam_index, char *filename); |
| | | |
| | | void run_yolo(int argc, char **argv) |
| | | { |
| | |
| | | else if(0==strcmp(argv[2], "train")) train_yolo(cfg, weights); |
| | | else if(0==strcmp(argv[2], "valid")) validate_yolo(cfg, weights); |
| | | else if(0==strcmp(argv[2], "recall")) validate_yolo_recall(cfg, weights); |
| | | else if(0==strcmp(argv[2], "demo")) demo_yolo(cfg, weights, thresh, cam_index); |
| | | else if(0==strcmp(argv[2], "demo")) demo_yolo(cfg, weights, thresh, cam_index, filename); |
| | | } |
| New file |
| | |
| | | #include "network.h" |
| | | #include "detection_layer.h" |
| | | #include "cost_layer.h" |
| | | #include "utils.h" |
| | | #include "parser.h" |
| | | #include "box.h" |
| | | #include "image.h" |
| | | #include <sys/time.h> |
| | | |
| | | #ifdef OPENCV |
| | | #include "opencv2/highgui/highgui.hpp" |
| | | #include "opencv2/imgproc/imgproc.hpp" |
| | | image ipl_to_image(IplImage* src); |
| | | 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); |
| | | void draw_yolo(image im, int num, float thresh, box *boxes, float **probs); |
| | | |
| | | extern char *voc_names[]; |
| | | extern image voc_labels[]; |
| | | |
| | | static float **probs; |
| | | static box *boxes; |
| | | static network net; |
| | | static image in ; |
| | | static image in_s ; |
| | | static image det ; |
| | | static image det_s; |
| | | static image disp ; |
| | | static CvCapture * cap; |
| | | static float fps = 0; |
| | | static float demo_thresh = 0; |
| | | |
| | | void *fetch_in_thread(void *ptr) |
| | | { |
| | | in = get_image_from_stream(cap); |
| | | in_s = resize_image(in, net.w, net.h); |
| | | return 0; |
| | | } |
| | | |
| | | void *detect_in_thread(void *ptr) |
| | | { |
| | | float nms = .4; |
| | | |
| | | detection_layer l = net.layers[net.n-1]; |
| | | float *X = det_s.data; |
| | | float *predictions = network_predict(net, X); |
| | | free_image(det_s); |
| | | convert_yolo_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, demo_thresh, probs, boxes, 0); |
| | | if (nms > 0) do_nms(boxes, probs, l.side*l.side*l.n, l.classes, nms); |
| | | printf("\033[2J"); |
| | | printf("\033[1;1H"); |
| | | printf("\nFPS:%.0f\n",fps); |
| | | printf("Objects:\n\n"); |
| | | draw_detections(det, l.side*l.side*l.n, demo_thresh, boxes, probs, voc_names, voc_labels, 20); |
| | | return 0; |
| | | } |
| | | |
| | | void demo_yolo(char *cfgfile, char *weightfile, float thresh, int cam_index, char *filename) |
| | | { |
| | | demo_thresh = thresh; |
| | | printf("YOLO demo\n"); |
| | | net = parse_network_cfg(cfgfile); |
| | | if(weightfile){ |
| | | load_weights(&net, weightfile); |
| | | } |
| | | set_batch_network(&net, 1); |
| | | |
| | | srand(2222222); |
| | | |
| | | if(filename){ |
| | | cap = cvCaptureFromFile(filename); |
| | | }else{ |
| | | cap = cvCaptureFromCAM(cam_index); |
| | | } |
| | | |
| | | if(!cap) error("Couldn't connect to webcam.\n"); |
| | | cvNamedWindow("YOLO", CV_WINDOW_NORMAL); |
| | | cvResizeWindow("YOLO", 512, 512); |
| | | |
| | | detection_layer l = net.layers[net.n-1]; |
| | | int j; |
| | | |
| | | boxes = (box *)calloc(l.side*l.side*l.n, sizeof(box)); |
| | | probs = (float **)calloc(l.side*l.side*l.n, sizeof(float *)); |
| | | for(j = 0; j < l.side*l.side*l.n; ++j) probs[j] = (float *)calloc(l.classes, sizeof(float *)); |
| | | |
| | | pthread_t fetch_thread; |
| | | pthread_t detect_thread; |
| | | |
| | | fetch_in_thread(0); |
| | | det = in; |
| | | det_s = in_s; |
| | | |
| | | fetch_in_thread(0); |
| | | detect_in_thread(0); |
| | | disp = det; |
| | | det = in; |
| | | det_s = in_s; |
| | | |
| | | while(1){ |
| | | struct timeval tval_before, tval_after, tval_result; |
| | | gettimeofday(&tval_before, NULL); |
| | | 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"); |
| | | show_image(disp, "YOLO"); |
| | | free_image(disp); |
| | | cvWaitKey(1); |
| | | pthread_join(fetch_thread, 0); |
| | | pthread_join(detect_thread, 0); |
| | | |
| | | 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 |
| | | void demo_yolo(char *cfgfile, char *weightfile, float thresh, int cam_index, char *filename){ |
| | | fprintf(stderr, "YOLO demo needs OpenCV for webcam images.\n"); |
| | | } |
| | | #endif |
| | | |