将EMNIST数据加载到TensorFlow可以通过以下步骤完成:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
data_path = 'path/to/emnist/dataset/'
train_images_file = data_path + 'emnist-byclass-train-images-idx3-ubyte'
train_labels_file = data_path + 'emnist-byclass-train-labels-idx1-ubyte'
test_images_file = data_path + 'emnist-byclass-test-images-idx3-ubyte'
test_labels_file = data_path + 'emnist-byclass-test-labels-idx1-ubyte'
然后,使用tf.data.FixedLengthRecordDataset
加载数据集文件。
def load_emnist_images(file_path):
return tf.data.FixedLengthRecordDataset(file_path, 28 * 28, header_bytes=16).map(
lambda s: tf.reshape(tf.io.decode_raw(s, tf.uint8), (28, 28, 1))
)
def load_emnist_labels(file_path):
return tf.data.FixedLengthRecordDataset(file_path, 1, header_bytes=8).map(
lambda s: tf.reshape(tf.io.decode_raw(s, tf.uint8), ())
)
train_images = load_emnist_images(train_images_file)
train_labels = load_emnist_labels(train_labels_file)
test_images = load_emnist_images(test_images_file)
test_labels = load_emnist_labels(test_labels_file)
train_images = train_images / 255.0
test_images = test_images / 255.0
plt.figure(figsize=(10, 10))
for i, (image, label) in enumerate(zip(train_images.take(25), train_labels.take(25))):
plt.subplot(5, 5, i + 1)
plt.imshow(image[:, :, 0], cmap='gray')
plt.title(chr(label + 96))
plt.axis('off')
plt.show()
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(26, activation='softmax')
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))
这是一个基本的将EMNIST数据加载到TensorFlow的流程。根据实际需求,你可以根据需要进行调整和扩展。
领取专属 10元无门槛券
手把手带您无忧上云