Connecting RESNET50 with my model in tensorflow - tensorflow

I want to connect a RESNET50 with my model in order to pretrain the second.
The RESNET50 model:
pre_trained = tf.keras.applications.ResNet50(include_top=False,
weights= None,
input_shape=(image_dimension, image_dimension, 3),
pooling='max')
for layer in pre_trained.layers:
layer.trainable = False
My model with the resnet50 in order to pretrain it:
model = models.Sequential()
model.add(pre_trained)
model.add(layers.Conv2D(filters=nodes, kernel_size=(ks,ks), activation='relu'))
model.add(layers.MaxPool2D(pool_size=(3,3)))
model.add(layers.Conv2D(filters=nodes*2, kernel_size=(ks,ks), activation='relu'))
model.add(layers.Conv2D(filters=nodes*2, kernel_size=(ks,ks), activation='relu'))
model.add(layers.MaxPool2D(pool_size=(3,3)))
model.add(layers.Flatten())
model.add(layers.Dense(512,activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.Dropout(rate=do))
model.add(layers.Dense(out_nodes,activation='softmax'))
I get the error:
Input 0 of layer "conv2d_66" is incompatible with the layer: expected min_ndim=4, found ndim=2. Full shape received: (None, 2048)
Do you have any idea how to solve the problem? thank you.

Related

ValueError: Shapes (None, 9) and (None, 22000, 9) are incompatible

The following is my code:
The shape of my X_train is TensorShape([600, 22000, 5])
The shape of my Y_train is (600, 9)
Is there an error with the type of data that I am using for this time-series problem?
model = Sequential()
model.add(LSTM(256,return_sequences=True,input_shape=(22000, 5)))
model.add(Dense(9, activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])
print(model.summary())
#print(model.summary())
model.fit(allfileswow[:600], features_a1[:600], epochs=100,verbose=0)
Hi just like #Djinn mentioned add a flatten layer to make everything 1-D
model = Sequential()
model.add(LSTM(256,return_sequences=True,input_shape=(22000, 5)))
model.add(Flatten())
model.add(Dense(9, activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])
print(model.summary())
Try removing 'return_sequences=True' as you don't need full sequence as the output for the next layer.
Refer lstm

Tensorflow - keras - shapes and loss for multilabel classification

X_np_new.shape, y.shape
((50876, 2304), (50876, 9))
Code:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation
from tensorflow.keras.optimizers import SGD
model = Sequential()
model.add(Dense(5000, activation='relu', input_dim=X_np_new.shape[1]))
model.add(Dropout(0.1))
model.add(Dense(600, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(X_np_new.shape[1], activation='sigmoid'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd)
model.fit(X_np_new, y, epochs=5, batch_size=2000)
preds = model.predict(X_np_new)
I get error:
ValueError: Shapes (None, 9) and (None, 2304) are incompatible
What went wrong here?
Replace
model.add(Dense(X_np_new.shape[1], activation='sigmoid'))
With
model.add(Dense(y.shape[1], activation='sigmoid'))
Explanation:
Putting X_np_new.shape[1] in the last layer means you have 2304 classes because X_np_new.shape[1]=2304 but you actually have 9 classes that you can get that from y.shape[1].
ValueError: Shapes (None, 9) and (None, 2304) are incompatible
means that your model is expecting labels of Size [*, 2304] but your labels size is [*, 9].

Keras input shape mismatch error for multi-feature CNN classification model

Here is my code:
model = Sequential()
model.add(Conv1D(32, kernel_size=3,
activation='relu',
input_shape=(14,1)))
model.add(MaxPooling1D(pool_size=1))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='softmax'))
model.summary()
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(X_train.values, y_train.values,
batch_size=4,
epochs=1,
verbose=2,
validation_data=(X_test.values,y_test.values))
And error is:
Error when checking input: expected conv1d_35_input to have 3 dimensions, but got array with shape (13166, 14)
As suggested by other posts, I tweaked with flatten layer before output layer but that did not work.
My X_train.values.shape gives (13166, 14)
Any suggestion how should I fix this?
You need to reshape the X_train.values from (13166, 14) to (13166, 14, 1) as your input shape of CNN network is (None, 14, 1).
This may solve your problem:
X_train.values.reshape([-1,14,1])

How to load fine-tuned keras model

I am following this tutorial to try fine-tuning using VGG16 model, I trained the model and saved .h5 file using model.save_weights and
vgg_conv = VGG16(include_top=False, weights='imagenet', input_shape=(image_size, image_size, 3))
# Freeze the layers except the last 4 layers
for layer in vgg_conv.layers[:-4]:
layer.trainable = False
model = Sequential()
model.add(vgg_conv)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(11, activation='softmax'))
I then tried to rebuild the architecture and load weights using the below
def create_model(self):
model = Sequential()
vgg_model = VGG16(include_top=False, weights='imagenet', input_shape=(150, 150, 3))
model.add(vgg_model)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(11, activation='softmax'))
model.load_weights(self.top_model_weights_path) # throws error
return model
but it then throws this error
ValueError: Cannot feed value of shape (512, 512, 3, 3) for Tensor 'Placeholder:0', which has shape '(3, 3, 3, 64)'
what am I doing wrong?
I am not sure how to intepret the error but you could try saving the model architecture and the weights together model.save("model.h5") after fine tuning.
To load the model you can type
model = load_model('model.h5')
# summarize model.
model.summary()
I think this has the benefeit of not having to rebuild the model and requires only one line to acomplish the same purpose.
The problem comes from the trainable difference between the two models. If you freeze the 4 last layers in the create_model function, it will work.
But as said by Igna, model.save and model.load_model is simpler.

Input dimension mismatch between dense layers and conv layers of imagenet while attempting transfer learning

I am trying to train dense layers on top of the conv layers of InceptionV3.
But I'm unable to initialize the fully connected model. I'm getting a ValueError.
model_inc = applications.InceptionV3(weights='imagenet',
include_top=False)
model = Sequential()
model.add(Flatten(input_shape=model_inc.output_shape[1:]))
model.add(Dense(256, activation= 'relu', kernel_initializer='he_normal'))
model.add(Dropout(0.5))
model.add(Dense(9, activation='softmax'))
I expected the model to compile successfully but I get "ValueError: The shape of the input to "Flatten" is not fully defined (got (None, None, 2048). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model."
You want your input to go to your model_inc so you have to define the input_shape there. Something like the following should work
model_inc = applications.InceptionV3(input_shape=(224,224,3), weights='imagenet',
include_top=False)
model = Sequential()
# you need to add your base model
model.add(model_inc)
model.add(Flatten())
model.add(Dense(256, activation= 'relu', kernel_initializer='he_normal'))
model.add(Dropout(0.5))
model.add(Dense(9, activation='softmax'))