我正在尝试使用自定义生成器在 keras 张量流中实现多输入模型,如下所示在接受的答案中在 keras 中创建混合数据生成器(图像,csv) :import randomimport pandas as pdimport numpy as npfrom glob import globfrom keras.preprocessing import image as krs_image# Create the arguments for image preprocessingdata_gen_args = dict( horizontal_flip=True, brightness_range=[0.5, 1.5], shear_range=10, channel_shift_range=50, rescale=1. / 255,)# Create an empty data generatordatagen = ImageDataGenerator()# Read the image list and csvimage_file_list = glob(f'{images_dir}/{split}/**/*.JPG', recursive=True)df = pd.read_csv(f'{csv_dir}/{split}.csv', index_col=csv_data[0])random.shuffle(image_file_list)def custom_generator(images_list, dataframe, batch_size): i = 0 while True: batch = {'images': [], 'csv': [], 'labels': []} for b in range(batch_size): if i == len(images_list): i = 0 random.shuffle(images_list) # Read image from list and convert to array image_path = images_list[i] image_name = os.path.basename(image_path).replace('.JPG', '') image = krs_image.load_img(image_path, target_size=(img_height, img_width)) image = datagen.apply_transform(image, data_gen_args) image = krs_image.img_to_array(image) # Read data from csv using the name of current image csv_row = dataframe.loc[image_name, :] label = csv_row['class'] csv_features = csv_row.drop(labels='class') batch['images'].append(image) batch['csv'].append(csv_features) batch['labels'].append(label) i += 1 batch['images'] = np.array(batch['images']) batch['csv'] = np.array(batch['csv']) # Convert labels to categorical values batch['labels'] = np.eye(num_classes)[batch['labels']] yield [batch['images'], batch['csv']], batch['labels']但是,我收到以下索引错误。任何帮助是极大的赞赏。
2 回答
慕虎7371278
TA贡献1802条经验 获得超4个赞
我认为你正在试图逃跑block by block and try to running again previously executed block
。另外,这段代码也没有问题。将整个代码放在一个块中并再次运行(或重新启动内核并立即运行所有代码)。如果这样还没有解决你的问题,你可以试试我的分享脚本。
手掌心
TA贡献1942条经验 获得超3个赞
索引错误是由于这部分造成的:
batch['labels'] = np.eye(num_classes)[batch['labels']]
np.eye
我使用 keras ' ' 代替' to_catgorical
' 将标签转换为一种热编码,并且它有效。
添加回答
举报
0/150
提交
取消