这段代码中有一个问题,我删除了SeBlock
类,只运行了CNN类,然后一切都很好。如果我将SeBlock
插入CNN
类,则会发生错误,并显示NotImplementedError
。我不知道这个问题的原因,我试图解决这个问题,但我搜索的方法都不起作用。有人能帮我吗,非常感谢!
import tensorflow as tf
class SeBlock(tf.keras.Model):
def __init__(self, ratio, channel):
super(SeBlock, self).__init__()
self.kernel_initializer = tf.keras.initializers.VarianceScaling()
self.bias_initializer = tf.constant_initializer(value=0.0)
self.ratio = ratio
self.ReduceMean = tf.keras.layers.GlobalAveragePooling2D()
self.DenseCut = tf.keras.Sequential([
tf.keras.layers.Dense(units=channel,
activation=tf.nn.relu, kernel_initializer=self.kernel_initializer,
bias_constraint=self.bias_initializer),
tf.keras.layers.Dense(units=channel,
activation=tf.nn.sigmoid,
kernel_initializer=self.kernel_initializer,
bias_constraint=self.bias_initializer)
])
self.flatten = tf.keras.layers.Reshape(target_shape=(1, 1, channel,))
def call(self, inputs, training=True):
if training:print("training network")
x = self.ReduceMean(inputs)
x = self.DenseCut(x, training)
scale = self.flatten(x)
scale = tf.keras.layers.multiply([inputs,scale])
# scale *= inputs
return scale
class CNN(tf.keras.Model):
def __init__(self, se_block):
super(CNN, self).__init__()
self.conv1 = tf.keras.layers.Conv2D(
filters=32, #
kernel_size=[5, 5], #
padding='same', #
activation=tf.nn.relu #
)
self.seblock1 = self._make_layer(se_block= se_block, ratio=1, input_channel=32)
self.pool1 = tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2)
self.conv2 = tf.keras.layers.Conv2D(
filters=64,
kernel_size=[5, 5],
padding='same',
activation=tf.nn.relu
)
self.pool2 = tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2)
self.flatten = tf.keras.layers.Reshape(target_shape=(112 * 112 * 64,))
self.dense2 = tf.keras.layers.Dense(units=10)
def _make_layer(self, se_block, ratio, input_channel):
return tf.keras.Sequential([se_block(ratio=ratio,channel=input_channel)])
def call(self, inputs, training=True):
print("1",inputs.get_shape().as_list())
x = self.conv1(inputs) # [batch_size, 28, 28, 32]
# print("start se-block")
x = self.seblock1(x, training)
# print("end se-block")
x = self.pool1(x) # [batch_size, 14, 14, 32]
x = self.conv2(x) # [batch_size, 14, 14, 64]
x = self.pool2(x) # [batch_size, 7, 7, 64]
x = self.flatten(x) # [batch_size, 7 * 7 * 64]
x = self.dense2(x) # [batch_size, 10]
return tf.nn.softmax(x)
def CNNDense():
return CNN(SeBlock)
主要代码如下
import tensorflow as tf
import LoadImage as readimage
import DenseBSE
tf.keras.backend.clear_session()
train_path = r"E:BaiduNetdiskDownload板角boardtrain"
test_path = r"E:BaiduNetdiskDownload板角boardtest"
BatchSize = 4
Epoch = 60
lr = 0.001
ds_train, train_count = readimage.load_tensor_img(train_path,
batch_size=BatchSize,
epoch=Epoch)
ds_test, test_count = readimage.load_tensor_img(test_path,
batch_size=BatchSize,
epoch=Epoch)
model = DenseBSE.CNNDense()
model.build(input_shape=(BatchSize, 448, 448, 3))
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
epoch_steps = train_count // BatchSize
val_steps = test_count // BatchSize
model.fit(ds_train, epochs=Epoch, steps_per_epoch = epoch_steps,
validation_data=ds_test, validation_steps = val_steps)
下面显示错误信息
Traceback (most recent call last):
File "C:ProgramDataAnaconda3libsite-packagesIPythoncoreinteractiveshell.py", line 3343, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-41c34ae2b3b4>", line 1, in <module>
runfile('E:/PythonProject/CNN_training.py', wdir='E:/PythonProject')
File "C:Program FilesJetBrainsPyCharm 2020.3.5pluginspythonhelperspydev_pydev_bundlepydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:Program FilesJetBrainsPyCharm 2020.3.5pluginspythonhelperspydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"n", file, 'exec'), glob, loc)
File "E:/PythonProject/CNN_training.py", line 35, in <module>
model.fit(ds_train, epochs=Epoch, steps_per_epoch = epoch_steps,
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasenginetraining.py", line 1100, in fit
tmp_logs = self.train_function(iterator)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerdef_function.py", line 828, in __call__
result = self._call(*args, **kwds)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerdef_function.py", line 871, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerdef_function.py", line 725, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerfunction.py", line 2969, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerfunction.py", line 3361, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerfunction.py", line 3196, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythonframeworkfunc_graph.py", line 990, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerdef_function.py", line 634, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythonframeworkfunc_graph.py", line 977, in wrapper
raise e.ag_error_metadata.to_exception(e)
NotImplementedError: in user code:
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasenginetraining.py:805 train_function *
return step_function(self, iterator)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasenginetraining.py:795 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:ProgramDataAnaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasenginetraining.py:788 run_step **
outputs = model.train_step(data)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasenginetraining.py:757 train_step
self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasoptimizer_v2optimizer_v2.py:498 minimize
return self.apply_gradients(grads_and_vars, name=name)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasoptimizer_v2optimizer_v2.py:631 apply_gradients
return distribute_ctx.get_replica_context().merge_call(
C:ProgramDataAnaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:2941 merge_call
return self._merge_call(merge_fn, args, kwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:2948 _merge_call
return merge_fn(self._strategy, *args, **kwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasoptimizer_v2optimizer_v2.py:682 _distributed_apply **
update_ops.extend(distribution.extended.update(
C:ProgramDataAnaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:2494 update
return self._update(var, fn, args, kwargs, group)
C:ProgramDataAnaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:3431 _update
return self._update_non_slot(var, fn, (var,) + tuple(args), kwargs, group)
C:ProgramDataAnaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:3437 _update_non_slot
result = fn(*args, **kwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasoptimizer_v2optimizer_v2.py:661 apply_grad_to_update_var **
return var.assign(var.constraint(var))
C:ProgramDataAnaconda3libsite-packagestensorflowpythonopsinit_ops_v2.py:290 __call__
return constant_op.constant(self.value, dtype=dtype, shape=shape)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonframeworkconstant_op.py:264 constant
return _constant_impl(value, dtype, shape, name, verify_shape=False,
C:ProgramDataAnaconda3libsite-packagestensorflowpythonframeworkconstant_op.py:281 _constant_impl
tensor_util.make_tensor_proto(
C:ProgramDataAnaconda3libsite-packagestensorflowpythonframeworktensor_util.py:454 make_tensor_proto
if shape is not None and np.prod(shape, dtype=np.int64) == 0:
<__array_function__ internals>:5 prod
C:ProgramDataAnaconda3libsite-packagesnumpycorefromnumeric.py:2961 prod
return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
C:ProgramDataAnaconda3libsite-packagesnumpycorefromnumeric.py:90 _wrapreduction
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonopsresource_variable_ops.py:483 __array__
return np.asarray(self.numpy())
C:ProgramDataAnaconda3libsite-packagestensorflowpythonopsresource_variable_ops.py:619 numpy
raise NotImplementedError(
NotImplementedError: numpy() is only available when eager execution is enabled.
这个笔记本应该有助于升级、检查和启用。祝你好运