Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Unet 2D #317

Open
jinxsfe opened this issue Apr 9, 2024 · 8 comments
Open

Issue with Unet 2D #317

jinxsfe opened this issue Apr 9, 2024 · 8 comments
Assignees

Comments

@jinxsfe
Copy link

jinxsfe commented Apr 9, 2024

actually, I trained the UNet 2d Successfullly in 1 years ago, but I trained again in recently and out put error is
image
I don't change the parameter. original data dimension is 1000

I also try to train the new dataset which dimension is 12*12, but also failed

image
I think no need add 3 in third dimension, the data is 32 bit unsigned integer,, the mask is 8 bit-binary.
image

can you give me some hint?

@IvanHCenalmor
Copy link
Collaborator

Dear @jinxsfe,

Not sure about the issue that you are facing. The first image is saying that it cannot find the image on the selected folder. The second one seems to be a problem with the shape of the image.

I have a new version of the notebook on a different branch (while it goes under an additional testing):

U-Net_2D_ZeroCostDL4Mic.ipynb Open In Colab

Please try again with this notebook and if you find the same problem let us know and we will iterate over it 🤗

Thanks again for your valuable feedback!

Iván

@IvanHCenalmor IvanHCenalmor self-assigned this Dec 17, 2024
@jinxsfe
Copy link
Author

jinxsfe commented Jan 27, 2025

Hi @IvanHCenalmor:
Thanks for your a sharing for your new notebooks, yes, I had tested the U-Net 2D and 3D and running successfully , here is the another question relate the notebook and my data set. My data is CryoET denoised data and original dimension is 1024x1024x151.

For U-Net 2D, you had mentioned

Image

the number of step equivalent to the number of samples in the training set divided by the batch size,
I calculate the number of steps below for this way, is correct? or we just need use 604/10

Image

For U-Net 3D, I seperate original data size 1024x1024x151 into 512x512x151, add one more depth and change initial filtering from 32 to 64, but the model still failed to catch the data's infomation, the dataset size is 24, I also set import tensorflow as tf and from tensorflow.keras.mixed_precision import set_global_policy

1. Enable mixed-precision BEFORE creating your model.

set_global_policy('mixed_float16')
" to reduce load for GPU, I use two ways for training, first is resize to patch_size for 224x224x16, second is randomcrop patch size 64x64x64, but model still failed for catch signal even I changed depth and parameters, but I use same data for U-Net2D and U-Net 3D can catch signal (original 4 layer for your notebook and 5 layer that I modified it)

Image

@jinxsfe
Copy link
Author

jinxsfe commented Jan 27, 2025

@IvanHCenalmor @paxcalpt

@IvanHCenalmor
Copy link
Collaborator

Hi @jinxsfe,

Number of steps on U-Net 2D

For the U-Net 2D, for what you want, you don´t need to calculate the number of steps. As it says in the notebook:

"This behaviour can also be obtained by setting it to 0."

So setting the number_of_steps to 0 would be enough.

U-Net 3D

I´m not sure what is the problem that you are facing on this notebook. Is it a memory issue? If you have a modified version of the notebook, could you please send a link to the notebook to have a better idea of which one could be the error? If you send the notebook, it would be also helpful if you keep the output of the cells to have a better insight.

Thanks a lot for the feedback,
Iván

@jinxsfe
Copy link
Author

jinxsfe commented Jan 29, 2025

@IvanHCenalmor Hi Ivan,

I had meet the two issue for UNET 2D and 3D until now.

for UNET 2D , during the unet2D lou calculation(quality), it seems it not working data type for float 32bit,

Image, I don't think normalized is good because it will change the pixel intensities due to CryoET is high resolution, do you have another way to solve this problems?

for UNET 3D, it's not memories issue cause the CNN can continue training, can you send me your personal email and I can send the notebook that I modified and dataset that I have.

Imagevalidation_dice efficient is still 0.0012 so I stop training via click button

@jinxsfe
Copy link
Author

jinxsfe commented Jan 30, 2025

@paxcalpt @cleterrier @IvanHCenalmor
it seems I change the read image method from skimage io to import tifffile, but it still show an error, the original function is "def getIoUvsThreshold(prediction_filepath, groud_truth_filepath):
prediction = io.imread(prediction_filepath)
ground_truth_image = img_as_ubyte(io.imread(groud_truth_filepath, as_gray=True), force_copy=True)

threshold_list = []
IoU_scores_list = []

for threshold in range(0,256):
# Convert to 8-bit for calculating the IoU
mask = img_as_ubyte(prediction, force_copy=True)
mask[mask > threshold] = 255
mask[mask <= threshold] = 0

# Intersection over Union metric
intersection = np.logical_and(ground_truth_image, np.squeeze(mask))
union = np.logical_or(ground_truth_image, np.squeeze(mask))
iou_score = np.sum(intersection) / np.sum(union)

threshold_list.append(threshold)
IoU_scores_list.append(iou_score)

return (threshold_list, IoU_scores_list)" i change it to

import numpy as np
import tifffile
from skimage import img_as_ubyte

def getIoUvsThreshold(prediction_filepath, ground_truth_filepath):
# --- 1) Read the prediction (32-bit float) with tifffile ---
prediction_float = tifffile.imread(prediction_filepath)
# Clamp to [0,1] in case the net produced values slightly out of range
prediction_float = np.clip(prediction_float, 0, 1)
# Convert to 8-bit [0..255]
prediction_ubyte = img_as_ubyte(prediction_float)

# --- 2) Read the ground truth (supposedly 8-bit) with tifffile ---
ground_truth_image = tifffile.imread(ground_truth_filepath)

# If the ground truth is truly 8-bit, ground_truth_image.dtype should be uint8.
# But in case it's not, cast it:
if ground_truth_image.dtype != np.uint8:
    ground_truth_image = ground_truth_image.astype(np.uint8)

# --- 3) Loop thresholds from 0..255 and compute IoU ---
threshold_list = []
IoU_scores_list = []

for threshold in range(256):
    mask = prediction_ubyte.copy()
    mask[mask > threshold] = 255
    mask[mask <= threshold] = 0

    intersection = np.logical_and(ground_truth_image, mask)
    union        = np.logical_or (ground_truth_image, mask)
    iou_score    = np.sum(intersection) / np.sum(union)

    threshold_list.append(threshold)
    IoU_scores_list.append(iou_score)

return (threshold_list, IoU_scores_list), and result is, 

Image I am still can not see the input image, the image should be like

Image which is 32 bit float.

@jinxsfe
Copy link
Author

jinxsfe commented Jan 30, 2025

this is training progress

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants