Skip to content

Commit

Permalink
Import keras before tf.keras (#95)
Browse files Browse the repository at this point in the history
* Import keras before tf.keras

Assume that a dedicated keras install is there for a reason

* Flake8 fix
  • Loading branch information
WardLT committed Aug 27, 2020
1 parent e1bd8f4 commit d12ea04
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
6 changes: 3 additions & 3 deletions dlhub_sdk/models/servables/keras.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
try:
# Attempt to use Tensorflow Keras first
from tensorflow import keras
except ImportError:
# Attempt to use regular keras first, as we figure it's installed for a reason
import keras
except ImportError:
from tensorflow import keras


from dlhub_sdk.models.servables.python import BasePythonServableModel
Expand Down
25 changes: 14 additions & 11 deletions dlhub_sdk/models/servables/tests/test_keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import shutil
import os

from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Input
try:
import keras
except ImportError:
from tensorflow import keras
from unittest import TestCase

from dlhub_sdk.models.servables.keras import KerasModel
Expand All @@ -15,9 +17,9 @@


def _make_simple_model():
model = Sequential()
model.add(Dense(16, input_shape=(1,), activation='relu', name='hidden'))
model.add(Dense(1, name='output'))
model = keras.models.Sequential()
model.add(keras.layers.Dense(16, input_shape=(1,), activation='relu', name='hidden'))
model.add(keras.layers.Dense(1, name='output'))
model.compile(optimizer='rmsprop', loss='mse')
return model

Expand Down Expand Up @@ -49,11 +51,11 @@ def test_keras_single_input(self):

def test_keras_multioutput(self):
# Make a Keras model
input_layer = Input(shape=(4,))
dense = Dense(16, activation='relu')(input_layer)
output_1 = Dense(1, activation='relu')(dense)
output_2 = Dense(2, activation='softmax')(dense)
model = Model([input_layer], [output_1, output_2])
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
Expand Down Expand Up @@ -95,7 +97,8 @@ def test_custom_layers(self):
model.save(model_path)

# Create the metadata
metadata = KerasModel.create_model(model_path, ['y'], custom_objects={'Dense': Dense})
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
Expand Down

0 comments on commit d12ea04

Please sign in to comment.