2

I am using Caffe for deep learning. My program is in C++.

Every iteration of forward at net_->Forward(&loss);, we pass through all layers as defined in the prototxt file and how can I visualize each layer's output in C++.

Inside net.cpp file inside Caffe library, this loop iterate to forward layer by layer.

template <typename Dtype>
Dtype Net<Dtype>::ForwardFromTo(int start, int end) {
  CHECK_GE(start, 0);
  CHECK_LT(end, layers_.size());
  Dtype loss = 0;
  for (int i = start; i <= end; ++i) {
    //cout << "Forwarding " << layer_names_[i] << endl;     
    Dtype layer_loss = layers_[i]->Forward(bottom_vecs_[i], top_vecs_[i]);
    loss += layer_loss;
    if (debug_info_) { ForwardDebugInfo(i); }
  }
  return loss;
}

top_vecs_[i] is output of each layer and how can I visualize it?

Shai
  • 111,146
  • 38
  • 238
  • 371
batuman
  • 7,066
  • 26
  • 107
  • 229
  • what exactly do you mean by "visualize it"? a plot? an image? a number? a vector? – Shai Oct 30 '17 at 07:03
  • Have you looked at [`ForwardDebugInfo(...)` and `BackwardDebugInfo(...)`](https://github.com/BVLC/caffe/blob/master/src/caffe/net.cpp#L585-L636) functions that output some [debug info](https://stackoverflow.com/q/40510706/1714410) for each layer? – Shai Oct 30 '17 at 07:05
  • @Shai yes I like to print image. Yes good idea. I think I can plot blobs there. Let me try. – batuman Oct 30 '17 at 07:10

1 Answers1

0

According to Shai's suggestion, what I did is as follow inside ForwardDebugInfo().

for (int top_id = 0; top_id < top_vecs_[layer_id].size(); ++top_id) {
        Blob<Dtype>& blob = *top_vecs_[layer_id][top_id];    
        const string& blob_name = blob_names_[top_id_vecs_[layer_id][top_id]];
            string name = blob_name;
            for (int i = 0; i < name.length(); ++i) {
               if (name[i] == '/')
                   name[i] = '_';
            }
            string foldname = "images/"+name;
            if (stat(foldname.c_str(), &st) == -1) {
                mkdir(foldname.c_str(), 0700);
            }
        //cout<<"blob_name " << blob_name << " layer_id is " << layer_id << " blob.num() " << blob.num() << " blob.channels() " << blob.channels() << " blob.height() " << blob.height() << " blob.width() " << blob.width() << endl;

        ///////Plotting output of individual layer
        if(blob.height()>1 && blob.width()>1){
            cv::Size ss(blob.width(), blob.height());
            Dtype* data = blob.mutable_cpu_data();
            for(int k=0; k < blob.channels(); k++)
            {
              cv::Mat channel(ss, CV_32FC1, data);
              stringstream s;
              s << k;
              cv::imwrite(foldname+"/"+s.str()+".jpg",channel*255.0);         
              channel.release();
              data += ss.area();
            }
        }


       // mainImg.release();

        /////////////////////////////////////////
        const Dtype data_abs_val_mean = blob.asum_data() / blob.count();
        LOG_IF(INFO, Caffe::root_solver())
            << "    [Forward] "
            << "Layer " << layer_names_[layer_id]
            << ", top blob " << blob_name
            << " data: " << data_abs_val_mean;
  }
batuman
  • 7,066
  • 26
  • 107
  • 229