Reputation: 51
I'm trying to use Concatenate() in order to create an ensemble of VGG16 and VGG19. My images are of the shape (224, 224, 3). I do not understand what is this error about.
Here's the code:
# Preprocessing the Training set
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
# Preprocessing the Train set
training_set = train_datagen.flow_from_directory('/content/drive/MyDrive/Model Development /tbdataset/Train',
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')
# Preprocessing the Test set
test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory('/content/drive/MyDrive/Model Development /tbdataset/Test',
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical',
shuffle=False)
vgg19 = VGG19(input_shape=IMAGE_SIZE, weights='imagenet', include_top=False)
for layer in vgg19.layers:
layer._name = layer._name + str('_19')
layer.trainable = False
vgg16 = VGG16(input_shape=IMAGE_SIZE, weights='imagenet', include_top=False)
for layer in vgg16.layers:
layer._name = layer._name + str('_16')
layer.trainable = False
vgg16_x = Flatten()(vgg16.output)
vgg19_x = Flatten()(vgg19.output)
x = Concatenate()([vgg16_x, vgg19_x])
out = Dense(2, activation='softmax')(x)
model = Model(inputs = [vgg16.input, vgg19.input], outputs = out)
model.compile(
loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(
learning_rate=0.0005,
name="Adam"),
metrics=['accuracy',
'AUC',
'Precision',
'Recall',
]
)
model.summary()
r = model.fit(
training_set,
validation_data=test_set,
epochs=20,
steps_per_epoch=len(training_set),
validation_steps=len(test_set)
)
I'm getting the following error:
ValueError: Layer model_16 expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, None, None, None) dtype=float32>]
Can anyone guide me with the above issue? Thank you in advance!
Upvotes: 1
Views: 388
Reputation: 22021
If vgg16
and vgg19
receive the same input you can use a shared input layer for both. In this way, your model will have only one input.
Here the code:
IMAGE_SIZE = (224,224,3)
vgg19 = tf.keras.applications.vgg19.VGG19(
input_shape=IMAGE_SIZE, weights='imagenet', include_top=False)
for layer in vgg19.layers:
layer._name = layer._name + str('_19')
layer.trainable = False
vgg16 = tf.keras.applications.vgg16.VGG16(
input_shape=IMAGE_SIZE, weights='imagenet', include_top=False)
for layer in vgg16.layers:
layer._name = layer._name + str('_16')
layer.trainable = False
inp = Input(IMAGE_SIZE)
vgg16_x = Flatten()(vgg16(inp))
vgg19_x = Flatten()(vgg19(inp))
x = Concatenate()([vgg16_x, vgg19_x])
out = Dense(2, activation='softmax')(x)
model = Model(inputs = inp, outputs = out)
model.compile(
loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(
learning_rate=0.0005,
name="Adam"),
metrics=['accuracy',
'AUC',
'Precision',
'Recall',
]
)
model.summary()
Upvotes: 2