train-images-idx1-ubyte
文件是用于存儲 MNIST 數據集中手寫數字圖像數據的文件。與標簽文件類似,這個文件使用的是一種簡單而緊湊的二進制格式。具體的文件格式如下:
-
文件頭(Header):
文件頭部分包含了一些描述文件內容的信息,具體如下:- 魔數(Magic Number):文件的前 4 個字節,用于標識文件類型。對于圖像文件,魔數通常是
2051
(十進制)。 - 圖像數量:緊隨魔數之后的 4 個字節,表示文件中包含的圖像數量。
- 圖像高度:再接下來的 4 個字節,表示圖像的高度(像素)。
- 圖像寬度:緊隨圖像高度之后的 4 個字節,表示圖像的寬度(像素)。
- 魔數(Magic Number):文件的前 4 個字節,用于標識文件類型。對于圖像文件,魔數通常是
-
圖像數據:
圖像數據部分緊隨文件頭之后,包含了所有圖像的像素數據,每個像素占用 1 個字節(0 到 255 之間的灰度值)。
具體來說,文件的格式可以用偽代碼表示如下:
[header]
| magic number (4 bytes) | number of images (4 bytes) | number of rows (4 bytes) | number of columns (4 bytes) |
[images]
| pixel 1 (1 byte) | pixel 2 (1 byte) | ... | pixel N (1 byte) |
其中,每個圖像的數據按行優先順序存儲(即每行的像素從左到右排列,按行排列)。
讀取 train-images-idx1-ubyte
文件的示例代碼
以下是一個 Python 示例代碼,用于讀取和解析 train-images-idx1-ubyte
文件:
import struct
import numpy as np
import matplotlib.pyplot as pltdef read_images(file_path):with open(file_path, 'rb') as file:# 讀取魔數和圖像信息magic_number, num_images, num_rows, num_columns = struct.unpack('>IIII', file.read(16))print(f"Magic Number: {magic_number}, Number of Images: {num_images}, Rows: {num_rows}, Columns: {num_columns}")# 讀取所有圖像images = np.fromfile(file, dtype=np.uint8).reshape(num_images, num_rows, num_columns)return images# 使用示例
file_path = 'train-images-idx3-ubyte'
images = read_images(file_path)
print(f"First Image Shape: {images[0].shape}")# 顯示第一張圖像
plt.imshow(images[0], cmap='gray')
plt.show()
在這段代碼中:
- 使用
struct.unpack
方法從文件中讀取二進制數據。 '>IIII'
表示以大端序讀取四個 4 字節的無符號整數,分別對應魔數、圖像數量、圖像高度和圖像寬度。np.fromfile
方法從文件中讀取剩余的像素數據,并將其重塑為 (num_images, num_rows, num_columns) 的形狀。
通過上述代碼,可以將 train-images-idx1-ubyte
文件中的所有圖像數據讀取到一個 NumPy 數組中,并展示第一張圖像。
train-labels-idx1-ubyte
是一個存儲在 Ubyte 格式中的文件,常用于 MNIST 數據集的標簽文件。這個文件的格式是一個二進制文件,包含了手寫數字圖片對應的標簽。它的存儲結構是非常簡單和緊湊的,下面是具體的存儲格式:
-
文件頭(Header):
文件頭部分包含了一些描述文件內容的信息,具體如下:- 魔數(Magic Number):文件的前 4 個字節,用于標識文件類型。對于標簽文件,魔數通常是
2049
(十進制)。 - 標簽數量:緊隨魔數之后的 4 個字節,表示文件中包含的標簽數量。
- 魔數(Magic Number):文件的前 4 個字節,用于標識文件類型。對于標簽文件,魔數通常是
-
標簽數據:
標簽數據部分緊隨文件頭之后,包含了所有圖片的標簽,每個標簽占用 1 個字節(表示 0 到 9 之間的數字)。
具體來說,文件的格式可以用偽代碼表示如下:
[header]
| magic number (4 bytes) | number of items (4 bytes) |
[labels]
| label 1 (1 byte) | label 2 (1 byte) | ... | label N (1 byte) |
讀取 train-labels-idx1-ubyte
文件的示例代碼
以下是一個 Python 示例代碼,用于讀取和解析 train-labels-idx1-ubyte
文件:
import structdef read_labels(file_path):with open(file_path, 'rb') as file:# 讀取魔數和標簽數量magic_number, num_labels = struct.unpack('>II', file.read(8))print(f"Magic Number: {magic_number}, Number of Labels: {num_labels}")# 讀取所有標簽labels = []for _ in range(num_labels):label = struct.unpack('B', file.read(1))[0]labels.append(label)return labels# 使用示例
file_path = 'train-labels-idx1-ubyte'
labels = read_labels(file_path)
print(f"First 10 Labels: {labels[:10]}")
在這段代碼中:
- 使用
struct.unpack
方法從文件中讀取二進制數據。 '>II'
表示以大端序讀取兩個 4 字節的無符號整數,分別對應魔數和標簽數量。'B'
表示讀取一個 1 字節的無符號整數,表示一個標簽。
通過上述代碼,可以將 train-labels-idx1-ubyte
文件中的所有標簽讀取到一個列表中。