cannot import name 'emnist' from 'keras.datasets' (E:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\datasets\__init__.py)
时间: 2023-09-30 19:12:10 浏览: 313
这个错误信息表明你在导入 `emnist` 时出现了问题。可能的原因是你使用的 Keras 版本不支持 `emnist` 数据集,或者你没有正确安装 `emnist` 数据集。
你可以尝试更新 Keras 版本,或者手动下载 `emnist` 数据集并将其放置在正确的位置。具体步骤如下:
1. 打开 https://www.nist.gov/itl/products-and-services/emnist-dataset 下载 `emnist` 数据集。
2. 将下载的 `.gz` 文件解压到本地。
3. 打开 Python 终端并执行以下代码:
```
from keras.datasets import mnist
import numpy as np
import os
def load_emnist():
emnist_path = os.path.join('data', 'emnist')
if not os.path.exists(emnist_path):
os.makedirs(emnist_path)
train_images_path = os.path.join(emnist_path, 'emnist_train_images.npy')
train_labels_path = os.path.join(emnist_path, 'emnist_train_labels.npy')
test_images_path = os.path.join(emnist_path, 'emnist_test_images.npy')
test_labels_path = os.path.join(emnist_path, 'emnist_test_labels.npy')
if not os.path.exists(train_images_path) or not os.path.exists(train_labels_path) or not os.path.exists(
test_images_path) or not os.path.exists(test_labels_path):
print('Preprocessing EMNIST dataset...')
emnist_train, emnist_test = load_raw_emnist()
np.save(train_images_path, emnist_train[0])
np.save(train_labels_path, emnist_train[1])
np.save(test_images_path, emnist_test[0])
np.save(test_labels_path, emnist_test[1])
else:
print('Loading preprocessed EMNIST dataset...')
emnist_train = (np.load(train_images_path), np.load(train_labels_path))
emnist_test = (np.load(test_images_path), np.load(test_labels_path))
return emnist_train, emnist_test
def load_raw_emnist():
from scipy.io import loadmat
emnist_path = os.path.join('data', 'emnist')
if not os.path.exists(emnist_path):
os.makedirs(emnist_path)
emnist_train_path = os.path.join(emnist_path, 'emnist-letters-train.mat')
emnist_test_path = os.path.join(emnist_path, 'emnist-letters-test.mat')
if not os.path.exists(emnist_train_path) or not os.path.exists(emnist_test_path):
print('Downloading EMNIST dataset...')
download_emnist(emnist_path)
print('Loading EMNIST dataset...')
train_data = loadmat(emnist_train_path)
test_data = loadmat(emnist_test_path)
emnist_train_images = train_data['dataset'][0][0][0][0][0][0]
emnist_train_labels = train_data['dataset'][0][0][0][0][0][1]
emnist_test_images = test_data['dataset'][0][0][0][0][0][0]
emnist_test_labels = test_data['dataset'][0][0][0][0][0][1]
emnist_train_images = emnist_train_images.reshape(
emnist_train_images.shape[0], 1, 28, 28).astype('float32') / 255.0
emnist_test_images = emnist_test_images.reshape(
emnist_test_images.shape[0], 1, 28, 28).astype('float32') / 255.0
return (emnist_train_images, emnist_train_labels), (emnist_test_images, emnist_test_labels)
def download_emnist(emnist_path):
import urllib.request
base_url = 'http://www.itl.nist.gov/iaui/vip/cs_links/EMNIST/matlab.zip'
zip_path = os.path.join(emnist_path, 'emnist.zip')
mat_path = os.path.join(emnist_path, 'matlab.zip')
print('Downloading EMNIST dataset...')
urllib.request.urlretrieve(base_url, zip_path)
print('Extracting EMNIST dataset...')
import zipfile
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(emnist_path)
os.rename(os.path.join(emnist_path, 'matlab'), mat_path)
print('Converting EMNIST dataset...')
import scipy.io as sio
train_data = sio.loadmat(os.path.join(emnist_path, 'matlab', 'emnist-letters-train.mat'))
test_data = sio.loadmat(os.path.join(emnist_path, 'matlab', 'emnist-letters-test.mat'))
sio.savemat(os.path.join(emnist_path, 'emnist-letters-train.mat'), {'dataset': train_data['dataset']})
sio.savemat(os.path.join(emnist_path, 'emnist-letters-test.mat'), {'dataset': test_data['dataset']})
os.remove(zip_path)
os.remove(mat_path)
load_emnist()
```
这个代码将下载 `emnist` 数据集并将其预处理为 NumPy 数组。你可以在自己的代码中使用这些数组。
希望这能帮到你!
阅读全文