-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LIB: Add mindspore backend #169
base: main
Are you sure you want to change the base?
Changes from 1 commit
d7ee67c
a7f92a0
77aaded
6dbc799
82d614b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright 2021 The KubeEdge Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
|
||
import mindspore.context as context | ||
from sedna.backend.base import BackendBase | ||
from sedna.common.file_ops import FileOps | ||
|
||
|
||
class MSBackend(BackendBase): | ||
def __init__(self, estimator, fine_tune=True, **kwargs): | ||
super(MSBackend, self).__init__(estimator=estimator, | ||
fine_tune=fine_tune, | ||
**kwargs) | ||
self.framework = "mindspore" | ||
|
||
if self.use_npu: | ||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") | ||
elif self.use_cuda: | ||
context.set_context(mode=context.GRAPH_MODE, device_target="GPU") | ||
else: | ||
context.set_context(mode=context.GRAPH_MODE, device_target="CPU") | ||
|
||
if callable(self.estimator): | ||
self.estimator = self.estimator() | ||
|
||
def train(self, train_data, valid_data=None, **kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on existing specifications, the first parameter in train is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is because both formats (CSV, TXT) in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the misunderstanding. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I get it. However, should I develop a specific There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be a huge help to our community. And I would like you to give the professional advice on how to design from sedna.datasources import BaseDataSource
mnist_ds = ds.MnistDataset(train_data_path)
train_data = BaseDataSource(data_type="train")
train_data.x = []
train_data.y = []
for item in mnist_ds.create_dict_iterator():
train_data.x.append(item["image"].asnumpy())
train_data.y.append(item["label"].asnumpy()) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I'll solve it as soon as possible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried your method, but it didn't work very well. That is because, for method
For solution 1, it increases the difficulty of model development and is not a popular way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PTAL @jaypume There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You can help us raise an issue so we can track it. |
||
if callable(self.estimator): | ||
self.estimator = self.estimator() | ||
if self.fine_tune and FileOps.exists(self.model_save_path): | ||
self.finetune() | ||
self.has_load = True | ||
varkw = self.parse_kwargs(self.estimator.train, **kwargs) | ||
return self.estimator.train(train_data=train_data, | ||
valid_data=valid_data, | ||
**varkw) | ||
|
||
def predict(self, data, **kwargs): | ||
if not self.has_load: | ||
self.load() | ||
varkw = self.parse_kwargs(self.estimator.predict, **kwargs) | ||
return self.estimator.predict(data=data, **varkw) | ||
|
||
def evaluate(self, data, **kwargs): | ||
if not self.has_load: | ||
self.load() | ||
varkw = self.parse_kwargs(self.estimator.evaluate, **kwargs) | ||
return self.estimator.evaluate(data, **varkw) | ||
|
||
def finetune(self): | ||
"""todo: no support yet""" | ||
|
||
def load_weights(self): | ||
model_path = FileOps.join_path(self.model_save_path, self.model_name) | ||
if os.path.exists(model_path): | ||
self.estimator.load_weights(model_path) | ||
|
||
def get_weights(self): | ||
"""todo: no support yet""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return self.estimator.parameters_dict() |
||
|
||
def set_weights(self, weights): | ||
"""todo: no support yet""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for name, weight in weights.items(): There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for your great job! BTW, should we provide some example about mindspore to end user? joint inference or incremental learning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll upload a resnet example by tomorrow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chou-shun please put your example in
lib/examples/backend/mindspore/
.