testing on one image

This commit is contained in:
Joseph Redmon 2015-06-11 15:38:58 -07:00
parent d41fbf638e
commit 11c72b1132
8 changed files with 1046 additions and 30 deletions

View File

@ -1,5 +1,5 @@
GPU=1
OPENCV=1
GPU=0
OPENCV=0
DEBUG=0
ARCH= -arch=sm_52

1000
data/shortnames.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -232,7 +232,9 @@ void rgbgr_filters(convolutional_layer l)
int i;
for(i = 0; i < l.n; ++i){
image im = get_convolutional_filter(l, i);
if (im.c == 3) rgbgr_image(im);
if (im.c == 3) {
rgbgr_image(im);
}
}
}

View File

@ -77,6 +77,7 @@ void partial(char *cfgfile, char *weightfile, char *outfile, int max)
void rgbgr_filters(convolutional_layer l);
void rgbgr_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -1;
network net = parse_network_cfg(cfgfile);
if(weightfile){
load_weights(&net, weightfile);

View File

@ -18,7 +18,7 @@ void draw_detection(image im, float *box, int side, char *label)
for(c = 0; c < side; ++c){
j = (r*side + c) * elems;
int class = max_index(box+j, classes);
if(box[j+class] > 0){
if(box[j+class] > 0.2){
printf("%f %s\n", box[j+class], class_names[class]);
float red = get_color(0,class,classes);
float green = get_color(1,class,classes);
@ -67,6 +67,7 @@ void train_detection(char *cfgfile, char *weightfile)
int classes = layer.classes;
int background = (layer.background || layer.objectness);
printf("%d\n", background);
int side = sqrt(get_detection_layer_locations(layer));
char **paths;
@ -205,8 +206,9 @@ void validate_detection(char *cfgfile, char *weightfile)
fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)(time(0) - start));
}
void test_detection(char *cfgfile, char *weightfile)
void test_detection(char *cfgfile, char *weightfile, char *filename)
{
network net = parse_network_cfg(cfgfile);
if(weightfile){
load_weights(&net, weightfile);
@ -217,24 +219,30 @@ void test_detection(char *cfgfile, char *weightfile)
set_batch_network(&net, 1);
srand(2222222);
clock_t time;
char filename[256];
char input[256];
while(1){
printf("Image Path: ");
fflush(stdout);
fgets(filename, 256, stdin);
strtok(filename, "\n");
image im = load_image_color(filename,0,0);
if(filename){
strncpy(input, filename, 256);
} else {
printf("Enter Image Path: ");
fflush(stdout);
fgets(input, 256, stdin);
strtok(input, "\n");
}
image im = load_image_color(input,0,0);
image sized = resize_image(im, im_size, im_size);
float *X = sized.data;
time=clock();
float *predictions = network_predict(net, X);
printf("%s: Predicted in %f seconds.\n", filename, sec(clock()-time));
printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
draw_detection(im, predictions, 7, "predictions");
free_image(im);
free_image(sized);
#ifdef OPENCV
#ifdef OPENCV
cvWaitKey(0);
#endif
cvDestroyAllWindows();
#endif
if (filename) break;
}
}
@ -247,7 +255,8 @@ void run_detection(int argc, char **argv)
char *cfg = argv[3];
char *weights = (argc > 4) ? argv[4] : 0;
if(0==strcmp(argv[2], "test")) test_detection(cfg, weights);
char *filename = (argc > 5) ? argv[5]: 0;
if(0==strcmp(argv[2], "test")) test_detection(cfg, weights, filename);
else if(0==strcmp(argv[2], "train")) train_detection(cfg, weights);
else if(0==strcmp(argv[2], "valid")) validate_detection(cfg, weights);
}

View File

@ -29,10 +29,10 @@ detection_layer make_detection_layer(int batch, int inputs, int classes, int coo
l.coords = coords;
l.rescore = rescore;
l.objectness = objectness;
l.background = background;
l.joint = joint;
l.cost = calloc(1, sizeof(float));
l.does_cost=1;
l.background = background;
int outputs = get_detection_layer_output_size(l);
l.outputs = outputs;
l.output = calloc(batch*outputs, sizeof(float));

View File

@ -101,37 +101,40 @@ void validate_imagenet(char *filename, char *weightfile)
}
}
void test_imagenet(char *cfgfile, char *weightfile)
void test_imagenet(char *cfgfile, char *weightfile, char *filename)
{
network net = parse_network_cfg(cfgfile);
if(weightfile){
load_weights(&net, weightfile);
}
set_batch_network(&net, 1);
//imgs=1;
srand(2222222);
int i = 0;
char **names = get_labels("cfg/shortnames.txt");
char **names = get_labels("data/shortnames.txt");
clock_t time;
char filename[256];
char input[256];
int indexes[10];
while(1){
fgets(filename, 256, stdin);
strtok(filename, "\n");
image im = load_image_color(filename, 256, 256);
scale_image(im, 2.);
translate_image(im, -1.);
printf("%d %d %d\n", im.h, im.w, im.c);
if(filename){
strncpy(input, filename, 256);
}else{
printf("Enter Image Path: ");
fflush(stdout);
fgets(input, 256, stdin);
strtok(input, "\n");
}
image im = load_image_color(input, 256, 256);
float *X = im.data;
time=clock();
float *predictions = network_predict(net, X);
top_predictions(net, 10, indexes);
printf("%s: Predicted in %f seconds.\n", filename, sec(clock()-time));
printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
for(i = 0; i < 10; ++i){
int index = indexes[i];
printf("%s: %f\n", names[index], predictions[index]);
}
free_image(im);
if (filename) break;
}
}
@ -144,7 +147,8 @@ void run_imagenet(int argc, char **argv)
char *cfg = argv[3];
char *weights = (argc > 4) ? argv[4] : 0;
if(0==strcmp(argv[2], "test")) test_imagenet(cfg, weights);
char *filename = (argc > 5) ? argv[5]: 0;
if(0==strcmp(argv[2], "test")) test_imagenet(cfg, weights, filename);
else if(0==strcmp(argv[2], "train")) train_imagenet(cfg, weights);
else if(0==strcmp(argv[2], "valid")) validate_imagenet(cfg, weights);
}

View File

@ -73,11 +73,11 @@ float sec(clock_t clocks)
void top_k(float *a, int n, int k, int *index)
{
int i,j;
for(j = 0; j < k; ++j) index[j] = 0;
for(j = 0; j < k; ++j) index[j] = -1;
for(i = 0; i < n; ++i){
int curr = i;
for(j = 0; j < k; ++j){
if(a[curr] > a[index[j]]){
if((index[j] < 0) || a[curr] > a[index[j]]){
int swap = curr;
curr = index[j];
index[j] = swap;