Skip to content

Commit

Permalink
Update AE layer
Browse files Browse the repository at this point in the history
  • Loading branch information
eric612 committed Aug 27, 2019
1 parent afcc31f commit 2b83740
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 9 deletions.
8 changes: 4 additions & 4 deletions include/caffe/layers/assisted_excitation_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ class AssistedExcitationLayer : public Layer<Dtype> {

virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
//virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
// const vector<Blob<Dtype>*>& top);
virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);

virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
//virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
// const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);

void visualization(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top);

Expand Down
10 changes: 5 additions & 5 deletions src/caffe/layers/assisted_excitation_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ void AssistedExcitationLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bot
if (!x)
break;

int lb_x = BOUND((int) ((x - w) * width + 0.5),0,width);
int lb_y = BOUND((int) ((y - h) * height + 0.5),0,height);
int rt_x = BOUND((int) ((x + w) * width + 0.5),0,width);
int rt_y = BOUND((int) ((y + h) * height + 0.5),0,height);
int lb_x = BOUND((int) ((x - w/2) * width + 0.5),0,width);
int lb_y = BOUND((int) ((y - h/2) * height + 0.5),0,height);
int rt_x = BOUND((int) ((x + w/2) * width + 0.5),0,width);
int rt_y = BOUND((int) ((y + h/2) * height + 0.5),0,height);
//LOG(INFO) << lb_x << "," << lb_y<< ","<< rt_x << "," << rt_y;

for (int i = lb_y;i < rt_y; i++) {
Expand Down Expand Up @@ -156,7 +156,7 @@ void AssistedExcitationLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& to
}

#ifdef CPU_ONLY
#STUB_GPU(AssistedExcitationLayer);
STUB_GPU(AssistedExcitationLayer);
#endif

INSTANTIATE_CLASS(AssistedExcitationLayer);
Expand Down
96 changes: 96 additions & 0 deletions src/caffe/layers/assisted_excitation_layer.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@


#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/video.hpp>
#endif // USE_OPENCV

#include <cmath>
#include <vector>
#include "caffe/util/math_functions.hpp"
#include "caffe/layers/assisted_excitation_layer.hpp"
#include "caffe/layers/region_loss_layer.hpp"
#include <iostream>
#include <algorithm>
#define BOUND(a,min_val,max_val) ( (a < min_val) ? min_val : (a >= max_val) ? (max_val) : a )
#define M_PI 3.14159265358979323846/* pi */
namespace caffe {


template <typename Dtype>
void AssistedExcitationLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {

const Dtype* bottom_data = bottom[0]->cpu_data();
Dtype* top_data = top[0]->mutable_gpu_data();
const int count = bottom[0]->count();
caffe_copy(count, bottom_data, top_data);
const Dtype* label_data = bottom[1]->cpu_data(); //[label,x,y,w,h]
Dtype* swap_data = swap_.mutable_cpu_data();

int width = bottom[0]->width();
int height = bottom[0]->height();
const Dtype* mean_data = bottom[2]->cpu_data();
//cv::Mat img(width, height, CV_8UC1);
//img = cv::Scalar(0);

float alpha = cos ((this->iter_ / (float) this->max_iter_)*M_PI/2.0f);
//LOG(INFO) << alpha ;
for (int b = 0; b < bottom[0]->num(); b++) {
Dtype* mask_data = mask_.mutable_cpu_data();
caffe_set(bottom[2]->count(), Dtype(0), mask_data);
for (int t = 0; t < 300; ++t) {
Dtype x = label_data[b * 300 * 5 + t * 5 + 1];
Dtype y = label_data[b * 300 * 5 + t * 5 + 2];
Dtype w = label_data[b * 300 * 5 + t * 5 + 3];
Dtype h = label_data[b * 300 * 5 + t * 5 + 4];
if (!x)
break;

int lb_x = BOUND((int) ((x - w/2) * width + 0.5),0,width);
int lb_y = BOUND((int) ((y - h/2) * height + 0.5),0,height);
int rt_x = BOUND((int) ((x + w/2) * width + 0.5),0,width);
int rt_y = BOUND((int) ((y + h/2) * height + 0.5),0,height);
//LOG(INFO) << lb_x << "," << lb_y<< ","<< rt_x << "," << rt_y;

for (int i = lb_y;i < rt_y; i++) {

for (int j = lb_x; j < rt_x; j++)
{
int index = i * width + j + b*bottom[2]->count(1);
mask_data[index] = alpha*mean_data[index];
//mask_data[index] = alpha;
//LOG(INFO) << index;
//caffe_set(count, alpha*mean_data[index], swap_data);
//ptr2[j] = (unsigned char) BOUND(fabs(mask_data[index])*255,0,255);
}
}
}
mask_data = mask_.mutable_gpu_data();
for (int c=0;c<bottom[0]->channels();c++) {
int offset = bottom[0]->offset(b) + c*bottom[2]->count(1);
caffe_gpu_axpy(width*height, Dtype(1) , mask_data, &top_data[offset]);
}

}
}

template <typename Dtype>
void AssistedExcitationLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) {
if (propagate_down[0]) {
const int count = top[0]->count();
const Dtype* top_data = top[0]->gpu_data();
const Dtype* top_diff = top[0]->gpu_diff();
Dtype* bottom_diff = bottom[0]->mutable_gpu_diff();
caffe_copy(count, top_diff, bottom_diff);
}
}

INSTANTIATE_LAYER_GPU_FUNCS(AssistedExcitationLayer);

} // namespace caffe

0 comments on commit 2b83740

Please sign in to comment.