model.predict 不适用于 Keras 自定义层(推理错误)
Posted
技术标签:
【中文标题】model.predict 不适用于 Keras 自定义层(推理错误)【英文标题】:model.predict doesn't work with Keras Custom Layer (inference error) 【发布时间】:2022-01-12 20:23:03 【问题描述】:我开发了一个自定义卷积层。我可以在模型中使用它并对其进行训练(model.fit 有效),但 model.predict() 会产生错误!
我将添加一个简单的代码来演示代码的结构。
modelx1 = tf.keras.models.Sequential([tf.keras.Input(shape=(49,)), Dense(1, activation = 'relu')])
class customLayer(tf.keras.layers.Layer):
def __init__(self,n=10):super(customLayer, self).__init__()
def call(self, inputs):
_, Dim0,Dim1, Dim3 = inputs.shape
input_victorized = tf.image.extract_patches(images=inputs, sizes=[-1, 7, 7, 1],
strides=[1, 1, 1, 1],rates=[1, 1, 1, 1], padding='SAME')
input_victorized2 = tf.reshape(input_victorized, [-1,49])
model_output = modelx1(input_victorized2)
out = tf.reshape(model_output,[-1,Dim0,Dim1,Dim3])
return out
自定义层对输入进行整形,然后将其提供给“modelx1”,然后对输出进行整形。
这是一个使用自定义层的简单模型:
input1 = tf.keras.Input(shape=(28,28,1))
x = Conv2D(filters = 2, kernel_size = 5, activation = 'relu')(input1)
Layeri = customLayer()(x)
xxc = Flatten()(Layeri)
y = Dense(units = 3, activation = 'softmax')(xxc)
model = tf.keras.Model(inputs=input1, outputs=y)
model.summary()
运行model.predict时出现错误:
model.predict(np.ones([100,28,28,1]))
UnimplementedError: Only support ksizes across space.
[[node model_58/custom_layer_9/ExtractImagePatches
(defined at <ipython-input-279-953feb59f882>:7)
]] [Op:__inference_predict_function_14640]
Errors may have originated from an input operation.
Input Source operations connected to node model_58/custom_layer_9/ExtractImagePatches:
In[0] model_58/conv2d_98/Relu (defined at /usr/local/lib/python3.7/dist-packages/keras/backend.py:4867)
【问题讨论】:
也许这个有帮助:***.com/questions/63177786/… 【参考方案1】:我认为这应该可行:-
image = tf.expand_dims(image, 0)
extracted_patches = tf.image.extract_patches(images = image,
sizes = [1, int(0.5 * image_height), int(0.5 * image_width), 1],
strides = [1, int(0.5 * image_height), int(0.5 * image_width), 1],
rates = [1, 1, 1, 1],
padding = "SAME")
然后使用tf.reshape
提取这些补丁
patches = tf.reshape(extracted_patches,
[-1,int(0.5*image_height),int(0.5*image_width),3])
几个月前我也遇到过类似的错误;这解决了它!
`
【讨论】:
以上是关于model.predict 不适用于 Keras 自定义层(推理错误)的主要内容,如果未能解决你的问题,请参考以下文章
Keras model.evaluate() 和 model.predict() 有啥区别?
model.predict(keras)输出中的类的顺序是啥?
Keras - model.predict 返回类而不是概率