def get_resnet_symbol(num_res):
    resnet = resnet18_v1(pretrained=True)
    resnet1 = resnet.features
    data = mx.sym.Variable("data")
    out = resnet1(data)
    all_layers = out.get_internals()
    sym0 = all_layers[43]
    sym1 = all_layers[84]
    sym2 = all_layers[125]
    sym3 = all_layers[166]
    resnet_symbol = mx.sym.Group([sym0, sym1, sym2, sym3])
    length_prefix = len(resnet.name + '_')
    return resnet_symbol[:num_res], length_prefix
Beispiel #2
0
    def _model(self):
        gpu_count = mx.context.num_gpus()
        ctx = mx.gpu() if gpu_count > 0 else mx.cpu()

        if self.ml_model_name and self.ml_model_name.split(
                '_')[0] == 'resnet18':
            model = vision.resnet18_v1(classes=len(self.LABELS), ctx=ctx)
        else:
            model = vision.mobilenet_v2_1_0(classes=len(self.LABELS), ctx=ctx)
        model_path = Path(self.ml_model.path) / Path(
            os.listdir(self.ml_model.path)[0])
        model.load_parameters(str(model_path), ctx=ctx)

        def get_value(img: Array3D) -> Tuple[Union[str, Tuple], float]:
            data = img.reshape((1, ) + img.shape)
            data = mx.nd.array(data)

            scores = model(mx.nd.array(self.img_transforms(data),
                                       ctx=ctx)).softmax().asnumpy()
            val = self.LABELS[int(np.argmax(scores, axis=1)[0])]
            prob = scores[0][int(np.argmax(scores, axis=1)[0])]
            return val, prob

        return get_value
    def compile_model(self):
        if device == 'vta':
            self.remote = rpc.connect(self.pynq_addr, 9091)
            vta.reconfig_runtime(self.remote)
            vta.program_fpga(self.remote, bitstream=None)
        else:
            self.remote = rpc.LocalSession()

        self.ctx = self.remote.ext_dev(
            0) if device == 'vta' else self.remote.cpu(0)

        # Load pre-configured AutoTVM schedules
        with autotvm.tophub.context(target):

            # Populate the shape and data type dictionary for ResNet input
            dtype_dict = {'data': 'float32'}
            shape_dict = {'data': (env.BATCH, 3, 224, 224)}

            gluon_model = vision.resnet18_v1(
                pretrained=True, ctx=ctx
            ).features if args.nonsplit else splitnet.resnet18_v1_split(
                self.id + 1)

            # Measure build start time
            build_start = time.time()

            # Start front end compilation
            mod, params = relay.frontend.from_mxnet(gluon_model, shape_dict)

            # Update shape and type dictionary
            shape_dict.update({k: v.shape for k, v in params.items()})
            dtype_dict.update({k: str(v.dtype) for k, v in params.items()})

            # Perform quantization in Relay
            with relay.quantize.qconfig(global_scale=8.0,
                                        skip_conv_layers=[0]):
                relay_prog = relay.quantize.quantize(mod['main'],
                                                     params=params)

            # Perform graph packing and constant folding for VTA target
            if target.device_name == 'vta':
                assert env.BLOCK_IN == env.BLOCK_OUT
                relay_prog = graph_pack(relay_prog,
                                        env.BATCH,
                                        env.BLOCK_OUT,
                                        env.WGT_WIDTH,
                                        start_name=start_pack,
                                        stop_name=stop_pack)

            # Compile Relay program with AlterOpLayout disabled
            with relay.build_config(opt_level=3,
                                    disabled_pass={'AlterOpLayout'}):
                if target.device_name != 'vta':
                    graph, lib, params = relay.build(
                        relay_prog,
                        target=target,
                        params=params,
                        target_host=env.target_host)
                else:
                    with vta.build_config():
                        graph, lib, params = relay.build(
                            relay_prog,
                            target=target,
                            params=params,
                            target_host=env.target_host)

            self.params = params

            # Measure Relay build time
            build_time = time.time() - build_start
            print(f'inference graph for thread {self.id} built in {0:.4f}s!'.
                  format(build_time))

            # Send the inference library over to the remote RPC server
            temp = util.tempdir()
            lib.save(temp.relpath('graphlib.o'))
            self.remote.upload(temp.relpath('graphlib.o'))
            lib = self.remote.load_module('graphlib.o')

            # Graph runtime
            self.m = graph_runtime.create(graph, lib, self.ctx)
        ans.append('{}: {:.2f}%'.format(category,
                                        probability.asscalar() * 100))

    return ans


# parse command line arguments
parser = argparse.ArgumentParser(
    description='live webcam demo for split ResNet')
parser.add_argument('--nonsplit', action='store_true', default=False)
args = parser.parse_args()

ctx = mx.cpu()
# get dense layer
if args.nonsplit:
    dense = vision.resnet18_v1(pretrained=True, ctx=ctx).output
else:
    dense = gluon.nn.Dense(1000)
    dense.load_parameters('params/dense-1.params', ctx=ctx)

#get categories for imagenet
categories = np.array(json.load(open('image_net_labels.json', 'r')))

assert tvm.module.enabled('rpc')
# Load VTA parameters from the vta/config/vta_config.json file
env = vta.get_env()

# device, `vta` or `cpu`
device = 'vta'
target = env.target if device == 'vta' else env.target_vta_cpu
ctx = mx.cpu(store.local_rank) if args.no_cuda else mx.gpu(store.local_rank)

# Load the training data
train_data = gluon.data.DataLoader(gluon.data.vision.CIFAR10(train=True,
                                   transform=transform), args.batch_size,
                                   sampler=SplitSampler(50000,
                                                        num_workers,
                                                        store.rank))

# Load the test data
test_data = gluon.data.DataLoader(gluon.data.vision.CIFAR10(train=False,
                                  transform=transform),
                                  args.batch_size, shuffle=False)

# Load ResNet18 model from GluonCV model zoo
net = vision.resnet18_v1()

# Initialize the parameters with Xavier initializer
net.initialize(mx.init.Xavier(), ctx=ctx)

# Use Adam optimizer. Ask trainer to use the distributor kv store.
trainer = gluon.Trainer(net.collect_params(), optimizer='adam',
                        optimizer_params={'learning_rate': args.lr},
                        kvstore=store)

train_metric = mx.gluon.metric.Accuracy()

# Run as many epochs as required
for epoch in range(args.epochs):
    tic = time.time()
    train_metric.reset()
Beispiel #6
0
##################
# Hyperparameter #
#----------------#
ctx = mx.cpu()
lr=0.05
epochs=10
momentum=0.9
batch_size=64
#----------------#
# Hyperparameter #
##################

################## model
from mxnet.gluon.model_zoo import vision
net = vision.resnet18_v1(classes=10, pretrained=False, ctx=ctx)
# net = vision.resnet34_v1(classes=10, pretrained=False, ctx=ctx)
# net = vision.resnet50_v1(classes=10, pretrained=False, ctx=ctx)
# net = vision.resnet101_v1(classes=10, pretrained=False, ctx=ctx)
# net = vision.resnet152_v1(classes=10, pretrained=False, ctx=ctx)


################## 그래프
import gluoncv
inputShape = (1,3,224,224)
gluoncv.utils.viz.plot_network(net, shape=inputShape)


##### 전처리 ##############################################
def transformer(data, label):
    data = mx.image.imresize(data, 224, 224)
  return train_transform, val_transform


def get_imagenet_iterator(root, batch_size, num_workers, data_shape=224, dtype='float32'):
  """Dataset loader with preprocessing."""
  train_dir = os.path.join(root, 'train')
  train_transform, val_transform = get_imagenet_transforms(data_shape, dtype)
  logging.info("Loading image folder %s, this may take a bit long...", train_dir)
  train_dataset = ImageFolderDataset(train_dir, transform=train_transform)
  train_data = DataLoader(train_dataset, batch_size, shuffle=True,
                          last_batch='discard', num_workers=num_workers)
  
  val_dir = os.path.join(root, 'val')
  if not os.path.isdir(os.path.expanduser(os.path.join(root, 'val', 'n01440764'))):
    user_warning = 'Make sure validation images are stored in one subdir per category, a helper script is available at https://git.io/vNQv1'
    raise ValueError(user_warning)
  
  logging.info("Loading image folder %s, this may take a bit long...", val_dir)
  val_dataset = ImageFolderDataset(val_dir, transform=val_transform)
  val_data = DataLoader(val_dataset, batch_size, last_batch='keep', num_workers=num_workers)
  return DataLoaderIter(train_data, dtype), DataLoaderIter(val_data, dtype)


resnet18 = vision.resnet18_v1(pretrained=True)
alexnet = vision.alexnet(pretrained=True)
inception = vision.inception_v3(pretrained=True)
#squeezenet = vision.squeezenet1_0()
#densenet = vision.densenet_161()

get_imagenet_iterator("c:\\data\\images", batch_size, num_workers, data_shape=224, dtype='float32'):
import mxnet as mx
import time
import gluoncv

from mxnet import nd, autograd
from mxnet import gluon
from mxnet.gluon import nn

inputShape = (1, 3, 224, 224)

from mxnet.gluon.model_zoo import vision

alexnet = vision.alexnet()
inception = vision.inception_v3()

resnet18v1 = vision.resnet18_v1()
resnet18v2 = vision.resnet18_v2()
squeezenet = vision.squeezenet1_0()
densenet = vision.densenet121()
mobilenet = vision.mobilenet0_5()

############### 그래프 ###############
import gluoncv
gluoncv.utils.viz.plot_network(resnet18v1, shape=inputShape)
#####################################
Beispiel #9
0
import json

import matplotlib.pyplot as plt
import mxnet as mx
from mxnet import gluon, nd
from mxnet.gluon.model_zoo import vision
import numpy as np

ctx = mx.cpu()

densenet121 = vision.densenet121(pretrained=True, ctx=ctx)
mobileNet = vision.mobilenet0_5(pretrained=True, ctx=ctx)
resnet18 = vision.resnet18_v1(pretrained=True, ctx=ctx)

mx.test_utils.download(
    'https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/doc/tutorials/onnx/image_net_labels.json'
)
categories = np.array(json.load(open('image_net_labels.json', 'r')))

# filename = mx.test_utils.download('https://github.com/dmlc/web-data/blob/master/mxnet/doc/tutorials/onnx/images/dog.jpg?raw=true', fname='dog.jpg')
filename = 'scorp.jpg'

image = mx.image.imread(filename)
plt.imshow(image.asnumpy())
plt.show()

# Read the image: this will return a NDArray shaped as (image height, image width, 3), with the three channels in RGB order.
# Resize the shorter edge of the image 224.
# Crop, using a size of 224x224 from the center of the image.
# Shift the mean and standard deviation of our color channels to match the ones of the dataset the network has been trained on.
# Transpose the array from (Height, Width, 3) to (3, Height, Width).
Beispiel #10
0
# Load the training data
cifar10_data = gluon.data.vision.CIFAR10(train=True, transform=transform)
train_data = gluon.data.DataLoader(cifar10_data,
                                   batch_size,
                                   sampler=SplitSampler(
                                       len(cifar10_data), store.num_workers,
                                       store.rank))

# Load the test data
test_data = gluon.data.DataLoader(gluon.data.vision.CIFAR10(
    train=False, transform=transform),
                                  batch_size,
                                  shuffle=False)

# Use ResNet from model zoo
net = vision.resnet18_v1(pretrained=False, ctx=ctx)

# Initialize the parameters with Xavier initializer
net.collect_params().initialize(mx.init.Xavier(), ctx=ctx)

# SoftmaxCrossEntropy is the most common choice of loss function for multiclass classification
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()

# Use Adam optimizer. Ask trainer to use the distributer kv store.
trainer = gluon.Trainer(net.collect_params(),
                        'adam', {'learning_rate': .001},
                        kvstore=store)


# Evaluate accuracy of the given network using the given data
def evaluate_accuracy(data_iterator, net):
Beispiel #11
0
# img, label = batch.data[0][3*i+j], batch.label[0][3*i+j]
# img = img.transpose((1,2,0)) * RGB_MEAN + RGB_STD
# img = img.clip(0,255).asnumpy()/255
# fig = figs[i][j]
# fig.imshow(img)
# rect = box_to_rect(label[0][1:5]*DATA_SHAPE , 'red', 2)
# fig.add_patch(rect)
# fig.axes.get_xaxis().set_visible(False)
# fig.axes.get_yaxis().set_visible(False)
# plt.show()

# Step 2: 定义网络模型

# In[106]:

pretrained = vision.resnet18_v1(pretrained=True).features
net = nn.HybridSequential()
for i in range(8):
    net.add(pretrained[i])
net.add(nn.GlobalAvgPool2D())
Dense_layer = nn.Dense(units=4)
Dense_layer.initialize()
net.add(Dense_layer)

#net.load_parameters('models/model_5.params')
net

# In[112]:

#check_result:
test_input = batch.data[0]
Beispiel #12
0
def transform(data, label, augs):
    data = data.astype('float32')
    for aug in augs:
        data = aug(data)
    data = nd.transpose(data, (2, 0, 1))
    return data, nd.array([label]).asscalar().astype('float32')


train_imgs = gluon.data.vision.ImageFolderDataset(
    data_dir + '/food/train',
    transform=lambda X, y: transform(X, y, train_augs))
test_imgs = gluon.data.vision.ImageFolderDataset(
    data_dir + '/food/test', transform=lambda X, y: transform(X, y, test_augs))

pretrained_net = models.resnet18_v1(pretrained=True)

finetune_net = models.resnet18_v1(classes=2)
finetune_net.features = pretrained_net.features
finetune_net.output.initialize(init.Xavier())


def train(net, ctx, batch_size=64, epochs=1, learning_rate=0.01, wd=0.001):
    train_data = gluon.data.DataLoader(train_imgs, batch_size, shuffle=True)
    test_data = gluon.data.DataLoader(test_imgs, batch_size)
    # 确保net的初始化在ctx上
    net.collect_params().reset_ctx(ctx)
    net.hybridize()
    loss = gluon.loss.SoftmaxCrossEntropyLoss()
    # 训练
    trainer = gluon.Trainer(net.collect_params(), 'sgd', {
    image.CenterCropAug((32,32))
]



batch_size = 64
learning_rate=.1
num_epochs = 50
randseed = 4
ctx = utils.try_all_gpus()

loss = gluon.loss.SoftmaxCrossEntropyLoss()
train_data, test_data, data_num = get_data('D:/lagBear/S***N/finally_data/cnn_data.mat',randseed,batch_size=batch_size,train_augs=train_augs)


pre_net = models.resnet18_v1(ctx = ctx,pretrained=True,prefix = 'sperm_3t2class_')
pre_net.output
pre_net.features[0].weight.data()[0][0]

net = models.resnet18_v1(classes=2,prefix = 'sperm_3t2class_',ctx = ctx)
net.features = pre_net.features
net.initialize(ctx=ctx)
net.output.initialize(init.Xavier())
net.hybridize()




trainer = gluon.Trainer(net.collect_params(),
                        'sgd', {'learning_rate': learning_rate})
Beispiel #14
0
        return iter(indices)

    def __len__(self):
        return self.part_len


# Load the training data
train_data = gluon.data.DataLoader(gluon.data.vision.CIFAR10(train=True, transform=transform), batch_size,
                                   sampler=SplitSampler(50000, store.num_workers, store.rank))

# Load the test data
test_data = gluon.data.DataLoader(gluon.data.vision.CIFAR10(train=False, transform=transform),
                                  batch_size, shuffle=False)

# Use ResNet from model zoo
net = vision.resnet18_v1()

# Initialize the parameters with Xavier initializer
net.collect_params().initialize(mx.init.Xavier(), ctx=ctx)

# SoftmaxCrossEntropy is the most common choice of loss function for multiclass classification
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()

# Use Adam optimizer. Ask trainer to use the distributor kv store.
trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': .001}, kvstore=store)


# Evaluate accuracy of the given network using the given data
def evaluate_accuracy(data_iterator, network):
    """ Measure the accuracy of ResNet
Beispiel #15
0
        mx.sym.Convolution(data=channels[i],
                           weight=skernel,
                           num_filter=1,
                           kernel=(3, 3),
                           pad=(1, 1),
                           no_bias=True,
                           stride=(1, 1)) for i in range(nchannel)
    ])
    kernel = mx.nd.array(
        np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]).reshape(
            (1, 1, 3, 3)), ctx) / 8.0
    out = out * tv_weight
    return out.bind(ctx, args={"img": img, "kernel": kernel})


resnet = resnet18_v1(pretrained=True)

flag = 'vgg'  #args.net
if flag == 'vgg':
    vgg_symbol = symbol.descriptor_symbol(args.num_res)
    pretrained = mx.nd.load(VGGPATH)
    arg_names = vgg_symbol.list_arguments()
elif flag == 'resnet':
    resnet_symbol, length_prefix = symbol.get_resnet_symbol(
        resnet, args.num_res)
    pretrained = mx.nd.load('../resnet18.params')
    arg_names = resnet_symbol.list_arguments()
    aux_names = resnet_symbol.list_auxiliary_states()
    aux_dict = {}
    for name in aux_names:
        if name == "data":