用于训练元组对象的tensorflow数据集api输入没有ndims属性

时间:2018-11-25 09:57:36

标签: python tensorflow tensorflow-datasets

所以我正在尝试使用新的TensorFlow数据集API训练GAN为图像着色 我无法使它正常工作

我正在尝试为数据集使用简单的一枪迭代器,我认为这可能会导致问题,但我不知道为什么

所以我要问的是

有人可以告诉我代码有什么问题

代码:

创建数据集

def get_next():

    #where gray_ls is just a list of image paths 
    gray_ds   = tf.data.Dataset.from_tensor_slices(gray_ls).shuffle(50).map(in_parser).batch(30).repeat()
    print(f"output types = {gray_ds.output_types}")   # --> output types = <dtype: 'float32'>
    print(f"output shapes = {gray_ds.output_shapes}") # --> output shapes = (?, ?, ?, ?)

    gray_iter  = gray_ds.make_one_shot_iterator()
    next_gray  = gray_iter.get_next()

    # next_color is the same as next gray just different images
    return next_color, next_gray

# mapping function 
def in_parser(img_path):

    img_file = tf.read_file(img_path)
    img = tf.image.decode_image(img_file,channels=3)
    img = tf.image.random_flip_left_right(img)
    img = tf.image.random_brightness(img, max_delta = 0.1)
    img = tf.image.random_contrast(img, lower = 0.9, upper = 1.1)
    img = tf.cast(img, tf.float32)
    img = img/255.0
    print(img)
    return img
#some global vars 
stddev  = 0.02
decay   = 0.9
epsilon = 1e-4
k_size  = [5,5]
strides = [2,2]
def gen(input, is_train):

#chanel number
c1 , c2 ,c3 ,c4 = 64, 128, 256, 512

with tf.variable_scope("gen",reuse=tf.AUTO_REUSE):

    #this is where it crashes
    conv1 = tf.layers.conv2d(input,c1,k_size,strides,'SAME',
                             kernel_initializer=tf.truncated_normal_initializer(stddev=stddev),
                             name='conv1')

    bn1 = tf.contrib.layers.batch_norm(conv1,is_training=is_train, updates_collections=None,
                                       decay=decay,epsilon=epsilon,scope='bn1')
    ac1 = lrelu(bn1,'ac1')
#there is more code after this

尝试运行它:

next_color, next_gray = get_next()

sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
foo = sess.run(next_gray)

print(f"foo ndims : {foo.ndim}") # --> foo ndims : 4
gen_image = gen(foo, True)
# some more code after this 

现在出现错误:

 AttributeError: 'tuple' object has no attribute 'ndims'
     ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-701a9276e633> in <module>()
     94 
     95 
---> 96 train()

<ipython-input-1-701a9276e633> in train()
     41 #     print(foo.shape)
     42     print("==========================+==============")
---> 43     gen_image = gen(foo, True)
     44 #     gen_image = gen(next_gray, True)
     45     print("==========================+==============")

~\Desktop\code\python\image_processing\Untitled Folder\Untitled Folder\testing1_2\my_gen.py in gen(input, is_train)
     30         conv1 = tf.layers.conv2d(input,c1,k_size,strides,'SAME',
     31                                  kernel_initializer=tf.truncated_normal_initializer(stddev=stddev),
---> 32                                  name='conv1')
     33 
     34         bn1 = tf.contrib.layers.batch_norm(conv1,is_training=is_train, updates_collections=None,

~\Anaconda2\envs\image_rec\lib\site-packages\tensorflow\python\layers\convolutional.py in conv2d(inputs, filters, kernel_size, strides, padding, data_format, dilation_rate, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, trainable, name, reuse)
    423       _reuse=reuse,
    424       _scope=name)
--> 425   return layer.apply(inputs)
    426 
    427 

~\Anaconda2\envs\image_rec\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in apply(self, inputs, *args, **kwargs)
    803       Output tensor(s).
    804     """
--> 805     return self.__call__(inputs, *args, **kwargs)
    806 
    807   def _set_learning_phase_metadata(self, inputs, outputs):

~\Anaconda2\envs\image_rec\lib\site-packages\tensorflow\python\layers\base.py in __call__(self, inputs, *args, **kwargs)
    360 
    361       # Actually call layer
--> 362       outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
    363 
    364     if not context.executing_eagerly():

~\Anaconda2\envs\image_rec\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
    718 
    719         # Check input assumptions set before layer building, e.g. input rank.
--> 720         self._assert_input_compatibility(inputs)
    721         if input_list and self._dtype is None:
    722           try:

~\Anaconda2\envs\image_rec\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _assert_input_compatibility(self, inputs)
   1408           spec.min_ndim is not None or
   1409           spec.max_ndim is not None):
-> 1410         if x.shape.ndims is None:
   1411           raise ValueError('Input ' + str(input_index) + ' of layer ' +
   1412                            self.name + ' is incompatible with the layer: '

AttributeError: 'tuple' object has no attribute 'ndims'

提前感谢

1 个答案:

答案 0 :(得分:0)

因此显然将输出放到tf.float32可以解决问题

Map<String, List<String>> map;