From f7e0d674be06c193b0003778a01884bc663f7754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Breixo=20Soli=C3=B1o?= Date: Tue, 23 Mar 2021 18:39:22 +0100 Subject: [PATCH 1/7] Manually updated code to tf2 --- GettingStarted.ipynb | 8 ++++---- KerasWeightsProcessing/convert_weights.py | 12 ++++++------ KerasWeightsProcessing/examples/mnist_keras.py | 10 +++++----- .../examples/multi_output_model.py | 10 +++++----- KerasWeightsProcessing/examples/test_network.py | 10 +++++----- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/GettingStarted.ipynb b/GettingStarted.ipynb index 046741c..ab6b1b7 100644 --- a/GettingStarted.ipynb +++ b/GettingStarted.ipynb @@ -32,11 +32,11 @@ ], "source": [ "import numpy as np\n", - "from keras.layers import Dense\n", - "from keras.models import Sequential\n", + "from tensorflow.keras.layers import Dense\n", + "from tensorflow.keras.models import Sequential\n", "from IPython.display import SVG\n", - "from keras.utils import model_to_dot\n", - "from tensorflow import set_random_seed" + "from tensorflow.keras.utils import model_to_dot\n", + "from tensorflow.compat.v1.random import set_random_seed" ] }, { diff --git a/KerasWeightsProcessing/convert_weights.py b/KerasWeightsProcessing/convert_weights.py index 90568cd..055edd6 100644 --- a/KerasWeightsProcessing/convert_weights.py +++ b/KerasWeightsProcessing/convert_weights.py @@ -6,12 +6,12 @@ import numpy as np import math -import keras -import keras.backend as K -from keras.models import Sequential, Model -from keras.layers import Dense, Dropout, BatchNormalization -from keras.layers import Input, Activation -from keras import optimizers +from tensorflow import keras +import tensorflow.keras.backend as K +from tensorflow.keras.models import Sequential, Model +from tensorflow.keras.layers import Dense, Dropout, BatchNormalization +from tensorflow.keras.layers import Input, Activation +from tensorflow.keras import optimizers INPUT = ['input'] ACTIVATIONS = ['relu', 'linear', 'leakyrelu', 'sigmoid'] diff --git a/KerasWeightsProcessing/examples/mnist_keras.py b/KerasWeightsProcessing/examples/mnist_keras.py index 78b8fb7..73dd338 100644 --- a/KerasWeightsProcessing/examples/mnist_keras.py +++ b/KerasWeightsProcessing/examples/mnist_keras.py @@ -2,11 +2,11 @@ sys.path.append('../') from convert_weights import h5_to_txt -import keras -from keras.datasets import mnist -from keras.models import Sequential, Model -from keras.layers import Dense, Input -from keras.optimizers import RMSprop +import tensorflow.keras as keras +from tensorflow.keras.datasets import mnist +from tensorflow.keras.models import Sequential, Model +from tensorflow.keras.layers import Dense, Input +from tensorflow.keras.optimizers import RMSprop weights_file_name = 'mnist_example.h5' txt_file_name = weights_file_name.replace('h5', 'txt') diff --git a/KerasWeightsProcessing/examples/multi_output_model.py b/KerasWeightsProcessing/examples/multi_output_model.py index e8ee1ab..84cbf99 100644 --- a/KerasWeightsProcessing/examples/multi_output_model.py +++ b/KerasWeightsProcessing/examples/multi_output_model.py @@ -6,14 +6,14 @@ import tensorflow as tf ############## REPRODUCIBILITY ############ -tf.set_random_seed(0) +tf.compat.v1.random.set_random_seed(0) np.random.seed(0) ########################################### -from keras.models import load_model -from keras.models import Sequential, Model -from keras.utils.vis_utils import plot_model -from keras.layers import Dense, BatchNormalization, Input +from tensorflow.keras.models import load_model +from tensorflow.keras.models import Sequential, Model +from tensorflow.compat.v1.keras.utils import plot_model +from tensorflow.keras.layers import Dense, BatchNormalization, Input input = x = Input((5,)) for i in range(3): diff --git a/KerasWeightsProcessing/examples/test_network.py b/KerasWeightsProcessing/examples/test_network.py index 14b0ea8..17b93be 100644 --- a/KerasWeightsProcessing/examples/test_network.py +++ b/KerasWeightsProcessing/examples/test_network.py @@ -1,4 +1,4 @@ -import keras +import tensorflow.keras import argparse import numpy as np import subprocess @@ -12,11 +12,11 @@ # set random seeds for reproducibility np.random.seed(123) import tensorflow as tf -tf.set_random_seed(123) +tf.compat.v1.random.set_random_seed(123) -from keras.models import Sequential, Model -from keras.layers import Dense, Input, LeakyReLU, Dropout, BatchNormalization -from keras.models import load_model +from tensorflow.keras.models import Sequential, Model +from tensorflow.keras.layers import Dense, Input, LeakyReLU, Dropout, BatchNormalization +from tensorflow.keras.models import load_model parser = argparse.ArgumentParser() parser.add_argument('--train', action='store_true') From 97f57e429355da0a3a15218552ceac6545d18525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Breixo=20Soli=C3=B1o?= Date: Tue, 23 Mar 2021 18:56:44 +0100 Subject: [PATCH 2/7] Converted files to v2 automatically --- KerasWeightsProcessing/convert_weights.py | 2 +- KerasWeightsProcessing/examples/mnist_keras.py | 2 +- KerasWeightsProcessing/examples/multi_output_model.py | 4 ++-- KerasWeightsProcessing/examples/test_network.py | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/KerasWeightsProcessing/convert_weights.py b/KerasWeightsProcessing/convert_weights.py index 90568cd..29b8dcd 100644 --- a/KerasWeightsProcessing/convert_weights.py +++ b/KerasWeightsProcessing/convert_weights.py @@ -127,7 +127,7 @@ def txt_to_h5(weights_file_name, output_file_name=''): # if not specified will use path of weights_file with h5 extension output_file_name = weights_file_name.replace('.txt', '_converted.h5') - model.save(output_file_name) + model.save(output_file_name, save_format='h5') def h5_to_txt(weights_file_name, output_file_name=''): ''' diff --git a/KerasWeightsProcessing/examples/mnist_keras.py b/KerasWeightsProcessing/examples/mnist_keras.py index 78b8fb7..954c86b 100644 --- a/KerasWeightsProcessing/examples/mnist_keras.py +++ b/KerasWeightsProcessing/examples/mnist_keras.py @@ -50,6 +50,6 @@ verbose=1, validation_data=(x_test, y_test)) -model.save(weights_file_name) +model.save(weights_file_name, save_format='h5') h5_to_txt(weights_file_name, txt_file_name) diff --git a/KerasWeightsProcessing/examples/multi_output_model.py b/KerasWeightsProcessing/examples/multi_output_model.py index e8ee1ab..db6e398 100644 --- a/KerasWeightsProcessing/examples/multi_output_model.py +++ b/KerasWeightsProcessing/examples/multi_output_model.py @@ -6,7 +6,7 @@ import tensorflow as tf ############## REPRODUCIBILITY ############ -tf.set_random_seed(0) +tf.compat.v1.set_random_seed(0) np.random.seed(0) ########################################### @@ -34,7 +34,7 @@ metrics=['accuracy'] ) # SAVE TO FILE FOR PARSING -multi_output_model.save('multi_output_model.h5') +multi_output_model.save('multi_output_model.h5', save_format='h5') # CONVERT TO TXT convert_weights.h5_to_txt('multi_output_model.h5', 'single_output_model.txt') diff --git a/KerasWeightsProcessing/examples/test_network.py b/KerasWeightsProcessing/examples/test_network.py index 14b0ea8..1e202aa 100644 --- a/KerasWeightsProcessing/examples/test_network.py +++ b/KerasWeightsProcessing/examples/test_network.py @@ -12,7 +12,7 @@ # set random seeds for reproducibility np.random.seed(123) import tensorflow as tf -tf.set_random_seed(123) +tf.compat.v1.set_random_seed(123) from keras.models import Sequential, Model from keras.layers import Dense, Input, LeakyReLU, Dropout, BatchNormalization @@ -111,7 +111,7 @@ keras_predictions = model.predict(example_input)[0] # save the weights -model.save(weights_file) +model.save(weights_file, save_format='h5') # convert h5 file to txt h5_to_txt( weights_file_name=weights_file, From b077a597d1988819e10001d4c9a5edd5d8501a36 Mon Sep 17 00:00:00 2001 From: bsolino Date: Wed, 24 Mar 2021 10:25:44 +0100 Subject: [PATCH 3/7] Keeping lines as in original --- KerasWeightsProcessing/examples/multi_output_model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/KerasWeightsProcessing/examples/multi_output_model.py b/KerasWeightsProcessing/examples/multi_output_model.py index a506e51..85023b7 100644 --- a/KerasWeightsProcessing/examples/multi_output_model.py +++ b/KerasWeightsProcessing/examples/multi_output_model.py @@ -7,7 +7,6 @@ ############## REPRODUCIBILITY ############ tf.compat.v1.set_random_seed(0) - np.random.seed(0) ########################################### From 77f9ec80dc8a1a797647278a25a0e23aa7bd0ae7 Mon Sep 17 00:00:00 2001 From: bsolino Date: Wed, 24 Mar 2021 10:27:19 +0100 Subject: [PATCH 4/7] Keeping lines as in original --- KerasWeightsProcessing/examples/test_network.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/KerasWeightsProcessing/examples/test_network.py b/KerasWeightsProcessing/examples/test_network.py index 8c0879d..64b4c6b 100644 --- a/KerasWeightsProcessing/examples/test_network.py +++ b/KerasWeightsProcessing/examples/test_network.py @@ -12,10 +12,8 @@ # set random seeds for reproducibility np.random.seed(123) import tensorflow as tf - tf.compat.v1.set_random_seed(123) - from tensorflow.keras.models import Sequential, Model from tensorflow.keras.layers import Dense, Input, LeakyReLU, Dropout, BatchNormalization from tensorflow.keras.models import load_model From 19be5f5bd38364bdc60cb417e9bb844d589457d1 Mon Sep 17 00:00:00 2001 From: bsolino Date: Wed, 24 Mar 2021 10:28:11 +0100 Subject: [PATCH 5/7] Import keras maintaining its name --- KerasWeightsProcessing/examples/test_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/KerasWeightsProcessing/examples/test_network.py b/KerasWeightsProcessing/examples/test_network.py index 64b4c6b..ea24760 100644 --- a/KerasWeightsProcessing/examples/test_network.py +++ b/KerasWeightsProcessing/examples/test_network.py @@ -1,4 +1,4 @@ -import tensorflow.keras +import tensorflow.keras as keras import argparse import numpy as np import subprocess From 69a2c9ed2d2be559cef69f027b4cae1923c3b594 Mon Sep 17 00:00:00 2001 From: bsolino Date: Wed, 24 Mar 2021 10:29:48 +0100 Subject: [PATCH 6/7] Prettier way to import keras --- KerasWeightsProcessing/examples/mnist_keras.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/KerasWeightsProcessing/examples/mnist_keras.py b/KerasWeightsProcessing/examples/mnist_keras.py index 123ba46..d954b76 100644 --- a/KerasWeightsProcessing/examples/mnist_keras.py +++ b/KerasWeightsProcessing/examples/mnist_keras.py @@ -2,7 +2,7 @@ sys.path.append('../') from convert_weights import h5_to_txt -import tensorflow.keras as keras +from tensorflow import keras from tensorflow.keras.datasets import mnist from tensorflow.keras.models import Sequential, Model from tensorflow.keras.layers import Dense, Input From 7b0a4395f822b0d48bd88bdc9d15dc0ec1f79e30 Mon Sep 17 00:00:00 2001 From: bsolino Date: Wed, 24 Mar 2021 10:30:27 +0100 Subject: [PATCH 7/7] Prettier way to import keras --- KerasWeightsProcessing/examples/test_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/KerasWeightsProcessing/examples/test_network.py b/KerasWeightsProcessing/examples/test_network.py index ea24760..7840dc9 100644 --- a/KerasWeightsProcessing/examples/test_network.py +++ b/KerasWeightsProcessing/examples/test_network.py @@ -1,4 +1,4 @@ -import tensorflow.keras as keras +from tensorflow import keras import argparse import numpy as np import subprocess