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

Update TensorFlow Task Runner and related workspaces #985

Open
wants to merge 26 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1112cec
create tf_cnn_mnist and update plan and src to match new taskrunner f…
kta-intel May 13, 2024
d092367
updates to tf-keras taskrunner
kta-intel May 14, 2024
c7452e7
update runner_tf.py to run on keras api with non-legacy optimizers. c…
kta-intel Jun 6, 2024
dd53e71
tf workspace update
kta-intel Jun 6, 2024
6c6f1dc
update unet tensorflow workspaces
kta-intel Jun 6, 2024
b3dd2fb
find_loader deprecation notice. changing to importlib.util.find_spec()
kta-intel Jun 6, 2024
228af82
Merge branch 'securefederatedai:develop' into TensorflowTaskRunner_up…
kta-intel Jun 6, 2024
2018e1f
minor fix to find_spec()
kta-intel Jun 6, 2024
a74d45c
cleaning up tf import lines
kta-intel Jun 6, 2024
e4dde55
Merge branch 'securefederatedai:develop' into TensorflowTaskRunner_up…
kta-intel Jun 7, 2024
bea8729
Fix lint with flake8
kta-intel Jun 7, 2024
05014d2
remove calls for legacy optimizer
kta-intel Jun 7, 2024
9003e81
fix lint - trailing whitespaces
kta-intel Jun 7, 2024
44474f7
Merge branch 'securefederatedai:develop' into TensorflowTaskRunner_up…
kta-intel Jul 9, 2024
3dc7170
update tf_2dunet workspace to use tensorflow2 aVnd remove legacy tens…
kta-intel Jul 11, 2024
9671ab1
update data path
kta-intel Jul 11, 2024
a78b836
pin tensorflow=2.15.1
kta-intel Jul 11, 2024
e9a7364
use absolute paths
kta-intel Jul 11, 2024
49e97c1
tf_3dunet_brats updated to new convention
kta-intel Jul 11, 2024
c17b84b
remove redundant naming
kta-intel Jul 11, 2024
cf05cad
remove op_parallelism_threads print statement
kta-intel Jul 11, 2024
4b085ec
remove extraneous oneshot function
kta-intel Jul 11, 2024
565df12
removing extraneous text
kta-intel Jul 11, 2024
a34c0ce
Revert "tf_3dunet_brats updated to new convention"
kta-intel Jul 12, 2024
6b6d628
update tf_3dunet_brats to newer convention and add general plan
kta-intel Jul 12, 2024
dad006f
update docstrings
kta-intel Jul 12, 2024
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
4 changes: 2 additions & 2 deletions openfl-workspace/tf_2dunet/src/tf_2dunet.py
kta-intel marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

import tensorflow.compat.v1 as tf

from openfl.federated import TensorFlowTaskRunner
from openfl.federated import TensorFlowTaskRunner_v1

tf.disable_v2_behavior()


class TensorFlow2DUNet(TensorFlowTaskRunner):
class TensorFlow2DUNet(TensorFlowTaskRunner_v1):
"""Initialize.

Args:
Expand Down
6 changes: 3 additions & 3 deletions openfl-workspace/tf_3dunet_brats/plan/plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ task_runner:
template: src.tf_3dunet_model.TensorFlow3dUNet
tasks:
aggregated_model_validation:
function: validate
function: validate_task
kwargs:
apply: global
batch_size: 4
Expand All @@ -65,7 +65,7 @@ tasks:
- soft_dice_coef
defaults: plan/defaults/tasks_tensorflow.yaml
locally_tuned_model_validation:
function: validate
function: validate_task
kwargs:
apply: local
batch_size: 4
Expand All @@ -74,7 +74,7 @@ tasks:
- soft_dice_coef
settings: {}
train:
function: train
function: train_task
kwargs:
batch_size: 4
epochs: 1
Expand Down
4 changes: 2 additions & 2 deletions openfl-workspace/tf_3dunet_brats/src/tf_3dunet_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

import tensorflow as tf

from openfl.federated import KerasTaskRunner
from openfl.federated import TensorFlowTaskRunner
from .define_model import build_model
kta-intel marked this conversation as resolved.
Show resolved Hide resolved
from .define_model import dice_coef
from .define_model import dice_loss
from .define_model import soft_dice_coef


class TensorFlow3dUNet(KerasTaskRunner):
class TensorFlow3dUNet(TensorFlowTaskRunner):
"""Initialize.

Args:
Expand Down
18 changes: 9 additions & 9 deletions openfl-workspace/tf_cnn_histology/plan/plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ aggregator :
defaults : plan/defaults/aggregator.yaml
template : openfl.component.Aggregator
settings :
init_state_path : save/tf_cnn_histology_init.pbuf
last_state_path : save/tf_cnn_histology_latest.pbuf
best_state_path : save/tf_cnn_histology_best.pbuf
db_store_rounds: 2
init_state_path : save/tf_cnn_histology_init.pbuf
last_state_path : save/tf_cnn_histology_latest.pbuf
best_state_path : save/tf_cnn_histology_best.pbuf
db_store_rounds : 2
rounds_to_train : 10

collaborator :
Expand All @@ -21,7 +21,7 @@ collaborator :

data_loader :
defaults : plan/defaults/data_loader.yaml
template : src.tfhistology_inmemory.TensorFlowHistologyInMemory
template : src.dataloader.TensorFlowHistologyInMemory
settings :
batch_size: 64
percent_train: 0.8
Expand All @@ -30,7 +30,7 @@ data_loader :

task_runner :
defaults : plan/defaults/task_runner.yaml
template : src.tf_cnn.TensorFlowCNN
template : src.taskrunner.TensorFlowCNN

network :
defaults : plan/defaults/network.yaml
Expand All @@ -41,22 +41,22 @@ assigner :
tasks:
defaults: plan/defaults/tasks_tensorflow.yaml
aggregated_model_validation:
function: validate
function: validate_task
kwargs:
apply: global
batch_size: 32
metrics:
- sparse_categorical_accuracy
locally_tuned_model_validation:
function: validate
function: validate_task
kwargs:
apply: local
batch_size: 32
metrics:
- sparse_categorical_accuracy
settings: {}
train:
function: train
function: train_task
kwargs:
batch_size: 32
epochs: 1
Expand Down
2 changes: 1 addition & 1 deletion openfl-workspace/tf_cnn_histology/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pillow
tensorflow==2.13
tensorflow==2.15.1
tensorflow-datasets
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,40 @@

"""You may copy this file as the starting point of your own model."""

from openfl.federated import TensorFlowDataLoader
from logging import getLogger

import numpy as np
import tensorflow_datasets as tfds

logger = getLogger(__name__)

class TensorFlowHistologyInMemory(TensorFlowDataLoader):
kta-intel marked this conversation as resolved.
Show resolved Hide resolved
"""TensorFlow Data Loader for Colorectal Histology Dataset."""

def __init__(self, data_path, batch_size, **kwargs):
"""
Initialize.

Args:
data_path: File path for the dataset
batch_size (int): The batch size for the data loader
**kwargs: Additional arguments, passed to super init and load_mnist_shard
"""
super().__init__(batch_size, **kwargs)

_, num_classes, X_train, y_train, X_valid, y_valid = load_histology_shard(
shard_num=data_path,
categorical=False, **kwargs
)

self.X_train = X_train
self.y_train = y_train
self.X_valid = X_valid
self.y_valid = y_valid

self.num_classes = num_classes


def one_hot(labels, classes):
kta-intel marked this conversation as resolved.
Show resolved Hide resolved
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

"""You may copy this file as the starting point of your own model."""

import numpy as np
import tensorflow as tf

from openfl.federated import KerasTaskRunner
from openfl.utilities import Metric
from openfl.federated import TensorFlowTaskRunner


class TensorFlowCNN(KerasTaskRunner):
class TensorFlowCNN(TensorFlowTaskRunner):
kta-intel marked this conversation as resolved.
Show resolved Hide resolved
"""Initialize.

Args:
Expand All @@ -17,40 +19,47 @@ class TensorFlowCNN(KerasTaskRunner):
"""

def __init__(self, **kwargs):
"""Initialize.
"""
kta-intel marked this conversation as resolved.
Show resolved Hide resolved
Initialize.

Args:
**kwargs: Additional parameters to pass to the function
kta-intel marked this conversation as resolved.
Show resolved Hide resolved

"""
super().__init__(**kwargs)

self.model = self.create_model(
self.model = self.build_model(
self.feature_shape,
self.data_loader.num_classes,
**kwargs
)
self.initialize_tensorkeys_for_functions()

def create_model(self,
input_shape,
num_classes,
training_smoothing=32.0,
validation_smoothing=1.0,
**kwargs):
"""Create the TensorFlow CNN Histology model.
self.model.summary(print_fn=self.logger.info)

self.logger.info(f'Train Set Size : {self.get_train_data_size()}')
self.logger.info(f'Valid Set Size : {self.get_valid_data_size()}')

def build_model(self,
input_shape,
kta-intel marked this conversation as resolved.
Show resolved Hide resolved
num_classes,
**kwargs):
"""
Define the model architecture.

Args:
training_smoothing (float): (Default=32.0)
validation_smoothing (float): (Default=1.0)
input_shape (numpy.ndarray): The shape of the data
kta-intel marked this conversation as resolved.
Show resolved Hide resolved
num_classes (int): The number of classes of the dataset
**kwargs: Additional parameters to pass to the function

Returns:
keras.src.engine.functional.Functional

"""
print(tf.config.threading.get_intra_op_parallelism_threads())
print(tf.config.threading.get_inter_op_parallelism_threads())
kta-intel marked this conversation as resolved.
Show resolved Hide resolved
# ## Define Model
#
# Convolutional neural network model

## Define Model using Functional API

inputs = tf.keras.layers.Input(shape=input_shape)
conv = tf.keras.layers.Conv2D(
Expand Down Expand Up @@ -96,13 +105,39 @@ def create_model(self,
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
)

self.tvars = model.layers
print(f'layer names: {[var.name for var in self.tvars]}')
return model

self.opt_vars = self.optimizer.variables()
print(f'optimizer vars: {self.opt_vars}')

# Two opt_vars for one tvar: gradient and square sum for RMSprop.
self.fl_vars = self.tvars + self.opt_vars
def train_(self, batch_generator, metrics: list = None, **kwargs):
Copy link
Collaborator

@teoparvanov teoparvanov Jul 18, 2024

Choose a reason for hiding this comment

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

In this particular case, could we consider not even overriding _train()? It looks like in this instance, it practically does the same as the base method (seemingly differing in terms of validity checks)?

"""Train single epoch.

return model
Override this function for custom training.

Args:
batch_generator: Generator of training batches.
Each batch is a tuple of N train images and N train labels
where N is the batch size of the DataLoader of the current TaskRunner instance.

epochs: Number of epochs to train.
metrics: Names of metrics to save.
"""
if metrics is None:
metrics = []

model_metrics_names = self.model.metrics_names

for param in metrics:
if param not in model_metrics_names:
raise ValueError(
f'TensorFlowTaskRunner does not support specifying new metrics. '
f'Param_metrics = {metrics}, model_metrics_names = {model_metrics_names}'
)

history = self.model.fit(batch_generator,
verbose=1,
**kwargs)
results = []
for metric in metrics:
value = np.mean([history.history[metric]])
results.append(Metric(name=metric, value=np.array(value)))
Comment on lines +122 to +124
Copy link
Collaborator

@teoparvanov teoparvanov Jul 18, 2024

Choose a reason for hiding this comment

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

Out of curiosity, why do we take the mean value, as opposed to the latest one?

If we consider accuracy for instance, wouldn't we be interested in the accuracy at the end of the training (which would certainly be higher than the mean value)?

return results
34 changes: 0 additions & 34 deletions openfl-workspace/tf_cnn_histology/src/tfhistology_inmemory.py

This file was deleted.

2 changes: 2 additions & 0 deletions openfl-workspace/tf_cnn_mnist/.workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
current_plan_name: default

5 changes: 5 additions & 0 deletions openfl-workspace/tf_cnn_mnist/plan/cols.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (C) 2020-2021 Intel Corporation
# Licensed subject to the terms of the separately executed evaluation license agreement between Intel Corporation and you.

collaborators:

7 changes: 7 additions & 0 deletions openfl-workspace/tf_cnn_mnist/plan/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (C) 2020-2021 Intel Corporation
# Licensed subject to the terms of the separately executed evaluation license agreement between Intel Corporation and you.

# collaborator_name,data_directory_path
one,1
kta-intel marked this conversation as resolved.
Show resolved Hide resolved


2 changes: 2 additions & 0 deletions openfl-workspace/tf_cnn_mnist/plan/defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
../../workspace/plan/defaults

42 changes: 42 additions & 0 deletions openfl-workspace/tf_cnn_mnist/plan/plan.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (C) 2020-2024 Intel Corporation
# Licensed subject to the terms of the separately executed evaluation license agreement between Intel Corporation and you.

aggregator :
defaults : plan/defaults/aggregator.yaml
template : openfl.component.Aggregator
settings :
init_state_path : save/tf_cnn_mnist_init.pbuf
best_state_path : save/tf_cnn_mnist_best.pbuf
last_state_path : save/tf_cnn_mnist_last.pbuf
kta-intel marked this conversation as resolved.
Show resolved Hide resolved
rounds_to_train : 10

collaborator :
defaults : plan/defaults/collaborator.yaml
template : openfl.component.Collaborator
settings :
delta_updates : false
opt_treatment : RESET

data_loader :
defaults : plan/defaults/data_loader.yaml
template : src.dataloader.TensorFlowMNISTInMemory
settings :
collaborator_count : 2
data_group_name : mnist
batch_size : 256

task_runner :
defaults : plan/defaults/task_runner.yaml
template : src.taskrunner.TensorFlowCNN

network :
defaults : plan/defaults/network.yaml

assigner :
defaults : plan/defaults/assigner.yaml

tasks :
defaults : plan/defaults/tasks_tensorflow.yaml

compression_pipeline :
defaults : plan/defaults/compression_pipeline.yaml
1 change: 1 addition & 0 deletions openfl-workspace/tf_cnn_mnist/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tensorflow==2.15.1
3 changes: 3 additions & 0 deletions openfl-workspace/tf_cnn_mnist/src/__init__.py
kta-intel marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright (C) 2020-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
"""You may copy this file as the starting point of your own model."""
Loading
Loading