【问题标题】:How to batch tensors with different shapes in Tensorflow Data API?如何在 Tensorflow Data API 中批量处理不同形状的张量?
【发布时间】:2021-04-06 14:50:17
【问题描述】:

在学习了Research paper 之后,我正在尝试从头开始实现一个全卷积神经网络。由于 FCN 适用于任何大小的输入,因此我很难为它实现 DataLoader。我正在尝试使用 batch() 函数对输入进行批处理,但由于可变长度张量而出现错误。有没有可行的方法来批量处理任意形状的张量?

我的代码:

import tensorflow as tf
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import os
from albumentations import ElasticTransform,VerticalFlip,RandomSizedCrop,Compose,RandomRotate90

# Train and Val contains the respective paths for [TrainImage,Labels] and [ValImage,Labels] respectively.

num_classes = 21
Train = tf.data.Dataset.from_tensor_slices(Train)
Val = tf.data.Dataset.from_tensor_slices(Val)


def Create_Mask(Img):
  Seg_Labels = np.zeros((Img.shape[0],Img.shape[1],num_classes),dtype=np.float32)
  for class_ in range(num_classes):
    Seg_Labels[:,:,class_] = (Img == class_)
  return tf.cast(Seg_Labels,dtype=tf.float32)


def Create_PreProcess_Mask_Img(Instance):
  Img = np.asarray(Image.open(Instance[0].numpy()))
  Mask = np.asarray(Image.open(Instance[1].numpy()))    
  # Since Mask is in 'P' mode it will automatically convert to labels using Color Palette 

  Normalization = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)

  if tf.random.uniform(()) > 0.5:  # Applying data Augmentation
    Num = tf.random.uniform((),minval=0,maxval=3,dtype=tf.int32)
    if Num == 0:
      aug = ElasticTransform(p=1)
      Augmented = aug(image = Img,mask = Mask)

    elif Num == 1:
      Height,Width = Img.shape[0],Img.shape[1]
      aug = RandomSizedCrop(min_max_height=(50,101),height=Height,width=Width,p = 1)
      Augmented = aug(image = Img,mask = Mask)

    elif Num == 2:
      aug = Compose([VerticalFlip(p=0.5),RandomRotate90(p=0.5)])
      Augmented = aug(image = Img,mask = Mask)

    Img = Augmented["image"]
    Mask = Augmented["mask"]
  
  return Normalization(Img),Create_Mask(Mask)


def Preprocess(Instance):
  Img,Mask = tf.py_function(Create_PreProcess_Mask_Img,[Instance],[tf.float32,tf.float32])
  return tf.ensure_shape(Img,[None,None,3]),tf.ensure_shape(Mask,[None,None,num_classes])  
  #tf.ensure_shape returns the matrix if shape matches else error

def DataLoader(dataset,BATCH_SIZE = 32,BUFFER_SIZE = 256):
  data = dataset.map(Preprocess,num_parallel_calls = tf.data.AUTOTUNE)
  data = data.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat(1)
  data = data.prefetch(buffer_size = tf.data.AUTOTUNE)
  return data

Train = DataLoader(Train)
Val = DataLoader(Val)

for X,Y in Train.take(1):
  print(X.shape)
  print(Y.shape)

堆栈跟踪:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/context.py in execution_mode(mode)
   2112       ctx.executor = executor_new
-> 2113       yield
   2114     finally:

9 frames
InvalidArgumentError: Cannot batch tensors with different shapes in component 0. First element had shape [500,375,3] and element 1 had shape [500,383,3]. [Op:IteratorGetNext]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/executor.py in wait(self)
     67   def wait(self):
     68     """Waits for ops dispatched in this executor to finish."""
---> 69     pywrap_tfe.TFE_ExecutorWaitForAllPendingNodes(self._handle)
     70 
     71   def clear_error(self):

InvalidArgumentError: Cannot batch tensors with different shapes in component 0. First element had shape [500,375,3] and element 1 had shape [500,383,3].

【问题讨论】:

标签: python tensorflow deep-learning conv-neural-network


【解决方案1】:

您不能拥有具有不同图像大小的批次,但如果您想解决该问题,您可以:

  • 调整所有图像的大小以形成一个共同的形状(如果图像的比例接近,则影响最小)look at this link
  • 将批次的大小减少到一张图像(这种方法会使训练非常不稳定)。

你可以去this post看看

【讨论】:

  • 请不要发布仅链接的答案。用你自己的代码解释解决方案。
猜你喜欢
  • 2021-01-06
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多