张量元组的输入形状错误

时间:2020-06-08 17:54:07

标签: python numpy tensorflow

我有一个图像张量的元组,每个张量具有(256,256,3)的形状,并且在元组中有1000个。当我将其提供给tensorflow输入时,它表示我的形状错误-收到的形状为[None,256,3]。如何将其转换为TF满意的数据类型?当我尝试images = np.array(list(images))时,它会冻结(或花了我很长时间才放弃)。

我对Python / Numpy / Tensorflow还是很陌生,因此感谢您的反馈。谢谢!

这是我的代码的相关部分:

 _root, dirs, files = next(os.walk(TRAINING_PATH))

def decode_img(img):
  # convert the compressed string to a 3D uint8 tensor
  img = tf.image.decode_png(img, channels=3)
  # Use `convert_image_dtype` to convert to floats in the [0,1] range.
  img = tf.image.convert_image_dtype(img, tf.float32)
  # resize the image to the desired size.
  return tf.image.resize(img, [IMG_HEIGHT,IMG_WIDTH])


# Returns a mask array where 0 is null, 1='A' ... 26='Z'
def processMasks(path):
    _root, _dirs, files = next(os.walk(path))
    combined_mask = np.full((IMG_HEIGHT, IMG_WIDTH,1), 0, dtype=np.uint8)
    for mask_path in files:
      mask = tf.keras.preprocessing.image.load_img(path+"\\"+mask_path, target_size=[IMG_HEIGHT,IMG_WIDTH],color_mode="grayscale")
      mask = tf.keras.preprocessing.image.img_to_array(mask)
      letter_index = mask_path.index('_')+1
      mask_letter = mask_path[letter_index]
      letter_code = ord(mask_letter)-ord('A') + 1
      # Only mask where it is above middle gray
      combined_mask = np.where(mask>=128,letter_code,combined_mask)
    return combined_mask


def processTrain(folder, path = TRAINING_PATH):
    img = decode_img(tf.io.read_file(path+folder+"\\"+RENDER_NAME))
    mask = processMasks(path+folder+"\\"+MASKS_PATH)
    mask = mask[:,:,0] # remove last dimension (single-value)
    # as the mask uses 0 as empty, let's push that to 255 and move A->0, z->25 (etc.)
    # This way, the one-hot will ignore 255 (anything >25)
    mask -= 1
    mask = tf.one_hot(mask,26)
    return img, mask

dataset = [processTrain(path) for path in dirs]

# Unzip the tuples
images, masks = zip(*dataset)   

############# Set up Model #################
mask_model = tf.keras.applications.MobileNetV2(input_shape=[256,256,3], classes=26, include_top=False)
mask_model.compile(loss="categorical_crossentropy",metrics=['accuracy'], optimizer='adam')
mask_model.fit(images,masks, batch_size=100,epochs=5)

我得到的错误是:

>>> mask_model.fit(images,masks, batch_size=100,epochs=5)
Epoch 1/5
WARNING:tensorflow:Model was constructed with shape (None, 256, 256, 3) for input Tensor("input_2:0", shape=(None, 256, 256, 3), dtype=float32), but it was called on an input with incompatible shape (None, 256, 3).
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\keras\engine\training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\keras\engine\training.py", line 848, in fit
    tmp_logs = train_function(iterator)
  File "C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\eager\def_function.py", line 580, in __call__
    result = self._call(*args, **kwds)
  File "C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\eager\def_function.py", line 627, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\eager\def_function.py", line 505, in _initialize
    self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
  File "C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\eager\function.py", line 2446, in _get_concrete_function_internal_garbage_collected
    graph_function, _, _ = self._maybe_define_function(args, kwargs)
  File "C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\eager\function.py", line 2777, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\eager\function.py", line 2657, in _create_graph_function
    func_graph_module.func_graph_from_py_func(
  File "C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\framework\func_graph.py", line 981, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\eager\def_function.py", line 441, in wrapped_fn
    return weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\framework\func_graph.py", line 968, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\keras\engine\training.py:571 train_function  *
        outputs = self.distribute_strategy.run(
    C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:951 run  **
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2290 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2649 _call_for_each_replica
        return fn(*args, **kwargs)
    C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\keras\engine\training.py:531 train_step  **
        y_pred = self(x, training=True)
    C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:927 __call__
        outputs = call_fn(cast_inputs, *args, **kwargs)
    C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\keras\engine\network.py:717 call
        return self._run_internal_graph(
    C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\keras\engine\network.py:888 _run_internal_graph
        output_tensors = layer(computed_tensors, **kwargs)
    C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:885 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs,
    C:\Data\Environments\python\machineLearning\lib\site-packages\tensorflow\python\keras\engine\input_spec.py:176 assert_input_compatibility
        raise ValueError('Input ' + str(input_index) + ' of layer ' +

    ValueError: Input 0 of layer Conv1_pad is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 256, 3]

1 个答案:

答案 0 :(得分:0)

我通过在张量列表上使用tf.stack解决了这个问题