I am running keras tensorflow 2.0 and am trying to create a model, train it, and then save it. My code looks as follows (simplified):
model = Model()
model.save("saved_models/model1", overwrite=True, save_format="tf")
Here, Model()
is a class that subclasses tf.keras.Model
and implements a call(inputs)
function.
The model class (requested below) looks like the following:
class Model(tf.keras.Model):
def __init__(self, loadFile: str = None):
super(Model, self).__init__()
#TODO: transfer from above into here V
self.conv1 = tf.keras.layers.Conv2D(filters=10,
kernel_size=(10, 30),
strides=(1, 30),
padding="same",
data_format="channels_last",
activation=tf.keras.activations.relu,
use_bias=True,
kernel_initializer=tf.keras.initializers.glorot_normal,
bias_initializer=tf.keras.initializers.zeros)
self.pool1 = tf.keras.layers.MaxPool2D(pool_size=(2, 1),
strides=(2, 1),
padding="same",
data_format="channels_last")
self.conv2 = tf.keras.layers.Conv2D(filters=10,
kernel_size=(4, 1),
strides=(2, 1),
padding="same",
data_format="channels_last",
activation=tf.keras.activations.relu,
use_bias=True,
kernel_initializer=tf.keras.initializers.glorot_normal,
bias_initializer=tf.keras.initializers.zeros)
self.pool2 = tf.keras.layers.MaxPool2D(pool_size=(2, 1),
strides=(2, 1),
padding="same",
data_format="channels_last")
self.flattened = tf.keras.layers.Flatten(data_format="channels_last")
self.fcl1 = tf.keras.layers.Dense(units=500,
activation=tf.keras.activations.softmax,
use_bias=True,
kernel_initializer=tf.keras.initializers.glorot_normal,
bias_initializer=tf.keras.initializers.zeros)
self.fcl2 = tf.keras.layers.Dense(units=4,
activation=tf.keras.activations.softmax,
use_bias=True,
kernel_initializer=tf.keras.initializers.glorot_normal,
bias_initializer=tf.keras.initializers.zeros)
if loadFile is not None:
self.load_weights(loadFile)
def call(self, inputs, training: bool = False):
conv1 = self.conv1(inputs)
pool1 = self.pool1(conv1)
conv2 = self.conv2(pool1)
pool2 = self.pool2(conv2)
flattened = self.flattened(pool2)
fcl1 = self.fcl1(flattened)
return self.fcl2(fcl1)
When I run the above, I get the following warning:
W tensorflow/python/util/util.cc:280] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
followed by the following error:
File "C:/Users/lavre/Desktop/Programming/Python/MyProject/neural_net/model.py", line 155, in <module>
model.save(value, overwrite=True, save_format="tf")
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1213, in save
saving.save_model(self, filepath, overwrite, include_optimizer, save_format)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\saving\save.py", line 106, in save_model
saved_model.save(model, filepath, overwrite, include_optimizer)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\saving\saved_model.py", line 1492, in save
save_lib.save(model, filepath)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\saved_model\save.py", line 849, in save
saveable_view, asset_info.asset_index)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\saved_model\save.py", line 604, in _serialize_object_graph
_write_object_proto(obj, obj_proto, asset_file_def_index)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\saved_model\save.py", line 643, in _write_object_proto
metadata=obj._tracking_metadata)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 2195, in _tracking_metadata
metadata['config'] = self.get_config()
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\layers\convolutional.py", line 252, in get_config
'kernel_initializer': initializers.serialize(self.kernel_initializer),
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\initializers.py", line 166, in serialize
return serialize_keras_object(initializer)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 140, in serialize_keras_object
instance.get_config())
TypeError: get_config() missing 1 required positional argument: 'self'
Why am I getting this error? Also what does the first warning mean?
change your initializer part, you can do like this:
self.conv1 = tf.keras.layers.Conv2D(filters=10,
kernel_size=(10, 30),
strides=(1, 30),
padding="same",
data_format="channels_last",
activation=tf.keras.activations.relu,
use_bias=True,
kernel_initializer=tf.keras.initializers.glorot_normal(),
bias_initializer=tf.keras.initializers.zeros())
change other layers like this, it's useful for my code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With