tf.Estimator.train throws as_list() is not defined on an unknown TensorShape

牧云@^-^@ 提交于 2020-01-30 06:58:48

问题


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

  • 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)
    
  • 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.
    

回答1:


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



来源:https://stackoverflow.com/questions/48136804/tf-estimator-train-throws-as-list-is-not-defined-on-an-unknown-tensorshape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!