简体   繁体   English

获取ValueError:使用来自tf.contrib.keras.preprocessing.image.ImageDatagenerator.flow的序列设置数组元素

[英]Getting ValueError: setting an array element with a sequence from tf.contrib.keras.preprocessing.image.ImageDatagenerator.flow

I am trying to do Data Augmentation in Tensorflow. 我正在尝试在Tensorflow中进行数据扩充。 I have written this code. 我写了这段代码。

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)

When I run the code using 当我使用运行代码时

augment_data(image_array,label_array)

I get an error which says 我收到一个错误

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. 编辑::即使我将单个图像作为参数传递,我也会收到此错误。

What am I doing wrong here? 我在这做错了什么? I can't understand. 我无法理解。 Please help. 请帮忙。

Edit :: I am getting this error even if I pass a single image as argument.` 编辑::即使我将单个图像作为参数传递,我也会收到此错误

Can you pass the single element as an array and see: 你可以将单个元素作为数组传递并查看:

example: 例:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自 tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory 的 tf.data.Dataset? - tf.data.Dataset from tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory? 带有 tf.image.rgb_to_grayscale 的自定义预处理函数 - ValueError:使用序列设置数组元素 - Custom preprocessing_function with tf.image.rgb_to_grayscale - ValueError: setting an array element with a sequence tf.data 与 tf.keras.preprocessing.image.ImageDataGenerator - tf.data vs tf.keras.preprocessing.image.ImageDataGenerator ValueError:设置具有序列Keras的数组元素 - ValueError: setting an array element with a sequence Keras ValueError:使用Keras的BatchNormalization层中的序列设置数组元素 - ValueError: setting an array element with a sequence from BatchNormalization layer of Keras 调用TensorFlow张量的tf.keras.utils.to_categorical时,“ ValueError:设置具有序列的数组元素。” - “ValueError: setting an array element with a sequence.” when calling tf.keras.utils.to_categorical of a TensorFlow tensor 图像数组“ValueError:使用序列设置数组元素” - Image Array "ValueError: setting an array element with a sequence" ValueError:使用 tf.keras.preprocessing.image_dataset_from_directory 时要解压的值太多(预期为 2) - ValueError: too many values to unpack (expected 2) when using tf.keras.preprocessing.image_dataset_from_directory ValueError:使用序列设置数组元素。 关于Keras model.fit - ValueError: setting an array element with a sequence. On Keras model.fit ValueError:使用序列设置数组元素? - ValueError: setting an array element with a sequence?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM