caffe研究net_->Forward();/*网络前向传播*/

net_->Forward();/网络前向传播:计算出该测试图像属于哪个每个类别的概率也就是最终的输出层/

net_->Forward();/*网络前向传播:计算出该测试图像属于哪个每个类别的概率也就是最终的输出层*/

template <typename Dtype>
const vector*>& Net::Forward(Dtype* loss) 
{
  if (loss != NULL) 
  {
    *loss = ForwardFromTo(0, layers_.size() - 1);
  } 
  else 
  {
    ForwardFromTo(0, layers_.size() - 1);
  }
  return net_output_blobs_;
}
template <typename Dtype>
Dtype Net::ForwardFromTo(int start, int end) 
{
  CHECK_GE(start, 0);
  CHECK_LT(end, layers_.size());
  Dtype loss = 0;
  for (int i = start; i <= end; ++i)//start=0,end=244共245层网络
  {
    // LOG(ERROR) << "Forwarding " << layer_names_[i];
    Dtype layer_loss = layers_[i]->Forward(bottom_vecs_[i], top_vecs_[i]);
    loss += layer_loss;
    if (debug_info_) 
    { 
        ForwardDebugInfo(i); 
    }
  }
  return loss;
}
// Forward and backward wrappers. You should implement the cpu and
// gpu specific implementations instead, and should not change these
// functions.
template <typename Dtype>
inline Dtype Layer::Forward(const vector*>& bottom,
    const vector*>& top) {
  // Lock during forward to ensure sequential forward
  Lock();
  Dtype loss = 0;
  Reshape(bottom, top);
  switch (Caffe::mode()) {
  case Caffe::CPU:
    Forward_cpu(bottom, top);
    for (int top_id = 0; top_id < top.size(); ++top_id) {
      if (!this->loss(top_id)) { continue; }
      const int count = top[top_id]->count();
      const Dtype* data = top[top_id]->cpu_data();
      const Dtype* loss_weights = top[top_id]->cpu_diff();
      loss += caffe_cpu_dot(count, data, loss_weights);
    }
    break;
  case Caffe::GPU:
    Forward_gpu(bottom, top);
#ifndef CPU_ONLY
    for (int top_id = 0; top_id < top.size(); ++top_id) {
      if (!this->loss(top_id)) { continue; }
      const int count = top[top_id]->count();
      const Dtype* data = top[top_id]->gpu_data();
      const Dtype* loss_weights = top[top_id]->gpu_diff();
      Dtype blob_loss = 0;
      caffe_gpu_dot(count, data, loss_weights, &blob_loss);
      loss += blob_loss;
    }
#endif
    break;
  default:
    LOG(FATAL) << "Unknown caffe mode.";
  }
  Unlock();
  return loss;
}

参考资料:
http://blog.csdn.net/mounty_fsc/article/details/51092906(Caffe,LeNet)前向计算(五)

你可能感兴趣的:(caffe研究net_->Forward();/*网络前向传播*/)