diff --git a/mindspore-jina/MindsporeLeNet/README.md b/mindspore-jina/MindsporeLeNet/README.md new file mode 100644 index 0000000..65748a7 --- /dev/null +++ b/mindspore-jina/MindsporeLeNet/README.md @@ -0,0 +1,3 @@ +# MindsporeLeNet + +Encoding image into vectors using mindspore \ No newline at end of file diff --git a/mindspore-jina/MindsporeLeNet/__init__.py b/mindspore-jina/MindsporeLeNet/__init__.py new file mode 100644 index 0000000..67884a9 --- /dev/null +++ b/mindspore-jina/MindsporeLeNet/__init__.py @@ -0,0 +1,37 @@ +import numpy as np +from jina.executors.encoders.frameworks import BaseMindsporeEncoder + + +class MindsporeLeNet(BaseMindsporeEncoder): + """ + :class:`MindsporeLeNet` Encoding image into vectors using mindspore. + """ + + def encode(self, data, *args, **kwargs): + # data is B x D, where D = 28 * 28 + # LeNet only accepts BCHW format where H=W=32 + # hence we need to do some simple transform + from mindspore import Tensor + + data = np.pad(data.reshape([-1, 1, 28, 28]), + [(0, 0), (0, 0), (0, 4), (0, 4)]).astype('float32') + return self.model(Tensor(data)).asnumpy() + + def get_cell(self): + from .lenet.src.lenet import LeNet5 + class LeNet5Embed(LeNet5): + def construct(self, x): + x = self.conv1(x) + x = self.relu(x) + x = self.max_pool2d(x) + x = self.conv2(x) + x = self.relu(x) + x = self.max_pool2d(x) + x = self.flatten(x) + x = self.fc1(x) + x = self.relu(x) + x = self.fc2(x) + x = self.relu(x) + return x + + return LeNet5Embed() diff --git a/mindspore-jina/README.md b/mindspore-jina/README.md new file mode 100644 index 0000000..99e90b6 --- /dev/null +++ b/mindspore-jina/README.md @@ -0,0 +1,25 @@ +# 三分钟教你用MindSpore和Jina搭建一个服装搜索系统 + +## 准备环境 + +- Mac OS or Linux +- Python 3.7, 3.8 +- [Jina 0.7+ with Hub extenstion (i.e. `pip install "jina[hub]"`)](https://get.jina.ai) +- Docker + +## 执行步骤 +### 1.创建Jina Hub 镜像 + +```bash +git clone https://github.com/hanxiao/mindspore-jina-example.git + +jina hub build MindsporeLeNet/ --pull --test-uses +``` + +### 2. Use in Jina Hello-World + +``` +jina hello-world --uses-index helloworld.flow.index.yml --uses-query helloworld.flow.query.yml +``` + +## 参考博客 \ No newline at end of file diff --git a/mindspore-jina/helloworld.flow.index.yml b/mindspore-jina/helloworld.flow.index.yml new file mode 100644 index 0000000..d3e53d7 --- /dev/null +++ b/mindspore-jina/helloworld.flow.index.yml @@ -0,0 +1,12 @@ +!Flow +with: + logserver: $WITH_LOGSERVER + compress_hwm: 1024 +pods: + encode: + uses: jinahub/pod.encoder.mindsporelenet:0.0.1 + parallel: $PARALLEL + index: + uses: $RESOURCE_DIR/helloworld.indexer.yml + shards: $SHARDS + separated_workspace: true \ No newline at end of file diff --git a/mindspore-jina/helloworld.flow.query.yml b/mindspore-jina/helloworld.flow.query.yml new file mode 100644 index 0000000..b47997b --- /dev/null +++ b/mindspore-jina/helloworld.flow.query.yml @@ -0,0 +1,16 @@ +!Flow +with: + read_only: true # better add this in the query time + logserver: $WITH_LOGSERVER + compress_hwm: 1024 +pods: + encode: + uses: jinahub/pod.encoder.mindsporelenet:0.0.1 + parallel: $PARALLEL + index: + uses: $RESOURCE_DIR/helloworld.indexer.yml + shards: $SHARDS + separated_workspace: true + polling: all + uses_after: $RESOURCE_DIR/helloworld.reduce.yml + timeout_ready: 100000 # larger timeout as in query time will read all the data \ No newline at end of file