def test_multi_file(self): """Test adding the architecture in a different file """ # Make a simple model model = _make_simple_model() tmpdir = mkdtemp() try: # Save it model_path = os.path.join(tmpdir, 'model.hd5') model.save(model_path, include_optimizer=False) model_json = os.path.join(tmpdir, 'model.json') with open(model_json, 'w') as fp: print(model.to_json(), file=fp) model_yaml = os.path.join(tmpdir, 'model.yml') with open(model_yaml, 'w') as fp: print(model.to_yaml(), file=fp) weights_path = os.path.join(tmpdir, 'weights.hd5') model.save_weights(weights_path) # Create the metadata metadata = KerasModel.create_model(weights_path, ['y'], arch_path=model_path) # Make sure both files are included in the files list self.assertEqual(metadata['dlhub']['files'], { 'arch': model_path, 'model': weights_path }) # Try it with the JSON and YAML versions KerasModel.create_model(weights_path, ['y'], arch_path=model_json) KerasModel.create_model(weights_path, ['y'], arch_path=model_yaml) finally: shutil.rmtree(tmpdir)
def test_custom_layers(self): """Test adding custom layers to the definition""" # Make a simple model model = _make_simple_model() tmpdir = mkdtemp() try: # Save it model_path = os.path.join(tmpdir, 'model.hd5') model.save(model_path) # Create the metadata metadata = KerasModel.create_model( model_path, ['y'], custom_objects={'Dense': keras.layers.Dense}) metadata.set_title('test').set_name('test') # Make sure it has the custom object definitions self.assertIn('Dense', metadata['servable']['options']['custom_objects']) # Validate it against DLHub schema validate_against_dlhub_schema(metadata.to_dict(), 'servable') finally: shutil.rmtree(tmpdir) # Test the errors with self.assertRaises(ValueError) as exc: metadata.add_custom_object('BadLayer', float) self.assertIn('subclass', str(exc.exception))
def test_keras_multifile(tmpdir): """Test loading a shim when model is saved as a separate arch and weights file""" # Make a Keras model model = _make_model() # Save it model_path = os.path.join(tmpdir, 'model.hd5') model.save(model_path, include_optimizer=False) model_json = os.path.join(tmpdir, 'model.json') with open(model_json, 'w') as fp: print(model.to_json(), file=fp) weights_path = os.path.join(tmpdir, 'weights.hd5') model.save_weights(weights_path) # Create the metadata metadata = KerasModel.create_model(weights_path, ['y'], arch_path=model_path) metadata.set_title('Keras Test') metadata.set_name('mlp') # Make the servable servable = KerasServable(**metadata.to_dict()) x = [[1]] assert model.predict(np.array(x))[0] == servable.run(x)[0] # Test with the JSON and YAML metadata = KerasModel.create_model(weights_path, ['y'], arch_path=model_json) metadata.set_title('Keras Test').set_name('mlp') servable = KerasServable(**metadata.to_dict()) assert model.predict(np.array(x))[0] == servable.run(x)[0] # Try it with YAML in earlier versions keras_major_version = tuple(int(x) for x in keras.__version__.split(".")[:2]) if keras_major_version < (2, 6): model_yaml = os.path.join(tmpdir, 'model.yml') with open(model_yaml, 'w') as fp: print(model.to_yaml(), file=fp) metadata = KerasModel.create_model(weights_path, ['y'], arch_path=model_yaml) metadata.set_title('Keras Test').set_name('mlp') servable = KerasServable(**metadata.to_dict()) assert model.predict(np.array(x))[0] == servable.run(x)[0]
def test_keras(tmpdir): # Make a Keras model model = _make_model() model_path = os.path.join(tmpdir, 'model.hd5') model.save(model_path) # Make the model metadata = KerasModel.create_model(model_path, ["y"]) metadata.set_title('Keras Test') metadata.set_name('mlp') # Make the servable servable = KerasServable(**metadata.to_dict()) x = [[1]] assert model.predict(np.array(x))[0] == servable.run(x)[0]
def test_keras_multioutput(self): # Make a Keras model input_layer = keras.layers.Input(shape=(4, )) dense = keras.layers.Dense(16, activation='relu')(input_layer) output_1 = keras.layers.Dense(1, activation='relu')(dense) output_2 = keras.layers.Dense(2, activation='softmax')(dense) model = keras.models.Model([input_layer], [output_1, output_2]) model.compile(optimizer='rmsprop', loss='mse') # Save it to disk tempdir = mkdtemp() try: model_path = os.path.join(tempdir, 'model.hd5') model.save(model_path) # Create a model metadata = KerasModel.create_model(model_path, [['y'], ['yes', 'no']]) metadata.set_title('Keras Test') metadata.set_name('mlp') self.assertEqual( metadata['servable']['methods']['run']['output'], { 'type': 'tuple', 'description': 'Tuple of tensors', 'element_types': [{ 'type': 'ndarray', 'description': 'Tensor', 'shape': [None, 1] }, { 'type': 'ndarray', 'description': 'Tensor', 'shape': [None, 2] }] }) output = metadata.to_dict() # Validate against schema validate_against_dlhub_schema(output, 'servable') finally: shutil.rmtree(tempdir)
def test_keras_single_input(self): # Make a Keras model model = _make_simple_model() # Save it to disk tempdir = mkdtemp() try: model_path = os.path.join(tempdir, 'model.hd5') model.save(model_path) # Create a model metadata = KerasModel.create_model(model_path, ["y"]) metadata.set_title('Keras Test') metadata.set_name('mlp') # Validate against schema output = metadata.to_dict() validate_against_dlhub_schema(output, 'servable') finally: shutil.rmtree(tempdir)
def test_custom_layers(tmpdir): """Test adding custom layers to the definition""" # Make a simple model model = _make_model() # Save it model_path = os.path.join(tmpdir, 'model.hd5') model.save(model_path) # Create the metadata metadata = KerasModel.create_model(model_path, ['y'], custom_objects={'Dense': keras.layers.Dense}) metadata.set_title('Keras Test') metadata.set_name('mlp') # Make the servable servable = KerasServable(**metadata.to_dict()) x = [[1]] assert model.predict(np.array(x))[0] == servable.run(x)[0]
def test_keras_multioutput(tmpdir): # Make a Keras model input_1 = keras.layers.Input(shape=(1,)) dense = keras.layers.Dense(16, activation='relu')(input_1) output_1 = keras.layers.Dense(1, activation='linear')(dense) output_2 = keras.layers.Dense(1, activation='linear')(dense) model = keras.models.Model(inputs=input_1, outputs=[output_1, output_2]) model.compile(optimizer='rmsprop', loss='mse') model_path = os.path.join(tmpdir, 'model.hd5') model.save(model_path) # Make the model metadata = KerasModel.create_model(model_path, ["y"]) metadata.set_title('Keras Test') metadata.set_name('mlp') # Make the servable servable = KerasServable(**metadata.to_dict()) x = [[1]] servable.run(x) assert np.isclose(model.predict(np.array(x)), servable.run(x)[0]).all()
def test_keras_multiinput(tmpdir): # Make a Keras model input_1 = keras.layers.Input(shape=(1,)) input_2 = keras.layers.Input(shape=(1,)) inputs = keras.layers.Concatenate()([input_1, input_2]) dense = keras.layers.Dense(16, activation='relu')(inputs) output = keras.layers.Dense(1, activation='linear')(dense) model = keras.models.Model(inputs=[input_1, input_2], outputs=output) model.compile(optimizer='rmsprop', loss='mse') model_path = os.path.join(tmpdir, 'model.hd5') model.save(model_path) # Make the model metadata = KerasModel.create_model(model_path, ["y"]) metadata.set_title('Keras Test') metadata.set_name('mlp') # Make the servable servable = KerasServable(**metadata.to_dict()) x = [[1]] servable.run([x, x]) assert model.predict([np.array(x)]*2)[0] == servable.run([x, x])[0]
from dlhub_sdk.models.servables.python import PythonClassMethodModel, PythonStaticMethodModel from dlhub_sdk.utils.schemas import validate_against_dlhub_schema from dlhub_sdk.utils.types import compose_argument_block from dlhub_sdk.models.servables.keras import KerasModel import json import os # Describe the deep learning model model = KerasModel.create_model( os.path.join('Deep-SMILES', 'CYP1A2_conv1_sequential_best.hdf5'), ['Yes', 'No']) # Describe the inputs and outputs model.input[ 'description'] = 'Encoding of the characters at each point in a string, padded by zeros' model.output['description'] = 'Binary classification of molecule' # Add provenance information model.set_authors(["Zhu, Mengyuan"], ["Georgia State University"]) model.set_title("Classification Model for AMDET Properties") model.set_name("deep-smiles_model") model.set_abstract( "A deep learning model that predicts AMDET properties given a SMILES string of a molecule." ) model.add_alternate_identifier("https://github.com/MengyuanZhu/Deep-SMILES", "URL") # Add requirements model.add_requirement('tensorflow', 'detect') model.add_requirement('keras', 'detect')
from dlhub_sdk.utils.schemas import validate_against_dlhub_schema from dlhub_sdk.models.servables.keras import KerasModel import json # Describe the keras model model = KerasModel.create_model('model.h5', list(map(str, range(10)))) # Describe the model model.set_title("MNIST Digit Classifier") model.set_name("mnist") model.add_alternate_identifier( "https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py", "URL") model.set_domains(["digit recognition"]) # Describe the outputs in more detail model.output['description'] = 'Probabilities of being 0-9' model.input['description'] = 'Image of a digit' # Print out the result # Sanity Check: Make sure it fits the schema metadata = model.to_dict() print(json.dumps(metadata, indent=2)) validate_against_dlhub_schema(metadata, 'servable') with open('model_metadata.json', 'w') as fp: json.dump(metadata, fp, indent=2)
from dlhub_sdk.models.servables.python import PythonClassMethodModel from dlhub_sdk.utils.schemas import validate_against_dlhub_schema from dlhub_sdk.utils.types import compose_argument_block from dlhub_sdk.models.servables.keras import KerasModel import json import os cifar_classes = [ "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" ] # Describe the deep learning model model = KerasModel.create_model(os.path.join('models', 'cifar10vgg.h5'), cifar_classes) # Describe the inputs and outputs model.set_inputs('list', 'List of images. Each image must be standardized by the mean' ' and standard deviation of the training set', item_type=compose_argument_block('ndarray', 'Image', shape=[32, 32, 3])) model.set_outputs('ndarray', 'Probabilities of being in each of the cifar classes', shape=[None, 10]) # Add provenance information model.set_authors(["Geifman, Yonatan"], ["Technion"]) model.set_title("Keras Model for Cifar10 based on VGGNet") model.set_name("cifar10_model")
from dlhub_sdk.models.servables.keras import KerasModel import pickle as pkl import json # Describe the keras model model_info = KerasModel.create_model('model.hd5', list(map(str, range(10)))) # Describe the model model_info.set_title("MNIST Digit Classifier") model_info.set_name("mnist_tiny_example") model_info.set_domains(["general", "digit recognition"]) # Describe the outputs in more detail model_info['servable']['methods']['run']['output'][ 'description'] = 'Probabilities of being 0-9' model_info['servable']['methods']['run']['input'][ 'description'] = 'Image of a digit' # Print out the result print('\n--> Model Information <--') print(json.dumps(model_info.to_dict(), indent=2)) # Save the model information to pickle with open('model_info.pkl', 'wb') as fp: pkl.dump(model_info, fp)
def test_keras_single_input(self): # Make a Keras model model = _make_simple_model() # Save it to disk tempdir = mkdtemp() try: model_path = os.path.join(tempdir, 'model.hd5') model.save(model_path) # Create a model metadata = KerasModel.create_model(model_path, ["y"]) metadata.set_title('Keras Test') metadata.set_name('mlp') output = metadata.to_dict() self.assertEqual( output, { "datacite": { "creators": [], "titles": [{ "title": "Keras Test" }], "publisher": "DLHub", "publicationYear": _year, "identifier": { "identifier": "10.YET/UNASSIGNED", "identifierType": "DOI" }, "resourceType": { "resourceTypeGeneral": "InteractiveResource" }, "descriptions": [], "fundingReferences": [], "relatedIdentifiers": [], "alternateIdentifiers": [], "rightsList": [] }, "dlhub": { "version": __version__, "domains": [], "visible_to": ["public"], 'type': 'servable', "name": "mlp", "files": { "model": model_path }, "dependencies": { "python": { 'keras': keras_version, 'h5py': h5py_version, 'tensorflow': tf_version } } }, "servable": { "methods": { "run": { "input": { "type": "ndarray", "description": "Tensor", "shape": [None, 1] }, "output": { "type": "ndarray", "description": "Tensor", "shape": [None, 1] }, "parameters": {}, "method_details": { "method_name": "predict", "classes": ["y"] } } }, "type": "Keras Model", "shim": "keras.KerasServable", "model_type": "Deep NN", "model_summary": """_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= hidden (Dense) (None, 16) 32 _________________________________________________________________ output (Dense) (None, 1) 17 ================================================================= Total params: 49 Trainable params: 49 Non-trainable params: 0 _________________________________________________________________ """ } }) # noqa: W291 (trailing whitespace needed for text match) # Validate against schema validate_against_dlhub_schema(output, 'servable') finally: shutil.rmtree(tempdir)