Skip to content

Update to Tensorflow 2 #13

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions GettingStarted.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down
14 changes: 7 additions & 7 deletions KerasWeightsProcessing/convert_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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=''):
'''
Expand Down
12 changes: 6 additions & 6 deletions KerasWeightsProcessing/examples/mnist_keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 tensorflow.keras.optimizers import RMSprop

weights_file_name = 'mnist_example.h5'
txt_file_name = weights_file_name.replace('h5', 'txt')
Expand Down Expand Up @@ -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)
12 changes: 6 additions & 6 deletions KerasWeightsProcessing/examples/multi_output_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import tensorflow as tf

############## REPRODUCIBILITY ############
tf.set_random_seed(0)
tf.compat.v1.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):
Expand All @@ -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')
Expand Down
12 changes: 6 additions & 6 deletions KerasWeightsProcessing/examples/test_network.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import keras
from tensorflow import keras
import argparse
import numpy as np
import subprocess
Expand All @@ -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.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')
Expand Down Expand Up @@ -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,
Expand Down