def test_run_tf_model(self): model = os.path.join(MODEL_DIR, 'graph.pb') bad_model = os.path.join(MODEL_DIR, 'pt-minimal.pt') with open(model, 'rb') as f: model_pb = f.read() with open(bad_model, 'rb') as f: wrong_model_pb = f.read() con = self.get_client() con.modelset('m', Backend.tf, Device.cpu, model_pb, input=['a', 'b'], output='mul') # wrong model self.assertRaises(ResponseError, con.modelset, 'm', Backend.tf, Device.cpu, wrong_model_pb, input=['a', 'b'], output='mul') # missing inputs/outputs self.assertRaises(ValueError, con.modelset, 'm', Backend.tf, Device.cpu, wrong_model_pb) # wrong backend self.assertRaises(ResponseError, con.modelset, 'm', Backend.torch, Device.cpu, model_pb, input=['a', 'b'], output='mul') con.tensorset('a', Tensor.scalar(DType.float, 2, 3)) con.tensorset('b', Tensor.scalar(DType.float, 2, 3)) con.modelrun('m', ['a', 'b'], 'c') tensor = con.tensorget('c') self.assertEqual([4, 9], tensor.value)
def testScriptLoad(self): con = self.get_client() dirname = os.path.dirname(__file__) path = f'{dirname}/testdata/script.txt' script = raimodel.Model.load(path) con.scriptset('script', Device.cpu, script) con.tensorset('a', Tensor.scalar(DType.float, 2, 5)) con.tensorset('b', Tensor.scalar(DType.float, 3, 7)) con.scriptrun('script', 'bar', ['a', 'b'], 'c') tensor = con.tensorget('c') self.assertEqual([5, 12], tensor.value)
def test_set_tensor(self): con = self.get_client() con.tensorset('x', Tensor.scalar(DType.float, 2, 3)) values = con.tensorget('x') self.assertEqual([2, 3], values.value) con.tensorset('x', Tensor.scalar(DType.int32, 2, 3)) values = con.tensorget('x').value self.assertEqual([2, 3], values) self.assertRaises(Exception, con.tensorset, 1) self.assertRaises(Exception, con.tensorset, 'x')
def testPyTorchGraph(self): torch_graph = MyModule() path = f'{time.time()}.pb' raimodel.Model.save(torch_graph, path) model = raimodel.Model.load(path) os.remove(path) con = self.get_client() con.modelset('ptmodel', Backend.torch, Device.cpu, model) con.tensorset('a', Tensor.scalar(DType.float, 2, 5)) con.tensorset('b', Tensor.scalar(DType.float, 3, 7)) con.modelrun('ptmodel', ['a', 'b'], 'c') tensor = con.tensorget('c') self.assertEqual([5, 12], tensor.value)
def test_scripts(self): con = self.get_client() self.assertRaises(ResponseError, con.scriptset, 'ket', Device.cpu, 'return 1') con.scriptset('ket', Device.cpu, r""" def bar(a, b): return a + b """) con.tensorset('a', Tensor.scalar(DType.float, 2, 3)) con.tensorset('b', Tensor.scalar(DType.float, 2, 3)) # try with bad arguments: self.assertRaises(ResponseError, con.scriptrun, 'ket', 'bar', input='a', output='c') con.scriptrun('ket', 'bar', input=['a', 'b'], output='c') tensor = con.tensorget('c') self.assertEqual([4, 6], tensor.value) # TODO: image/blob tests; more numpy tests..
def testTFGraph(self): y = get_tf_graph() init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) path = f'{time.time()}.pb' raimodel.Model.save(sess, path, output=['output']) model = raimodel.Model.load(path) os.remove(path) con = self.get_client() con.modelset('tfmodel', Backend.tf, Device.cpu, model, input=['input'], output=['output']) con.tensorset('a', Tensor.scalar(DType.float, 2)) con.modelrun('tfmodel', ['a'], 'c') tensor = con.tensorget('c') self.assertEqual([13], tensor.value)
def testTensorShapes(self): t = Tensor(DType.float, [4], [1, 2, 3, 4]) self.assertEqual([4], t.shape) t = BlobTensor.from_numpy(np.array([[1, 2, 3], [4, 5, 6]])) self.assertEqual([2, 3], t.shape)
from __future__ import print_function from redisai import Client, Tensor, ScalarTensor, \ BlobTensor, DType, Device, Backend from redisai import model as raimodel 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)) 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 = raimodel.Model.load('../RedisAI/examples/models/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)))