- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我试图理解用 C++ 版本的 Caffe 编写的特定代码,以便将其移植到 Python 版本的 Keras 中。
很明显,Caffe中的layer可以定义如下例子:
template <typename Dtype>
void ROIPoolingLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
其中 bottom 是一个接受输入的一维数组,top 是一个产生输出的一维数组。
然后不久之后,已经使用 bottom
vector 设置了几个参数:
template <typename Dtype>
void ROIPoolingLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
ROIPoolingParameter roi_pool_param = this->layer_param_.roi_pooling_param();
CHECK_GT(roi_pool_param.pooled_h(), 0)
<< "pooled_h must be > 0";
CHECK_GT(roi_pool_param.pooled_w(), 0)
<< "pooled_w must be > 0";
pooled_height_ = roi_pool_param.pooled_h();
pooled_width_ = roi_pool_param.pooled_w();
spatial_scale_ = roi_pool_param.spatial_scale();
LOG(INFO) << "Spatial scale: " << spatial_scale_;
}
template <typename Dtype>
void ROIPoolingLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
channels_ = bottom[0]->channels();
height_ = bottom[0]->height();
width_ = bottom[0]->width();
top[0]->Reshape(bottom[1]->num(), channels_, pooled_height_,
pooled_width_);
max_idx_.Reshape(bottom[1]->num(), channels_, pooled_height_,
pooled_width_);
}
如果我们进一步扩展代码,他们会使用 cpu_data
方法:
template <typename Dtype>
void ROIPoolingLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[0]->cpu_data();
const Dtype* bottom_rois = bottom[1]->cpu_data();
来自 Caffe 文档:
As we are often interested in the values as well as the gradients of the blob, a Blob stores two chunks of memories, data and diff. The former is the normal data that we pass along, and the latter is the gradient computed by the network.
Further, as the actual values could be stored either on the CPU and on the GPU, there are two different ways to access them: the const way, which does not change the values, and the mutable way, which changes the values:
const Dtype* cpu_data() const; Dtype* mutable_cpu_data();
那么根据上面的描述,在上面最近的代码块中定义的 bottom_data[0].cpu_data()
是否只是一个存储在 CPU 寄存器中的数组,其中包含输入数据和关于误差的偏导数?如果是这样,我怎么能在 Keras 中复制这样的代码?它在 Keras 中是否更重要(该层已经被评估或只是一个空的形状)?
谢谢!
最佳答案
bottom_data[0].cpu_data()
是方法,它将返回一个指向第一个输入 blob 内存的常量指针。如有必要,数据将首先从 GPU 内存中复制。
您不需要在 Keras 中对如此低级的概念进行操作。
查看 Keras example 中的这段代码:
def call(self, x):
return K.dot(x, self.kernel)
此处返回输入张量与层内核之间的点积结果。
与 Caffe 不同的是,在 Keras 中,您(大部分)在张量上定义操作,而不是在内存数组上。在运行 session 时,张量将填充有关执行时间的实际数据。 Keras 后端将处理执行 K.dot
操作(也返回一个张量)所需的所有内存操作。
此外,您还可以选择用于放置张量的设备:FAQ .同样,Keras 将在幕后执行所有必要的操作。
关于python - Keras 相当于 Caffe 的 'cpu_data()' 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54372265/
例子 我试图理解用 C++ 版本的 Caffe 编写的特定代码,以便将其移植到 Python 版本的 Keras 中。 很明显,Caffe中的layer可以定义如下例子: template void
我是一名优秀的程序员,十分优秀!