| 271 | |
| 272 | |
| 273 | def writeFloat(name, data): |
| 274 | f = open(name, "wb") |
| 275 | |
| 276 | dim = len(data.shape) |
| 277 | if dim > 3: |
| 278 | raise Exception("bad float file dimension: %d" % dim) |
| 279 | |
| 280 | f.write(("float\n").encode("ascii")) |
| 281 | f.write(("%d\n" % dim).encode("ascii")) |
| 282 | |
| 283 | if dim == 1: |
| 284 | f.write(("%d\n" % data.shape[0]).encode("ascii")) |
| 285 | else: |
| 286 | f.write(("%d\n" % data.shape[1]).encode("ascii")) |
| 287 | f.write(("%d\n" % data.shape[0]).encode("ascii")) |
| 288 | for i in range(2, dim): |
| 289 | f.write(("%d\n" % data.shape[i]).encode("ascii")) |
| 290 | |
| 291 | data = data.astype(np.float32) |
| 292 | if dim == 2: |
| 293 | data.tofile(f) |
| 294 | |
| 295 | else: |
| 296 | np.transpose(data, (2, 0, 1)).tofile(f) |
| 297 | |
| 298 | |
| 299 | def check_dim_and_resize(tensor_list): |