2017-06-02 06:31:13 +03:00
|
|
|
#include "darknet.h"
|
2016-03-01 00:54:12 +03:00
|
|
|
|
2016-06-06 23:22:45 +03:00
|
|
|
void train_tag(char *cfgfile, char *weightfile, int clear)
|
2016-03-01 00:54:12 +03:00
|
|
|
{
|
|
|
|
srand(time(0));
|
|
|
|
float avg_loss = -1;
|
|
|
|
char *base = basecfg(cfgfile);
|
|
|
|
char *backup_directory = "/home/pjreddie/backup/";
|
|
|
|
printf("%s\n", base);
|
|
|
|
network net = parse_network_cfg(cfgfile);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
2016-06-06 23:22:45 +03:00
|
|
|
if(clear) *net.seen = 0;
|
2016-03-01 00:54:12 +03:00
|
|
|
printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
|
|
|
|
int imgs = 1024;
|
|
|
|
list *plist = get_paths("/home/pjreddie/tag/train.list");
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
printf("%d\n", plist->size);
|
|
|
|
int N = plist->size;
|
|
|
|
clock_t time;
|
|
|
|
pthread_t load_thread;
|
|
|
|
data train;
|
|
|
|
data buffer;
|
|
|
|
|
|
|
|
load_args args = {0};
|
|
|
|
args.w = net.w;
|
|
|
|
args.h = net.h;
|
|
|
|
|
|
|
|
args.min = net.w;
|
|
|
|
args.max = net.max_crop;
|
|
|
|
args.size = net.w;
|
|
|
|
|
|
|
|
args.paths = paths;
|
|
|
|
args.classes = net.outputs;
|
|
|
|
args.n = imgs;
|
|
|
|
args.m = N;
|
|
|
|
args.d = &buffer;
|
|
|
|
args.type = TAG_DATA;
|
|
|
|
|
2016-09-02 02:48:41 +03:00
|
|
|
args.angle = net.angle;
|
|
|
|
args.exposure = net.exposure;
|
|
|
|
args.saturation = net.saturation;
|
|
|
|
args.hue = net.hue;
|
|
|
|
|
2016-03-01 00:54:12 +03:00
|
|
|
fprintf(stderr, "%d classes\n", net.outputs);
|
|
|
|
|
|
|
|
load_thread = load_data_in_thread(args);
|
|
|
|
int epoch = (*net.seen)/N;
|
|
|
|
while(get_current_batch(net) < net.max_batches || net.max_batches == 0){
|
|
|
|
time=clock();
|
|
|
|
pthread_join(load_thread, 0);
|
|
|
|
train = buffer;
|
|
|
|
|
|
|
|
load_thread = load_data_in_thread(args);
|
|
|
|
printf("Loaded: %lf seconds\n", sec(clock()-time));
|
|
|
|
time=clock();
|
|
|
|
float loss = train_network(net, train);
|
|
|
|
if(avg_loss == -1) avg_loss = loss;
|
|
|
|
avg_loss = avg_loss*.9 + loss*.1;
|
2017-06-13 02:19:08 +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);
|
2016-03-01 00:54:12 +03:00
|
|
|
free_data(train);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
pthread_join(load_thread, 0);
|
|
|
|
free_data(buffer);
|
|
|
|
free_network(net);
|
|
|
|
free_ptrs((void**)paths, plist->size);
|
|
|
|
free_list(plist);
|
|
|
|
free(base);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_tag(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);
|
|
|
|
int i = 0;
|
|
|
|
char **names = get_labels("data/tags.txt");
|
|
|
|
clock_t time;
|
|
|
|
int indexes[10];
|
|
|
|
char buff[256];
|
|
|
|
char *input = buff;
|
2016-03-14 09:18:42 +03:00
|
|
|
int size = net.w;
|
2016-03-01 00:54:12 +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-03-14 09:18:42 +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);
|
2016-03-01 00:54:12 +03:00
|
|
|
|
2016-03-14 09:18:42 +03:00
|
|
|
float *X = r.data;
|
2016-03-01 00:54:12 +03:00
|
|
|
time=clock();
|
|
|
|
float *predictions = network_predict(net, X);
|
|
|
|
top_predictions(net, 10, indexes);
|
|
|
|
printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
|
|
|
|
for(i = 0; i < 10; ++i){
|
|
|
|
int index = indexes[i];
|
|
|
|
printf("%.1f%%: %s\n", predictions[index]*100, names[index]);
|
|
|
|
}
|
2016-06-10 03:20:31 +03:00
|
|
|
if(r.data != im.data) free_image(r);
|
2016-03-01 00:54:12 +03:00
|
|
|
free_image(im);
|
|
|
|
if (filename) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void run_tag(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-06-06 23:22:45 +03:00
|
|
|
int clear = find_arg(argc, argv, "-clear");
|
2016-03-01 00:54:12 +03:00
|
|
|
char *cfg = argv[3];
|
|
|
|
char *weights = (argc > 4) ? argv[4] : 0;
|
|
|
|
char *filename = (argc > 5) ? argv[5] : 0;
|
2016-06-06 23:22:45 +03:00
|
|
|
if(0==strcmp(argv[2], "train")) train_tag(cfg, weights, clear);
|
2016-03-01 00:54:12 +03:00
|
|
|
else if(0==strcmp(argv[2], "test")) test_tag(cfg, weights, filename);
|
|
|
|
}
|
|
|
|
|