X_train,T_train=idx2numpy.convert_from_file('emnist/emnist-letters-train-images-idx3-ubyte'),idx2numpy.convert_from_file('emnist/emnist-letters-train-labels-idx1-ubyte')转化为相同形式train_num = 60000 test_num = 10000 img_dim = (1, 28, 28) img_size = 784 def _download(file_name): file_path = dataset_dir + "/" + file_name if os.path.exists(file_path): return print("Downloading " + file_name + " ... ") urllib.request.urlretrieve(url_base + file_name, file_path) print("Done") def download_mnist(): for v in key_file.values(): _download(v) def _load_label(file_name): file_path = dataset_dir + "/" + file_name print("Converting " + file_name + " to NumPy Array ...") with gzip.open(file_path, 'rb') as f: labels = np.frombuffer(f.read(), np.uint8, offset=8) print("Done") return labels def _load_img(file_name): file_path = dataset_dir + "/" + file_name print("Converting " + file_name + " to NumPy Array ...") with gzip.open(file_path, 'rb') as f: data = np.frombuffer(f.read(), np.uint8, offset=16) data = data.reshape(-1, img_size) print("Done") return data def _convert_numpy(): dataset = {} dataset['train_img'] = _load_img(key_file['train_img']) dataset['train_label'] = _load_label(key_file['train_label']) dataset['test_img'] = _load_img(key_file['test_img']) dataset['test_label'] = _load_label(key_file['test_label']) return dataset def init_mnist(): download_mnist() dataset = _convert_numpy() print("Creating pickle file ...") with open(save_file, 'wb') as f: pickle.dump(dataset, f, -1) print("Done!") def _change_one_hot_label(X): T = np.zeros((X.size, 10)) for idx, row in enumerate(T): row[X[idx]] = 1 return T def load_mnist(normalize=True, flatten=True, one_hot_label=False): """读入MNIST数据集 Parameters ---------- normalize : 将图像的像素值正规化为0.0~1.0 one_hot_label : one_hot_label为True的情况下,标签作为one-hot数组返回 one-hot数组是指[0,0,1,0,0,0,0,0,0,0]这样的数组 flatten : 是否将图像展开为一维数组 Returns ------- (训练图像, 训练标签), (测试图像, 测试标签) """ if not os.path.exists(save_file): init_mnist() with open(save_file, 'rb') as f: dataset = pickle.load(f) if normalize: for key in ('train_img', 'test_img'): dataset[key] = dataset[key].astype(np.float32) dataset[key] /= 255.0 if one_hot_label: dataset['train_label'] = _change_one_hot_label(dataset['train_label']) dataset['test_label'] = _change_one_hot_label(dataset['test_label']) if not flatten: for key in ('train_img', 'test_img'): dataset[key] = dataset[key].reshape(-1, 1, 28, 28) return (dataset['train_img'], dataset['train_label']), (dataset['test_img'], dataset['test_label']) if name == 'main': init_mnist()模仿这段代码将获取同样形式
时间: 2024-02-13 13:06:23 浏览: 131
idx1-ubyte.rar idx3-ubyte.rar
你提供的是MNIST数据集的加载代码,而不是EMNIST。如果要加载EMNIST数据集,可以使用以下代码:
```python
import idx2numpy
import numpy as np
def load_emnist():
X_train = idx2numpy.convert_from_file('emnist/emnist-letters-train-images-idx3-ubyte')
T_train = idx2numpy.convert_from_file('emnist/emnist-letters-train-labels-idx1-ubyte')
X_test = idx2numpy.convert_from_file('emnist/emnist-letters-test-images-idx3-ubyte')
T_test = idx2numpy.convert_from_file('emnist/emnist-letters-test-labels-idx1-ubyte')
# 将数据展开为一维数组
X_train = X_train.reshape(X_train.shape[0], -1)
X_test = X_test.reshape(X_test.shape[0], -1)
# 将标签转换为one-hot编码
T_train = np.eye(26)[T_train]
T_test = np.eye(26)[T_test]
return (X_train, T_train), (X_test, T_test)
```
这个代码将返回一个tuple,其中第一个元素是训练数据,第二个元素是测试数据,每个数据都是一个tuple,其中第一个元素是图像数据,第二个元素是标签数据。图像数据是展开为一维数组的,标签数据是one-hot编码的。
阅读全文