def test_session_push_sklearn(): '''Tests basic model pushing functionality with sklearn''' clear_jwt() with _patch_auth(): with MockServer() as server: iris = load_iris() X = iris.data y = iris.target clf = RandomForestClassifier(random_state=0) clf.fit(X, y) columns = [ 'sepallength', 'sepalwidth', 'petallength', 'petalwidth' ] X_df = pd.DataFrame(X, columns=columns) DataFrame = create_dataframe('DataFrame', X_df) Predictions = create_namedtuple('Predictions', [('predictions', List[int])]) def predict(df: DataFrame) -> Predictions: '''Predicts the class of iris''' X = np.column_stack(df) yhat = clf.predict(X) preds = Predictions(predictions=yhat) return preds model = Model(predict=predict) model_url, auth_url, _, _ = server.config s = AcumosSession(model_url, auth_url) s.push(model, name='sklearn_iris_push')
def test_wrapped_sklearn(): '''Tests model wrap and load functionality''' iris = load_iris() X = iris.data y = iris.target clf = RandomForestClassifier(random_state=0) clf.fit(X, y) yhat = clf.predict(X) columns = ['sepallength', 'sepalwidth', 'petallength', 'petalwidth'] X_df = pd.DataFrame(X, columns=columns) IrisDataFrame = create_dataframe('IrisDataFrame', X_df) def f1(data: IrisDataFrame) -> List[int]: '''Creates a numpy ndarray and predicts''' X = np.column_stack(data) return clf.predict(X) def f2(data: IrisDataFrame) -> List[int]: '''Creates a pandas DataFrame and predicts''' X = np.column_stack(data) df = pd.DataFrame(X, columns=columns) return clf.predict(df.values) in_ = tuple(col for col in X.T) out = (yhat, ) for func in (f1, f2): _generic_test(func, in_, out, wrapped_eq=lambda a, b: (a[0] == b[0]).all())
def test_session_push_keras(): '''Tests basic model pushing functionality with keras''' clear_jwt() with _patch_auth(): with MockServer() as server: iris = load_iris() X = iris.data y = pd.get_dummies(iris.target).values clf = Sequential() clf.add(Dense(3, input_dim=4, activation='relu')) clf.add(Dense(3, activation='softmax')) clf.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) clf.fit(X, y) columns = [ 'sepallength', 'sepalwidth', 'petallength', 'petalwidth' ] X_df = pd.DataFrame(X, columns=columns) DataFrame = create_dataframe('DataFrame', X_df) Predictions = create_namedtuple('Predictions', [('predictions', List[int])]) def predict(df: DataFrame) -> Predictions: '''Predicts the class of iris''' X = np.column_stack(df) yhat = clf.predict(X) preds = Predictions(predictions=yhat) return preds model = Model(predict=predict) model_url, auth_url, _, _ = server.config s = AcumosSession(model_url, auth_url) s.push(model, name='keras_iris_push')
def test_model2proto(): '''Tests the generation of protobuf messages from a Model''' T1 = NamedTuple('T1', [('x', int), ('y', int)]) T2 = NamedTuple('T2', [('data', int)]) Thing = Enum('Thing', 'a b c d e') def f1(x: int, y: int) -> int: return x + y def f2(data: T1) -> T2: return T2(data.x + data.y) def f3(data: List[Thing]) -> Thing: return data[0] def f4(data: List[T1]) -> None: pass def f5(x: List[np.int32]) -> np.int32: return np.sum(x) df = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]}) TestDataFrame = create_dataframe('TestDataFrame', df) def f6(in_: TestDataFrame) -> None: pass model = Model(f1=f1, f2=f2, f3=f3, f4=f4, f5=f5, f6=f6) module = 'model' package = 'pkg' protostr = model2proto(model, package) # main test is to make sure that compilation doesn't fail with tempfile.TemporaryDirectory() as tdir: compile_protostr(protostr, package, module, tdir)
y: target_onehot }) print("Epoch {} | Loss {}".format(epoch, loss)) prediction = tf.argmax(logits, 1) yhat = sess.run([prediction], {x: data})[0] # note: this predicts on the training set for illustration purposes only print(classification_report(target, yhat)) # ============================================================================= # create a acumos model from the tensorflow model # ============================================================================= X_df = pd.DataFrame( data, columns=['sepal_length', 'sepal_width', 'petal_length', 'petal_width']) IrisDataFrame = create_dataframe('IrisDataFrame', X_df) def classify_iris(df: IrisDataFrame) -> List[int]: '''Returns an array of iris classifications''' X = np.column_stack(df) return prediction.eval({x: X}, sess) model = Model(classify=classify_iris) session = AcumosSession() session.dump(model, 'model', '.') # creates ./model
from acumos.session import AcumosSession if __name__ == '__main__': '''Main''' iris = load_iris() X = iris.data y = iris.target clf = RandomForestClassifier(random_state=0) clf.fit(X, y) columns = ['sepallength', 'sepalwidth', 'petallength', 'petalwidth'] X_df = pd.DataFrame(X, columns=columns) DataFrame = create_dataframe('DataFrame', X_df) Predictions = create_namedtuple('Predictions', [('predictions', List[int])]) def predict(df: DataFrame) -> Predictions: '''Predicts the class of iris''' X = np.column_stack(df) yhat = clf.predict(X) preds = Predictions(predictions=yhat) return preds model = Model(transform=predict) s = AcumosSession(None) s.dump(model, 'model', '.')
def test_wrapped_tensorflow(): '''Tests model wrap and load functionality''' tf.set_random_seed(0) iris = load_iris() data = iris.data target = iris.target target_onehot = pd.get_dummies(target).values.astype(float) # ============================================================================= # test with explicit session # ============================================================================= tf.reset_default_graph() session = tf.Session() x, y, prediction = _build_tf_model(session, data, target_onehot) yhat = session.run([prediction], {x: data})[0] X_df = pd.DataFrame( data, columns=['sepal_length', 'sepal_width', 'petal_length', 'petal_width']) IrisDataFrame = create_dataframe('IrisDataFrame', X_df) def f1(df: IrisDataFrame) -> List[int]: '''Tests with explicit session provided''' X = np.column_stack(df) return prediction.eval({x: X}, session) in_ = tuple(col for col in data.T) out = (yhat, ) _generic_test(f1, in_, out, wrapped_eq=lambda a, b: (a[0] == b[0]).all(), preload=tf.reset_default_graph) # ============================================================================= # test with implicit default session # ============================================================================= tf.reset_default_graph() session = tf.InteractiveSession() x, y, prediction = _build_tf_model(session, data, target_onehot) yhat = session.run([prediction], {x: data})[0] def f2(df: IrisDataFrame) -> List[int]: '''Tests with implicit default session''' X = np.column_stack(df) return prediction.eval({x: X}) in_ = tuple(col for col in data.T) out = (yhat, ) _generic_test(f2, in_, out, wrapped_eq=lambda a, b: (a[0] == b[0]).all(), preload=tf.reset_default_graph)