【问题标题】:model.predict doesn't work with Keras Custom Layer (inference error)model.predict 不适用于 Keras 自定义层(推理错误)
【发布时间】: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)

【问题讨论】:

标签: python tensorflow keras deep-learning conv-neural-network


【解决方案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])

几个月前我也遇到过类似的错误;这解决了它!

`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-03
    • 2011-09-12
    • 2018-12-04
    • 2018-11-06
    • 2011-08-01
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    相关资源
    最近更新 更多