我正在尝试构建一个keras模型并得到'NoneType' object has no attribute '_inbound_nodes'
我在这里研究了可能的解决方案,这些解决方案建议将操作更改为lambda函数。我这样做了,但是仍然缺少一些东西。
这是自动编码器keras模型的一部分:
编码器:
from keras.layers import Dense, Input, InputLayer
from keras.layers import Conv2D, MaxPooling2D, Flatten, Conv3D, ReLU, Add, BatchNormalization, Lambda
from keras. models import Model
import tensorflow as tf
fc_layer_size = [1024]
n_deconv_filters = [128, 128, 128, 64, 32, 1]
n_convfilter = [96, 128, 256, 256, 256, 256]
n_gru_vox = 4
def batchNormalization(layer):
return BatchNormalization(axis=-1,
momentum=0.99,
epsilon=0.001,
center=True,
scale=True,
trainable=True,
beta_initializer="ones",
gamma_initializer="ones",) (layer)
# Build the encoder network
def build_encoder(x):
# The encoder uses the deep residual network.
outputs = []
pooling = [1, 2, 2, 1]
shape = x.shape
bs = shape[0]
seq = shape[1]
img_w = shape[2]
img_h = shape[3]
ch = shape[4]
temp_shape=[bs*seq, img_w,img_h,ch]
# x = np.reshape(x, temp_shape)
# x = tf.convert_to_tensor(x,dtype=tf.float32)
x = Input(shape=(127,127,3,), dtype='float32' )
conv0_0 = Conv2D(filters=n_convfilter[0], kernel_size=(7,7), input_shape=temp_shape[1:], padding='same')(x)
conv0_0 = batchNormalization(conv0_0)
conv0_0 = ReLU()(conv0_0)
conv0_1 = Conv2D(filters=n_convfilter[0], kernel_size=(3,3), padding='same')(conv0_0)
conv0_1 = batchNormalization(conv0_1)
conv0_1 = ReLU()(conv0_1)
shortcut0 = Conv2D(filters=n_convfilter[0], kernel_size=(1,1),input_shape=temp_shape[1:], padding='same')(x)
shortcut0 = batchNormalization(shortcut0)
shortcut0 = ReLU()(shortcut0)
inject = Add()([shortcut0, conv0_1])
layer0 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')( inject ) # [bs * size, 64, 64, 96]
conv1_0 = Conv2D(filters=n_convfilter[1], kernel_size=(3,3), padding='same')(layer0)
conv1_0 = batchNormalization(conv1_0)
conv1_0 = ReLU()(conv1_0)
conv1_1 = Conv2D(filters=n_convfilter[1], kernel_size=(3,3), padding='same')(conv1_0)
conv1_1 = batchNormalization(conv1_1)
conv1_1 = ReLU()(conv1_1)
shortcut1 = Conv2D(filters=n_convfilter[1], kernel_size=(1,1),input_shape=temp_shape[1:], padding='same')(layer0)
shortcut1 = batchNormalization(shortcut1)
shortcut1 = ReLU()(shortcut1)
inject = Add()([shortcut1, conv1_1])
layer1 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')( inject ) # [bs * size, 32, 32, 128]
conv2_0 = Conv2D(filters=n_convfilter[2], kernel_size=(3,3), padding='same')(layer1)
conv2_0 = batchNormalization(conv2_0)
conv2_0 = ReLU()(conv2_0)
conv2_1 = Conv2D(filters=n_convfilter[2], kernel_size=(3,3), padding='same')(conv2_0)
conv2_1 = batchNormalization(conv2_1)
conv2_1 = ReLU()(conv2_1)
shortcut2 = Conv2D(filters=n_convfilter[2], kernel_size=(1,1),input_shape=temp_shape[1:], padding='same')(layer1)
shortcut2 = batchNormalization(shortcut2)
shortcut2 = ReLU()(shortcut2)
inject = Add()([shortcut2, conv2_1])
layer2 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')( inject ) # [bs * size, 16, 16, 256]
conv3_0 = Conv2D(filters=n_convfilter[3], kernel_size=(3,3), padding='same')(layer2)
conv3_0 = batchNormalization(conv3_0)
conv3_0 = ReLU()(conv3_0)
conv3_1 = Conv2D(filters=n_convfilter[3], kernel_size=(3,3), padding='same')(conv3_0)
conv3_1 = batchNormalization(conv3_1)
conv3_1 = ReLU()(conv3_1)
layer3 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')( conv3_1 ) # [bs * size, 8, 8, 256]
conv4_0 = Conv2D(filters=n_convfilter[4], kernel_size=(3,3), padding='same')(layer3)
conv4_0 = batchNormalization(conv4_0)
conv4_0 = ReLU()(conv4_0)
conv4_1 = Conv2D(filters=n_convfilter[4], kernel_size=(3,3), padding='same')(conv4_0)
conv4_1 = batchNormalization(conv4_1)
conv4_1 = ReLU()(conv4_1)
shortcut4 = Conv2D(filters=n_convfilter[4], kernel_size=(1,1),input_shape=temp_shape[1:], padding='same')(layer3)
shortcut4 = batchNormalization(shortcut4)
shortcut4 = ReLU()(shortcut4)
inject = Add()([shortcut4, conv4_1])
layer4 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')( inject ) # [bs * size, 8, 8, 256]
conv5_0 = Conv2D(filters=n_convfilter[5], kernel_size=(3,3), padding='same')(layer4)
conv5_0 = batchNormalization(conv5_0)
conv5_0 = ReLU()(conv5_0)
conv5_1 = Conv2D(filters=n_convfilter[5], kernel_size=(3,3), padding='same')(conv5_0)
conv5_1 = batchNormalization(conv5_1)
conv5_1 = ReLU()(conv5_1)
shortcut5 = Conv2D(filters=n_convfilter[5], kernel_size=(1,1),input_shape=temp_shape[1:], padding='same')(layer4)
shortcut5 = batchNormalization(shortcut5)
shortcut5 = ReLU()(shortcut5)
inject = Add()([shortcut5, conv5_1])
layer5 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')( inject ) # [bs * size, 8, 8, 256]
fin_shape=[bs,seq,fc_layer_size[0]]
flatten = Flatten()(layer5)
layer_fc = Dense(fc_layer_size[0], activation='relu')(flatten) #fully connected
# outputs = tf.reshape(flatten, fin_shape) #[bs,size,1024]
outputs = Lambda( lambda x : tf.reshape(x, fin_shape) ) (flatten)
return x, outputs
GRU:
from keras.activations import sigmoid, tanh
from keras.layers import Dense, ReLU, MaxPooling3D, Conv3D, Add, Lambda, Multiply
import numpy as np
import tensorflow as tf
n_gru_vox = 4
n_deconv_filters = [128, 128, 128, 64, 32, 1]
def fcconv3D_layer(inp, x, filters, n_gru_vox):
output_shape= inp.shape
fc_output = Dense(n_gru_vox * n_gru_vox * n_gru_vox * filters)(x)
fc_output = ReLU()(fc_output)
# fc_output = tf.reshape(fc_output,output_shape)
fc_output = Lambda(lambda x: tf.reshape(x,output_shape))(fc_output)
sconv3d = Conv3D(filters=filters,kernel_size=[3,3,3], padding="same")(inp)
h_tn = Add()([fc_output,sconv3d])
return h_tn
def recc(inp,x,filters,n_gru_vox):
u_t=sigmoid( fcconv3D_layer(inp,x,filters,n_gru_vox) )
r_t=sigmoid( fcconv3D_layer(inp,x,filters,n_gru_vox) )
sub0 = Lambda(lambda x : 1. - x)(u_t)
sub0 = Multiply()([sub0,inp])
sub1 = Multiply()([inp,tanh(fcconv3D_layer(r_t * inp, x, filters, n_gru_vox))])
h_tn = Add()([sub0,sub1])
# h_tn = (1.0 - u_t) * inp + inp * tanh(fcconv3D_layer(r_t * inp, x, filters, n_gru_vox))
return h_tn
def build_3dgru(features):
shape = features.shape
h = [None for _ in range(shape[1] + 1)]
# zero = tf.zeros(shape = [shape[0], n_gru_vox, n_gru_vox, n_gru_vox, n_deconv_filters[0]], dtype = tf.float32)
h[0] = Lambda(lambda x:x)(tf.zeros(shape = [shape[0], n_gru_vox, n_gru_vox, n_gru_vox, n_deconv_filters[0]], dtype = tf.float32))
for i in range(shape[1]):
fc = features[:, i, ...]
h[i + 1] = Lambda (lambda x:x) (recc(h[i], fc, n_deconv_filters[0], n_gru_vox))
# [bs, 4, 4, 4, 128]
return h[-1]
测试:
x = np.ones((16, 5, 127, 127, 3), dtype = np.float32)
inpLay,toLstm = build_encoder(x)
lstm = build_3dgru(toLstm)
model = Model(inpLay,lstm)
mode.Summary()
Error: 'NoneType' object has no attribute '_inbound_nodes'