这几天整理vision transformer 的代码,遇到这个问题,尝试了各种方法,最终得到解决,特此记录一下,也为了帮助遇到同样问题的同学。
TypeError: Failed to convert object of type <class ‘list’> to Tensor. Contents: [None, 144, 64]. Consider casting elements to a supported type.
TypeError: Failed to convert object of type <class ‘tuple’> to Tensor. Contents: [None, 144, 64]. Consider casting elements to a supported type.
代码出错的位置:
def call(self, inputs, **kwargs):B, H, W, C = inputs.shapex = self.proj(inputs)# [B, H, W, C] -> [B, H*W, C]x = tf.reshape(x, [B, self.num_patches, self.embed_dim])return x
我查了网上现存的一些解决方案:
诸如:https://blog.csdn.net/baoyan2015/article/details/114694546
还有
https://blog.csdn.net/qq_35523233/article/details/88140567?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-13.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-13.control
https://blog.csdn.net/qq_36783848/article/details/87979993?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.control
https://blog.csdn.net/ThorKing01/article/details/89394649?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control
https://blog.csdn.net/qq_37285574/article/details/103054440?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control
上面的答案都不符合我要解决的问题,最终在这个解决方案中找到灵感:
https://blog.csdn.net/suiyueruge1314/article/details/105843759
即,a.shape : TensorShape类型 (不符合计算类型)和 tf.shape(a) : Tensor类型(符合计算类型)
两种取出维度的方式的问题。不能用 inputs.shape 取出代码中的B, H, W, C,而要用 tf.shape(inputs),即
def call(self, inputs, **kwargs):B, H, W, C = tf.shape(inputs)[0], tf.shape(inputs)[1], tf.shape(inputs)[2], tf.shape(inputs)[3]x = self.proj(inputs)# [B, H, W, C] -> [B, H*W, C]x = tf.reshape(x, [B, self.num_patches, self.embed_dim])return x
目前上面的回答中,都在解决tf.reshape 的None的问题。而目前发现究其原因不在这个地方,另一方面,我自己查官方文档的过程中,也找到另一种解决方法,就是不用None,用keras API:
self.flatten = tf.keras.layers.Reshape(target_shape=(self.num_patches, self.embed_dim,))def call(self, inputs, **kwargs):x = self.proj(inputs)x = self.flatten(x)
这样就不显示的写 B 这个维度,依然得到同样的输出。谢谢各位批评指正!也感谢
Zero_to_zero1234 的 https://blog.csdn.net/suiyueruge1314/article/details/105843759
tensorflow 官方https://tensorflow.google.cn/api_docs/python/tf/keras/layers/Reshape?hl=en