gpt4 book ai didi

python - 获取 ValueError : setting an array element with a sequence from tf. contrib.keras.preprocessing.image.ImageDatagenerator.flow

转载 作者:行者123 更新时间:2023-11-28 19:04:06 25 4
gpt4 key购买 nike

我正在尝试在 Tensorflow 中进行数据扩充。我写了这段代码。

import numpy as np
import tensorflow as tf
import tensorflow.contrib.keras as keras
import time, random

def get_image_data_generator():
return keras.preprocessing.image.ImageDataGenerator(
rotation_range=get_random_rotation_angle(),\
width_shift_range=get_random_wh_shift(),\
height_shift_range=get_random_wh_shift(),\
shear_range=get_random_shear(),\
zoom_range=get_random_zoom(),\
horizontal_flip=get_random_flip(),\
vertical_flip=get_random_flip(),\
preprocessing_function=get_random_function())

def augment_data(image_array,label_array):
print image_array.shape
images_array = image_array.copy()
labels_array = label_array.copy()
#Create a list of various datagenerators with different arguments
datagenerators = []
ndg = 10
#Creating 10 different generators
for ndata in xrange(ndg):
datagenerators.append(get_image_data_generator())
#Setting batch_size to be equal to no.of images
bsize = image_array.shape[0]
print bsize
#Obtaining the augmented data
for dgen in datagenerators:
dgen.fit(image_array)
(aug_img,aug_label) = dgen.flow(image_array,label_array,batch_size=bsize,shuffle=True)
print aug_img.shape
#Concatenating with the original data
images_array = np.concatenate([images_array,aug_img],axis=0)
labels_array = np.concatenate([labels_array,aug_label],axis=0)
return (images_array,labels_array)

当我运行代码时使用

augment_data(image_array,label_array)

我收到一条错误消息

Traceback (most recent call last):
File "cnn_model.py", line 40, in <module>
images_array,labels_array = augment_data(image_array,label_array)
File "/media/siladittya/d801fb13-809a-41b4-8f2e-d617ba103aba/ISI/code/2. known_object_detection/aug_data.py", line 47, in augment_data
(aug_img,aug_label) = dgen.flow(image_array,label_array,batch_size=10000,shuffle=True)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/_impl/keras/preprocessing/image.py", line 1018, in next
return self._get_batches_of_transformed_samples(index_array)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/_impl/keras/preprocessing/image.py", line 991, in _get_batches_of_transformed_samples
batch_x[i] = x
ValueError: setting an array element with a sequence.

编辑::即使我将单个图像作为参数传递,我也会收到此错误。

我在这里做错了什么?我不明白。请帮忙。

最佳答案

Edit :: I am getting this error even if I pass a single image as argument.`

你能否将单个元素作为数组传递并查看:

例子:

image_array, label_array = augment_data([image], [label])

关于python - 获取 ValueError : setting an array element with a sequence from tf. contrib.keras.preprocessing.image.ImageDatagenerator.flow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48955971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com