I tried to build up a custom model by mimicking tf2.0's doc. enter link description here
class CBR(layers.Layer):
"""Convolution + Batch normalisation + Relu"""
def __int__(self, filterNum, kSize, strSize, padMode, name='cbr', **kwargs):
super(CBR, self).__init__(name=name, **kwargs)
self.conv3D = layers.Conv3D(filters=filterNum, kernel_size=kSize, strides=strSize, padding=padMode, data_format='channels_first')
self.BN = layers.BatchNormalization(axis=1)
def call(self, inputs):
x = self.conv3D(inputs)
x=self.BN(x)
return activations.relu(x)
class TestNet(tf.keras.Model):
def __init__(self, inDim, classNum, name='testNet', **kwargs):
super(TestNet, self).__init__(name=name, **kwargs)
self.inDim = inDim
self.classNum = classNum
self.en_st1_cbr1 = CBR(32, 3, 1, 'valid')
def call(self, inputs):
x = layers.Input(shape=self.inDim)
x = self.en_st1_cbr1(x)
outputs = activations.softmax(x, axis=1)
return outputs
When I test it by
classNum = 3
mbSize = 16
inDim = [4, 64, 64, 64]
TNet = TestNet(inDim, classNum)
TNet.build(input_shape=inDim)
it always raises an error on line of x = self.en_st1_cbr1(x)
by printing
File "D:\TProgramFiles\Anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 814, in __call__
with graph.as_default(), backend.name_scope(self._name_scope()):
File "D:\TProgramFiles\Anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\keras\backend.py", line 765, in name_scope
return ops.name_scope_v2(name)
File "D:\TProgramFiles\Anaconda3\envs\keras-gpu\lib\site-packages\tensorflow_core\python\framework\ops.py", line 6422, in __init__
raise ValueError("name for name_scope must be a string.")
ValueError: name for name_scope must be a string.
I followed the example step by step, yet it simply does not work. Can anyone help? Thanks.
[Fixed] name for name_scope must be a string. """ Args: name: The prefix to use on all names created within the name scope.
These Python ValueErrors are built-in exceptions in the Python programming language. The syntax for Python ValueError is: n is the number of values we add inside the brackets.
""" Args: name: The prefix to use on all names created within the name scope. Raises: ValueError: If name is not a string. """ if not isinstance (name, six.string_types): raise ValueError ("name for name_scope must be a string.") self._name = name self._exit_fns = [] @property def name(self): return self._name
I had the same error due to missing square brackets. I guess you have a typo somewhere in your code. For me this code produced the same error
model = tf.keras.Sequential(
feature_extractor,
layers.Dense(num_classes))
Adding square brackets solved the issue
model = tf.keras.Sequential( [
feature_extractor,
layers.Dense(num_classes) ])
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