Keras-“节点”对象没有属性“输出掩码”

时间:2018-11-28 10:12:45

标签: python tensorflow keras

我是Tensorflow和Keras的新手。我试图在Keras中运行代码。我有一些标签可以转换为一种热编码,深度为2,然后将其转换为Keras图层格式。 现在,当我朗读我的代码时,我得到了这个错误:

  

回溯(最近通话最近):   文件“ /Users/vahid/Documents/Thesis/Code/new_code/CapsNet_model.py”,第224行,在       CapsNet((None,28,28,1),2,3,trainImg,trainLbl)     CapsNet中的文件“ /Users/vahid/Documents/Thesis/Code/new_code/CapsNet_model.py”,第164行       x_recon = k.layers.Dense(512,activation ='relu')(已屏蔽)     在致电中,文件“ /Users/vahid/TensorProject/lib/python3.6/site-packages/keras/engine/base_layer.py”,第443行       previous_mask = _collect_previous_mask(输入)     _collect_previous_mask中的文件“ /Users/vahid/TensorProject/lib/python3.6/site-packages/keras/engine/base_layer.py”,行1311   mask = node.output_masks [tensor_index]       AttributeError:“节点”对象没有属性“输出掩码”

以及我运行并得到错误的代码部分:

def CapsNet(input_shape, n_class, num_routing,img,lbl):
"""
:param input_shape: (None, width, height, channels)
:param n_class: number of classes
:param num_routing: number of routing iterations
:return: A Keras Model with 2 inputs (image, label) and
         2 outputs (capsule output and reconstruct image)
"""
# Image
#x = k.layers.Input(shape=input_shape,tensor=img)
# ReLU Conv1

images, labels = tf.train.shuffle_batch([img, lbl], batch_size=50, capacity=30, num_threads=1, min_after_dequeue=10)

inp = k.layers.Lambda(lambda x: x)(images)
#inpy = k.layers.Lambda(lambda x: x,output_shape=(n_class,2))(labels)

#conv1 = k.layers.Conv2D(nb_filter= 256,nb_row= 9 , nb_col= 9, subsample =(1,1),activation='relu', name='conv1')(x)
conv1 = k.layers.Conv2D(filters = 256, kernel_size =9 , strides=(1,1),padding='valid', activation='relu', name='conv1')(inp)

# PrimaryCapsules: Conv2D layer with `squash` activation,
# reshape to [None, num_capsule, dim_vector]
primarycaps = PrimaryCap(conv1, dim_vector=8, n_channels=32,kernel_size=9, strides=2, padding='valid')
#primarycaps = PrimaryCap(conv1, dim_vector=8, n_channels=32 , subsample =(2,2) )

# DigiCap: Capsule layer. Routing algorithm works here.

digitcaps = DigiCap(num_capsule = n_class, dim_vector = 16, num_routing = num_routing, name='digitcaps')(primarycaps)

# The length of the capsule's output vector
out_caps = Length(name='out_caps')(digitcaps)

# Decoder network.
y = k.layers.Input(shape=(n_class,),tensor=labels)

# The true label is used to extract the corresponding vj
masked = Mask()([digitcaps, y])
x_recon = k.layers.Dense(512, activation='relu')(masked)
x_recon = k.layers.Dense(1024, activation='relu')(x_recon)
x_recon = k.layers.Dense(784, activation='sigmoid')(x_recon)
x_recon = k.layers.Reshape(target_shape=[28, 28, 1], name='out_recon')(x_recon)

# two-input-two-output keras Model
return k.models.Model([inp, y], [out_caps, x_recon])

掩码代码:

class Mask(tf.layers.Layer):
"""
Mask a Tensor with shape=[None, d1, d2] by the max value in axis=1.
Output shape: [None, d2]
"""
def call(self, inputs, **kwargs):
    # use true label to select target capsule, shape=[batch_size, num_capsule]
    if type(inputs) is list:  # true label is provided with shape = [batch_size, n_classes], i.e. one-hot code.
        assert len(inputs) == 2
        inputs, mask = inputs

    else:  # if no true label, mask by the max length of vectors of capsules
        x = inputs
        x = tf.cast(x,tf.float32)
        # Enlarge the range of values in x to make max(new_x)=1 and others < 0
        x = (x - K.max(x, 1, True)) / K.epsilon() + 1
        mask = K.clip(x, 0, 1)  # the max value in x clipped to 1 and other to 0

    # masked inputs, shape = [batch_size, dim_vector]
    inputs_masked = K.batch_dot(inputs, mask, [1, 1])
    return inputs_masked

1 个答案:

答案 0 :(得分:0)

好吧,我了解了如何解决此问题。实际上,我们可以在代码中使用两种Keras。 Keras包或仅使用tf.keras。在此代码中,当使用“密集”时,我使用Keras包。例如在x_recon = k.layers.Dense(512, activation='relu')(masked)中,所以tf.keras和Keras似乎有不同的来源,当我将k.layers更改为tf.keras时,问题已得到解决。