Skip to content

Commit 07ffbd8

Browse files
Tensorflow Version update to 2.19 (#597)
* .h5 to .keras and many more updates! * python 3.11 and 3.12 --------- Co-authored-by: rxrathod <rrathod@broadinstitute.org>
1 parent 56f3a5c commit 07ffbd8

35 files changed

+2606
-2125
lines changed

.github/workflows/python-package.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
runs-on: ubuntu-latest
2222
strategy:
2323
matrix:
24-
python-version: ["3.8", "3.9"]
24+
python-version: [3.11, 3.12]
2525

2626
steps:
2727
- uses: actions/checkout@v2
@@ -35,7 +35,6 @@ jobs:
3535
python -m pip install --upgrade pip
3636
# Install the ml4h Python package.
3737
pip install .
38-
pip install -r docker/vm_boot_images/config/tensorflow-requirements.txt
39-
- name: Test with pytest
38+
- name: Test with pytest and pytest-xdist
4039
run: |
41-
pytest tests -m "not slow"
40+
pytest tests -m "not slow" -n auto

docker/vm_boot_images/Dockerfile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ WORKDIR /app
2020
# to minimize full recompilation where possible.
2121

2222
# Basic setup
23+
#RUN rm /etc/apt/sources.list.d/cuda.list
24+
##RUN rm /etc/apt/sources.list.d/nvidia-ml.list
25+
#RUN apt-key del 7fa2af80
26+
#RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/3bf863cc.pub
27+
#RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/7fa2af80.pub
2328
RUN ./ubuntu.sh
2429

2530
# Point any MLflow tracking hooks at the main MLflow instance on Cloud Run
@@ -32,7 +37,10 @@ ENV MLFLOW_TRACKING_URI='https://mlflow-783282864357.us-central1.run.app'
3237
# RUN pip3 uninstall -y fastai
3338
# RUN ./fastai.sh
3439

35-
RUN apt-get install python3-tk libgl1-mesa-glx libxt-dev -y
40+
RUN apt-get update
41+
RUN apt-get upgrade -y
42+
RUN apt-get install python3 python3-pip python3-tk libgl1-mesa-glx libxt-dev -y
43+
RUN apt-get install -y wget unzip curl python3-pydot graphviz git ffmpeg
3644

3745
# Requirements for the tensorflow project
3846
RUN pip3 install --upgrade pip

docker/vm_boot_images/build.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ CPU_ONLY="false"
1919
PUSH_TO_GCR="false"
2020
PUSH_TO_LATEST="false"
2121

22-
BASE_IMAGE_GPU="tensorflow/tensorflow:2.9.1-gpu"
23-
BASE_IMAGE_CPU="tensorflow/tensorflow:2.9.1"
22+
BASE_IMAGE_GPU="tensorflow/tensorflow:2.19.0-gpu"
23+
BASE_IMAGE_CPU="tensorflow/tensorflow:2.19.0"
2424

25-
LATEST_TAG_GPU="tf2.9-latest-gpu"
26-
LATEST_TAG_CPU="tf2.9-latest-cpu"
25+
LATEST_TAG_GPU="tf2.19-latest-gpu"
26+
LATEST_TAG_CPU="tf2.19-latest-cpu"
2727

2828
SCRIPT_NAME=$( echo $0 | sed 's#.*/##g' )
2929

docker/vm_boot_images/config/tensorflow-requirements.txt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
pydot
22
nibabel==4.0.2
33
pydicom==1.2.2
4-
hyperopt==0.1.2
54
seaborn
65
scikit-image
76
peakutils
87
biosppy
9-
vtk==9.2.6
10-
imageio==2.6.1
8+
imageio
119
ipywidgets>=7.5.1
1210
bokeh
1311
pillow
1412
notebook
1513
pytest
14+
pytest-xdist
1615
pysam
17-
tensorflow==2.9.1
18-
tensorflow-addons==0.17.1
16+
tensorflow==2.19.0
1917
tensorflow_hub
20-
tensorflow_probability==0.17.0
18+
tensorflow_probability
2119
tensorflow-text
2220
tf-models-official
2321
keras-tuner
@@ -35,7 +33,7 @@ plotnine
3533
vega
3634
ipycanvas>=0.7.0
3735
ipyannotations>=0.2.1
38-
torch==1.12.1
36+
torch==2.2.2
3937
opencv-python
4038
blosc
4139
boto3
@@ -47,4 +45,3 @@ voxelmorph
4745
pystrum
4846
av
4947
lmdb
50-
mlflow

docker/vm_boot_images/config/ubuntu.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@
22

33
# Other necessities
44
apt-get update
5+
56
echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections
6-
apt-get install -y wget unzip curl python3-pydot python3-pydot-ng graphviz ttf-mscorefonts-installer git pip ffmpeg hdf5-tools
7+
8+
apt-get install -y wget unzip curl python3-pydot python3-pydot-ng graphviz ttf-mscorefonts-installer git pip ffmpeg
9+
10+
wget https://developer.download.nvidia.com/compute/cudnn/9.8.0/local_installers/cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb
11+
dpkg -i cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb
12+
cp /var/cudnn-local-repo-ubuntu2204-9.8.0/cudnn-local-8138232B-keyring.gpg /usr/share/keyrings/
13+
apt-get update
14+
apt-get -y install cudnn

ml4h/arguments.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
from ml4h.TensorMap import TensorMap, TimeSeriesOrder
2929
from ml4h.defines import IMPUTATION_RANDOM, IMPUTATION_MEAN
3030
from ml4h.tensormap.mgb.dynamic import make_mgb_dynamic_tensor_maps
31+
from ml4h.tensormap.tensor_map_maker import generate_categorical_tensor_map_from_file, \
32+
generate_latent_tensor_map_from_file
33+
3134
from ml4h.models.legacy_models import parent_sort, check_no_bottleneck
3235
from ml4h.tensormap.tensor_map_maker import make_test_tensor_maps, generate_random_pixel_as_text_tensor_maps
3336
from ml4h.models.legacy_models import NORMALIZATION_CLASSES, CONV_REGULARIZATION_CLASSES, DENSE_REGULARIZATION_CLASSES
@@ -292,7 +295,7 @@ def parse_args():
292295
help='Maximum number of models for the hyper-parameter optimizer to evaluate before returning.',
293296
)
294297
parser.add_argument('--balance_csvs', default=[], nargs='*', help='Balances batches with representation from sample IDs in this list of CSVs')
295-
parser.add_argument('--optimizer', default='radam', type=str, help='Optimizer for model training')
298+
parser.add_argument('--optimizer', default='adam', type=str, help='Optimizer for model training')
296299
parser.add_argument('--learning_rate_schedule', default=None, type=str, choices=['triangular', 'triangular2', 'cosine_decay'], help='Adjusts learning rate during training.')
297300
parser.add_argument('--anneal_rate', default=0., type=float, help='Annealing rate in epochs of loss terms during training')
298301
parser.add_argument('--anneal_shift', default=0., type=float, help='Annealing offset in epochs of loss terms during training')
@@ -430,6 +433,10 @@ def parse_args():
430433
# TensorMap prefix for convenience
431434
parser.add_argument('--tensormap_prefix', default="ml4h.tensormap", type=str, help="Module prefix path for TensorMaps. Defaults to \"ml4h.tensormap\"")
432435

436+
#Parent Sort enable or disable
437+
parser.add_argument('--parent_sort', default=True, type=lambda x: x.lower() == 'true', help='disable or enable parent_sort on output tmaps')
438+
#Dictionary outputs
439+
parser.add_argument('--named_outputs', default=False, type=lambda x: x.lower() == 'true', help='pass output tmaps as dictionaries if true else pass as list')
433440
args = parser.parse_args()
434441
_process_args(args)
435442
return args
@@ -538,7 +545,7 @@ def _process_args(args):
538545

539546
if args.latent_input_file is not None:
540547
args.tensor_maps_in.append(
541-
generate_latent_tensor_map_from_file(args.latent_input_file, args.input_tensors.pop(0)),
548+
generate_latent_tensor_map_from_file(args.latent_input_file, args.input_tensors.pop(0))
542549
)
543550
args.tensor_maps_in.extend([tensormap_lookup(it, args.tensormap_prefix) for it in args.input_tensors])
544551

@@ -564,11 +571,13 @@ def _process_args(args):
564571
args.output_tensors.pop(0),
565572
),
566573
)
574+
567575
if len(args.latent_output_files) > 0:
568576
for lof in args.latent_output_files:
569577
args.tensor_maps_out.append(
570578
generate_latent_tensor_map_from_file(lof, args.output_tensors.pop(0)),
571579
)
580+
572581
args.tensor_maps_out.extend([tensormap_lookup(ot, args.tensormap_prefix) for ot in args.output_tensors])
573582
args.tensor_maps_out = parent_sort(args.tensor_maps_out)
574583
args.tensor_maps_protected = [tensormap_lookup(it, args.tensormap_prefix) for it in args.protected_tensors]

ml4h/defines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __str__(self):
2828
IMAGE_EXT = '.png'
2929
PDF_EXT = '.pdf'
3030
TENSOR_EXT = '.hd5'
31-
MODEL_EXT = '.h5'
31+
MODEL_EXT = '.keras'
3232
XML_EXT = '.xml'
3333

3434
STOP_CHAR = '!'

ml4h/metrics.py

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88

99
from sklearn.metrics import roc_curve, auc, average_precision_score
1010

11-
1211
from tensorflow.keras.losses import binary_crossentropy, categorical_crossentropy, sparse_categorical_crossentropy
13-
from tensorflow.keras.losses import logcosh, cosine_similarity, mean_squared_error, mean_absolute_error, mean_absolute_percentage_error
12+
from tensorflow.keras.losses import LogCosh, CosineSimilarity, MSE, MAE, MAPE, Dice
13+
from keras.saving import register_keras_serializable
1414

15-
from neurite.tf.losses import Dice
1615

1716
STRING_METRICS = [
1817
'categorical_crossentropy','binary_crossentropy','mean_absolute_error','mae',
19-
'mean_squared_error', 'mse', 'cosine_similarity', 'logcosh', 'sparse_categorical_crossentropy',
18+
'mean_squared_error', 'mse', 'cosine_similarity', 'log_cosh', 'sparse_categorical_crossentropy',
2019
]
2120

2221

@@ -48,6 +47,7 @@ def weighted_crossentropy(weights, name='anonymous'):
4847
string_fxn += '\treturn loss\n'
4948
exec(string_fxn, globals(), locals())
5049
loss_fxn = eval(name + fxn_postfix, globals(), locals())
50+
loss_fxn = register_keras_serializable()(loss_fxn)
5151
return loss_fxn
5252

5353

@@ -109,39 +109,39 @@ def paired_angle_between_batches(tensors):
109109

110110
def ignore_zeros_l2(y_true, y_pred):
111111
mask = K.cast(K.not_equal(y_true, 0), K.floatx())
112-
return mean_squared_error(y_true * mask, y_pred * mask)
112+
return MSE(y_true * mask, y_pred * mask)
113113

114114

115115
def ignore_zeros_logcosh(y_true, y_pred):
116116
mask = K.cast(K.not_equal(y_true, 0), K.floatx())
117-
return logcosh(y_true * mask, y_pred * mask)
117+
return LogCosh(y_true * mask, y_pred * mask)
118118

119119

120120
def sentinel_logcosh_loss(sentinel: float):
121121
def ignore_sentinel_logcosh(y_true, y_pred):
122122
mask = K.cast(K.not_equal(y_true, sentinel), K.floatx())
123-
return logcosh(y_true * mask, y_pred * mask)
123+
return LogCosh(y_true * mask, y_pred * mask)
124124
return ignore_sentinel_logcosh
125125

126126

127127
def y_true_times_mse(y_true, y_pred):
128-
return K.maximum(y_true, 1.0)*mean_squared_error(y_true, y_pred)
128+
return K.maximum(y_true, 1.0)*MSE(y_true, y_pred)
129129

130130

131131
def mse_10x(y_true, y_pred):
132-
return 10.0*mean_squared_error(y_true, y_pred)
132+
return 10.0*MSE(y_true, y_pred)
133133

134134

135135
def y_true_squared_times_mse(y_true, y_pred):
136-
return K.maximum(1.0+y_true, 1.0)*K.maximum(1.0+y_true, 1.0)*mean_squared_error(y_true, y_pred)
136+
return K.maximum(1.0+y_true, 1.0)*K.maximum(1.0+y_true, 1.0)*MSE(y_true, y_pred)
137137

138138

139139
def y_true_cubed_times_mse(y_true, y_pred):
140-
return K.maximum(y_true, 1.0)*K.maximum(y_true, 1.0)*K.maximum(y_true, 1.0)*mean_squared_error(y_true, y_pred)
140+
return K.maximum(y_true, 1.0)*K.maximum(y_true, 1.0)*K.maximum(y_true, 1.0)*MSE(y_true, y_pred)
141141

142142

143143
def y_true_squared_times_logcosh(y_true, y_pred):
144-
return K.maximum(1.0+y_true, 1.0)*K.maximum(1.0+y_true, 1.0)*logcosh(y_true, y_pred)
144+
return K.maximum(1.0+y_true, 1.0)*K.maximum(1.0+y_true, 1.0)*LogCosh(y_true, y_pred)
145145

146146

147147
def two_batch_euclidean(tensors):
@@ -265,6 +265,7 @@ def loss(y_true, y_pred):
265265
return loss
266266

267267
def dice(y_true, y_pred):
268+
return Dice()(y_true, y_pred)
268269
return Dice(laplace_smoothing=1e-05).mean_loss(y_true, y_pred)
269270

270271
def per_class_dice(labels):
@@ -273,12 +274,13 @@ def per_class_dice(labels):
273274
label_idx = labels[label_key]
274275
fxn_name = label_key.replace('-', '_').replace(' ', '_')
275276
string_fxn = 'def ' + fxn_name + '_dice(y_true, y_pred):\n'
276-
string_fxn += '\tdice = Dice(laplace_smoothing=1e-05).dice(y_true, y_pred)\n'
277-
string_fxn += '\tdice = K.mean(dice, axis=0)['+str(label_idx)+']\n'
277+
string_fxn += '\tdice = tf.keras.losses.Dice()(y_true, y_pred)\n'
278+
#string_fxn += '\tdice = K.mean(dice, axis=0)['+str(label_idx)+']\n'
278279
string_fxn += '\treturn dice'
279280

280281
exec(string_fxn)
281282
dice_fxn = eval(fxn_name + '_dice')
283+
dice_fxn = register_keras_serializable()(dice_fxn)
282284
dice_fxns.append(dice_fxn)
283285

284286
return dice_fxns
@@ -299,6 +301,7 @@ def per_class_recall(labels):
299301

300302
exec(string_fxn)
301303
recall_fxn = eval(fxn_name + '_recall')
304+
recall_fxn = register_keras_serializable()(recall_fxn)
302305
recall_fxns.append(recall_fxn)
303306

304307
return recall_fxns
@@ -317,6 +320,7 @@ def per_class_precision(labels):
317320

318321
exec(string_fxn)
319322
precision_fxn = eval(fxn_name + '_precision')
323+
precision_fxn = register_keras_serializable()(precision_fxn)
320324
precision_fxns.append(precision_fxn)
321325

322326
return precision_fxns
@@ -335,6 +339,7 @@ def per_class_recall_3d(labels):
335339

336340
exec(string_fxn)
337341
recall_fxn = eval(fxn_prefix + '_recall')
342+
recall_fxn = register_keras_serializable()(recall_fxn)
338343
recall_fxns.append(recall_fxn)
339344

340345
return recall_fxns
@@ -353,6 +358,7 @@ def per_class_precision_3d(labels):
353358

354359
exec(string_fxn)
355360
precision_fxn = eval(fxn_prefix + '_precision')
361+
precision_fxn = register_keras_serializable()(precision_fxn)
356362
precision_fxns.append(precision_fxn)
357363

358364
return precision_fxns
@@ -371,6 +377,7 @@ def per_class_recall_4d(labels):
371377

372378
exec(string_fxn)
373379
recall_fxn = eval(fxn_prefix + '_recall')
380+
recall_fxn = register_keras_serializable()(recall_fxn)
374381
recall_fxns.append(recall_fxn)
375382

376383
return recall_fxns
@@ -389,6 +396,8 @@ def per_class_precision_4d(labels):
389396

390397
exec(string_fxn)
391398
precision_fxn = eval(fxn_prefix + '_precision')
399+
precision_fxn = register_keras_serializable()(precision_fxn)
400+
392401
precision_fxns.append(precision_fxn)
393402

394403
return precision_fxns
@@ -407,6 +416,7 @@ def per_class_recall_5d(labels):
407416

408417
exec(string_fxn)
409418
recall_fxn = eval(fxn_prefix + '_recall')
419+
recall_fxn = register_keras_serializable()(recall_fxn)
410420
recall_fxns.append(recall_fxn)
411421

412422
return recall_fxns
@@ -425,6 +435,7 @@ def per_class_precision_5d(labels):
425435

426436
exec(string_fxn)
427437
precision_fxn = eval(fxn_prefix + '_precision')
438+
precision_fxn = register_keras_serializable()(precision_fxn)
428439
precision_fxns.append(precision_fxn)
429440

430441
return precision_fxns
@@ -449,15 +460,15 @@ def get_metric_dict(output_tensor_maps):
449460
elif tm.loss == 'binary_crossentropy':
450461
losses.append(binary_crossentropy)
451462
elif tm.loss == 'mean_absolute_error' or tm.loss == 'mae':
452-
losses.append(mean_absolute_error)
463+
losses.append(MSE)
453464
elif tm.loss == 'mean_squared_error' or tm.loss == 'mse':
454-
losses.append(mean_squared_error)
465+
losses.append(MSE)
455466
elif tm.loss == 'cosine_similarity':
456-
losses.append(cosine_similarity)
457-
elif tm.loss == 'logcosh':
458-
losses.append(logcosh)
467+
losses.append(CosineSimilarity)
468+
elif tm.loss == 'log_cosh':
469+
losses.append(LogCosh)
459470
elif tm.loss == 'mape':
460-
losses.append(mean_absolute_percentage_error)
471+
losses.append(MAPE)
461472
elif hasattr(tm.loss, '__name__'):
462473
metrics[tm.loss.__name__] = tm.loss
463474
losses.append(tm.loss)
@@ -857,4 +868,12 @@ def result(self):
857868
def reset_state(self):
858869
# Reset the metric state variables
859870
self.total_ssim.assign(0.0)
860-
self.count.assign(0.0)
871+
self.count.assign(0.0)
872+
873+
874+
def _register_all(module_globals):
875+
for name, obj in module_globals.items():
876+
if callable(obj) and not name.startswith("_"):
877+
module_globals[name] = register_keras_serializable()(obj)
878+
879+
_register_all(globals())

0 commit comments

Comments
 (0)