data_test=[] data_train = [] for i in range (all_num): if i <train_num: image= image_list[i] image = cv2.imread(image) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #将图片转换成RGB格式 image = cv2.resize(image, (28, 28)) image = image.astype('float32') image = np.array(image)/255#归一化[0,1] image=image.reshape(-1,28,28) data_train.append(image) # label_train.append(label_list[i]) else: image = image_list[i] image = cv2.imread(image) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image = cv2.resize(image, (28, 28)) image = image.astype('float32') image = np.array(image) / 255 image = image.reshape(-1, 28, 28) data_test.append(image) # label_test.append(label_list[i]) data_train=np.array(data_train) label_train = np.array(label_train) data_test = np.array(data_test) label_test = np.array(label_test)
时间: 2024-02-14 14:23:36 浏览: 116
这段代码是用来将图片数据分为训练集和测试集,并对图片进行预处理的。首先,定义了两个空列表data_train和data_test,用来存储处理后的训练集和测试集数据。然后,通过循环对所有的图片进行遍历,将前train_num个图片作为训练集,后面的图片作为测试集。在对每张图片进行处理时,首先使用cv2.imread函数读取图片,然后使用cv2.cvtColor函数将图片转换成灰度图像,接着使用cv2.resize函数将图片大小调整为28x28,然后将像素值转换为float32类型,并将像素值归一化到[0,1]的范围内。最后,将处理后的图片数据添加到data_train或data_test列表中,并将列表转换为numpy数组类型。同时,该代码中注释掉了label_train和label_test的添加操作,因此在这段代码中并没有对图片进行标签的处理。
相关问题
import os import cv2 import numpy as np def load_data(file_dir): all_num = 4000 train_num = int(all_num * 0.75) cats = [] label_cats = [] dogs = [] label_dogs = [] for file in os.listdir(file_dir): file="\\"+file name = file.split(sep='.') if 'cat' in name[0]: cats.append(file_dir + file) label_cats.append(0) else: if 'dog' in name[0]: dogs.append(file_dir + file) label_dogs.append(1) image_list = np.hstack((cats,dogs)) label_list = np.hstack((label_cats, label_dogs)) temp = np.array([image_list, label_list]) # 矩阵转置 temp = temp.transpose() # 打乱顺序 np.random.shuffle(temp) # print(temp) # 取出第一个元素作为 image 第二个元素作为 label image_list = temp[:, 0] label1_train = temp[:train_num, 1] # print(label1_train) # 单出,去掉单字符 label_train = [int(y) for y in label1_train] # print(label_train) label1_test = temp[train_num:, 1] label_test = [int(y) for y in label1_test] data_test=[] data_train = [] for i in range (all_num): if i <train_num: image= image_list[i] image = cv2.imread(image) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #将图片转换成RGB格式 image = cv2.resize(image, (28, 28)) image = image.astype('float32') image = np.array(image)/255#归一化[0,1] image=image.reshape(-1,28,28) data_train.append(image) # label_train.append(label_list[i]) else: image = image_list[i] image = cv2.imread(image) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image = cv2.resize(image, (28, 28)) image = image.astype('float32') image = np.array(image) / 255 image = image.reshape(-1, 28, 28) data_test.append(image) # label_test.append(label_list[i]) data_train=np.array(data_train) label_train = np.array(label_train) data_test = np.array(data_test) label_test = np.array(label_test) return data_train,label_train,data_test, label_test
这段代码是定义了一个函数load_data,用来加载数据集,并将数据集划分为训练集和测试集。函数接收一个参数file_dir,表示数据集所在的路径。首先,定义了all_num和train_num两个变量,用来表示数据集的总数和训练集的数量,其中训练集的数量是总数的75%。然后,定义了四个空列表,分别用来存储猫的图片路径、猫的标签、狗的图片路径和狗的标签。接着,使用os.listdir函数遍历数据集路径下的所有文件,对每个文件进行判断,如果文件名中包含'cat'字符串,则将该文件的路径添加到cats列表中,并将标签0添加到label_cats列表中;如果文件名中包含'dog'字符串,则将该文件的路径添加到dogs列表中,并将标签1添加到label_dogs列表中。然后,使用numpy.hstack函数将猫和狗的图片路径和标签拼接成两个一维数组image_list和label_list。接着,使用numpy.array函数将image_list和label_list拼接成一个二维数组temp,并将其转置,使得图片路径和标签分别位于temp的第一列和第二列。然后,使用numpy.random.shuffle函数对temp进行打乱顺序操作。接着,将temp的第一列(即图片路径)赋值给image_list,将temp的前train_num行的第二列(即标签)赋值给label1_train,将temp的后面部分的第二列(即标签)赋值给label1_test。然后,将label1_train和label1_test从字符串类型转换为整型,并分别赋值给label_train和label_test。最后,调用前面提到的数据预处理代码,将image_list中的每张图片进行预处理,并将处理后的图片数据分别添加到data_train和data_test列表中,并将列表转换为numpy数组类型。最后,函数返回data_train、label_train、data_test和label_test四个变量。
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数据集
要加载MNIST数据集,可以使用以下代码修改上述函数:
```
import tensorflow as tf
from tensorflow.keras.datasets import mnist
import numpy as np
def get_MNIST_data(num_training=50000, num_validation=10000, num_test=10000):
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = np.expand_dims(X_train, axis=-1)
X_test = np.expand_dims(X_test, axis=-1)
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255
mean_image = np.mean(X_train, axis=0)
X_train -= mean_image
X_test -= mean_image
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]
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,
}
```
此代码使用 TensorFlow 的 MNIST 数据集加载函数,将数据集的维度转换为 (num_samples, height, width, depth) 的形式,并对图像像素值进行归一化。
阅读全文