tensorflow.python.framework.errors_impl.InvalidArgumentError如何解决?

import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split

np.random.seed(4213)

data = np.random.randint(low=1,high=29, size=(500, 160, 160, 10)) 
labels = np.random.randint(low=0,high=5, size=(500, 160, 160)) 
nclass = len(np.unique(labels))
print (nclass)

samples, width, height, nbands = data.shape


X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.25, random_state=421)

print (X_train.shape)
print (y_train.shape)

arch = tf.keras.applications.VGG16(input_shape=[width, height, nbands],
                      include_top=False,
                      weights=None)

model = tf.keras.Sequential()
model.add(arch)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(nclass))

model.compile(optimizer = tf.keras.optimizers.Adam(0.0001),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(),
              metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
    

model.fit(X_train,
          y_train,                                 
          epochs=3,
          batch_size=32,
          verbose=2)


res = model.predict(X_test)
print(res.shape)

semantic segmentation运行以上代码时,发生了异常:

InvalidArgumentError
 Incompatible shapes: [32,160,160] vs. [32]
     [[node Equal (defined at c...:38) ]] [Op:__inference_train_function_1815]


tensorflow.python.framework.errors_impl.InvalidArgumentError
伊巴龙

你的问题来自于最后一层的大小(以避免这些错误总是希望使用Python常数N_IMAGESWIDTHHEIGHTN_CHANNELSN_CLASSES):

用于图像分类

您应该为每个图像分配一个标签。尝试切换labels

import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split

np.random.seed(4213)

N_IMAGES, WIDTH, HEIGHT, N_CHANNELS = (500, 160, 160, 10)
N_CLASSES  = 5

data = np.random.randint(low=1,high=29, size=(N_IMAGES, WIDTH, HEIGHT, N_CHANNELS)) 
labels = np.random.randint(low=0,high=N_CLASSES, size=(N_IMAGES)) 
#...

用于语义分割

确保您的分类器(网络的最后一层)的大小相应。在这种情况下,每个像素需要1个类:

#...
model = tf.keras.Sequential()
model.add(arch)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(width * height))
model.add(tf.keras.layers.Reshape([width , height]))
#...

这是最简单的方法。相反,您可以设置多个反卷积层以用作分类器,甚至可以将arch体系结构翻转并使用它来生成分类结果。正交地,您可以one_hot对标签执行编码,从而将其扩展为N_CLASSES,从而有效地乘以最后一层中的神经元数量。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

tensorflow.python.framework.errors_impl.InvalidArgumentError:无效的参数:断言失败:

来自分类Dev

tensorflow.python.framework.errors_impl.InvalidArgumentError:预期的大小[0]在[0,512]中,但得到891 [Op:Slice]

来自分类Dev

Tensorflow retrain.py tensorflow.python.framework.errors_impl.FailedPreconditionError

来自分类Dev

带有 Pycharm 的 Tensorflow:tensorflow.python.framework.errors_impl.InternalError 错误上升:cudaGetDevice() 失败

来自分类Dev

Tensorflow:freeze_graph - tensorflow.python.framework.errors_impl.DataLossError

来自分类Dev

在 Tensorflow 对象检测中评估预训练模型时出错 (tensorflow.python.framework.errors_impl.NotFoundError:)

来自分类Dev

在运行“ model_main_tf2.py”以在tensorflow中训练对象检测模型时出现“ tensorflow.python.framework.errors_impl.FailedPreconditionError”

来自分类Dev

Google Inception tensorflow.python.framework.errors.ResourceExhaustedError

来自分类Dev

Python的Robot Framework关键字:字符串或IP不匹配时出错。如何解决这个问题?

来自分类Dev

我如何解决TensorFlow中的``gpu内存不足''

来自分类Dev

python-如何解决KeyError:2?

来自分类Dev

如何解决Python中的内存错误

来自分类Dev

Python / JSON:如何解决UnicodeDecodeError

来自分类Dev

Python-如何解决UnicodeEncodeError

来自分类Dev

如何解决python中的属性错误

来自分类Dev

如何解决Python Gekko中的积分?

来自分类Dev

如何解决Python诗歌依赖错误

来自分类Dev

如何解决Python 3.6 itertools错误?

来自分类Dev

Python:如何解决此类继承

来自分类Dev

如何解决python中的拆分问题?

来自分类Dev

如何解决Python中的编码错误

来自分类Dev

如何解决Python中的编码问题

来自分类Dev

如何解决 Python 中的“MissingSchema”错误?

来自分类Dev

如何解决python中的这个AttributeError?

来自分类Dev

如何解决 Python 中的 KeyError 问题?

来自分类Dev

如何解决Microsoft Bot Framework中的内容类型错误?

来自分类Dev

如何解决Microsoft Bot Framework中的内容类型错误?

来自分类Dev

如何解释TensorFlow输出?

来自分类Dev

shout-python segmentation fault我该如何解决?

Related 相关文章

  1. 1

    tensorflow.python.framework.errors_impl.InvalidArgumentError:无效的参数:断言失败:

  2. 2

    tensorflow.python.framework.errors_impl.InvalidArgumentError:预期的大小[0]在[0,512]中,但得到891 [Op:Slice]

  3. 3

    Tensorflow retrain.py tensorflow.python.framework.errors_impl.FailedPreconditionError

  4. 4

    带有 Pycharm 的 Tensorflow:tensorflow.python.framework.errors_impl.InternalError 错误上升:cudaGetDevice() 失败

  5. 5

    Tensorflow:freeze_graph - tensorflow.python.framework.errors_impl.DataLossError

  6. 6

    在 Tensorflow 对象检测中评估预训练模型时出错 (tensorflow.python.framework.errors_impl.NotFoundError:)

  7. 7

    在运行“ model_main_tf2.py”以在tensorflow中训练对象检测模型时出现“ tensorflow.python.framework.errors_impl.FailedPreconditionError”

  8. 8

    Google Inception tensorflow.python.framework.errors.ResourceExhaustedError

  9. 9

    Python的Robot Framework关键字:字符串或IP不匹配时出错。如何解决这个问题?

  10. 10

    我如何解决TensorFlow中的``gpu内存不足''

  11. 11

    python-如何解决KeyError:2?

  12. 12

    如何解决Python中的内存错误

  13. 13

    Python / JSON:如何解决UnicodeDecodeError

  14. 14

    Python-如何解决UnicodeEncodeError

  15. 15

    如何解决python中的属性错误

  16. 16

    如何解决Python Gekko中的积分?

  17. 17

    如何解决Python诗歌依赖错误

  18. 18

    如何解决Python 3.6 itertools错误?

  19. 19

    Python:如何解决此类继承

  20. 20

    如何解决python中的拆分问题?

  21. 21

    如何解决Python中的编码错误

  22. 22

    如何解决Python中的编码问题

  23. 23

    如何解决 Python 中的“MissingSchema”错误?

  24. 24

    如何解决python中的这个AttributeError?

  25. 25

    如何解决 Python 中的 KeyError 问题?

  26. 26

    如何解决Microsoft Bot Framework中的内容类型错误?

  27. 27

    如何解决Microsoft Bot Framework中的内容类型错误?

  28. 28

    如何解释TensorFlow输出?

  29. 29

    shout-python segmentation fault我该如何解决?

热门标签

归档