-
-
Save helxsz/1cd4065fc182ca8fbedf to your computer and use it in GitHub Desktop.
A function to load CSV data into a caffe datum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool ReadCSVToDatum(const string& filename, Datum* datum) { | |
// read in the CSV file into a vector | |
fstream file(filename.c_str(), ios::in); | |
vector<vector<int> > label; | |
std::string line; | |
while (std::getline(file, line)) { | |
// replace commas with spaces | |
for (int i = 0; i < line.length(); i++) { | |
if (line[i] == ',') | |
line[i] = ' '; | |
} | |
// get row from line | |
std::stringstream ss(line); | |
int temp; | |
vector<int> row; | |
while (ss >> temp) | |
row.push_back(temp); | |
// push row into label | |
label.push_back(row); | |
} | |
file.close(); | |
int num_channels = 1; | |
int num_rows = label.size(); | |
int num_cols = label[0].size(); | |
datum->set_channels(num_channels); | |
datum->set_height(num_rows); | |
datum->set_width(num_cols); | |
datum->clear_data(); | |
datum->clear_float_data(); | |
datum->set_encoded(false); | |
int datum_channels = datum->channels(); | |
int datum_height = datum->height(); | |
int datum_width = datum->width(); | |
int datum_size = datum_channels * datum_height * datum_width; | |
//LOG(INFO) << "c: " << datum_channels << ", rows: " << datum_height << ", cols: " << datum_width; | |
std::string buffer(datum_size, ' '); | |
for (int h = 0; h < datum_height; ++h) { | |
for (int w = 0; w < datum_width; ++w) { | |
int datum_index = h * datum_width + w; | |
//LOG(INFO) << datum_index << ":: " << label[h][w]; | |
buffer[datum_index] = static_cast<char>(label[h][w]); | |
} | |
} | |
datum->set_data(buffer); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment