2020-06-15 19:48:51 +00:00
|
|
|
# TRAINS - Example of manual model configuration and uploading
|
2019-06-10 17:00:28 +00:00
|
|
|
#
|
2019-10-06 18:57:50 +00:00
|
|
|
import os
|
|
|
|
from tempfile import gettempdir
|
|
|
|
|
2020-06-15 19:48:51 +00:00
|
|
|
from keras import Input, layers, Model
|
|
|
|
|
2019-06-10 17:00:28 +00:00
|
|
|
from trains import Task
|
|
|
|
|
|
|
|
|
2020-06-15 19:48:51 +00:00
|
|
|
task = Task.init(project_name='examples', task_name='Model configuration and upload')
|
|
|
|
|
|
|
|
|
|
|
|
def get_model():
|
|
|
|
# Create a simple model.
|
|
|
|
inputs = Input(shape=(32,))
|
|
|
|
outputs = layers.Dense(1)(inputs)
|
|
|
|
keras_model = Model(inputs, outputs)
|
|
|
|
keras_model.compile(optimizer='adam', loss='mean_squared_error')
|
|
|
|
return keras_model
|
|
|
|
|
2019-06-10 17:00:28 +00:00
|
|
|
|
|
|
|
# create a model
|
2020-06-15 19:48:51 +00:00
|
|
|
model = get_model()
|
2019-06-10 17:00:28 +00:00
|
|
|
|
2019-11-15 20:00:10 +00:00
|
|
|
# Connect a local configuration file
|
2020-06-15 19:48:51 +00:00
|
|
|
config_file = os.path.join('..', '..', 'reporting', 'data_samples', 'sample.json')
|
2019-11-15 20:00:10 +00:00
|
|
|
config_file = task.connect_configuration(config_file)
|
|
|
|
# then read configuration as usual, the backend will contain a copy of it.
|
|
|
|
# later when executing remotely, the returned `config_file` will be a temporary file
|
|
|
|
# containing a new copy of the configuration retrieved form the backend
|
|
|
|
# # model_config_dict = json.load(open(config_file, 'rt'))
|
|
|
|
|
|
|
|
# Or Store dictionary of definition for a specific network design
|
2019-06-10 17:00:28 +00:00
|
|
|
model_config_dict = {
|
|
|
|
'value': 13.37,
|
2019-11-15 20:00:10 +00:00
|
|
|
'dict': {'sub_value': 'string', 'sub_integer': 11},
|
2019-06-10 17:00:28 +00:00
|
|
|
'list_of_ints': [1, 2, 3, 4],
|
|
|
|
}
|
2019-11-15 20:00:10 +00:00
|
|
|
model_config_dict = task.connect_configuration(model_config_dict)
|
2019-06-10 17:00:28 +00:00
|
|
|
|
2019-11-15 20:00:10 +00:00
|
|
|
# We now update the dictionary after connecting it, and the changes will be tracked as well.
|
|
|
|
model_config_dict['new value'] = 10
|
|
|
|
model_config_dict['value'] *= model_config_dict['new value']
|
2019-06-10 17:00:28 +00:00
|
|
|
|
2019-11-15 20:00:10 +00:00
|
|
|
# store the label enumeration of the training model
|
|
|
|
labels = {'background': 0, 'cat': 1, 'dog': 2}
|
|
|
|
task.connect_label_enumeration(labels)
|
2019-06-10 17:00:28 +00:00
|
|
|
|
|
|
|
# storing the model, it will have the task network configuration and label enumeration
|
2019-11-15 20:00:10 +00:00
|
|
|
print('Any model stored from this point onwards, will contain both model_config and label_enumeration')
|
2019-10-06 18:57:50 +00:00
|
|
|
|
2020-06-15 19:48:51 +00:00
|
|
|
model.save(os.path.join(gettempdir(), "model"))
|
2019-06-10 17:00:28 +00:00
|
|
|
print('Model saved')
|