cerebrou
cerebrou

Reputation: 5540

How to measure time of executation of each layer in CNNs

In convolutional neural network architectures for image classification (e.g. VGG or AlexNet) I would like to compare the time it takes to compute the result of each layer of the network while making a forward pass in test time (preferebly using Caffe).

In particular, I am interesteed how much time is spent on convolutional layers vs. fully connected layers.

Upvotes: 2

Views: 819

Answers (4)

Dale
Dale

Reputation: 1608

What about using class caffe::Timer in net.cpp just for your test like this:

#include "caffe/util/benchmark.hpp" //use class caffe::Timer

Dtype Net<Dtype>::ForwardFromTo(int start, int end) {
  ... //Some original contents
  Timer timer;
  for (int i = start; i <= end; ++i) {
    ...//Some original contents
    string layer_name = layers_[i]->layer_param().name(); //get layer name
    timer.Start();
    Dtype layer_loss = layers_[i]->Forward(bottom_vecs_[i], top_vecs_[i]);
    float forward_time = timer.MicroSeconds();
    LOG(ERROR) << layer_name << " consumes: " << forward_time << " microseconds during forward.";
    ...
  }
  return loss;
}

Upvotes: 0

wang
wang

Reputation: 55

so, what is you problem. caffe time did not work?

Upvotes: 0

7VoltCrayon
7VoltCrayon

Reputation: 662

Another way of doing it would be to create two networks, one which only has the convolutional layers and one which only has the dense (fully connected) layers. Do a forward pass using the convolutional network, measure its time, pass the result (output of convolutional only network) into the fully connected network, do a forward pass and measure its time.

Upvotes: 0

wang
wang

Reputation: 55

every caffe layer has a forward and backward function in src/caffe/layers dir, for example src/caffe/layers/pooling_layer.cpp is cpu implementation, src/caffe/layers/pooling_layer.cu is the gpu implementation.So you need to add a time function in the forward function in .cpp or .cu, depending on you are using cpu or gpu.

or, the simplest way, using caffe timecommand

Upvotes: -1

Related Questions