Browse Source

add mindspore-jina code

pull/90/head
littlecats7 Gitee 4 years ago
parent
commit
6e629d1ec7
5 changed files with 93 additions and 0 deletions
  1. +3
    -0
      mindspore-jina/MindsporeLeNet/README.md
  2. +37
    -0
      mindspore-jina/MindsporeLeNet/__init__.py
  3. +25
    -0
      mindspore-jina/README.md
  4. +12
    -0
      mindspore-jina/helloworld.flow.index.yml
  5. +16
    -0
      mindspore-jina/helloworld.flow.query.yml

+ 3
- 0
mindspore-jina/MindsporeLeNet/README.md View File

@@ -0,0 +1,3 @@
# MindsporeLeNet

Encoding image into vectors using mindspore

+ 37
- 0
mindspore-jina/MindsporeLeNet/__init__.py View File

@@ -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()

+ 25
- 0
mindspore-jina/README.md View File

@@ -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
```

## 参考博客

+ 12
- 0
mindspore-jina/helloworld.flow.index.yml View File

@@ -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

+ 16
- 0
mindspore-jina/helloworld.flow.query.yml View File

@@ -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

Loading…
Cancel
Save