HenriquesLab / DL4MicEverywhere

Bringing the ZeroCostDL4Mic experience running everywhere via easy-to-install docker images
Creative Commons Attribution 4.0 International
70 stars 10 forks source link

ValueError: could not broadcast input array from shape (256,256,3) into shape (256,256) #50

Open jazberna1 opened 3 months ago

jazberna1 commented 3 months ago

Hello,

I get the following error in section 5.1 Generate prediction(s) from unseen dataset when running the notebook U-Net_2D_ZeroCostDL4Mic.ipynb with this dataset https://zenodo.org/records/5639253 which I believe is given as example.

ValueError                                Traceback (most recent call last)
Cell In[6], line 88, in function_29(output_widget)
     86 predictions = []
     87 for i in tqdm(range(number_of_dataset)):
---> 88   predictions.append(predict_as_tiles(os.path.join(Data_folder, source_dir_list[i]), unet))
     89   # predictions.append(prediction(os.path.join(Data_folder, source_dir_list[i]), os.path.join(Prediction_model_path, Prediction_model_name)))
     90 
     91 # Save the results in the folder along with the masks according to the set threshold
     92 saveResult(Results_folder, predictions, source_dir_list, prefix=prediction_prefix, threshold=None)

Cell In[7], line 514, in predict_as_tiles(Image_path, model)
    512     # Get the prediction from the patch and paste it in the prediction in the right place
    513     predicted_patch = model.predict(patch, batch_size = 1)
--> 514     prediction[xi:xi+patch_size[0], yi:yi+patch_size[1]] = np.squeeze(predicted_patch)
    516 return prediction[0:Image_raw.shape[0], 0: Image_raw.shape[1]]

ValueError: could not broadcast input array from shape (256,256,3) into shape (256,256)

To Reproduce I have run the following sections of the notebook:

  1. Install U-Net dependencies 1.1. Load key dependencies
  2. Select your parameters and paths 2.1. Setting main training parameters 2.2. Data augmentation Here I disable Use_Data_augmentation 2.3. Using weights from a pre-trained model as initial weights Here I selected Use_pretrained_model and the code picks up the appropiate weights
    Weights found in:
    data/training/model/my_model/weights_best.hdf5
  3. Using the trained model 5.1 Generate prediction(s) from unseen dataset Here in setion 5.1 is where I get the error.

Describe the bug ValueError: could not broadcast input array from shape (256,256,3) into shape (256,256)

Expected behavior Get model predictions

Screenshots Screenshot 2024-08-23 at 20 17 04

Desktop (please complete the following information):

Smartphone (please complete the following information): Does not apply

jazberna1 commented 3 months ago

Hi,

I took a look to the code and this is what I think is happening: In function predict_as_tiles the input image is read as one channel image Image_raw = io.imread(Image_path, as_gray = True) Then the placeholder for the whole prediction is set as as having one channel too: prediction = np.zeros(Image.shape) However the predicted_patch has three channels: predicted_patch = model.predict(patch, batch_size = 1)

Hence the error here when trying to fill the prediction array with the corresponding predicted patch prediction[xi:xi+patch_size[0], yi:yi+patch_size[1]] = np.squeeze(predicted_patch)

I did make this change so the prediction array has three channels: prediction = np.zeros((Image.shape[0],Image.shape[1],3))

By doing so I was able to get the predictions:

Screenshot 2024-08-26 at 13 27 37

This is the version I am using:

6. Version log
v2.1.1:

Replaced all absolute pathing with relative pathing
v2.1:

TensorFlow version updated from 1.15 to the one locally installed in Google Colab's virtual machine (currently TensorFlow 2.11).

Export BioImage Model Zoo model has been updated. Currently using version 0.5.8

Generate predictions from unseen data has been corrected to be compatible with other data format (e.g. png)

Installs a new version of fpdf and fixes bugs in the function to write pdf files.
IvanHCenalmor commented 3 months ago

Hi @jazberna1 ,

Thank you very much for reporting this bug! ❤️ Seems that you are right, the seems that the code is only prepared for 1 channel. Your proposed solution seems a really good approach, I will test it and check that it works in other possible scenarios and if works I will upload the newest version and let you know 🤗

Thanks again!