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

Use non deprecated resnet weights to compute camelyon16 features #294

Merged
merged 4 commits into from
Jan 4, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from openslide import open_slide
from torch.nn import Identity
from torch.utils.data import DataLoader, Dataset, IterableDataset
from torchvision.transforms import Compose, Normalize, ToTensor
from torchvision.transforms import Compose, ToTensor
from tqdm import tqdm

from flamby.utils import read_config, write_value_in_config
Expand Down Expand Up @@ -101,7 +101,9 @@ def save_dict_to_csv(dict_arg, file_name):
df.to_csv(file_name, index=False)


def main(batch_size, num_workers_torch, tile_from_scratch, remove_big_tiff, output_path):
def main(
batch_size, num_workers_torch, tile_from_scratch, remove_big_tiff, output_path
):
"""Function tiling slides that have been downloaded using download.py.

Parameters
Expand Down Expand Up @@ -142,8 +144,8 @@ def main(batch_size, num_workers_torch, tile_from_scratch, remove_big_tiff, outp
debug = True
else:
raise ValueError(
"The dataset was not downloaded in normal or debug mode, \
please run the download script beforehand"
"The dataset was not downloaded in normal or debug mode,"
" please run the download script beforehand"
)

if debug:
Expand Down Expand Up @@ -175,8 +177,17 @@ def main(batch_size, num_workers_torch, tile_from_scratch, remove_big_tiff, outp
suffix=".png",
tissue_percent=60,
)
net = models.resnet50(pretrained=True)

# syntax with pretrained=True is now deprecated
# https://pytorch.org/vision/stable/models.html#initializing-pre-trained-models
resnet_weights = models.ResNet50_Weights.IMAGENET1K_V1
net = models.resnet50(weights=resnet_weights)
net.fc = Identity()

# IMAGENET preprocessing of images scaled between 0. and 1. with ToTensor
resnet_transform = lambda img: resnet_weights.transforms(antialias=True)(img)
transform = Compose([ToTensor(), resnet_transform])

# Probably unnecessary still it feels safer and might speed up computations
for param in net.parameters():
param.requires_grad = False
Expand All @@ -188,13 +199,10 @@ def main(batch_size, num_workers_torch, tile_from_scratch, remove_big_tiff, outp
net = net.cuda()
else:
print(
"Consider using an environment with GPU otherwise the process will \
take weeks."
"Consider using an environment with GPU otherwise the process will "
"take weeks."
)
# IMAGENET preprocessing of images scaled between 0. and 1. with ToTensor
transform = Compose(
[ToTensor(), Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])]
)

path_to_coords_file = os.path.join(
Path(os.path.realpath(__file__)).parent.resolve(),
"tiling_coordinates_camelyon16.csv",
Expand Down Expand Up @@ -291,7 +299,7 @@ def main(batch_size, num_workers_torch, tile_from_scratch, remove_big_tiff, outp
if output_path is not None:
write_value_in_config(config_file, "dataset_path", output_path)

if args.remove_big_tiff:
if remove_big_tiff:
print("Removing all slides")
for slide in slides_paths:
os.remove(slide)
Expand Down Expand Up @@ -319,8 +327,10 @@ def main(batch_size, num_workers_torch, tile_from_scratch, remove_big_tiff, outp
parser.add_argument(
"--remove-big-tiff",
action="store_true",
help="Whether or not to remove the original slides images that take \
up to 800G, after computing the features using them.",
help=(
"Whether or not to remove the original slides images that take "
"up to 800G, after computing the features using them."
),
)
parser.add_argument(
"--output-path", type=str, help="The path where to store the tiles"
Expand Down
Loading