2017-11-11 141 views
3

我有一個程序性生成(無限)數據源,並試圖使用它作爲高級別Tensorflow Estimator的輸入來訓練基於圖像的3D物體檢測器。「TypeError:'張量'對象是不可迭代的'與張量流量的錯誤估計器

我設置的數據集,就像在Tensorflor估計Quickstart,我dataset_input_fn收益特徵的元組和標籤Tensor的,就像Estimator.train函數指定,這該怎麼tutorial shows,但我得到一個錯誤時,試圖調用列車功能:

TypeError: 'Tensor' object is not iterable.

我在做什麼錯?


def data_generator(): 
     """ 
     Generator for image (features) and ground truth object positions (labels) 

     Sample an image and object positions from a procedurally generated data source 
     """ 
     while True: 
      source.step() # generate next data point 

      object_ground_truth = source.get_ground_truth() # list of 9 floats 
      cam_img = source.get_cam_frame() # image (224, 224, 3) 
      yield (cam_img, object_ground_truth) 

    def dataset_input_fn(): 
     """ 
     Tensorflow `Dataset` object from generator 
     """ 

     dataset = tf.data.Dataset.from_generator(data_generator, (tf.uint8, tf.float32), \ 
      (tf.TensorShape([224, 224, 3]), tf.TensorShape([9]))) 
     dataset = dataset.batch(16) 

     iterator = dataset.make_one_shot_iterator() 

     features, labels = iterator.get_next() 
     return features, labels 

    def main(): 
     """ 
     Estimator [from Keras model](https://www.tensorflow.org/programmers_guide/estimators#creating_estimators_from_keras_models) 

     Try to call `est_vgg.train()` leads to the error 
     """ 
     .... 
     est_vgg16 = tf.keras.estimator.model_to_estimator(keras_model=keras_vgg16) 
     est_vgg16.train(input_fn=dataset_input_fn, steps=10) 
     .... 

這裏是full code

(注:事情是從這個問題不同的名稱)

這裏是堆棧跟蹤:

Traceback (most recent call last): 
    File "./rock_detector.py", line 155, in <module> 
    main() 
    File "./rock_detector.py", line 117, in main 
    est_vgg16.train(input_fn=dataset_input_fn, steps=10) 
    File "/usr/local/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 302, in train 
    loss = self._train_model(input_fn, hooks, saving_listeners) 
    File "/usr/local/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 711, in _train_model 
    features, labels, model_fn_lib.ModeKeys.TRAIN, self.config) 
    File "/usr/local/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 694, in _call_model_fn 
    model_fn_results = self._model_fn(features=features, **kwargs) 
    File "/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/estimator.py", line 145, in model_fn 
    labels) 
    File "/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/estimator.py", line 92, in _clone_and_build_model 
    keras_model, features) 
    File "/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/estimator.py", line 58, in _create_ordered_io 
    for key in estimator_io_dict: 
    File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 505, in __iter__ 
    raise TypeError("'Tensor' object is not iterable.") 
TypeError: 'Tensor' object is not iterable. 
+0

我想你只是想:'get_next = iterator.get_next(); est_vgg16.train(input_fn = get_next,steps = 10',但我沒有使用keras,所以我不完全熟悉那裏使用的'.train'函數。 –

+0

您可以分享錯誤的完整堆棧跟蹤? – mrry

+0

使用堆棧跟蹤進行了更新後,很難理解高級api背後發生了什麼,我通過切換到tf的更低級別的接口來實現儘可能多的工作,只是用發電機手動「餵食」,然而關於高級別api的好處是,它可以處理所有的訓練和細節,並且可以優化處理。 – matwilso

回答

3

讓您的輸入功能返回這樣的功能詞典:

def dataset_input_fn(): 
    ... 
    features, labels = iterator.get_next() 
    return {'image': features}, labels 
+0

解決了這個問題,謝謝。必須在'dataset_input_fn'中將'tf.uint8'更改爲'tf.float32'。 – matwilso

相關問題