Skip to content

Commit

Permalink
Merge pull request #13 from RedisAI/cleanup
Browse files Browse the repository at this point in the history
Removed model utlities
  • Loading branch information
Sherin Thomas authored Jul 20, 2019
2 parents 6d4174c + ba73bdf commit 0a82131
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 324 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.pydevproject
*.pyc
.venv/
redisai.egg-info
redisai.egg-info
.idea
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# RedisAI Python Client


## Installing
## Installation

1. Install Redis 5.0 or above

Expand All @@ -20,6 +20,49 @@
$ pip install redisai
```

[RedisAI example repo](https://github.com/RedisAI/redisai-examples) shows few examples made using redisai-py under `python_client` section.
4. Install serialization-deserialization utility (optional)
```sh
$ pip install ml2rt
```

[RedisAI example repo](https://github.com/RedisAI/redisai-examples) shows few examples made using redisai-py under `python_client` section. Checkout [ml2rt](https://github.com/hhsecond/ml2rt) for convenient functions those might help in converting models (sparkml, sklearn, xgboost to ONNX), serializing models to disk, loading it back to redisai-py etc.

For a quick walk through, checkout this example

```python
from redisai import Client
from redisai import Tensor, BlobTensor, DType, Device, Backend
import ml2rt

client = Client()
client.tensorset('x', Tensor(DType.float, [2], [2, 3]))
t = client.tensorget('x')
print(t.value)

model = mlut.load_model('test/testdata/graph.pb')
client.tensorset('a', Tensor.scalar(DType.float, 2, 3))
client.tensorset('b', Tensor.scalar(DType.float, 12, 10))
client.modelset('m', Backend.tf,
Device.cpu,
input=['a', 'b'],
output='mul',
data=model)
client.modelrun('m', ['a', 'b'], ['mul'])
print(client.tensorget('mul').value)

# Try with a script
script = mlut.load_script('test/testdata/script.txt')
client.scriptset('ket', Device.cpu, script)
client.scriptrun('ket', 'bar', input=['a', 'b'], output='c')

b1 = client.tensorget('c', as_type=BlobTensor)
b2 = client.tensorget('c', as_type=BlobTensor)

client.tensorset('d', BlobTensor(DType.float, b1.shape, b1, b2))

tnp = b1.to_numpy()
print(tnp)

```


23 changes: 9 additions & 14 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from __future__ import print_function
from redisai import Client, Tensor, ScalarTensor, \
from redisai import Client, Tensor, \
BlobTensor, DType, Device, Backend
from redisai import model as raimodel
import mlut

client = Client()
client.tensorset('x', Tensor(DType.float, [2], [2, 3]))
t = client.tensorget('x')
print(t.value)

model = raimodel.Model.load('../RedisAI/examples/models/graph.pb')
client.tensorset('a', ScalarTensor(DType.float, 2, 3))
client.tensorset('b', ScalarTensor(DType.float, 12, 10))
model = mlut.load_model('test/testdata/graph.pb')
client.tensorset('a', Tensor.scalar(DType.float, 2, 3))
client.tensorset('b', Tensor.scalar(DType.float, 12, 10))
client.modelset('m', Backend.tf,
Device.cpu,
input=['a', 'b'],
Expand All @@ -20,18 +19,14 @@
print(client.tensorget('mul').value)

# Try with a script
script = raimodel.Model.load('../RedisAI/examples/models/script.txt')
script = mlut.load_script('test/testdata/script.txt')
client.scriptset('ket', Device.cpu, script)
client.scriptrun('ket', 'bar', input=['a', 'b'], output='c')

b1 = client.tensorget('c', astype=BlobTensor)
b2 = client.tensorget('c', astype=BlobTensor)
bt = BlobTensor(DType.float, b1.shape, b1, b2)

print(len(bytes(bt.blob)))
print(bt.shape)
b1 = client.tensorget('c', as_type=BlobTensor)
b2 = client.tensorget('c', as_type=BlobTensor)

client.tensorset('d', BlobTensor(DType.float, b1.shape, b1, b2))

tnp = b1.to_numpy()
client.tensorset('e', tnp)
client.tensorset('e', tnp)
24 changes: 2 additions & 22 deletions redisai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,2 @@
from .version import __version__
from .client import (Client, Tensor, BlobTensor, DType, Device, Backend)


def save_model(*args, **kwargs):
"""
Importing inside to avoid loading the TF/PyTorch/ONNX
into the scope unnecessary. This function wraps the
internal save model utility to make it user friendly
"""
from .model import Model
Model.save(*args, **kwargs)


def load_model(*args, **kwargs):
"""
Importing inside to avoid loading the TF/PyTorch/ONNX
into the scope unnecessary. This function wraps the
internal load model utility to make it user friendly
"""
from .model import Model
return Model.load(*args, **kwargs)
from .version import __version__ # noqa
from .client import (Client, Tensor, BlobTensor, DType, Device, Backend) # noqa
6 changes: 3 additions & 3 deletions redisai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
np = None

try:
from typing import Union, Any, AnyStr, ByteString, Collection, Type
from typing import Union, Any, AnyStr, ByteString, Collection, Type # noqa
except ImportError:
pass

Expand Down Expand Up @@ -165,7 +165,7 @@ def _to_numpy_type(t):
}
if t in mm:
return mm[t]
return t
return t.lower()

@classmethod
def from_resp(cls, dtype, shape, value):
Expand Down Expand Up @@ -225,7 +225,7 @@ def tensorset(self, key, tensor):
return self.execute_command(*args)

def tensorget(self, key, as_type=Tensor, meta_only=False):
# type: (AnyStr, Type[Tensor], bool) -> Tensor
# type: (AnyStr, Type[Tensor], bool) -> Union[Tensor, BlobTensor]
"""
Retrieve the value of a tensor from the server
:param key: the name of the tensor
Expand Down
144 changes: 0 additions & 144 deletions redisai/model.py

This file was deleted.

6 changes: 1 addition & 5 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
numpy
torch
tensorflow
onnx
skl2onnx
pandas
mlut
2 changes: 1 addition & 1 deletion test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np
import os.path
from redisai import Client, DType, Backend, Device, Tensor, BlobTensor
from redisai import load_model
from mlut import load_model
from redis.exceptions import ResponseError


Expand Down
Loading

0 comments on commit 0a82131

Please sign in to comment.