gpt4 book ai didi

c++ - 如何在 C++ 中可视化 Caffe 深度学习过程的各个层输出?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:42:26 26 4
gpt4 key购买 nike

我正在使用 Caffe用于深度学习。我的程序是用 C++ 编写的。

net_->Forward(&loss); 中 forward 的每次迭代,我们都会按照 prototxt 文件中定义的方式遍历所有层,以及如何在 C++ 中可视化每一层的输出。

Caffe库中的net.cpp文件中,这个循环逐层迭代。

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] 是每一层的输出 我如何可视化它?

最佳答案

根据Shai的建议,我在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;
}

关于c++ - 如何在 C++ 中可视化 Caffe 深度学习过程的各个层输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47009303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com