2015-11-04 06:23:17 +03:00
|
|
|
#include "network.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "option_list.h"
|
2016-01-19 02:40:14 +03:00
|
|
|
#include "blas.h"
|
2016-08-06 01:27:07 +03:00
|
|
|
#include "assert.h"
|
2016-05-07 02:25:16 +03:00
|
|
|
#include "classifier.h"
|
2016-03-14 09:18:42 +03:00
|
|
|
#include <sys/time.h>
|
2015-11-04 06:23:17 +03:00
|
|
|
|
|
|
|
#ifdef OPENCV
|
|
|
|
#include "opencv2/highgui/highgui_c.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
list *read_data_cfg(char *filename)
|
|
|
|
{
|
|
|
|
FILE *file = fopen(filename, "r");
|
|
|
|
if(file == 0) file_error(filename);
|
|
|
|
char *line;
|
|
|
|
int nu = 0;
|
|
|
|
list *options = make_list();
|
|
|
|
while((line=fgetl(file)) != 0){
|
|
|
|
++ nu;
|
|
|
|
strip(line);
|
|
|
|
switch(line[0]){
|
|
|
|
case '\0':
|
|
|
|
case '#':
|
|
|
|
case ';':
|
|
|
|
free(line);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if(!read_option(line, options)){
|
|
|
|
fprintf(stderr, "Config file error line %d, could parse: %s\n", nu, line);
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2016-06-03 01:25:24 +03:00
|
|
|
void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int clear)
|
2015-11-04 06:23:17 +03:00
|
|
|
{
|
2016-08-11 21:54:24 +03:00
|
|
|
int nthreads = 8;
|
2016-08-06 01:27:07 +03:00
|
|
|
int i;
|
|
|
|
|
2015-11-04 06:23:17 +03:00
|
|
|
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);
|
|
|
|
}
|
2016-06-03 01:25:24 +03:00
|
|
|
if(clear) *net.seen = 0;
|
2015-11-04 06:23:17 +03:00
|
|
|
printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
|
2016-08-06 01:27:07 +03:00
|
|
|
int imgs = net.batch*net.subdivisions/nthreads;
|
|
|
|
assert(net.batch*net.subdivisions % nthreads == 0);
|
2015-11-04 06:23:17 +03:00
|
|
|
|
|
|
|
list *options = read_data_cfg(datacfg);
|
|
|
|
|
|
|
|
char *backup_directory = option_find_str(options, "backup", "/backup/");
|
|
|
|
char *label_list = option_find_str(options, "labels", "data/labels.list");
|
|
|
|
char *train_list = option_find_str(options, "train", "data/train.list");
|
|
|
|
int classes = option_find_int(options, "classes", 2);
|
|
|
|
|
|
|
|
char **labels = get_labels(label_list);
|
|
|
|
list *plist = get_paths(train_list);
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
printf("%d\n", plist->size);
|
|
|
|
int N = plist->size;
|
|
|
|
clock_t time;
|
2016-08-06 01:27:07 +03:00
|
|
|
|
|
|
|
pthread_t *load_threads = calloc(nthreads, sizeof(pthread_t));
|
|
|
|
data *trains = calloc(nthreads, sizeof(data));
|
|
|
|
data *buffers = calloc(nthreads, sizeof(data));
|
2015-11-04 06:23:17 +03:00
|
|
|
|
|
|
|
load_args args = {0};
|
|
|
|
args.w = net.w;
|
|
|
|
args.h = net.h;
|
2016-03-01 00:54:12 +03:00
|
|
|
|
2016-05-07 02:25:16 +03:00
|
|
|
args.min = net.min_crop;
|
2016-03-01 00:54:12 +03:00
|
|
|
args.max = net.max_crop;
|
2016-08-11 21:54:24 +03:00
|
|
|
args.angle = net.angle;
|
|
|
|
args.exposure = net.exposure;
|
|
|
|
args.saturation = net.saturation;
|
2016-03-01 00:54:12 +03:00
|
|
|
args.size = net.w;
|
|
|
|
|
2015-11-04 06:23:17 +03:00
|
|
|
args.paths = paths;
|
|
|
|
args.classes = classes;
|
|
|
|
args.n = imgs;
|
|
|
|
args.m = N;
|
|
|
|
args.labels = labels;
|
|
|
|
args.type = CLASSIFICATION_DATA;
|
|
|
|
|
2016-08-06 01:27:07 +03:00
|
|
|
for(i = 0; i < nthreads; ++i){
|
|
|
|
args.d = buffers + i;
|
|
|
|
load_threads[i] = load_data_in_thread(args);
|
|
|
|
}
|
|
|
|
|
2015-11-04 06:23:17 +03:00
|
|
|
int epoch = (*net.seen)/N;
|
|
|
|
while(get_current_batch(net) < net.max_batches || net.max_batches == 0){
|
|
|
|
time=clock();
|
2016-08-06 01:27:07 +03:00
|
|
|
for(i = 0; i < nthreads; ++i){
|
|
|
|
pthread_join(load_threads[i], 0);
|
|
|
|
trains[i] = buffers[i];
|
|
|
|
}
|
|
|
|
data train = concat_datas(trains, nthreads);
|
|
|
|
|
|
|
|
for(i = 0; i < nthreads; ++i){
|
|
|
|
args.d = buffers + i;
|
|
|
|
load_threads[i] = load_data_in_thread(args);
|
|
|
|
}
|
2015-11-04 06:23:17 +03:00
|
|
|
|
|
|
|
printf("Loaded: %lf seconds\n", sec(clock()-time));
|
|
|
|
time=clock();
|
2016-03-01 00:54:12 +03:00
|
|
|
|
2016-08-11 21:54:24 +03:00
|
|
|
if(0){
|
|
|
|
int u;
|
|
|
|
for(u = 0; u < imgs; ++u){
|
|
|
|
image im = float_to_image(net.w, net.h, 3, train.X.vals[u]);
|
|
|
|
show_image(im, "loaded");
|
|
|
|
cvWaitKey(0);
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 00:54:12 +03:00
|
|
|
|
2015-11-04 06:23:17 +03:00
|
|
|
float loss = train_network(net, train);
|
|
|
|
if(avg_loss == -1) avg_loss = loss;
|
|
|
|
avg_loss = avg_loss*.9 + loss*.1;
|
|
|
|
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);
|
|
|
|
free_data(train);
|
2016-08-06 01:27:07 +03:00
|
|
|
for(i = 0; i < nthreads; ++i){
|
|
|
|
free_data(trains[i]);
|
|
|
|
}
|
2015-11-04 06:23:17 +03:00
|
|
|
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);
|
|
|
|
}
|
2016-06-03 01:25:24 +03:00
|
|
|
if(get_current_batch(net)%100 == 0){
|
2015-12-08 04:18:04 +03:00
|
|
|
char buff[256];
|
|
|
|
sprintf(buff, "%s/%s.backup",backup_directory,base);
|
|
|
|
save_weights(net, buff);
|
|
|
|
}
|
2015-11-04 06:23:17 +03:00
|
|
|
}
|
|
|
|
char buff[256];
|
|
|
|
sprintf(buff, "%s/%s.weights", backup_directory, base);
|
|
|
|
save_weights(net, buff);
|
|
|
|
|
2016-08-06 01:27:07 +03:00
|
|
|
for(i = 0; i < nthreads; ++i){
|
|
|
|
pthread_join(load_threads[i], 0);
|
|
|
|
free_data(buffers[i]);
|
|
|
|
}
|
|
|
|
free(buffers);
|
|
|
|
free(trains);
|
|
|
|
free(load_threads);
|
|
|
|
|
2015-11-04 06:23:17 +03:00
|
|
|
free_network(net);
|
|
|
|
free_ptrs((void**)labels, classes);
|
|
|
|
free_ptrs((void**)paths, plist->size);
|
|
|
|
free_list(plist);
|
|
|
|
free(base);
|
|
|
|
}
|
|
|
|
|
2016-08-06 01:27:07 +03:00
|
|
|
void validate_classifier_crop(char *datacfg, char *filename, char *weightfile)
|
2015-11-04 06:23:17 +03:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
network net = parse_network_cfg(filename);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
list *options = read_data_cfg(datacfg);
|
|
|
|
|
|
|
|
char *label_list = option_find_str(options, "labels", "data/labels.list");
|
|
|
|
char *valid_list = option_find_str(options, "valid", "data/train.list");
|
|
|
|
int classes = option_find_int(options, "classes", 2);
|
2015-12-14 22:57:10 +03:00
|
|
|
int topk = option_find_int(options, "top", 1);
|
2015-11-04 06:23:17 +03:00
|
|
|
|
|
|
|
char **labels = get_labels(label_list);
|
|
|
|
list *plist = get_paths(valid_list);
|
|
|
|
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
int m = plist->size;
|
|
|
|
free_list(plist);
|
|
|
|
|
|
|
|
clock_t time;
|
|
|
|
float avg_acc = 0;
|
|
|
|
float avg_topk = 0;
|
2015-12-19 02:55:58 +03:00
|
|
|
int splits = m/1000;
|
2015-11-04 06:23:17 +03:00
|
|
|
int num = (i+1)*m/splits - i*m/splits;
|
|
|
|
|
|
|
|
data val, buffer;
|
|
|
|
|
|
|
|
load_args args = {0};
|
|
|
|
args.w = net.w;
|
|
|
|
args.h = net.h;
|
2016-03-01 00:54:12 +03:00
|
|
|
|
2015-11-04 06:23:17 +03:00
|
|
|
args.paths = paths;
|
|
|
|
args.classes = classes;
|
|
|
|
args.n = num;
|
|
|
|
args.m = 0;
|
|
|
|
args.labels = labels;
|
|
|
|
args.d = &buffer;
|
2016-03-01 00:54:12 +03:00
|
|
|
args.type = OLD_CLASSIFICATION_DATA;
|
2015-11-04 06:23:17 +03:00
|
|
|
|
|
|
|
pthread_t load_thread = load_data_in_thread(args);
|
|
|
|
for(i = 1; i <= splits; ++i){
|
|
|
|
time=clock();
|
|
|
|
|
|
|
|
pthread_join(load_thread, 0);
|
|
|
|
val = buffer;
|
|
|
|
|
|
|
|
num = (i+1)*m/splits - i*m/splits;
|
|
|
|
char **part = paths+(i*m/splits);
|
|
|
|
if(i != splits){
|
|
|
|
args.paths = part;
|
|
|
|
load_thread = load_data_in_thread(args);
|
|
|
|
}
|
|
|
|
printf("Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock()-time));
|
|
|
|
|
|
|
|
time=clock();
|
|
|
|
float *acc = network_accuracies(net, val, topk);
|
|
|
|
avg_acc += acc[0];
|
|
|
|
avg_topk += acc[1];
|
|
|
|
printf("%d: top 1: %f, top %d: %f, %lf seconds, %d images\n", i, avg_acc/i, topk, avg_topk/i, sec(clock()-time), val.X.rows);
|
|
|
|
free_data(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 02:40:14 +03:00
|
|
|
void validate_classifier_10(char *datacfg, char *filename, char *weightfile)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
network net = parse_network_cfg(filename);
|
|
|
|
set_batch_network(&net, 1);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
list *options = read_data_cfg(datacfg);
|
|
|
|
|
|
|
|
char *label_list = option_find_str(options, "labels", "data/labels.list");
|
|
|
|
char *valid_list = option_find_str(options, "valid", "data/train.list");
|
|
|
|
int classes = option_find_int(options, "classes", 2);
|
|
|
|
int topk = option_find_int(options, "top", 1);
|
|
|
|
|
|
|
|
char **labels = get_labels(label_list);
|
|
|
|
list *plist = get_paths(valid_list);
|
|
|
|
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
int m = plist->size;
|
|
|
|
free_list(plist);
|
|
|
|
|
|
|
|
float avg_acc = 0;
|
|
|
|
float avg_topk = 0;
|
|
|
|
int *indexes = calloc(topk, sizeof(int));
|
|
|
|
|
|
|
|
for(i = 0; i < m; ++i){
|
|
|
|
int class = -1;
|
|
|
|
char *path = paths[i];
|
|
|
|
for(j = 0; j < classes; ++j){
|
|
|
|
if(strstr(path, labels[j])){
|
|
|
|
class = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 00:54:12 +03:00
|
|
|
int w = net.w;
|
|
|
|
int h = net.h;
|
|
|
|
int shift = 32;
|
2016-03-14 09:18:42 +03:00
|
|
|
image im = load_image_color(paths[i], w+shift, h+shift);
|
2016-01-19 02:40:14 +03:00
|
|
|
image images[10];
|
2016-03-01 00:54:12 +03:00
|
|
|
images[0] = crop_image(im, -shift, -shift, w, h);
|
|
|
|
images[1] = crop_image(im, shift, -shift, w, h);
|
|
|
|
images[2] = crop_image(im, 0, 0, w, h);
|
|
|
|
images[3] = crop_image(im, -shift, shift, w, h);
|
|
|
|
images[4] = crop_image(im, shift, shift, w, h);
|
2016-01-19 02:40:14 +03:00
|
|
|
flip_image(im);
|
2016-03-01 00:54:12 +03:00
|
|
|
images[5] = crop_image(im, -shift, -shift, w, h);
|
|
|
|
images[6] = crop_image(im, shift, -shift, w, h);
|
|
|
|
images[7] = crop_image(im, 0, 0, w, h);
|
|
|
|
images[8] = crop_image(im, -shift, shift, w, h);
|
|
|
|
images[9] = crop_image(im, shift, shift, w, h);
|
2016-01-19 02:40:14 +03:00
|
|
|
float *pred = calloc(classes, sizeof(float));
|
|
|
|
for(j = 0; j < 10; ++j){
|
|
|
|
float *p = network_predict(net, images[j].data);
|
|
|
|
axpy_cpu(classes, 1, p, 1, pred, 1);
|
|
|
|
free_image(images[j]);
|
|
|
|
}
|
|
|
|
free_image(im);
|
|
|
|
top_k(pred, classes, topk, indexes);
|
|
|
|
free(pred);
|
|
|
|
if(indexes[0] == class) avg_acc += 1;
|
|
|
|
for(j = 0; j < topk; ++j){
|
|
|
|
if(indexes[j] == class) avg_topk += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 00:54:12 +03:00
|
|
|
void validate_classifier_full(char *datacfg, char *filename, char *weightfile)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
network net = parse_network_cfg(filename);
|
|
|
|
set_batch_network(&net, 1);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
list *options = read_data_cfg(datacfg);
|
|
|
|
|
|
|
|
char *label_list = option_find_str(options, "labels", "data/labels.list");
|
|
|
|
char *valid_list = option_find_str(options, "valid", "data/train.list");
|
|
|
|
int classes = option_find_int(options, "classes", 2);
|
|
|
|
int topk = option_find_int(options, "top", 1);
|
|
|
|
|
|
|
|
char **labels = get_labels(label_list);
|
|
|
|
list *plist = get_paths(valid_list);
|
|
|
|
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
int m = plist->size;
|
|
|
|
free_list(plist);
|
|
|
|
|
|
|
|
float avg_acc = 0;
|
|
|
|
float avg_topk = 0;
|
|
|
|
int *indexes = calloc(topk, sizeof(int));
|
|
|
|
|
2016-03-14 09:18:42 +03:00
|
|
|
int size = net.w;
|
2016-03-01 00:54:12 +03:00
|
|
|
for(i = 0; i < m; ++i){
|
|
|
|
int class = -1;
|
|
|
|
char *path = paths[i];
|
|
|
|
for(j = 0; j < classes; ++j){
|
|
|
|
if(strstr(path, labels[j])){
|
|
|
|
class = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
image im = load_image_color(paths[i], 0, 0);
|
2016-03-14 09:18:42 +03:00
|
|
|
image resized = resize_min(im, size);
|
|
|
|
resize_network(&net, resized.w, resized.h);
|
2016-03-01 00:54:12 +03:00
|
|
|
//show_image(im, "orig");
|
|
|
|
//show_image(crop, "cropped");
|
|
|
|
//cvWaitKey(0);
|
2016-03-14 09:18:42 +03:00
|
|
|
float *pred = network_predict(net, resized.data);
|
2016-03-01 00:54:12 +03:00
|
|
|
|
|
|
|
free_image(im);
|
2016-03-14 09:18:42 +03:00
|
|
|
free_image(resized);
|
2016-03-01 00:54:12 +03:00
|
|
|
top_k(pred, classes, topk, indexes);
|
|
|
|
|
|
|
|
if(indexes[0] == class) avg_acc += 1;
|
|
|
|
for(j = 0; j < topk; ++j){
|
|
|
|
if(indexes[j] == class) avg_topk += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void validate_classifier_single(char *datacfg, char *filename, char *weightfile)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
network net = parse_network_cfg(filename);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
2016-06-14 21:30:28 +03:00
|
|
|
set_batch_network(&net, 1);
|
2016-03-01 00:54:12 +03:00
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
list *options = read_data_cfg(datacfg);
|
|
|
|
|
|
|
|
char *label_list = option_find_str(options, "labels", "data/labels.list");
|
|
|
|
char *valid_list = option_find_str(options, "valid", "data/train.list");
|
|
|
|
int classes = option_find_int(options, "classes", 2);
|
|
|
|
int topk = option_find_int(options, "top", 1);
|
|
|
|
|
|
|
|
char **labels = get_labels(label_list);
|
|
|
|
list *plist = get_paths(valid_list);
|
|
|
|
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
int m = plist->size;
|
|
|
|
free_list(plist);
|
|
|
|
|
|
|
|
float avg_acc = 0;
|
|
|
|
float avg_topk = 0;
|
|
|
|
int *indexes = calloc(topk, sizeof(int));
|
|
|
|
|
|
|
|
for(i = 0; i < m; ++i){
|
|
|
|
int class = -1;
|
|
|
|
char *path = paths[i];
|
|
|
|
for(j = 0; j < classes; ++j){
|
|
|
|
if(strstr(path, labels[j])){
|
|
|
|
class = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
image im = load_image_color(paths[i], 0, 0);
|
|
|
|
image resized = resize_min(im, net.w);
|
|
|
|
image crop = crop_image(resized, (resized.w - net.w)/2, (resized.h - net.h)/2, net.w, net.h);
|
|
|
|
//show_image(im, "orig");
|
|
|
|
//show_image(crop, "cropped");
|
|
|
|
//cvWaitKey(0);
|
|
|
|
float *pred = network_predict(net, crop.data);
|
|
|
|
|
2016-06-03 01:25:24 +03:00
|
|
|
if(resized.data != im.data) free_image(resized);
|
2016-03-01 00:54:12 +03:00
|
|
|
free_image(im);
|
|
|
|
free_image(crop);
|
|
|
|
top_k(pred, classes, topk, indexes);
|
|
|
|
|
|
|
|
if(indexes[0] == class) avg_acc += 1;
|
|
|
|
for(j = 0; j < topk; ++j){
|
|
|
|
if(indexes[j] == class) avg_topk += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 02:40:14 +03:00
|
|
|
void validate_classifier_multi(char *datacfg, char *filename, char *weightfile)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
network net = parse_network_cfg(filename);
|
|
|
|
set_batch_network(&net, 1);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
list *options = read_data_cfg(datacfg);
|
|
|
|
|
|
|
|
char *label_list = option_find_str(options, "labels", "data/labels.list");
|
|
|
|
char *valid_list = option_find_str(options, "valid", "data/train.list");
|
|
|
|
int classes = option_find_int(options, "classes", 2);
|
|
|
|
int topk = option_find_int(options, "top", 1);
|
|
|
|
|
|
|
|
char **labels = get_labels(label_list);
|
|
|
|
list *plist = get_paths(valid_list);
|
2016-03-16 14:30:48 +03:00
|
|
|
int scales[] = {192, 224, 288, 320, 352};
|
2016-01-19 02:40:14 +03:00
|
|
|
int nscales = sizeof(scales)/sizeof(scales[0]);
|
|
|
|
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
int m = plist->size;
|
|
|
|
free_list(plist);
|
|
|
|
|
|
|
|
float avg_acc = 0;
|
|
|
|
float avg_topk = 0;
|
|
|
|
int *indexes = calloc(topk, sizeof(int));
|
|
|
|
|
|
|
|
for(i = 0; i < m; ++i){
|
|
|
|
int class = -1;
|
|
|
|
char *path = paths[i];
|
|
|
|
for(j = 0; j < classes; ++j){
|
|
|
|
if(strstr(path, labels[j])){
|
|
|
|
class = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
float *pred = calloc(classes, sizeof(float));
|
|
|
|
image im = load_image_color(paths[i], 0, 0);
|
|
|
|
for(j = 0; j < nscales; ++j){
|
2016-03-14 09:18:42 +03:00
|
|
|
image r = resize_min(im, scales[j]);
|
|
|
|
resize_network(&net, r.w, r.h);
|
2016-01-19 02:40:14 +03:00
|
|
|
float *p = network_predict(net, r.data);
|
|
|
|
axpy_cpu(classes, 1, p, 1, pred, 1);
|
|
|
|
flip_image(r);
|
|
|
|
p = network_predict(net, r.data);
|
|
|
|
axpy_cpu(classes, 1, p, 1, pred, 1);
|
2016-06-03 01:25:24 +03:00
|
|
|
if(r.data != im.data) free_image(r);
|
2016-01-19 02:40:14 +03:00
|
|
|
}
|
|
|
|
free_image(im);
|
|
|
|
top_k(pred, classes, topk, indexes);
|
|
|
|
free(pred);
|
|
|
|
if(indexes[0] == class) avg_acc += 1;
|
|
|
|
for(j = 0; j < topk; ++j){
|
|
|
|
if(indexes[j] == class) avg_topk += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-04 06:23:17 +03:00
|
|
|
void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename)
|
|
|
|
{
|
|
|
|
network net = parse_network_cfg(cfgfile);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
|
|
|
set_batch_network(&net, 1);
|
|
|
|
srand(2222222);
|
|
|
|
|
|
|
|
list *options = read_data_cfg(datacfg);
|
|
|
|
|
2015-12-14 22:57:10 +03:00
|
|
|
char *name_list = option_find_str(options, "names", 0);
|
|
|
|
if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list");
|
2015-11-04 06:23:17 +03:00
|
|
|
int top = option_find_int(options, "top", 1);
|
|
|
|
|
|
|
|
int i = 0;
|
2015-12-14 22:57:10 +03:00
|
|
|
char **names = get_labels(name_list);
|
2015-11-04 06:23:17 +03:00
|
|
|
clock_t time;
|
2015-12-19 02:55:58 +03:00
|
|
|
int *indexes = calloc(top, sizeof(int));
|
2015-11-04 06:23:17 +03:00
|
|
|
char buff[256];
|
|
|
|
char *input = buff;
|
2016-06-10 03:20:31 +03:00
|
|
|
int size = net.w;
|
2015-11-04 06:23:17 +03:00
|
|
|
while(1){
|
|
|
|
if(filename){
|
|
|
|
strncpy(input, filename, 256);
|
|
|
|
}else{
|
|
|
|
printf("Enter Image Path: ");
|
|
|
|
fflush(stdout);
|
|
|
|
input = fgets(input, 256, stdin);
|
|
|
|
if(!input) return;
|
|
|
|
strtok(input, "\n");
|
|
|
|
}
|
2016-06-10 03:20:31 +03:00
|
|
|
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 = r.data;
|
2015-11-04 06:23:17 +03:00
|
|
|
time=clock();
|
|
|
|
float *predictions = network_predict(net, X);
|
|
|
|
top_predictions(net, top, indexes);
|
|
|
|
printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
|
|
|
|
for(i = 0; i < top; ++i){
|
|
|
|
int index = indexes[i];
|
|
|
|
printf("%s: %f\n", names[index], predictions[index]);
|
|
|
|
}
|
2016-06-10 03:20:31 +03:00
|
|
|
if(r.data != im.data) free_image(r);
|
2015-11-04 06:23:17 +03:00
|
|
|
free_image(im);
|
|
|
|
if (filename) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 01:25:24 +03:00
|
|
|
|
|
|
|
void label_classifier(char *datacfg, char *filename, char *weightfile)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
network net = parse_network_cfg(filename);
|
|
|
|
set_batch_network(&net, 1);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
list *options = read_data_cfg(datacfg);
|
|
|
|
|
|
|
|
char *label_list = option_find_str(options, "names", "data/labels.list");
|
|
|
|
char *test_list = option_find_str(options, "test", "data/train.list");
|
|
|
|
int classes = option_find_int(options, "classes", 2);
|
|
|
|
|
|
|
|
char **labels = get_labels(label_list);
|
|
|
|
list *plist = get_paths(test_list);
|
|
|
|
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
int m = plist->size;
|
|
|
|
free_list(plist);
|
|
|
|
|
|
|
|
for(i = 0; i < m; ++i){
|
|
|
|
image im = load_image_color(paths[i], 0, 0);
|
|
|
|
image resized = resize_min(im, net.w);
|
|
|
|
image crop = crop_image(resized, (resized.w - net.w)/2, (resized.h - net.h)/2, net.w, net.h);
|
|
|
|
float *pred = network_predict(net, crop.data);
|
|
|
|
|
|
|
|
if(resized.data != im.data) free_image(resized);
|
|
|
|
free_image(im);
|
|
|
|
free_image(crop);
|
|
|
|
int ind = max_index(pred, classes);
|
|
|
|
|
|
|
|
printf("%s\n", labels[ind]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-19 02:55:58 +03:00
|
|
|
void test_classifier(char *datacfg, char *cfgfile, char *weightfile, int target_layer)
|
2015-11-04 06:23:17 +03:00
|
|
|
{
|
|
|
|
int curr = 0;
|
2015-12-19 02:55:58 +03:00
|
|
|
network net = parse_network_cfg(cfgfile);
|
2015-11-04 06:23:17 +03:00
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
list *options = read_data_cfg(datacfg);
|
|
|
|
|
|
|
|
char *test_list = option_find_str(options, "test", "data/test.list");
|
|
|
|
int classes = option_find_int(options, "classes", 2);
|
|
|
|
|
|
|
|
list *plist = get_paths(test_list);
|
|
|
|
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
int m = plist->size;
|
|
|
|
free_list(plist);
|
|
|
|
|
|
|
|
clock_t time;
|
|
|
|
|
|
|
|
data val, buffer;
|
|
|
|
|
|
|
|
load_args args = {0};
|
|
|
|
args.w = net.w;
|
|
|
|
args.h = net.h;
|
|
|
|
args.paths = paths;
|
|
|
|
args.classes = classes;
|
|
|
|
args.n = net.batch;
|
|
|
|
args.m = 0;
|
2015-12-19 02:55:58 +03:00
|
|
|
args.labels = 0;
|
2015-11-04 06:23:17 +03:00
|
|
|
args.d = &buffer;
|
2016-03-01 00:54:12 +03:00
|
|
|
args.type = OLD_CLASSIFICATION_DATA;
|
2015-11-04 06:23:17 +03:00
|
|
|
|
|
|
|
pthread_t load_thread = load_data_in_thread(args);
|
|
|
|
for(curr = net.batch; curr < m; curr += net.batch){
|
|
|
|
time=clock();
|
|
|
|
|
|
|
|
pthread_join(load_thread, 0);
|
|
|
|
val = buffer;
|
|
|
|
|
|
|
|
if(curr < m){
|
|
|
|
args.paths = paths + curr;
|
|
|
|
if (curr + net.batch > m) args.n = m - curr;
|
|
|
|
load_thread = load_data_in_thread(args);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock()-time));
|
|
|
|
|
|
|
|
time=clock();
|
|
|
|
matrix pred = network_predict_data(net, val);
|
2016-03-01 00:54:12 +03:00
|
|
|
|
2015-12-19 02:55:58 +03:00
|
|
|
int i, j;
|
2015-11-04 06:23:17 +03:00
|
|
|
if (target_layer >= 0){
|
2015-11-27 01:34:47 +03:00
|
|
|
//layer l = net.layers[target_layer];
|
2015-11-04 06:23:17 +03:00
|
|
|
}
|
|
|
|
|
2015-12-19 02:55:58 +03:00
|
|
|
for(i = 0; i < pred.rows; ++i){
|
|
|
|
printf("%s", paths[curr-net.batch+i]);
|
|
|
|
for(j = 0; j < pred.cols; ++j){
|
|
|
|
printf("\t%g", pred.vals[i][j]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2015-11-04 06:23:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
free_matrix(pred);
|
|
|
|
|
2016-01-19 02:40:14 +03:00
|
|
|
fprintf(stderr, "%lf seconds, %d images, %d total\n", sec(clock()-time), val.X.rows, curr);
|
2015-11-04 06:23:17 +03:00
|
|
|
free_data(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-14 09:18:42 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-04 06:23:17 +03:00
|
|
|
void run_classifier(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if(argc < 4){
|
|
|
|
fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-14 09:18:42 +03:00
|
|
|
int cam_index = find_int_arg(argc, argv, "-c", 0);
|
2016-06-03 01:25:24 +03:00
|
|
|
int clear = find_arg(argc, argv, "-clear");
|
2015-11-04 06:23:17 +03:00
|
|
|
char *data = argv[3];
|
|
|
|
char *cfg = argv[4];
|
|
|
|
char *weights = (argc > 5) ? argv[5] : 0;
|
|
|
|
char *filename = (argc > 6) ? argv[6]: 0;
|
|
|
|
char *layer_s = (argc > 7) ? argv[7]: 0;
|
|
|
|
int layer = layer_s ? atoi(layer_s) : -1;
|
|
|
|
if(0==strcmp(argv[2], "predict")) predict_classifier(data, cfg, weights, filename);
|
2016-06-03 01:25:24 +03:00
|
|
|
else if(0==strcmp(argv[2], "train")) train_classifier(data, cfg, weights, clear);
|
2016-03-14 09:18:42 +03:00
|
|
|
else if(0==strcmp(argv[2], "demo")) demo_classifier(data, cfg, weights, cam_index, filename);
|
2015-12-19 02:55:58 +03:00
|
|
|
else if(0==strcmp(argv[2], "test")) test_classifier(data, cfg, weights, layer);
|
2016-06-03 01:25:24 +03:00
|
|
|
else if(0==strcmp(argv[2], "label")) label_classifier(data, cfg, weights);
|
2016-08-06 01:27:07 +03:00
|
|
|
else if(0==strcmp(argv[2], "valid")) validate_classifier_single(data, cfg, weights);
|
2016-01-19 02:40:14 +03:00
|
|
|
else if(0==strcmp(argv[2], "validmulti")) validate_classifier_multi(data, cfg, weights);
|
2016-08-06 01:27:07 +03:00
|
|
|
else if(0==strcmp(argv[2], "valid10")) validate_classifier_10(data, cfg, weights);
|
|
|
|
else if(0==strcmp(argv[2], "validcrop")) validate_classifier_crop(data, cfg, weights);
|
2016-03-01 00:54:12 +03:00
|
|
|
else if(0==strcmp(argv[2], "validfull")) validate_classifier_full(data, cfg, weights);
|
2015-11-04 06:23:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|