我正在尝试使用带有Tensorflow后端的Keras构建自定义图层,以便处理图像。
使用Keras时,该过程失败,但与tensorflow(部分)一起使用。
为了更好地理解我实现了Identity层的问题并遇到了同样的问题:
'Node'对象没有属性'output_masks'
这是我的代码:
from tensorflow.python.keras.layers import *
from tensorflow.python.keras import Model
from tensorflow.python.keras import optimizers
import cv2
from keras.engine.topology import Layer # This Layer does not work
#
# USE KERAS IMPLEMENTED INTO TENSORFLOW TO MAKE IT WORK
#
#from tensorflow.python.keras._impl.keras.engine.base_layer import Layer
import numpy as np
class Identity(Layer):
def __init__(self, **kwargs):
super(Identity, self).__init__(**kwargs)
def build(self, input_shape):
super(Identity, self).build(input_shape) # Be sure to call this somewhere!
def call(self, x):
return x
def compute_output_shape(self, input_shape):
return tuple(input_shape)
height = 12
width = 18
channels = 3
image_original = cv2.imread("./predict_base.png")
input_data = np.zeros((2, 12, 18, 3))
input_data[0] = image_original
input_data[1] = image_original
input_shape = (height, width, channels)
input = Input(shape=input_shape)
output = Identity()(input)
# add the model on top of the convolutional base
model = Model(inputs=input, outputs=output)
adam = optimizers.Adam()
model.compile(loss='mean_squared_error', optimizer=adam)
model.summary()
image = model.predict(input_data)
cv2.imshow("result", image[0])
cv2.waitKey(0)
虽然更复杂的层可以直接使用tensorflow,但它无法正确计算输出形状。
如果我在输出形状计算中引入错误,例如:
def compute_output_shape(self, input_shape):
output_shape = []
output_shape[0] = input_shape[0]
output_shape[1] = input_shape[1]
output_shape[2] = input_shape[2]
output_shape[3] = 4 #Here is the error I added
return tuple(output_shape)
输出形状仍然相同,张量流不能正确计算输出。
你知道如何解决这个问题吗?
非常感谢。