Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A function to load CSV data into a caffe datum
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
You can’t perform that action at this time.