利用Caffe做迴歸(regression)、多標籤訓練_caffe

來源:互聯網
上載者:User

迴歸:

轉載網址:

http://www.cnblogs.com/frombeijingwithlove/p/5314042.html


看了很多,覺得這個還可以,推薦使用。

按照作者方法可以實現,算是一個基礎模組,之後其他可以自行延伸。


多標籤:

轉載網址:

1、修改源碼:http://blog.csdn.net/hubin232/article/details/50960201  按照博文方法比較固定,不太靈活。

2、分別輸入資料和標籤的lmdb格式資料:http://blog.csdn.net/hyman_yx/article/details/51791136

      裡面的標籤儲存和資料存放區方法可以改一下,代碼如下:

int main(int argc, char** argv) {#ifdef USE_OPENCV::google::InitGoogleLogging(argv[0]);// Print output to stderr (while still logging)FLAGS_alsologtostderr = 1;#ifndef GFLAGS_GFLAGS_H_namespace gflags = google;#endifgflags::SetUsageMessage("Convert a set of images to the leveldb/lmdb\n""format used as input for Caffe.\n""Usage:\n""    convert_imageset [FLAGS] ROOTFOLDER/ LISTFILE DB_NAME\n""The ImageNet dataset for the training demo is at\n""    http://www.image-net.org/download-images\n");gflags::ParseCommandLineFlags(&argc, &argv, true);if (argc < 6) {gflags::ShowUsageWithFlagsRestrict(argv[0], "tools/convert_imageset");return 1;}const bool is_color = !FLAGS_gray;const bool check_size = FLAGS_check_size;const bool encoded = FLAGS_encoded;const string encode_type = FLAGS_encode_type;std::ifstream infile(argv[2]);std::vector<std::pair<std::string, std::vector<float>> > lines;std::string filename;std::string label_count_string = argv[5];int label_count = std::atoi(label_count_string.c_str());std::vector<float> label(label_count);while (infile >> filename) {for (int i = 0; i < label_count;i++){infile >> label[i];}lines.push_back(std::make_pair(filename, label));}if (FLAGS_shuffle) {// randomly shuffle dataLOG(INFO) << "Shuffling data";shuffle(lines.begin(), lines.end());}LOG(INFO) << "A total of " << lines.size() << " images.";if (encode_type.size() && !encoded)LOG(INFO) << "encode_type specified, assuming encoded=true.";int resize_height = std::max<int>(0, FLAGS_resize_height);int resize_width = std::max<int>(0, FLAGS_resize_width);// Create new DBscoped_ptr<db::DB> db_image(db::GetDB(FLAGS_backend));scoped_ptr<db::DB> db_label(db::GetDB(FLAGS_backend));db_image->Open(argv[3], db::NEW);db_label->Open(argv[4], db::NEW);scoped_ptr<db::Transaction> txn_image(db_image->NewTransaction());scoped_ptr<db::Transaction> txn_label(db_label->NewTransaction());// Storing to dbstd::string root_folder(argv[1]);Datum datum_label;Datum datum_image;int count = 0;int data_size_label = 0;int data_size_image = 0;bool data_size_initialized = false;for (int line_id = 0; line_id < lines.size(); ++line_id) {bool status;std::string enc = encode_type;if (encoded && !enc.size()) {// Guess the encoding type from the file namestring fn = lines[line_id].first;size_t p = fn.rfind('.');if (p == fn.npos)LOG(WARNING) << "Failed to guess the encoding of '" << fn << "'";enc = fn.substr(p);std::transform(enc.begin(), enc.end(), enc.begin(), ::tolower);}status = ReadImageToDatum(root_folder + lines[line_id].first,lines[line_id].second[0], resize_height, resize_width, is_color,enc, &datum_image);if (status == false) continue;datum_label.set_height(1);datum_label.set_width(1);datum_label.set_channels(label_count);int count_tmp = datum_label.float_data_size();for (int index_label = 0; index_label < lines[line_id].second.size(); index_label++){float tmp_float_value = lines[line_id].second[index_label];datum_label.add_float_data(tmp_float_value);}if (check_size) {if (!data_size_initialized) {data_size_label = datum_label.channels() * datum_label.height() * datum_label.width();data_size_image = datum_image.channels() * datum_image.height() * datum_image.width();data_size_initialized = true;}else {const std::string& data_label = datum_label.data();CHECK_EQ(data_label.size(), data_size_label) << "Incorrect data field size "<< data_label.size();const std::string& data_image = data_image.data();CHECK_EQ(data_image.size(), data_size_image) << "Incorrect data field size "<< data_image.size();}}// sequentialstring key_str_image = caffe::format_int(line_id, 8) + "_" + lines[line_id].first;string key_str_label = caffe::format_int(line_id, 8) + "label_" + lines[line_id].first;// Put in dbstring out_label;string out_image;CHECK(datum_label.SerializeToString(&out_label));CHECK(datum_image.SerializeToString(&out_image));datum_label.clear_float_data();txn_label->Put(key_str_label, out_label);txn_image->Put(key_str_image, out_image);if (++count % 1000 == 0) {// Commit dbtxn_image->Commit();txn_image.reset(db_image->NewTransaction());txn_label->Commit();txn_label.reset(db_label->NewTransaction());LOG(INFO) << "Processed " << count << " files.";}}// write the last batchif (count % 1000 != 0) {txn_label->Commit();txn_image->Commit();LOG(INFO) << "Processed " << count << " files.";}#elseLOG(FATAL) << "This tool requires OpenCV; compile with USE_OPENCV.";#endif  // USE_OPENCVreturn 0;}

      注意一點,train和val的資料一定不能是一樣的,因為有限於caffe的資料讀取方式,不然會無法讀取資料。

      prototxt和網址內的寫法一樣即可。





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.