2017-06-02 06:31:13 +03:00
|
|
|
#include "darknet.h"
|
2017-03-27 09:42:30 +03:00
|
|
|
#include <sys/time.h>
|
2017-06-01 07:06:35 +03:00
|
|
|
#include <assert.h>
|
2017-03-27 09:42:30 +03:00
|
|
|
|
|
|
|
void train_regressor(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
float avg_loss = -1;
|
|
|
|
char *base = basecfg(cfgfile);
|
|
|
|
printf("%s\n", base);
|
|
|
|
printf("%d\n", ngpus);
|
2017-10-17 21:41:34 +03:00
|
|
|
network **nets = calloc(ngpus, sizeof(network*));
|
2017-03-27 09:42:30 +03:00
|
|
|
|
|
|
|
srand(time(0));
|
|
|
|
int seed = rand();
|
|
|
|
for(i = 0; i < ngpus; ++i){
|
|
|
|
srand(seed);
|
|
|
|
#ifdef GPU
|
|
|
|
cuda_set_device(gpus[i]);
|
|
|
|
#endif
|
2017-10-17 21:41:34 +03:00
|
|
|
nets[i] = load_network(cfgfile, weightfile, clear);
|
|
|
|
nets[i]->learning_rate *= ngpus;
|
2017-03-27 09:42:30 +03:00
|
|
|
}
|
|
|
|
srand(time(0));
|
2017-10-17 21:41:34 +03:00
|
|
|
network *net = nets[0];
|
2017-03-27 09:42:30 +03:00
|
|
|
|
2017-10-17 21:41:34 +03:00
|
|
|
int imgs = net->batch * net->subdivisions * ngpus;
|
2017-03-27 09:42:30 +03:00
|
|
|
|
2017-10-17 21:41:34 +03:00
|
|
|
printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net->learning_rate, net->momentum, net->decay);
|
2017-03-27 09:42:30 +03:00
|
|
|
list *options = read_data_cfg(datacfg);
|
|
|
|
|
|
|
|
char *backup_directory = option_find_str(options, "backup", "/backup/");
|
|
|
|
char *train_list = option_find_str(options, "train", "data/train.list");
|
2018-01-23 05:09:36 +03:00
|
|
|
int classes = option_find_int(options, "classes", 1);
|
2017-03-27 09:42:30 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
load_args args = {0};
|
2017-10-17 21:41:34 +03:00
|
|
|
args.w = net->w;
|
|
|
|
args.h = net->h;
|
2017-03-27 09:42:30 +03:00
|
|
|
args.threads = 32;
|
2018-01-23 05:09:36 +03:00
|
|
|
args.classes = classes;
|
2017-03-27 09:42:30 +03:00
|
|
|
|
2018-01-23 05:09:36 +03:00
|
|
|
args.min = net->min_ratio*net->w;
|
|
|
|
args.max = net->max_ratio*net->w;
|
2017-10-17 21:41:34 +03:00
|
|
|
args.angle = net->angle;
|
|
|
|
args.aspect = net->aspect;
|
|
|
|
args.exposure = net->exposure;
|
|
|
|
args.saturation = net->saturation;
|
|
|
|
args.hue = net->hue;
|
|
|
|
args.size = net->w;
|
2017-03-27 09:42:30 +03:00
|
|
|
|
|
|
|
args.paths = paths;
|
|
|
|
args.n = imgs;
|
|
|
|
args.m = N;
|
|
|
|
args.type = REGRESSION_DATA;
|
|
|
|
|
|
|
|
data train;
|
|
|
|
data buffer;
|
|
|
|
pthread_t load_thread;
|
|
|
|
args.d = &buffer;
|
|
|
|
load_thread = load_data(args);
|
|
|
|
|
2017-10-17 21:41:34 +03:00
|
|
|
int epoch = (*net->seen)/N;
|
|
|
|
while(get_current_batch(net) < net->max_batches || net->max_batches == 0){
|
2017-03-27 09:42:30 +03:00
|
|
|
time=clock();
|
|
|
|
|
|
|
|
pthread_join(load_thread, 0);
|
|
|
|
train = buffer;
|
|
|
|
load_thread = load_data(args);
|
|
|
|
|
|
|
|
printf("Loaded: %lf seconds\n", sec(clock()-time));
|
|
|
|
time=clock();
|
|
|
|
|
|
|
|
float loss = 0;
|
|
|
|
#ifdef GPU
|
|
|
|
if(ngpus == 1){
|
|
|
|
loss = train_network(net, train);
|
|
|
|
} else {
|
|
|
|
loss = train_networks(nets, ngpus, train, 4);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
loss = train_network(net, train);
|
|
|
|
#endif
|
|
|
|
if(avg_loss == -1) avg_loss = loss;
|
|
|
|
avg_loss = avg_loss*.9 + loss*.1;
|
2017-10-17 21:41:34 +03:00
|
|
|
printf("%ld, %.3f: %f, %f avg, %f rate, %lf seconds, %ld images\n", get_current_batch(net), (float)(*net->seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net->seen);
|
2017-03-27 09:42:30 +03:00
|
|
|
free_data(train);
|
2017-10-17 21:41:34 +03:00
|
|
|
if(*net->seen/N > epoch){
|
|
|
|
epoch = *net->seen/N;
|
2017-03-27 09:42:30 +03:00
|
|
|
char buff[256];
|
|
|
|
sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch);
|
|
|
|
save_weights(net, buff);
|
|
|
|
}
|
|
|
|
if(get_current_batch(net)%100 == 0){
|
|
|
|
char buff[256];
|
|
|
|
sprintf(buff, "%s/%s.backup",backup_directory,base);
|
|
|
|
save_weights(net, buff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
char buff[256];
|
|
|
|
sprintf(buff, "%s/%s.weights", backup_directory, base);
|
|
|
|
save_weights(net, buff);
|
|
|
|
|
|
|
|
free_network(net);
|
|
|
|
free_ptrs((void**)paths, plist->size);
|
|
|
|
free_list(plist);
|
|
|
|
free(base);
|
|
|
|
}
|
|
|
|
|
|
|
|
void predict_regressor(char *cfgfile, char *weightfile, char *filename)
|
|
|
|
{
|
2017-10-17 21:41:34 +03:00
|
|
|
network *net = load_network(cfgfile, weightfile, 0);
|
|
|
|
set_batch_network(net, 1);
|
2017-03-27 09:42:30 +03:00
|
|
|
srand(2222222);
|
|
|
|
|
|
|
|
clock_t time;
|
|
|
|
char buff[256];
|
|
|
|
char *input = buff;
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
image im = load_image_color(input, 0, 0);
|
2017-10-17 21:41:34 +03:00
|
|
|
image sized = letterbox_image(im, net->w, net->h);
|
2017-03-27 09:42:30 +03:00
|
|
|
|
|
|
|
float *X = sized.data;
|
|
|
|
time=clock();
|
|
|
|
float *predictions = network_predict(net, X);
|
|
|
|
printf("Predicted: %f\n", predictions[0]);
|
|
|
|
printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
|
|
|
|
free_image(im);
|
|
|
|
free_image(sized);
|
|
|
|
if (filename) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void demo_regressor(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
|
|
|
|
{
|
|
|
|
#ifdef OPENCV
|
|
|
|
printf("Regressor Demo\n");
|
2017-10-17 21:41:34 +03:00
|
|
|
network *net = load_network(cfgfile, weightfile, 0);
|
|
|
|
set_batch_network(net, 1);
|
2017-03-27 09:42:30 +03:00
|
|
|
|
|
|
|
srand(2222222);
|
|
|
|
CvCapture * cap;
|
|
|
|
|
|
|
|
if(filename){
|
|
|
|
cap = cvCaptureFromFile(filename);
|
|
|
|
}else{
|
|
|
|
cap = cvCaptureFromCAM(cam_index);
|
|
|
|
}
|
2018-01-23 05:09:36 +03:00
|
|
|
list *options = read_data_cfg(datacfg);
|
|
|
|
int classes = option_find_int(options, "classes", 1);
|
|
|
|
char *name_list = option_find_str(options, "names", 0);
|
|
|
|
char **names = get_labels(name_list);
|
2017-03-27 09:42:30 +03:00
|
|
|
|
|
|
|
if(!cap) error("Couldn't connect to webcam.\n");
|
|
|
|
cvNamedWindow("Regressor", CV_WINDOW_NORMAL);
|
|
|
|
cvResizeWindow("Regressor", 512, 512);
|
|
|
|
float fps = 0;
|
|
|
|
|
|
|
|
while(1){
|
|
|
|
struct timeval tval_before, tval_after, tval_result;
|
|
|
|
gettimeofday(&tval_before, NULL);
|
|
|
|
|
|
|
|
image in = get_image_from_stream(cap);
|
2018-01-23 05:09:36 +03:00
|
|
|
image crop = center_crop_image(in, net->w, net->h);
|
|
|
|
grayscale_image_3c(crop);
|
2017-03-27 09:42:30 +03:00
|
|
|
|
2018-01-23 05:09:36 +03:00
|
|
|
float *predictions = network_predict(net, crop.data);
|
2017-03-27 09:42:30 +03:00
|
|
|
|
|
|
|
printf("\033[2J");
|
|
|
|
printf("\033[1;1H");
|
|
|
|
printf("\nFPS:%.0f\n",fps);
|
|
|
|
|
2018-01-23 05:09:36 +03:00
|
|
|
int i;
|
|
|
|
for(i = 0; i < classes; ++i){
|
|
|
|
printf("%s: %f\n", names[i], predictions[i]);
|
|
|
|
}
|
2017-03-27 09:42:30 +03:00
|
|
|
|
2018-08-04 01:57:48 +03:00
|
|
|
show_image(crop, "Regressor", 10);
|
2017-03-27 09:42:30 +03:00
|
|
|
free_image(in);
|
2018-01-23 05:09:36 +03:00
|
|
|
free_image(crop);
|
2017-03-27 09:42:30 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void run_regressor(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if(argc < 4){
|
|
|
|
fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *gpu_list = find_char_arg(argc, argv, "-gpus", 0);
|
|
|
|
int *gpus = 0;
|
|
|
|
int gpu = 0;
|
|
|
|
int ngpus = 0;
|
|
|
|
if(gpu_list){
|
|
|
|
printf("%s\n", gpu_list);
|
|
|
|
int len = strlen(gpu_list);
|
|
|
|
ngpus = 1;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < len; ++i){
|
|
|
|
if (gpu_list[i] == ',') ++ngpus;
|
|
|
|
}
|
|
|
|
gpus = calloc(ngpus, sizeof(int));
|
|
|
|
for(i = 0; i < ngpus; ++i){
|
|
|
|
gpus[i] = atoi(gpu_list);
|
|
|
|
gpu_list = strchr(gpu_list, ',')+1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gpu = gpu_index;
|
|
|
|
gpus = &gpu;
|
|
|
|
ngpus = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cam_index = find_int_arg(argc, argv, "-c", 0);
|
|
|
|
int clear = find_arg(argc, argv, "-clear");
|
|
|
|
char *data = argv[3];
|
|
|
|
char *cfg = argv[4];
|
|
|
|
char *weights = (argc > 5) ? argv[5] : 0;
|
|
|
|
char *filename = (argc > 6) ? argv[6]: 0;
|
|
|
|
if(0==strcmp(argv[2], "test")) predict_regressor(data, cfg, weights);
|
|
|
|
else if(0==strcmp(argv[2], "train")) train_regressor(data, cfg, weights, gpus, ngpus, clear);
|
|
|
|
else if(0==strcmp(argv[2], "demo")) demo_regressor(data, cfg, weights, cam_index, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
|