本文介绍了tf.Estimator.train抛出as_list()在未知TensorShape上未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义input_func,并将一个keras模型转换为tf.Estimator进行训练.但是,它总是让我出错.

I created a custom input_func and converted a keras model into tf.Estimator for training. However, it keeps throwing me error.

  • 这是我的模型摘要.我试图用batch_shape=(16, 320, 320, 3)设置Input层进行测试,但是问题仍然存在

  • Here is my model summary. I have attempted to set the Input layer with batch_shape=(16, 320, 320, 3) for testing but the problem still persits

inputs  = Input(batch_shape=(16, 320, 320, 3), name='input_images')
outputs = yolov2.predict(intputs)
model   = Model(inputs, outputs)

model.compile(optimizer= tf.keras.optimizers.Adam(lr=learning_rate),
              loss     = compute_loss)

  • 我使用了tf.keras.estimator.model_to_estimator进行转换.我还创建了input_fn进行培训:

  • I used tf.keras.estimator.model_to_estimator for conversion. I also create a input_fn for training:

    def input_fun(images, labels, batch_size, shuffle=True):
         dataset = create_tfdataset(images, labels)
         dataset = dataset.shuffle().batch(batch_size)
         iterator = dataset.make_one_shot_iterator()
         images, labels = iterator.next()
    
         return {'input_images': images}, labels
    
    estimator = tf.keras.estimator.model_to_estimator(keras_model=model)
    estimator.train(input_fn = lambda: input_fn(images, labels, 32),
                    max_steps = 1000)
    

  • 这引发了我这个错误

  • And it throws me this error

    input_tensor = Input(tensor=x, name='input_wrapper_for_' + name)
    ...
    File "/home/dat/anaconda3/envs/webapp/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 1309, in __init__
    self._batch_input_shape = tuple(input_tensor.get_shape().as_list())
    
    "as_list() is not defined on an unknown TensorShape.")
     ValueError: as_list() is not defined on an unknown TensorShape.
    

  • 推荐答案

    我遇到了同样的问题.在input_fun中,如果查看"return {'input_images':images},标签"行中的图像,则会看到张量没有形状.您必须为每个图像调用set_shape.查看 https://github.com/tensorflow/models/blob/master/official/resnet/imagenet_main.py ,他们调用vgg_preprocessing.preprocess_image设置形状

    I had the same problem.In input_fun, if you look at images in line "return {'input_images': images}, labels", you'll see that your tensor has no shape. You have to call set_shape for each image. Look at https://github.com/tensorflow/models/blob/master/official/resnet/imagenet_main.py, they call vgg_preprocessing.preprocess_image to set the shape

    这篇关于tf.Estimator.train抛出as_list()在未知TensorShape上未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-23 22:21