回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我一直在玩弄TensorFlow图像分类的数据增强。我使用了一个教程来创建一个自定义的增强类,代码如下,然后使用Keras Lamda将其加载到模型中。培训工作正常,但是当我尝试在培训后立即保存模型时,它会崩溃,出现标题中提供的错误</p>
<p>我对python非常陌生,但我的理解是该类应该有一个隐藏的变量名。有什么好处</p>
<p>自定义扩充类</p>
<pre><code>class CustomAugment(object):
def __call__(self, image):
# Random flips and grayscale with some stochasticity
img = self._random_apply(tf.image.flip_left_right, image, p=0.6)
img = self._random_apply(self._color_drop, img, p=0.9)
return img
def _color_drop(self, x):
image = tf.image.rgb_to_grayscale(x)
image = tf.tile(x, [1, 1, 1, 3])
return x
def _random_apply(self, func, x, p):
return tf.cond(
tf.less(tf.random.uniform([], minval=0, maxval=1, dtype=tf.float32),
tf.cast(p, tf.float32)),
lambda: func(x),
lambda: x)
data_augmentation = keras.Sequential(
[
tf.keras.layers.Lambda(CustomAugment()),
layers.experimental.preprocessing.RandomRotation(0.1),
]
)
</code></pre>
<p>在模型中使用它</p>
<pre><code>model = Sequential([
tf.keras.Input(shape=(160, 160, 3)),
data_augmentation,
layers.Conv2D(16, 3, padding='same', activation='relu', input_shape=(160, 160, 3)),
layers.Conv2D(16, 3, padding='same', activation='relu'),
#rest of model here
</code></pre>
<p>在这里撞车</p>
<pre><code>model.save('el_patho_maximo')
</code></pre>