keras: Cannot stack models in Model Class API
I cannot stack a classifier model on top of a (partly) pre-trained VGG16 model in Model Class API. There are several threads with people having the same problem Google Groups and StackoverFlow. I have tried all variants, but I keep getting a Graph disconnect error:
Exception: Graph disconnected: cannot obtain value for tensor input_2 at layer "
My last attempt:
from keras.applications.vgg16 import VGG16
from keras.models import Model
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D, GlobalAveragePooling2D
from keras.layers import Dropout, Flatten, Dense, Input
img_width, img_height = 150, 150
input_1 = Input(shape=(3, img_width, img_height))
vgg16_model = VGG16(include_top=False, weights='imagenet', input_tensor=input_1)
input_2 = Input(shape=vgg16_model.output_shape[1:])
x = GlobalAveragePooling2D()(input_2)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
predict = Dense(1, activation='sigmoid')(x)
top_model = Model(input=input_2, output=predict)
# load the best saved weights
#top_model.load_weights(os.path.join(data_path, 'VGG16Classifier.hdf5'))
# add the model on top of the convolutional base
model = Model(input=input_1, output=top_model.output)
For info I am trying to re-create this Keras Blog , but with the Model Class API in order to later modify the VGG16 model. Is there a bug that is preventing this from working? Thanks, Jan
Oh, and I am totally thrilled about Keras!!! Really impressive!
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 15 (7 by maintainers)
Everyone, please be professional.
@farizrahman4u This is rude comment. This is actually an error that @fchollet fixed, #4477.
You irritate me with your existence.
No it doesn’t. If it does, kindly comment the particular line, Sir.
Here is the original code by OP. You will still get the error, with latest version of Keras; because it is supposed to.
Maybe next time ask someone less rude?
The issue is exactly what the error message says. There is no path from input_1 to top_model.output. So nothing to fix on keras side.
@farizrahman4u You’ve made me irritated with your comment so I will carefully show you the path from input_t to top_model.output.
input_1 feeds into vgg16_model, vgg_model.output feeds into input_2. Input_2 goes to x via the functional API. x goes into predict and then predict goes into top_model = Model(input=input_2, output=predict).
@farizrahman4u @fchollet
This is it for me. I am moving to tensorflow.
@farizrahman4u Boo hoo buddy, get over it
Just split the model: