Keras将图像视为数组的数组,而不是单个图片。

huangapple go评论68阅读模式
英文:

Keras treat Image as array of arrays not a single picture

问题

问题可能出在你的输入图像形状重塑上。在神经网络中,输入的形状必须与模型的输入层匹配。你的模型的输入层期望形状是(1, 240, 256, 3),但你的 picture 数组似乎已经具有正确的形状(1, 240, 256, 3)

问题可能出在你对 picture 的重塑方式上。请确保在调用self.model.predict(picture, verbose=1)之前,picture 的形状是(1, 240, 256, 3),而不是更多的嵌套维度。

如果问题仍然存在,还请检查模型的架构,确保模型的输出层是正确的,并且适合你的问题。如果输出仍然不正确,可能需要进一步调查模型和数据的问题。

英文:

So I have an NN inside of a class

      self.model = Sequential()
      self.model.add(Conv2D(50, (3, 3), activation='relu', input_shape=(240,256,3)))
      self.model.add(Dense(264,activation='relu'))
      self.model.add(Dense(7,activation='relu'))
      self.model.compile(optimizer=Adam(lr=0.001),loss='categorical_crossentropy',metrics=['accuracy'])

and I have an array that is in shape (240, 256, 3)

print(picture.shape) #(240, 256, 3)
picture = np.reshape(picture,(1,240,256,3))

and then try to

self.model.predict(picture,verbose=1)

but instead of output like this [ 0. 25.21973 0. 0. 0. 1.8569145 0.] I got something like

[[[[ 0.         25.21973     0.         ...  0.          1.8569145
     0.        ]
   [ 0.         25.21973     0.         ...  0.          1.8569145
     0.        ]
   [ 0.         25.21973     0.         ...  0.          1.8569145
     0.        ]
   ...
  [[ 0.         14.3257885   0.         ...  1.7455587   0.
     0.        ]
   [ 0.         25.417042    0.         ...  0.          7.501096
     0.        ]
   [ 0.         24.028965    0.         ... 14.10364     0.
     0.        ]
   ...
   [ 0.         17.480661    0.         ...  3.4586341   0.
     0.        ]]

  [[ 0.         21.477276    0.         ...  0.          0.
     0.        ]
   [ 0.         15.683931    0.         ...  0.          0.
     0.        ]
   [ 0.         10.419488    0.         ...  0.          0.29006004
     0.        ]
   ...

   [ 0.          7.038389    0.         ...  0.          0.
     0.        ]]

  [[ 0.         18.099554    0.         ...  0.          0.
     0.        ]
   [ 0.          8.225699    0.         ...  0.751534    0.
     0.        ]
   [ 0.         13.256775    0.         ...  0.          2.1235647
     0.        ]]]]

can you tell me what is the problem?

答案1

得分: 2

默认情况下,Keras的Dense层在输入的最后一个维度上操作,因此当您输入一张图像时,会得到另一张图像作为输出。问题出在您的模型上。如果您使用model.summary(),您会看到模型的输出形状实际上与通过predict看到的形状相同。

解决方法很简单,在最后一个Conv2D层之后添加一个Flatten层:

self.model = Sequential()
self.model.add(Conv2D(50, (3, 3), activation='relu', input_shape=(240, 256, 3)))
self.model.add(Flatten())
self.model.add(Dense(264, activation='relu'))
self.model.add(Dense(7, activation='relu'))

然后您的模型将按预期工作。

英文:

By default, Keras' Dense layers operate on the last dimension of the input, so when you input an image, you get another image as output. The problem is with your model. If you use model.summary() you will see that the output shape of your model is actually the one you are seeing through predict.

The solution is simple, add a Flatten layer after the last Conv2D:

self.model = Sequential()
self.model.add(Conv2D(50, (3, 3), activation='relu', input_shape=(240,256,3)))
self.model.add(Flatten())
self.model.add(Dense(264,activation='relu'))
self.model.add(Dense(7,activation='relu'))

Then your model will behave as expected.

huangapple
  • 本文由 发表于 2020年1月7日 01:39:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定