1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] I got a problem with my U-Net model i don't know how to fix it

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024 às 22:12.

  1. Stack

    Stack Membro Participativo

    I'm building a U-Net model to detect breast cancer, I got the dataset from here: https://www.kaggle.com/datasets/aryashah2k/breast-ultrasound-images-dataset

    Even though, all images are of format png, when trying to training my model an error occurs that my images are not of adequate format. here is the error:

    ---------------------------------------------------------------------------
    InvalidArgumentError Traceback (most recent call last)
    Cell In[51], line 7
    5 train_dataset = image_ds.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE)
    6 print(image_ds.element_spec)
    ----> 7 model_history = unet.fit(train_dataset, epochs=EPOCHS)

    File ~\anaconda3\Lib\site-packages\keras\src\utils\traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    119 filtered_tb = _process_traceback_frames(e.__traceback__)
    120 # To get the full stack trace, call:
    121 # `keras.config.disable_traceback_filtering()`
    --> 122 raise e.with_traceback(filtered_tb) from None
    123 finally:
    124 del filtered_tb

    File ~\anaconda3\Lib\site-packages\tensorflow\python\eager\execute.py:53, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
    51 try:
    52 ctx.ensure_initialized()
    ---> 53 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
    54 inputs, attrs, num_outputs)
    55 except core._NotOkStatusException as e:
    56 if name is not None:

    InvalidArgumentError: Graph execution error:

    Detected at node decode_image/DecodeImage defined at (most recent call last):
    <stack traces unavailable>
    Error in user-defined function passed to MapDataset:3 transformation with iterator: Iterator::Root::prefetch::BatchV2::Shuffle::MemoryCacheImpl::Filter::parallelMapV2: Unknown image file format. One of JPEG, PNG, GIF, BMP required.
    [[{{node decode_image/DecodeImage}}]]
    [[IteratorGetNext]] [Op:__inference_one_step_on_iterator_9520]


    The erro only occurs when trying to train the model, and it refers to this function:

    def preprocess_image(image, mask, target_size=(256, 256)):
    try:
    # Decode the image and mask safely
    image = tf.io.decode_image(image, channels=3, expand_animations=False)
    mask = tf.io.decode_image(mask, channels=1, expand_animations=False)

    # Check for undefined or zero dimensions
    if image.shape is None or image.shape[0] == 0 or image.shape[1] == 0:
    print(f"Error: Image has undefined or zero dimensions: {image.shape}")
    return None, None

    if mask.shape is None or mask.shape[0] == 0 or mask.shape[1] == 0:
    print(f"Error: Mask has undefined or zero dimensions: {mask.shape}")
    return None, None

    # Ensure the image has exactly 3 channels (RGB)
    if image.shape[-1] != 3:
    print(f"Error: Image does not have 3 channels (found {image.shape[-1]}).")
    return None, None

    # Normalize the image to range [0, 1]
    image = tf.image.convert_image_dtype(image, tf.float32)

    # Resize image and mask to target dimensions (256*256)
    image = tf.image.resize(image, target_size, method='nearest')
    mask = tf.image.resize(mask, target_size, method='nearest')

    # Convert mask to a binary (0 or 1) format for classification tasks
    mask = tf.cast(tf.math.reduce_max(mask, axis=-1, keepdims=True) > 0, tf.float32) # Ensure binary mask

    return image, mask

    except Exception as e:
    print(f"Error during preprocessing: {str(e)}")
    return None, None

    # Apply the preprocessing function to the dataset
    image_ds = dataset.map(preprocess_image)

    # Filter out None values returned by preprocess_image
    image_ds = image_ds.filter(lambda img, mask: img is not None and mask is not None)


    I tried multiple ways trying to fix this using chatgpt but nothing seems to work.

    Continue reading...

Compartilhe esta Página