生成日志
打开Caffe,找到src/common.cpp文件
1、 添加头文件
#include <boost/date_time.hpp>
#include <process.h>
#include <direct.h>
2、 添加生成日志的函数(定义在void GlobalInit(int* pargc, char*** pargv)之前)
void initGlog() {
FLAGS_log_dir = ".\\log\\";//存放日志文件的文件夹路径,我们可以自己指定
_mkdir(FLAGS_log_dir.c_str());
std::string LOG_INFO_FILE;
std::string LOG_WARNING_FILE;
std::string LOG_ERROR_FILE;
std::string LOG_FATAL_FILE;
std::string now_time = boost::posix_time::to_iso_extended_string(boost::posix_time::second_clock::local_time());
now_time[13] = '-';
now_time[16] = '-';
LOG_INFO_FILE = FLAGS_log_dir + "INFO" + now_time + ".log";
google::SetLogDestination(google::GLOG_INFO, LOG_INFO_FILE.c_str());
LOG_WARNING_FILE = FLAGS_log_dir + "WARNING" + now_time + ".txt";
google::SetLogDestination(google::GLOG_WARNING, LOG_WARNING_FILE.c_str());
LOG_ERROR_FILE = FLAGS_log_dir + "ERROR" + now_time + ".txt";
google::SetLogDestination(google::GLOG_ERROR, LOG_ERROR_FILE.c_str());
LOG_FATAL_FILE = FLAGS_log_dir + "FATAL" + now_time + ".txt";
google::SetLogDestination(google::GLOG_FATAL, LOG_FATAL_FILE.c_str());
}
3、 在void GlobalInit(int* pargc, char*** pargv)函数中调用2中定义的子函数,然后重新编译
void GlobalInit(int* pargc, char*** pargv) {
// Google flags.
::gflags::ParseCommandLineFlags(pargc, pargv, true);
// Provide a backtrace on segfault.
//::google::InstallFailureSignalHandler();
// Google logging.
initGlog();
::google::InitGoogleLogging(*(pargv)[0]);
}
根据日志生成loss曲线
1、 准备工具 caffe-master/tools/extra/parse_log.sh、caffe-master/tools/extra/extract_seconds.py和caffe-master/tools/extra/plot_training_log.py
2、 修改plot_training_log.py文件
def load_data(data_file, field_idx0, field_idx1):
data = [[], []]
with open(data_file, 'r') as f:
for line in f:
line = line.replace(',',' ')
if line[0] != 'N':
fields = line.split()
data[0].append(float(fields[field_idx0].strip()))
data[1].append(float(fields[field_idx1].strip()))
return data
def create_field_index():
train_key = 'Train'
test_key = 'Test'
field_index = {train_key:{'Iters':0, 'Seconds':1, train_key + ' learning rate':2,
train_key + ' loss':3},
test_key:{'Iters':0, 'Seconds':1, test_key + ' learning rate':2,
test_key + ' accuracy':3, test_key + ' loss':4}}
fields = set()
for data_file_type in field_index.keys():
fields = fields.union(set(field_index[data_file_type].keys()))
fields = list(fields)
fields.sort()
return field_index, fields
3、 运行代码
python plot_training_log.py num save.png LOG.log