问题
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
Inputlayer withbatch_shape=(16, 320, 320, 3)for testing but the problem still persitsinputs = 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_estimatorfor conversion. I also create ainput_fnfor 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