Reputation: 2743
I am trying to use the Dataset with TF2.0 along with keras ImageDataGenerator, but when I try to call it it give me an error. So this is what I am doing. I have a Data folder where there are 4 folder for each type of category. This I assume will be the label as like the old keras method. There 4 forlders have 72 or so images in them.
Here is the code that I am using to generate the code
augment = True
if augment:
train_datagen = ImageDataGenerator(
rescale=1./ 255,
shear_range=0,
rotation_range=20,
zoom_range=0.15,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
fill_mode='nearest') # set validation split
else:
train_datagen = ImageDataGenerator(
rescale=1./ 255,
horizontal_flip=True,
fill_mode='nearest') # set validation split
images, labels = next(train_datagen.flow_from_directory(DATA_PATH))
print(images.dtype, images.shape)
print(labels.dtype, labels.shape)
input_shape = images.shape[1:]
print("InputShape:", input_shape)
img_shape = (input_shape[0], input_shape[1])
ds = tf.data.Dataset.from_generator(train_datagen.flow_from_directory,
args=[DATA_PATH], output_types=(tf.float32, tf.float32))
This produces this:
Found 324 images belonging to 4 classes.
float32 (32, 256, 256, 3)
float32 (32, 4)
InputShape: (256, 256, 3)
DS: <DatasetV1Adapter shapes: (<unknown>, <unknown>), types: (tf.float32, tf.float32)>
So that looks right to me. So when I try to use it in my model like this
history = model.fit(ds, epochs=10, verbose=1)
It gives me this error:
Epoch 1/10
Traceback (most recent call last):
File "C:/Users/gus/Documents/ImageSimularity/FoodTrainer.py", line 75, in <module>
history = model.fit(ds, epochs=10, verbose=1)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 728, in fit
use_multiprocessing=use_multiprocessing)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 324, in fit
total_epochs=epochs)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 123, in run_one_epoch
batch_outs = execution_function(iterator)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 86, in execution_function
distributed_function(input_fn))
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 457, in __call__
result = self._call(*args, **kwds)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 503, in _call
self._initialize(args, kwds, add_initializers_to=initializer_map)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 408, in _initialize
*args, **kwds))
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\eager\function.py", line 1848, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\eager\function.py", line 2150, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\eager\function.py", line 2041, in _create_graph_function
capture_by_value=self._capture_by_value),
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 915, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 358, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 66, in distributed_function
model, input_iterator, mode)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 112, in _prepare_feed_values
inputs, targets, sample_weights = _get_input_from_iterator(inputs)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 149, in _get_input_from_iterator
distribution_strategy_context.get_strategy(), x, y, sample_weights)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\distribute\distributed_training_utils.py", line 308, in validate_distributed_dataset_inputs
x_values_list = validate_per_replica_inputs(distribution_strategy, x)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\distribute\distributed_training_utils.py", line 356, in validate_per_replica_inputs
validate_all_tensor_shapes(x, x_values)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\distribute\distributed_training_utils.py", line 373, in validate_all_tensor_shapes
x_shape = x_values[0].shape.as_list()
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 1171, in as_list
raise ValueError("as_list() is not defined on an unknown TensorShape.")
ValueError: as_list() is not defined on an unknown TensorShape.
1/Unknown - 0s 10ms/step
1/Unknown - 0s 10ms/step
Process finished with exit code 1
It seems like it starts to run but then stops because nothing is being produced.
Upvotes: 0
Views: 676
Reputation: 1488
Using tf.data.Dataset
with Keras ImageDataGenerator
is a bit tricky. You could instead use Keras built in fit_generator method.
In order to do so, you could skip this part
# ds = tf.data.Dataset.from_generator(train_datagen.flow_from_directory,
# args=[DATA_PATH], output_types=(tf.float32, tf.float32))
and use Keras generator:
train_generator = train_datagen.flow_from_directory(
DATA_PATH,
target_size=(150, 150), # or other parameters you need
batch_size=32,
class_mode='binary')
finally, the training can be invoked via mentioned fit_generator
:
model.fit_generator(
train_generator,
steps_per_epoch=2000,
epochs=50,
validation_data=validation_generator,
validation_steps=800)
The documentation on this topic is pretty good and I suggest checking it out. Cheers!
Upvotes: 1