model.predict 不适用于 Keras 自定义层(推理错误)

model.predict doesn't work with Keras Custom Layer (inference error)

我开发了一个自定义卷积层。我可以在模型中使用它并训练它(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)

我认为这应该可行:-

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])

几个月前我遇到过类似的错误;这已修复!

`