Browse Source

add code

pull/90/head
littlecats7 Gitee 5 years ago
parent
commit
859755d62f
6 changed files with 105 additions and 0 deletions
  1. +20
    -0
      mindspore-jina/Dockerfile
  2. +37
    -0
      mindspore-jina/__init__.py
  3. +17
    -0
      mindspore-jina/config.yml
  4. +14
    -0
      mindspore-jina/manifest.yml
  5. +1
    -0
      mindspore-jina/tests/__init__.py
  6. +16
    -0
      mindspore-jina/tests/test_mindsporelenet.py

+ 20
- 0
mindspore-jina/Dockerfile View File

@@ -0,0 +1,20 @@
FROM mindspore/mindspore-cpu:1.0.0

# setup the workspace
COPY . /workspace
WORKDIR /workspace

# fix: add to path
ENV PATH="/root/.local/bin:${PATH}"

# install the third-party requirements
RUN pip install --user -r requirements.txt

RUN cd lenet && \
python train.py --data_path data/fashion/ --ckpt_path ckpt --device_target="CPU" && \
cd -

# for testing the image
RUN pip install --user pytest && pytest -s

ENTRYPOINT ["jina", "pod", "--uses", "config.yml"]

+ 37
- 0
mindspore-jina/__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()

+ 17
- 0
mindspore-jina/config.yml View File

@@ -0,0 +1,17 @@
!MindsporeLeNet
with:
model_path: lenet/ckpt/checkpoint_lenet-1_1875.ckpt
metas:
py_modules:
- __init__.py
# - You can put more dependencies here
requests:
on:
[IndexRequest, SearchRequest]:
- !Blob2PngURI {}
- !EncodeDriver {}
- !ExcludeQL
with:
fields:
- buffer
- chunks

+ 14
- 0
mindspore-jina/manifest.yml View File

@@ -0,0 +1,14 @@
manifest_version: 1
name: MindsporeLeNet
kind: encoder
description:
|
Encoding image into vectors using mindspore
author: Jina AI Dev-Team (dev-team@jina.ai)
url: https://jina.ai
vendor: Jina AI Limited
documentation: https://github.com/jina-ai/jina-hub
version: 0.0.2
license: apache-2.0
keywords: [mindspore, lenet]
type: pod

+ 1
- 0
mindspore-jina/tests/__init__.py View File

@@ -0,0 +1 @@
##

+ 16
- 0
mindspore-jina/tests/test_mindsporelenet.py View File

@@ -0,0 +1,16 @@
import numpy as np

from .. import MindsporeLeNet


def test_mindsporelenet():
"""here is my test code

https://docs.pytest.org/en/stable/getting-started.html#create-your-first-test
"""
mln = MindsporeLeNet('lenet/ckpt/checkpoint_lenet-1_1875.ckpt')
tmp = np.random.random([4, 28 * 28])

# The sixth layer is a fully connected layer (F6) with 84 units.
# it is the last layer before the output
assert mln.encode(tmp).shape == (4, 84)

Loading…
Cancel
Save