1 yolo模型转caffe模型
1.1 修改caffe源码使其支持upsample和passthrough操作
step1 修改src/caffe/proto/caffe.proto文件,在message LayerParameter中添加如下内容
optional UpsampleParameter upsample_param = 205;
optional PassThroughParameter pass_through_param = 300;
step2 修改src/caffe/proto/caffe.proto文件,添加UpsampleParameter消息
message UpsampleParameter {
// DEPRECATED. No need to specify upsampling scale factors when
// exact output shape is given by upsample_h, upsample_w parameters.
optional uint32 scale = 1 [default = 1];
// DEPRECATED. No need to specify upsampling scale factors when
// exact output shape is given by upsample_h, upsample_w parameters.
//optional uint32 scale_h = 2;
// DEPRECATED. No need to specify upsampling scale factors when
// exact output shape is given by upsample_h, upsample_w parameters.
//optional uint32 scale_w = 3;
// DEPRECATED. Specify exact output height using upsample_h. This
// parameter only works when scale is 2
//optional bool pad_out_h = 4 [default = false];
// DEPRECATED. Specify exact output width using upsample_w. This
// parameter only works when scale is 2
//optional bool pad_out_w = 5 [default = false];
//optional uint32 upsample_h = 6;
//optional uint32 upsample_w = 7;
}
step3 在include/caffe/layers 路径下添加头文件upsample_layer.hpp
#ifndef CAFFE_UPSAMPLE_LAYER_HPP_
#define CAFFE_UPSAMPLE_LAYER_HPP_
#include <vector>
#include "caffe/blob.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
namespace caffe {
template <typename Dtype>
class UpsampleLayer : public Layer<Dtype> {
public:
explicit UpsampleLayer(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 "Upsample"; }
virtual inline int MinBottomBlobs() const { return 1; }
virtual inline int MaxBottomBlobs() const { return 1; }
virtual inline int ExactNumTopBlobs() const { return 1; }
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);
private:
int scale_;
};
} // namespace caffe
#endif // CAFFE_UPSAMPLE_LAYER_HPP_
step4 在src/caffe/layers 路径下添加实现文件upsample_layer.cpp
#include <vector>
#include "caffe/layers/upsample_layer.hpp"
namespace caffe {
template <typename Dtype>
void UpsampleLayer<Dtype>::LayerSetUp(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
UpsampleParameter upsample_param = this->layer_param_.upsample_param();
scale_ = upsample_param.scale();
}
template <typename Dtype>
void UpsampleLayer<Dtype>::Reshape(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
vector<int> out_shape;
for (int i = 0; i < bottom[0]->num_axes(); i++) {
out_shape.push_back(bottom[0]->shape(i));
}
out_shape[bottom[0]->num_axes() - 1] *= scale_;
out_shape[bottom[0]->num_axes() - 2] *= scale_;
top[0]->Reshape(out_shape);
}
template <typename Dtype>
void UpsampleLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
int N = top[0]->shape(0);
int C = top[0]->shape(1);
int H = top[0]->shape(2);
int W = top[0]->shape(3);
const Dtype *input = bottom[0]->cpu_data();
Dtype *output = top[0]->mutable_cpu_data();
for (int n = 0; n < N; n++) {
for (int c = 0; c < C; c++) {
for (int h = 0; h < H; h++) {
for (int w = 0; w < W; w++) {
int nw = w/scale_;
int nh = h/scale_;
int out_idx = (((n * C + c) * H) + h) * W + w;
int in_idx = (((n * C + c) * (H / scale_)) + nh) * (W / scale_) + nw;
output[out_idx] = input[in_idx];
}
}
}
}
}
template <typename Dtype>
void UpsampleLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
int N = bottom[0]->shape(0);
int C = bottom[0]->shape(1);
int H = bottom[0]->shape(2);
int W = bottom[0]->shape(3);
const Dtype *output_grad = top[0]->cpu_diff();
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
caffe_set(bottom[0]->count(), Dtype(0), bottom_diff);
for (int n = 0; n < N; n++) {
for (int c = 0; c < C; c++) {
for (int h = 0; h < H; h++) {
for (int w = 0; w < W; w++) {
for (int i = 0; i < scale_; i++) {
for (int j = 0; j < scale_; j++) {
int nw = w * scale_ + i;
int nh = h * scale_ + j;
int out_idx = (((n * C + c) * H) + h) * W + w;
int in_idx = (((n * C + c) * (H * scale_))
+ nh) * (W * scale_) + nw;
bottom_diff[out_idx] += output_grad[in_idx];
}
}
}
}
}
}
}
#ifdef CPU_ONLY
STUB_GPU(UpsampleLayer);
#endif
INSTANTIATE_CLASS(UpsampleLayer);
REGISTER_LAYER_CLASS(Upsample);
} // namespace caffe
step5 在src/caffe/layers 路径下添加cuda实现文件upsample_layer.cu
#include <vector>
#include "caffe/filler.hpp"
#include "caffe/layers/upsample_layer.hpp"
#include "caffe/util/math_functions.hpp"
namespace caffe {
__device__ int translate_idx(int ii, int d1, int d2, int d3, int scale_factor) {
int x, y, z, w;
w = ii % d3;
ii = ii/d3;
z = ii % d2;
ii = ii/d2;
y = ii % d1;
ii = ii/d1;
x = ii;
w = w/scale_factor;
z = z/scale_factor;
d2 /= scale_factor;
d3 /= scale_factor;
return (((x*d1+y)*d2)+z)*d3+w;
}
__device__ int