Extract the images into a 4D uint8 numpy array [index, y, x, depth]. Args: f: A file object that can be passed into a gzip reader. Returns: data: A 4D uint8 numpy array [index, y, x, depth]. Raises: ValueError: If the bytestream does not start with 2051.
(f)
| 45 | |
| 46 | @deprecated(None, "Please use tf.data to implement this functionality.") |
| 47 | def _extract_images(f): |
| 48 | """Extract the images into a 4D uint8 numpy array [index, y, x, depth]. |
| 49 | |
| 50 | Args: |
| 51 | f: A file object that can be passed into a gzip reader. |
| 52 | |
| 53 | Returns: |
| 54 | data: A 4D uint8 numpy array [index, y, x, depth]. |
| 55 | |
| 56 | Raises: |
| 57 | ValueError: If the bytestream does not start with 2051. |
| 58 | |
| 59 | """ |
| 60 | print("Extracting", f.name) |
| 61 | with gzip.GzipFile(fileobj=f) as bytestream: |
| 62 | magic = _read32(bytestream) |
| 63 | if magic != 2051: |
| 64 | msg = f"Invalid magic number {magic} in MNIST image file: {f.name}" |
| 65 | raise ValueError(msg) |
| 66 | num_images = _read32(bytestream) |
| 67 | rows = _read32(bytestream) |
| 68 | cols = _read32(bytestream) |
| 69 | buf = bytestream.read(rows * cols * num_images) |
| 70 | data = np.frombuffer(buf, dtype=np.uint8) |
| 71 | data = data.reshape(num_images, rows, cols, 1) |
| 72 | return data |
| 73 | |
| 74 | |
| 75 | @deprecated(None, "Please use tf.one_hot on tensors.") |
no test coverage detected