darknet.c
Go to the documentation of this file.
00001 #include <time.h>
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 
00005 #include "parser.h"
00006 #include "utils.h"
00007 #include "cuda.h"
00008 #include "blas.h"
00009 #include "connected_layer.h"
00010 
00011 #ifdef OPENCV
00012 #include "opencv2/highgui/highgui_c.h"
00013 #endif
00014 
00015 extern void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top);
00016 extern void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh);
00017 extern void run_voxel(int argc, char **argv);
00018 extern void run_yolo(int argc, char **argv);
00019 extern void run_detector(int argc, char **argv);
00020 extern void run_coco(int argc, char **argv);
00021 extern void run_writing(int argc, char **argv);
00022 extern void run_captcha(int argc, char **argv);
00023 extern void run_nightmare(int argc, char **argv);
00024 extern void run_dice(int argc, char **argv);
00025 extern void run_compare(int argc, char **argv);
00026 extern void run_classifier(int argc, char **argv);
00027 extern void run_char_rnn(int argc, char **argv);
00028 extern void run_vid_rnn(int argc, char **argv);
00029 extern void run_tag(int argc, char **argv);
00030 extern void run_cifar(int argc, char **argv);
00031 extern void run_go(int argc, char **argv);
00032 extern void run_art(int argc, char **argv);
00033 extern void run_super(int argc, char **argv);
00034 
00035 void average(int argc, char *argv[])
00036 {
00037     char *cfgfile = argv[2];
00038     char *outfile = argv[3];
00039     gpu_index = -1;
00040     network net = parse_network_cfg(cfgfile);
00041     network sum = parse_network_cfg(cfgfile);
00042 
00043     char *weightfile = argv[4];   
00044     load_weights(&sum, weightfile);
00045 
00046     int i, j;
00047     int n = argc - 5;
00048     for(i = 0; i < n; ++i){
00049         weightfile = argv[i+5];   
00050         load_weights(&net, weightfile);
00051         for(j = 0; j < net.n; ++j){
00052             layer l = net.layers[j];
00053             layer out = sum.layers[j];
00054             if(l.type == CONVOLUTIONAL){
00055                 int num = l.n*l.c*l.size*l.size;
00056                 axpy_cpu(l.n, 1, l.biases, 1, out.biases, 1);
00057                 axpy_cpu(num, 1, l.weights, 1, out.weights, 1);
00058                 if(l.batch_normalize){
00059                     axpy_cpu(l.n, 1, l.scales, 1, out.scales, 1);
00060                     axpy_cpu(l.n, 1, l.rolling_mean, 1, out.rolling_mean, 1);
00061                     axpy_cpu(l.n, 1, l.rolling_variance, 1, out.rolling_variance, 1);
00062                 }
00063             }
00064             if(l.type == CONNECTED){
00065                 axpy_cpu(l.outputs, 1, l.biases, 1, out.biases, 1);
00066                 axpy_cpu(l.outputs*l.inputs, 1, l.weights, 1, out.weights, 1);
00067             }
00068         }
00069     }
00070     n = n+1;
00071     for(j = 0; j < net.n; ++j){
00072         layer l = sum.layers[j];
00073         if(l.type == CONVOLUTIONAL){
00074             int num = l.n*l.c*l.size*l.size;
00075             scal_cpu(l.n, 1./n, l.biases, 1);
00076             scal_cpu(num, 1./n, l.weights, 1);
00077                 if(l.batch_normalize){
00078                     scal_cpu(l.n, 1./n, l.scales, 1);
00079                     scal_cpu(l.n, 1./n, l.rolling_mean, 1);
00080                     scal_cpu(l.n, 1./n, l.rolling_variance, 1);
00081                 }
00082         }
00083         if(l.type == CONNECTED){
00084             scal_cpu(l.outputs, 1./n, l.biases, 1);
00085             scal_cpu(l.outputs*l.inputs, 1./n, l.weights, 1);
00086         }
00087     }
00088     save_weights(sum, outfile);
00089 }
00090 
00091 void speed(char *cfgfile, int tics)
00092 {
00093     if (tics == 0) tics = 1000;
00094     network net = parse_network_cfg(cfgfile);
00095     set_batch_network(&net, 1);
00096     int i;
00097     time_t start = time(0);
00098     image im = make_image(net.w, net.h, net.c);
00099     for(i = 0; i < tics; ++i){
00100         network_predict(net, im.data);
00101     }
00102     double t = difftime(time(0), start);
00103     printf("\n%d evals, %f Seconds\n", tics, t);
00104     printf("Speed: %f sec/eval\n", t/tics);
00105     printf("Speed: %f Hz\n", tics/t);
00106 }
00107 
00108 void operations(char *cfgfile)
00109 {
00110     gpu_index = -1;
00111     network net = parse_network_cfg(cfgfile);
00112     int i;
00113     long ops = 0;
00114     for(i = 0; i < net.n; ++i){
00115         layer l = net.layers[i];
00116         if(l.type == CONVOLUTIONAL){
00117             ops += 2l * l.n * l.size*l.size*l.c * l.out_h*l.out_w;
00118         } else if(l.type == CONNECTED){
00119             ops += 2l * l.inputs * l.outputs;
00120         }
00121     }
00122     printf("Floating Point Operations: %ld\n", ops);
00123     printf("Floating Point Operations: %.2f Bn\n", (float)ops/1000000000.);
00124 }
00125 
00126 void oneoff(char *cfgfile, char *weightfile, char *outfile)
00127 {
00128     gpu_index = -1;
00129     network net = parse_network_cfg(cfgfile);
00130     int oldn = net.layers[net.n - 2].n;
00131     int c = net.layers[net.n - 2].c;
00132     net.layers[net.n - 2].n = 9372;
00133     net.layers[net.n - 2].biases += 5;
00134     net.layers[net.n - 2].weights += 5*c;
00135     if(weightfile){
00136         load_weights(&net, weightfile);
00137     }
00138     net.layers[net.n - 2].biases -= 5;
00139     net.layers[net.n - 2].weights -= 5*c;
00140     net.layers[net.n - 2].n = oldn;
00141     printf("%d\n", oldn);
00142     layer l = net.layers[net.n - 2];
00143     copy_cpu(l.n/3, l.biases, 1, l.biases +   l.n/3, 1);
00144     copy_cpu(l.n/3, l.biases, 1, l.biases + 2*l.n/3, 1);
00145     copy_cpu(l.n/3*l.c, l.weights, 1, l.weights +   l.n/3*l.c, 1);
00146     copy_cpu(l.n/3*l.c, l.weights, 1, l.weights + 2*l.n/3*l.c, 1);
00147     *net.seen = 0;
00148     save_weights(net, outfile);
00149 }
00150 
00151 void partial(char *cfgfile, char *weightfile, char *outfile, int max)
00152 {
00153     gpu_index = -1;
00154     network net = parse_network_cfg(cfgfile);
00155     if(weightfile){
00156         load_weights_upto(&net, weightfile, max);
00157     }
00158     *net.seen = 0;
00159     save_weights_upto(net, outfile, max);
00160 }
00161 
00162 #include "convolutional_layer.h"
00163 void rescale_net(char *cfgfile, char *weightfile, char *outfile)
00164 {
00165     gpu_index = -1;
00166     network net = parse_network_cfg(cfgfile);
00167     if(weightfile){
00168         load_weights(&net, weightfile);
00169     }
00170     int i;
00171     for(i = 0; i < net.n; ++i){
00172         layer l = net.layers[i];
00173         if(l.type == CONVOLUTIONAL){
00174             rescale_weights(l, 2, -.5);
00175             break;
00176         }
00177     }
00178     save_weights(net, outfile);
00179 }
00180 
00181 void rgbgr_net(char *cfgfile, char *weightfile, char *outfile)
00182 {
00183     gpu_index = -1;
00184     network net = parse_network_cfg(cfgfile);
00185     if(weightfile){
00186         load_weights(&net, weightfile);
00187     }
00188     int i;
00189     for(i = 0; i < net.n; ++i){
00190         layer l = net.layers[i];
00191         if(l.type == CONVOLUTIONAL){
00192             rgbgr_weights(l);
00193             break;
00194         }
00195     }
00196     save_weights(net, outfile);
00197 }
00198 
00199 void reset_normalize_net(char *cfgfile, char *weightfile, char *outfile)
00200 {
00201     gpu_index = -1;
00202     network net = parse_network_cfg(cfgfile);
00203     if (weightfile) {
00204         load_weights(&net, weightfile);
00205     }
00206     int i;
00207     for (i = 0; i < net.n; ++i) {
00208         layer l = net.layers[i];
00209         if (l.type == CONVOLUTIONAL && l.batch_normalize) {
00210             denormalize_convolutional_layer(l);
00211         }
00212         if (l.type == CONNECTED && l.batch_normalize) {
00213             denormalize_connected_layer(l);
00214         }
00215         if (l.type == GRU && l.batch_normalize) {
00216             denormalize_connected_layer(*l.input_z_layer);
00217             denormalize_connected_layer(*l.input_r_layer);
00218             denormalize_connected_layer(*l.input_h_layer);
00219             denormalize_connected_layer(*l.state_z_layer);
00220             denormalize_connected_layer(*l.state_r_layer);
00221             denormalize_connected_layer(*l.state_h_layer);
00222         }
00223     }
00224     save_weights(net, outfile);
00225 }
00226 
00227 layer normalize_layer(layer l, int n)
00228 {
00229     int j;
00230     l.batch_normalize=1;
00231     l.scales = calloc(n, sizeof(float));
00232     for(j = 0; j < n; ++j){
00233         l.scales[j] = 1;
00234     }
00235     l.rolling_mean = calloc(n, sizeof(float));
00236     l.rolling_variance = calloc(n, sizeof(float));
00237     return l;
00238 }
00239 
00240 void normalize_net(char *cfgfile, char *weightfile, char *outfile)
00241 {
00242     gpu_index = -1;
00243     network net = parse_network_cfg(cfgfile);
00244     if(weightfile){
00245         load_weights(&net, weightfile);
00246     }
00247     int i;
00248     for(i = 0; i < net.n; ++i){
00249         layer l = net.layers[i];
00250         if(l.type == CONVOLUTIONAL && !l.batch_normalize){
00251             net.layers[i] = normalize_layer(l, l.n);
00252         }
00253         if (l.type == CONNECTED && !l.batch_normalize) {
00254             net.layers[i] = normalize_layer(l, l.outputs);
00255         }
00256         if (l.type == GRU && l.batch_normalize) {
00257             *l.input_z_layer = normalize_layer(*l.input_z_layer, l.input_z_layer->outputs);
00258             *l.input_r_layer = normalize_layer(*l.input_r_layer, l.input_r_layer->outputs);
00259             *l.input_h_layer = normalize_layer(*l.input_h_layer, l.input_h_layer->outputs);
00260             *l.state_z_layer = normalize_layer(*l.state_z_layer, l.state_z_layer->outputs);
00261             *l.state_r_layer = normalize_layer(*l.state_r_layer, l.state_r_layer->outputs);
00262             *l.state_h_layer = normalize_layer(*l.state_h_layer, l.state_h_layer->outputs);
00263             net.layers[i].batch_normalize=1;
00264         }
00265     }
00266     save_weights(net, outfile);
00267 }
00268 
00269 void statistics_net(char *cfgfile, char *weightfile)
00270 {
00271     gpu_index = -1;
00272     network net = parse_network_cfg(cfgfile);
00273     if (weightfile) {
00274         load_weights(&net, weightfile);
00275     }
00276     int i;
00277     for (i = 0; i < net.n; ++i) {
00278         layer l = net.layers[i];
00279         if (l.type == CONNECTED && l.batch_normalize) {
00280             printf("Connected Layer %d\n", i);
00281             statistics_connected_layer(l);
00282         }
00283         if (l.type == GRU && l.batch_normalize) {
00284             printf("GRU Layer %d\n", i);
00285             printf("Input Z\n");
00286             statistics_connected_layer(*l.input_z_layer);
00287             printf("Input R\n");
00288             statistics_connected_layer(*l.input_r_layer);
00289             printf("Input H\n");
00290             statistics_connected_layer(*l.input_h_layer);
00291             printf("State Z\n");
00292             statistics_connected_layer(*l.state_z_layer);
00293             printf("State R\n");
00294             statistics_connected_layer(*l.state_r_layer);
00295             printf("State H\n");
00296             statistics_connected_layer(*l.state_h_layer);
00297         }
00298         printf("\n");
00299     }
00300 }
00301 
00302 void denormalize_net(char *cfgfile, char *weightfile, char *outfile)
00303 {
00304     gpu_index = -1;
00305     network net = parse_network_cfg(cfgfile);
00306     if (weightfile) {
00307         load_weights(&net, weightfile);
00308     }
00309     int i;
00310     for (i = 0; i < net.n; ++i) {
00311         layer l = net.layers[i];
00312         if (l.type == CONVOLUTIONAL && l.batch_normalize) {
00313             denormalize_convolutional_layer(l);
00314             net.layers[i].batch_normalize=0;
00315         }
00316         if (l.type == CONNECTED && l.batch_normalize) {
00317             denormalize_connected_layer(l);
00318             net.layers[i].batch_normalize=0;
00319         }
00320         if (l.type == GRU && l.batch_normalize) {
00321             denormalize_connected_layer(*l.input_z_layer);
00322             denormalize_connected_layer(*l.input_r_layer);
00323             denormalize_connected_layer(*l.input_h_layer);
00324             denormalize_connected_layer(*l.state_z_layer);
00325             denormalize_connected_layer(*l.state_r_layer);
00326             denormalize_connected_layer(*l.state_h_layer);
00327             l.input_z_layer->batch_normalize = 0;
00328             l.input_r_layer->batch_normalize = 0;
00329             l.input_h_layer->batch_normalize = 0;
00330             l.state_z_layer->batch_normalize = 0;
00331             l.state_r_layer->batch_normalize = 0;
00332             l.state_h_layer->batch_normalize = 0;
00333             net.layers[i].batch_normalize=0;
00334         }
00335     }
00336     save_weights(net, outfile);
00337 }
00338 
00339 void visualize(char *cfgfile, char *weightfile)
00340 {
00341     network net = parse_network_cfg(cfgfile);
00342     if(weightfile){
00343         load_weights(&net, weightfile);
00344     }
00345     visualize_network(net);
00346 #ifdef OPENCV
00347     cvWaitKey(0);
00348 #endif
00349 }
00350 
00351 int main(int argc, char **argv)
00352 {
00353     //test_resize("data/bad.jpg");
00354     //test_box();
00355     //test_convolutional_layer();
00356     if(argc < 2){
00357         fprintf(stderr, "usage: %s <function>\n", argv[0]);
00358         return 0;
00359     }
00360     gpu_index = find_int_arg(argc, argv, "-i", 0);
00361     if(find_arg(argc, argv, "-nogpu")) {
00362         gpu_index = -1;
00363     }
00364 
00365 #ifndef GPU
00366     gpu_index = -1;
00367 #else
00368     if(gpu_index >= 0){
00369         cuda_set_device(gpu_index);
00370     }
00371 #endif
00372     float thresh = find_float_arg(argc, argv, "-thresh", .25);
00373     char *filename = (argc > 1) ? argv[1]: 0;
00374     test_detector("cfg/coco.data", "cfg/yolo.cfg", "yolo.weights", filename, thresh);
00375     return 0;
00376 }
00377 


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:29