tensorflow-addons.image 错误,输入错误?

tensorflow-addons.image error, wrong input?

如何在 tensorflow_addons.image 中使用 translate? 我尝试了以下但没有成功

(版本:tensorflow: 2.4.1tensorflow-addons: 0.12.1python: 3.8.8

import tensorflow as tf
import numpy as np
from tensorflow_addons.image import translate as tfa_translate

# 5 images 10x10, BW:
imgs = np.random.rand(5, 10, 10, 1).astype(np.float32)

# vector for x-y translations:
vec = np.random.randn(5, 2).astype(np.float32)

# both of these give me the same error below:
tfa_translate(imgs, vec)
tfa_translate(tf.convert_to_tensor(imgs), tf.convert_to_tensor(vec))

我得到的错误是关于使用 tf 或 np 张量。我试过 tf.convert_to_tensor(imgs) 但没有成功:

NotImplementedError: Cannot convert a symbolic Tensor 
(translate/translations_to_projective_transforms/strided_slice:0) to a numpy array. 
This error` may indicate that you're trying to pass a Tensor to a NumPy call, 
which is not supported

编辑:这些 examples 中图像的所有操作都在我的系统中工作。但是翻译没有。

我能够在这个组合中执行上面的代码 tensorflow: 2.4.1, tensorflow-addons: 0.12.1 and python: 3.7.10。你会成功的。

import sys
import tensorflow as tf
import numpy as np
import tensorflow_addons as tfa
from tensorflow_addons.image import translate as tfa_translate
print(tf.__version__)
print(tfa.__version__)
print(sys.version)

# 5 images 10x10, BW:
imgs = np.random.rand(5, 10, 10, 1).astype(np.float32)

# vector for x-y translations:
vec = np.random.randn(5, 2).astype(np.float32)

# both of these give me the same error below:
tfa_translate(imgs, vec)
tfa_translate(tf.convert_to_tensor(imgs), tf.convert_to_tensor(vec))

输出:

2.4.1
0.12.1
3.7.10 
<tf.Tensor: shape=(5, 10, 10, 1), dtype=float32, numpy=
array([[[[0.73720914],
         [0.05389478],
         [0.00823868],
         [0.81953126],
         [0.1538573 ],
         [0.90768087],
         [0.4161973 ],
         [0.38890013],
         [0.7050291 ],
         [0.20925364]],

        [[0.37302   ],
         [0.4195738 ],
         [0.6881046 ],
         [0.3580131 ],
         [0.47772875],
         [0.5959794 ],
         [0.60560894],
         [0.03321437],
         [0.48405352],
         [0.7554338 ]],

        [[0.8335805 ],
         [0.69475925],
         [0.7356104 ],
         [0.72107244],
         [0.0654715 ],
         [0.7524717 ],
         [0.7242604 ],
         [0.8744024 ],
         [0.4255406 ],
         [0.8113107 ]],

        [[0.6129395 ],
         [0.29516342],
         [0.9028247 ],
         [0.13342927],
         [0.52949995],
         [0.06142981],
         [0.88151217],
         [0.52935964],
         [0.9567349 ],
         [0.8202645 ]],

        [[0.16741282],
         [0.6734363 ],
         [0.1131402 ],
         [0.8026573 ],
         [0.468824  ],
         [0.8553658 ],
         [0.45999104],
         [0.6134152 ],
         [0.56909144],
         [0.41677374]],

        [[0.35705274],
         [0.76314545],
         [0.07150301],
         [0.487003  ],
         [0.13564219],
         [0.45595258],
         [0.33955264],
         [0.9714719 ],
         [0.3500332 ],
         [0.6937466 ]],

        [[0.648089  ],
         [0.6019247 ],
         [0.3049946 ],
         [0.20571095],
         [0.2413046 ],
         [0.03899029],
         [0.85995656],
         [0.73361784],
         [0.83635384],
         [0.7748581 ]],

        [[0.4335603 ],
         [0.5851778 ],
         [0.11063848],
         [0.80494994],
         [0.49294096],
         [0.25171277],
         [0.81821585],
         [0.85311455],
         [0.6131411 ],
         [0.7952116 ]],

        [[0.7456568 ],
         [0.6693832 ],
         [0.05555756],
         [0.11654849],
         [0.674931  ],
         [0.01180321],
         [0.9755547 ],
         [0.4340065 ],
         [0.1364122 ],
         [0.31032914]],

        [[0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ]]],


       [[[0.34124446],
         [0.08647306],
         [0.9612431 ],
         [0.8725844 ],
         [0.6298909 ],
         [0.55960226],
         [0.3880017 ],
         [0.15042423],
         [0.58690244],
         [0.        ]],

        [[0.81984246],
         [0.8341217 ],
         [0.35999405],
         [0.3214622 ],
         [0.27512822],
         [0.905134  ],
         [0.5411199 ],
         [0.5287951 ],
         [0.56175965],
         [0.        ]],

        [[0.7129698 ],
         [0.17935541],
         [0.23184128],
         [0.62122566],
         [0.31914395],
         [0.93782455],
         [0.68015796],
         [0.32218182],
         [0.94558555],
         [0.        ]],

        [[0.9850416 ],
         [0.63286716],
         [0.02245921],
         [0.0168714 ],
         [0.787904  ],
         [0.7566337 ],
         [0.6966046 ],
         [0.74724126],
         [0.12862223],
         [0.        ]],

        [[0.9159414 ],
         [0.44754454],
         [0.4354201 ],
         [0.00121895],
         [0.9533174 ],
         [0.6897298 ],
         [0.5429657 ],
         [0.89288723],
         [0.17915004],
         [0.        ]],

        [[0.35772014],
         [0.25488794],
         [0.7412978 ],
         [0.77173555],
         [0.44523025],
         [0.515731  ],
         [0.6853389 ],
         [0.90855956],
         [0.28565803],
         [0.        ]],

        [[0.9447069 ],
         [0.25103524],
         [0.2835534 ],
         [0.5845979 ],
         [0.61925954],
         [0.5675663 ],
         [0.5097425 ],
         [0.7441513 ],
         [0.4252864 ],
         [0.        ]],

        [[0.71359545],
         [0.98984915],
         [0.25157735],
         [0.00311989],
         [0.59827286],
         [0.2282706 ],
         [0.74741423],
         [0.71861583],
         [0.4377698 ],
         [0.        ]],

        [[0.09316336],
         [0.90708685],
         [0.09424796],
         [0.49117166],
         [0.16317427],
         [0.8758143 ],
         [0.40267643],
         [0.9177538 ],
         [0.962702  ],
         [0.        ]],

        [[0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ]]],


       [[[0.        ],
         [0.        ],
         [0.29238865],
         [0.11071314],
         [0.04251722],
         [0.8379944 ],
         [0.7078936 ],
         [0.9524308 ],
         [0.9807888 ],
         [0.5980042 ]],

        [[0.        ],
         [0.        ],
         [0.6076964 ],
         [0.01608464],
         [0.5086389 ],
         [0.6162034 ],
         [0.13877843],
         [0.35256362],
         [0.49832624],
         [0.8281829 ]],

        [[0.        ],
         [0.        ],
         [0.16112986],
         [0.7567848 ],
         [0.3147346 ],
         [0.37325364],
         [0.7240583 ],
         [0.55212367],
         [0.53437054],
         [0.6456858 ]],

        [[0.        ],
         [0.        ],
         [0.9337465 ],
         [0.91347605],
         [0.20626037],
         [0.76980096],
         [0.42335644],
         [0.25908992],
         [0.11779127],
         [0.92509085]],

        [[0.        ],
         [0.        ],
         [0.42465472],
         [0.0791868 ],
         [0.260981  ],
         [0.98131573],
         [0.75192803],
         [0.94773096],
         [0.56423026],
         [0.41657394]],

        [[0.        ],
         [0.        ],
         [0.06342959],
         [0.14268413],
         [0.62750655],
         [0.40286288],
         [0.5470112 ],
         [0.16235335],
         [0.5923443 ],
         [0.2899487 ]],

        [[0.        ],
         [0.        ],
         [0.46837795],
         [0.64133584],
         [0.19043817],
         [0.3560451 ],
         [0.84368664],
         [0.54878306],
         [0.7572676 ],
         [0.63685185]],

        [[0.        ],
         [0.        ],
         [0.6362246 ],
         [0.9802078 ],
         [0.07812112],
         [0.6737748 ],
         [0.09005726],
         [0.5559371 ],
         [0.8273103 ],
         [0.8465655 ]],

        [[0.        ],
         [0.        ],
         [0.29354632],
         [0.8277752 ],
         [0.24557658],
         [0.7144999 ],
         [0.14932555],
         [0.7992488 ],
         [0.9322711 ],
         [0.7795503 ]],

        [[0.        ],
         [0.        ],
         [0.6565971 ],
         [0.9446715 ],
         [0.30254883],
         [0.87504804],
         [0.64962775],
         [0.13229366],
         [0.3159616 ],
         [0.80296475]]],


       [[[0.        ],
         [0.70018524],
         [0.7575729 ],
         [0.9112962 ],
         [0.7581045 ],
         [0.48528266],
         [0.7747405 ],
         [0.24268304],
         [0.51620704],
         [0.34689027]],

        [[0.        ],
         [0.95382404],
         [0.80364156],
         [0.48280716],
         [0.37804484],
         [0.24970461],
         [0.9051866 ],
         [0.7865404 ],
         [0.06981565],
         [0.44362214]],

        [[0.        ],
         [0.2775535 ],
         [0.9653021 ],
         [0.63792914],
         [0.7292578 ],
         [0.16679579],
         [0.43742415],
         [0.11834401],
         [0.79483396],
         [0.19500774]],

        [[0.        ],
         [0.07943237],
         [0.5328288 ],
         [0.77599657],
         [0.63793445],
         [0.19860882],
         [0.11559258],
         [0.9838874 ],
         [0.25520208],
         [0.5648425 ]],

        [[0.        ],
         [0.05485739],
         [0.68932915],
         [0.46699798],
         [0.42969576],
         [0.18972297],
         [0.46102318],
         [0.9518804 ],
         [0.49459723],
         [0.08304291]],

        [[0.        ],
         [0.3869838 ],
         [0.80616343],
         [0.29453617],
         [0.90575564],
         [0.9491408 ],
         [0.0916879 ],
         [0.36297712],
         [0.9152429 ],
         [0.67923504]],

        [[0.        ],
         [0.22770868],
         [0.04828448],
         [0.59498733],
         [0.8257741 ],
         [0.77833605],
         [0.30659854],
         [0.30935422],
         [0.96905756],
         [0.3825807 ]],

        [[0.        ],
         [0.2682056 ],
         [0.12400189],
         [0.14326625],
         [0.13485809],
         [0.60932964],
         [0.49995294],
         [0.48518673],
         [0.9061716 ],
         [0.6811738 ]],

        [[0.        ],
         [0.5348281 ],
         [0.4997965 ],
         [0.63221145],
         [0.46155205],
         [0.7663521 ],
         [0.91362387],
         [0.2826063 ],
         [0.68864125],
         [0.6800111 ]],

        [[0.        ],
         [0.39924976],
         [0.45846376],
         [0.7625721 ],
         [0.99027014],
         [0.39566088],
         [0.94737506],
         [0.4506151 ],
         [0.24510984],
         [0.89123327]]],


       [[[0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ],
         [0.        ]],

        [[0.        ],
         [0.9952434 ],
         [0.01445136],
         [0.20864646],
         [0.5734403 ],
         [0.9085302 ],
         [0.51775485],
         [0.8855587 ],
         [0.28934476],
         [0.06262866]],

        [[0.        ],
         [0.9793667 ],
         [0.5434468 ],
         [0.2679368 ],
         [0.10213093],
         [0.8542698 ],
         [0.2450935 ],
         [0.11804035],
         [0.8925573 ],
         [0.24042603]],

        [[0.        ],
         [0.4573032 ],
         [0.2561469 ],
         [0.00524052],
         [0.57438296],
         [0.5396885 ],
         [0.8146391 ],
         [0.9708599 ],
         [0.59276104],
         [0.35178396]],

        [[0.        ],
         [0.8042833 ],
         [0.7458325 ],
         [0.5945834 ],
         [0.9203529 ],
         [0.76014346],
         [0.69867104],
         [0.25335407],
         [0.96289504],
         [0.80579585]],

        [[0.        ],
         [0.16236259],
         [0.4947492 ],
         [0.41423783],
         [0.34527972],
         [0.4402823 ],
         [0.47581652],
         [0.82424086],
         [0.6161921 ],
         [0.6575118 ]],

        [[0.        ],
         [0.98747057],
         [0.66196424],
         [0.043115  ],
         [0.9993771 ],
         [0.12203664],
         [0.7465267 ],
         [0.3408288 ],
         [0.94268566],
         [0.8820024 ]],

        [[0.        ],
         [0.9850347 ],
         [0.5541368 ],
         [0.07462564],
         [0.07905959],
         [0.1890236 ],
         [0.6264842 ],
         [0.36688676],
         [0.6111452 ],
         [0.20477323]],

        [[0.        ],
         [0.07814959],
         [0.49131715],
         [0.14614591],
         [0.7088246 ],
         [0.77301365],
         [0.7900142 ],
         [0.6824042 ],
         [0.02469799],
         [0.20656963]],

        [[0.        ],
         [0.27418277],
         [0.2796882 ],
         [0.7702565 ],
         [0.9263017 ],
         [0.96371156],
         [0.9759365 ],
         [0.46440127],
         [0.12412024],
         [0.4896511 ]]]], dtype=float32)>