SwissDataScienceCenter / mlschema-model-converters

Apache License 2.0
3 stars 0 forks source link

mls from tensorflow #8

Closed chrisbarber closed 3 years ago

chrisbarber commented 4 years ago

keras models have to_json:

{
  "class_name":"Sequential",
  "config":{
    "name":"sequential",
    "layers":[
      {
        "class_name":"Flatten",
        "config":{
          "name":"flatten",
          "trainable":true,
          "batch_input_shape":[
            null,
            28,
            28
          ],
          "dtype":"float32",
          "data_format":"channels_last"
        }
      },
      {
        "class_name":"Dense",
        "config":{
          "name":"dense",
          "trainable":true,
          "dtype":"float32",
          "units":128,
          "activation":"relu",
          "use_bias":true,
          "kernel_initializer":{
            "class_name":"GlorotUniform",
            "config":{
              "seed":null
            }
          },
          "bias_initializer":{
            "class_name":"Zeros",
            "config":{

            }
          },
          "kernel_regularizer":null,
          "bias_regularizer":null,
          "activity_regularizer":null,
          "kernel_constraint":null,
          "bias_constraint":null
        }
      },
      {
        "class_name":"Dropout",
        "config":{
          "name":"dropout",
          "trainable":true,
          "dtype":"float32",
          "rate":0.2,
          "noise_shape":null,
          "seed":null
        }
      },
      {
        "class_name":"Dense",
        "config":{
          "name":"dense_1",
          "trainable":true,
          "dtype":"float32",
          "units":10,
          "activation":"linear",
          "use_bias":true,
          "kernel_initializer":{
            "class_name":"GlorotUniform",
            "config":{
              "seed":null
            }
          },
          "bias_initializer":{
            "class_name":"Zeros",
            "config":{

            }
          },
          "kernel_regularizer":null,
          "bias_regularizer":null,
          "activity_regularizer":null,
          "kernel_constraint":null,
          "bias_constraint":null
        }
      }
    ],
    "build_input_shape":[
      null,
      28,
      28
    ]
  },
  "keras_version":"2.3.0-tf",
  "backend":"tensorflow"
}

test round trip:

>>> tf.keras.models.model_from_json(model.to_json()).to_json()==model.to_json()
True

after fit the json is the same however. the tensorflow.python.keras.callbacks.History object returned by model.fit has some stuff in it:

>>> history.params
{'verbose': 1, 'epochs': 5, 'steps': 1875}
>>> history.history
{'loss': [0.06762340664863586, 0.05985958129167557, 0.05271807685494423, 0.049675822257995605, 0.045876361429691315], 'accuracy': [0.9781000018119812, 0.9809499979019165, 0.9829833507537842, 0.9839166402816772, 0.9847999811172485]}
vigsterkr commented 4 years ago

ok the history is somewhat related to the #6 task so lets just have that in the backlog for a while. strictly speaking if we just look at keras, we could use the to_json() but that needs a transcriber into MLS format.

note there's a problem with that to_json(), as in for example it does not contain what's the optimizer you are using, nor the loss function:

import tensorflow
from tensorflow.keras.optimizers import RMSprop

inputs = tensorflow.keras.Input(shape=(3,))
x = tensorflow.keras.layers.Dense(4, activation=tensorflow.nn.relu)(inputs)
outputs = tensorflow.keras.layers.Dense(5, activation=tensorflow.nn.softmax)(x)
model = tensorflow.keras.Model(inputs=inputs, outputs=outputs)
optimizer = RMSprop(lr=0.01)
model.compile(optimizer=optimizer, loss='mae')

is a quite simple example. if you check model.to_json() it will only contain the layers but not the optimizer. one can have access to optimizer's config by model.optimizer.get_config(), but looking at keras' api i was trying to find a way where one could get all these infos (layers, optimizers etc) as one so we could just generate the MLS format using that.

chrisbarber commented 4 years ago

Took a quick look at what is inside the SavedModel file. It definitely has everything in it but it looks maybe too verbose; so I guess I lean towards just using model.optimizer.get_config(), unless a 3rd option becomes known..?

parsed contents of SavedModel .pb file

Just to clarify @vigsterkr , since I am not familiar, is keras fine as a target for this for now? It seems like raw tensorflow is maybe too low level to expect to be able to convert to mls.