def test_session_dump_zip(replace: bool): '''Tests session dump zip''' def my_transform(x: int, y: int) -> int: return x + y model = Model(transform=my_transform) model_name = 'my-model' session = AcumosSession() with tempfile.TemporaryDirectory() as tdir: model_zip_path = Path(tdir) / f"{model_name}.zip" session.dump_zip(model, model_name, model_zip_path) import zipfile with zipfile.ZipFile(model_zip_path, "r") as model_zip: assert set(model_zip.namelist()) == set(_REQ_FILES) if replace is False: with pytest.raises(AcumosError): session.dump_zip(model, model_name, model_zip_path) # file already exists else: session.dump_zip( model, model_name, model_zip_path, replace=replace) # file already exists but it will be replaced
from acumos.modeling import Model, List, create_dataframe 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) X_df = pd.DataFrame( X, 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 clf.predict_classes(X) model = Model(classify=classify_iris) session = AcumosSession() session.dump_zip(model, 'keras', './keras.zip', replace=True) # creates ./keras.zip
}) 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_zip(model, 'tensorflow', './tensorflow.zip', replace=True) # creates ./tensorflow.zip
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ===============LICENSE_END========================================================= ''' Dumps an example model for illustrating acumos_model_runner usage ''' from acumos.session import AcumosSession from acumos.modeling import Model, new_type # allow users to specify a "raw" bytes type. no protobuf message is generated here Image = new_type(bytes, 'Image', { 'dcae_input_name': 'a', 'dcae_output_name': 'a' }, 'example description') def image_func(image: Image) -> Image: '''Return an image''' return Image(image) if __name__ == '__main__': '''Main''' model = Model(imgae_func=image_func) session = AcumosSession() session.dump_zip(model, 'raw', './raw.zip', replace=True) # creates ./raw.zip
import io import PIL from acumos.modeling import Model, create_namedtuple from acumos.session import AcumosSession ImageShape = create_namedtuple('ImageShape', [('width', int), ('height', int)]) def get_format(data: bytes) -> str: '''Returns the format of an image''' buffer = io.BytesIO(data) img = PIL.Image.open(buffer) return img.format def get_shape(data: bytes) -> ImageShape: '''Returns the width and height of an image''' buffer = io.BytesIO(data) img = PIL.Image.open(buffer) shape = ImageShape(width=img.width, height=img.height) return shape model = Model(get_format=get_format, get_shape=get_shape) session = AcumosSession() session.dump_zip(model, 'image-model', './image-model.zip', replace=True) # creates ./image-model.zip