t-kalinowski / deep-learning-with-R-2nd-edition-code

Code from the book "Deep Learning with R, 2nd Edition"
https://blogs.rstudio.com/ai/posts/2022-05-31-deep-learning-with-r-2e/
41 stars 18 forks source link

image_dataset_from_directory() error: 'size' must be a 1-D int32 Tensor #12

Closed jonbry closed 5 months ago

jonbry commented 5 months ago

I'm working on the example problem in Chapter 8 for classifying cats and dog images, but I can't seem to get image_dataset_from_directory() to work:

train_dataset <-
  image_dataset_from_directory(new_base_dir / "train",
                               image_size = c(180, 180),
                               batch_size = 32)
#> Error in image_dataset_from_directory(new_base_dir/"train", image_size = c(180, : could not find function "image_dataset_from_directory"

Created on 2024-04-03 with reprex v2.1.0

The regex error is a bit different then what shows up in console:

Found 2000 files belonging to 2 classes.
Error in py_call_impl(callable, call_args$unnamed, call_args$named) : 
  ValueError: 'size' must be a 1-D int32 Tensor

Here's the python last error:

── Python Exception Message ──────────
Traceback (most recent call last):
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/keras/src/utils/image_dataset_utils.py", line 313, in image_dataset_from_directory
    dataset = paths_and_labels_to_dataset(
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/keras/src/utils/image_dataset_utils.py", line 365, in paths_and_labels_to_dataset
    img_ds = path_ds.map(
  File "/Users/<user_name>.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 2299, in map
    return map_op._map_v2(
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/data/ops/map_op.py", line 40, in _map_v2
    return _ParallelMapDataset(
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/data/ops/map_op.py", line 148, in __init__
    self._map_func = structured_function.StructuredFunctionWrapper(
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/data/ops/structured_function.py", line 265, in __init__
    self._function = fn_factory()
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 1251, in get_concrete_function
    concrete = self._get_concrete_function_garbage_collected(*args, **kwargs)
  File "/Users<user_name>t/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 1221, in _get_concrete_function_garbage_collected
    self._initialize(args, kwargs, add_initializers_to=initializers)
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 696, in _initialize
    self._concrete_variable_creation_fn = tracing_compilation.trace_function(
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py", line 178, in trace_function
    concrete_function = _maybe_define_function(
  File "/Users<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py", line 283, in _maybe_define_function
    concrete_function = _create_concrete_function(
  File "/Users/<user_name>t/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py", line 310, in _create_concrete_function
    traced_func_graph = func_graph_module.func_graph_from_py_func(
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/framework/func_graph.py", line 1059, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 599, in wrapped_fn
    out = weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/data/ops/structured_function.py", line 231, in wrapped_fn
    ret = wrapper_helper(*args)
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/data/ops/structured_function.py", line 161, in wrapper_helper
    ret = autograph.tf_convert(self._func, ag_ctx)(*nested_args)
  File "/Users/<user_name>.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/autograph/impl/api.py", line 690, in wrapper
    return converted_call(f, args, kwargs, options=options)
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/autograph/impl/api.py", line 377, in converted_call
    return _call_unconverted(f, args, kwargs, options)
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/autograph/impl/api.py", line 459, in _call_unconverted
    return f(*args, **kwargs)
  File "/Users<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/keras/src/utils/image_dataset_utils.py", line 366, in <lambda>
    lambda x: load_image(x, *args), num_parallel_calls=tf.data.AUTOTUNE
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/keras/src/utils/image_dataset_utils.py", line 402, in load_image
    img = tf.image.resize(img, image_size, method=interpolation)
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/ops/image_ops_impl.py", line 1475, in _resize_images_common
    raise ValueError('\'size\' must be a 1-D int32 Tensor')
ValueError: 'size' must be a 1-D int32 Tensor

── R Traceback ───────────────────────
    ▆
 1. └─keras3::image_dataset_from_directory(...)
 2.   ├─base::do.call(keras$utils$image_dataset_from_directory, args)
 3.   └─reticulate (local) `<python.builtin.function>`(...)
 4.     └─reticulate:::py_call_impl(callable, call_args$unnamed, call_args$named)

Let me know if there is any additional information I can provide to help troubleshoot the issue.

Thank you!

t-kalinowski commented 5 months ago

Thanks for reporting, this is a bug in the R package wrapper - will fix shortly. For now, you can work around it by explicitly supplying integers to image_size.

train_dataset <-
  image_dataset_from_directory(new_base_dir / "train",
                               image_size = as.integer(c(180, 180)),
                               batch_size = 32)
t-kalinowski commented 5 months ago

Fixed in the development version of keras3 now: https://github.com/rstudio/keras/commit/8e390879ea22a4efbdc65476ce2a8473e010bb77