From 859755d62ff0e4872b5abb0a1c694bc7109eb253 Mon Sep 17 00:00:00 2001 From: littlecats7 <7389039+littlecats7@user.noreply.gitee.com> Date: Thu, 26 Nov 2020 15:04:54 +0800 Subject: [PATCH] add code --- mindspore-jina/Dockerfile | 20 +++++++++++ mindspore-jina/__init__.py | 37 +++++++++++++++++++++ mindspore-jina/config.yml | 17 ++++++++++ mindspore-jina/manifest.yml | 14 ++++++++ mindspore-jina/tests/__init__.py | 1 + mindspore-jina/tests/test_mindsporelenet.py | 16 +++++++++ 6 files changed, 105 insertions(+) create mode 100644 mindspore-jina/Dockerfile create mode 100644 mindspore-jina/__init__.py create mode 100644 mindspore-jina/config.yml create mode 100644 mindspore-jina/manifest.yml create mode 100644 mindspore-jina/tests/__init__.py create mode 100644 mindspore-jina/tests/test_mindsporelenet.py diff --git a/mindspore-jina/Dockerfile b/mindspore-jina/Dockerfile new file mode 100644 index 0000000..e1fa02d --- /dev/null +++ b/mindspore-jina/Dockerfile @@ -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"] \ No newline at end of file diff --git a/mindspore-jina/__init__.py b/mindspore-jina/__init__.py new file mode 100644 index 0000000..67884a9 --- /dev/null +++ b/mindspore-jina/__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/config.yml b/mindspore-jina/config.yml new file mode 100644 index 0000000..18f681d --- /dev/null +++ b/mindspore-jina/config.yml @@ -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 \ No newline at end of file diff --git a/mindspore-jina/manifest.yml b/mindspore-jina/manifest.yml new file mode 100644 index 0000000..fcabe54 --- /dev/null +++ b/mindspore-jina/manifest.yml @@ -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 \ No newline at end of file diff --git a/mindspore-jina/tests/__init__.py b/mindspore-jina/tests/__init__.py new file mode 100644 index 0000000..1b6ba71 --- /dev/null +++ b/mindspore-jina/tests/__init__.py @@ -0,0 +1 @@ +## \ No newline at end of file diff --git a/mindspore-jina/tests/test_mindsporelenet.py b/mindspore-jina/tests/test_mindsporelenet.py new file mode 100644 index 0000000..b2dcd53 --- /dev/null +++ b/mindspore-jina/tests/test_mindsporelenet.py @@ -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)