-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Min version is 2.3, for full compatibility with TFLite and Coral.
- Loading branch information
Showing
7 changed files
with
36 additions
and
76 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
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
''' | ||
********************************************************** | ||
* SpectraKeras_CNN Classifier and Regressor | ||
* 20201210a | ||
* 20210510a | ||
* Uses: TensorFlow | ||
* By: Nicola Ferralis <[email protected]> | ||
*********************************************************** | ||
|
@@ -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) | ||
|
@@ -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() | ||
|
||
|
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
''' | ||
********************************************************** | ||
* SpectraKeras_MLP Classifier and Regressor | ||
* 20201210a | ||
* 20210510a | ||
* Uses: TensorFlow | ||
* By: Nicola Ferralis <[email protected]> | ||
*********************************************************** | ||
|
@@ -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] | ||
|
||
|
@@ -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() | ||
|
||
|
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
''' | ||
********************************************************** | ||
* libSpectraKeas - Library for SpectraKeras | ||
* 20201210a | ||
* 20210510a | ||
* Uses: TensorFlow | ||
* By: Nicola Ferralis <[email protected]> | ||
*********************************************************** | ||
|
@@ -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] | ||
|
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 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