Skip to content

Commit

Permalink
Mindspore demo:dog croissants classification
Browse files Browse the repository at this point in the history
Signed-off-by: Lj1ang <[email protected]>
  • Loading branch information
Lj1ang committed Oct 28, 2022
1 parent 9b7071b commit 9416568
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
FROM mindspore/mindspore-cpu:1.8.1

COPY ../lib/requirements.txt /home
FROM mindspore/mindspore-cpu:1.7.1
COPY lib/requirements.txt /home
# install requirements of sedna lib
RUN pip install -r /home/requirements.txt
RUN pip install Pillow
Expand All @@ -10,9 +9,9 @@ RUN pip install mindvision
ENV PYTHONPATH "/home/lib"

WORKDIR /home/work
COPY ../lib /home/lib
COPY lib /home/lib

COPY incremental_learning/dog_croissants_classification/training /home/work/
COPY examples/incremental_learning/dog_croissants_classification/training /home/work/


ENTRYPOINT ["python"]
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


# Dog-Croissants-classification Demo
## Prepare Model
auto-download

Expand All @@ -11,7 +10,7 @@ mkdir -p /data/dog_croissants/
mkdir /output
```

TODO:download dataset
TODO:download dataset. I have no idea where I should put dataset
```shell


Expand All @@ -24,7 +23,10 @@ download checkpoint
mkdir -p /models/base_model
mkdir -p /models/deploy_model
cd /models/base_model
#wget https://download.mindspore.cn/vision/classification/mobilenet_v2_1.0_224.ckpt
curl https://download.mindspore.cn/vision/classification/mobilenet_v2_1.0_224.ckpt -o base_model.ckpt
cd ../deploy_model
curl https://download.mindspore.cn/vision/classification/mobilenet_v2_1.0_224.ckpt -o deploy_model.ckpt

```
## build docker file
```shell
Expand Down Expand Up @@ -197,8 +199,3 @@ kubectl delete model initial-model
kubectl delete model deploy-model
kubectl delete IncrementalLearningJob dog-croissants-classification-demo
```
```shell
ctr -n k8s.io image pull registry.aliyuncs.com/google_containers/pause:3.5
ctr -n k8s.io image tag registry.aliyuncs.com/google_containers/pause:3.5 k8s.gcr.io/pause:3.5

```
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ spec:
hostPath:
path: /incremental_learning/he/
type: DirectoryOrCreate
outputDir: "/output"
outputDir: "/output"
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,3 @@ def parse(self, *args, path=None, train=True, image_shape=224, batch_size=2,num_
num_parallel_workers=num_parallel_workers)
dataset = dataset.batch(batch_size, drop_remainder=True)
return dataset

'''
def download_dataset(self):
dataset_url = "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/beginner/DogCroissants.zip"
path = "./datasets"
dl = DownLoad()
dl.download_and_extract_archive(dataset_url, path)
'''

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# 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.path

from sedna.common.config import Context
Expand Down Expand Up @@ -45,4 +46,3 @@ def main():

if __name__ == "__main__":
main()

Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
# 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 glob
import os


import glob
from PIL import Image
from sedna.common.config import Context
from sedna.core.incremental_learning import IncrementalLearning
Expand All @@ -23,15 +21,16 @@
import mindspore as ms
from mobilenet_v2 import mobilenet_v2_fine_tune


he_saved_url = Context.get_parameters("HE_SAVED_URL", './tmp')


def output_deal(is_hard_example, infer_image_path):
img_name=infer_image_path.split(r"/")[-1]
img_category = infer_image_path.split(r"/")[-2]
if is_hard_example:
shutil.copy(infer_image_path,f"{he_saved_url}/{img_category}_{img_name}")


def main():

hard_example_mining = IncrementalLearning.get_hem_algorithm_from_config(
Expand Down Expand Up @@ -64,6 +63,6 @@ def main():
print(f"{each_img}--->{results}-->{hard_example}")
output_deal(is_hard_example, each_img)


if __name__ == "__main__":
main()

Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
# limitations under the License.

from __future__ import division


import os

import PIL
import numpy as np
from PIL import Image
Expand All @@ -26,47 +23,36 @@
from mindvision.engine.callback import ValAccMonitor
from mobilenet_v2 import mobilenet_v2_fine_tune


os.environ['BACKEND_TYPE'] = 'MINDSPORE'


def preprocess(img:PIL.Image.Image):
#image=Image.open(img_path).convert("RGB").resize((224 ,224))
image=img.convert("RGB").resize((224,224))
mean = np.array([0.485 * 255, 0.456 * 255, 0.406 * 255])
std = np.array([0.229 * 255, 0.224 * 255, 0.225 * 255])
image = np.array(image)
image = (image - mean) / std
image = image.astype(np.float32)

image = np.transpose(image, (2, 0, 1))
image = np.expand_dims(image, axis=0)
return image

class Estimator:


class Estimator:
def __init__(self,**kwargs):
self.trained_ckpt_url=None


# TODO:save url
# example : https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.0/train.html#id3
def train(self, train_data,base_model_url, trained_ckpt_url, valid_data=None,epochs=10, **kwargs):
def train(self, train_data,base_model_url, trained_ckpt_url, valid_data=None, epochs=10, **kwargs):
network=mobilenet_v2_fine_tune(base_model_url).get_train_network()
network_opt=nn.Momentum(params=network.trainable_params(),learning_rate=0.01,momentum=0.9)
network_opt=nn.Momentum(params=network.trainable_params(), learning_rate=0.01, momentum=0.9)
network_loss=CrossEntropySmooth(sparse=True, reduction="mean", smooth_factor=0.1, classes_num=2)
metrics = {"Accuracy" : nn.Accuracy()}
metrics = {"Accuracy": nn.Accuracy()}
model=ms.Model(network, loss_fn=network_loss, optimizer=network_opt, metrics=metrics)
num_epochs = epochs
#best_ckpt_name=deploy_model_url.split(r"/")[-1]
#ckpt_dir=deploy_model_url.replace(best_ckpt_name, "")
model.train(num_epochs, train_data, callbacks=[ValAccMonitor(model, valid_data, num_epochs, save_best_ckpt=True, ckpt_directory=trained_ckpt_url), ms.TimeMonitor()])
self.trained_ckpt_url=trained_ckpt_url+"/best.ckpt"
# sedna will save model checkpoint in the path which is the value of MODEL_URL or MODEL_PATH
#ms.save_checkpoint(network, deploy_model_url)
self.trained_ckpt_url = trained_ckpt_url+"/best.ckpt"


def evaluate(self,data,model_path="",class_name="",input_shape=(224,224),**kwargs):
def evaluate(self,data,model_path="", class_name="", input_shape=(224, 224), **kwargs):
# load
network = mobilenet_v2_fine_tune(model_path).get_eval_network()
# eval
Expand All @@ -79,27 +65,16 @@ def evaluate(self,data,model_path="",class_name="",input_shape=(224,224),**kwarg
print(acc)
return acc


def predict(self, data,model, input_shape=None, **kwargs):
# load

def predict(self, data, model, input_shape=None, **kwargs):
# preprocess
preprocessed_data=preprocess(data)
# predict
pre=model.predict(ms.Tensor(preprocessed_data))
result=np.argmax(pre)
class_name={0:"Croissants", 1:"Dog"}
#print(class_name[result])
#return class_name[result]
return pre

def load(self, model_url):
pass

def save(self, model_path=None):
if not model_path:
return
#model_dir, model_name = os.path.split(model_path)
network = mobilenet_v2_fine_tune(self.trained_ckpt_url).get_eval_network()
ms.save_checkpoint(network, model_path)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
# 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 mindspore as ms
from mindvision.classification.models import mobilenet_v2
from mindvision.dataset import DownLoad



class mobilenet_v2_fine_tune:
# TODO: save model
def __init__(self, base_model_url=None):
models_download_url = "https://download.mindspore.cn/vision/classification/mobilenet_v2_1.0_224.ckpt"
dl=DownLoad()
if base_model_url==None:
dl.download_url(models_download_url)
else:
dl.download_url(models_download_url,filename=base_model_url )
def __init__(self, base_model_url):
self.network = mobilenet_v2(num_classes=2, resize=224)
#print("base_model_url == "+base_model_url)
self.param_dict = ms.load_checkpoint(base_model_url)


def get_train_network(self):
self.filter_list = [x.name for x in self.network.head.classifier.get_parameters()]
filter_list = [x.name for x in self.network.head.classifier.get_parameters()]
for key in list(self.param_dict.keys()):
for name in self.filter_list:
for name in filter_list:
if name in key:
print("Delete parameter from checkpoint: ", key)
del self.param_dict[key]
Expand All @@ -31,4 +35,4 @@ def get_train_network(self):

def get_eval_network(self):
ms.load_param_into_net(self.network, self.param_dict)
return self.network
return self.network
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# 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

from sedna.common.config import Context, BaseConfig
Expand Down Expand Up @@ -44,7 +45,6 @@ def main():
else:
print("valid_dataset_url : NULL")


train_data = ImgDataset(data_type="train").parse(path=train_dataset_url,
train=True,
image_shape=input_shape,
Expand All @@ -62,6 +62,7 @@ def main():
valid_data=valid_data,
epochs=1)


if __name__ == "__main__":
main()
print("train_phase_done")
18 changes: 0 additions & 18 deletions incremental-learning-dog-croissants-classification.Dockerfile

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def __call__(self, infer_result=None) -> bool:
return (len(confidence_score_list) / len(infer_result)
>= (1 - self.threshold_img))


@ClassFactory.register(ClassType.HEM, alias="Random")
class RandomFilter(BaseFilter):
"""judge a image is hard example or not randomly
Expand All @@ -197,9 +198,9 @@ class RandomFilter(BaseFilter):
`True` means hard sample, `False` means not.
"""
def __init__(self, random_ratio=0.3, **kwargs):
self.random_ratio=random_ratio
self.random_ratio = random_ratio

def __call__(self, *args, **kwargs):
if random.uniform(0,1) < self.random_ratio:
if random.uniform(0, 1) < self.random_ratio:
return True
return False
11 changes: 7 additions & 4 deletions lib/sedna/backend/mindspore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ def __init__(self, estimator, fine_tune=True, **kwargs):
**kwargs)
self.framework = "mindspore"
if self.use_npu:
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
context.set_context(mode=context.GRAPH_MODE,
device_target="Ascend")
elif self.use_cuda:
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
context.set_context(mode=context.GRAPH_MODE,
device_target="GPU")
else:
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
context.set_context(mode=context.GRAPH_MODE,
device_target="CPU")

if callable(self.estimator):
self.estimator = self.estimator()
Expand Down Expand Up @@ -70,4 +73,4 @@ def get_weights(self):
"""todo: no support yet"""

def set_weights(self, weights):
"""todo: no support yet"""
"""todo: no support yet"""

0 comments on commit 9416568

Please sign in to comment.