9 for(i = 0; i < d->
y.
rows; ++i){
11 for(j = 0; j < n; ++j){
23 float *X = calloc(net->
batch*test.
X.
cols,
sizeof(
float));
24 float *y = calloc(net->
batch*test.
y.
cols,
sizeof(
float));
26 for(b = 0; b < net->
batch; ++b){
27 if(i+b == test.
X.
rows)
break;
41 for(b = 0; b < net->
batch; ++b){
42 if(i+b == test.
X.
rows)
break;
45 pred.
vals[i+b][0] = -err;
54 void train_attention(
char *datacfg,
char *cfgfile,
char *weightfile,
int *gpus,
int ngpus,
int clear)
58 float avg_cls_loss = -1;
59 float avg_att_loss = -1;
62 printf(
"%d\n", ngpus);
67 for(i = 0; i < ngpus; ++i){
83 char *backup_directory =
option_find_str(options,
"backup",
"/backup/");
84 char *label_list =
option_find_str(options,
"labels",
"data/labels.list");
85 char *train_list =
option_find_str(options,
"train",
"data/train.list");
91 printf(
"%d\n", plist->
size);
99 args.
w = divs*net->
w/size;
100 args.
h = divs*net->
h/size;
101 args.
size = divs*net->
w/size;
126 int epoch = (*net->
seen)/N;
130 pthread_join(load_thread, 0);
143 for (i = 0; i < divs*divs/ngpus; ++i) {
144 #pragma omp parallel for 145 for(j = 0; j < ngpus; ++j){
146 int index = i*ngpus + j;
149 for(z = 0; z < resized.
y.
rows; ++z){
155 int *inds = calloc(resized.
y.
rows,
sizeof(
int));
156 for(z = 0; z < resized.
y.
rows; ++z){
159 for(i = 0; i < divs*divs; ++i){
160 resized.
y.
vals[z][train.
y.
cols + i] = (i == index)? 1 : 0;
169 closs = train_networks(nets, ngpus, best, 4);
172 for (i = 0; i < divs*divs; ++i) {
173 printf(
"%.2f ", resized.
y.
vals[0][train.
y.
cols + i]);
174 if((i+1)%divs == 0) printf(
"\n");
192 aloss = train_networks(nets, ngpus, resized, 4);
195 for(i = 0; i < divs*divs; ++i){
196 printf(
"%f ", nets[0]->output[1000 + i]);
197 if ((i+1) % divs == 0) printf(
"\n");
203 if(avg_cls_loss == -1) avg_cls_loss = closs;
204 if(avg_att_loss == -1) avg_att_loss = aloss;
205 avg_cls_loss = avg_cls_loss*.9 + closs*.1;
206 avg_att_loss = avg_att_loss*.9 + aloss*.1;
208 printf(
"%ld, %.3f: Att: %f, %f avg, Class: %f, %f avg, %f rate, %lf seconds, %ld images\n",
get_current_batch(net), (
float)(*net->
seen)/N, aloss, avg_att_loss, closs, avg_cls_loss,
get_current_rate(net),
what_time_is_it_now()-time, *net->
seen);
209 if(*net->
seen/N > epoch){
210 epoch = *net->
seen/N;
212 sprintf(buff,
"%s/%s_%d.weights",backup_directory,base, epoch);
217 sprintf(buff,
"%s/%s.backup",backup_directory,base);
222 sprintf(buff,
"%s/%s.weights", backup_directory, base);
224 pthread_join(load_thread, 0);
242 char *label_list =
option_find_str(options,
"labels",
"data/labels.list");
245 char *valid_list =
option_find_str(options,
"valid",
"data/train.list");
258 int *indexes = calloc(topk,
sizeof(
int));
262 float *avgs = calloc(classes,
sizeof(
float));
263 int *inds = calloc(divs*divs,
sizeof(
int));
265 for(i = 0; i < m; ++i){
267 char *path = paths[i];
269 if(strstr(path, labels[j])){
276 image crop =
crop_image(resized, (resized.
w - net->
w*divs/size)/2, (resized.
h - net->
h*divs/size)/2, net->
w*divs/size, net->
h*divs/size);
283 for(j = 0; j < divs*divs; ++j){
284 printf(
"%.2f ", pred[classes + j]);
285 if((j+1)%divs == 0) printf(
"\n");
288 copy_cpu(classes, pred, 1, avgs, 1);
289 top_k(pred + classes, divs*divs, divs*divs, inds);
291 for(j = 0; j < extra; ++j){
293 int row = index / divs;
294 int col = index % divs;
295 int y = row * crop.
h / divs - (net->
h - crop.
h/divs)/2;
296 int x = col * crop.
w / divs - (net->
w - crop.
w/divs)/2;
297 printf(
"%d %d %d %d\n", row, col, y, x);
300 axpy_cpu(classes, 1., pred, 1, avgs, 1);
310 top_k(pred, classes, topk, indexes);
312 if(indexes[0] ==
class) avg_acc += 1;
313 for(j = 0; j < topk; ++j){
314 if(indexes[j] ==
class) avg_topk += 1;
317 printf(
"%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
330 char *label_list =
option_find_str(options,
"labels",
"data/labels.list");
331 char *valid_list =
option_find_str(options,
"valid",
"data/train.list");
337 int scales[] = {224, 288, 320, 352, 384};
338 int nscales =
sizeof(scales)/
sizeof(scales[0]);
346 int *indexes = calloc(topk,
sizeof(
int));
348 for(i = 0; i < m; ++i){
350 char *path = paths[i];
352 if(strstr(path, labels[j])){
357 float *pred = calloc(classes,
sizeof(
float));
359 for(j = 0; j < nscales; ++j){
364 axpy_cpu(classes, 1, p, 1, pred, 1);
367 axpy_cpu(classes, 1, p, 1, pred, 1);
371 top_k(pred, classes, topk, indexes);
373 if(indexes[0] ==
class) avg_acc += 1;
374 for(j = 0; j < topk; ++j){
375 if(indexes[j] ==
class) avg_topk += 1;
378 printf(
"%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
391 if(!name_list) name_list =
option_find_str(options,
"labels",
"data/labels.list");
397 int *indexes = calloc(top,
sizeof(
int));
402 strncpy(input, filename, 256);
404 printf(
"Enter Image Path: ");
406 input = fgets(input, 256, stdin);
420 fprintf(stderr,
"%s: Predicted in %f seconds.\n", input,
sec(clock()-time));
421 for(i = 0; i < top; ++i){
422 int index = indexes[i];
425 printf(
"%5.2f%%: %s\n", predictions[index]*100, names[index]);
437 fprintf(stderr,
"usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
447 int clear =
find_arg(argc, argv,
"-clear");
448 char *
data = argv[3];
450 char *weights = (argc > 5) ? argv[5] : 0;
451 char *filename = (argc > 6) ? argv[6]: 0;
452 char *layer_s = (argc > 7) ? argv[7]: 0;
453 if(0==strcmp(argv[2],
"predict"))
predict_attention(data, cfg, weights, filename, top);
454 else if(0==strcmp(argv[2],
"train"))
train_attention(data, cfg, weights, gpus, ngpus, clear);
void train_attention(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear)
data select_data(data *orig, int *inds)
data * tile_data(data orig, int divs, int size)
image resize_image(image im, int w, int h)
int find_arg(int argc, char *argv[], char *arg)
data resize_data(data orig, int w, int h)
void set_batch_network(network *net, int b)
int max_index(float *a, int n)
matrix network_loss_data(network *net, data test)
int show_image(image p, const char *name, int ms)
char * find_char_arg(int argc, char **argv, char *arg, char *def)
char * basecfg(char *cfgfile)
void change_leaves(tree *t, char *leaf_list)
void ** list_to_array(list *l)
char * option_find_str(list *l, char *key, char *def)
float train_network(network *net, data d)
void top_k(float *a, int n, int k, int *index)
void free_network(network *net)
image float_to_image(int w, int h, int c, float *data)
void save_weights(network *net, char *filename)
void run_attention(int argc, char **argv)
int resize_network(network *net, int w, int h)
image resize_min(image im, int min)
int * read_intlist(char *s, int *n, int d)
void forward_network(network *net)
void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
image load_image_color(char *filename, int w, int h)
float get_current_rate(network *net)
void extend_data_truth(data *d, int n, float val)
float sum_array(float *a, int n)
float sec(clock_t clocks)
int find_int_arg(int argc, char **argv, char *arg, int def)
network * load_network(char *cfg, char *weights, int clear)
char ** get_labels(char *filename)
image crop_image(image im, int dx, int dy, int w, int h)
void predict_attention(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top)
void free_matrix(matrix m)
void * load_thread(void *ptr)
void cuda_set_device(int n)
list * read_data_cfg(char *filename)
void copy_cpu(int N, float *X, int INCX, float *Y, int INCY)
size_t get_current_batch(network *net)
matrix make_matrix(int rows, int cols)
list * get_paths(char *filename)
pthread_t load_data(load_args args)
int option_find_int(list *l, char *key, int def)
void validate_attention_multi(char *datacfg, char *filename, char *weightfile)
void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves, int stride)
double what_time_is_it_now()
void validate_attention_single(char *datacfg, char *filename, char *weightfile)