Keras, using two pre-trained autoencoder models - python

I'm looking to use two pre-trained autoencoder models and build a model on top with binary output. This is my code:
autoencoder = load_model("autoencoder.h5")
model1 = autoencoder
model2 = autoencoder
model1_out = model1.get_layer(index=7).output
model2_out = model2.get_layer(index=7).output
x = tf.keras.layers.concatenate([model1_out, model2_out])
x = Dense(400, activation='softmax')(x)
x = Dense(200, activation='softmax')(x)
x = Dense(100, activation='softmax')(x)
x = Dense(1, activation='sigmoid')(x)
model=tf.keras.Model(inputs=[model1.input,model2.input],outputs=x)
model.compile(optimizer=Adam(learning_rate=0.001),loss='binary_crossentropy',
metrics=['accuracy'])
model.fit([X_train,X_train], y_train)
I get the following error:
ValueError: The list of inputs passed to the model contains the same input multiple times.
All inputs should only appear once.
Received inputs=
[<KerasTensor: shape=(None, 768) dtype=float32 (created by layer 'input_1')>,
<KerasTensor: shape=(None, 768) dtype=float32 (created by layer 'input_1')>]
Can somebody tell me what I am doing wrong?
Thanks!

Related

Concatenate layers without using API methode

I had 5 LSTM layers and 2 MLP's which must be concatenate together into another MLP which produce the final output. Here is the code I wrote using the API approach, which works fine:
lstm_input = Input(shape=(X_dynamic_LSTM.shape[1], X_dynamic_LSTM.shape[2]))
x = LSTM(70, activation='tanh', return_sequences=True)(lstm_input )
x = Dropout(0.3)(x)
x = Dense(1, activation='tanh')(x)
mlp_input=Input(shape=(X_static_MLP.shape[1]))
mlp = Dense(30, activation='relu')(mlp_input)
mlp = Dense(10, activation='relu')(mlp)
merge = Concatenate()([x, mlp])
hidden1 = Dense(5, activation='relu')(merge)
mlp_out = Dense(1, activation='relu')(hidden1)
model = Model(inputs=[lstm_input, mlp_input], outputs=mlp_out)
model.compile(loss='mae', optimizer='Adam')
history = model.fit([X_dynamic_LSTM, X_static_MLP], y_train, batch_size=20,
epochs=10, validation_split=0.2)
If I want to convert this format to one similar to below:
x = Sequential()
x.add(LSTM(70, return_sequences=True))
x.add(Dropout(0.3))
x.add(Dense(1, activation='tanh'))
Can any one help me how should I define the MLP, the Concatenate and the part regarding the "model = Model(inputs=[lstm_input, mlp_input], outputs=mlp_out)" ??
My main problem is I want to add an Embedding layer to the LSTM. when I add the dollowing code to non-API approach the model works perfect.
x.add(Embedding(X_dynamic_LSTM.shape[0], 1,mask_zero=True))
But when instead I used
lstm_input = Embedding(X_dynamic_LSTM.shape[0], 1,mask_zero=True)
It gave me the error : TypeError: Inputs to a layer should be tensors, So I got to stick with non-API approach.

keras sequential tensor as argument

I am trying to reproduce the image classification problem cat or dog using tensorflow and transfer learning (Xception model pretrained with imagenet). The code is:
base_model = keras.applications.Xception(
weights='imagenet',
# image shape = 128x128x3
input_shape=(128, 128, 3),
include_top=False)
# freeze layers
base_model.trainable = False
inputs = keras.Input(shape=(128, 128, 3))
x = data_augmentation(inputs)
x = tf.keras.applications.xception.preprocess_input(x)
x = base_model(x, training=False)
x = keras.layers.Flatten()(x)
x = keras.layers.Dense(128, activation='relu')(x)
outputs = keras.layers.Dense(1, activation='sigmoid')(x)
model = keras.Model(inputs, outputs)
I am now trying to make use of models.Sequential. So far my code looks like this:
theModel=models.Sequential([
tf.keras.Input(shape=(128, 128, 3)),
tf.keras.applications.xception.preprocess_input(), <-------- how to pass tensor as argument?
base_model,
Flatten(),
Dense(128, activation='relu'),
Dense(1,activation='sigmoid')
])
My question, is there a way to make use of models.Sequentials, defining everything as I've done but passing the tensor as argument like in the first code snipped?
Thanks in advance,
metc
You cannot use tf.keras.applications.xception.preprocess_input() inside the sequential model. You have to define it outside the model and can pass the output of it to the sequential model by assigning values to the tensor argument in the input layer.
x=tf.random.uniform(shape=(1,128,128,3))
x= tf.keras.applications.xception.preprocess_input(x)
theModel=tf.keras.models.Sequential([
tf.keras.Input(tensor=x),
base_model,
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1,activation='sigmoid')
])
For more details, Please refer to this gist.Thank You!

Incompatiblity with the layer in the image captioning model

i am working on a image captioning model using flikr8k dataset. when i try to fit the model i am getting this error.
***ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 2048 but received input with shape (None, 1)***
This is https://www.kaggle.com/shadabhussain/automated-image-captioning-flickr8/comments code i am trying to replicate.
but the moment i ran model.fit i get a value error.
conca = Concatenate()([image_model.output, language_model.output])
x = LSTM(128, return_sequences=True)(conca)
x = LSTM(512, return_sequences=False)(x)
x = Dense(vocab_size)(x)
out = Activation('softmax')(x)
model = Model(inputs=[image_model.input, language_model.input], outputs = out)
# model.load_weights("../input/model_weights.h5")
model.compile(loss='categorical_crossentropy', optimizer='RMSprop', metrics=['accuracy'])
model.summary()
hist = model.fit([images, captions], next_words, batch_size=512, epochs=200)
I have attached the image of the model architecture
Thanks

Input Shape in Keras Autoencoder

i'm trying to train an autoencoder in the following code:
encoder_input = keras.layers.Input(shape=(x_Train.shape[1]), name='img')
encoder_out = keras.layers.Dense(1, activation = "relu")(encoder_input)
encoder = keras.Model(encoder_input, encoder_out, name="encoder")
decoder_input = keras.layers.Dense(602896, activation = "relu")(encoder_out)
decoder_output = keras.layers.Reshape((769, 28, 28))(decoder_input)
opt = keras.optimizers.RMSprop(learning_rate=1e-3)
autoencoder = keras.Model(encoder_input, decoder_output, name = "autoencoder")
autoencoder.summary()
autoencoder.compile(opt, loss='mse')
autoencoder.fit(x_Train, x_Train, epochs=10, batch_size=64, validation_split = 0.1)
However, it returns the error:
"tensorflow:Model was constructed with shape (None, 28) for input KerasTensor(type_spec=TensorSpec(shape=(None, 28), dtype=tf.float32, name='img'), name='img', description="created by layer 'img'"), but it was called on an input with incompatible shape (None, 28, 28)."
I don't know how to deal with that or to resize my input. My x_train is a vector with size [769,28,28]
Could someone help me to handle the error?
That's the summary
Thanks
Your input shape for your autoencoder is a little weird, your training data has a shaped of 28x28, with 769 as your batch, so the fix should be like this:
encoder_input = keras.layer.Input(shape=(28, 28), name='img')
encoder_out = keras.layers.Dense(1, activation = "relu")(encoder_input)
# For ur decoder, you need to change a bit as well
decoder_input = keras.layers.Dense(784, activation = "sigmoid")(encoder_out) # Flatten until 28x28 =784
decoder_output = keras.layers.Reshape((28, 28))(decoder_input) # From there reshape back to 28x28
The problem (apart from the wrong shape in the input layer (has to be shape=(28, 28) and the output layer (has to be (28,28)) like in Edwin Cheong 's answer) is that you forgot a flatten layer after your input layer. This leads to the incompatible shape.
Adapted the answer from above:
encoder_input = keras.layer.Input(shape=(28, 28), name='img')
encoder_input = keras.layer.Flatten()(encoder_input)
encoder_out = keras.layers.Dense(1, activation = "relu")(encoder_input)
decoder_input = keras.layers.Dense(784, activation = "sigmoid")(encoder_out)
decoder_output = keras.layers.Reshape((28, 28))(decoder_input)

InvalidArgumentError: logits and labels must have the same first dimension

I am trying to classify images. Those images have different shapes, but this is not a problem.
However, I am trying to create a dataset using the tf.data.Dataset.from_generator function provided by Tensorflow and I have the feeling that something is not working as it should.
Here is the code:
filenames_ds = tf.data.Dataset.from_tensor_slices(categ_img[:1000]['image_name'])
labels_ds = tf.data.Dataset.from_tensor_slices(categ_img[:1000]['category_label'])
images_ds = filenames_ds.map(lambda x: tf.image.decode_jpeg(tf.read_file(x)))
labels_ds = labels_ds.map(lambda x: tf.one_hot(x, NUM_CATEGORIES))
ds = tf.data.Dataset.zip((images_ds, labels_ds)).batch(1)
I also tried to create the labels_ds like this:
labels_ds.map(lambda x: tf.expand_dims(tf.one_hot(x, NUM_CATEGORIES), axis=0))
categ_imgis a pandas.DataFrame containing image paths and labels under image_name and category_label columns respectively.
And I keep getting this error:
InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [1,50] and labels shape [50]
My model is based on a pretrained ResNet model provided by Keras:
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(None, None, 3))
for layer in base_model.layers:
layer.trainable = False
x = base_model.output
x = GlobalAveragePooling2D()(x)
for fc in FC_LAYERS:
x = Dense(fc, activation='relu')(x)
x = Dropout(DROPOUT)(x)
output = Dense(NUM_CATEGORIES, activation='softmax', name='fully-connected')(x)
model = Model(inputs=base_model.input, outputs=output)
optimizer = tf.keras.optimizers.SGD(lr=LEARNING_RATE)
cce = tf.keras.losses.CategoricalCrossentropy()
model.compile(optimizer, loss=cce)
return model
It is trained like this:
model_classification.fit(
ds,
epochs=epochs,
steps_per_epoch=steps
)
Which seems pretty straight-forward to me.
Any help would be appreciated.
Thank you.
I finally tried something that worked.
Here is the line you need to change:
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
I don't know why, but this made things working.

Categories