2017-06-01 07:06:35 +03:00
|
|
|
#include "darknet/network.h"
|
|
|
|
#include "darknet/utils.h"
|
|
|
|
#include "darknet/parser.h"
|
2015-08-14 02:02:22 +03:00
|
|
|
|
|
|
|
char *dice_labels[] = {"face1","face2","face3","face4","face5","face6"};
|
|
|
|
|
|
|
|
void train_dice(char *cfgfile, char *weightfile)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
|
|
|
|
int imgs = 1024;
|
2015-09-05 03:52:44 +03:00
|
|
|
int i = *net.seen/imgs;
|
2015-08-14 02:02:22 +03:00
|
|
|
char **labels = dice_labels;
|
|
|
|
list *plist = get_paths("data/dice/dice.train.list");
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
printf("%d\n", plist->size);
|
|
|
|
clock_t time;
|
|
|
|
while(1){
|
|
|
|
++i;
|
|
|
|
time=clock();
|
2016-09-20 21:34:49 +03:00
|
|
|
data train = load_data_old(paths, imgs, plist->size, labels, 6, net.w, net.h);
|
2015-08-14 02:02:22 +03:00
|
|
|
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;
|
2015-09-05 03:52:44 +03:00
|
|
|
printf("%d: %f, %f avg, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), *net.seen);
|
2015-08-14 02:02:22 +03:00
|
|
|
free_data(train);
|
|
|
|
if((i % 100) == 0) net.learning_rate *= .1;
|
|
|
|
if(i%100==0){
|
|
|
|
char buff[256];
|
|
|
|
sprintf(buff, "%s/%s_%d.weights",backup_directory,base, i);
|
|
|
|
save_weights(net, buff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void validate_dice(char *filename, char *weightfile)
|
|
|
|
{
|
|
|
|
network net = parse_network_cfg(filename);
|
|
|
|
if(weightfile){
|
|
|
|
load_weights(&net, weightfile);
|
|
|
|
}
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
char **labels = dice_labels;
|
|
|
|
list *plist = get_paths("data/dice/dice.val.list");
|
|
|
|
|
|
|
|
char **paths = (char **)list_to_array(plist);
|
|
|
|
int m = plist->size;
|
|
|
|
free_list(plist);
|
|
|
|
|
2016-09-20 21:34:49 +03:00
|
|
|
data val = load_data_old(paths, m, 0, labels, 6, net.w, net.h);
|
2015-10-09 22:50:43 +03:00
|
|
|
float *acc = network_accuracies(net, val, 2);
|
2015-08-14 02:02:22 +03:00
|
|
|
printf("Validation Accuracy: %f, %d images\n", acc[0], m);
|
|
|
|
free_data(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_dice(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 = dice_labels;
|
2015-09-24 00:13:43 +03:00
|
|
|
char buff[256];
|
|
|
|
char *input = buff;
|
2015-08-14 02:02:22 +03:00
|
|
|
int indexes[6];
|
|
|
|
while(1){
|
|
|
|
if(filename){
|
|
|
|
strncpy(input, filename, 256);
|
|
|
|
}else{
|
|
|
|
printf("Enter Image Path: ");
|
|
|
|
fflush(stdout);
|
2015-09-24 00:13:43 +03:00
|
|
|
input = fgets(input, 256, stdin);
|
|
|
|
if(!input) return;
|
2015-08-14 02:02:22 +03:00
|
|
|
strtok(input, "\n");
|
|
|
|
}
|
|
|
|
image im = load_image_color(input, net.w, net.h);
|
|
|
|
float *X = im.data;
|
|
|
|
float *predictions = network_predict(net, X);
|
|
|
|
top_predictions(net, 6, indexes);
|
|
|
|
for(i = 0; i < 6; ++i){
|
|
|
|
int index = indexes[i];
|
|
|
|
printf("%s: %f\n", names[index], predictions[index]);
|
|
|
|
}
|
|
|
|
free_image(im);
|
|
|
|
if (filename) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_dice(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 *cfg = argv[3];
|
|
|
|
char *weights = (argc > 4) ? argv[4] : 0;
|
|
|
|
char *filename = (argc > 5) ? argv[5]: 0;
|
|
|
|
if(0==strcmp(argv[2], "test")) test_dice(cfg, weights, filename);
|
|
|
|
else if(0==strcmp(argv[2], "train")) train_dice(cfg, weights);
|
|
|
|
else if(0==strcmp(argv[2], "valid")) validate_dice(cfg, weights);
|
|
|
|
}
|
|
|
|
|