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

Compare Virtual Staining Models with Metrics #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions 1.develop_vision_models/ImageMetaDataset.py
MattsonCam marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import pathlib
MattsonCam marked this conversation as resolved.
Show resolved Hide resolved
from typing import Optional

import numpy as np
import torch
from albumentations import ImageOnlyTransform
from PIL import Image
from torch.utils.data import Dataset


class ImageMetaDataset(Dataset):
"""Iterable Image Dataset for Stained Images, which supports applying transformations to the inputs and targets"""
MattsonCam marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line looks a little longer than the usual Python line length expectations. Consider using multiple lines and adding a formatter to check for this type of thing (Black or ruff-format, for example).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I'll change that


def __init__(
self,
_input_dir: pathlib.Path,
_target_dir: pathlib.Path,
_input_transform: Optional[ImageOnlyTransform] = None,
_target_transform: Optional[ImageOnlyTransform] = None
):
self.__input_dir = _input_dir
self.__target_dir = _target_dir

# Retrieve all data from the specified directory
self.__image_path = list(self.__input_dir.glob('*'))

self.__input_transform = _input_transform
self.__target_transform = _target_transform

def __len__(self):
return len(self.__image_path)

@property
def input_transform(self):
return self.__input_transform

@property
def target_transform(self):
return self.__target_transform

@property
def input_name(self):
if not self.__input_name:
raise ValueError("The input is not yet defined, so __input_name is not defined.")
return self.__input_name

@property
def target_name(self):
MattsonCam marked this conversation as resolved.
Show resolved Hide resolved
if not self.__target_name:
raise ValueError("The target is not yet defined, so __target_name is not defined.")
return self.__target_name

def __getitem__(self, _idx):
"""Retrieve input and target image stain"""
MattsonCam marked this conversation as resolved.
Show resolved Hide resolved

self.__input_name = self.__image_path[_idx].name
self.__target_name = str(self.__input_name).replace("CH0", "CH2").replace("dapi", "gold")
MattsonCam marked this conversation as resolved.
Show resolved Hide resolved
input_image = np.array(Image.open(self.__input_dir / self.__input_name).convert('I;16'))
target_image = np.array(Image.open(self.__target_dir / self.__target_name).convert('I;16'))

if self.__input_transform:
input_image = self.__input_transform(image=input_image)["image"]
input_image = torch.from_numpy(input_image).unsqueeze(0).float()

if self.__target_transform:
target_image = self.__target_transform(image=target_image)["image"]
target_image = torch.from_numpy(target_image).unsqueeze(0).float()

return input_image, target_image, {"input_name": self.__input_name,
"target_name": self.__target_name
}
MattsonCam marked this conversation as resolved.
Show resolved Hide resolved
Loading