parent
201c5ff4ee
commit
1f66e59648
@ -1,24 +0,0 @@
|
|||||||
CONVERTER="../../../../../mindspore/lite/build/tools/converter/converter_lite"
|
|
||||||
if [ ! -f "$CONVERTER" ]; then
|
|
||||||
if ! command -v converter_lite &> /dev/null
|
|
||||||
then
|
|
||||||
echo "converter_lite could not be found in MindSpore build directory nor in system path"
|
|
||||||
exit
|
|
||||||
else
|
|
||||||
CONVERTER=converter_lite
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "============Exporting=========="
|
|
||||||
if [ -n "$1" ]; then
|
|
||||||
DOCKER_IMG=$1
|
|
||||||
docker run -w $PWD --runtime=nvidia -v /home/$USER:/home/$USER --privileged=true ${DOCKER_IMG} /bin/bash -c "python lenet_export.py; chmod 444 lenet_tod.mindir; rm -rf __pycache__"
|
|
||||||
else
|
|
||||||
echo "MindSpore docker was not provided, attempting to run locally"
|
|
||||||
python lenet_export.py
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
echo "============Converting========="
|
|
||||||
$CONVERTER --fmk=MINDIR --trainModel=true --modelFile=lenet_tod.mindir --outputFile=lenet_tod
|
|
||||||
|
|
@ -1,82 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
display_usage() {
|
|
||||||
echo -e "\nUsage: prepare_and_run.sh dataset_path [mindspore_docker] [release.tar.gz]\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
if [ -n "$1" ]; then
|
|
||||||
MNIST_DATA_PATH=$1
|
|
||||||
else
|
|
||||||
echo "MNIST Dataset directory path was not provided"
|
|
||||||
display_usage
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$2" ]; then
|
|
||||||
DOCKER=$2
|
|
||||||
else
|
|
||||||
DOCKER=""
|
|
||||||
#echo "MindSpore docker was not provided"
|
|
||||||
#display_usage
|
|
||||||
#exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$3" ]; then
|
|
||||||
TARBALL=$3
|
|
||||||
else
|
|
||||||
if [ -f ../../../../output/mindspore-lite-*-runtime-arm64-cpu-train.tar.gz ]; then
|
|
||||||
TARBALL="../../../../output/mindspore-lite-*-runtime-arm64-cpu-train.tar.gz"
|
|
||||||
else
|
|
||||||
echo "release.tar.gz was not found"
|
|
||||||
display_usage
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
# Prepare the model
|
|
||||||
cd model/
|
|
||||||
rm -f *.ms
|
|
||||||
./prepare_model.sh $DOCKER
|
|
||||||
cd -
|
|
||||||
|
|
||||||
# Copy the .ms model to the package folder
|
|
||||||
rm -rf package
|
|
||||||
mkdir -p package/model
|
|
||||||
cp model/*.ms package/model
|
|
||||||
|
|
||||||
# Copy the running script to the package
|
|
||||||
cp scripts/train.sh package/
|
|
||||||
cp scripts/eval.sh package/
|
|
||||||
|
|
||||||
# Copy the shared MindSpore ToD library
|
|
||||||
tar -xzvf ${TARBALL} --wildcards --no-anchored libmindspore-lite.so
|
|
||||||
tar -xzvf ${TARBALL} --wildcards --no-anchored include
|
|
||||||
mv mindspore-*/lib package/
|
|
||||||
mkdir msl
|
|
||||||
mv mindspore-*/* msl/
|
|
||||||
rm -rf mindspore-*
|
|
||||||
|
|
||||||
# Copy the dataset to the package
|
|
||||||
cp -r ${MNIST_DATA_PATH} package/dataset
|
|
||||||
|
|
||||||
# Compile program
|
|
||||||
make TARGET=arm64
|
|
||||||
|
|
||||||
# Copy the executable to the package
|
|
||||||
mv bin package/
|
|
||||||
|
|
||||||
# Push the folder to the device
|
|
||||||
adb push package /data/local/tmp/
|
|
||||||
|
|
||||||
echo "Training on Device"
|
|
||||||
adb shell < scripts/run_train.sh
|
|
||||||
|
|
||||||
echo
|
|
||||||
echo "Load trained model and evaluate accuracy"
|
|
||||||
adb shell < scripts/run_eval.sh
|
|
||||||
echo
|
|
||||||
|
|
||||||
#rm -rf src/*.o package model/__pycache__ model/*.ms
|
|
||||||
|
|
||||||
#./prepare_and_run.sh /opt/share/dataset/mnist mindspore_dev:5
|
|
@ -1,2 +0,0 @@
|
|||||||
cd /data/local/tmp/package
|
|
||||||
/system/bin/sh eval.sh
|
|
@ -1,2 +0,0 @@
|
|||||||
cd /data/local/tmp/package
|
|
||||||
/system/bin/sh train.sh
|
|
@ -0,0 +1,41 @@
|
|||||||
|
BASE_DIR=$(realpath ../../../../)
|
||||||
|
APP:=bin/net_runner
|
||||||
|
MSLIB:=mindspore-lite
|
||||||
|
MSDIR:=$(realpath package-$(TARGET)/lib)
|
||||||
|
|
||||||
|
SRC:=src/net_runner.cc src/dataset.cc
|
||||||
|
OBJ:=$(SRC:.cc=.o)
|
||||||
|
|
||||||
|
CFLAGS := -Ofast -std=c++17 \
|
||||||
|
-I . \
|
||||||
|
-I ./msl \
|
||||||
|
-I ./msl/third_party/flatbuffers/include
|
||||||
|
|
||||||
|
|
||||||
|
ifeq ($(TARGET),arm64)
|
||||||
|
CXX := ${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++
|
||||||
|
CFLAGS += --target=aarch64-none-linux-android21 --gcc-toolchain=${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fdata-sections -ffunction-sections
|
||||||
|
LDFLAGS := --target=aarch64-none-linux-android21 --gcc-toolchain=${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot -Wl,--gc-sections
|
||||||
|
LDFLAGS += -L$(MSDIR) -l$(MSLIB) -pthread -llog -latomic -lm
|
||||||
|
else
|
||||||
|
CFLAGS += -g
|
||||||
|
LDFLAGS := -L$(MSDIR) -l$(MSLIB) -lpthread -Wl,-rpath,$(MSDIR)
|
||||||
|
endif
|
||||||
|
LD := ${CXX}
|
||||||
|
|
||||||
|
|
||||||
|
all:$(APP)
|
||||||
|
|
||||||
|
$(APP): $(OBJ) $(MSDIR)/lib$(MSLIB).so
|
||||||
|
@mkdir -p bin
|
||||||
|
$(LD) $(OBJ) $(LDFLAGS) -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf src/*.o bin/
|
||||||
|
|
||||||
|
|
||||||
|
mrproper:
|
||||||
|
rm -rf package* msl src/*.o bin/ model/*.mindir model/*.ms model/*.so model/converter_lite
|
||||||
|
|
||||||
|
%.o:%.cc
|
||||||
|
$(CXX) $(CFLAGS) -c $< -o $@
|
@ -0,0 +1,32 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo "============Exporting=========="
|
||||||
|
if [ -n "$1" ]; then
|
||||||
|
DOCKER_IMG=$1
|
||||||
|
docker run -w $PWD --runtime=nvidia -v /home/$USER:/home/$USER --privileged=true ${DOCKER_IMG} /bin/bash -c "PYTHONPATH=../../../../../model_zoo/official/cv/lenet/src python lenet_export.py; chmod 444 lenet_tod.mindir; rm -rf __pycache__"
|
||||||
|
else
|
||||||
|
echo "MindSpore docker was not provided, attempting to run locally"
|
||||||
|
PYTHONPATH=../../../../../model_zoo/official/cv/lenet/src python lenet_export.py
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
CONVERTER="../../../build/tools/converter/converter_lite"
|
||||||
|
if [ ! -f "$CONVERTER" ]; then
|
||||||
|
if ! command -v converter_lite &> /dev/null
|
||||||
|
then
|
||||||
|
tar -xzf ../../../../../output/mindspore-lite-*-converter-ubuntu-train.tar.gz --strip-components 2 --wildcards --no-anchored converter_lite libmindspore_gvar.so
|
||||||
|
if [ -f ./converter_lite ]; then
|
||||||
|
CONVERTER=./converter_lite
|
||||||
|
else
|
||||||
|
echo "converter_lite could not be found in MindSpore build directory nor in system path"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
CONVERTER=converter_lite
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
echo "============Converting========="
|
||||||
|
LD_LIBRARY_PATH=./ $CONVERTER --fmk=MINDIR --trainModel=true --modelFile=lenet_tod.mindir --outputFile=lenet_tod
|
||||||
|
|
@ -0,0 +1,116 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
display_usage()
|
||||||
|
{
|
||||||
|
echo -e "\nUsage: prepare_and_run.sh -D dataset_path [-d mindspore_docker] [-r release.tar.gz] [-t arm64|x86]\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
checkopts()
|
||||||
|
{
|
||||||
|
TARGET="arm64"
|
||||||
|
DOCKER=""
|
||||||
|
MNIST_DATA_PATH=""
|
||||||
|
while getopts 'D:d:r:t:' opt
|
||||||
|
do
|
||||||
|
OPTARG=$(echo ${OPTARG} | tr '[A-Z]' '[a-z]')
|
||||||
|
case "${opt}" in
|
||||||
|
D)
|
||||||
|
MNIST_DATA_PATH=$OPTARG
|
||||||
|
;;
|
||||||
|
d)
|
||||||
|
DOCKER=$OPTARG
|
||||||
|
;;
|
||||||
|
t)
|
||||||
|
if [ "$OPTARG" == "arm64" ] || [ "$OPTARG" == "x86" ]; then
|
||||||
|
TARGET=$OPTARG
|
||||||
|
else
|
||||||
|
echo "No such target " $OPTARG
|
||||||
|
display_usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
r)
|
||||||
|
TARBALL=$OPTARG
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Unknown option ${opt}!"
|
||||||
|
display_usage
|
||||||
|
exit 1
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
checkopts "$@"
|
||||||
|
if [ "$MNIST_DATA_PATH" == "" ]; then
|
||||||
|
echo "MNIST Dataset directory path was not provided"
|
||||||
|
display_usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$TARBALL" == "" ]; then
|
||||||
|
file=$(ls ../../../../output/mindspore-lite-*-runtime-${TARGET}-cpu-train.tar.gz)
|
||||||
|
if [ -f ${file} ]; then
|
||||||
|
TARBALL=${file}
|
||||||
|
else
|
||||||
|
echo "release.tar.gz was not found"
|
||||||
|
display_usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Prepare the model
|
||||||
|
cd model/ || exit 1
|
||||||
|
rm -f *.ms
|
||||||
|
./prepare_model.sh $DOCKER || exit 1
|
||||||
|
cd ../
|
||||||
|
|
||||||
|
# Copy the .ms model to the package folder
|
||||||
|
|
||||||
|
PACKAGE=package-${TARGET}
|
||||||
|
|
||||||
|
rm -rf ${PACKAGE}
|
||||||
|
mkdir -p ${PACKAGE}/model
|
||||||
|
cp model/*.ms ${PACKAGE}/model
|
||||||
|
|
||||||
|
# Copy the running script to the package
|
||||||
|
cp scripts/*.sh ${PACKAGE}/
|
||||||
|
|
||||||
|
# Copy the shared MindSpore ToD library
|
||||||
|
tar -xzf ${TARBALL} --wildcards --no-anchored libmindspore-lite.so
|
||||||
|
tar -xzf ${TARBALL} --wildcards --no-anchored include
|
||||||
|
mv mindspore-*/lib ${PACKAGE}/
|
||||||
|
rm -rf msl
|
||||||
|
mkdir msl
|
||||||
|
mv mindspore-*/* msl/
|
||||||
|
rm -rf mindspore-*
|
||||||
|
|
||||||
|
# Copy the dataset to the package
|
||||||
|
cp -r ${MNIST_DATA_PATH} ${PACKAGE}/dataset
|
||||||
|
|
||||||
|
echo "==========Compiling============"
|
||||||
|
make TARGET=${TARGET}
|
||||||
|
|
||||||
|
# Copy the executable to the package
|
||||||
|
mv bin ${PACKAGE}/ || exit 1
|
||||||
|
|
||||||
|
if [ "${TARGET}" == "arm64" ]; then
|
||||||
|
echo "=======Pushing to device======="
|
||||||
|
adb push ${PACKAGE} /data/local/tmp/
|
||||||
|
|
||||||
|
echo "========Training on Device====="
|
||||||
|
adb shell "cd /data/local/tmp/package-arm64 && /system/bin/sh train.sh"
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "===Evaluating trained Model====="
|
||||||
|
adb shell "cd /data/local/tmp/package-arm64 && /system/bin/sh eval.sh"
|
||||||
|
echo
|
||||||
|
else
|
||||||
|
cd ${PACKAGE} || exit 1
|
||||||
|
echo "======Training Locally========="
|
||||||
|
./train.sh
|
||||||
|
|
||||||
|
echo "===Evaluating trained Model====="
|
||||||
|
./eval.sh
|
||||||
|
cd ..
|
||||||
|
fi
|
||||||
|
|
@ -0,0 +1,41 @@
|
|||||||
|
BASE_DIR=$(realpath ../../../../)
|
||||||
|
APP:=bin/net_runner
|
||||||
|
MSLIB:=mindspore-lite
|
||||||
|
MSDIR:=$(realpath package-$(TARGET)/lib)
|
||||||
|
|
||||||
|
SRC:=src/net_runner.cc src/dataset.cc
|
||||||
|
OBJ:=$(SRC:.cc=.o)
|
||||||
|
|
||||||
|
CFLAGS := -Ofast -std=c++17 \
|
||||||
|
-I . \
|
||||||
|
-I ./msl \
|
||||||
|
-I ./msl/third_party/flatbuffers/include
|
||||||
|
|
||||||
|
|
||||||
|
ifeq ($(TARGET),arm64)
|
||||||
|
CXX := ${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++
|
||||||
|
CFLAGS += --target=aarch64-none-linux-android21 --gcc-toolchain=${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fdata-sections -ffunction-sections
|
||||||
|
LDFLAGS := --target=aarch64-none-linux-android21 --gcc-toolchain=${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot -Wl,--gc-sections
|
||||||
|
LDFLAGS += -L$(MSDIR) -l$(MSLIB) -pthread -llog -latomic -lm
|
||||||
|
else
|
||||||
|
CFLAGS += -g
|
||||||
|
LDFLAGS := -L$(MSDIR) -l$(MSLIB) -lpthread -Wl,-rpath,$(MSDIR)
|
||||||
|
endif
|
||||||
|
LD := ${CXX}
|
||||||
|
|
||||||
|
|
||||||
|
all:$(APP)
|
||||||
|
|
||||||
|
$(APP): $(OBJ) $(MSDIR)/lib$(MSLIB).so
|
||||||
|
@mkdir -p bin
|
||||||
|
$(LD) $(OBJ) $(LDFLAGS) -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf src/*.o bin/
|
||||||
|
|
||||||
|
|
||||||
|
mrproper:
|
||||||
|
rm -rf dataset package* msl src/*.o bin/ model/*.mindir model/*.ms
|
||||||
|
|
||||||
|
%.o:%.cc
|
||||||
|
$(CXX) $(CFLAGS) -c $< -o $@
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,34 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ ! -f "efficient_net_b0.ckpt" ]; then
|
||||||
|
echo "Pretrained model weights are missing, downloading efficient_net_b0.ckpt"
|
||||||
|
wget https://download.mindspore.cn/model_zoo/official/lite/efficient_net/efficient_net_b0.ckpt
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "============Exporting=========="
|
||||||
|
if [ -n "$1" ]; then
|
||||||
|
DOCKER_IMG=$1
|
||||||
|
docker run -w $PWD --runtime=nvidia -v /home/$USER:/home/$USER --privileged=true ${DOCKER_IMG} /bin/bash -c "python transfer_learning_export.py; chmod 444 transfer_learning_tod.mindir; rm -rf __pycache__"
|
||||||
|
else
|
||||||
|
echo "MindSpore docker was not provided, attempting to run locally"
|
||||||
|
python transfer_learning_export.py
|
||||||
|
fi
|
||||||
|
|
||||||
|
CONVERTER="../../../build/tools/converter/converter_lite"
|
||||||
|
if [ ! -f "$CONVERTER" ]; then
|
||||||
|
if ! command -v converter_lite &> /dev/null
|
||||||
|
then
|
||||||
|
tar -xzf ../../../../../output/mindspore-lite-*-converter-ubuntu-train.tar.gz --strip-components 2 --wildcards --no-anchored converter_lite libmindspore_gvar.so
|
||||||
|
if [ -f ./converter_lite ]; then
|
||||||
|
CONVERTER=./converter_lite
|
||||||
|
else
|
||||||
|
echo "converter_lite could not be found in MindSpore build directory nor in system path"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
CONVERTER=converter_lite
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "============Converting========="
|
||||||
|
LD_LIBRARY_PATH=./ $CONVERTER --fmk=MINDIR --trainModel=true --modelFile=transfer_learning_tod.mindir --outputFile=transfer_learning_tod
|
@ -0,0 +1,34 @@
|
|||||||
|
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
# ============================================================================
|
||||||
|
"""train_utils."""
|
||||||
|
|
||||||
|
import mindspore.nn as nn
|
||||||
|
from mindspore.common.parameter import ParameterTuple
|
||||||
|
|
||||||
|
def TrainWrap(net, loss_fn=None, optimizer=None, weights=None):
|
||||||
|
"""
|
||||||
|
TrainWrap
|
||||||
|
"""
|
||||||
|
if loss_fn is None:
|
||||||
|
loss_fn = nn.SoftmaxCrossEntropyWithLogits(reduction='mean')
|
||||||
|
loss_net = nn.WithLossCell(net, loss_fn)
|
||||||
|
loss_net.set_train()
|
||||||
|
if weights is None:
|
||||||
|
weights = ParameterTuple(net.trainable_params())
|
||||||
|
if optimizer is None:
|
||||||
|
optimizer = nn.Adam(weights, learning_rate=1e-3, beta1=0.9, beta2=0.999, eps=1e-8,
|
||||||
|
use_locking=False, use_nesterov=False, weight_decay=0.0, loss_scale=1.0)
|
||||||
|
train_net = nn.TrainOneStepCell(loss_net, optimizer)
|
||||||
|
return train_net
|
@ -0,0 +1,63 @@
|
|||||||
|
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
# ============================================================================
|
||||||
|
"""transfer_learning_export."""
|
||||||
|
|
||||||
|
import mindspore as M
|
||||||
|
from mindspore.nn import Cell
|
||||||
|
from mindspore.train.serialization import load_checkpoint
|
||||||
|
from mindspore.common.parameter import ParameterTuple
|
||||||
|
from mindspore.train.serialization import export
|
||||||
|
from effnet import effnet
|
||||||
|
import numpy as np
|
||||||
|
from train_utils import TrainWrap
|
||||||
|
|
||||||
|
|
||||||
|
class TransferNet(Cell):
|
||||||
|
def __init__(self, backbone, head):
|
||||||
|
super().__init__(TransferNet)
|
||||||
|
self.backbone = backbone
|
||||||
|
self.head = head
|
||||||
|
|
||||||
|
def construct(self, x):
|
||||||
|
x = self.backbone(x)
|
||||||
|
x = self.head(x)
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
BACKBONE = effnet(num_classes=1000)
|
||||||
|
load_checkpoint("efficient_net_b0.ckpt", BACKBONE)
|
||||||
|
HEAD = M.nn.Dense(1000, 10)
|
||||||
|
HEAD.weight.set_data(M.Tensor(np.random.normal(
|
||||||
|
0, 0.1, HEAD.weight.data.shape).astype("float32")))
|
||||||
|
HEAD.bias.set_data(M.Tensor(np.zeros(HEAD.bias.data.shape, dtype="float32")))
|
||||||
|
|
||||||
|
n = TransferNet(BACKBONE, HEAD)
|
||||||
|
|
||||||
|
trainable_weights_list = []
|
||||||
|
trainable_weights_list.extend(n.head.trainable_params())
|
||||||
|
trainable_weights = ParameterTuple(trainable_weights_list)
|
||||||
|
|
||||||
|
M.context.set_context(mode=M.context.PYNATIVE_MODE,
|
||||||
|
device_target="GPU", save_graphs=False)
|
||||||
|
BATCH_SIZE = 32
|
||||||
|
X = M.Tensor(np.ones((BATCH_SIZE, 3, 224, 224)), M.float32)
|
||||||
|
label = M.Tensor(np.zeros([BATCH_SIZE, 10]).astype(np.float32))
|
||||||
|
|
||||||
|
sgd = M.nn.SGD(trainable_weights, learning_rate=0.01, momentum=0.9,
|
||||||
|
dampening=0.01, weight_decay=0.0, nesterov=False, loss_scale=1.0)
|
||||||
|
net = TrainWrap(n, optimizer=sgd, weights=trainable_weights)
|
||||||
|
export(net, X, label, file_name="transfer_learning_tod.mindir", file_format='MINDIR')
|
||||||
|
|
||||||
|
print("Exported")
|
@ -0,0 +1,122 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
display_usage()
|
||||||
|
{
|
||||||
|
echo -e "\nUsage: prepare_and_run.sh -D dataset_path [-d mindspore_docker] [-r release.tar.gz] [-t arm64|x86]\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
checkopts()
|
||||||
|
{
|
||||||
|
TARGET="arm64"
|
||||||
|
DOCKER=""
|
||||||
|
PLACES_DATA_PATH=""
|
||||||
|
while getopts 'D:d:r:t:' opt
|
||||||
|
do
|
||||||
|
OPTARG=$(echo ${OPTARG} | tr '[A-Z]' '[a-z]')
|
||||||
|
case "${opt}" in
|
||||||
|
D)
|
||||||
|
PLACES_DATA_PATH=$OPTARG
|
||||||
|
;;
|
||||||
|
d)
|
||||||
|
DOCKER=$OPTARG
|
||||||
|
;;
|
||||||
|
t)
|
||||||
|
if [ "$OPTARG" == "arm64" ] || [ "$OPTARG" == "x86" ]; then
|
||||||
|
TARGET=$OPTARG
|
||||||
|
else
|
||||||
|
echo "No such target " $OPTARG
|
||||||
|
display_usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
r)
|
||||||
|
TARBALL=$OPTARG
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Unknown option ${opt}!"
|
||||||
|
display_usage
|
||||||
|
exit 1
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
checkopts "$@"
|
||||||
|
if [ "$PLACES_DATA_PATH" == "" ]; then
|
||||||
|
echo "Places Dataset directory path was not provided"
|
||||||
|
display_usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$TARBALL" == "" ]; then
|
||||||
|
file=$(ls ../../../../output/mindspore-lite-*-runtime-${TARGET}-cpu-train.tar.gz)
|
||||||
|
if [ -f ${file} ]; then
|
||||||
|
TARBALL=${file}
|
||||||
|
else
|
||||||
|
echo "release.tar.gz was not found"
|
||||||
|
display_usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Prepare the model
|
||||||
|
cd model/ || exit 1
|
||||||
|
rm -f *.ms
|
||||||
|
./prepare_model.sh $DOCKER || exit 1
|
||||||
|
cd ../
|
||||||
|
|
||||||
|
# Copy the .ms model to the package folder
|
||||||
|
|
||||||
|
PACKAGE=package-${TARGET}
|
||||||
|
|
||||||
|
rm -rf ${PACKAGE}
|
||||||
|
mkdir -p ${PACKAGE}/model
|
||||||
|
cp model/*.ms ${PACKAGE}/model
|
||||||
|
|
||||||
|
# Copy the running script to the package
|
||||||
|
cp scripts/*.sh ${PACKAGE}/
|
||||||
|
|
||||||
|
# Copy the shared MindSpore ToD library
|
||||||
|
tar -xzf ${TARBALL} --wildcards --no-anchored libmindspore-lite.so
|
||||||
|
tar -xzf ${TARBALL} --wildcards --no-anchored include
|
||||||
|
mv mindspore-*/lib ${PACKAGE}/
|
||||||
|
rm -rf msl
|
||||||
|
mkdir msl
|
||||||
|
mv mindspore-*/* msl/
|
||||||
|
rm -rf mindspore-*
|
||||||
|
|
||||||
|
# Convert the dataset into the package
|
||||||
|
./prepare_dataset.sh ${PLACES_DATA_PATH}
|
||||||
|
cp -r dataset ${PACKAGE}
|
||||||
|
|
||||||
|
echo "==========Compiling============"
|
||||||
|
make TARGET=${TARGET}
|
||||||
|
|
||||||
|
# Copy the executable to the package
|
||||||
|
mv bin ${PACKAGE}/ || exit 1
|
||||||
|
|
||||||
|
if [ "${TARGET}" == "arm64" ]; then
|
||||||
|
echo "=======Pushing to device======="
|
||||||
|
adb push ${PACKAGE} /data/local/tmp/
|
||||||
|
|
||||||
|
echo "==Evaluating Untrained Model==="
|
||||||
|
adb shell "cd /data/local/tmp/package-arm64 && /system/bin/sh eval_untrained.sh"
|
||||||
|
|
||||||
|
echo "========Training on Device====="
|
||||||
|
adb shell "cd /data/local/tmp/package-arm64 && /system/bin/sh train.sh"
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "===Evaluating trained Model====="
|
||||||
|
adb shell "cd /data/local/tmp/package-arm64 && /system/bin/sh eval.sh"
|
||||||
|
echo
|
||||||
|
else
|
||||||
|
cd ${PACKAGE} || exit 1
|
||||||
|
echo "==Evaluating Untrained Model==="
|
||||||
|
./eval_untrained.sh
|
||||||
|
|
||||||
|
echo "======Training Locally========="
|
||||||
|
./train.sh
|
||||||
|
|
||||||
|
echo "===Evaluating trained Model====="
|
||||||
|
./eval.sh
|
||||||
|
cd ..
|
||||||
|
fi
|
||||||
|
|
@ -0,0 +1,19 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo "=======Preparing Dataset======="
|
||||||
|
[ -d "dataset" ] && echo "dataset was already created" && exit
|
||||||
|
echo "Preparing dataset"
|
||||||
|
PLACES_DATA_PATH=$1
|
||||||
|
class_id=0
|
||||||
|
classes=("4" "98" "6" "7" "10" "15" "17" "70" "26" "30")
|
||||||
|
for class in "${classes[@]}"; do
|
||||||
|
mkdir -p dataset/$class_id
|
||||||
|
i=0
|
||||||
|
cat scripts/places365_val.txt | grep -w ${class} | awk '{print $1}' | while read line
|
||||||
|
do
|
||||||
|
echo converting ${PLACES_DATA_PATH}/val_256/$line to bmp
|
||||||
|
convert -colorspace RGB -gravity center -crop '224x224+0+0' ${PLACES_DATA_PATH}/val_256/$line dataset/$class_id/$i.bmp;
|
||||||
|
i=$(($i+1));
|
||||||
|
done
|
||||||
|
class_id=$(($class_id+1))
|
||||||
|
done
|
@ -0,0 +1,17 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
# ============================================================================
|
||||||
|
|
||||||
|
LD_LIBRARY_PATH=./lib/ bin/net_runner -f model/transfer_learning_tod_trained.ms -e 0 -d dataset
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue