[Python] Python 3.13 How to apply map(func, parameter) to a list of tuples with a function...

Discussão em 'Python' iniciado por Stack, Agosto 26, 2025.

  1. Stack

    Stack Membro Participativo

    def ProcessImage(path: str, size):
    """
    Process input image file content into a tensor.
    """
    img: tf.Tensor = tf.io.read_file(path)
    img = tf.image.decode_jpeg(img, channels=3)
    # Normalize the colour channel values from 0-255 to 0-1
    img = tf.image.convert_image_dtype(img, tf.float32)
    # Resize the image
    img = tf.image.resize(img, size=[size, size])
    return img

    def get_image_label(path: str, label, size):
    """
    Convenience function to pack 2 input parameters into a tuple.
    Return: (Image, label)
    """
    return ProcessImage(path, size), label

    def create_data_batches(X, Y, batch_size):
    data = tf.data.Dataset.from_tensor_slices((tf.constant(X), tf.constant(Y)))
    return data.map(get_image_label).batch(batch_size) # XXX: How to pass 'size' input param?


    When I try with:

    return data.shuffle(len(X)).map(get_image_label, IMAGE_SIZE).batch(batch_size)


    I get the following error:

    -> 2341 return map_op._map_v2(
    2342 self,
    2343 map_func,
    2344 num_parallel_calls=num_parallel_calls,
    2345 deterministic=deterministic,
    2346 synchronous=synchronous,
    2347 use_unbounded_threadpool=use_unbounded_threadpool,
    2348 name=name,
    ...
    File "/tmp/ipykernel_387325/1370336719.py", line 15, in ProcessImage *
    img = tf.image.resize(img, size=[size, size])

    ValueError: 'size' must be a 1-D int32 Tensor

    Continue reading...

Compartilhe esta Página