【问题标题】:How to change input shape in keras如何在keras中更改输入形状
【发布时间】:2020-08-07 04:20:03
【问题描述】:

我从 GitHub 下载了一个 .h5 文件,并想从中进行预测。我执行了以下代码:

from keras.models import load_model
from PIL import Image
import numpy as np

im = Image.open('img.jpg')
img = np.array(im)

model = load_model('model_weight.h5')

a = model.predict(img)
print(a)

并得到以下错误:

ValueError: Input 0 of layer dense_1 is incompatible with the layer: expected axis -1 of input shape to have value 4096 but received input with shape [None, 300, 3]

这是模型摘要:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 4096)]       0                                            
__________________________________________________________________________________________________
input_2 (InputLayer)            [(None, 34)]         0                                            
__________________________________________________________________________________________________
dropout_1 (Dropout)             (None, 4096)         0           input_1[0][0]                    
__________________________________________________________________________________________________
embedding_1 (Embedding)         (None, 34, 256)      1940224     input_2[0][0]                    
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 128)          524416      dropout_1[0][0]                  
__________________________________________________________________________________________________
lstm_1 (LSTM)                   (None, 34, 256)      525312      embedding_1[0][0]                
__________________________________________________________________________________________________
repeat_vector_1 (RepeatVector)  (None, 34, 128)      0           dense_1[0][0]                    
__________________________________________________________________________________________________
time_distributed_1 (TimeDistrib (None, 34, 128)      32896       lstm_1[0][0]                     
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 34, 256)      0           repeat_vector_1[0][0]            
                                                                 time_distributed_1[0][0]         
__________________________________________________________________________________________________
lstm_2 (LSTM)                   (None, 1000)         5028000     concatenate_1[0][0]              
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 7579)         7586579     lstm_2[0][0]                     
==================================================================================================
Total params: 15,637,427
Trainable params: 15,637,427
Non-trainable params: 0
__________________________________________________________________________________________________
WARNING:tensorflow:Model was constructed with shape (None, 4096) for input Tensor("input_1:0", shape=(None, 4096), dtype=float32), but it was called on an input with incompatible shape (None, 300, 3).

我知道我必须重塑它,但不知道如何去做。任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 使用model.summary()检查模型的输入形状
  • 完成了,但是如何改变形状?
  • 这个模型是用来处理图片的吗?
  • 存在类似问题,希望对您有所帮助! Solution
  • 是的,它适用于图像

标签: python numpy tensorflow keras predict


【解决方案1】:

假设模型接受正方形图像(宽度和高度相等),图像输入的大小应为 (64, 64) 并具有 1 个通道。您可以使用以下步骤使用模型进行图像预测

  1. 将图像从 RGB 转换为灰度
  2. 将图像大小调整为 (64, 64)
  3. 将图像重塑为 (1, 4096)
  4. 将其馈送到网络。

from keras.models import load_model
from PIL import Image
import numpy as np

im = Image.open('img.jpg').convert('L') # Grayscale conversion
im = im.resize((64, 64)) # Resizing to neccessary size. 
img = np.array(im).reshape(1, 4096)  

model = load_model('model_weight.h5')

sentence = np.zeros((1, 34));
sentence = model.predict([img, sentence])
print(sentence)

句子的生成


stop_token_index = 0;  

i = 0;

sentence = np.zeros((1, 34));
while i < sentence.shape[-1]:
    next_word = np.argmax(model.predict([img, sentence]).squeeze())
    if next_word == stop_token_index:
        break
    sentence[0][i] = next_word
    i += 1
print(sentence)

【讨论】:

  • 这正是我已经知道的。我知道我需要调整它的大小和形状,但我不知道怎么做?
  • AssertionError: 无法计算输出 Tensor("dense_3/Identity:0", shape=(None, 7579), dtype=float32
  • 顺便说一句,网络有两个输入。我认为第二个输入不是图像。
  • 第二个输入是什么?我只放了图片
  • 第二个输入,看起来像顺序数据。例如,这可能是一个句子。
猜你喜欢
  • 2017-06-30
  • 2021-06-05
  • 2018-02-16
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
  • 2017-08-14
  • 2018-11-21
  • 1970-01-01
相关资源
最近更新 更多