Extract the labels into a 1D uint8 numpy array [index]. Args: f: A file object that can be passed into a gzip reader. one_hot: Does one hot encoding for the result. num_classes: Number of classes for the one hot encoding. Returns: labels: a 1D uint8 numpy array.
(f, one_hot=False, num_classes=10)
| 84 | |
| 85 | @deprecated(None, "Please use tf.data to implement this functionality.") |
| 86 | def _extract_labels(f, one_hot=False, num_classes=10): |
| 87 | """Extract the labels into a 1D uint8 numpy array [index]. |
| 88 | |
| 89 | Args: |
| 90 | f: A file object that can be passed into a gzip reader. |
| 91 | one_hot: Does one hot encoding for the result. |
| 92 | num_classes: Number of classes for the one hot encoding. |
| 93 | |
| 94 | Returns: |
| 95 | labels: a 1D uint8 numpy array. |
| 96 | |
| 97 | Raises: |
| 98 | ValueError: If the bystream doesn't start with 2049. |
| 99 | """ |
| 100 | print("Extracting", f.name) |
| 101 | with gzip.GzipFile(fileobj=f) as bytestream: |
| 102 | magic = _read32(bytestream) |
| 103 | if magic != 2049: |
| 104 | msg = f"Invalid magic number {magic} in MNIST label file: {f.name}" |
| 105 | raise ValueError(msg) |
| 106 | num_items = _read32(bytestream) |
| 107 | buf = bytestream.read(num_items) |
| 108 | labels = np.frombuffer(buf, dtype=np.uint8) |
| 109 | if one_hot: |
| 110 | return _dense_to_one_hot(labels, num_classes) |
| 111 | return labels |
| 112 | |
| 113 | |
| 114 | class _DataSet: |
no test coverage detected