Im unable to reshape tensor loaded from my own custom dataset. As shown below ds_train
has batch size of 8 and I want to reshape it such as: len(ds_train),128*128
. So that I can feed the batch to my keras autoencoder model. Im new to TF and couldnt find solutions online, thus posting here.
ds_train = tf.keras.preprocessing.image_dataset_from_directory(
directory=healthy_path,
labels="inferred",
label_mode=None,
color_mode="grayscale",
batch_size=8,
image_size=(128, 128),
shuffle=True,
seed=123,
validation_split=0.05,
subset="training",)
Similarly my model is based on TF2 Functional API as Follows:
inputs = keras.Input(shape=(128*128))
norm = layers.experimental.preprocessing.Rescaling(1./255)(inputs)
encode = layers.Dense(14, activation='relu', name='encode')(norm)
coded = layers.Dense(3, activation='relu', name='coded')(encode)
decode = layers.Dense(14, activation='relu', name='decode')(coded)
decoded = layers.Dense(128*128, activation='sigmoid', name='decoded')(decode)
My attempt at reshaping
ds_train = tf.reshape(ds_train, shape=[-1])
ds_validation = tf.reshape(ds_train, shape=[-1])
#AUTOTUNE = tf.data.experimental.AUTOTUNE
#ds_train = ds_train.cache().prefetch(buffer_size=AUTOTUNE)
#ds_validation = ds_validation.cache().prefetch(buffer_size=AUTOTUNE)
Error :
ValueError: Attempt to convert a value (<BatchDataset shapes: (None, 128, 128, 1), types: tf.float32>) with an unsupported type (<class 'tensorflow.python.data.ops.dataset_ops.BatchDataset'>) to a Tensor.
Entire Error Callstack:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-764a960c83e5> in <module>
----> 1 ds_train = tf.reshape(ds_train, shape=[-1])
2 ds_validation = tf.reshape(ds_train, shape=[-1])
3 #AUTOTUNE = tf.data.experimental.AUTOTUNE
4 #ds_train = ds_train.cache().prefetch(buffer_size=AUTOTUNE)
5 #ds_validation = ds_validation.cache().prefetch(buffer_size=AUTOTUNE)
C:\Anaconda3\lib\site-packages\tensorflow\python\util\dispatch.py in wrapper(*args, **kwargs)
199 """Call target, and fall back on dispatchers if there is a TypeError."""
200 try:
--> 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a
C:\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py in reshape(tensor, shape, name)
193 A `Tensor`. Has the same type as `tensor`.
194 """
--> 195 result = gen_array_ops.reshape(tensor, shape, name)
196 tensor_util.maybe_set_static_shape(result, shape)
197 return result
C:\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py in reshape(tensor, shape, name)
8227 try:
8228 return reshape_eager_fallback(
-> 8229 tensor, shape, name=name, ctx=_ctx)
8230 except _core._SymbolicException:
8231 pass # Add nodes to the TensorFlow graph.
C:\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py in reshape_eager_fallback(tensor, shape, name, ctx)
8247
8248 def reshape_eager_fallback(tensor, shape, name, ctx):
-> 8249 _attr_T, (tensor,) = _execute.args_to_matching_eager([tensor], ctx)
8250 _attr_Tshape, (shape,) = _execute.args_to_matching_eager([shape], ctx, _dtypes.int32)
8251 _inputs_flat = [tensor, shape]
C:\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py in args_to_matching_eager(l, ctx, default_dtype)
261 ret.append(
262 ops.convert_to_tensor(
--> 263 t, dtype, preferred_dtype=default_dtype, ctx=ctx))
264 if dtype is None:
265 dtype = ret[-1].dtype
C:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1497
1498 if ret is None:
-> 1499 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1500
1501 if ret is NotImplemented:
C:\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
336 as_ref=False):
337 _ = as_ref
--> 338 return constant(v, dtype=dtype, name=name)
339
340
C:\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name)
262 """
263 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 264 allow_broadcast=True)
265
266
C:\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
273 with trace.Trace("tf.constant"):
274 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 275 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
276
277 g = ops.get_default_graph()
C:\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
298 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
299 """Implementation of eager constant."""
--> 300 t = convert_to_eager_tensor(value, ctx, dtype)
301 if shape is None:
302 return t
C:\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
96 dtype = dtypes.as_dtype(dtype).as_datatype_enum
97 ctx.ensure_initialized()
---> 98 return ops.EagerTensor(value, ctx.device_name, dtype)
99
100