以下の関数はtf.keras.layers.Lambda()
、TF2.0に準拠して呼び出そうとしています。inputs
そしてoutputs
テンソルは、3つのカラーチャネルを有する同じ寸法の二つの画像であろう。私の目標は、outputs
テンソルからマスクを抽出し、それをテンソルに適用してinputs
、結果のテンソルを返すことです。テンソルを平坦化する動機は、tf.tensor_scatter_nd_update()
機能の制限によるものです。モデルを作成すると、が値であるupdates
ため、初期化に失敗します。初期化するために2つのテンソルを使用してモデルの外部でこのレイヤーを呼び出すと、熱心な実行で完全に正常に実行されます(テンソルには値が定義されているため)。残念ながら、この関数をで呼び出すと、次のエラーが発生します。indices.shape[0]
None
tf.constant()
x
x
tf.keras.layers.Lambda()
TypeError: can't multiply sequence by non-int of type 'NoneType'
@tf.function
def applyMask(x):
# Extract Tensors
inputs = x[0]
outputs = x[1]
# Flatten the Outputs Tensor and Extract Mask Indices
outputs = tf.reshape(outputs,(tf.size(outputs),))
indices = tf.where(outputs==1.)
indices = tf.cast(indices, tf.int32)
# Construct Updates Tensor from Mask Indices
updates = tf.constant([1.]*indices.shape[0])
# Flatten Input Tensor and Apply Mask
out_dim = inputs.shape
inputs = tf.reshape(inputs,(tf.size(inputs),))
tensor = tf.tensor_scatter_nd_update(inputs, indices, updates)
# Reconstruct Input Into Tensor
tensor = tf.reshape(tensor, out_dim)
return tensor
この複雑である必要はありません。単に行う、
inp1 = Input(shape=(None, None, 3)) # Inputs
inp2 = Input(shape=(None, None, 3)) # Outputs
out = Lambda(lambda x: tf.where(tf.equal(x[1], 1), x[1], x[0]))([inp1, inp2])
そして、あなたも持つことができるheight
とwidth
None
している限りに渡される並列サンプルとしてinp1
とがinp2
あるまったく同じ(形状的に)、tf.where
罰金を動作します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加