-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Keras and TensorFlow Task Runner and related workspaces. (#1174)
* keras and tf updated Signed-off-by: yes <[email protected]> * skipped rebuild model for round 0 Signed-off-by: yes <[email protected]> * formatting issues fixed Signed-off-by: yes <[email protected]> * added keras in workflow Signed-off-by: yes <[email protected]> * pipeline tensorflowversion check Signed-off-by: yes <[email protected]> * revert changes Signed-off-by: yes <[email protected]> * revert changes Signed-off-by: yes <[email protected]> * removed extra line Signed-off-by: yes <[email protected]> * workspace changes Signed-off-by: yes <[email protected]> * removed python 3.8 for taskrunner workflow Signed-off-by: yes <[email protected]> * updated python version to 3.9 Signed-off-by: yes <[email protected]> * updated python version to 3.9 Signed-off-by: yes <[email protected]> * saved model name changes in tests Signed-off-by: yes <[email protected]> * code change to save model Signed-off-by: yes <[email protected]> * keras_nlp workspace changes Signed-off-by: yes <[email protected]> * keras and tf updated Signed-off-by: yes <[email protected]> * skipped rebuild model for round 0 Signed-off-by: yes <[email protected]> * formatting issues fixed Signed-off-by: yes <[email protected]> * added keras in workflow Signed-off-by: yes <[email protected]> * pipeline tensorflowversion check Signed-off-by: yes <[email protected]> * revert changes Signed-off-by: yes <[email protected]> * revert changes Signed-off-by: yes <[email protected]> * removed extra line Signed-off-by: yes <[email protected]> * workspace changes Signed-off-by: yes <[email protected]> * removed python 3.8 for taskrunner workflow Signed-off-by: yes <[email protected]> * updated python version to 3.9 Signed-off-by: yes <[email protected]> * updated python version to 3.9 Signed-off-by: yes <[email protected]> * saved model name changes in tests Signed-off-by: yes <[email protected]> * code change to save model Signed-off-by: yes <[email protected]> * keras_nlp workspace changes Signed-off-by: yes <[email protected]> * removed duplicate code Signed-off-by: yes <[email protected]> * removed oython 3.8 from taskrunnner e2e workflow Signed-off-by: yes <[email protected]> * code changes as per comments Signed-off-by: yes <[email protected]> * fix for formatting issue Signed-off-by: yes <[email protected]> * keras cnn with compression code changes Signed-off-by: yes <[email protected]> * remove version changes Signed-off-by: yes <[email protected]> * revert version changes Signed-off-by: yes <[email protected]> * keep keras runner for tensorflow workspaces Signed-off-by: yes <[email protected]> * formatting issue Signed-off-by: yes <[email protected]> * removed keras as ke Signed-off-by: yes <[email protected]> * comment changes Signed-off-by: yes <[email protected]> * code changes Signed-off-by: yes <[email protected]> * code changes Signed-off-by: yes <[email protected]> * code changes Signed-off-by: yes <[email protected]> * code changes Signed-off-by: yes <[email protected]> --------- Signed-off-by: yes <[email protected]>
- Loading branch information
Showing
76 changed files
with
373 additions
and
3,003 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
openfl-workspace/tf_2dunet/requirements.txt → ...l-workspace/keras_2dunet/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
keras==3.6.0 | ||
nibabel | ||
setuptools>=65.5.1 # not directly required, pinned by Snyk to avoid a vulnerability | ||
tensorflow==2.13 | ||
tensorflow==2.18.0 |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
# 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.""" | ||
|
||
import tensorflow as tf | ||
import keras | ||
|
||
from openfl.federated import KerasTaskRunner | ||
|
||
|
||
class Keras2DUNet(KerasTaskRunner): | ||
"""Initialize. | ||
Args: | ||
**kwargs: Additional parameters to pass to the function | ||
""" | ||
|
||
def __init__(self, **kwargs): | ||
"""Initialize. | ||
Args: | ||
**kwargs: Additional parameters to pass to the function | ||
""" | ||
super().__init__(**kwargs) | ||
|
||
self.model = self.build_model(self.data_loader.get_feature_shape(), use_upsampling=True, **kwargs) | ||
self.model.summary(print_fn=self.logger.info, line_length=120) | ||
self.initialize_tensorkeys_for_functions() | ||
|
||
|
||
def build_model(self, input_shape, | ||
use_upsampling=False, | ||
n_cl_out=1, | ||
dropout=0.2, | ||
activation_function='relu', | ||
seed=0xFEEDFACE, | ||
depth=5, | ||
dropout_at=None, | ||
initial_filters=32, | ||
batch_norm=True, | ||
**kwargs): | ||
"""Define the TensorFlow model. | ||
Args: | ||
input_shape: input shape of the model | ||
use_upsampling (bool): True = use bilinear interpolation; | ||
False = use transposed convolution (Default=False) | ||
n_cl_out (int): Number of channels in input layer (Default=1) | ||
dropout (float): Dropout percentage (Default=0.2)(Default = True) | ||
activation_function: The activation function to use after convolutional layers (Default='relu') | ||
seed: random seed (Default=0xFEEDFACE) | ||
depth (int): Number of max pooling layers in encoder (Default=5) | ||
dropout_at: Layers to perform dropout after (Default=[2,3]) | ||
initial_filters (int): Number of filters in first convolutional layer (Default=32) | ||
batch_norm (bool): True = use batch normalization (Default=True) | ||
**kwargs: Additional parameters to pass to the function | ||
""" | ||
if dropout_at is None: | ||
dropout_at = [2, 3] | ||
|
||
inputs = keras.layers.Input(shape=input_shape, name='Images') | ||
|
||
if activation_function == 'relu': | ||
activation = tf.nn.relu | ||
elif activation_function == 'leakyrelu': | ||
activation = tf.nn.leaky_relu | ||
|
||
params = { | ||
'activation': activation, | ||
'kernel_initializer': keras.initializers.he_uniform(seed=seed), | ||
'kernel_size': (3, 3), | ||
'padding': 'same', | ||
} | ||
|
||
convb_layers = {} | ||
|
||
net = inputs | ||
filters = initial_filters | ||
for i in range(depth): | ||
name = f'conv{i + 1}a' | ||
net = keras.layers.Conv2D(name=name, filters=filters, **params)(net) | ||
if i in dropout_at: | ||
net = keras.layers.Dropout(dropout)(net) | ||
name = f'conv{i + 1}b' | ||
net = keras.layers.Conv2D(name=name, filters=filters, **params)(net) | ||
if batch_norm: | ||
net = keras.layers.BatchNormalization()(net) | ||
convb_layers[name] = net | ||
# only pool if not last level | ||
if i != depth - 1: | ||
name = f'pool{i + 1}' | ||
net = keras.layers.MaxPooling2D(name=name, pool_size=(2, 2))(net) | ||
filters *= 2 | ||
|
||
# do the up levels | ||
filters //= 2 | ||
for i in range(depth - 1): | ||
if use_upsampling: | ||
up = keras.layers.UpSampling2D( | ||
name=f'up{depth + i + 1}', size=(2, 2))(net) | ||
else: | ||
up = keras.layers.Conv2DTranspose( | ||
name='transConv6', filters=filters, | ||
kernel_size=(2, 2), strides=(2, 2), padding='same')(net) | ||
net = keras.layers.concatenate( | ||
[up, convb_layers[f'conv{depth - i - 1}b']], | ||
axis=-1 | ||
) | ||
net = keras.layers.Conv2D( | ||
name=f'conv{depth + i + 1}a', | ||
filters=filters, **params)(net) | ||
net = keras.layers.Conv2D( | ||
name=f'conv{depth + i + 1}b', | ||
filters=filters, **params)(net) | ||
filters //= 2 | ||
net = keras.layers.Conv2D(name='Mask', filters=n_cl_out, | ||
kernel_size=(1, 1), | ||
activation='sigmoid')(net) | ||
model = keras.models.Model(inputs=[inputs], outputs=[net]) | ||
|
||
|
||
self.optimizer = keras.optimizers.RMSprop(1e-2) | ||
model.compile( | ||
loss=self.dice_coef_loss, | ||
optimizer=self.optimizer, | ||
metrics=["acc"] | ||
) | ||
|
||
return model | ||
|
||
def dice_coef_loss(self, y_true, y_pred, smooth=1.0): | ||
"""Dice coefficient loss. | ||
Calculate the -log(Dice Coefficient) loss | ||
Args: | ||
y_true: Ground truth annotation array | ||
y_pred: Prediction array from model | ||
smooth (float): Laplace smoothing factor (Default=1.0) | ||
Returns: | ||
float: -log(Dice cofficient) metric | ||
""" | ||
intersection = tf.reduce_sum(y_true * y_pred, axis=(1, 2, 3)) | ||
|
||
term1 = -tf.math.log(tf.constant(2.0) * intersection + smooth) | ||
term2 = tf.math.log(tf.reduce_sum(y_true, axis=(1, 2, 3)) | ||
+ tf.reduce_sum(y_pred, axis=(1, 2, 3)) + smooth) | ||
|
||
term1 = tf.reduce_mean(term1) | ||
term2 = tf.reduce_mean(term2) | ||
|
||
loss = term1 + term2 | ||
|
||
return loss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
numpy==1.23.5 | ||
tensorflow==2.13 | ||
keras==3.6.0 | ||
tensorflow==2.18.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.