test code for PETA datasets ....

 #ifdef WITH_PYTHON_LAYER
#include "boost/python.hpp"
namespace bp = boost::python;
#endif #include <glog/logging.h> #include <cstring>
#include <map>
#include <string>
#include <vector> #include "boost/algorithm/string.hpp"
#include "caffe/caffe.hpp"
#include "caffe/util/signal_handler.h"
#include <fstream>
#include <sstream>
#include <iostream> using caffe::Blob;
using caffe::Caffe;
using caffe::Net;
using caffe::Layer;
using caffe::Solver;
using caffe::shared_ptr;
using caffe::string;
using caffe::Timer;
using caffe::vector;
using std::ostringstream;
using namespace std; DEFINE_string(gpu, "",
"Optional; run in GPU mode on given device IDs separated by ','."
"Use '-gpu all' to run on all available GPUs. The effective training "
"batch size is multiplied by the number of devices.");
DEFINE_string(solver, "",
"The solver definition protocol buffer text file.");
DEFINE_string(model, "",
"The model definition protocol buffer text file..");
DEFINE_string(snapshot, "",
"Optional; the snapshot solver state to resume training.");
DEFINE_string(weights, "",
"Optional; the pretrained weights to initialize finetuning, "
"separated by ','. Cannot be set simultaneously with snapshot.");
// DEFINE_int32(iteratinos, 29329,
DEFINE_int32(iterations, ,
"The number of iterations to run.");
DEFINE_string(sigint_effect, "stop",
"Optional; action to take when a SIGINT signal is received: "
"snapshot, stop or none.");
DEFINE_string(sighup_effect, "snapshot",
"Optional; action to take when a SIGHUP signal is received: "
"snapshot, stop or none."); // A simple registry for caffe commands.
typedef int (*BrewFunction)();
typedef std::map<caffe::string, BrewFunction> BrewMap;
BrewMap g_brew_map; #define RegisterBrewFunction(func) \
namespace { \
class __Registerer_##func { \
public: /* NOLINT */ \
__Registerer_##func() { \
g_brew_map[#func] = &func; \
} \
}; \
__Registerer_##func g_registerer_##func; \
} static BrewFunction GetBrewFunction(const caffe::string& name) {
if (g_brew_map.count(name)) {
return g_brew_map[name];
} else {
LOG(ERROR) << "Available caffe actions:";
for (BrewMap::iterator it = g_brew_map.begin();
it != g_brew_map.end(); ++it) {
LOG(ERROR) << "\t" << it->first;
}
LOG(FATAL) << "Unknown action: " << name;
return NULL; // not reachable, just to suppress old compiler warnings.
}
} // Parse GPU ids or use all available devices
static void get_gpus(vector<int>* gpus) {
if (FLAGS_gpu == "all") {
int count = ;
#ifndef CPU_ONLY
CUDA_CHECK(cudaGetDeviceCount(&count));
#else
NO_GPU;
#endif
for (int i = ; i < count; ++i) {
gpus->push_back(i);
}
} else if (FLAGS_gpu.size()) {
vector<string> strings;
boost::split(strings, FLAGS_gpu, boost::is_any_of(","));
for (int i = ; i < strings.size(); ++i) {
gpus->push_back(boost::lexical_cast<int>(strings[i]));
}
} else {
CHECK_EQ(gpus->size(), );
}
} // caffe commands to call by
// caffe <command> <args>
//
// To add a command, define a function "int command()" and register it with
// RegisterBrewFunction(action); // Device Query: show diagnostic information for a GPU device.
int device_query() {
LOG(INFO) << "Querying GPUs " << FLAGS_gpu;
vector<int> gpus;
get_gpus(&gpus);
for (int i = ; i < gpus.size(); ++i) {
caffe::Caffe::SetDevice(gpus[i]);
caffe::Caffe::DeviceQuery();
}
return ;
}
RegisterBrewFunction(device_query); // Load the weights from the specified caffemodel(s) into the train and
// test nets.
void CopyLayers(caffe::Solver<float>* solver, const std::string& model_list) {
std::vector<std::string> model_names;
boost::split(model_names, model_list, boost::is_any_of(",") );
for (int i = ; i < model_names.size(); ++i) {
LOG(INFO) << "Finetuning from " << model_names[i];
solver->net()->CopyTrainedLayersFrom(model_names[i]);
for (int j = ; j < solver->test_nets().size(); ++j) {
solver->test_nets()[j]->CopyTrainedLayersFrom(model_names[i]);
}
}
} // Translate the signal effect the user specified on the command-line to the
// corresponding enumeration.
caffe::SolverAction::Enum GetRequestedAction(
const std::string& flag_value) {
if (flag_value == "stop") {
return caffe::SolverAction::STOP;
}
if (flag_value == "snapshot") {
return caffe::SolverAction::SNAPSHOT;
}
if (flag_value == "none") {
return caffe::SolverAction::NONE;
}
LOG(FATAL) << "Invalid signal effect \""<< flag_value << "\" was specified";
} // Train / Finetune a model.
int train() {
CHECK_GT(FLAGS_solver.size(), ) << "Need a solver definition to train.";
CHECK(!FLAGS_snapshot.size() || !FLAGS_weights.size())
<< "Give a snapshot to resume training or weights to finetune "
"but not both."; caffe::SolverParameter solver_param;
caffe::ReadProtoFromTextFileOrDie(FLAGS_solver, &solver_param); // If the gpus flag is not provided, allow the mode and device to be set
// in the solver prototxt.
if (FLAGS_gpu.size() ==
&& solver_param.solver_mode() == caffe::SolverParameter_SolverMode_GPU) {
if (solver_param.has_device_id()) {
FLAGS_gpu = "" +
boost::lexical_cast<string>(solver_param.device_id());
} else { // Set default GPU if unspecified
FLAGS_gpu = "" + boost::lexical_cast<string>();
}
} vector<int> gpus;
get_gpus(&gpus);
if (gpus.size() == ) {
LOG(INFO) << "Use CPU.";
Caffe::set_mode(Caffe::CPU);
} else {
ostringstream s;
for (int i = ; i < gpus.size(); ++i) {
s << (i ? ", " : "") << gpus[i];
}
LOG(INFO) << "Using GPUs " << s.str(); solver_param.set_device_id(gpus[]);
Caffe::SetDevice(gpus[]);
Caffe::set_mode(Caffe::GPU);
Caffe::set_solver_count(gpus.size());
} caffe::SignalHandler signal_handler(
GetRequestedAction(FLAGS_sigint_effect),
GetRequestedAction(FLAGS_sighup_effect)); shared_ptr<caffe::Solver<float> >
solver(caffe::GetSolver<float>(solver_param)); solver->SetActionFunction(signal_handler.GetActionFunction()); if (FLAGS_snapshot.size()) {
LOG(INFO) << "Resuming from " << FLAGS_snapshot;
solver->Restore(FLAGS_snapshot.c_str());
} else if (FLAGS_weights.size()) {
CopyLayers(solver.get(), FLAGS_weights);
} if (gpus.size() > ) {
caffe::P2PSync<float> sync(solver, NULL, solver->param());
sync.run(gpus);
} else {
LOG(INFO) << "Starting Optimization";
solver->Solve();
}
LOG(INFO) << "Optimization Done.";
return ;
}
RegisterBrewFunction(train); // Test: score a model.
int test() {
CHECK_GT(FLAGS_model.size(), ) << "Need a model definition to score.";
CHECK_GT(FLAGS_weights.size(), ) << "Need model weights to score."; // Set device id and mode
vector<int> gpus;
get_gpus(&gpus);
if (gpus.size() != ) {
LOG(INFO) << "Use GPU with device ID " << gpus[];
Caffe::SetDevice(gpus[]);
Caffe::set_mode(Caffe::GPU);
} else {
LOG(INFO) << "Use CPU.";
Caffe::set_mode(Caffe::CPU);
} // Instantiate the caffe net.
Net<float> caffe_net(FLAGS_model, caffe::TEST);
caffe_net.CopyTrainedLayersFrom(FLAGS_weights);
LOG(INFO) << "Running for " << FLAGS_iterations << " iterations."; vector<Blob<float>* > bottom_vec;
vector<int> test_score_output_id;
vector<float> test_score;
// float loss = 0;
int nu = ;
// int num = 0;
// int num2 = 0; const int att_num = ;
static int TP[att_num] = {};
static int TN[att_num] = {};
static int FP[att_num] = {};
static int FN[att_num] = {}; for (int i = ; i < FLAGS_iterations; ++i) { // num of images; test image:7600 LOG(INFO) << "batch " << i << "/" << FLAGS_iterations << ", waiting..."; float iter_loss;
const vector<Blob<float>*>& result =
caffe_net.Forward(bottom_vec, &iter_loss); // outPut result LOG(INFO) << "result.size: " << result.size() << " " << result[]->count() << " " << result[]->count()
<< " " << result[]->count() ; const float* result_index = result[]->cpu_data(); // index
const float* result_score = result[]->cpu_data(); // predict score;
const float* result_label = result[]->cpu_data(); // Groundtruth label; for (int k = ; k < att_num; ++k) { // for 35 attributes // const float index_ = result_index;
const float Predict_score = result_score[k];
const float GT_label = result_label[k]; float threshold_ = 0.4;
if ((Predict_score < threshold_) && (int(GT_label) == ))
TN[k] = TN[k] + ;
if ((Predict_score > threshold_) && (int(GT_label) == ))
FP[k] = FP[k] + ;
if ((Predict_score < threshold_) && (int(GT_label) == ))
FN[k] = FN[k] + ;
if ((Predict_score > threshold_) && (int(GT_label) == ))
TP[k] = TP[k] + ; // write the predicted score into txt files
ofstream file("/home/wangxiao/Downloads/whole_benchmark/Sec_Batch_/sec_Batch_unlabel.txt",ios::app);
if(!file) return -; if(nu < att_num){
file << Predict_score << " " ;
nu++;
} else {
nu = ;
file << endl;
file << Predict_score << " " ;
}
file.close(); // // write the Index into txt files
// ofstream file2("/home/wangxiao/Downloads/caffe-master/wangxiao/bvlc_alexnet/43_attributes_index.txt",ios::app);
// if(!file2) return -1; // if(num < att_num){
// file2 << index_ << " " ;
// num++;
// } else {
// num = 1;
// file2 << endl;
// file2 << index_ << " " ;
// }
// file2.close(); // // write the GroundTruth Label into txt files
// ofstream file3("/home/wangxiao/Downloads/whole_benchmark/Unlabeled_data/Unlabeled_AnHui_label.txt",ios::app);
// if(!file3) return -1; // if(num2 < att_num){
// file3 << GT_label << " " ;
// num2++;
// } else {
// num2 = 1;
// file3 << endl;
// file3 << GT_label << " " ;
// }
// file3.close(); }
} double aver_accuracy = ; for (int k = ; k < att_num; ++k){
aver_accuracy += 0.5*(double(TP[k])/(TP[k]+FN[k]) + double(TN[k])/(TN[k] + FP[k]));
LOG(INFO) << "Accuracy: " << k << " "
<< 0.5*(double(TP[k])/(TP[k]+FN[k]) + double(TN[k])/(TN[k] + FP[k]));
LOG(INFO) << "TP = " << double(TP[k]);
LOG(INFO) << "FN = " << double(FN[k]);
LOG(INFO) << "FP = " << double(FP[k]);
LOG(INFO) << "TN = " << double(TN[k]);
} LOG(INFO) << "####################";
LOG(INFO) << " ";
LOG(INFO) << "Average_accuracy: " << aver_accuracy / att_num;
LOG(INFO) << " ";
LOG(INFO) << "####################"; return ;
} RegisterBrewFunction(test); // Time: benchmark the execution time of a model.
int time() {
CHECK_GT(FLAGS_model.size(), ) << "Need a model definition to time."; // Set device id and mode
vector<int> gpus;
get_gpus(&gpus);
if (gpus.size() != ) {
LOG(INFO) << "Use GPU with device ID " << gpus[];
Caffe::SetDevice(gpus[]);
Caffe::set_mode(Caffe::GPU);
} else {
LOG(INFO) << "Use CPU.";
Caffe::set_mode(Caffe::CPU);
}
// Instantiate the caffe net.
Net<float> caffe_net(FLAGS_model, caffe::TRAIN); // Do a clean forward and backward pass, so that memory allocation are done
// and future iterations will be more stable.
LOG(INFO) << "Performing Forward";
// Note that for the speed benchmark, we will assume that the network does
// not take any input blobs.
float initial_loss;
caffe_net.Forward(vector<Blob<float>*>(), &initial_loss);
LOG(INFO) << "Initial loss: " << initial_loss;
LOG(INFO) << "Performing Backward";
caffe_net.Backward(); const vector<shared_ptr<Layer<float> > >& layers = caffe_net.layers();
const vector<vector<Blob<float>*> >& bottom_vecs = caffe_net.bottom_vecs();
const vector<vector<Blob<float>*> >& top_vecs = caffe_net.top_vecs();
const vector<vector<bool> >& bottom_need_backward =
caffe_net.bottom_need_backward();
LOG(INFO) << "*** Benchmark begins ***";
LOG(INFO) << "Testing for " << FLAGS_iterations << " iterations.";
Timer total_timer;
total_timer.Start();
Timer forward_timer;
Timer backward_timer;
Timer timer;
std::vector<double> forward_time_per_layer(layers.size(), 0.0);
std::vector<double> backward_time_per_layer(layers.size(), 0.0);
double forward_time = 0.0;
double backward_time = 0.0;
for (int j = ; j < FLAGS_iterations; ++j) {
Timer iter_timer;
iter_timer.Start();
forward_timer.Start();
for (int i = ; i < layers.size(); ++i) {
timer.Start();
layers[i]->Forward(bottom_vecs[i], top_vecs[i]);
forward_time_per_layer[i] += timer.MicroSeconds();
}
forward_time += forward_timer.MicroSeconds();
backward_timer.Start();
for (int i = layers.size() - ; i >= ; --i) {
timer.Start();
layers[i]->Backward(top_vecs[i], bottom_need_backward[i],
bottom_vecs[i]);
backward_time_per_layer[i] += timer.MicroSeconds();
}
backward_time += backward_timer.MicroSeconds();
LOG(INFO) << "Iteration: " << j + << " forward-backward time: "
<< iter_timer.MilliSeconds() << " ms.";
}
LOG(INFO) << "Average time per layer: ";
for (int i = ; i < layers.size(); ++i) {
const caffe::string& layername = layers[i]->layer_param().name();
LOG(INFO) << std::setfill(' ') << std::setw() << layername <<
"\tforward: " << forward_time_per_layer[i] / /
FLAGS_iterations << " ms.";
LOG(INFO) << std::setfill(' ') << std::setw() << layername <<
"\tbackward: " << backward_time_per_layer[i] / /
FLAGS_iterations << " ms.";
}
total_timer.Stop();
LOG(INFO) << "Average Forward pass: " << forward_time / /
FLAGS_iterations << " ms.";
LOG(INFO) << "Average Backward pass: " << backward_time / /
FLAGS_iterations << " ms.";
LOG(INFO) << "Average Forward-Backward: " << total_timer.MilliSeconds() /
FLAGS_iterations << " ms.";
LOG(INFO) << "Total Time: " << total_timer.MilliSeconds() << " ms.";
LOG(INFO) << "*** Benchmark ends ***";
return ;
}
RegisterBrewFunction(time); int main(int argc, char** argv) {
// Print output to stderr (while still logging).
FLAGS_alsologtostderr = ;
// Usage message.
gflags::SetUsageMessage("command line brew\n"
"usage: caffe <command> <args>\n\n"
"commands:\n"
" train train or finetune a model\n"
" test score a model\n"
" device_query show GPU diagnostic information\n"
" time benchmark model execution time");
// Run tool or show usage.
caffe::GlobalInit(&argc, &argv);
if (argc == ) {
#ifdef WITH_PYTHON_LAYER
try {
#endif
return GetBrewFunction(caffe::string(argv[]))();
#ifdef WITH_PYTHON_LAYER
} catch (bp::error_already_set) {
PyErr_Print();
return ;
}
#endif
} else {
gflags::ShowUsageWithFlagsRestrict(argv[], "tools/caffe");
}
}

caffe: test code for PETA dataset的更多相关文章

  1. caffe: test code for Deep Learning approach

    #include <stdio.h> // for snprintf #include <string> #include <vector> #include &q ...

  2. caffe: test code Check failed: K_ == new_K (768 vs. 1024) Input size incompatible with inner product parameters.

    I0327 20:24:22.966171 20521 net.cpp:849] Copying source layer drop7I0327 20:24:22.966179 20521 net.c ...

  3. caffe: test code 执行出问题: Check failed: FLAGS_weights.size() > 0 (0 vs. 0) Need model weights to score.

    Check failed: FLAGS_weights.size() > 0 (0 vs. 0) Need model weights to score. 出现这个错误,但是我记得昨天还好好的, ...

  4. SSRS 通过Customer Code访问Dataset

    A dataset in Reporting Services is not the same type of object as an ADO.Net dataset.  A report data ...

  5. 使用caffe训练自己的CNN

    现在有这样的一个场景:给一张行人的小矩形框图片, 根据该行人的特征识别出性别. 分析: (1),行人的姿态各异,变化多端.很难提取图像的特定特征 (2),正常人肉眼判别行人的根据是身材比例,头发长度等 ...

  6. 学习笔记之 初试Caffe,Matlab接口提取feature

    Caffe 提供了matlab接口,可以用于提取图像的feature.

  7. 机器学习caffe环境搭建——redhat7.1和caffe的python接口编译

    相信看这篇文章的都知道caffe是干嘛的了,无非就是深度学习.神经网络.计算机视觉.人工智能这些,这个我就不多介绍了,下面说说我的安装过程即遇到的问题,当然还有解决方法. 说下我的环境:1>虚拟 ...

  8. {Reship}{Code}{CV}

    UIUC的Jia-Bin Huang同学收集了很多计算机视觉方面的代码,链接如下: https://netfiles.uiuc.edu/jbhuang1/www/resources/vision/in ...

  9. Client Dataset Basics

    文章出处:  http://www.informit.com/articles/article.aspx?p=24094 In the preceding two chapters, I discus ...

随机推荐

  1. Struts2 和 spring mvc的 迭代标签常用属性对比

    <s:iterator value="#users" var="u" status="st"> <c:forEach  i ...

  2. C++11的new concepts (move semantic)

    MoveConstructible 和MoveAssignable MoveConstructible Specifies that an instance of the type can be mo ...

  3. WPF Step By Step 控件介绍

    WPF Step By Step 控件介绍 回顾 上一篇,我们主要讨论了WPF的几个重点的基本知识的介绍,本篇,我们将会简单的介绍几个基本控件的简单用法,本文会举几个项目中的具体的例子,结合这些 例子 ...

  4. SQL 存储过程中in

    存储过程中用in,如果将条件(1,2,3)这样的集合当成参数传进来的话是不能执行的,因为集合转成一个变量是出错 解决办法拼接SQL字符串传进来,后者在存过中拼接字符串都可以如: ALTER PROCE ...

  5. 中文圣经 for Android

    中文圣经(For Android) 目前,中文圣经App包含了如下圣经版本: 和合本 现代中文译本 吕振中译本 中文新译本 英文标准本(ESV) King James Version(KJV) New ...

  6. C# subString的理解

    public void TestMethod1()        {            string str = "ABCDEFGHIJKLMN"; string result ...

  7. SecureCRT rz和sz命令不可用,安装lrzsz

    1.从网站下载 lrzsz-x.xx.xx.tar.gz2.解压文件[root@localhost src]# tar zxvf lrzsz-0.12.20.tar.gz3.安装[root@local ...

  8. Python 如何跳出多重循环

    Python 如何跳出多重循环 抛异常 return

  9. QQ截图取色方法

    转自:http://www.oicqzone.com/qqjiqiao/2014110920194.html ctrl+alt+a截图的时候,会显示RGB值.是的,你也许会想,但是我要的是#RRGGB ...

  10. Qt之qSetMessagePattern

    简述 改变默认的消息处理输出. 允许改变qDebug().qWarning().qCritical().qFatal()的输出. 简述 占位符 示例 qSetMessagePattern QT_MES ...