How to save the weights of VGG-16 model after training it? How to load the saved weights in to the model?
I tried this:
fname = "weights-Test-CNN.hdf5"
custom_vgg_model.save_weights(fname,overwrite=True)
custom_vgg_model.load_weights(weights-Test-CNN.hdf5, by_name=False)
I got following error:
NameError Traceback (most recent call
last) in ()
----> 1 custom_vgg_model.load_weights(weights-Test-CNN.hdf5, by_name=False)
NameError: name 'weights' is not defined
Surround the second weights-Test-CNN.hdf5 with quotes or you use fname as you have defined...
custom_vgg_model.load_weights("weights-Test-CNN.hdf5", by_name=False)
Related
I am trying to save a fine-tuned binary classification model based on pretrained Bert module 'uncased_L-12_H-768_A-12'. I'm using tf2.
The code set up the model structure:
bert_classifier, bert_encoder =bert.bert_models.classifier_model(bert_config, num_labels=2)
then:
# import pre-trained model structure from the check point file
checkpoint = tf.train.Checkpoint(model=bert_encoder)
checkpoint.restore(
os.path.join(gs_folder_bert, 'bert_model.ckpt')).assert_consumed()
then: I compiled and fit the model
bert_classifier.compile(
optimizer=optimizer,
loss=loss,
metrics=metrics)
bert_classifier.fit(
Text_train, Label_train,
validation_data=(Text_val, Label_val),
batch_size=32,
epochs=1)
at last: I saved the model in the model folder which then automatically generates a file named saved_model.pb within
bert_classifier.save('/content/drive/My Drive/model')
also tried this:
tf.saved_model.save(bert_classifier, export_dir='/content/drive/My Drive/omg')
now I try to load the model and apply it on test data:
from tensorflow import keras
ttt = keras.models.load_model('/content/drive/My Drive/model')
I got:
KeyError Traceback (most recent call last)
<ipython-input-77-93f80aa585da> in <module>()
----> 1 tf.keras.models.load_model(filepath='/content/drive/My Drive/omg', custom_objects={'Transformera':bert_classifier})
9 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in _revive_graph_network(self, metadata, node_id)
392 else:
393 model = models_lib.Functional(
--> 394 inputs=[], outputs=[], name=config['name'])
395
396 # Record this model and its layers. This will later be used to reconstruct
KeyError: 'name'
This error message doesn't help me with what to do...please kindly advice.
I also tried to save the model in h5 format, but when i load it
ttt = keras.models.load_model('/content/drive/My Drive/model.h5')
I got this error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-36-12f76139ec24> in <module>()
----> 1 ttt = keras.models.load_model('/content/drive/My Drive/model.h5')
5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py in class_and_config_for_serialized_keras_object(config, module_objects, custom_objects, printable_module_name)
294 cls = get_registered_object(class_name, custom_objects, module_objects)
295 if cls is None:
--> 296 raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
297
298 cls_config = config['config']
ValueError: Unknown layer: BertClassifier
Seems as if you have the answer right in the question: '/content/drive/My Drive/model' will fail due to the whitespace character.
You could try it with escaping the backspace: '/content/drive/My\ Drive/model'.
Other option, after I had exactly the same problem with saving and loading. What helped was to just save the weights of the pre-trained model and not saving the whole model:
Just take a look right here: https://keras.io/api/models/model_saving_apis/, especially at the methods save_weights() and load_weights().
the error is mentioned in the picture.
TypeError Traceback (most recent call last)
<ipython-input-200-8ea67f851d87> in <module>()
1 final_model = Sequential([
----> 2 Concatenate([image_model, caption_model], mode='concat', concat_axis=1),
3 Bidirectional(LSTM(256, return_sequences=False)),
4 Dense(vocab_size),
5 Activation('softmax')
You are using the Concatenate layer wrong. Change your code to the following:
final_model = Sequential([
Concatenate([image_model, caption_model], axis=1),
Bidirectional(LSTM(256, return_sequences=False))
Dense(vocab_size), 5 Activation('softmax'),
Activation('softmax')
])
I'm currently using DNN regressor with 11 input neurons multiple hidden layers, and 11 output neurons.
I've have finished training the model. However, when I try to interface with the model with this code:
a = np.array(0,0,0,0,0,0,0,0,0,0,0)
y_pred = regressor.predict(a, as_iterable=False)
print(y_pred)
However this generates an error:
InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 11 values, but the requested shape has 121
[[node dnn/input_from_feature_columns/input_layer/X/Reshape (defined at regressor_full.py:177) ]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "regressor_full.py", line 206, in <module>
a = np.array(0,0,0,0,0,0,0,0,0,0,0)
ValueError: only 2 non-keyword arguments accepted.
I'm just trying to generate a prediction for 11 input variables and predict 11 output variables(as I trained the model), however, it generates an error.
I've tried changing the as_iterable to True, however this doesn't change anything. What is currently causing the error, and how do I fix it? Thank you for your time.
As requested, I included the code that defines the model.
import tensorflow.contrib.learn as skflow
regressor = skflow.DNNRegressor(feature_columns=feature_columns,
label_dimension=11,
hidden_units=hidden_layers,
model_dir=MODEL_PATH,
dropout=dropout,
config=test_config,
activation_fn = tf.nn.relu,
optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate = learning_rate)
)
Try changing the input array to a = np.array([0,0,0,0,0,0,0,0,0,0,0],dtype=" ") and also define the dtype which might solve the issue.
I have 3176 records. i split into 80% training data and 20% testing data. But I train the following the code.
this error occurs.
Traceback (most recent call last):
File "D:/Python Project/data.py", line 179, in
test_results.append(tests_new[i - 2535])
IndexError: list index out of range
from sklearn import model_selection
X_train, X_test, y_train, y_test = model_selection.train_test_split(train_data, target_data, test_size=0.20)
train data: your features list
target data: your predict data
Use try and except block then you will be able to figure out for exactly which index value it is getting error form test_results.append(tests_new[i - 2535]).
My model is correctly formed:
model = Sequential()
model.add(Lambda(lambda x:x/255.0 - 0.5, input_shape=(160,320,3)))
model.compile(loss='mse', optimizer='adam')
model.fit(train_generator, samples_per_epoch= len(train_samples), validation_data=validation_generator, nb_val_samples=len(validation_samples), nb_epoch=3)
Note, parenthesis are in place. However when I fit I get the following error:
Traceback (most recent call last): File "modell.py", line 70, in <module>
model.fit(train_generator, samples_per_epoch= len(train_samples), validation_data=validation_generator, nb_val_samples=len(validation_samples), nb_epoch=3)
TypeError: fit() missing 1 required positional argument: 'y'
train_generator is a 2D array
train_generator = generator(train_samples, batch_size=32)
I must be blind because I can't spot the problem. Does anyone know why fit is looking for an extra argument?
When using a generator to train, you must use the method model.fit_generator.
The method fit will always demand for inputs (X) and outputs/targets (Y)