Repository navigation
AttributeError: 'deeptrack.deeplay' has no attribute 'Model' and RuntimeError with channel mismatch #242
Description
Activity
It should work if you make the following additional changes:
import deeptrack.deeplay as dl import torchmetrics as tm import torch unet_reg = dl.Regressor( model=net, loss=torch.nn.CrossEntropyLoss(), optimizer=dl.Adam(lr=1e-3), metrics=[tm.F1Score(task="multiclass", num_classes=3)], ).create() data_pipeline = (((image_of_particles >> dt.MoveAxis(-1, 0)) & (image_of_particles >> get_target_image)) >> dt.pytorch.ToTensor(dtype=torch.float)) # This prepares the pipeline for PyTorch. dataset = dt.pytorch.Dataset(data_pipeline, length=640, replace=0.01) train_loader = torch.utils.data.DataLoader(dataset, batch_size=8, shuffle=True) unet_trainer = dl.Trainer(max_epochs=200, accelerator="auto") # auto here should select automatically CPU/GPU. unet_trainer.fit(unet_reg, train_loader)
and for the image prediction:
predicted_image = unet_reg.predict(input_image).softmax(1) ... plt.imshow(input_image[i, 0, ...], cmap="gray") ...
Please, check it and let us know if you need more help.
Note: We're in the process to adapt all examples to DeepTarck2 2.0.0 to accommodate the shift to torch and deeplay from TensorFlow and Keras.
Some already updated notebooks that might be relevant for you are here:
https://github.com/DeepTrackAI/DeepLearningCrashCourse/blob/main/Ch05_UNet/ec05_1_unet/unet.ipynb
https://github.com/DeepTrackAI/DeepLearningCrashCourse/blob/main/Ch05_UNet/ec05_A_qdots_localization/qdots_localization.ipynb
https://github.com/DeepTrackAI/DeepLearningCrashCourse/blob/main/Ch05_UNet/ec05_B_cell_counting/cell_counting.ipynb@LiYuan-SJTU We have now corrected the notebook and it's working. See: https://github.com/DeepTrackAI/DeepTrack2/blob/develop/examples/tutorials/03.%20distinguishing_particles_in_brightfield_tutorial.ipynb
Thank you for providing the updated version of
03. distinguishing_particles_in_brightfield_tutorial.ipynb. I’ve tried running the new version, but I encountered another issue.When executing the following line:
unet_trainer.fit(unet_reg, train_loader)
I got the error:
RuntimeError: expected scalar type Long but found Float.
The error seems to originate from:
return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)located inlighting\pytorch\trainer\trainer.py.
Could you help clarify how to resolve this issue? Is it related to the data type of the labels or inputs in the train_loader? Or is it a compatibility issue with the Lightning package? My lightning package version is 2.5.0.post0.Thanks again for your assistance!
- locked and limited conversation to collaborators
on Feb 12, 2025
I downloaded the
03. distinguishing_particles_in_brightfield_tutorial.ipynbfrom the tutorial and ran it locally. However, I encountered the following error when executing the code:To resolve this, I modified the code to use dl.Regressor instead of dl.Model:
This works, but then I encounter another error when executing the following code:
My pytorch version is 2.2.1 and python version is 3.11.5.
dl.Regressorinstead ofdl.Model correct? If yes, how can I handle the RuntimeError related to channel mismatch in the following code?Thanks in advance for your help!