Reputation: 25
EDIT:
Apparently some issues with keras vs tensorflow.keras caused my problem. I followed this to change my imports: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.engine.input_layer.InputLayer>
I'm trying to load a model and exchange the last dense layer with another one of different output dimension. This is what I've got so far:
The model as it is saved:
def create_model(n_timesteps=828 , n_features=1, n_outputs=7):
dtype='float32'
model = Sequential([
Convolution1D(input_shape=(n_timesteps, n_features), kernel_size=3, filters=128, activation='relu', kernel_regularizer='l2', dtype=dtype, name="conv1"),
MaxPool1D(pool_size=4, strides=4), # reduce size of the input by 4
Dropout(0.3, dtype=dtype),
Flatten(),
Dense(32, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(), dtype=dtype, name="dense1"),
Dropout(0.2, dtype=dtype),
Dense(n_outputs, activation='softmax', dtype=dtype, name="dense2")
])
return model
and the attempt to retrain:
def retrain(model_name="cnn_general"):
model = tf.keras.models.load_model('saved_models\\' + model_name)
model.trainable = False
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
print(model.summary())
output = Dense(1, activation='sigmoid')(model.layers[-1].output)
retrained_model = Model(inputs=model.inputs, outputs=output)
print(retrained_model.summary())
I get the error output:
Traceback (most recent call last):
File "C:/.../CNN_heatbeats.py", line 233, in <module>
retrain((X_train, y_train, X_test, y_test, X_val, y_val))
File "C:/.../CNN_heatbeats.py", line 161, in retrain
output = Dense(10, activation='sigmoid')(model.layers[-1].output)
File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper
return func(*args, **kwargs)
File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\keras\engine\base_layer.py", line 475, in __call__
previous_mask = _collect_previous_mask(inputs)
File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\keras\engine\base_layer.py", line 1441, in _collect_previous_mask
mask = node.output_masks[tensor_index]
AttributeError: 'Node' object has no attribute 'output_masks'
I've only found information on models like VGG16 but cannot apply that to my own network. Is what I've done in the retrain() function an appropriate approach? How can I make it work?
Upvotes: 0
Views: 464
Reputation: 71
have tried it this way?
def retrain(model_name="cnn_general"):
model = tf.keras.models.load_model('saved_models\\' + model_name)
model.trainable = False
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
model.summary()
retrained_model = Sequential()
for layer in model.layers[:-1]:
layer.trainable = False
retrained_model.add(layer)
retrained_model.add(Dense(1, activation='sigmoid'))
retrained_model.summary()
Upvotes: 1