def get_image_num(path, train_own_data): num = 0 if train_own_data: train_path = os.path.join(path, 'images_background') for character in os.listdir(train_path): # 在大众类下遍历小种类。 character_path = os.path.join(train_path, character) num += len(os.listdir(character_path)) else: train_path = os.path.join(path, 'images_background') for alphabet in os.listdir(train_path): # 然后遍历images_background下的每一个文件夹,代表一个大种类 alphabet_path = os.path.join(train_path, alphabet) for character in os.listdir(alphabet_path): # 在大众类下遍历小种类。 character_path = os.path.join(alphabet_path, character) num += len(os.listdir(character_path)) return num
时间: 2024-04-17 07:29:17 浏览: 102
这段代码用于获取图像数据集中的图像数量。
首先,根据 `train_own_data` 的值判断是否使用自定义数据集。如果是自定义数据集,则遍历 `path` 下的 `images_background` 文件夹,对每个字符类别下的图像数量进行累加。
如果不是自定义数据集,则同样遍历 `path` 下的 `images_background` 文件夹,然后再遍历每个字母类别和字符类别下的图像数量进行累加。
最后,返回累计的图像数量 `num`。
这段代码的作用是计算给定数据集中的总图像数量,根据 `train_own_data` 参数的不同,会有不同的遍历方式。
相关问题
def get_CIFAR10_data(num_training=5000, num_validation=500, num_test=500): cifar10_dir = r'D:\daima\cifar-10-python\cifar-10-batches-py' X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir) print(X_train.shape) mask = range(num_training, num_training + num_validation) X_val = X_train[mask] y_val = y_train[mask] mask = range(num_training) X_train = X_train[mask] y_train = y_train[mask] mask = range(num_test) X_test = X_test[mask] y_test = y_test[mask] mean_image = np.mean(X_train, axis=0) X_train -= mean_image X_val -= mean_image X_test -= mean_image X_train = X_train.transpose(0, 3, 1, 2).copy() X_val = X_val.transpose(0, 3, 1, 2).copy() X_test = X_test.transpose(0, 3, 1, 2).copy() return { 'X_train': X_train, 'y_train': y_train, 'X_val': X_val, 'y_val': y_val, 'X_test': X_test, 'y_test': y_test, }这是一个加载cifar10数据集的函数,如何修改使其能加载mnist数据集,不使用使用 TensorFlow
要加载MNIST数据集,可以使用以下代码:
```
import numpy as np
import gzip
def load_mnist(path, kind='train'):
"""Load MNIST data from `path`"""
labels_path = f'{path}/{kind}-labels-idx1-ubyte.gz'
images_path = f'{path}/{kind}-images-idx3-ubyte.gz'
with gzip.open(labels_path, 'rb') as lbpath:
labels = np.frombuffer(lbpath.read(), dtype=np.uint8, offset=8)
with gzip.open(images_path, 'rb') as imgpath:
images = np.frombuffer(imgpath.read(), dtype=np.uint8, offset=16).reshape(len(labels), 784)
return images, labels
def get_MNIST_data(num_training=50000, num_validation=10000, num_test=10000):
mnist_dir = r'path/to/MNIST'
X_train, y_train = load_mnist(mnist_dir, kind='train')
X_test, y_test = load_mnist(mnist_dir, kind='t10k')
mask = range(num_training, num_training + num_validation)
X_val = X_train[mask]
y_val = y_train[mask]
mask = range(num_training)
X_train = X_train[mask]
y_train = y_train[mask]
mask = range(num_test)
X_test = X_test[mask]
y_test = y_test[mask]
# normalize images
mean_image = np.mean(X_train, axis=0)
X_train -= mean_image
X_val -= mean_image
X_test -= mean_image
# reshape images to 28x28x1
X_train = X_train.reshape(-1, 1, 28, 28)
X_val = X_val.reshape(-1, 1, 28, 28)
X_test = X_test.reshape(-1, 1, 28, 28)
return {
'X_train': X_train, 'y_train': y_train,
'X_val': X_val, 'y_val': y_val,
'X_test': X_test, 'y_test': y_test
}
```
其中 `load_mnist` 函数会从指定路径加载MNIST数据集,返回的 `images` 是一个形状为 `(num_samples, 784)` 的numpy数组,`labels`是一个形状为 `(num_samples,)` 的numpy数组。 `get_MNIST_data` 函数会调用 `load_mnist` 函数来加载数据集,并进行预处理,最后返回一个字典,包含训练集、验证集和测试集的图像和标签。
阅读全文