Changed class to class_id

pull/492/head
AlexeyAB 7 years ago
parent 17b22d7ce7
commit 35a66b73a2
  1. 8
      src/box.c
  2. 12
      src/cifar.c
  3. 32
      src/classifier.c
  4. 40
      src/compare.c
  5. 8
      src/data.c
  6. 6
      src/detector.c
  7. 22
      src/image.c
  8. 4
      src/matrix.c
  9. 40
      src/region_layer.c

@ -232,7 +232,7 @@ dbox diou(box a, box b)
typedef struct{
int index;
int class;
int class_id;
float **probs;
} sortable_bbox;
@ -240,7 +240,7 @@ int nms_comparator(const void *pa, const void *pb)
{
sortable_bbox a = *(sortable_bbox *)pa;
sortable_bbox b = *(sortable_bbox *)pb;
float diff = a.probs[a.index][b.class] - b.probs[b.index][b.class];
float diff = a.probs[a.index][b.class_id] - b.probs[b.index][b.class_id];
if(diff < 0) return 1;
else if(diff > 0) return -1;
return 0;
@ -253,13 +253,13 @@ void do_nms_sort(box *boxes, float **probs, int total, int classes, float thresh
for(i = 0; i < total; ++i){
s[i].index = i;
s[i].class = 0;
s[i].class_id = 0;
s[i].probs = probs;
}
for(k = 0; k < classes; ++k){
for(i = 0; i < total; ++i){
s[i].class = k;
s[i].class_id = k;
}
qsort(s, total, sizeof(sortable_bbox), nms_comparator);
for(i = 0; i < total; ++i){

@ -137,8 +137,8 @@ void test_cifar_multi(char *filename, char *weightfile)
axpy_cpu(10, 1, p, 1, pred, 1);
int index = max_index(pred, 10);
int class = max_index(test.y.vals[i], 10);
if(index == class) avg_acc += 1;
int class_id = max_index(test.y.vals[i], 10);
if(index == class_id) avg_acc += 1;
free_image(im);
printf("%4d: %.2f%%\n", i, 100.*avg_acc/(i+1));
}
@ -174,16 +174,16 @@ char *labels[] = {"airplane","automobile","bird","cat","deer","dog","frog","hors
data test = load_cifar10_data("data/cifar/cifar-10-batches-bin/test_batch.bin");
for(i = 0; i < train.X.rows; ++i){
image im = float_to_image(32, 32, 3, train.X.vals[i]);
int class = max_index(train.y.vals[i], 10);
int class_id = max_index(train.y.vals[i], 10);
char buff[256];
sprintf(buff, "data/cifar/train/%d_%s",i,labels[class]);
sprintf(buff, "data/cifar/train/%d_%s",i,labels[class_id]);
save_image_png(im, buff);
}
for(i = 0; i < test.X.rows; ++i){
image im = float_to_image(32, 32, 3, test.X.vals[i]);
int class = max_index(test.y.vals[i], 10);
int class_id = max_index(test.y.vals[i], 10);
char buff[256];
sprintf(buff, "data/cifar/test/%d_%s",i,labels[class]);
sprintf(buff, "data/cifar/test/%d_%s",i,labels[class_id]);
save_image_png(im, buff);
}
}

@ -362,11 +362,11 @@ void validate_classifier_10(char *datacfg, char *filename, char *weightfile)
int *indexes = calloc(topk, sizeof(int));
for(i = 0; i < m; ++i){
int class = -1;
int class_id = -1;
char *path = paths[i];
for(j = 0; j < classes; ++j){
if(strstr(path, labels[j])){
class = j;
class_id = j;
break;
}
}
@ -396,9 +396,9 @@ void validate_classifier_10(char *datacfg, char *filename, char *weightfile)
free_image(im);
top_k(pred, classes, topk, indexes);
free(pred);
if(indexes[0] == class) avg_acc += 1;
if(indexes[0] == class_id) avg_acc += 1;
for(j = 0; j < topk; ++j){
if(indexes[j] == class) avg_topk += 1;
if(indexes[j] == class_id) avg_topk += 1;
}
printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
@ -435,11 +435,11 @@ void validate_classifier_full(char *datacfg, char *filename, char *weightfile)
int size = net.w;
for(i = 0; i < m; ++i){
int class = -1;
int class_id = -1;
char *path = paths[i];
for(j = 0; j < classes; ++j){
if(strstr(path, labels[j])){
class = j;
class_id = j;
break;
}
}
@ -456,9 +456,9 @@ void validate_classifier_full(char *datacfg, char *filename, char *weightfile)
free_image(resized);
top_k(pred, classes, topk, indexes);
if(indexes[0] == class) avg_acc += 1;
if(indexes[0] == class_id) avg_acc += 1;
for(j = 0; j < topk; ++j){
if(indexes[j] == class) avg_topk += 1;
if(indexes[j] == class_id) avg_topk += 1;
}
printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
@ -497,11 +497,11 @@ void validate_classifier_single(char *datacfg, char *filename, char *weightfile)
int *indexes = calloc(topk, sizeof(int));
for(i = 0; i < m; ++i){
int class = -1;
int class_id = -1;
char *path = paths[i];
for(j = 0; j < classes; ++j){
if(strstr(path, labels[j])){
class = j;
class_id = j;
break;
}
}
@ -519,9 +519,9 @@ void validate_classifier_single(char *datacfg, char *filename, char *weightfile)
free_image(crop);
top_k(pred, classes, topk, indexes);
if(indexes[0] == class) avg_acc += 1;
if(indexes[0] == class_id) avg_acc += 1;
for(j = 0; j < topk; ++j){
if(indexes[j] == class) avg_topk += 1;
if(indexes[j] == class_id) avg_topk += 1;
}
printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
@ -559,11 +559,11 @@ void validate_classifier_multi(char *datacfg, char *filename, char *weightfile)
int *indexes = calloc(topk, sizeof(int));
for(i = 0; i < m; ++i){
int class = -1;
int class_id = -1;
char *path = paths[i];
for(j = 0; j < classes; ++j){
if(strstr(path, labels[j])){
class = j;
class_id = j;
break;
}
}
@ -583,9 +583,9 @@ void validate_classifier_multi(char *datacfg, char *filename, char *weightfile)
free_image(im);
top_k(pred, classes, topk, indexes);
free(pred);
if(indexes[0] == class) avg_acc += 1;
if(indexes[0] == class_id) avg_acc += 1;
for(j = 0; j < topk; ++j){
if(indexes[j] == class) avg_topk += 1;
if(indexes[j] == class_id) avg_topk += 1;
}
printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));

@ -148,21 +148,21 @@ void validate_compare(char *filename, char *weightfile)
typedef struct {
network net;
char *filename;
int class;
int class_id;
int classes;
float elo;
float *elos;
} sortable_bbox;
int total_compares = 0;
int current_class = 0;
int current_class_id = 0;
int elo_comparator(const void*a, const void *b)
{
sortable_bbox box1 = *(sortable_bbox*)a;
sortable_bbox box2 = *(sortable_bbox*)b;
if(box1.elos[current_class] == box2.elos[current_class]) return 0;
if(box1.elos[current_class] > box2.elos[current_class]) return -1;
if(box1.elos[current_class_id] == box2.elos[current_class_id]) return 0;
if(box1.elos[current_class_id] > box2.elos[current_class_id]) return -1;
return 1;
}
@ -172,7 +172,7 @@ int bbox_comparator(const void *a, const void *b)
sortable_bbox box1 = *(sortable_bbox*)a;
sortable_bbox box2 = *(sortable_bbox*)b;
network net = box1.net;
int class = box1.class;
int class_id = box1.class_id;
image im1 = load_image_color(box1.filename, net.w, net.h);
image im2 = load_image_color(box2.filename, net.w, net.h);
@ -184,24 +184,24 @@ int bbox_comparator(const void *a, const void *b)
free_image(im1);
free_image(im2);
free(X);
if (predictions[class*2] > predictions[class*2+1]){
if (predictions[class_id*2] > predictions[class_id*2+1]){
return 1;
}
return -1;
}
void bbox_update(sortable_bbox *a, sortable_bbox *b, int class, int result)
void bbox_update(sortable_bbox *a, sortable_bbox *b, int class_id, int result)
{
int k = 32;
float EA = 1./(1+pow(10, (b->elos[class] - a->elos[class])/400.));
float EB = 1./(1+pow(10, (a->elos[class] - b->elos[class])/400.));
float EA = 1./(1+pow(10, (b->elos[class_id] - a->elos[class_id])/400.));
float EB = 1./(1+pow(10, (a->elos[class_id] - b->elos[class_id])/400.));
float SA = result ? 1 : 0;
float SB = result ? 0 : 1;
a->elos[class] += k*(SA - EA);
b->elos[class] += k*(SB - EB);
a->elos[class_id] += k*(SA - EA);
b->elos[class_id] += k*(SB - EB);
}
void bbox_fight(network net, sortable_bbox *a, sortable_bbox *b, int classes, int class)
void bbox_fight(network net, sortable_bbox *a, sortable_bbox *b, int classes, int class_id)
{
image im1 = load_image_color(a->filename, net.w, net.h);
image im2 = load_image_color(b->filename, net.w, net.h);
@ -213,7 +213,7 @@ void bbox_fight(network net, sortable_bbox *a, sortable_bbox *b, int classes, in
int i;
for(i = 0; i < classes; ++i){
if(class < 0 || class == i){
if(class_id < 0 || class_id == i){
int result = predictions[i*2] > predictions[i*2+1];
bbox_update(a, b, i, result);
}
@ -244,7 +244,7 @@ void SortMaster3000(char *filename, char *weightfile)
for(i = 0; i < N; ++i){
boxes[i].filename = paths[i];
boxes[i].net = net;
boxes[i].class = 7;
boxes[i].class_id = 7;
boxes[i].elo = 1500;
}
clock_t time=clock();
@ -297,12 +297,12 @@ void BattleRoyaleWithCheese(char *filename, char *weightfile)
printf("Round: %f secs, %d remaining\n", sec(clock()-round_time), N);
}
int class;
int class_id;
for (class = 0; class < classes; ++class){
for (class_id = 0; class_id < classes; ++class_id){
N = total;
current_class = class;
current_class_id = class_id;
qsort(boxes, N, sizeof(sortable_bbox), elo_comparator);
N /= 2;
@ -312,7 +312,7 @@ void BattleRoyaleWithCheese(char *filename, char *weightfile)
sorta_shuffle(boxes, N, sizeof(sortable_bbox), 10);
for(i = 0; i < N/2; ++i){
bbox_fight(net, boxes+i*2, boxes+i*2+1, classes, class);
bbox_fight(net, boxes+i*2, boxes+i*2+1, classes, class_id);
}
qsort(boxes, N, sizeof(sortable_bbox), elo_comparator);
if(round <= 20) N = (N*9/10)/2*2;
@ -320,10 +320,10 @@ void BattleRoyaleWithCheese(char *filename, char *weightfile)
printf("Round: %f secs, %d remaining\n", sec(clock()-round_time), N);
}
char buff[256];
sprintf(buff, "results/battle_%d.log", class);
sprintf(buff, "results/battle_%d.log", class_id);
FILE *outfp = fopen(buff, "w");
for(i = 0; i < N; ++i){
fprintf(outfp, "%s %f\n", boxes[i].filename, boxes[i].elos[class]);
fprintf(outfp, "%s %f\n", boxes[i].filename, boxes[i].elos[class_id]);
}
fclose(outfp);
}

@ -967,8 +967,8 @@ data load_cifar10_data(char *filename)
for(i = 0; i < 10000; ++i){
unsigned char bytes[3073];
fread(bytes, 1, 3073, fp);
int class = bytes[0];
y.vals[i][class] = 1;
int class_id = bytes[0];
y.vals[i][class_id] = 1;
for(j = 0; j < X.cols; ++j){
X.vals[i][j] = (double)bytes[j+1];
}
@ -1031,8 +1031,8 @@ data load_all_cifar10()
for(i = 0; i < 10000; ++i){
unsigned char bytes[3073];
fread(bytes, 1, 3073, fp);
int class = bytes[0];
y.vals[i+b*10000][class] = 1;
int class_id = bytes[0];
y.vals[i+b*10000][class_id] = 1;
for(j = 0; j < X.cols; ++j){
X.vals[i+b*10000][j] = (double)bytes[j+1];
}

@ -245,8 +245,8 @@ void print_imagenet_detections(FILE *fp, int id, box *boxes, float **probs, int
if (ymax > h) ymax = h;
for(j = 0; j < classes; ++j){
int class = j;
if (probs[i][class]) fprintf(fp, "%d %d %f %f %f %f %f\n", id, j+1, probs[i][class],
int class_id = j;
if (probs[i][class_id]) fprintf(fp, "%d %d %f %f %f %f %f\n", id, j+1, probs[i][class_id],
xmin, ymin, xmax, ymax);
}
}
@ -777,7 +777,7 @@ void validate_detector_map(char *datacfg, char *cfgfile, char *weightfile)
avg_precision += cur_precision;
}
avg_precision = avg_precision / 11;
printf("class = %d, name = %s, \t ap = %2.2f %% \n", i, names[i], avg_precision*100);
printf("class_id = %d, name = %s, \t ap = %2.2f %% \n", i, names[i], avg_precision*100);
mean_average_precision += avg_precision;
}

@ -188,8 +188,8 @@ void draw_detections(image im, int num, float thresh, box *boxes, float **probs,
int i;
for(i = 0; i < num; ++i){
int class = max_index(probs[i], classes);
float prob = probs[i][class];
int class_id = max_index(probs[i], classes);
float prob = probs[i][class_id];
if(prob > thresh){
//// for comparison with OpenCV version of DNN Darknet Yolo v2
@ -207,8 +207,8 @@ void draw_detections(image im, int num, float thresh, box *boxes, float **probs,
alphabet = 0;
}
printf("%s: %.0f%%\n", names[class], prob*100);
int offset = class*123457 % classes;
printf("%s: %.0f%%\n", names[class_id], prob*100);
int offset = class_id*123457 % classes;
float red = get_color(2,offset,classes);
float green = get_color(1,offset,classes);
float blue = get_color(0,offset,classes);
@ -233,7 +233,7 @@ void draw_detections(image im, int num, float thresh, box *boxes, float **probs,
draw_box_width(im, left, top, right, bot, width, red, green, blue);
if (alphabet) {
image label = get_label(alphabet, names[class], (im.h*.03)/10);
image label = get_label(alphabet, names[class_id], (im.h*.03)/10);
draw_label(im, top + width, left, label, rgb);
}
}
@ -246,8 +246,8 @@ void draw_detections_cv(IplImage* show_img, int num, float thresh, box *boxes, f
int i;
for (i = 0; i < num; ++i) {
int class = max_index(probs[i], classes);
float prob = probs[i][class];
int class_id = max_index(probs[i], classes);
float prob = probs[i][class_id];
if (prob > thresh) {
int width = show_img->height * .012;
@ -257,8 +257,8 @@ void draw_detections_cv(IplImage* show_img, int num, float thresh, box *boxes, f
alphabet = 0;
}
printf("%s: %.0f%%\n", names[class], prob * 100);
int offset = class * 123457 % classes;
printf("%s: %.0f%%\n", names[class_id], prob * 100);
int offset = class_id * 123457 % classes;
float red = get_color(2, offset, classes);
float green = get_color(1, offset, classes);
float blue = get_color(0, offset, classes);
@ -299,14 +299,14 @@ void draw_detections_cv(IplImage* show_img, int num, float thresh, box *boxes, f
color.val[2] = blue * 256;
cvRectangle(show_img, pt1, pt2, color, width, 8, 0);
//printf("left=%d, right=%d, top=%d, bottom=%d, obj_id=%d, obj=%s \n", left, right, top, bot, class, names[class]);
//printf("left=%d, right=%d, top=%d, bottom=%d, obj_id=%d, obj=%s \n", left, right, top, bot, class_id, names[class_id]);
cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, width, 8, 0);
cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, CV_FILLED, 8, 0); // filled
CvScalar black_color;
black_color.val[0] = 0;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, font_size, font_size, 0, font_size * 3, 8);
cvPutText(show_img, names[class], pt_text, &font, black_color);
cvPutText(show_img, names[class_id], pt_text, &font, black_color);
}
}
}

@ -22,8 +22,8 @@ float matrix_topk_accuracy(matrix truth, matrix guess, int k)
for(i = 0; i < truth.rows; ++i){
top_k(guess.vals[i], n, k, indexes);
for(j = 0; j < k; ++j){
int class = indexes[j];
if(truth.vals[i][class]){
int class_id = indexes[j];
if(truth.vals[i][class_id]){
++correct;
break;
}

@ -110,27 +110,27 @@ float delta_region_box(box truth, float *x, float *biases, int n, int index, int
return iou;
}
void delta_region_class(float *output, float *delta, int index, int class, int classes, tree *hier, float scale, float *avg_cat)
void delta_region_class(float *output, float *delta, int index, int class_id, int classes, tree *hier, float scale, float *avg_cat)
{
int i, n;
if(hier){
float pred = 1;
while(class >= 0){
pred *= output[index + class];
int g = hier->group[class];
while(class_id >= 0){
pred *= output[index + class_id];
int g = hier->group[class_id];
int offset = hier->group_offset[g];
for(i = 0; i < hier->group_size[g]; ++i){
delta[index + offset + i] = scale * (0 - output[index + offset + i]);
}
delta[index + class] = scale * (1 - output[index + class]);
delta[index + class_id] = scale * (1 - output[index + class_id]);
class = hier->parent[class];
class_id = hier->parent[class_id];
}
*avg_cat += pred;
} else {
for(n = 0; n < classes; ++n){
delta[index + n] = scale * (((n == class)?1 : 0) - output[index + n]);
if(n == class) *avg_cat += output[index + n];
delta[index + n] = scale * (((n == class_id)?1 : 0) - output[index + n]);
if(n == class_id) *avg_cat += output[index + n];
}
}
}
@ -191,31 +191,31 @@ void forward_region_layer(const region_layer l, network_state state)
*(l.cost) = 0;
for (b = 0; b < l.batch; ++b) {
if(l.softmax_tree){
int onlyclass = 0;
int onlyclass_id = 0;
for(t = 0; t < l.max_boxes; ++t){
box truth = float_to_box(state.truth + t*5 + b*l.truths);
if(!truth.x) break;
int class = state.truth[t*5 + b*l.truths + 4];
int class_id = state.truth[t*5 + b*l.truths + 4];
float maxp = 0;
int maxi = 0;
if(truth.x > 100000 && truth.y > 100000){
for(n = 0; n < l.n*l.w*l.h; ++n){
int index = size*n + b*l.outputs + 5;
float scale = l.output[index-1];
float p = scale*get_hierarchy_probability(l.output + index, l.softmax_tree, class);
float p = scale*get_hierarchy_probability(l.output + index, l.softmax_tree, class_id);
if(p > maxp){
maxp = p;
maxi = n;
}
}
int index = size*maxi + b*l.outputs + 5;
delta_region_class(l.output, l.delta, index, class, l.classes, l.softmax_tree, l.class_scale, &avg_cat);
delta_region_class(l.output, l.delta, index, class_id, l.classes, l.softmax_tree, l.class_scale, &avg_cat);
++class_count;
onlyclass = 1;
onlyclass_id = 1;
break;
}
}
if(onlyclass) continue;
if(onlyclass_id) continue;
}
for (j = 0; j < l.h; ++j) {
for (i = 0; i < l.w; ++i) {
@ -223,13 +223,13 @@ void forward_region_layer(const region_layer l, network_state state)
int index = size*(j*l.w*l.n + i*l.n + n) + b*l.outputs;
box pred = get_region_box(l.output, l.biases, n, index, i, j, l.w, l.h);
float best_iou = 0;
int best_class = -1;
int best_class_id = -1;
for(t = 0; t < l.max_boxes; ++t){
box truth = float_to_box(state.truth + t*5 + b*l.truths);
if(!truth.x) break;
float iou = box_iou(pred, truth);
if (iou > best_iou) {
best_class = state.truth[t*5 + b*l.truths + 4];
best_class_id = state.truth[t*5 + b*l.truths + 4];
best_iou = iou;
}
}
@ -240,7 +240,7 @@ void forward_region_layer(const region_layer l, network_state state)
if (best_iou > l.thresh) {
l.delta[index + 4] = 0;
if(l.classfix > 0){
delta_region_class(l.output, l.delta, index + 5, best_class, l.classes, l.softmax_tree, l.class_scale*(l.classfix == 2 ? l.output[index + 4] : 1), &avg_cat);
delta_region_class(l.output, l.delta, index + 5, best_class_id, l.classes, l.softmax_tree, l.class_scale*(l.classfix == 2 ? l.output[index + 4] : 1), &avg_cat);
++class_count;
}
}
@ -310,9 +310,9 @@ void forward_region_layer(const region_layer l, network_state state)
}
int class = state.truth[t*5 + b*l.truths + 4];
if (l.map) class = l.map[class];
delta_region_class(l.output, l.delta, best_index + 5, class, l.classes, l.softmax_tree, l.class_scale, &avg_cat);
int class_id = state.truth[t*5 + b*l.truths + 4];
if (l.map) class_id = l.map[class_id];
delta_region_class(l.output, l.delta, best_index + 5, class_id, l.classes, l.softmax_tree, l.class_scale, &avg_cat);
++count;
++class_count;
}

Loading…
Cancel
Save