random.shuffle(imgs)
时间: 2024-03-04 08:46:17 浏览: 15
random.shuffle(imgs)是一个用于打乱列表顺序的函数。它接受一个列表作为参数,并将列表中的元素随机重新排列。这个函数通常用于需要随机化数据顺序的场景,比如在机器学习中对数据集进行随机化处理。
例如,如果有一个包含图片路径的列表imgs,可以使用random.shuffle(imgs)来打乱图片的顺序,使得每次训练时都能够以不同的顺序呈现给模型,增加模型的泛化能力。
注意:random.shuffle函数会直接修改原始列表,而不会返回一个新的打乱后的列表。
相关问题
# 定义数据集读取器 def load_data(mode='train'): # 数据文件 datafile = './data/data116648/mnist.json.gz' print('loading mnist dataset from {} ......'.format(datafile)) data = json.load(gzip.open(datafile)) train_set, val_set, eval_set = data # 数据集相关参数,图片高度IMG_ROWS, 图片宽度IMG_COLS IMG_ROWS = 28 IMG_COLS = 28 if mode == 'train': imgs = train_set[0] labels = train_set[1] elif mode == 'valid': imgs = val_set[0] labels = val_set[1] elif mode == 'eval': imgs = eval_set[0] labels = eval_set[1] imgs_length = len(imgs) assert len(imgs) == len(labels), \ "length of train_imgs({}) should be the same as train_labels({})".format( len(imgs), len(labels)) index_list = list(range(imgs_length)) # 读入数据时用到的batchsize BATCHSIZE = 100 # 定义数据生成器 def data_generator(): if mode == 'train': random.shuffle(index_list) imgs_list = [] labels_list = [] for i in index_list: img = np.reshape(imgs[i], [1, IMG_ROWS, IMG_COLS]).astype('float32') img_trans=-img #转变颜色 label = np.reshape(labels[i], [1]).astype('int64') label_trans=label imgs_list.append(img) imgs_list.append(img_trans) labels_list.append(label) labels_list.append(label_trans) if len(imgs_list) == BATCHSIZE: yield np.array(imgs_list), np.array(labels_list) imgs_list = [] labels_list = [] # 如果剩余数据的数目小于BATCHSIZE, # 则剩余数据一起构成一个大小为len(imgs_list)的mini-batch if len(imgs_list) > 0: yield np.array(imgs_list), np.array(labels_list) return data_generator
这段代码定义了一个数据集读取器load_data,用于读取MNIST数据集。具体实现如下:
- 首先从文件中加载MNIST数据集,数据集文件为'mnist.json.gz',其中包含了训练集、验证集和测试集的图像和标签数据。
- 根据不同的mode参数,从数据集中选择相应的数据集,包括训练集、验证集和测试集。
- 定义了一个数据生成器data_generator,用于生成mini-batch大小的图像数据和标签数据。其中,通过random.shuffle(index_list)实现数据集的随机打乱,防止模型过拟合。另外,为了增加数据的多样性,对于每个图像数据,还进行了颜色反转的处理。
- 最后,返回数据生成器。
将下面代码简洁化:def split_dataset(img_path, target_folder_path, output_path): filename = [] total_imgs = os.listdir(img_path) #for root, dirs, files in os.walk(img_path): for img in total_imgs: filename.append(img) np.random.shuffle(filename) train = filename[:int(len(filename) * 0.9)] test = filename[int(len(filename) * 0.9):] out_images = os.path.join(output_path, 'imgs') if not os.path.exists(out_images): os.makedirs(out_images) out_images_train = os.path.join(out_images, 'training') if not os.path.exists(out_images_train): os.makedirs(out_images_train) out_images_test = os.path.join(out_images, 'test') if not os.path.exists(out_images_test): os.makedirs(out_images_test) out_annotations = os.path.join(output_path, 'annotations') if not os.path.exists(out_annotations): os.makedirs(out_annotations) out_annotations_train = os.path.join(out_annotations, 'training') if not os.path.exists(out_annotations_train): os.makedirs(out_annotations_train) out_annotations_test = os.path.join(out_annotations, 'test') if not os.path.exists(out_annotations_test): os.makedirs(out_annotations_test) for i in train: print(os.path.join(img_path, i)) print(os.path.join(out_images_train, i)) shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_train, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_train, annotations_name)) for i in test: shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_test, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_test, annotations_name))
def split_dataset(img_path, target_folder_path, output_path):
filename = os.listdir(img_path)
np.random.shuffle(filename)
train = filename[:int(len(filename) * 0.9)]
test = filename[int(len(filename) * 0.9):]
out_images = os.path.join(output_path, 'imgs')
os.makedirs(out_images, exist_ok=True)
out_images_train = os.path.join(out_images, 'training')
os.makedirs(out_images_train, exist_ok=True)
out_images_test = os.path.join(out_images, 'test')
os.makedirs(out_images_test, exist_ok=True)
out_annotations = os.path.join(output_path, 'annotations')
os.makedirs(out_annotations, exist_ok=True)
out_annotations_train = os.path.join(out_annotations, 'training')
os.makedirs(out_annotations_train, exist_ok=True)
out_annotations_test = os.path.join(out_annotations, 'test')
os.makedirs(out_annotations_test, exist_ok=True)
for i in train:
img_src = os.path.join(img_path, i)
img_dst = os.path.join(out_images_train, i)
shutil.copyfile(img_src, img_dst)
annotations_name = "gt_" + i[:-3] + 'txt'
annotations_src = os.path.join(target_folder_path, annotations_name)
annotations_dst = os.path.join(out_annotations_train, annotations_name)
shutil.copyfile(annotations_src, annotations_dst)
for i in test:
img_src = os.path.join(img_path, i)
img_dst = os.path.join(out_images_test, i)
shutil.copyfile(img_src, img_dst)
annotations_name = "gt_" + i[:-3] + 'txt'
annotations_src = os.path.join(target_folder_path, annotations_name)
annotations_dst = os.path.join(out_annotations_test, annotations_name)
shutil.copyfile(annotations_src, annotations_dst)