提问人:Joshua Arenson 提问时间:10/26/2023 最后编辑:Joshua Arenson 更新时间:10/27/2023 访问量:60
了解如何在 keras 模型中正确使用 keras.layers.Normalization per image
Understanding how to properly use keras.layers.Normalization per image in a keras model
问:
我想将归一化预处理层合并到我的 keras 模型中,以规范化每个图像。这些图像是只有一个通道的 3D 荧光显微镜图像,因此 (batch_size,x,y,z,1)。相关:我认为我还应该在任何增强层之后应用这种归一化,因为模型应该预测的任何数据也会首先归一化,对吗?
如果我想在整个训练集中进行归一化,我可以用
InSize = xTrain.shape[1:]
inputs = keras.Input(shape = InSize + (1,), name = "InputLayer")
normalizer = keras.layers.Normalization(axis = None, name = 'NormLayer')
normalizer.adapt(xTrain)
x = normalizer(inputs)
模型的其余部分如下。 是训练数据。但是,当我更改为 .我需要调整normalizer.adapt,但我不确定如何调整。我试过了xTrain
axis = None
axis = 0
normalizer.adapt(xTrain,batch_size = batch_size)
但这产生了错误
/mnt/e/FLYWORKS Dropbox/Tamiki Komatsuzaki/jarenson/Code/Classifier/CanNonCan_v1.0.1.ipynb Cell 18 line 2
22 # thresholder = tf.keras.layers.ThresholdedReLU(theta = Threshold, name = 'ThresholdLayer')
23 # thresholder = tf.keras.layers.ReLU(max_value = max_value, negative_slope = 0.0, threshold = Threshold)
24 print(xTrain.shape)
---> 25 normalizer.adapt(xTrain,batch_size = batch_size)
27 inputs = keras.Input(shape = InSize + (1,), name = "InputLayer")
28 # x = thresholder(inputs)
29 # normalizer.adapt(x,batch_size = batch_size)
File ~/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/layers/preprocessing/normalization.py:286, in Normalization.adapt(self, data, batch_size, steps)
240 def adapt(self, data, batch_size=None, steps=None):
241 """Computes the mean and variance of values in a dataset.
242
243 Calling `adapt()` on a `Normalization` layer is an alternative to
(...)
284 argument is not supported with array inputs.
285 """
--> 286 super().adapt(data, batch_size=batch_size, steps=steps)
File ~/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/engine/base_preprocessing_layer.py:258, in PreprocessingLayer.adapt(self, data, batch_size, steps)
256 with data_handler.catch_stop_iteration():
257 for _ in data_handler.steps():
--> 258 self._adapt_function(iterator)
259 if data_handler.should_sync:
260 context.async_wait()
File ~/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb
File /tmp/__autograph_generated_filerl2staau.py:10, in outer_factory.<locals>.inner_factory.<locals>.tf__adapt_step(iterator)
8 with ag__.FunctionScope('adapt_step', 'fscope', ag__.ConversionOptions(recursive=True, user_requested=True, optional_features=(), internal_convert_user_code=True)) as fscope:
9 data = ag__.converted_call(ag__.ld(next), (ag__.ld(iterator),), None, fscope)
---> 10 ag__.converted_call(ag__.ld(self)._adapt_maybe_build, (ag__.ld(data),), None, fscope)
11 ag__.converted_call(ag__.ld(self).update_state, (ag__.ld(data),), None, fscope)
File ~/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/engine/base_preprocessing_layer.py:295, in PreprocessingLayer._adapt_maybe_build(self, data)
292 if batch_input_shape is None:
293 # Set the number of dimensions.
294 self._batch_input_shape = data_shape_nones
--> 295 self.build(data_shape)
296 self.built = True
File ~/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/layers/preprocessing/normalization.py:187, in Normalization.build(self, input_shape)
185 for d in self._keep_axis:
186 if input_shape[d] is None:
--> 187 raise ValueError(
188 "All `axis` values to be kept must have known shape. "
189 "Got axis: {}, "
190 "input shape: {}, with unknown axis at index: {}".format(
191 self.axis, input_shape, d
192 )
193 )
194 # Axes to be reduced.
195 self._reduce_axis = [d for d in range(ndim) if d not in self._keep_axis]
ValueError: in user code:
File "/home/jgarenson/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/engine/base_preprocessing_layer.py", line 122, in adapt_step *
self._adapt_maybe_build(data)
File "/home/jgarenson/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/engine/base_preprocessing_layer.py", line 295, in _adapt_maybe_build **
self.build(data_shape)
File "/home/jgarenson/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/layers/preprocessing/normalization.py", line 187, in build
raise ValueError(
ValueError: All `axis` values to be kept must have known shape. Got axis: (0,), input shape: [None, 27, 54, 198], with unknown axis at index: 0
我尝试将代码更改为
inputs = keras.Input(shape = InSize + (1,), name = "InputLayer")
normalizer = keras.layers.Normalization(axis = None, name = 'NormLayer')
normalizer.adapt(xTrain)
x = normalizer(inputs)
但这会产生错误:
File ~/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/layers/preprocessing/normalization.py:286, in Normalization.adapt(self, data, batch_size, steps)
240 def adapt(self, data, batch_size=None, steps=None):
241 """Computes the mean and variance of values in a dataset.
242
243 Calling `adapt()` on a `Normalization` layer is an alternative to
(...)
284 argument is not supported with array inputs.
285 """
--> 286 super().adapt(data, batch_size=batch_size, steps=steps)
File ~/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/engine/base_preprocessing_layer.py:246, in PreprocessingLayer.adapt(self, data, batch_size, steps)
244 if self.built:
245 self.reset_state()
--> 246 data_handler = data_adapter.DataHandler(
247 data,
248 batch_size=batch_size,
249 steps_per_epoch=steps,
250 epochs=1,
251 steps_per_execution=self._steps_per_execution,
252 distribute=False,
253 )
254 self._adapt_function = self.make_adapt_function()
255 for _, iterator in data_handler.enumerate_epochs():
File ~/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/engine/data_adapter.py:1259, in DataHandler.__init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution, distribute)
1256 self._steps_per_execution = steps_per_execution
1258 adapter_cls = select_data_adapter(x, y)
-> 1259 self._adapter = adapter_cls(
1260 x,
1261 y,
1262 batch_size=batch_size,
1263 steps=steps_per_epoch,
1264 epochs=epochs - initial_epoch,
1265 sample_weights=sample_weight,
1266 shuffle=shuffle,
1267 max_queue_size=max_queue_size,
1268 workers=workers,
1269 use_multiprocessing=use_multiprocessing,
1270 distribution_strategy=tf.distribute.get_strategy(),
1271 model=model,
1272 )
1274 strategy = tf.distribute.get_strategy()
1276 self._current_step = 0
File ~/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/engine/data_adapter.py:471, in GenericArrayLikeDataAdapter.__init__(self, *args, **kwargs)
462 def __init__(self, *args, **kwargs):
463 logging.warning(
464 "Keras is training/fitting/evaluating on array-like data. Keras "
465 "may not be optimized for this format, so if your input data "
(...)
468 "load a Dataset instead."
469 )
--> 471 super().__init__(*args, **kwargs)
File ~/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/engine/data_adapter.py:257, in TensorLikeDataAdapter.__init__(self, x, y, sample_weights, sample_weight_modes, batch_size, epochs, steps, shuffle, **kwargs)
251 (sample_weights, _, _) = training_utils.handle_partial_sample_weights(
252 y, sample_weights, sample_weight_modes, check_all_flat=True
253 )
255 inputs = pack_x_y_sample_weight(x, y, sample_weights)
--> 257 num_samples = set(
258 int(i.shape[0]) for i in tf.nest.flatten(inputs)
259 ).pop()
260 _check_data_cardinality(inputs)
262 # If batch_size is not passed but steps is, calculate from the input
263 # data. Default to 32 for backwards compat.
File ~/PythonEnv/Classifier1.0/env/lib/python3.10/site-packages/keras/engine/data_adapter.py:258, in <genexpr>(.0)
251 (sample_weights, _, _) = training_utils.handle_partial_sample_weights(
252 y, sample_weights, sample_weight_modes, check_all_flat=True
253 )
255 inputs = pack_x_y_sample_weight(x, y, sample_weights)
257 num_samples = set(
--> 258 int(i.shape[0]) for i in tf.nest.flatten(inputs)
259 ).pop()
260 _check_data_cardinality(inputs)
262 # If batch_size is not passed but steps is, calculate from the input
263 # data. Default to 32 for backwards compat.
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
最后,我不完全确定在这种情况下应该如何应用.adapt方法。有没有人经历过这个问题?
答: 暂无答案
上一个:序列到序列预测转换器:需要归一化
下一个:如何不均匀地归一化数据集
评论
Insize
xTrain
xTrain
是训练集、 或每个图像的形状。假设输入数据只有 1 个通道,因此使用 in 。我已经编辑了我的帖子以包含该信息。InSize = xTrain.shape[1:]
InSize + (1,)
keras.Input