-
Notifications
You must be signed in to change notification settings - Fork 5
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
Sami's Developpement #4
base: Dev
Are you sure you want to change the base?
Changes from 120 commits
05bb833
6328037
62c716b
14309eb
5423e0e
2259dac
73a9e13
e4d69ab
54d0686
4d2707b
06e41ad
a627343
98141f8
8c99ae7
46cade7
536b7f9
7ffa3ce
308e464
abc363d
2a73681
9cc40d3
295e86d
a41fde3
b4417de
954f64d
9734cb4
9cb0d7a
49f8580
b90174e
0c4cfba
7a66ba1
372d898
4094949
08fcda4
5c52e12
a1ad594
28992c6
9e431fd
ce1d571
094b743
b5196a1
df29091
51ce3a3
24e69c9
684ed46
83aa459
c7fa9b3
0952279
24877d2
017b161
35cf7f8
8594219
b203812
cafd069
c523288
3845b38
48b08c2
8989cc4
238d190
4491d00
54e1fee
32ac5d9
7f194b0
3616305
2db9c69
560aa99
5fa81cd
cd77277
0021bac
7da89c2
745a294
733d72c
831f14c
a524bb3
202050c
5c4f783
db3255d
290c259
e7967ed
2d67a74
53e6d5b
56fafa9
46099d8
adc9728
d994745
b4cc422
25a353c
af289fc
e0d85a7
ca8b053
1a80e74
c8f5097
d4cc139
2a8df11
0e9121f
7d234e1
bca58f7
54167f7
8226105
9d34ae8
f4a73f9
bcebf96
dbd7487
2f79d78
bf6a17a
baa3735
e446ce5
12a300d
591494d
89820ac
7ff6988
67d0eff
e5b5255
44302bf
78269d2
ee3bf77
b3ddcb6
cedc40b
d56cc6d
aec59e1
095650f
09f77e6
409d7cc
733c482
2ff92b2
9119cba
a14f26d
e404288
bccc3b3
fca5e68
b0684d7
3bc7acb
725aa85
acddb1d
11e0491
cdd09f6
76869bd
3bd5b28
84ddbb5
05b6ece
6e11ec3
c488dce
bf1fa04
1edd81f
c26e351
8884ef9
cacc806
4e052ed
ed9ef6d
80c1b5e
295b099
e48bc68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
# create a venv : | ||
python -m venv NAME_OF_YOUR_VIRTUAL_ENV | ||
# then install : | ||
pip install pyinstaller python-tk numpy SimpleITK scikit-image tqdm paramiko pyyaml matplotlib | ||
# run the pyinstaller (with console) and exclude modules | ||
pyinstaller -F --clean --upx-dir PATH_TO_UPX PATH_TO_GUI\gui.py --exclude=biom3d --exclude=torch --hidden-import='PIL._tkinter_finder' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,10 @@ def compute_median(path, return_spacing=False): | |
for i in range(len(path_imgs)): | ||
|
||
img,metadata = adaptive_imread(path_imgs[i]) | ||
# Check if the image is 2D (has two dimensions) | ||
if len(img.shape) == 2: | ||
# Add a third dimension with size 1 to make it 3D | ||
img = np.expand_dims(img, axis=0) | ||
spacing = None if not 'spacing' in metadata.keys() else metadata['spacing'] | ||
|
||
assert len(img.shape)>0, "[Error] Wrong image image." | ||
|
@@ -91,6 +95,10 @@ def data_fingerprint(img_dir, msk_dir=None, num_samples=10000): | |
|
||
for i in range(len(path_imgs)): | ||
img,metadata = adaptive_imread(path_imgs[i]) | ||
# Check if the image is 2D (has two dimensions) | ||
if len(img.shape) == 2: | ||
# Add a third dimension with size 1 to make it 3D | ||
img = np.expand_dims(img, axis=0) | ||
spacing = None if not 'spacing' in metadata.keys() else metadata['spacing'] | ||
|
||
# store the size | ||
|
@@ -103,6 +111,10 @@ def data_fingerprint(img_dir, msk_dir=None, num_samples=10000): | |
if msk_dir is not None: | ||
# read msk | ||
msk,_ = adaptive_imread(path_msks[i]) | ||
# Check if the image is 2D (has two dimensions) | ||
if len(msk.shape) == 2: | ||
# Add a third dimension with size 1 to make it 3D | ||
msk = np.expand_dims(msk, axis=0) | ||
|
||
# extract only useful voxels | ||
img = img[msk > 0] | ||
|
@@ -151,7 +163,7 @@ def find_patch_pool_batch(dims, max_dims=(128,128,128), max_pool=5, epsilon=1e-3 | |
batch: numpy.ndarray | ||
Batch size. | ||
""" | ||
# transform tuples into arrays | ||
# transform tuples into arrays | ||
assert len(dims)==3 or len(dims)==4, print("Dims has not the correct number of dimensions: len(dims)=", len(dims)) | ||
if len(dims)==4: | ||
dims=dims[1:] | ||
|
@@ -279,15 +291,23 @@ def get_aug_patch(patch_size): | |
|
||
# ---------------------------------------------------------------------------- | ||
# Display | ||
def parameters_return(patch, pool, batch, config_path): | ||
print(batch) | ||
print(patch) | ||
aug_patch= get_aug_patch(patch) | ||
print(aug_patch) | ||
print(pool) | ||
print(config_path) | ||
|
||
def display_info(patch, pool, batch): | ||
"""Print in terminal the patch size, the number of pooling and the batch size. | ||
""" | ||
|
||
print("*"*20,"YOU CAN COPY AND PASTE THE FOLLOWING LINES INSIDE THE CONFIG FILE", "*"*20) | ||
print("BATCH_SIZE =", batch) | ||
print("PATCH_SIZE =", list(patch)) | ||
aug_patch = get_aug_patch(patch) | ||
print("AUG_PATCH_SIZE =",list(aug_patch)) | ||
print("AUG_PATCH_SIZE =",list(aug_patch)) | ||
print("NUM_POOLS =", list(pool)) | ||
|
||
def auto_config(img_dir=None, median=None, max_dims=(128,128,128), max_batch=16, min_batch=2): | ||
|
@@ -342,6 +362,8 @@ def auto_config(img_dir=None, median=None, max_dims=(128,128,128), max_batch=16, | |
help="(default=\'configs/\') Configuration folder to save the auto-configuration.") | ||
parser.add_argument("--base_config", type=str, default=None, | ||
help="(default=None) Optional. Path to an existing configuration file which will be updated with the preprocessed values.") | ||
parser.add_argument("--remote", default=False, dest='remote', | ||
help="Use this arg when using remote autoconfing only") | ||
args = parser.parse_args() | ||
|
||
median = compute_median(path=args.img_dir, return_spacing=args.spacing) | ||
|
@@ -353,8 +375,24 @@ def auto_config(img_dir=None, median=None, max_dims=(128,128,128), max_batch=16, | |
patch, pool, batch = find_patch_pool_batch(dims=median, max_dims=(args.max_dim, args.max_dim, args.max_dim)) | ||
aug_patch = np.array(patch)+2**(np.array(pool)+1) | ||
|
||
display_info(patch, pool, batch) | ||
|
||
|
||
if args.remote: | ||
try: | ||
from biom3d.utils import save_python_config | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. duplica avec lignes 401 à 411. peut être essayer de compresser |
||
config_path = save_python_config( | ||
config_dir=args.config_dir, | ||
base_config=args.base_config, | ||
BATCH_SIZE=batch, | ||
AUG_PATCH_SIZE=aug_patch, | ||
PATCH_SIZE=patch, | ||
NUM_POOLS=pool, | ||
) | ||
parameters_return(patch, pool, batch, config_path) | ||
except: | ||
print("[Error] Import error. Biom3d must be installed if you want to save your configuration. Another solution is to config the function function in biom3d.utils here...") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. faute d'orthographe |
||
raise ImportError | ||
else : | ||
display_info(patch, pool, batch) | ||
if args.spacing:print("MEDIAN_SPACING =",list(median_spacing)) | ||
if args.median:print("MEDIAN =", list(median)) | ||
|
||
|
@@ -375,4 +413,4 @@ def auto_config(img_dir=None, median=None, max_dims=(128,128,128), max_batch=16, | |
print("[Error] Import error. Biom3d must be installed if you want to save your configuration. Another solution is to config the function function in biom3d.utils here...") | ||
raise ImportError | ||
|
||
# ---------------------------------------------------------------------------- | ||
# ---------------------------------------------------------------------------- |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,24 @@ def centered_crop(img, msk, center, crop_shape, margin=np.zeros(3)): | |
|
||
def located_crop(img, msk, location, crop_shape, margin=np.zeros(3)): | ||
"""Do a crop, forcing the location voxel to be located in the crop. | ||
|
||
Parameters | ||
---------- | ||
img : ndarray | ||
Image data. | ||
msk : ndarray | ||
Mask data. | ||
location : array_like | ||
Specific voxel location to include in the crop. | ||
crop_shape : array_like | ||
Shape of the crop. | ||
margin : array_like, optional | ||
Margin around the location. | ||
|
||
Returns | ||
------- | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indiquer le type de sortie |
||
Cropped image and mask. | ||
""" | ||
img_shape = np.array(img.shape)[1:] | ||
location = np.array(location) | ||
|
@@ -96,6 +114,26 @@ def located_crop(img, msk, location, crop_shape, margin=np.zeros(3)): | |
|
||
def foreground_crop(img, msk, final_size, fg_margin, fg=None, use_softmax=True): | ||
"""Do a foreground crop. | ||
|
||
Parameters | ||
---------- | ||
img : ndarray | ||
Image data. | ||
msk : ndarray | ||
Mask data. | ||
final_size : array_like | ||
Final size of the cropped image and mask. | ||
fg_margin : array_like | ||
Margin around the foreground location. | ||
fg : dict, optional | ||
Foreground information. | ||
use_softmax : bool, optional | ||
If True, assumes softmax activation. | ||
|
||
Returns | ||
------- | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indiquer le type de sortie |
||
Cropped image and mask. | ||
""" | ||
if fg is not None: | ||
locations = fg[random.choice(list(fg.keys()))] | ||
|
@@ -119,6 +157,20 @@ def foreground_crop(img, msk, final_size, fg_margin, fg=None, use_softmax=True): | |
def random_crop(img, msk, crop_shape): | ||
""" | ||
randomly crop a portion of size prop of the original image size. | ||
|
||
Parameters | ||
---------- | ||
img : ndarray | ||
Image data. | ||
msk : ndarray | ||
Mask data. | ||
crop_shape : array_like | ||
Shape of the crop. | ||
|
||
Returns | ||
------- | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indiquer le type de sortie |
||
Cropped image and mask. | ||
""" | ||
img_shape = np.array(img.shape)[1:] | ||
assert len(img_shape)==len(crop_shape),"[Error] Not the same dimensions! Image shape {}, Crop shape {}".format(img_shape, crop_shape) | ||
|
@@ -135,6 +187,20 @@ def random_crop(img, msk, crop_shape): | |
def centered_pad(img, final_size, msk=None): | ||
""" | ||
centered pad an img and msk to fit the final_size | ||
|
||
Parameters | ||
---------- | ||
img : ndarray | ||
Image data. | ||
final_size : array_like | ||
Final size after padding. | ||
msk : ndarray, optional | ||
Mask data. | ||
|
||
Returns | ||
------- | ||
tuple or ndarray | ||
Padded image and mask, or only the image if mask is None. | ||
""" | ||
final_size = np.array(final_size) | ||
img_shape = np.array(img.shape[1:]) | ||
|
@@ -156,6 +222,28 @@ def centered_pad(img, final_size, msk=None): | |
def random_crop_pad(img, msk, final_size, fg_rate=0.33, fg_margin=np.zeros(3), fg=None, use_softmax=True): | ||
""" | ||
random crop and pad if needed. | ||
|
||
Parameters | ||
---------- | ||
img : ndarray | ||
Image data. | ||
msk : ndarray | ||
Mask data. | ||
final_size : array_like | ||
Final size after cropping and padding. | ||
fg_rate : float, optional | ||
Probability of focusing the crop on the foreground. | ||
fg_margin : array_like, optional | ||
Margin around the foreground location. | ||
fg : dict, optional | ||
Foreground information. | ||
use_softmax : bool, optional | ||
If True, assumes softmax activation; otherwise sigmoid is used. | ||
|
||
Returns | ||
------- | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indiquer le type de sortie |
||
Cropped and padded image and mask. | ||
""" | ||
if type(img)==list: # then batch mode | ||
imgs, msks = [], [] | ||
|
@@ -179,6 +267,20 @@ def random_crop_pad(img, msk, final_size, fg_rate=0.33, fg_margin=np.zeros(3), f | |
return img, msk | ||
|
||
class RandomCropAndPadTransform(AbstractTransform): | ||
""" | ||
BatchGenerator transform for random cropping and padding. | ||
|
||
Parameters | ||
---------- | ||
crop_size : array_like | ||
Size of the crop. | ||
fg_rate : float, optional | ||
Probability of focusing the crop on the foreground. | ||
data_key : str, optional | ||
Key for the data in the data dictionary. | ||
label_key : str, optional | ||
Key for the label in the data dictionary. | ||
""" | ||
def __init__(self, crop_size, fg_rate=0.33, data_key="data", label_key="seg"): | ||
self.data_key = data_key | ||
self.label_key = label_key | ||
|
@@ -601,6 +703,31 @@ def get_validation_transforms(patch_size: Union[np.ndarray, Tuple[int]], | |
class BatchGenDataLoader(SlimDataLoaderBase): | ||
""" | ||
Similar as torchio.SubjectsDataset but can be use with an unlimited amount of steps. | ||
|
||
Parameters | ||
---------- | ||
img_dir : str | ||
Directory containing the images. | ||
msk_dir : str | ||
Directory containing the masks. | ||
batch_size : int | ||
Size of the batches. | ||
nbof_steps : int | ||
Number of steps per epoch. | ||
fg_dir : str, optional | ||
Directory containing foreground information. | ||
folds_csv : str, optional | ||
CSV file containing fold information for dataset splitting. | ||
fold : int, optional | ||
Current fold number for training/validation splitting. | ||
val_split : float, optional | ||
Proportion of data to be used for validation. | ||
train : bool, optional | ||
If True, use the dataset for training; otherwise, use it for validation. | ||
load_data : bool, optional | ||
If True, loads the entire dataset into computer memory. | ||
num_threads_in_mt : int, optional | ||
Number of threads in multi-threaded augmentation. | ||
""" | ||
|
||
def __init__( | ||
|
@@ -810,6 +937,39 @@ def configure_rotation_dummyDA_mirroring_and_inital_patch_size(patch_size): | |
return rotation_for_DA, do_dummy_2d_data_aug, initial_patch_size, mirror_axes | ||
|
||
class MTBatchGenDataLoader(MultiThreadedAugmenter): | ||
""" | ||
Multi-threaded data loader for efficient data augmentation and loading. | ||
|
||
Parameters | ||
---------- | ||
img_dir : str | ||
Directory containing the images. | ||
msk_dir : str | ||
Directory containing the masks. | ||
patch_size : array_like | ||
The size of the patches to be extracted. | ||
batch_size : int | ||
Size of the batches. | ||
nbof_steps : int | ||
Number of steps per epoch. | ||
fg_dir : str, optional | ||
Directory containing foreground information. | ||
folds_csv : str, optional | ||
CSV file containing fold information for dataset splitting. | ||
fold : int, optional | ||
Current fold number for training/validation splitting. | ||
val_split : float, optional | ||
Proportion of data to be used for validation. | ||
train : bool, optional | ||
If True, use the dataset for training; otherwise, use it for validation. | ||
load_data : bool, optional | ||
If True, loads the entire dataset into computer memory. | ||
fg_rate : float, optional | ||
Foreground rate for cropping. | ||
num_threads_in_mt : int, optional | ||
Number of threads in multi-threaded augmentation. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
img_dir, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a quoi sert cette fonction? documentation