在Google Colab上掩盖TPU的R-CNN [英] Mask R-CNN for TPU on Google Colab

查看:117
本文介绍了在Google Colab上掩盖TPU的R-CNN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用Google Colab TPU构建图像分割深度学习模型.我们的模型是Mask R-CNN.

We are trying to build an image segmentation deep learning model using Google Colab TPU. Our model is Mask R-CNN.

TPU_WORKER = 'grpc://' + os.environ['COLAB_TPU_ADDR']

import tensorflow as tf
tpu_model = tf.contrib.tpu.keras_to_tpu_model(
model.keras_model,
strategy=tf.contrib.tpu.TPUDistributionStrategy(
tf.contrib.cluster_resolver.TPUClusterResolver(TPU_WORKER)))

但是在将我们的Mask R-CNN模型转换为TPU模型时遇到了问题,如下所示.

However I am running into issues while converting our Mask R-CNN model to TPU model as pasted below.

ValueError: 
Layer <keras.engine.topology.InputLayer object at 0x7f58574f1940> has a 
variable shape in a non-batch dimension.  TPU models must
have constant shapes for all operations.

You may have to specify `input_length` for RNN/TimeDistributed layers.

Layer: <keras.engine.topology.InputLayer object at 0x7f58574f1940>
Input shape: (None, None, None, 3)
Output shape: (None, None, None, 3)

感谢任何帮助.

推荐答案

Google最近发布了代码,看起来他们用固定的输入大小定义了模型,以解决您遇到的问题.

Google recently released a tutorial on getting Mask R-CNN going on their TPUs. For this, they are using an experimental model for Mask RCNN on Google's TPU github repository (under models/experimental/mask_rcnn). Looking through the code, it looks like they define the model with a fixed input size to overcome the issue you are seeing.

有关更多说明,请参见下文

See below for more explanation:

正如@ aman2930所指出的那样,您的输入张量的形状不是静态的.这是行不通的,因为Tensorflow 使用XLA编译模型以使用TPU ,并且XLA必须具有所有在编译时定义的张量形状.在上面的链接中,网站专门指出了这一点:

As @aman2930 points out, the shape of your input tensor is not static. This won't work because Tensorflow compiles models with XLA to use a TPU and XLA must have all tensor shapes defined at compile time. In the link above, the website specifically calls this out:

静态形状

在常规使用期间,TensorFlow会尝试在图形构建期间确定每个tf.Tensor的形状.期间 执行任何未知的形状尺寸是动态确定的,请参见 张量形状以获取更多详细信息.

During regular usage TensorFlow attempts to determine the shapes of each tf.Tensor during graph construction. During execution any unknown shape dimensions are determined dynamically, see Tensor Shapes for more details.

要在Cloud TPU上运行,请使用XLA编译TensorFlow模型. XLA 使用类似的系统在编译时确定形状. XLA 要求在编译时静态定义所有张量尺寸 时间.所有形状的求值必须为常数,而不是依赖于 外部数据或有状态操作(例如变量或随机变量) 数字生成器.

To run on Cloud TPUs TensorFlow models are compiled using XLA. XLA uses a similar system for determining shapes at compile time. XLA requires that all tensor dimensions be statically defined at compile time. All shapes must evaluate to a constant, and not depend on external data, or stateful operations like variables or a random number generator.

在文档的下侧,他们提到输入功能在CPU上运行,因此不受静态XLA大小的限制.他们指出问题是批次大小,而不是图像大小:

That side, further down the document, they mention that the input function is run on the CPU, so isn't limited by static XLA sizes. They point to batch size being the issue, not image size:

静态形状和批量大小

Static shapes and batch size

由您生成的输入管道 input_fn在CPU上运行.因此,它基本上不受严格的静态限制 XLA/TPU环境施加的形状要求.唯一的那个 要求是从输入管道馈送的成批数据 根据标准确定,TPU的形状是静态的 TensorFlow形状推断算法.中间张量可自由 具有动态形状.如果形状推断失败,但形状为 已知可以使用tf.set_shape()施加正确的形状.

The input pipeline generated by your input_fn is run on CPU. So it is mostly free from the strict static shape requirements imposed by the XLA/TPU environment. The one requirement is that the batches of data fed from your input pipeline to the TPU have a static shape, as determined by the standard TensorFlow shape inference algorithm. Intermediate tensors are free to have a dynamic shapes. If shape inference has failed, but the shape is known it is possible to impose the correct shape using tf.set_shape().

因此,您可以通过重新构建模型以使其具有固定的批次大小或使用

So you could fix this by reformulating your model to have fixed batch size or to use tf.contrib.data.batch_and_drop_remainder as they suggest.

这篇关于在Google Colab上掩盖TPU的R-CNN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆