Tensorflow: 动态分割图像成块

我是Tensorflow的新手,需要帮助管理我的图像数据集。由于图像集非常大,读取和训练阶段需要重叠。每个图像的宽度像素远大于高度。我需要将宽图像分割成正方形部分。由于图像的宽度不同,分割的部分数量也会变化。

以下是我的示例代码,我需要帮助使其工作:

def main(unused_argv):   filenames = tf.constant(['im_01.png', 'im_02.png', 'im_03.png', 'im_04.png'])   labels = tf.constant([0, 1, 0, 1])   def parse_fn(file, label):      image = tf.image.decode_png(tf.read_file(file), channels=1)       shape_list = image.get_shape().as_list()       image_width = shape_list[0]       image_height = shape_list[1]       num_pieces = int(image_width/image_height)       Xy_pairs = []       for i in range(num_pieces):          offset_width = i * image_height          sub_image = tf.image.crop_to_bounding_box(image, 0, offset_width, image_height, image_height)          sub_image = tf.image.resize_images(sub_image, [128, 128])          Xy_pairs.append((sub_image, label))       return Dataset.from_tensor_slices(Xy_pairs)dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))dataset = dataset.flat_map(parse_fn)sess = tf.InteractiveSession()it = dataset.make_one_shot_iterator()while True:    print('OUT: ' + it.get_next().eval())

错误看起来像这样 TypeError: unsupported operand type(s) for /: 'NoneType' and 'NoneType',因为Tensorflow在计算数据流图时不知道图像的大小。我几乎可以肯定我需要为image.get_shape()设置一个占位符。我希望社区能帮我解决这个问题。


回答:

正如评论中指出的,您的image形状在图中未定义,因此在评估之前尝试获取值(通过image.get_shape().as_list())会返回(None, None, None)

为了使图中的一切都动态,您可以使用tf.while_loop()来代替当前的for循环提取图像块,如下所示(此代码可能需要一些调整):

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注