Skip to content

Commit

Permalink
Simplified recognition of definition of callback metrics for TF 1x vs…
Browse files Browse the repository at this point in the history
… 2.x

This is now more robust.
  • Loading branch information
feranick committed Jan 21, 2020
1 parent cc315bd commit f162b1c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 28 deletions.
16 changes: 3 additions & 13 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
* 20200113a
* 20200121a
* Uses: TensorFlow
* By: Nicola Ferralis <[email protected]>
***********************************************************
Expand Down Expand Up @@ -189,10 +189,6 @@ def main():
#************************************
def train(learnFile, testFile, flag):
dP = Conf()

def_mae = 'mae'
def_val_mae = 'val_mae'

import tensorflow as tf
import tensorflow.keras as keras
from pkg_resources import parse_version
Expand All @@ -215,21 +211,13 @@ def train(learnFile, testFile, flag):
# gpus[0],
# [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=dP.maxMem)])

def_acc = 'accuracy'
def_val_acc = 'val_accuracy'

else:
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)

def_mae = 'mean_absolute_error'
def_val_mae = 'val_mean_absolute_error'
def_acc = 'acc'
def_val_acc = 'val_acc'

learnFileRoot = os.path.splitext(learnFile)[0]
En, A, Cl = readLearnFile(learnFile, dP)
if testFile != None:
Expand Down Expand Up @@ -409,6 +397,7 @@ def train(learnFile, testFile, flag):
val_loss = np.asarray(log.history['val_loss'])

if dP.regressor:
def_mae, def_val_mae = [list(log.history)[i] for i in (1,3)]
val_mae = np.asarray(log.history[def_val_mae])
printParam()
print('\n ==========================================================')
Expand All @@ -432,6 +421,7 @@ def train(learnFile, testFile, flag):
predictions[i][0], score[0], score[1]))
print('\n ==========================================================\n')
else:
def_acc, def_val_acc = [list(log.history)[i] for i in (1,3)]
accuracy = np.asarray(log.history[def_acc])
val_acc = np.asarray(log.history[def_val_acc])
print(" Number unique classes (training): ", np.unique(Cl).size)
Expand Down
16 changes: 3 additions & 13 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
* 20200113a
* 20200121a
* Uses: TensorFlow
* By: Nicola Ferralis <[email protected]>
***********************************************************
Expand Down Expand Up @@ -165,10 +165,6 @@ def main():
#************************************
def train(learnFile, testFile):
dP = Conf()

def_mae = 'mae'
def_val_mae = 'val_mae'

import tensorflow as tf
import tensorflow.keras as keras
from pkg_resources import parse_version
Expand All @@ -191,20 +187,12 @@ def train(learnFile, testFile):
# gpus[0],
# [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=dP.maxMem)])

def_acc = 'accuracy'
def_val_acc = 'val_accuracy'

else:
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)

def_mae = 'mean_absolute_error'
def_val_mae = 'val_mean_absolute_error'
def_acc = 'acc'
def_val_acc = 'val_acc'

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

Expand Down Expand Up @@ -345,6 +333,7 @@ def train(learnFile, testFile):
val_loss = np.asarray(log.history['val_loss'])

if dP.regressor:
def_mae, def_val_mae = [list(log.history)[i] for i in (1,3)]
val_mae = np.asarray(log.history[def_val_mae])
printParam()
print('\n ==========================================================')
Expand All @@ -367,6 +356,7 @@ def train(learnFile, testFile):
predictions[i][0], score[0], score[1]))
print('\n ==========================================================\n')
else:
def_acc, def_val_acc = [list(log.history)[i] for i in (1,3)]
accuracy = np.asarray(log.history[def_acc])
val_acc = np.asarray(log.history[def_val_acc])
print(" Number unique classes (training): ", np.unique(Cl).size)
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='20200113a',
version='20200121a',
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='20200113a',
version='20200121a',
description='Machine learning for spectral data',
long_description= """ Machine learning for spectral data """,
author='Nicola Ferralis',
Expand Down

0 comments on commit f162b1c

Please sign in to comment.