派生自 Algorithm/baseDetector

Scheaven
2021-06-03 168af40fe9a3cc81c6ee16b3e81f154780c36bdb
lib/detecter_tools/darknet/classifier.c
@@ -32,6 +32,7 @@
    int i;
    float avg_loss = -1;
    float avg_contrastive_acc = 0;
    char *base = basecfg(cfgfile);
    printf("%s\n", base);
    printf("%d\n", ngpus);
@@ -69,13 +70,18 @@
    int topk_data = option_find_int(options, "top", 5);
    char topk_buff[10];
    sprintf(topk_buff, "top%d", topk_data);
    if (classes != net.layers[net.n - 1].inputs) {
    layer l = net.layers[net.n - 1];
    if (classes != l.outputs && (l.type == SOFTMAX || l.type == COST)) {
        printf("\n Error: num of filters = %d in the last conv-layer in cfg-file doesn't match to classes = %d in data-file \n",
            net.layers[net.n - 1].inputs, classes);
            l.outputs, classes);
        getchar();
    }
    char **labels = get_labels(label_list);
    if (net.unsupervised) {
        free(labels);
        labels = NULL;
    }
    list *plist = get_paths(train_list);
    char **paths = (char **)list_to_array(plist);
    printf("%d\n", plist->size);
@@ -87,8 +93,10 @@
    args.h = net.h;
    args.c = net.c;
    args.threads = 32;
    if (net.contrastive && args.threads > net.batch/2) args.threads = net.batch / 2;
    args.hierarchy = net.hierarchy;
    args.contrastive = net.contrastive;
    args.dontuse_opencv = dontuse_opencv;
    args.min = net.min_crop;
    args.max = net.max_crop;
@@ -116,7 +124,7 @@
#ifdef OPENCV
    //args.threads = 3;
    mat_cv* img = NULL;
    float max_img_loss = 10;
    float max_img_loss = net.max_chart_loss;
    int number_of_lines = 100;
    int img_size = 1000;
    char windows_name[100];
@@ -182,8 +190,16 @@
        int draw_precision = 0;
        if (calc_topk && (i >= calc_topk_for_each || i == net.max_batches)) {
            iter_topk = i;
            if (net.contrastive && l.type != SOFTMAX && l.type != COST) {
                int k;
                for (k = 0; k < net.n; ++k) if (net.layers[k].type == CONTRASTIVE) break;
                topk = *(net.layers[k].loss) / 100;
                sprintf(topk_buff, "Contr");
            }
            else {
            topk = validate_classifier_single(datacfg, cfgfile, weightfile, &net, topk_data); // calc TOP-n
            printf("\n accuracy %s = %f \n", topk_buff, topk);
            }
            draw_precision = 1;
        }
@@ -194,7 +210,15 @@
        start = what_time_is_it_now();
        printf("%d, %.3f: %f, %f avg, %f rate, %lf seconds, %ld images, %f hours left\n", get_current_batch(net), (float)(*net.seen)/ train_images_num, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen, avg_time);
#ifdef OPENCV
        if (!dontuse_opencv) draw_train_loss(windows_name, img, img_size, avg_loss, max_img_loss, i, net.max_batches, topk, draw_precision, topk_buff, dont_show, mjpeg_port, avg_time);
        if (net.contrastive) {
            float cur_con_acc = -1;
            int k;
            for (k = 0; k < net.n; ++k)
                if (net.layers[k].type == CONTRASTIVE) cur_con_acc = *net.layers[k].loss;
            if (cur_con_acc >= 0) avg_contrastive_acc = avg_contrastive_acc*0.99 + cur_con_acc * 0.01;
            printf("  avg_contrastive_acc = %f \n", avg_contrastive_acc);
        }
        if (!dontuse_opencv) draw_train_loss(windows_name, img, img_size, avg_loss, max_img_loss, i, net.max_batches, topk, draw_precision, topk_buff, avg_contrastive_acc / 100, dont_show, mjpeg_port, avg_time);
#endif  // OPENCV
        if (i >= (iter_save + 1000)) {
@@ -238,10 +262,9 @@
    free(nets);
    //free_ptrs((void**)labels, classes);
    free(labels);
    if(labels) free(labels);
    free_ptrs((void**)paths, plist->size);
    free_list(plist);
    free(nets);
    free(base);
    free_list_contents_kvp(options);
@@ -818,9 +841,10 @@
    if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list");
    int classes = option_find_int(options, "classes", 2);
    printf(" classes = %d, output in cfg = %d \n", classes, net.layers[net.n - 1].c);
    if (classes != net.layers[net.n - 1].inputs) {
    layer l = net.layers[net.n - 1];
    if (classes != l.outputs && (l.type == SOFTMAX || l.type == COST)) {
        printf("\n Error: num of filters = %d in the last conv-layer in cfg-file doesn't match to classes = %d in data-file \n",
            net.layers[net.n - 1].inputs, classes);
            l.outputs, classes);
        getchar();
    }
    if (top == 0) top = option_find_int(options, "top", 1);
@@ -845,12 +869,10 @@
        }
        image im = load_image_color(input, 0, 0);
        image resized = resize_min(im, net.w);
        image r = crop_image(resized, (resized.w - net.w)/2, (resized.h - net.h)/2, net.w, net.h);
        //image r = resize_min(im, size);
        //resize_network(&net, r.w, r.h);
        printf("%d %d\n", r.w, r.h);
        image cropped = crop_image(resized, (resized.w - net.w)/2, (resized.h - net.h)/2, net.w, net.h);
        printf("%d %d\n", cropped.w, cropped.h);
        float *X = r.data;
        float *X = cropped.data;
        double time = get_time_point();
        float *predictions = network_predict(net, X);
@@ -864,9 +886,13 @@
            if(net.hierarchy) printf("%d, %s: %f, parent: %s \n",index, names[index], predictions[index], (net.hierarchy->parent[index] >= 0) ? names[net.hierarchy->parent[index]] : "Root");
            else printf("%s: %f\n",names[index], predictions[index]);
        }
        if(r.data != im.data) free_image(r);
        free_image(im);
        free_image(cropped);
        if (resized.data != im.data) {
        free_image(resized);
        }
        free_image(im);
        if (filename) break;
    }
    free(indexes);