Skip to content

Commit

Permalink
Removed support for Tensorflow 1.x
Browse files Browse the repository at this point in the history
Min version is 2.3, for full compatibility with TFLite and Coral.
  • Loading branch information
feranick committed May 11, 2021
1 parent a4f2fb8 commit d8e511b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 76 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ This software requires Python (3.6 or higher). It has been tested with Python 3.
pydot
graphviz
h5py
tensorflow

tensorflow (>=2.3, not compatible with TF 1.x)

In addition, these packages may be needed depending on your platform (via ```apt-get``` in debian/ubuntu or ```port``` in OSX):

Expand Down
48 changes: 15 additions & 33 deletions SpectraKeras/SpectraKeras_CNN.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'''
**********************************************************
* SpectraKeras_CNN Classifier and Regressor
* 20201210a
* 20210510a
* Uses: TensorFlow
* By: Nicola Ferralis <[email protected]>
***********************************************************
Expand Down Expand Up @@ -214,34 +214,19 @@ def train(learnFile, testFile, flag):

from pkg_resources import parse_version
import tensorflow as tf

if parse_version(tf.version.VERSION) < parse_version('2.0.0'):
useTF2 = False
else:
useTF2 = True

if useTF2:
opts = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=1) # Tensorflow 2.0
conf = tf.compat.v1.ConfigProto(gpu_options=opts) # Tensorflow 2.0

gpus = tf.config.list_physical_devices('GPU')
if gpus:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
# if dP.setMaxMem:
# tf.config.set_virtual_device_configuration(
# gpus[0],
# [tf.config.VirtualDeviceConfiguration(memory_limit=dP.maxMem)])

else:
tf.compat.v1.enable_eager_execution()
opts = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=1)
conf = tf.compat.v1.ConfigProto(gpu_options=opts)
conf.gpu_options.allow_growth = True

tf.compat.v1.Session(config=conf)

import tensorflow.keras as keras

opts = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=1) # Tensorflow 2.0
conf = tf.compat.v1.ConfigProto(gpu_options=opts) # Tensorflow 2.0

gpus = tf.config.list_physical_devices('GPU')
if gpus:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
# if dP.setMaxMem:
# tf.config.set_virtual_device_configuration(
# gpus[0],
# [tf.config.VirtualDeviceConfiguration(memory_limit=dP.maxMem)])

learnFileRoot = os.path.splitext(learnFile)[0]
En, A, Cl = readLearnFile(learnFile, dP)
Expand Down Expand Up @@ -392,11 +377,8 @@ def train(learnFile, testFile, flag):
callbacks = tbLogs,
verbose=2,
validation_split=dP.cv_split)

if useTF2:
model.save(dP.model_name, save_format='h5')
else:
model.save(dP.model_name)

model.save(dP.model_name, save_format='h5')
keras.utils.plot_model(model, to_file=dP.model_png, show_shapes=True)
model.summary()

Expand Down
46 changes: 14 additions & 32 deletions SpectraKeras/SpectraKeras_MLP.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'''
**********************************************************
* SpectraKeras_MLP Classifier and Regressor
* 20201210a
* 20210510a
* Uses: TensorFlow
* By: Nicola Ferralis <[email protected]>
***********************************************************
Expand Down Expand Up @@ -191,33 +191,17 @@ def train(learnFile, testFile):
import tensorflow as tf
import tensorflow.keras as keras

if parse_version(tf.version.VERSION) < parse_version('2.0.0'):
useTF2 = False
else:
useTF2 = True

if useTF2:
opts = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=1) # Tensorflow 2.0
conf = tf.compat.v1.ConfigProto(gpu_options=opts) # Tensorflow 2.0
opts = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=1) # Tensorflow 2.0
conf = tf.compat.v1.ConfigProto(gpu_options=opts) # Tensorflow 2.0

gpus = tf.config.list_physical_devices('GPU')
if gpus:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
# if dP.setMaxMem:
# tf.config.set_virtual_device_configuration(
# gpus[0],
# [tf.config.VirtualDeviceConfiguration(memory_limit=dP.maxMem)])

else:
tf.compat.v1.enable_eager_execution()
opts = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=1)
conf = tf.compat.v1.ConfigProto(gpu_options=opts)
conf.gpu_options.allow_growth = True

tf.compat.v1.Session(config=conf)

import tensorflow.keras as keras
gpus = tf.config.list_physical_devices('GPU')
if gpus:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
# if dP.setMaxMem:
# tf.config.set_virtual_device_configuration(
# gpus[0],
# [tf.config.VirtualDeviceConfiguration(memory_limit=dP.maxMem)])

learnFileRoot = os.path.splitext(learnFile)[0]

Expand Down Expand Up @@ -332,11 +316,9 @@ def train(learnFile, testFile):
callbacks = tbLogs,
verbose=2,
validation_split=dP.cv_split)

if useTF2:
model.save(dP.model_name, save_format='h5')
else:
model.save(dP.model_name)

model.save(dP.model_name, save_format='h5')

keras.utils.plot_model(model, to_file=dP.model_png, show_shapes=True)
model.summary()

Expand Down
9 changes: 3 additions & 6 deletions SpectraKeras/libSpectraKeras.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'''
**********************************************************
* libSpectraKeas - Library for SpectraKeras
* 20201210a
* 20210510a
* Uses: TensorFlow
* By: Nicola Ferralis <[email protected]>
***********************************************************
Expand Down Expand Up @@ -144,11 +144,8 @@ def representative_dataset_gen():
for input_value in A.take(100):
yield[input_value]

try:
#converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model_file(dP.model_name) # TF2.0-2.2 (will be deprecated)
converter = tf.lite.TFLiteConverter.from_keras_model(model) # TF2.3 and higher only.
except:
converter = tf.lite.TFLiteConverter.from_keras_model_file(dP.model_name) # T1.x
#converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model_file(dP.model_name) # TF2.0-2.2 (will be deprecated)
converter = tf.lite.TFLiteConverter.from_keras_model(model) # TF2.3 and higher only.

#converter.optimizations = [tf.lite.Optimize.DEFAULT]
#converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_LATENCY]
Expand Down
2 changes: 1 addition & 1 deletion SpectraKeras/setup-gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
entry_points={'console_scripts' : ['SpectraKeras_MLP=SpectraKeras_MLP:SpectraKeras_MLP',
'SpectraKeras_CNN=SpectraKeras_CNN:SpectraKeras_CNN']},
py_modules=['SpectraKeras_MLP','SpectraKeras_CNN','libSpectraKeras'],
version='20201210a',
version='20210510a',
description='Machine learning for spectral data',
long_description= """ Machine learning for spectral data """,
author='Nicola Ferralis',
Expand Down
2 changes: 1 addition & 1 deletion SpectraKeras/setup-tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
entry_points={'console_scripts' : ['SpectraKeras_MLP=SpectraKeras_MLP:SpectraKeras_MLP',
'SpectraKeras_CNN=SpectraKeras_CNN:SpectraKeras_CNN']},
py_modules=['SpectraKeras_MLP','SpectraKeras_CNN','libSpectraKeras'],
version='20201210a',
version='20210510a',
description='Machine learning for spectral data',
long_description= """ Machine learning for spectral data """,
author='Nicola Ferralis',
Expand Down
2 changes: 1 addition & 1 deletion SpectraKeras/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
entry_points={'console_scripts' : ['SpectraKeras_MLP=SpectraKeras_MLP:SpectraKeras_MLP',
'SpectraKeras_CNN=SpectraKeras_CNN:SpectraKeras_CNN']},
py_modules=['SpectraKeras_MLP','SpectraKeras_CNN','libSpectraKeras'],
version='20201210a',
version='20210510a',
description='Machine learning for spectral data',
long_description= """ Machine learning for spectral data """,
author='Nicola Ferralis',
Expand Down

0 comments on commit d8e511b

Please sign in to comment.