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

Docs/sk901: A new readme file is created for the MonAI tutorial #643

Closed
wants to merge 15 commits into from
Closed
Prev Previous commit
Next Next commit
working
mattiasakesson committed Jun 19, 2024
commit 7607b676b8a048986b03733e7cfe0be0c3893ee4
33 changes: 18 additions & 15 deletions examples/monai-2D-mednist/README.rst
Original file line number Diff line number Diff line change
@@ -24,21 +24,6 @@ If using pseudo-distributed mode with docker-compose:
- `Docker <https://docs.docker.com/get-docker>`__
- `Docker Compose <https://docs.docker.com/compose/install>`__

Download and Prepare the data
-------------------------------------------

Install monai

.. code-block::

pip install monai

Download and divide the data into parts. Set the number of
data parts as an arguments python prepare_data.py NR-OF-DATAPARTS. In the
below command we divide the dataset into 10 parts.
.. code-block::

python prepare_data.py 10

Creating the compute package and seed model
-------------------------------------------
@@ -72,6 +57,24 @@ Next, generate a seed model (the first model in a global model trail):

This will create a seed model called 'seed.npz' in the root of the project. This step will take a few minutes, depending on hardware and internet connection (builds a virtualenv).

Download and Prepare the data
-------------------------------------------

Install monai

.. code-block::

pip install monai

Download and divide the data into parts. Set the number of
data parts as an arguments python prepare_data.py NR-OF-DATAPARTS. In the
below command we divide the dataset into 10 parts.
.. code-block::

python prepare_data.py 10



Using FEDn Studio
-----------------

1 change: 0 additions & 1 deletion examples/monai-2D-mednist/client/data.py
Original file line number Diff line number Diff line change
@@ -145,7 +145,6 @@ def __len__(self):
return len(self.image_files)

def __getitem__(self, index):
print("__getitem__ path: ", os.path.join(self.data_path, self.image_files[index]))
return (self.transforms(os.path.join(self.data_path, self.image_files[index])), DATA_CLASSES[os.path.dirname(self.image_files[index])])


14 changes: 4 additions & 10 deletions examples/monai-2D-mednist/client/train.py
Original file line number Diff line number Diff line change
@@ -22,7 +22,6 @@
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.abspath(dir_path))


train_transforms = Compose(
[
LoadImage(image_only=True),
@@ -54,32 +53,27 @@ def train(in_model_path, out_model_path, data_path=None, client_settings_path=No
if client_settings_path is None:
client_settings_path = os.environ.get("FEDN_CLIENT_SETTINGS_PATH", dir_path + "/client_settings.yaml")

print("client_settings_path: ", client_settings_path)
with open(client_settings_path, "r") as fh: # Used by CJG for local training
try:
client_settings = dict(yaml.safe_load(fh))
except yaml.YAMLError:
raise

print("client settings: ", client_settings)
batch_size = client_settings["batch_size"]
max_epochs = client_settings["local_epochs"]
num_workers = client_settings["num_workers"]
split_index = os.environ.get("FEDN_DATA_SPLIT_INDEX")#client_settings["split_index"]
print("split index: ", split_index)
split_index = os.environ.get("FEDN_DATA_SPLIT_INDEX") #client_settings["split_index"]
lr = client_settings["lr"]

if data_path is None:
data_path = os.environ.get("FEDN_DATA_PATH")
print("os.path.join(os.path.dirname(data_path), data_splits.yaml: ", os.path.join(os.path.dirname(data_path), "data_splits.yaml"))

with open(os.path.join(os.path.dirname(data_path), "data_splits.yaml"), "r") as file:
clients = yaml.safe_load(file)

image_list = clients["client " + str(split_index)]["train"]
print("image_list len: ", len(image_list))
train_ds = MedNISTDataset(data_path="app/data/MedNIST", transforms=train_transforms, image_files=image_list)
print("train_ds len: ", len(train_ds))
print("batch_size: ", batch_size, ", num_workers: ", num_workers)

train_ds = MedNISTDataset(data_path=data_path, transforms=train_transforms, image_files=image_list)
train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True, num_workers=num_workers)

# Load parmeters and initialize model
2 changes: 1 addition & 1 deletion examples/monai-2D-mednist/client/validate.py
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ def validate(in_model_path, out_json_path, data_path=None, client_settings_path=

num_workers = client_settings["num_workers"]
batch_size = client_settings["batch_size"]
split_index = client_settings["split_index"]
split_index = os.environ.get("FEDN_DATA_SPLIT_INDEX") # client_settings["split_index"]

if data_path is None:
data_path = os.environ.get("FEDN_DATA_PATH")