Skip to content

Commit 0a82131

Browse files
author
Sherin Thomas
authored
Merge pull request #13 from RedisAI/cleanup
Removed model utlities
2 parents 6d4174c + ba73bdf commit 0a82131

File tree

9 files changed

+63
-324
lines changed

9 files changed

+63
-324
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.pydevproject
33
*.pyc
44
.venv/
5-
redisai.egg-info
5+
redisai.egg-info
6+
.idea

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# RedisAI Python Client
99

1010

11-
## Installing
11+
## Installation
1212

1313
1. Install Redis 5.0 or above
1414

@@ -20,6 +20,49 @@
2020
$ pip install redisai
2121
```
2222

23-
[RedisAI example repo](https://github.com/RedisAI/redisai-examples) shows few examples made using redisai-py under `python_client` section.
23+
4. Install serialization-deserialization utility (optional)
24+
```sh
25+
$ pip install ml2rt
26+
```
27+
28+
[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.
29+
30+
For a quick walk through, checkout this example
31+
32+
```python
33+
from redisai import Client
34+
from redisai import Tensor, BlobTensor, DType, Device, Backend
35+
import ml2rt
36+
37+
client = Client()
38+
client.tensorset('x', Tensor(DType.float, [2], [2, 3]))
39+
t = client.tensorget('x')
40+
print(t.value)
41+
42+
model = mlut.load_model('test/testdata/graph.pb')
43+
client.tensorset('a', Tensor.scalar(DType.float, 2, 3))
44+
client.tensorset('b', Tensor.scalar(DType.float, 12, 10))
45+
client.modelset('m', Backend.tf,
46+
Device.cpu,
47+
input=['a', 'b'],
48+
output='mul',
49+
data=model)
50+
client.modelrun('m', ['a', 'b'], ['mul'])
51+
print(client.tensorget('mul').value)
52+
53+
# Try with a script
54+
script = mlut.load_script('test/testdata/script.txt')
55+
client.scriptset('ket', Device.cpu, script)
56+
client.scriptrun('ket', 'bar', input=['a', 'b'], output='c')
57+
58+
b1 = client.tensorget('c', as_type=BlobTensor)
59+
b2 = client.tensorget('c', as_type=BlobTensor)
60+
61+
client.tensorset('d', BlobTensor(DType.float, b1.shape, b1, b2))
62+
63+
tnp = b1.to_numpy()
64+
print(tnp)
65+
66+
```
2467

2568

example.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
from __future__ import print_function
2-
from redisai import Client, Tensor, ScalarTensor, \
1+
from redisai import Client, Tensor, \
32
BlobTensor, DType, Device, Backend
4-
from redisai import model as raimodel
3+
import mlut
54

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

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

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

27-
b1 = client.tensorget('c', astype=BlobTensor)
28-
b2 = client.tensorget('c', astype=BlobTensor)
29-
bt = BlobTensor(DType.float, b1.shape, b1, b2)
30-
31-
print(len(bytes(bt.blob)))
32-
print(bt.shape)
26+
b1 = client.tensorget('c', as_type=BlobTensor)
27+
b2 = client.tensorget('c', as_type=BlobTensor)
3328

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

3631
tnp = b1.to_numpy()
37-
client.tensorset('e', tnp)
32+
client.tensorset('e', tnp)

redisai/__init__.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,2 @@
1-
from .version import __version__
2-
from .client import (Client, Tensor, BlobTensor, DType, Device, Backend)
3-
4-
5-
def save_model(*args, **kwargs):
6-
"""
7-
Importing inside to avoid loading the TF/PyTorch/ONNX
8-
into the scope unnecessary. This function wraps the
9-
internal save model utility to make it user friendly
10-
"""
11-
from .model import Model
12-
Model.save(*args, **kwargs)
13-
14-
15-
def load_model(*args, **kwargs):
16-
"""
17-
Importing inside to avoid loading the TF/PyTorch/ONNX
18-
into the scope unnecessary. This function wraps the
19-
internal load model utility to make it user friendly
20-
"""
21-
from .model import Model
22-
return Model.load(*args, **kwargs)
1+
from .version import __version__ # noqa
2+
from .client import (Client, Tensor, BlobTensor, DType, Device, Backend) # noqa

redisai/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
np = None
1010

1111
try:
12-
from typing import Union, Any, AnyStr, ByteString, Collection, Type
12+
from typing import Union, Any, AnyStr, ByteString, Collection, Type # noqa
1313
except ImportError:
1414
pass
1515

@@ -165,7 +165,7 @@ def _to_numpy_type(t):
165165
}
166166
if t in mm:
167167
return mm[t]
168-
return t
168+
return t.lower()
169169

170170
@classmethod
171171
def from_resp(cls, dtype, shape, value):
@@ -225,7 +225,7 @@ def tensorset(self, key, tensor):
225225
return self.execute_command(*args)
226226

227227
def tensorget(self, key, as_type=Tensor, meta_only=False):
228-
# type: (AnyStr, Type[Tensor], bool) -> Tensor
228+
# type: (AnyStr, Type[Tensor], bool) -> Union[Tensor, BlobTensor]
229229
"""
230230
Retrieve the value of a tensor from the server
231231
:param key: the name of the tensor

redisai/model.py

Lines changed: 0 additions & 144 deletions
This file was deleted.

test-requirements.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
numpy
2-
torch
3-
tensorflow
4-
onnx
5-
skl2onnx
6-
pandas
2+
mlut

test/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33
import os.path
44
from redisai import Client, DType, Backend, Device, Tensor, BlobTensor
5-
from redisai import load_model
5+
from mlut import load_model
66
from redis.exceptions import ResponseError
77

88

0 commit comments

Comments
 (0)