Table of Contents
1.在RefineDet/include/caffe/layers下新建interp_layer.hpp文件,添加代碼如下:
2. 在RefineDet/include/caffe/util下添加interp.hpp文件,添加代碼如下:
3. 在RefineDet/include/caffe下添加文件common.cuh,添加代碼如下:
4. 在RefineDet/src/caffe/layers下添加文件interp_layer.cpp,添加代碼如下:
5. 在RefineDet/caffe/util下添加文件interp.cpp,添加代碼如下:
6.在RefineDet/src/caffe/proto下添加文件caffe.proto,添加代碼如下:
配置RefineDet-caffe
https://github.com/sfzhang15/RefineDet
其實readme已經很詳細了,主要注意一點
cd $RefineDet_ROOT
# Modify Makefile.config according to your Caffe installation.
# Make sure to include $RefineDet_ROOT/python to your PYTHONPATH.
cp Makefile.config.example Makefile.config
make all -j && make py
這裏的# Make sure to include $RefineDet_ROOT/python to your PYTHONPATH. 要將RefineDet中的python路徑添加到.bashrc中並生效。因爲剛開始沒理解這個,所以後續訓練一直報錯。
在~/.bashrc中將RefineDet中的python路徑添加PYTHONPATH路徑,
sudo gedit ~/.bashrc
export PYTHONPATH=/media/****/****/RefineDet/python:$PYTHONPATH
source ~/.bashrc
畫loss曲線
1. 查看log文件
保存在RefineDet/jobs/VGGNet/VOC0712/SSD_300x300
2. 進行log文件分析
caffe自帶日記分析工具
cd RefineDet/tools/extra
./parse_log.sh ../../jobs/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300.log
之後當前目錄下會生成VGG_VOC0712_SSD_300x300.log.test,VGG_VOC0712_SSD_300x300.log.train兩個解析過的文件。
3. 利用gnuplot繪製曲線
複製plot_log.gnuplot.example的一個副本plot_log.gnuplot#
cp plot_log.gnuplot.example plot_log.gnuplot
修改plot_log.gnuplot,
#在tools/extra下執行
gnuplot plot_log.gnuplot
運行之後即可在當前目錄下生成loss曲線
以RefineDet爲例添加雙線性插值函數
1.在RefineDet/include/caffe/layers下新建interp_layer.hpp文件,添加代碼如下:
#ifndef CAFFE_INTERP_LAYER_HPP_
#define CAFFE_INTERP_LAYER_HPP_
#include <vector>
#include "caffe/blob.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
namespace caffe {
/**
* @brief Changes the spatial resolution by bi-linear interpolation.
* The target size is specified in terms of pixels.
* The start and end pixels of the input are mapped to the start
* and end pixels of the output.
*/
template <typename Dtype>
class InterpLayer : public Layer<Dtype> {
public:
explicit InterpLayer(const LayerParameter& param)
: Layer<Dtype>(param) {}
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual inline const char* type() const { return "Interp"; }
virtual inline int MinBottomBlobs() const { return 1; } // virtual inline int MinBottomBlobs() const { return 1; }
virtual inline int ExactNumTopBlobs() const { return 1; }
protected:
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 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);
int num_, channels_;
int height_in_, width_in_;
int height_out_, width_out_;
int pad_beg_, pad_end_;
int height_in_eff_, width_in_eff_;
};
} // namespace caffe
#endif // CAFFE_CONV_LAYER_HPP_
2. 在RefineDet/include/caffe/util下添加interp.hpp文件,添加代碼如下:
// Copyright 2014 George Papandreou
#ifndef CAFFE_UTIL_INTERP_H_
#define CAFFE_UTIL_INTERP_H_
//#include <cublas_v2.h>
#include "caffe/proto/caffe.pb.h"
namespace caffe {
// Bi-linear interpolation
// IN : [channels height1 width1] cropped from a bigger [Height1 Width1] image // OUT: [channels height2 width2] cropped from a bigger [Height2 Width2] image
template <typename Dtype, bool packed>
void caffe_cpu_interp2(const int channels,
const Dtype *data1, const int x1, const int y1, const int height1, const int width1, const int Height1, const int Width1,
Dtype *data2, const int x2, const int y2, const int height2, const int width2, const int Height2, const int Width2);
template <typename Dtype, bool packed>
void caffe_gpu_interp2(const int channels,
const Dtype *data1, const int x1, const int y1, const int height1, const int width1, const int Height1, const int Width1,
Dtype *data2, const int x2, const int y2, const int height2, const int width2, const int Height2, const int Width2);
// Backward (adjoint) operation
template <typename Dtype, bool packed>
void caffe_cpu_interp2_backward(const int channels,
Dtype *data1, const int x1, const int y1, const int height1, const int width1, const int Height1, const int Width1,
const Dtype *data2, const int x2, const int y2, const int height2, const int width2, const int Height2, const int Width2);
template <typename Dtype, bool packed>
void caffe_gpu_interp2_backward(const int channels,
Dtype *data1, const int x1, const int y1, const int height1, const int width1, const int Height1, const int Width1,
const Dtype *data2, const int x2, const int y2, const int height2, const int width2, const int Height2, const int Width2);
// Create Gaussian pyramid of an image. Assume output space is pre-allocated.
// IN : [channels height width]
template <typename Dtype, bool packed>
void caffe_cpu_pyramid2(const int channels,
const Dtype *data, const int height, const int width,
Dtype *data_pyr, const int levels);
template <typename Dtype, bool packed>
void caffe_gpu_pyramid2(const int channels,
const Dtype *data, const int height, const int width,
Dtype *data_pyr, const int levels);
/*
template <typename Dtype, bool packed>
void caffe_cpu_mosaic(const int channels,
const Dtype *data1, const MosaicParameter mosaic_params1,
const Dtype *data_pyr, const int levels,
Dtype *data2, const MosaicParameter mosaic_params2);
template <typename Dtype, bool packed>
void caffe_gpu_mosaic(const int channels,
const Dtype *data1, const MosaicParameter mosaic_params1,
const Dtype *data_pyr, const int levels,
Dtype *data2, const MosaicParameter mosaic_params2);
*/
} // namespace caffe
#endif
3. 在RefineDet/include/caffe下添加文件common.cuh,添加代碼如下:
// Copyright 2014 George Papandreou
#ifndef CAFFE_COMMON_CUH_
#define CAFFE_COMMON_CUH_
#include <cuda.h>
//這裏要根據自己的環境進行配置
#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 600
#else
// CUDA: atomicAdd is not defined for doubles
static __inline__ __device__ double atomicAdd(double *address, double val) {
unsigned long long int* address_as_ull = (unsigned long long int*)address;
unsigned long long int old = *address_as_ull, assumed;
if (val==0.0)
return __longlong_as_double(old);
do {
assumed = old;
old = atomicCAS(address_as_ull, assumed, __double_as_longlong(val +__longlong_as_double(assumed)));
} while (assumed != old);
return __longlong_as_double(old);
}
#endif
#endif
4. 在RefineDet/src/caffe/layers下添加文件interp_layer.cpp,添加代碼如下:
#include <vector>
#include "caffe/layer.hpp"
#include "caffe/util/math_functions.hpp"
#include "caffe/util/interp.hpp"
#include "caffe/layers/interp_layer.hpp"
namespace caffe {
template <typename Dtype>
void InterpLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
InterpParameter interp_param = this->layer_param_.interp_param();
pad_beg_ = interp_param.pad_beg();
pad_end_ = interp_param.pad_end();
CHECK_LE(pad_beg_, 0) << "Only supports non-pos padding (cropping) for now";
CHECK_LE(pad_end_, 0) << "Only supports non-pos padding (cropping) for now";
}
template <typename Dtype>
void InterpLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
num_ = bottom[0]->num();
channels_ = bottom[0]->channels();
height_in_ = bottom[0]->height();
width_in_ = bottom[0]->width();
height_in_eff_ = height_in_ + pad_beg_ + pad_end_;
width_in_eff_ = width_in_ + pad_beg_ + pad_end_;
InterpParameter interp_param = this->layer_param_.interp_param();
if (interp_param.use_blob_size()){
CHECK_GT(bottom.size(),1)<<"use blob size should have another blob to supply size";
height_out_ = bottom[1]->height();
width_out_ = bottom[1]->width();
} else if (interp_param.has_shrink_factor() &&
!interp_param.has_zoom_factor()) {
const int shrink_factor = interp_param.shrink_factor();
CHECK_GE(shrink_factor, 1) << "Shrink factor must be positive";
height_out_ = (height_in_eff_ - 1) / shrink_factor + 1;
width_out_ = (width_in_eff_ - 1) / shrink_factor + 1;
} else if (interp_param.has_zoom_factor() &&
!interp_param.has_shrink_factor()) {
const int zoom_factor = interp_param.zoom_factor();
CHECK_GE(zoom_factor, 1) << "Zoom factor must be positive";
height_out_ = height_in_eff_ + (height_in_eff_ - 1) * (zoom_factor - 1);
width_out_ = width_in_eff_ + (width_in_eff_ - 1) * (zoom_factor - 1);
} else if (interp_param.has_height() && interp_param.has_width()) {
height_out_ = interp_param.height();
width_out_ = interp_param.width();
} else if (interp_param.has_shrink_factor() &&
interp_param.has_zoom_factor()) {
const int shrink_factor = interp_param.shrink_factor();
const int zoom_factor = interp_param.zoom_factor();
CHECK_GE(shrink_factor, 1) << "Shrink factor must be positive";
CHECK_GE(zoom_factor, 1) << "Zoom factor must be positive";
height_out_ = (height_in_eff_ - 1) / shrink_factor + 1;
width_out_ = (width_in_eff_ - 1) / shrink_factor + 1;
height_out_ = height_out_ + (height_out_ - 1) * (zoom_factor - 1);
width_out_ = width_out_ + (width_out_ - 1) * (zoom_factor - 1);
} else {
LOG(FATAL);
}
CHECK_GT(height_in_eff_, 0) << "height should be positive";
CHECK_GT(width_in_eff_, 0) << "width should be positive";
CHECK_GT(height_out_, 0) << "height should be positive";
CHECK_GT(width_out_, 0) << "width should be positive";
top[0]->Reshape(num_, channels_, height_out_, width_out_);
}
template <typename Dtype>
void InterpLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
caffe_cpu_interp2<Dtype,false>(num_ * channels_,
bottom[0]->cpu_data(), - pad_beg_, - pad_beg_, height_in_eff_, width_in_eff_, height_in_, width_in_,
top[0]->mutable_cpu_data(), 0, 0, height_out_, width_out_, height_out_, width_out_);
}
template <typename Dtype>
void InterpLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
if (!propagate_down[0]) { return; }
caffe_set(bottom[0]->count(), Dtype(0), bottom[0]->mutable_cpu_diff());
caffe_cpu_interp2_backward<Dtype,false>(num_ * channels_,
bottom[0]->mutable_cpu_diff(), - pad_beg_, - pad_beg_, height_in_eff_, width_in_eff_, height_in_, width_in_,
top[0]->cpu_diff(), 0, 0, height_out_, width_out_, height_out_, width_out_);
}
#ifndef CPU_ONLY
template <typename Dtype>
void InterpLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
caffe_gpu_interp2<Dtype,false>(num_ * channels_,
bottom[0]->gpu_data(), - pad_beg_, - pad_beg_, height_in_eff_, width_in_eff_, height_in_, width_in_,
top[0]->mutable_gpu_data(), 0, 0, height_out_, width_out_, height_out_, width_out_);
}
template <typename Dtype>
void InterpLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
if (!propagate_down[0]) { return; }
caffe_gpu_set(bottom[0]->count(), Dtype(0), bottom[0]->mutable_gpu_diff());
caffe_gpu_interp2_backward<Dtype,false>(num_ * channels_,
bottom[0]->mutable_gpu_diff(), - pad_beg_, - pad_beg_, height_in_eff_, width_in_eff_, height_in_, width_in_,
top[0]->gpu_diff(), 0, 0, height_out_, width_out_, height_out_, width_out_);
}
#endif
#ifdef CPU_ONLY
STUB_GPU(InterpLayer);
#endif
INSTANTIATE_CLASS(InterpLayer);
REGISTER_LAYER_CLASS(Interp);
} // namespace caffe
5. 在RefineDet/caffe/util下添加文件interp.cpp,添加代碼如下:
// Copyright 2014 George Papandreou
#include "caffe/common.hpp"
#include "caffe/util/interp.hpp"
#include <algorithm>
#include <cmath>
namespace caffe {
// Bi-linear interpolation
// IN : [channels height1 width1] cropped from a bigger [Height1 Width1] image
// OUT: [channels height2 width2] cropped from a bigger [Height2 Width2] image
template <typename Dtype, bool packed>
void caffe_cpu_interp2(const int channels,
const Dtype *data1, const int x1, const int y1, const int height1, const int width1, const int Height1, const int Width1,
Dtype *data2, const int x2, const int y2, const int height2, const int width2, const int Height2, const int Width2) {
CHECK(x1 >= 0 && y1 >= 0 && height1 > 0 && width1 > 0 && x2 >= 0 && y2 >= 0 && height2 > 0 && width2 > 0);
CHECK(Width1 >= width1 + x1 && Height1 >= height1 + y1 && Width2 >= width2 + x2 && Height2 >= height2 + y2);
// special case: just copy
if (height1 == height2 && width1 == width2) {
for (int h2 = 0; h2 < height2; ++h2) {
const int h1 = h2;
for (int w2 = 0; w2 < width2; ++w2) {
const int w1 = w2;
if (packed) {
const Dtype* pos1 = &data1[channels * ((y1 + h1) * Width1 + (x1 + w1))];
Dtype* pos2 = &data2[channels * ((y2 + h2) * Width2 + (x2 + w2))];
for (int c = 0; c < channels; ++c) {
pos2[0] = pos1[0];
pos1++;
pos2++;
}
}
else {
const Dtype* pos1 = &data1[(y1 + h1) * Width1 + (x1 + w1)];
Dtype* pos2 = &data2[(y2 + h2) * Width2 + (x2 + w2)];
for (int c = 0; c < channels; ++c) {
pos2[0] = pos1[0];
pos1 += Width1 * Height1;
pos2 += Width2 * Height2;
}
}
}
}
return;
}
const float rheight = (height2 > 1) ? static_cast<float>(height1 - 1) / (height2 - 1) : 0.f;
const float rwidth = (width2 > 1) ? static_cast<float>(width1 - 1) / (width2 - 1) : 0.f;
for (int h2 = 0; h2 < height2; ++h2) {
const float h1r = rheight * h2;
const int h1 = h1r;
const int h1p = (h1 < height1 - 1) ? 1 : 0;
const Dtype h1lambda = h1r - h1;
const Dtype h0lambda = Dtype(1.) - h1lambda;
for (int w2 = 0; w2 < width2; ++w2) {
const float w1r = rwidth * w2;
const int w1 = w1r;
const int w1p = (w1 < width1 - 1) ? 1 : 0;
const Dtype w1lambda = w1r - w1;
const Dtype w0lambda = Dtype(1.) - w1lambda;
if (packed) {
const Dtype* pos1 = &data1[channels * ((y1 + h1) * Width1 + (x1 + w1))];
Dtype* pos2 = &data2[channels * ((y2 + h2) * Width2 + (x2 + w2))];
for (int c = 0; c < channels; ++c) {
pos2[0] =
h0lambda * (w0lambda * pos1[0] + w1lambda * pos1[channels * w1p]) +
h1lambda * (w0lambda * pos1[channels * h1p * Width1] + w1lambda * pos1[channels * (h1p * Width1 + w1p)]);
pos1++;
pos2++;
}
}
else {
const Dtype* pos1 = &data1[(y1 + h1) * Width1 + (x1 + w1)];
Dtype* pos2 = &data2[(y2 + h2) * Width2 + (x2 + w2)];
for (int c = 0; c < channels; ++c) {
pos2[0] =
h0lambda * (w0lambda * pos1[0] + w1lambda * pos1[w1p]) +
h1lambda * (w0lambda * pos1[h1p * Width1] + w1lambda * pos1[h1p * Width1 + w1p]);
pos1 += Width1 * Height1;
pos2 += Width2 * Height2;
}
}
}
}
}
// Backward (adjoint) operation 1 <- 2 (accumulates)
template <typename Dtype, bool packed>
void caffe_cpu_interp2_backward(const int channels,
Dtype *data1, const int x1, const int y1, const int height1, const int width1, const int Height1, const int Width1,
const Dtype *data2, const int x2, const int y2, const int height2, const int width2, const int Height2, const int Width2) {
CHECK(x1 >= 0 && y1 >= 0 && height1 > 0 && width1 > 0 && x2 >= 0 && y2 >= 0 && height2 > 0 && width2 > 0);
CHECK(Width1 >= width1 + x1 && Height1 >= height1 + y1 && Width2 >= width2 + x2 && Height2 >= height2 + y2);
// special case: same-size matching grids
if (height1 == height2 && width1 == width2) {
for (int h2 = 0; h2 < height2; ++h2) {
const int h1 = h2;
for (int w2 = 0; w2 < width2; ++w2) {
const int w1 = w2;
if (packed) {
Dtype* pos1 = &data1[channels * ((y1 + h1) * Width1 + (x1 + w1))];
const Dtype* pos2 = &data2[channels * ((y2 + h2) * Width2 + (x2 + w2))];
for (int c = 0; c < channels; ++c) {
pos1[0] += pos2[0];
pos1++;
pos2++;
}
}
else {
Dtype* pos1 = &data1[(y1 + h1) * Width1 + (x1 + w1)];
const Dtype* pos2 = &data2[(y2 + h2) * Width2 + (x2 + w2)];
for (int c = 0; c < channels; ++c) {
pos1[0] += pos2[0];
pos1 += Width1 * Height1;
pos2 += Width2 * Height2;
}
}
}
}
return;
}
const float rheight = (height2 > 1) ? static_cast<float>(height1 - 1) / (height2 - 1) : 0.f;
const float rwidth = (width2 > 1) ? static_cast<float>(width1 - 1) / (width2 - 1) : 0.f;
for (int h2 = 0; h2 < height2; ++h2) {
const float h1r = rheight * h2;
const int h1 = h1r;
const int h1p = (h1 < height1 - 1) ? 1 : 0;
const Dtype h1lambda = h1r - h1;
const Dtype h0lambda = Dtype(1.) - h1lambda;
for (int w2 = 0; w2 < width2; ++w2) {
const float w1r = rwidth * w2;
const int w1 = w1r;
const int w1p = (w1 < width1 - 1) ? 1 : 0;
const Dtype w1lambda = w1r - w1;
const Dtype w0lambda = Dtype(1.) - w1lambda;
if (packed) {
Dtype* pos1 = &data1[channels * ((y1 + h1) * Width1 + (x1 + w1))];
const Dtype* pos2 = &data2[channels * ((y2 + h2) * Width2 + (x2 + w2))];
for (int c = 0; c < channels; ++c) {
pos1[0] += h0lambda * w0lambda * pos2[0];
pos1[channels * w1p] += h0lambda * w1lambda * pos2[0];
pos1[channels * h1p * Width1] += h1lambda * w0lambda * pos2[0];
pos1[channels * (h1p * Width1 + w1p)] += h1lambda * w1lambda * pos2[0];
pos1++;
pos2++;
}
}
else {
Dtype* pos1 = &data1[(y1 + h1) * Width1 + (x1 + w1)];
const Dtype* pos2 = &data2[(y2 + h2) * Width2 + (x2 + w2)];
for (int c = 0; c < channels; ++c) {
pos1[0] += h0lambda * w0lambda * pos2[0];
pos1[w1p] += h0lambda * w1lambda * pos2[0];
pos1[h1p * Width1] += h1lambda * w0lambda * pos2[0];
pos1[h1p * Width1 + w1p] += h1lambda * w1lambda * pos2[0];
pos1 += Width1 * Height1;
pos2 += Width2 * Height2;
}
}
}
}
}
// Create Gaussian pyramid of an image. Assume output space is pre-allocated.
// IN : [channels height width]
template <typename Dtype, bool packed>
void caffe_cpu_pyramid2(const int channels,
const Dtype *data, const int height, const int width,
Dtype *data_pyr, const int levels) {
CHECK(height > 0 && width > 0 && levels >= 0);
int height1 = height, width1 = width;
int height2 = height, width2 = width;
const Dtype *data1 = data;
Dtype *data2 = data_pyr;
for (int l = 0; l < levels; ++l) {
height2 /= 2;
width2 /= 2;
if (height2 == 0 || width2 == 0) {
break;
}
for (int h2 = 0; h2 < height2; ++h2) {
const int h1 = 2 * h2;
for (int w2 = 0; w2 < width2; ++w2) {
const int w1 = 2 * w2;
if (packed) {
const Dtype* pos1 = &data1[channels * (h1 * width1 + w1)];
Dtype* pos2 = &data2[channels * (h2 * width2 + w2)];
for (int c = 0; c < channels; ++c) {
pos2[0] = static_cast<Dtype>(.25) *
(pos1[0] + pos1[channels] +
pos1[channels * width1] + pos1[channels * (width1 + 1)]);
pos1++;
pos2++;
}
}
else {
const Dtype* pos1 = &data1[h1 * width1 + w1];
Dtype* pos2 = &data2[h2 * width2 + w2];
for (int c = 0; c < channels; ++c) {
pos2[0] = static_cast<Dtype>(.25) *
(pos1[0] + pos1[1] +
pos1[width1] + pos1[width1 + 1]);
pos1 += width1 * height1;
pos2 += width2 * height2;
}
}
}
}
data1 = data2;
height1 = height2;
width1 = width2;
data2 += channels * height2 * width2;
}
}
/*
template <typename Dtype, bool packed>
void caffe_cpu_mosaic(const int channels,
const Dtype *data1, const MosaicParameter mosaic_params1,
const Dtype *data_pyr, const int levels,
Dtype *data2, const MosaicParameter mosaic_params2) {
const int num1 = mosaic_params1.rects_size();
const int num2 = mosaic_params2.rects_size();
CHECK(num1 == num2 || (num1 == 1 && num2 > 1) || (num2 == 1 && num1 > 1));
const int num = std::max(num1, num2);
for (int i = 0; i < num; ++i) {
const Rect rect1 = mosaic_params1.rects((i < num1) ? i : 0);
const Rect rect2 = mosaic_params2.rects((i < num2) ? i : 0);
int level = log2(sqrt((float)rect1.height() * rect1.width() / rect2.height() / rect2.width()));
level = std::max(0, std::min(levels, level));
if (data_pyr == 0 || level == 0) {
caffe_cpu_interp2<Dtype,packed>(channels,
data1, rect1.x(), rect1.y(), rect1.height(), rect1.width(), mosaic_params1.height(), mosaic_params1.width(),
data2, rect2.x(), rect2.y(), rect2.height(), rect2.width(), mosaic_params2.height(), mosaic_params2.width());
}
else {
const Dtype *data_pyr_l = data_pyr;
int factor = 2;
for (int l = 1; l < level; ++l) {
data_pyr_l += channels * (mosaic_params1.height() / factor) * (mosaic_params1.width() / factor);
factor *= 2;
}
caffe_cpu_interp2<Dtype,packed>(channels,
data_pyr_l, rect1.x() / factor, rect1.y() / factor, rect1.height() / factor, rect1.width() / factor, mosaic_params1.height() / factor, mosaic_params1.width() / factor,
data2, rect2.x(), rect2.y(), rect2.height(), rect2.width(), mosaic_params2.height(), mosaic_params2.width());
}
}
}
template <typename Dtype, bool packed>
void caffe_gpu_mosaic(const int channels,
const Dtype *data1, const MosaicParameter mosaic_params1,
const Dtype *data_pyr, const int levels,
Dtype *data2, const MosaicParameter mosaic_params2) {
const int num1 = mosaic_params1.rects_size();
const int num2 = mosaic_params2.rects_size();
CHECK(num1 == num2 || (num1 == 1 && num2 > 1) || (num2 == 1 && num1 > 1));
const int num = std::max(num1, num2);
for (int i = 0; i < num; ++i) {
const Rect rect1 = mosaic_params1.rects((i < num1) ? i : 0);
const Rect rect2 = mosaic_params2.rects((i < num2) ? i : 0);
int level = log2(sqrt((float)rect1.height() * rect1.width() / rect2.height() / rect2.width()));
level = std::max(0, std::min(levels, level));
if (data_pyr == 0 || level == 0) {
caffe_gpu_interp2<Dtype,packed>(channels,
data1, rect1.x(), rect1.y(), rect1.height(), rect1.width(), mosaic_params1.height(), mosaic_params1.width(),
data2, rect2.x(), rect2.y(), rect2.height(), rect2.width(), mosaic_params2.height(), mosaic_params2.width());
}
else {
const Dtype *data_pyr_l = data_pyr;
int factor = 2;
for (int l = 1; l < level; ++l) {
data_pyr_l += channels * (mosaic_params1.height() / factor) * (mosaic_params1.width() / factor);
factor *= 2;
}
caffe_gpu_interp2<Dtype,packed>(channels,
data_pyr_l, rect1.x() / factor, rect1.y() / factor, rect1.height() / factor, rect1.width() / factor, mosaic_params1.height() / factor, mosaic_params1.width() / factor,
data2, rect2.x(), rect2.y(), rect2.height(), rect2.width(), mosaic_params2.height(), mosaic_params2.width());
}
}
}
*/
// Explicit instances
template void caffe_cpu_interp2<float,false>(const int, const float *, const int, const int, const int, const int, const int, const int, float *, const int, const int, const int, const int, const int, const int);
template void caffe_cpu_interp2<float,true>(const int, const float *, const int, const int, const int, const int, const int, const int, float *, const int, const int, const int, const int, const int, const int);
template void caffe_cpu_interp2<double,false>(const int, const double *, const int, const int, const int, const int, const int, const int, double *, const int, const int, const int, const int, const int, const int);
template void caffe_cpu_interp2<double,true>(const int, const double *, const int, const int, const int, const int, const int, const int, double *, const int, const int, const int, const int, const int, const int);
template void caffe_cpu_interp2_backward<float,false>(const int, float *, const int, const int, const int, const int, const int, const int, const float *, const int, const int, const int, const int, const int, const int);
template void caffe_cpu_interp2_backward<double,false>(const int, double *, const int, const int, const int, const int, const int, const int, const double *, const int, const int, const int, const int, const int, const int);
template void caffe_cpu_pyramid2<float,false>(const int, const float *, const int, const int, float *, const int);
template void caffe_cpu_pyramid2<float,true>(const int, const float *, const int, const int, float *, const int);
template void caffe_cpu_pyramid2<double,false>(const int, const double *, const int, const int, double *, const int);
template void caffe_cpu_pyramid2<double,true>(const int, const double *, const int, const int, double *, const int);
/*
template void caffe_cpu_mosaic<float,false>(const int, const float *, const MosaicParameter, const float *, const int, float *, const MosaicParameter);
template void caffe_cpu_mosaic<float,true>(const int, const float *, const MosaicParameter, const float *, const int, float *, const MosaicParameter);
template void caffe_cpu_mosaic<double,false>(const int, const double *, const MosaicParameter, const double *, const int, double *, const MosaicParameter);
template void caffe_cpu_mosaic<double,true>(const int, const double *, const MosaicParameter, const double *, const int, double *, const MosaicParameter);
template void caffe_gpu_mosaic<float,false>(const int, const float *, const MosaicParameter, const float *, const int, float *, const MosaicParameter);
template void caffe_gpu_mosaic<float,true>(const int, const float *, const MosaicParameter, const float *, const int, float *, const MosaicParameter);
template void caffe_gpu_mosaic<double,false>(const int, const double *, const MosaicParameter, const double *, const int, double *, const MosaicParameter);
template void caffe_gpu_mosaic<double,true>(const int, const double *, const MosaicParameter, const double *, const int, double *, const MosaicParameter);
*/
} // namespace caffe
6. 在RefineDet/src/caffe/util下添加文件interp.cu,添加代碼如下:
// Copyright 2014 George Papandreou
#include "caffe/common.hpp"
#include "caffe/common.cuh"
#include "caffe/util/interp.hpp"
namespace caffe {
// Bi-linear interpolation
// IN : [channels height1 width1] cropped from a bigger [Height1 Width1] image
// OUT: [channels height2 width2] cropped from a bigger [Height2 Width2] image
template <typename Dtype, bool packed>
__global__ void caffe_gpu_interp2_kernel(const int n, const float rheight, const float rwidth,
const int channels,
const Dtype *data1, const int x1, const int y1, const int height1, const int width1, const int Height1, const int Width1,
Dtype *data2, const int x2, const int y2, const int height2, const int width2, const int Height2, const int Width2) {
int index = threadIdx.x + blockIdx.x * blockDim.x;
if (index < n) {
const int w2 = index % width2; // 0:width2-1
const int h2 = index / width2; // 0:height2-1
// special case: just copy
if (height1 == height2 && width1 == width2) {
const int h1 = h2;
const int w1 = w2;
if (packed) {
const Dtype* pos1 = &data1[channels * ((y1 + h1) * Width1 + (x1 + w1))];
Dtype* pos2 = &data2[channels * ((y2 + h2) * Width2 + (x2 + w2))];
for (int c = 0; c < channels; ++c) {
pos2[0] = pos1[0];
pos1++;
pos2++;
}
}
else {
const Dtype* pos1 = &data1[(y1 + h1) * Width1 + (x1 + w1)];
Dtype* pos2 = &data2[(y2 + h2) * Width2 + (x2 + w2)];
for (int c = 0; c < channels; ++c) {
pos2[0] = pos1[0];
pos1 += Width1 * Height1;
pos2 += Width2 * Height2;
}
}
return;
}
//
const float h1r = rheight * h2;
const int h1 = h1r;
const int h1p = (h1 < height1 - 1) ? 1 : 0;
const Dtype h1lambda = h1r - h1;
const Dtype h0lambda = Dtype(1.) - h1lambda;
//
const float w1r = rwidth * w2;
const int w1 = w1r;
const int w1p = (w1 < width1 - 1) ? 1 : 0;
const Dtype w1lambda = w1r - w1;
const Dtype w0lambda = Dtype(1.) - w1lambda;
//
if (packed) {
const Dtype* pos1 = &data1[channels * ((y1 + h1) * Width1 + (x1 + w1))];
Dtype* pos2 = &data2[channels * ((y2 + h2) * Width2 + (x2 + w2))];
for (int c = 0; c < channels; ++c) {
pos2[0] =
h0lambda * (w0lambda * pos1[0] + w1lambda * pos1[channels * w1p]) +
h1lambda * (w0lambda * pos1[channels * h1p * Width1] + w1lambda * pos1[channels * (h1p * Width1 + w1p)]);
pos1++;
pos2++;
}
}
else {
const Dtype* pos1 = &data1[(y1 + h1) * Width1 + (x1 + w1)];
Dtype* pos2 = &data2[(y2 + h2) * Width2 + (x2 + w2)];
for (int c = 0; c < channels; ++c) {
pos2[0] =
h0lambda * (w0lambda * pos1[0] + w1lambda * pos1[w1p]) +
h1lambda * (w0lambda * pos1[h1p * Width1] + w1lambda * pos1[h1p * Width1 + w1p]);
pos1 += Width1 * Height1;
pos2 += Width2 * Height2;
}
}
}
}
template <typename Dtype, bool packed>
void caffe_gpu_interp2(const int channels,
const Dtype *data1, const int x1, const int y1, const int height1, const int width1, const int Height1, const int Width1,
Dtype *data2, const int x2, const int y2, const int height2, const int width2, const int Height2, const int Width2) {
CHECK(x1 >= 0 && y1 >= 0 && height1 > 0 && width1 > 0 && x2 >= 0 && y2 >= 0 && height2 > 0 && width2 > 0);
CHECK(Width1 >= width1 + x1 && Height1 >= height1 + y1 && Width2 >= width2 + x2 && Height2 >= height2 + y2);
const float rheight = (height2 > 1) ? static_cast<float>(height1 - 1) / (height2 - 1) : 0.f;
const float rwidth = (width2 > 1) ? static_cast<float>(width1 - 1) / (width2 - 1) : 0.f;
const int num_kernels = height2 * width2;
caffe_gpu_interp2_kernel<Dtype,packed><<<CAFFE_GET_BLOCKS(num_kernels), CAFFE_CUDA_NUM_THREADS>>>
(num_kernels, rheight, rwidth, channels,
data1, x1, y1, height1, width1, Height1, Width1,
data2, x2, y2, height2, width2, Height2, Width2);
CUDA_POST_KERNEL_CHECK;
}
// Backward (adjoint) operation 1 <- 2 (accumulates)
template <typename Dtype, bool packed>
__global__ void caffe_gpu_interp2_kernel_backward(const int n, const float rheight, const float rwidth,
const int channels,
Dtype *data1, const int x1, const int y1, const int height1, const int width1, const int Height1, const int Width1,
const Dtype *data2, const int x2, const int y2, const int height2, const int width2, const int Height2, const int Width2) {
int index = threadIdx.x + blockIdx.x * blockDim.x;
if (index < n) {
const int w2 = index % width2; // 0:width2-1
const int h2 = index / width2; // 0:height2-1
// special case: just copy
if (height1 == height2 && width1 == width2) {
const int h1 = h2;
const int w1 = w2;
if (packed) {
Dtype* pos1 = &data1[channels * ((y1 + h1) * Width1 + (x1 + w1))];
const Dtype* pos2 = &data2[channels * ((y2 + h2) * Width2 + (x2 + w2))];
for (int c = 0; c < channels; ++c) {
pos1[0] += pos2[0];
pos1++;
pos2++;
}
}
else {
Dtype* pos1 = &data1[(y1 + h1) * Width1 + (x1 + w1)];
const Dtype* pos2 = &data2[(y2 + h2) * Width2 + (x2 + w2)];
for (int c = 0; c < channels; ++c) {
pos1[0] += pos2[0];
pos1 += Width1 * Height1;
pos2 += Width2 * Height2;
}
}
return;
}
//
const float h1r = rheight * h2;
const int h1 = h1r;
const int h1p = (h1 < height1 - 1) ? 1 : 0;
const Dtype h1lambda = h1r - h1;
const Dtype h0lambda = Dtype(1.) - h1lambda;
//
const float w1r = rwidth * w2;
const int w1 = w1r;
const int w1p = (w1 < width1 - 1) ? 1 : 0;
const Dtype w1lambda = w1r - w1;
const Dtype w0lambda = Dtype(1.) - w1lambda;
//
if (packed) {
Dtype* pos1 = &data1[channels * ((y1 + h1) * Width1 + (x1 + w1))];
const Dtype* pos2 = &data2[channels * ((y2 + h2) * Width2 + (x2 + w2))];
for (int c = 0; c < channels; ++c) {
atomicAdd(&pos1[0], h0lambda * w0lambda * pos2[0]);
atomicAdd(&pos1[channels * w1p], h0lambda * w1lambda * pos2[0]);
atomicAdd(&pos1[channels * h1p * Width1], h1lambda * w0lambda * pos2[0]);
atomicAdd(&pos1[channels * (h1p * Width1 + w1p)], h1lambda * w1lambda * pos2[0]);
pos1++;
pos2++;
}
}
else {
Dtype* pos1 = &data1[(y1 + h1) * Width1 + (x1 + w1)];
const Dtype* pos2 = &data2[(y2 + h2) * Width2 + (x2 + w2)];
for (int c = 0; c < channels; ++c) {
atomicAdd(&pos1[0], h0lambda * w0lambda * pos2[0]);
atomicAdd(&pos1[w1p], h0lambda * w1lambda * pos2[0]);
atomicAdd(&pos1[h1p * Width1], h1lambda * w0lambda * pos2[0]);
atomicAdd(&pos1[h1p * Width1 + w1p], h1lambda * w1lambda * pos2[0]);
pos1 += Width1 * Height1;
pos2 += Width2 * Height2;
}
}
}
}
template <typename Dtype, bool packed>
void caffe_gpu_interp2_backward(const int channels,
Dtype *data1, const int x1, const int y1, const int height1, const int width1, const int Height1, const int Width1,
const Dtype *data2, const int x2, const int y2, const int height2, const int width2, const int Height2, const int Width2) {
CHECK(x1 >= 0 && y1 >= 0 && height1 > 0 && width1 > 0 && x2 >= 0 && y2 >= 0 && height2 > 0 && width2 > 0);
CHECK(Width1 >= width1 + x1 && Height1 >= height1 + y1 && Width2 >= width2 + x2 && Height2 >= height2 + y2);
const float rheight = (height2 > 1) ? static_cast<float>(height1 - 1) / (height2 - 1) : 0.f;
const float rwidth = (width2 > 1) ? static_cast<float>(width1 - 1) / (width2 - 1) : 0.f;
const int num_kernels = height2 * width2;
caffe_gpu_interp2_kernel_backward<Dtype,packed><<<CAFFE_GET_BLOCKS(num_kernels), CAFFE_CUDA_NUM_THREADS>>>
(num_kernels, rheight, rwidth, channels,
data1, x1, y1, height1, width1, Height1, Width1,
data2, x2, y2, height2, width2, Height2, Width2);
CUDA_POST_KERNEL_CHECK;
}
// Create Gaussian pyramid of an image. Assume output space is pre-allocated.
// IN : [channels height width]
template <typename Dtype, bool packed>
__global__ void caffe_gpu_pyramid2_kernel(const int n, const int channels,
const Dtype *data1, const int height1, const int width1,
Dtype *data2, const int height2, const int width2) {
int index = threadIdx.x + blockIdx.x * blockDim.x;
if (index < n) {
const int w2 = index % width2; // 0:width2-1
const int h2 = index / width2; // 0:height2-1
const int w1 = 2 * w2;
const int h1 = 2 * h2;
if (packed) {
const Dtype* pos1 = &data1[channels * (h1 * width1 + w1)];
Dtype* pos2 = &data2[channels * (h2 * width2 + w2)];
for (int c = 0; c < channels; ++c) {
pos2[0] = static_cast<Dtype>(.25) *
(pos1[0] + pos1[channels] +
pos1[channels * width1] + pos1[channels * (width1 + 1)]);
pos1++;
pos2++;
}
}
else {
const Dtype* pos1 = &data1[h1 * width1 + w1];
Dtype* pos2 = &data2[h2 * width2 + w2];
for (int c = 0; c < channels; ++c) {
pos2[0] = static_cast<Dtype>(.25) *
(pos1[0] + pos1[1] +
pos1[width1] + pos1[width1 + 1]);
pos1 += width1 * height1;
pos2 += width2 * height2;
}
}
}
}
template <typename Dtype, bool packed>
void caffe_gpu_pyramid2(const int channels,
const Dtype *data, const int height, const int width,
Dtype *data_pyr, const int levels) {
CHECK(height > 0 && width > 0 && levels >= 0);
int height1 = height, width1 = width;
int height2 = height, width2 = width;
const Dtype *data1 = data;
Dtype *data2 = data_pyr;
for (int l = 0; l < levels; ++l) {
height2 /= 2;
width2 /= 2;
if (height2 == 0 || width2 == 0) {
break;
}
const int num_kernels = height2 * width2;
caffe_gpu_pyramid2_kernel<Dtype,packed><<<CAFFE_GET_BLOCKS(num_kernels), CAFFE_CUDA_NUM_THREADS>>>
(num_kernels, channels, data1, height1, width1, data2, height2, width2);
CUDA_POST_KERNEL_CHECK;
data1 = data2;
height1 = height2;
width1 = width2;
data2 += channels * height2 * width2;
}
}
// Explicit instances
template void caffe_gpu_interp2<float,false>(const int, const float *, const int, const int, const int, const int, const int, const int, float *, const int, const int, const int, const int, const int, const int);
template void caffe_gpu_interp2<float,true>(const int, const float *, const int, const int, const int, const int, const int, const int, float *, const int, const int, const int, const int, const int, const int);
template void caffe_gpu_interp2<double,false>(const int, const double *, const int, const int, const int, const int, const int, const int, double *, const int, const int, const int, const int, const int, const int);
template void caffe_gpu_interp2<double,true>(const int, const double *, const int, const int, const int, const int, const int, const int, double *, const int, const int, const int, const int, const int, const int);
template void caffe_gpu_interp2_backward<float,false>(const int, float *, const int, const int, const int, const int, const int, const int, const float *, const int, const int, const int, const int, const int, const int);
template void caffe_gpu_interp2_backward<double,false>(const int, double *, const int, const int, const int, const int, const int, const int, const double *, const int, const int, const int, const int, const int, const int);
template void caffe_gpu_pyramid2<float,false>(const int, const float *, const int, const int, float *, const int);
template void caffe_gpu_pyramid2<float,true>(const int, const float *, const int, const int, float *, const int);
template void caffe_gpu_pyramid2<double,false>(const int, const double *, const int, const int, double *, const int);
template void caffe_gpu_pyramid2<double,true>(const int, const double *, const int, const int, double *, const int);
} // namespace caffe
6.在RefineDet/src/caffe/proto下添加文件caffe.proto,添加代碼如下:
在message LayerParameter{}中末尾添加 optional InterpParameter interp_param = 20001; 數字可自己調整,但不能與其他數字重複
另外添加函數
message InterpParameter {
optional int32 height = 1 [default = 0]; // Height of output
optional int32 width = 2 [default = 0]; // Width of output
optional int32 zoom_factor = 3 [default = 1]; // zoom factor
optional int32 shrink_factor = 4 [default = 1]; // shrink factor
optional int32 pad_beg = 5 [default = 0]; // padding at begin of input
optional int32 pad_end = 6 [default = 0]; // padding at end of input
optional bool use_blob_size = 7 [default = false];// whether use another blob's size to resize the first one
}
7.編譯
1. 編譯cafe.proto
protoc --version //確定protobuf的版本
libprotoc 2.5.0
//進入RefineDet/src/caffe/proto目錄下,編譯caffe.proto
protoc -I=./ --cpp_out=./ ./caffe.proto
//查看編譯結果
ls
//會出現caffe.pb.cc caffe.pb.h caffe.proto
2. 編譯caffe
//進入caffe路徑
make clean
make -j8
make pycaffe
8. OK啦
L.Interp()調用雙線性插值函數
例如:
net['conv9_2_us'] = L.Interp(net['conv9_2'],interp_param={'height':5,'width':5})
參考:https://www.cnblogs.com/wmr95/p/8715607.html