Skip to content

Commit

Permalink
changes before merging
Browse files Browse the repository at this point in the history
  • Loading branch information
cheeyos committed Jan 29, 2015
1 parent 0677605 commit 93792f4
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 218 deletions.
106 changes: 50 additions & 56 deletions examples/filter_visualization_car_deeppy.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion models/brody/solver_normalization.prototxt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ max_iter: 1450000
momentum: 0.9
weight_decay: 0.0005
snapshot: 1000
snapshot_prefix: "models/brody/driving_normalization"
snapshot_prefix: "models/brody/driving_normalization_nl"
solver_mode: GPU
18 changes: 9 additions & 9 deletions models/brody/train_val_driving_normalization.prototxt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ layers {
blobs_lr: 1
blobs_lr: 1
weight_decay: 1
weight_decay: 0
weight_decay: 1
convolution_param {
num_output: 96
kernel_size: 11
Expand Down Expand Up @@ -171,7 +171,7 @@ layers {
blobs_lr: 1
blobs_lr: 1
weight_decay: 1
weight_decay: 0
weight_decay: 1
convolution_param {
num_output: 256
pad: 2
Expand Down Expand Up @@ -223,7 +223,7 @@ layers {
blobs_lr: 1
blobs_lr: 1
weight_decay: 1
weight_decay: 0
weight_decay: 1
convolution_param {
num_output: 384
pad: 1
Expand Down Expand Up @@ -252,7 +252,7 @@ layers {
blobs_lr: 1
blobs_lr: 1
weight_decay: 1
weight_decay: 0
weight_decay: 1
convolution_param {
num_output: 384
pad: 1
Expand Down Expand Up @@ -282,7 +282,7 @@ layers {
blobs_lr: 1
blobs_lr: 1
weight_decay: 1
weight_decay: 0
weight_decay: 1
convolution_param {
num_output: 384
pad: 1
Expand Down Expand Up @@ -324,7 +324,7 @@ layers {
blobs_lr: 1
blobs_lr: 1
weight_decay: 1
weight_decay: 0
weight_decay: 1
convolution_param {
num_output: 4096
kernel_size: 6
Expand Down Expand Up @@ -362,7 +362,7 @@ layers {
blobs_lr: 1
blobs_lr: 1
weight_decay: 1
weight_decay: 0
weight_decay: 1
convolution_param {
num_output: 4096
kernel_size: 1
Expand Down Expand Up @@ -400,7 +400,7 @@ layers {
blobs_lr: 1
blobs_lr: 1
weight_decay: 1
weight_decay: 0
weight_decay: 1
convolution_param {
num_output: 256
kernel_size: 1
Expand All @@ -423,7 +423,7 @@ layers {
blobs_lr: 1
blobs_lr: 1
weight_decay: 1
weight_decay: 0
weight_decay: 1
convolution_param {
num_output: 128
kernel_size: 1
Expand Down
188 changes: 36 additions & 152 deletions src/caffe/layers/driving_data_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,14 @@ unsigned int DrivingDataLayer<Dtype>::Rand() {
}

template <typename Dtype>
bool DrivingDataLayer<Dtype>::ReadBoundingBoxLabelToDatumLegacy(
bool DrivingDataLayer<Dtype>::ReadBoundingBoxLabelToDatum(
const DrivingData& data, Datum* datum, const int h_off, const int w_off) {
const int grid_dim = data.car_label_resolution();
const int width = data.car_label_width();
const int height = data.car_label_height();
const int full_label_width = width * grid_dim;
const int full_label_height = height * grid_dim;
const float half_shrink_factor = data.car_shrink_factor() / 2;
const float scaling = static_cast<float>(full_label_width) / data.car_cropped_width();

// 1 pixel label, 4 bounding box coordinates, 3 normalization labels.
Expand All @@ -285,177 +286,60 @@ bool DrivingDataLayer<Dtype>::ReadBoundingBoxLabelToDatumLegacy(
int ymin = data.car_boxes(i).ymin();
int xmax = data.car_boxes(i).xmax();
int ymax = data.car_boxes(i).ymax();
xmin = std::min(std::max(0, xmin - w_off), data.car_cropped_width() - 1);
xmax = std::min(std::max(0, xmax - w_off), data.car_cropped_width() - 1);
ymin = std::min(std::max(0, ymin - h_off), data.car_cropped_height() - 1);
ymax = std::min(std::max(0, ymax - h_off), data.car_cropped_height() - 1);
xmin = std::min(std::max(0, xmin - w_off), data.car_cropped_width());
xmax = std::min(std::max(0, xmax - w_off), data.car_cropped_width());
ymin = std::min(std::max(0, ymin - h_off), data.car_cropped_height());
ymax = std::min(std::max(0, ymax - h_off), data.car_cropped_height());
float w = xmax - xmin;
float h = ymax - ymin;
if (w < 4 || h < 4) {
// drop boxes that are too small
continue;
}
// shrink bboxes
int gxmin = cvRound((xmin + w / 4) * scaling);
int gxmax = cvRound((xmax - w / 4) * scaling);
int gymin = cvRound((ymin + h / 4) * scaling);
int gymax = cvRound((ymax - h / 4) * scaling);
int gxmin = cvFloor((xmin + w * half_shrink_factor) * scaling);
int gxmax = cvCeil((xmax - w * half_shrink_factor) * scaling);
int gymin = cvFloor((ymin + h * half_shrink_factor) * scaling);
int gymax = cvCeil((ymax - h * half_shrink_factor) * scaling);

CHECK_LE(gxmin, gxmax);
CHECK_LE(gymin, gymax);
CHECK_LE(0, gxmin);
CHECK_LE(0, gymin);
CHECK_LE(gxmax, full_label_width);
CHECK_LE(gymax, full_label_height);
if (gxmin >= full_label_width) {
gxmin = full_label_width - 1;
}
if (gymin >= full_label_height) {
gymin = full_label_height - 1;
}
CHECK_LT(gxmin, full_label_width);
CHECK_LT(gymin, full_label_height);
cv::Rect r(gxmin, gymin,
gxmax - gxmin + (gxmax == gxmin && gxmax < full_label_width ? 1 : 0),
gymax - gymin + (gymax == gymin && gymax < full_label_height ? 1 : 0));

int normalization_height = ymax - ymin == 0 ? 1 : ymax - ymin;
CHECK_GT(normalization_height, 0);
int normalization_width = xmax - xmin == 0 ? 1 : xmax - xmin;
CHECK_GT(normalization_width, 0);
float default_flabels[num_total_labels] =
{1.0, xmin, ymin, xmax, ymax, 1.0 / normalization_width, 1.0};
float y_flabels[num_total_labels] =
{1.0, xmin, ymin, xmax, ymax, 1.0 / normalization_height, 1.0};
for (int j = 0; j < num_total_labels; ++j) {
if (j == 2 || j == 4) {
cv::Mat roi(*labels[j], r);
roi = cv::Scalar(y_flabels[j]);
} else {
cv::Mat roi(*labels[j], r);
roi = cv::Scalar(default_flabels[j]);
CHECK_LE(0, gxmin);
CHECK_LE(0, gymin);
CHECK_LE(gxmax, full_label_width);
CHECK_LE(gymax, full_label_height);
if (gxmin == gxmax) {
if (gxmax < full_label_width - 1) {
gxmax++;
} else if (gxmin > 0) {
gxmin--;
}
}
}


int total_num_pixels = 0;
for (int y = 0; y < full_label_height; ++y) {
for (int x = 0; x < full_label_width; ++x) {
if (labels[num_total_labels - 1]->at<float>(y, x) == 1.0) {
total_num_pixels++;
if (gymin == gymax) {
if (gymax < full_label_height - 1) {
gymax++;
} else if (gymin > 0) {
gymin--;
}
}
}
if (total_num_pixels != 0) {
float reweight_value = 1.0 / total_num_pixels;
for (int y = 0; y < full_label_height; ++y) {
for (int x = 0; x < full_label_width; ++x) {
if (labels[num_total_labels - 1]->at<float>(y, x) == 1.0) {
labels[num_total_labels - 1]->at<float>(y, x) = reweight_value;
}
}
CHECK_LT(gxmin, gxmax);
CHECK_LT(gymin, gymax);
if (gxmax == full_label_width) {
gxmax--;
}
}

datum->set_channels(num_total_labels * grid_dim * grid_dim);
datum->set_height(height);
datum->set_width(width);
datum->set_label(0); // dummy label
datum->clear_data();
datum->clear_float_data();

for (int m = 0; m < num_total_labels; ++m) {
for (int dy = 0; dy < grid_dim; ++dy) {
for (int dx = 0; dx < grid_dim; ++dx) {
for (int y = 0; y < full_label_height; y += grid_dim) {
for (int x = 0; x < full_label_width; x += grid_dim) {
float adjustment = 0;
float val = labels[m]->at<float>(y + dy, x + dx);
if (m == 0 || m > 4) {
// do nothing
} else if (labels[0]->at<float>(y + dy, x + dx) == 0.0) {
// do nothing
} else if (m % 2 == 1) {
// x coordinate
adjustment = (x + dx) / scaling;
} else {
// y coordinate
adjustment = (y + dy) / scaling;
}
datum->add_float_data(val - adjustment);
}
}
}
if (gymax == full_label_height) {
gymax--;
}
}
cv::Rect r(gxmin, gymin, gxmax - gxmin + 1, gymax - gymin + 1);

CHECK_EQ(datum->float_data_size(),
num_total_labels * full_label_height * full_label_width);
for (int i = 0; i < num_total_labels; ++i) {
delete labels[i];
}

return true;
}

template <typename Dtype>
bool DrivingDataLayer<Dtype>::ReadBoundingBoxLabelToDatum(
const DrivingData& data, Datum* datum, const int h_off, const int w_off) {
const int grid_dim = data.car_label_resolution();
const int width = data.car_label_width();
const int height = data.car_label_height();
const int full_label_width = width * grid_dim;
const int full_label_height = height * grid_dim;
const float half_shrink_factor = data.car_shrink_factor() / 2;
const float scaling = static_cast<float>(full_label_width) / data.car_cropped_width();

// 1 pixel label, 4 bounding box coordinates, 3 normalization labels.
const int num_total_labels = kNumRegressionMasks;
vector<cv::Mat *> labels;
for (int i = 0; i < num_total_labels; ++i) {
labels.push_back(
new cv::Mat(full_label_height, full_label_width, CV_32F, cv::Scalar(0.0)));
}

for (int i = 0; i < data.car_boxes_size(); ++i) {
int xmin = data.car_boxes(i).xmin();
int ymin = data.car_boxes(i).ymin();
int xmax = data.car_boxes(i).xmax();
int ymax = data.car_boxes(i).ymax();
xmin = std::min(std::max(0, xmin - w_off), data.car_cropped_width() - 1);
xmax = std::min(std::max(0, xmax - w_off), data.car_cropped_width() - 1);
ymin = std::min(std::max(0, ymin - h_off), data.car_cropped_height() - 1);
ymax = std::min(std::max(0, ymax - h_off), data.car_cropped_height() - 1);
float w = xmax - xmin;
float h = ymax - ymin;
// shrink bboxes
int gxmin = cvRound((xmin + w * half_shrink_factor) * scaling);
int gxmax = cvRound((xmax - w * half_shrink_factor) * scaling);
int gymin = cvRound((ymin + h * half_shrink_factor) * scaling);
int gymax = cvRound((ymax - h * half_shrink_factor) * scaling);

CHECK_LE(gxmin, gxmax);
CHECK_LE(gymin, gymax);
CHECK_LE(0, gxmin);
CHECK_LE(0, gymin);
CHECK_LE(gxmax, full_label_width);
CHECK_LE(gymax, full_label_height);
if (gxmin >= full_label_width) {
gxmin = full_label_width - 1;
}
if (gymin >= full_label_height) {
gymin = full_label_height - 1;
}
CHECK_LT(gxmin, full_label_width);
CHECK_LT(gymin, full_label_height);
cv::Rect r(gxmin, gymin,
gxmax - gxmin + (gxmax == gxmin && gxmax < full_label_width ? 1 : 0),
gymax - gymin + (gymax == gymin && gymax < full_label_height ? 1 : 0));

int normalization_width = xmax - xmin == 0 ? 1 : xmax - xmin;
CHECK_GT(normalization_width, 0);
int normalization_height = ymax - ymin == 0 ? 1 : ymax - ymin;
CHECK_GT(normalization_height, 0);
float flabels[num_total_labels] =
{1.0, xmin, ymin, xmax, ymax, 1.0 / normalization_width,
1.0 / normalization_height, 1.0};
{1.0, xmin, ymin, xmax, ymax, 1.0 / w, 1.0 / h, 1.0};
for (int j = 0; j < num_total_labels; ++j) {
cv::Mat roi(*labels[j], r);
roi = cv::Scalar(flabels[j]);
Expand Down

0 comments on commit 93792f4

Please sign in to comment.