Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

python - keras VGG19 add Input layer got graph disconnected error

I have below code, I do not understand why this is graph disconnected error? I do not know what is wrong.

def create_model_tl_attn_posInputOnly(input_shape):
    print("start creating model - transfer learning ...")

    emb = embed_encoding2d(input_shape[0], input_shape[1], input_shape[2]) # this is from another class

    img_input = Input(shape=input_shape)
    base_model = vgg19.VGG19(include_top=False, input_shape=input_shape, weights="imagenet")
    base_model.trainable = False
    x = base_model(img_input + emb)
    
    flat1 = Flatten()(x)
    class1 = Dense(1024, activation='relu')(flat1)
    dropout1 = Dropout(0.2)(class1)
    class2 = Dense(512, activation='relu')(dropout1)
    dropout2 = Dropout(0.2)(class2)
    
    output = Dense(num_classes, activation='softmax')(dropout2) 
    
    model = Model(inputs=img_input, outputs=output)
    
    return model

I changed to model = Model(inputs=base_model.inputs, outputs=output), but still got graph disconnected error.

question from:https://stackoverflow.com/questions/66061919/keras-vgg19-add-input-layer-got-graph-disconnected-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I update code as below, no error, however I do not know if the input is a sum of img_input and emb, how to check during training?

def create_model_tl_attn_posInputOnly(input_shape):
    print("start creating model - transfer learning ...")

    emb = embed_encoding2d(input_shape[0], input_shape[1], input_shape[2]) # this is from another class

    img_input = Input(shape=input_shape)
    base_model = vgg19.VGG19(include_top=False, input_shape=input_shape, weights="imagenet")
    base_model.trainable = False
    x = base_model(img_input + emb)
    x = base_model.layers[-1].output

    flat1 = Flatten()(x)
    class1 = Dense(1024, activation='relu')(flat1)
    dropout1 = Dropout(0.2)(class1)
    class2 = Dense(512, activation='relu')(dropout1)
    dropout2 = Dropout(0.2)(class2)
    
    output = Dense(num_classes, activation='softmax')(dropout2) 
    
    model = Model(inputs=base_model.inputs, outputs=output)
    
    return model

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...