示例#1
0
from train_utils import save_inout, train_wrap
from official.cv.xception.src.Xception import Xception
import mindspore.common.dtype as mstype
from mindspore import context, Tensor, nn
from mindspore.train.serialization import export

context.set_context(mode=context.PYNATIVE_MODE,
                    device_target="GPU",
                    save_graphs=False)

n = Xception(num_classes=1000)
n.dropout = nn.Dropout(keep_prob=1.0)

loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
optimizer = nn.SGD(n.trainable_params(),
                   learning_rate=0.01,
                   momentum=0.9,
                   dampening=0.0,
                   weight_decay=0.0,
                   nesterov=True,
                   loss_scale=1.0)
net = train_wrap(n, loss_fn, optimizer)

batch = 2
x = Tensor(np.random.randn(batch, 3, 299, 299), mstype.float32)
label = Tensor(np.zeros([batch, 1000]).astype(np.float32))
export(net, x, label, file_name="mindir/xception_train", file_format='MINDIR')

if len(sys.argv) > 1:
    save_inout(sys.argv[1] + "xception", x, label, n, net)
示例#2
0
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""vgg_train_export."""

import sys
import numpy as np
from train_utils import save_inout, train_wrap
from official.cv.vgg16.src.vgg import vgg16
import mindspore.common.dtype as mstype
from mindspore import context, Tensor, nn
from mindspore.train.serialization import export

context.set_context(mode=context.PYNATIVE_MODE,
                    device_target="GPU",
                    save_graphs=False)

batch = 2

n = vgg16(num_classes=10)
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
optimizer = nn.Momentum(n.trainable_params(), 0.01, 0.9, use_nesterov=False)
net = train_wrap(n, loss_fn, optimizer)

x = Tensor(np.random.randn(batch, 3, 224, 224), mstype.float32)
label = Tensor(np.zeros([batch, 10]).astype(np.float32))
export(net, x, label, file_name="mindir/vgg_train", file_format='MINDIR')

if len(sys.argv) > 1:
    save_inout(sys.argv[1] + "vgg", x, label, n, net)
示例#3
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.
# ============================================================================
"""effnet_train_export."""

import sys
import numpy as np
from train_utils import save_inout, train_wrap
from effnet import effnet
import mindspore.common.dtype as mstype
from mindspore import context, Tensor, nn
from mindspore.train.serialization import export

context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU", save_graphs=False)

n = effnet(num_classes=10)
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
optimizer = nn.SGD(n.trainable_params(), learning_rate=0.01, momentum=0.9, dampening=0.0, weight_decay=0.0,
                   nesterov=True, loss_scale=1.0)
net = train_wrap(n, loss_fn, optimizer)

x = Tensor(np.random.randn(2, 3, 224, 224), mstype.float32)
label = Tensor(np.zeros([2, 10]).astype(np.float32))
export(net, x, label, file_name="mindir/effnet_train", file_format='MINDIR')

if len(sys.argv) > 1:
    save_inout(sys.argv[1] + "effnet", x, label, n, net)
示例#4
0
import numpy as np
from train_utils import save_inout, train_wrap
from official.cv.lenet.src.lenet import LeNet5
import mindspore.common.dtype as mstype
from mindspore import context, Tensor, nn
from mindspore.train.serialization import export

context.set_context(mode=context.PYNATIVE_MODE,
                    device_target="GPU",
                    save_graphs=False)

n = LeNet5()
loss_fn = nn.MSELoss()
optimizer = nn.Adam(n.trainable_params(),
                    learning_rate=1e-2,
                    beta1=0.5,
                    beta2=0.7,
                    eps=1e-2,
                    use_locking=True,
                    use_nesterov=False,
                    weight_decay=0.0,
                    loss_scale=0.3)
net = train_wrap(n, loss_fn, optimizer)

x = Tensor(np.random.randn(32, 1, 32, 32), mstype.float32)
label = Tensor(np.zeros([32, 10]).astype(np.float32))
export(net, x, label, file_name="mindir/lenet_train", file_format='MINDIR')

if len(sys.argv) > 1:
    save_inout(sys.argv[1] + "lenet", x, label, n, net, sparse=False)
示例#5
0
                    device_target="GPU",
                    save_graphs=False)

# Mini alexnet is designed for MNIST data
batch = 2
number_of_classes = 10
n = AlexNet(phase='test')

loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
optimizer = nn.Adam(n.trainable_params(),
                    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)
net = train_wrap(n, loss_fn, optimizer)

x = Tensor(np.ones([batch, 1, 32, 32]).astype(np.float32) * 0.01)
label = Tensor(np.zeros([batch, number_of_classes]).astype(np.float32))
export(net,
       x,
       label,
       file_name="mindir/mini_alexnet_train",
       file_format='MINDIR')

if len(sys.argv) > 1:
    save_inout(sys.argv[1] + "mini_alexnet", x, label, n, net, sparse=False)
示例#6
0
import sys
import numpy as np
from train_utils import save_inout, train_wrap
from official.cv.googlenet.src.googlenet import GoogleNet
import mindspore.common.dtype as mstype
from mindspore import context, Tensor, nn
from mindspore.train.serialization import export

context.set_context(mode=context.PYNATIVE_MODE,
                    device_target="GPU",
                    save_graphs=False)

n = GoogleNet(num_classes=10)
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
optimizer = nn.SGD(n.trainable_params(),
                   learning_rate=0.01,
                   momentum=0.9,
                   dampening=0.0,
                   weight_decay=5e-4,
                   nesterov=True,
                   loss_scale=0.9)
net = train_wrap(n, loss_fn, optimizer)

batch = 2
x = Tensor(np.random.randn(batch, 3, 224, 224), mstype.float32)
label = Tensor(np.zeros([batch, 10]).astype(np.float32))
export(net, x, label, file_name="mindir/googlenet_train", file_format='MINDIR')

if len(sys.argv) > 1:
    save_inout(sys.argv[1] + "googlenet", x, label, n, net)
示例#7
0
import sys
import numpy as np
from train_utils import save_inout, train_wrap
from NetworkInNetwork import NiN
import mindspore.common.dtype as mstype
from mindspore import context, Tensor, nn
from mindspore.train.serialization import export

context.set_context(mode=context.PYNATIVE_MODE,
                    device_target="GPU",
                    save_graphs=False)

n = NiN(num_classes=10)
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
optimizer = nn.SGD(n.trainable_params(),
                   learning_rate=0.01,
                   momentum=0.9,
                   dampening=0.0,
                   weight_decay=5e-4,
                   nesterov=True,
                   loss_scale=0.9)
net = train_wrap(n, loss_fn, optimizer)

batch = 2
x = Tensor(np.random.randn(batch, 3, 32, 32), mstype.float32)
label = Tensor(np.zeros([batch]).astype(np.int32))
export(net, x, label, file_name="mindir/nin_train", file_format='MINDIR')

if len(sys.argv) > 1:
    save_inout(sys.argv[1] + "nin", x, label, n, net)
示例#8
0
from train_utils import save_inout, train_wrap
import mindspore.common.dtype as mstype
from mindspore import context, Tensor, nn
from mindspore.train.serialization import export
from src.network.densenet import DenseNet121
sys.path.append(os.environ['CLOUD_MODEL_ZOO'] + 'official/cv/densenet121/')
#pylint: disable=wrong-import-position

context.set_context(mode=context.PYNATIVE_MODE,
                    device_target="GPU",
                    save_graphs=False)

n = DenseNet121(num_classes=10)
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
optimizer = nn.SGD(n.trainable_params(),
                   learning_rate=0.001,
                   momentum=0.9,
                   dampening=0.0,
                   weight_decay=0.0,
                   nesterov=True,
                   loss_scale=0.9)
net = train_wrap(n, loss_fn, optimizer)

batch = 2
x = Tensor(np.random.randn(batch, 3, 224, 224), mstype.float32)
label = Tensor(np.zeros([batch, 10]).astype(np.float32))
export(net, x, label, file_name="mindir/densenet_train", file_format='MINDIR')

if len(sys.argv) > 1:
    save_inout(sys.argv[1] + "densenet", x, label, n, net)
示例#9
0
# 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.
# ============================================================================
"""shufflenetv2_train_export."""

import sys
import numpy as np
from train_utils import save_inout, train_wrap
from official.cv.shufflenetv2.src.shufflenetv2 import ShuffleNetV2
import mindspore.common.dtype as mstype
from mindspore import context, Tensor, nn
from mindspore.train.serialization import export

context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU", save_graphs=False)

n = ShuffleNetV2(n_class=10)
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
optimizer = nn.Momentum(n.trainable_params(), 0.01, 0.9, use_nesterov=False)

net = train_wrap(n, loss_fn, optimizer)

batch = 2
x = Tensor(np.random.randn(batch, 3, 224, 224), mstype.float32)
label = Tensor(np.zeros([batch, 10]).astype(np.float32))
export(net, x, label, file_name="mindir/shufflenetv2_train", file_format='MINDIR')

if len(sys.argv) > 1:
    save_inout(sys.argv[1] + "shufflenetv2", x, label, n, net)
示例#10
0
from train_utils import save_inout, train_wrap
from official.cv.mobilenetv2.src.mobilenetV2 import MobileNetV2Backbone, MobileNetV2Head, mobilenet_v2
import mindspore.common.dtype as mstype
from mindspore import context, Tensor, nn
from mindspore.train.serialization import export

context.set_context(mode=context.PYNATIVE_MODE,
                    device_target="GPU",
                    save_graphs=False)
batch = 16

backbone_net = MobileNetV2Backbone()
head_net = MobileNetV2Head(input_channel=backbone_net.out_channels,
                           num_classes=10)
n = mobilenet_v2(backbone_net, head_net)

loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
optimizer = nn.Momentum(n.trainable_params(), 0.01, 0.9, use_nesterov=False)
net = train_wrap(n, loss_fn, optimizer)

x = Tensor(np.random.randn(batch, 3, 224, 224), mstype.float32)
label = Tensor(np.zeros([batch, 10]).astype(np.float32))
export(net,
       x,
       label,
       file_name="mindir/mobilenetv2_train",
       file_format='MINDIR')

if len(sys.argv) > 1:
    save_inout(sys.argv[1] + "mobilenetv2", x, label, n, net, sparse=False)
示例#11
0
from mindspore import context, Tensor, nn
from mindspore.train.serialization import export

context.set_context(mode=context.PYNATIVE_MODE,
                    device_target="GPU",
                    save_graphs=False)

n = MobileNetV1(10)
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
optimizer = nn.SGD(n.trainable_params(),
                   learning_rate=1e-2,
                   momentum=0.9,
                   dampening=0.1,
                   weight_decay=0.0,
                   nesterov=False,
                   loss_scale=1.0)
net = train_wrap(n, loss_fn, optimizer)

batch = 2
x = Tensor(np.random.randn(batch, 3, 224, 224), mstype.float32)
label = Tensor(np.zeros([batch, 10]).astype(np.float32))

export(net,
       x,
       label,
       file_name="mindir/mobilenetv1_train",
       file_format='MINDIR')

if len(sys.argv) > 1:
    save_inout(sys.argv[1] + "mobilenetv1", x, label, n, net)