AlexeyAB / darknet

YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object Detection (Windows and Linux version of Darknet )
http://pjreddie.com/darknet/
Other
21.71k stars 7.96k forks source link

the function get_metadata runs error in yolo_cpp_dll #2105

Closed duohappy closed 5 years ago

duohappy commented 5 years ago

OS: win7 64 IDE: visual studio 2017

I download souce code today, and complie to yolo_cpp_dll with yolo_cpp_dll_no_gpu.sln. But, get_metadata dosen't work well I debug it, but I cant find any reason.


//#define GPU //#define CUDNN

extern "C" {

struct network_state;

struct layer;
typedef struct layer layer;

typedef struct {
    float x, y, w, h;
} box;

typedef struct detection {
    box bbox;
    int classes;
    float *prob;
    float *mask;
    float objectness;
    int sort_class;
} detection;

typedef struct {
    int w;
    int h;
    int c;
    float *data;
} image;

typedef struct {
    int classes;
    char **names;
} metadata;

typedef enum {
    CONVOLUTIONAL,
    DECONVOLUTIONAL,
    CONNECTED,
    MAXPOOL,
    SOFTMAX,
    DETECTION,
    DROPOUT,
    CROP,
    ROUTE,
    COST,
    NORMALIZATION,
    AVGPOOL,
    LOCAL,
    SHORTCUT,
    ACTIVE,
    RNN,
    GRU,
    CRNN,
    BATCHNORM,
    NETWORK,
    XNOR,
    REGION,
    YOLO,
    REORG,
    UPSAMPLE,
    REORG_OLD,
    BLANK
} LAYER_TYPE;

typedef enum {
    LOGISTIC, RELU, RELIE, LINEAR, RAMP, TANH, PLSE, LEAKY, ELU, LOGGY, STAIR, HARDTAN, LHTAN, SELU
}ACTIVATION;

typedef enum {
    SSE, MASKED, SMOOTH
} COST_TYPE;

typedef struct {
    int *leaf;
    int n;
    int *parent;
    int *child;
    int *group;
    char **name;

    int groups;
    int *group_size;
    int *group_offset;
} tree;

struct layer {
    LAYER_TYPE type;
    ACTIVATION activation;
    COST_TYPE cost_type;
    void(*forward)   (struct layer, struct network_state);
    void(*backward)  (struct layer, struct network_state);
    void(*update)    (struct layer, int, float, float, float);
    void(*forward_gpu)   (struct layer, struct network_state);
    void(*backward_gpu)  (struct layer, struct network_state);
    void(*update_gpu)    (struct layer, int, float, float, float);
    int batch_normalize;
    int shortcut;
    int batch;
    int forced;
    int flipped;
    int inputs;
    int outputs;
    int truths;
    int h, w, c;
    int out_h, out_w, out_c;
    int n;
    int max_boxes;
    int groups;
    int size;
    int side;
    int stride;
    int reverse;
    int spatial;
    int pad;
    int sqrt;
    int flip;
    int index;
    int binary;
    int xnor;
    int use_bin_output;
    int steps;
    int hidden;
    float dot;
    float angle;
    float jitter;
    float saturation;
    float exposure;
    float shift;
    float ratio;
    int focal_loss;
    int noloss;
    int softmax;
    int classes;
    int coords;
    int background;
    int rescore;
    int objectness;
    int does_cost;
    int joint;
    int noadjust;
    int reorg;
    int log;
    int tanh;
    int *mask;
    int total;
    float bflops;

    int adam;
    float B1;
    float B2;
    float eps;
    float *m_gpu;
    float *v_gpu;
    int t;
    float *m;
    float *v;

    tree *softmax_tree;
    int  *map;

    float alpha;
    float beta;
    float kappa;

    float coord_scale;
    float object_scale;
    float noobject_scale;
    float mask_scale;
    float class_scale;
    int bias_match;
    int random;
    float ignore_thresh;
    float truth_thresh;
    float thresh;
    float focus;
    int classfix;
    int absolute;

    int onlyforward;
    int stopbackward;
    int dontload;
    int dontloadscales;

    float temperature;
    float probability;
    float scale;

    int *indexes;
    float *rand;
    float *cost;
    char  *cweights;
    float *state;
    float *prev_state;
    float *forgot_state;
    float *forgot_delta;
    float *state_delta;

    float *concat;
    float *concat_delta;

    float *binary_weights;

    float *biases;
    float *bias_updates;

    float *scales;
    float *scale_updates;

    float *weights;
    float *weight_updates;

    char *align_bit_weights_gpu;
    float *mean_arr_gpu;
    float *align_workspace_gpu;
    float *transposed_align_workspace_gpu;
    int align_workspace_size;

    char *align_bit_weights;
    float *mean_arr;
    int align_bit_weights_size;
    int lda_align;
    int new_lda;
    int bit_align;

    float *col_image;
    int   * input_layers;
    int   * input_sizes;
    float * delta;
    float * output;
    float * loss;
    float * squared;
    float * norms;

    float * spatial_mean;
    float * mean;
    float * variance;

    float * mean_delta;
    float * variance_delta;

    float * rolling_mean;
    float * rolling_variance;

    float * x;
    float * x_norm;

    struct layer *input_layer;
    struct layer *self_layer;
    struct layer *output_layer;

    struct layer *input_gate_layer;
    struct layer *state_gate_layer;
    struct layer *input_save_layer;
    struct layer *state_save_layer;
    struct layer *input_state_layer;
    struct layer *state_state_layer;

    struct layer *input_z_layer;
    struct layer *state_z_layer;

    struct layer *input_r_layer;
    struct layer *state_r_layer;

    struct layer *input_h_layer;
    struct layer *state_h_layer;

    float *z_cpu;
    float *r_cpu;
    float *h_cpu;

    float *binary_input;

    size_t workspace_size;

ifdef GPU

    float *z_gpu;
    float *r_gpu;
    float *h_gpu;

    int *indexes_gpu;
    float * prev_state_gpu;
    float * forgot_state_gpu;
    float * forgot_delta_gpu;
    float * state_gpu;
    float * state_delta_gpu;
    float * gate_gpu;
    float * gate_delta_gpu;
    float * save_gpu;
    float * save_delta_gpu;
    float * concat_gpu;
    float * concat_delta_gpu;

    float *binary_input_gpu;
    float *binary_weights_gpu;

    float * mean_gpu;
    float * variance_gpu;

    float * rolling_mean_gpu;
    float * rolling_variance_gpu;

    float * variance_delta_gpu;
    float * mean_delta_gpu;

    float * col_image_gpu;

    float * x_gpu;
    float * x_norm_gpu;
    float * weights_gpu;
    float * weight_updates_gpu;

    float * weights_gpu16;
    float * weight_updates_gpu16;

    float * biases_gpu;
    float * bias_updates_gpu;

    float * scales_gpu;
    float * scale_updates_gpu;

    float * output_gpu;
    float * loss_gpu;
    float * delta_gpu;
    float * rand_gpu;
    float * squared_gpu;
    float * norms_gpu;

ifdef CUDNN

    cudnnTensorDescriptor_t srcTensorDesc, dstTensorDesc;
    cudnnTensorDescriptor_t dsrcTensorDesc, ddstTensorDesc;
    cudnnTensorDescriptor_t normTensorDesc, normDstTensorDesc, normDstTensorDescF16;
    cudnnFilterDescriptor_t weightDesc;
    cudnnFilterDescriptor_t dweightDesc;
    cudnnConvolutionDescriptor_t convDesc;
    cudnnConvolutionFwdAlgo_t fw_algo;
    cudnnConvolutionBwdDataAlgo_t bd_algo;
    cudnnConvolutionBwdFilterAlgo_t bf_algo;
    cudnnPoolingDescriptor_t poolingDesc;

endif

endif

};

typedef enum {
    CONSTANT, STEP, EXP, POLY, STEPS, SIG, RANDOM
} learning_rate_policy;

typedef struct network {
    float *workspace;
    int n;
    int batch;
    int *seen;
    float epoch;
    int subdivisions;
    float momentum;
    float decay;
    layer *layers;
    int outputs;
    float *output;
    learning_rate_policy policy;

    float learning_rate;
    float gamma;
    float scale;
    float power;
    int time_steps;
    int step;
    int max_batches;
    float *scales;
    int   *steps;
    int num_steps;
    int burn_in;

    int adam;
    float B1;
    float B2;
    float eps;

    int inputs;
    int h, w, c;
    int max_crop;
    int min_crop;
    int flip; // horizontal flip 50% probability augmentaiont for classifier training (default = 1)
    float angle;
    float aspect;
    float exposure;
    float saturation;
    float hue;
    int small_object;

    int gpu_index;
    tree *hierarchy;

ifdef GPU

    float *input_state_gpu;

    float **input_gpu;
    float **truth_gpu;
    float **input16_gpu;
    float **output16_gpu;
    size_t *max_input16_size;
    size_t *max_output16_size;
    int wait_stream;

endif

} network;

typedef struct network_state {
    float *truth;
    float *input;
    float *delta;
    float *workspace;
    int train;
    int index;
    network net;
} network_state;

} typedef int(network_width)(network net); typedef int(network_height)(network net); typedef network (load_network_custom)(char cfg, char weights, int clear, int batch); typedef metadata(get_metadata)(char file); typedef image(load_image_color)(char filename, int w, int h); typedef float (network_predict_image)(network net, image im); typedef detection (get_network_boxes)(network net, int w, int h, float thresh, float hier, int map, int relative, int num, int letter); typedef void(do_nms_sort)(detection dets, int total, int classes, float thresh); typedef void(free_image)(image m); typedef void(free_detections)(detection *dets, int n);


- main.cpp
``` C++
#include <iostream>
#include <string>
#include <stdio.h>
#include <Windows.h>

#include "darknetYolov3.h"

using namespace std;

class DarkYolo
{
public:
    DarkYolo(char *imagePath, char *configPath, char *weightPath, char *metaPath,
        float thresh, float hier_thresh, float nms);

    void load_dll(void);

    ~DarkYolo();

private:
    char *imagePath = NULL;
    char *configPath = NULL;
    char *weightPath = NULL;
    char *metaPath = NULL;

    float thresh;
    float hier_thresh;
    float nms;

    network *netMain = NULL;
    metadata *metaMain = NULL;

    HMODULE hModule = NULL;
    load_network_custom load_network = NULL;
    get_metadata load_meta = NULL;

};

DarkYolo::DarkYolo(char *imagePath, char *configPath, char *weightPath, char *metaPath,
    float thresh=0.5, float hier_thresh=0.5, float nms=0.45)
{
    this->imagePath = imagePath;
    this->configPath = configPath;
    this->weightPath = weightPath;
    this->metaPath = metaPath;

    this->thresh = thresh;
    this->hier_thresh = hier_thresh;
    this->nms = nms;
}

void DarkYolo::load_dll(void)
{
    this->hModule = LoadLibrary("./yolo_cpp_dll.dll");

    if (this->hModule != NULL)
    {
        // run well
        //this->load_network = (load_network_custom)GetProcAddress(hModule, "load_network_custom");
        //this->netMain = this->load_network(this->configPath, this->weightPath, 0, 1);

        this->load_meta = (get_metadata)GetProcAddress(hModule, "get_metadata");
        this->metaMain = &(load_meta(this->metaPath));
    }

    // error
    printf("%d\n", this->metaMain->classes);  // garbage value
    for (int i = 0; i < 80; ++i)
    {
        printf("%s\n", this->metaMain->names[i]);
    }

}

DarkYolo::~DarkYolo()
{
    if (this->hModule != NULL)
    {
        FreeLibrary(this->hModule);
    }
}

int main()
{
    char imagePath[] = "./dog.jpg";
    char configPath[] = "./cfg/yolov3.cfg";
    char weightPath[] = "./yolov3.weights";
    char metaPath[] = "./data/coco.data";

    DarkYolo dy(imagePath, configPath, weightPath, metaPath);
    dy.load_dll();

    system("pause");

    return 0;
}
AlexeyAB commented 5 years ago

@duohappy Hi,

What repository do you use?

duohappy commented 5 years ago

@duohappy Hi,

What repository do you use?

@AlexeyAB ,I just downloaded source code from your master branch repository.

I dont modify any code, and just open yolo_cpp_dll_no_gpu.sln to complie a dll.

duohappy commented 5 years ago

I'm sorry, and I made a stupid mistake.

this reason why it can't work for me is that I don't realize local variable is released.


// metadata *metaMain = NULL;
// modify code as follow

metadata metaMain = { 0 };
void DarkYolo::load_dll(void)
{
    this->hModule = LoadLibrary("./yolo_cpp_dll.dll");

    if (this->hModule != NULL)
    {

        this->load_meta = (get_metadata)GetProcAddress(hModule, "get_metadata");
        //this->metaMain = &(load_meta(this->metaPath));
               // modify code as follow

                 this->metaMain = load_meta(this->metaPath);
    }

    // right
    printf("%d\n", this->metaMain.classes);
    for (int i = 0; i < 80; ++i)
    {
        printf("%s\n", this->metaMain.names[i]);
    }

}

In order to explian, I adjust some code.

void DarkYolo::load_dll(void)
{
    this->hModule = LoadLibrary("./yolo_cpp_dll.dll");

    if (this->hModule != NULL)
    {

        this->load_meta = (get_metadata)GetProcAddress(hModule, "get_metadata");
                metaInfo = load_meta(this->metaPath)
        this->metaMain = &(metaInfo);
    }

    // variable 'metaInfo' is released
       // *(this->metaMain) is a garbage value
    printf("%d\n", this->metaMain->classes);
    for (int i = 0; i < 80; ++i)
    {
        printf("%s\n", this->metaMain->names[i]);
    }

}