| 108 | } |
| 109 | |
| 110 | void DataParser::parse_image(const std::string& name, const std::string& path) { |
| 111 | // load binary ppm/pgm |
| 112 | std::ifstream fin; |
| 113 | fin.open(path, std::ifstream::binary | std::ifstream::in); |
| 114 | mgb_assert(fin.is_open(), "open file %s failed for --input", path.c_str()); |
| 115 | |
| 116 | size_t w = 0, h = 0, channel = 0; |
| 117 | char buf[128] = {0}; |
| 118 | |
| 119 | fin.getline(buf, 128); |
| 120 | if ('5' == buf[1]) { |
| 121 | channel = 1; |
| 122 | } else if ('6' == buf[1]) { |
| 123 | channel = 3; |
| 124 | } else { |
| 125 | mgb_assert(0, "not a formal ppm/pgm"); |
| 126 | } |
| 127 | |
| 128 | while (fin.getline(buf, 128)) { |
| 129 | if (buf[0] == '#') { |
| 130 | continue; |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | std::stringstream ss; |
| 135 | ss << std::string(buf); |
| 136 | ss >> w; |
| 137 | ss >> h; |
| 138 | |
| 139 | mgb_assert(w > 0 and h > 0); |
| 140 | |
| 141 | mgb::HostTensorND hv; |
| 142 | hv.comp_node(mgb::CompNode::default_cpu(), true) |
| 143 | .dtype(mgb::dtype::Uint8()) |
| 144 | .resize({1, h, w, channel}); |
| 145 | |
| 146 | fin.read((char*)(hv.raw_ptr()), hv.layout().total_nr_elems()); |
| 147 | fin.close(); |
| 148 | inputs.insert(std::make_pair(name, std::move(hv))); |
| 149 | } |
| 150 | |
| 151 | void DataParser::parse_npy(const std::string& name, const std::string& path) { |
| 152 | std::string type_str; |