#
# "Freezes" the model and saves to a .pb file.
# A .hdf5 (also called .hd5 or .h5) file saves everything about the model,
# including its gradients, so that training can be resumed if desired.
# A .pb file contains only the weights (frozen) and is much smaller.
# Either file type can be used to make classifications, but the .pb format
# is preferred for importing into C (and other language) programs.
# see https://www.tensorflow.org/tutorials/keras/save_and_load
#

# TensorFlow, keras, numpy
import tensorflow as tf
from tensorflow import keras
from keras import backend as K
import numpy as np


# open file
f=open('simslope.txt','r')
d=[[int(x) for x in line.split()] for line in f]
f.close()

# copy to classes and data
classes=[]
data=[]

for a in range(0,len(d)):
  classes.append(d[a][0])
  row=[]
  for b in range(1,len(d[a])):
    row.append(d[a][b])
  data.append(row)

#print(classes)
#print(data)

# normalize each row of data
normdata=[]
for a in range(0,len(data)):
  norm=[]
  s=min(data[a])
  t=max(data[a])
  for b in range(0,len(data[a])):
    norm.append((float(data[a][b])-float(s))/(float(t)-float(s)))
  normdata.append(norm)

#print(normdata)

classes=np.array(classes)
data=np.array(normdata)

print("data has shape", data.shape)
print("classes has shape ", classes.shape)


# we must explicitly create a session to freeze the graph later

sess=tf.Session()
with sess:
  model = keras.Sequential([
  # cannot have InputLayer() or network will not save/load (bug in TF)
  # instead, specify input_shape in first real layer
  #    keras.layers.InputLayer(input_shape=(20,)),
      keras.layers.Dense(2, input_shape=(20,), activation='sigmoid')
      ])

  model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

  print("Training")
  metrics=model.fit(data,classes,epochs=100,verbose=2,validation_split=0.2)
  print("Max value: ", max(metrics.history['acc']), " at epoch",
	 np.argmax(metrics.history['acc']))

  print("Testing")
  test_loss, test_acc = model.evaluate(data, classes)
  print('Test accuracy:', test_acc)

  predictions = model.predict(data)
  print('Actual, prediction:')
  for a in range(0,len(classes)):
    print(classes[a],predictions[a])



##############################################################
# BELOW CODE TAKEN FROM WEB.  See: https://www.dlology.com/blog/how-to-convert-trained-keras-model-to-tensorflow-and-make-prediction/
##############################################################
  def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
    """
    Freezes the state of a session into a pruned computation graph.
    Creates a new computation graph where variable nodes are replaced by
    constants taking their current value in the session. The new graph will be
    pruned so subgraphs that are not necessary to compute the requested
    outputs are removed.
    @param session The TensorFlow session to be frozen.
    @param keep_var_names A list of variable names that should not be frozen,
                          or None to freeze all the variables in the graph.
    @param output_names Names of the relevant graph outputs.
    @param clear_devices Remove the device directives from the graph for better portability.
    @return The frozen graph definition.
    """
    graph = session.graph
    with graph.as_default():
        freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
        output_names = output_names or []
        output_names += [v.op.name for v in tf.global_variables()]
        input_graph_def = graph.as_graph_def()
        if clear_devices:
            for node in input_graph_def.node:
                node.device = ""
        frozen_graph = tf.graph_util.convert_variables_to_constants(
            session, input_graph_def, output_names, freeze_var_names)
        return frozen_graph

#############################################
# The following lines create the frozen model and save it to a file
#############################################

  frozen_graph=freeze_session(sess, 
			output_names=[out.op.name for out in model.outputs])
  tf.train.write_graph(frozen_graph,"./","simslope.pb",as_text=False)

# these are needed in the C program to identify input and output layers
  print('model inputs: ',model.inputs)
  print('model outputs: ',model.outputs)

# save an H5 version, can use it to cross-compare C program classifications
  tf.keras.models.save_model(model,'simslope.h5',True,True)


