Skip to content

Commit

Permalink
Another version of mosaic=1 for Detector
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyAB committed Dec 7, 2019
1 parent 4f52ba1 commit 87f36b7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 31 deletions.
92 changes: 61 additions & 31 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ void blend_truth(float *new_truth, int boxes, float *old_truth)
}


void blend_truth_mosaic(float *new_truth, int boxes, float *old_truth, int w, int h, int cut_x, int cut_y, int i_mixup)
void blend_truth_mosaic(float *new_truth, int boxes, float *old_truth, int w, int h, float cut_x, float cut_y, int i_mixup)
{
const int t_size = 4 + 1;
int count_new_truth = 0;
Expand All @@ -835,38 +835,64 @@ void blend_truth_mosaic(float *new_truth, int boxes, float *old_truth, int w, in
float wb = old_truth_ptr[2];
float hb = old_truth_ptr[3];

// shift 4 images
if (i_mixup == 0) {
xb = xb - (float)(w - cut_x) / w;
yb = yb - (float)(h - cut_y) / h;
}
if (i_mixup == 1) {
xb = xb + (float)cut_x / w;
yb = yb - (float)(h - cut_y) / h;
}
if (i_mixup == 2) {
xb = xb - (float)(w - cut_x) / w;
yb = yb + (float)cut_y / h;
}
if (i_mixup == 3) {
xb = xb + (float)cut_x / w;
yb = yb + (float)cut_y / h;
}

int left = (xb - wb / 2)*w;
int right = (xb + wb / 2)*w;
int top = (yb - hb / 2)*h;
int bot = (yb + hb / 2)*h;

if ((i_mixup == 0 && left < cut_x && top < cut_y) ||
(i_mixup == 1 && right > cut_x && top < cut_y) ||
(i_mixup == 2 && left < cut_x && bot > cut_y) ||
(i_mixup == 3 && right > cut_x && bot > cut_y))
{
if ((i_mixup == 0 || i_mixup == 2) && right > cut_x) {
float diff_x = (float)(right - cut_x) / w;
xb = xb - diff_x / 2;
wb = wb - diff_x;
}
if ((i_mixup == 1 || i_mixup == 3) && left < cut_x) {
float diff_x = (float)(cut_x - left) / w;
xb = xb + diff_x / 2;
wb = wb - diff_x;
}
// fix out of bound
if (left < 0) {
float diff = (float)left / w;
xb = xb - diff / 2;
wb = wb + diff;
}

if ((i_mixup == 0 || i_mixup == 1) && bot > cut_y) {
float diff_y = (float)(bot - cut_y) / h;
yb = yb - diff_y / 2;
hb = hb - diff_y;
}
if ((i_mixup == 2 || i_mixup == 3) && top < cut_y) {
float diff_y = (float)(cut_y - top) / h;
yb = yb + diff_y / 2;
hb = hb - diff_y;
}
if (right > w) {
float diff = (float)(right - w) / w;
xb = xb - diff / 2;
wb = wb - diff;
}

if (top < 0) {
float diff = (float)top / h;
yb = yb - diff / 2;
hb = hb + diff;
}

if (bot > h) {
float diff = (float)(bot - h) / h;
yb = yb - diff / 2;
hb = hb - diff;
}

left = (xb - wb / 2)*w;
right = (xb + wb / 2)*w;
top = (yb - hb / 2)*h;
bot = (yb + hb / 2)*h;

// leave only within the image
if(left >= 0 && right <= w && top >= 0 && bot <= h &&
wb > 0 && wb < 1 && hb > 0 && hb < 1 &&
xb > 0 && xb < 1 && yb > 0 && yb < 1)
{
new_truth_ptr[0] = xb;
new_truth_ptr[1] = yb;
new_truth_ptr[2] = wb;
Expand All @@ -889,7 +915,7 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int c, int bo
c = c ? c : 3;

assert(use_mixup != 2);
if (random_gen() % 2 == 0) use_mixup = 0;
//if (random_gen() % 2 == 0) use_mixup = 0;
int i;

int *cut_x = NULL, *cut_y = NULL;
Expand Down Expand Up @@ -1046,16 +1072,20 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int c, int bo
for (y = 0; y < h; ++y) {
int j = y*w + k*w*h;
if (i_mixup == 0 && y < cut_y[i]) {
memcpy(&d.X.vals[i][j + 0], &ai.data[j + 0], cut_x[i] * sizeof(float));
int j_src = (w - cut_x[i]) + (y + h - cut_y[i])*w + k*w*h;
memcpy(&d.X.vals[i][j + 0], &ai.data[j_src], cut_x[i] * sizeof(float));
}
if (i_mixup == 1 && y < cut_y[i]) {
memcpy(&d.X.vals[i][j + cut_x[i]], &ai.data[j + cut_x[i]], (w-cut_x[i]) * sizeof(float));
int j_src = (y + h - cut_y[i])*w + k*w*h;
memcpy(&d.X.vals[i][j + cut_x[i]], &ai.data[j_src], (w-cut_x[i]) * sizeof(float));
}
if (i_mixup == 2 && y >= cut_y[i]) {
memcpy(&d.X.vals[i][j + 0], &ai.data[j + 0], cut_x[i] * sizeof(float));
int j_src = (w - cut_x[i]) + (y - cut_y[i])*w + k*w*h;
memcpy(&d.X.vals[i][j + 0], &ai.data[j_src], cut_x[i] * sizeof(float));
}
if (i_mixup == 3 && y >= cut_y[i]) {
memcpy(&d.X.vals[i][j + cut_x[i]], &ai.data[j + cut_x[i]], (w - cut_x[i]) * sizeof(float));
int j_src = (y - cut_y[i])*w + k*w*h;
memcpy(&d.X.vals[i][j + cut_x[i]], &ai.data[j_src], (w - cut_x[i]) * sizeof(float));
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/yolo_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,11 @@ void forward_yolo_layer(const layer l, network_state state)
for (t = 0; t < l.max_boxes; ++t) {
box truth = float_to_box_stride(state.truth + t*(4 + 1) + b*l.truths, 1);
if (truth.x < 0 || truth.y < 0 || truth.x > 1 || truth.y > 1 || truth.w < 0 || truth.h < 0) {
char buff[256];
printf(" Wrong label: truth.x = %f, truth.y = %f, truth.w = %f, truth.h = %f \n", truth.x, truth.y, truth.w, truth.h);
sprintf(buff, "echo \"Wrong label: truth.x = %f, truth.y = %f, truth.w = %f, truth.h = %f\" >> bad_label.list",
truth.x, truth.y, truth.w, truth.h);
system(buff);
}
int class_id = state.truth[t*(4 + 1) + b*l.truths + 4];
if (class_id >= l.classes) continue; // if label contains class_id more than number of classes in the cfg-file
Expand Down

0 comments on commit 87f36b7

Please sign in to comment.