我正在嘗試使用預處理函式來處理資料集地圖,但出現以下錯誤(底部的完整堆疊跟蹤):
ValueError: Tensor-typed variable initializers must either be wrapped in an init_scope or callable (e.g., `tf.Variable(lambda : tf.truncated_normal([10, 40]))`) when building functions. Please file a feature request if this restriction inconveniences you.
以下是重現該問題的完整片段。我的問題是,為什么在一個用例(僅裁剪)中它有效,而當使用 RandomFlip 時卻沒有?如何解決這個問題?
import functools
import numpy as np
import tensorflow as tf
def data_gen():
for i in range(10):
x = np.random.random(size=(80, 80, 3)) * 255 # rgb image
x = x.astype('uint8')
y = np.random.random(size=(40, 40, 1)) * 255 # downsized mono image
y = y.astype('uint8')
yield x, y
def preprocess(image, label, cropped_image_size, cropped_label_size, skip_augmentations=False):
x = image
y = label
x_size = cropped_image_size
y_size = cropped_label_size
if not skip_augmentations:
x = tf.keras.layers.RandomFlip(mode="horizontal")(x)
y = tf.keras.layers.RandomFlip(mode="horizontal")(y)
x = tf.keras.layers.RandomRotation(factor=1.0, fill_mode='constant')(x)
y = tf.keras.layers.RandomRotation(factor=1.0, fill_mode='constant')(y)
x = tf.keras.layers.CenterCrop(x_size, x_size)(x)
y = tf.keras.layers.CenterCrop(y_size, y_size)(y)
return x, y
print(tf.__version__) # 2.6.0
dataset = tf.data.Dataset.from_generator(data_gen, output_signature=(
tf.TensorSpec(shape=(80, 80, 3), dtype='uint8'),
tf.TensorSpec(shape=(40, 40, 1), dtype='uint8')
))
crop_only_fn = functools.partial(preprocess, cropped_image_size=50, cropped_label_size=25, skip_augmentations=True)
train_preprocess_fn = functools.partial(preprocess, cropped_image_size=50, cropped_label_size=25, skip_augmentations=False)
# This works
crop_dataset = dataset.map(crop_only_fn)
# This fails: ValueError: Tensor-typed variable initializers must either be wrapped in an init_scope or callable
train_dataset = dataset.map(train_preprocess_fn)
完整的堆疊跟蹤:
Traceback (most recent call last):
File "./issue_dataaug.py", line 50, in <module>
train_dataset = dataset.map(train_preprocess_fn)
File "/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1861, in map
return MapDataset(self, map_func, preserve_cardinality=True)
File "/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 4985, in __init__
use_legacy_function=use_legacy_function)
File "/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 4218, in __init__
self._function = fn_factory()
File "/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 3151, in get_concrete_function
*args, **kwargs)
File "/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 3116, in _get_concrete_function_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 3463, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 3308, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 1007, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 4195, in wrapped_fn
ret = wrapper_helper(*args)
File "/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 4125, in wrapper_helper
ret = autograph.tf_convert(self._func, ag_ctx)(*nested_args)
File "/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/autograph/impl/api.py", line 695, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
./issue_dataaug.py:25 preprocess *
x = tf.keras.layers.RandomFlip(mode="horizontal")(x)
/...//virtualenvs/cvi36/lib/python3.6/site-packages/keras/layers/preprocessing/image_preprocessing.py:414 __init__ **
self._rng = make_generator(self.seed)
/...//virtualenvs/cvi36/lib/python3.6/site-packages/keras/layers/preprocessing/image_preprocessing.py:1375 make_generator
return tf.random.Generator.from_non_deterministic_state()
/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/ops/stateful_random_ops.py:396 from_non_deterministic_state
return cls(state=state, alg=alg)
/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/ops/stateful_random_ops.py:476 __init__
trainable=False)
/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/ops/stateful_random_ops.py:489 _create_variable
return variables.Variable(*args, **kwargs)
/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/ops/variables.py:268 __call__
return cls._variable_v2_call(*args, **kwargs)
/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/ops/variables.py:262 _variable_v2_call
shape=shape)
/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/ops/variables.py:243 <lambda>
previous_getter = lambda **kws: default_variable_creator_v2(None, **kws)
/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py:2675 default_variable_creator_v2
shape=shape)
/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/ops/variables.py:270 __call__
return super(VariableMetaclass, cls).__call__(*args, **kwargs)
/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py:1613 __init__
distribute_strategy=distribute_strategy)
/...//virtualenvs/cvi36/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py:1695 _init_from_args
raise ValueError("Tensor-typed variable initializers must either be "
ValueError: Tensor-typed variable initializers must either be wrapped in an init_scope or callable (e.g., `tf.Variable(lambda : tf.truncated_normal([10, 40]))`) when building functions. Please file a feature request if this restriction inconveniences you.
uj5u.com熱心網友回復:
我不太確定這是否與您的問題直接相關,但是在TF2.7 上您的代碼根本不起作用,因為所有Keras增強層都期望值float而不是uint8. 所以,也許可以嘗試像這樣投射你的資料:
import functools
import numpy as np
import tensorflow as tf
def data_gen():
for i in range(10):
x = np.random.random(size=(80, 80, 3)) * 255 # rgb image
x = x.astype('uint8')
y = np.random.random(size=(40, 40, 1)) * 255 # downsized mono image
y = y.astype('uint8')
yield x, y
def preprocess(image, label, cropped_image_size, cropped_label_size, skip_augmentations=False):
x = tf.cast(image, dtype=tf.float32)
y = tf.cast(label, dtype=tf.float32)
x_size = cropped_image_size
y_size = cropped_label_size
if not skip_augmentations:
x = tf.keras.layers.RandomFlip(mode="horizontal")(x)
y = tf.keras.layers.RandomFlip(mode="horizontal")(y)
x = tf.keras.layers.RandomRotation(factor=1.0, fill_mode='constant')(x)
y = tf.keras.layers.RandomRotation(factor=1.0, fill_mode='constant')(y)
x = tf.keras.layers.CenterCrop(x_size, x_size)(x)
y = tf.keras.layers.CenterCrop(y_size, y_size)(y)
return tf.cast(x, dtype=tf.uint8), tf.cast(y, dtype=tf.uint8)
print(tf.__version__) # 2.6.0
dataset = tf.data.Dataset.from_generator(data_gen, output_signature=(
tf.TensorSpec(shape=(80, 80, 3), dtype=tf.uint8),
tf.TensorSpec(shape=(40, 40, 1), dtype=tf.uint8)
))
crop_only_fn = functools.partial(preprocess, cropped_image_size=50, cropped_label_size=25, skip_augmentations=True)
train_preprocess_fn = functools.partial(preprocess, cropped_image_size=50, cropped_label_size=25, skip_augmentations=False)
# This works
crop_dataset = dataset.map(crop_only_fn)
# This fails: ValueError: Tensor-typed variable initializers must either be wrapped in an init_scope or callable
train_dataset = dataset.map(train_preprocess_fn)
附帶說明一下,Keras增強層通常用作您計劃訓練的模型的一部分。tf.image例如tf.image.central_crop,您也可以使用這些函式,tf.image.random_flip_left_right甚至tfa.image.rotate.
更新1:你得到的評論中提到的錯誤,因為記錄在這里,層tf.keras.layers.RandomFlip和tf.keras.layers.RandomRotation訓練期間才有效。所以嘗試使用其他方法:
import functools
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import tensorflow_addons as tfa
def preprocess(image, label, cropped_image_size, cropped_label_size, skip_augmentations=False):
x = tf.cast(image, dtype=tf.float32)
y = tf.cast(label, dtype=tf.float32)
x_size = cropped_image_size
y_size = cropped_label_size
if not skip_augmentations:
x = tf.image.random_flip_left_right(x)
y = tf.image.random_flip_left_right(y)
x = tfa.image.rotate(x, 90, fill_mode='constant')
y = tfa.image.rotate(y, 90, fill_mode='constant')
x = tf.keras.layers.CenterCrop(x_size, x_size)(x)
y = tf.keras.layers.CenterCrop(y_size, y_size)(y)
return tf.cast(x, dtype=tf.uint8), tf.cast(y, dtype=tf.uint8)
dataset = tf.data.Dataset.from_generator(data_gen, output_signature=(
tf.TensorSpec(shape=(80, 80, 3), dtype=tf.uint8),
tf.TensorSpec(shape=(40, 40, 1), dtype=tf.uint8)
))
crop_only_fn = functools.partial(preprocess, cropped_image_size=50, cropped_label_size=25, skip_augmentations=True)
train_preprocess_fn = functools.partial(preprocess, cropped_image_size=50, cropped_label_size=25, skip_augmentations=False)
crop_dataset = dataset.map(crop_only_fn)
train_dataset = dataset.map(train_preprocess_fn)
image, _ = next(iter(train_dataset.take(1)))
plt.imshow(image.numpy())
我排除了tf.keras.preprocessing.image.random_rotation,因為它現在似乎不適用于張量。
uj5u.com熱心網友回復:
正如我評論的那樣,你提到的錯誤我沒有發現可重現。然而,它只需要在__init___方法中初始化增強層。
ValueError:
tf.Variable(lambda : tf.truncated_normal([10, 40]))在構建函式時,張量型別的變數初始值設定項必須包裝在 init_scope 或可呼叫(例如,)中。如果此限制給您帶來不便,請提交功能請求。
這是完整的作業代碼。
def data_gen():
for i in range(10):
x = np.random.random(size=(80, 80, 3)) * 255 # rgb image
x = x.astype('uint8')
y = np.random.random(size=(40, 40, 1)) * 255 # downsized mono image
y = y.astype('uint8')
yield x, y
class Augment(tf.keras.layers.Layer):
def __init__(self, seed=42):
super().__init__()
self.flip_a = tf.keras.layers.RandomFlip(mode="horizontal", seed=seed)
self.flip_b = tf.keras.layers.RandomFlip(mode="horizontal", seed=seed)
self.rot_a = tf.keras.layers.RandomRotation(factor=1.0,
fill_mode='constant', seed=seed)
self.rot_b = tf.keras.layers.RandomRotation(factor=1.0,
fill_mode='constant', seed=seed)
def call(self, inputs, labels):
x = self.flip_a(inputs)
x = self.rot_a(x)
y = self.flip_b(labels)
y = self.rot_b(y)
return x, y
def preprocess(image, label, cropped_image_size, cropped_label_size):
x = image
y = label
x_size = cropped_image_size
y_size = cropped_label_size
x = tf.cast(x, dtype=tf.float32)
y = tf.cast(y, dtype=tf.float32)
x = tf.keras.layers.CenterCrop(x_size, x_size)(x)
y = tf.keras.layers.CenterCrop(y_size, y_size)(y)
x = tf.cast(x, dtype=tf.uint8)
y = tf.cast(y, dtype=tf.uint8)
return x, y
資料
dataset = tf.data.Dataset.from_generator(data_gen, output_signature=(
tf.TensorSpec(shape=(80, 80, 3), dtype='uint8'),
tf.TensorSpec(shape=(40, 40, 1), dtype='uint8')
))
測驗 1
crop_only_fn = functools.partial(preprocess,
cropped_image_size=50,
cropped_label_size=25)
# This works
crop_dataset = dataset.map(crop_only_fn)
x, y = next(iter(crop_dataset))
x.shape, y.shape
(TensorShape([50, 50, 3]), TensorShape([25, 25, 1]))
測驗 2
train_preprocess_fn = functools.partial(preprocess,
cropped_image_size=50,
cropped_label_size=25)
train_dataset = dataset.map(train_preprocess_fn)
train_dataset = train_dataset.map(Augment()) # < calling now.
x, y = next(iter(train_dataset))
x.shape, y.shape
(TensorShape([50, 50, 3]), TensorShape([25, 25, 1]))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381032.html
