def get_model_resnet50_v2(classes_num, ctx): pretrained_net = models.resnet50_v2(pretrained=True) # print(pretrained_net) finetune_net = models.resnet50_v2(classes=classes_num) # 输出为classes_num个类 finetune_net.features = pretrained_net.features # 特征设置为resnet50_v2的特征 finetune_net.output.initialize(init.Xavier(), ctx=ctx) # 对输出层做初始化 finetune_net.collect_params().reset_ctx(ctx) # 设置CPU或GPU finetune_net.hybridize() # gluon特征,运算转成符号运算,提高运行速度 return finetune_net
def __init__(self, fname, compressed_dim): self._ctx = mx.gpu(config.gpu_id) if config.use_gpu else mx.cpu(0) self._resnet50 = vision.resnet50_v2(pretrained=True, ctx=self._ctx) self._compressed_dim = compressed_dim self._cell_size = [4, 16] self.penalty = [0., 0.] self.min_cell_size = np.min(self._cell_size)
def __init__(self, **kwargs): super().__init__(**kwargs) with self.name_scope(): self.features = models.resnet50_v2(pretrained=True).features # self.features = models.densenet121(pretrained=True) self.features.collect_params().setattr('lr_mult', 0.1) self.output = nn.Dense(units=101)
def predict_with_models_from_gluon_model_zoo_example(): # Gluon model zoo provides multiple pre-trained powerful models. # We can download and load a pre-trained ResNet-50 V2 model that was trained on the ImageNet dataset. net = models.resnet50_v2(pretrained=True) # Download and load the text labels for each class. url = 'http://data.mxnet.io/models/imagenet/synset.txt' fname = download(url) with open(fname, 'r') as f: text_labels = [' '.join(l.split()[1:]) for l in f] # Randomly pick a dog image from Wikipedia as a test image, download and read it. url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/Golden_Retriever_medium-to-light-coat.jpg/365px-Golden_Retriever_medium-to-light-coat.jpg' fname = download(url) x = image.imread(fname) # Use the image processing functions provided in the MXNet image module. x = image.resize_short(x, 256) x, _ = image.center_crop(x, (224, 224)) plt.imshow(x.asnumpy()) plt.show() def transform(data): data = data.transpose((2, 0, 1)).expand_dims(axis=0) rgb_mean = nd.array([0.485, 0.456, 0.406]).reshape((1, 3, 1, 1)) rgb_std = nd.array([0.229, 0.224, 0.225]).reshape((1, 3, 1, 1)) return (data.astype('float32') / 255 - rgb_mean) / rgb_std prob = net(transform(x)).softmax() idx = prob.topk(k=5)[0] for i in idx: i = int(i.asscalar()) print('With prob = %.5f, it contains %s' % (prob[0, i].asscalar(), text_labels[i]))
def main(): net = models.resnet50_v2(pretrained=True) url = 'http://data.mxnet.io/models/imagenet/synset.txt' fname = download(url) with open(fname, 'r') as f: text_labels = [' '.join(l.split()[1:]) for l in f] url2 = 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/\ Golden_Retriever_medium-to-light-coat.jpg/\ 365px-Golden_Retriever_medium-to-light-coat.jpg' fname2 = download(url2) x = image.imread(fname2) x = image.resize_short(x, 256) x, _ = image.center_crop(x, (224, 224)) plt.imshow(x.asnumpy()) plt.show() prob = net(transform(x)).softmax() idx = prob.topk(k=5)[0] for i in idx: i = int(i.asscalar()) print('With prob = %.5f, it contains %s' % (prob[0, i].asscalar(), text_labels[i]))
def __init__(self, fname, compressed_dim,config=otb_deep_config.OTBDeepConfig()): super(ResNet50Feature,self).__init__(config) self._ctx = mx.gpu(gpu_config.gpu_id) if gpu_config.use_gpu else mx.cpu(0) self._resnet50 = vision.resnet50_v2(pretrained=True, ctx = self._ctx) self._compressed_dim = compressed_dim self._cell_size = [4, 16] self.penalty = [0., 0.] self.min_cell_size = np.min(self._cell_size)
def get_net(ctx, centroid = None): finetune_net = models.resnet50_v2(pretrained = True, ctx = ctx) # features and output seem to be seperated. finetune_net.output_new = nn.HybridSequential(prefix = '') # finetune_net.output_new.add(nn.InstanceNorm()) finetune_net.output_new.add(L2norm()) finetune_net.output_new.add(nn.Dense(2356)) finetune_net.output_new.initialize(init.Xavier(),ctx = ctx) # finetune_net.output_new.add(nn.Dense(persons, activation = None, use_bias = False, weight_initializer = initializer.load(centroid))) finetune_net.collect_params().reset_ctx(ctx) return finetune_net
def __init__(self, is_color, img_sample_sz=[], size_mode='same'): super().__init__(is_color) use_for_color = settings.cnn_params.get('useForColor', True) use_for_gray = settings.cnn_params.get('useForGray', True) self.use_feature = (use_for_color and is_color) or (use_for_gray and not is_color) self.net = vision.resnet50_v2(pretrained=True, ctx=mx.cpu(0)) self.compressed_dim = settings.cnn_params['compressed_dim'] self.cell_size = np.array([4, 16]) self.penalty = np.zeros((2, 1)) self.nDim = np.array([64, 1024]) self.img_sample_sz = self._set_size(img_sample_sz, size_mode) self.data_sz = np.ceil(self.img_sample_sz / self.cell_size[:, None])
def __init__(self, args, ctx): super(ResidualNet, self).__init__() self.args = args resnet = resnet50_v2(pretrained=True, ctx=ctx, root=args.pretrained_models_dir) self.features = resnet.features self.classifier = nn.Sequential() with self.classifier.name_scope(): self.classifier.add( # nn.Flatten(), nn.Dense(2048, in_units=2048, activation='relu'), nn.Dropout(0.5), nn.Dense(2048, in_units=2048, activation='relu'), nn.Dropout(0.5), nn.Dense(2, in_units=2048))
def convertToMxNetAndBack(fileName, outputFileName, inputs): from mxnet.gluon.model_zoo import vision import mxnet.contrib.onnx as onnx_mxnet import mxnet ret = None if "resnet" in fileName: model = vision.resnet50_v2(pretrained=True) model.hybridize() ret = [model(mxnet.nd.array(inputs[0][0]))] model.export("mxnet-model") sym = "./mxnet-model-symbol.json" arg = "./mxnet-model-0000.params" else: sym, arg, aux = onnx_mxnet.import_model(fileName) onnx_mxnet.export_model(sym, arg, getOnnxInputShapes(fileName), np.float32, outputFileName) return ret
def main(): random.seed() mx.random.seed(random.randint(0, 100000)) logging.info("random seed set") epoch_size = max(int(args.num_examples / args.batch_size), 1) lr_schedule = [epoch_size * x for x in [30, 60, 90]] lr_scheduler = mx.lr_scheduler.MultiFactorScheduler(step=lr_schedule, factor=0.1) train_data_begin = mx.io.ImageRecordIter( path_imgrec=os.path.join(args.data_dir, "train_480_q90.rec"), label_width=1, data_name='data', label_name='softmax_label', data_shape=(3, 224, 224), batch_size=args.batch_size, pad=0, fill_value=127, # only used when pad is valid rand_crop=True, max_random_scale=1.0, # 480 with imagnet min_random_scale=0.533, # 256.0/480.0 max_aspect_ratio=0.25, random_h=36, # 0.4*90 random_s=50, # 0.4*127 random_l=50, # 0.4*127 max_rotate_angle=0, max_shear_ratio=0, rand_mirror=True, shuffle=True, preprocess_threads=32, prefetch_buffer=32) train_data_end = mx.io.ImageRecordIter( path_imgrec=os.path.join(args.data_dir, "train_256_q90.rec"), label_width=1, data_name='data', label_name='softmax_label', data_shape=(3, 224, 224), batch_size=args.batch_size, pad=0, fill_value=127, # only used when pad is valid rand_crop=True, # max_random_scale = 1.0, # 480 with imagnet # min_random_scale = 0.533, # 256.0/480.0 # max_aspect_ratio = 0.25, # random_h = 36, # 0.4*90 # random_s = 50, # 0.4*127 # random_l = 50, # 0.4*127 max_rotate_angle=0, max_shear_ratio=0, rand_mirror=True, shuffle=True, preprocess_threads=32, prefetch_buffer=32) val_data = mx.io.ImageRecordIter(path_imgrec=os.path.join( args.data_dir, "val_256_q90.rec"), label_width=1, data_name='data', label_name='softmax_label', batch_size=args.batch_size, data_shape=(3, 224, 224), rand_crop=False, rand_mirror=False, preprocess_threads=32, prefetch_buffer=32) ctx = [mx.gpu(int(i)) for i in args.gpus.split(',')] net = vision.resnet50_v2(ctx=ctx) net.initialize(mx.init.Xavier(magnitude=2), ctx=ctx) if args.optim == 'adam': trainer = gluon.Trainer(net.collect_params(), args.optim, { 'learning_rate': args.lr, 'wd': args.wd, 'beta1': 0.9, 'beta2': 0.999, 'epsilon': 1e-08, 'lr_scheduler': lr_scheduler }, kvstore='device') else: trainer = gluon.Trainer(net.collect_params(), args.optim, { 'learning_rate': args.lr, 'wd': args.wd, 'momentum': 0.9, 'lr_scheduler': lr_scheduler }, kvstore='device') metric_top1 = mx.metric.Accuracy(name='top_1_accuracy') metric_topk = mx.metric.TopKAccuracy(top_k=5, name='top_5_accuracy') metric = mx.metric.create([metric_top1, metric_topk]) loss = gluon.loss.SoftmaxCrossEntropyLoss() top1trains = [] top5trains = [] top1vals = [] top5vals = [] if not os.path.exists("./models"): os.mkdir("./models") if not os.path.exists("./results"): os.mkdir("./results") train_data = train_data_begin for epoch in range(args.epochs): if epoch == 95: train_data = train_data_end tic = time.time() train_data.reset() metric.reset() btic = time.time() for i, batch in enumerate(train_data): data = gluon.utils.split_and_load(batch.data[0], ctx_list=ctx, batch_axis=0) label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0) outputs = [] Ls = [] with ag.record(): for x, y in zip(data, label): z = net(x) L = loss(z, y) # store the loss and do backward after we have done forward # on all GPUs for better speed on multiple GPUs. Ls.append(L) outputs.append(z) for L in Ls: L.backward() trainer.step(batch.data[0].shape[0]) metric.update(label, outputs) if args.frequent and not (i + 1) % args.frequent: name, acc = metric.get() logging.info( 'Epoch[{}] Batch [{}]\tSpeed: {} samples/sec\t{}={}\t{}={}' .format(epoch, i, int(args.batch_size / (time.time() - btic)), name[0], round(acc[0], 6), name[1], round(acc[1], 6))) btic = time.time() name, acc = metric.get() logging.info('[Epoch %d] training top1: %s=%f' % (epoch, name[0], acc[0])) logging.info('[Epoch %d] training top5: %s=%f' % (epoch, name[1], acc[1])) logging.info('[Epoch %d] time cost: %f' % (epoch, time.time() - tic)) name, val_acc = test(ctx, val_data, net) logging.info('[Epoch %d] validation top1: %s=%f' % (epoch, name[0], val_acc[0])) logging.info('[Epoch %d] validation top5: %s=%f' % (epoch, name[1], val_acc[1])) top1trains.append(acc[0]) top5trains.append(acc[1]) top1vals.append(val_acc[0]) top5vals.append(val_acc[1]) if epoch % 10 == 0: filename = 'models/real50-{}-epoch-{}-lr-{}-wd-{}-mom0.params'.format( args.optim, epoch, args.lr, args.wd) net.save_params(filename) f = open( './results/real50-{}-lr-{}-wd-{}-mom0'.format( args.optim, args.lr, args.wd), 'wb') pickle.dump([top1trains, top5trains, top1vals, top5vals], f) f.close()
f.imshow(x.reshape((28, 28)).asnumpy()) ax = f.axes ax.set_title(text_labels[yi] + '\n' + text_labels[pyi]) ax.title.set_fontsize(20) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show() # predict with models from gluon model zoo from mxnet.gluon.model_zoo import vision as models from mxnet.gluon.utils import download from mxnet import image net = models.resnet50_v2(pretrained=True) url = 'http://data.mxnet.io/models/imagenet/synset.txt' fname = download(url) with open(fname, 'r') as f: text_labels = [' '.join(l.split()[1]) for l in f] url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/Golden_Retriever_medium-to-light-coat.jpg/365px-Golden_Retriever_medium-to-light-coat.jpg' fname = download(url) x = image.imread(fname) x = image.resize_short(x, 256) x, _ = image.center_crop(x, (224, 224)) plt.imshow(x.asnumpy()) plt.show()
# 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 mxnet as mx # Convert gluon model to a static model from mxnet.gluon.model_zoo import vision import time batch_shape = (1, 3, 224, 224) input_data = mx.nd.zeros(batch_shape) resnet_gluon = vision.resnet50_v2(pretrained=True) resnet_gluon.hybridize() resnet_gluon.forward(input_data) resnet_gluon.export('resnet50_v2') resnet_sym, arg_params, aux_params = mx.model.load_checkpoint('resnet50_v2', 0) # Load the model into nGraph as a static graph model = resnet_sym.simple_bind(ctx=mx.cpu(), data=batch_shape, grad_req='null') model.copy_params_from(arg_params, aux_params) # To test the model's performance, we've provided this helpful code snippet # customizable dry_run = 5 num_batches = 100 for i in range(dry_run + num_batches):
from mxnet.gluon import nn from mxnet.gluon.model_zoo import vision as models res_net152=models.resnet50_v2(prefix="a_") res_net152.load_parameters("/home/sz/.mxnet/models/resnet50_v2-81a4e66a.params") net=nn.HybridSequential(prefix="a_") for layer in res_net152.features[0:12]: with net.name_scope(): net.add(layer) net.add(nn.Dense(7,activation="relu")) net.add(nn.Dense(1))
################################################ categories = dataset_train.synsets NUM_CLASSES = len(categories) print(categories) print(NUM_CLASSES) ################################################ ################################################ # Model creation ################################################ # get pretrained squeezenet net = models.resnet50_v2(pretrained=True, prefix='kavisar',ctx=ctx) my_net = models.resnet50_v2(prefix='kavisar', classes=NUM_CLASSES, ctx=ctx) my_net.collect_params().initialize(ctx=ctx) my_net.features = net.features ################################################# # Evaluation accuracy ################################################# def evaluate_accuracy_gluon(data_iterator, net): num_instance = nd.zeros(1, ctx=ctx) sum_metric = nd.zeros(1,ctx=ctx, dtype=np.int32) for i, (data, label) in enumerate(data_iterator): data = data.astype(np.float32).as_in_context(ctx) label = label.astype(np.int32).as_in_context(ctx)
# Output bucket output_bucket = os.environ.get("OUTPUT_BUCKET") # Download resource from resource bucket resource_bucket = os.environ.get("RESOURCE_BUCKET") # Lambda has no GPU support label_classes = "synset.txt" model_params = "resnet50_v2.params" label_file = "/tmp/{}".format(label_classes) model_file = "/tmp/{}".format(model_params) s3.Bucket(resource_bucket).download_file(label_classes, label_file) s3.Bucket(resource_bucket).download_file(model_params, model_file) model_ctx = mx.cpu() net = models.resnet50_v2(pretrained=False) net.load_parameters(model_file, ctx=model_ctx) net.hybridize() with open(label_file, "r") as f: labels = [' '.join(l.split()[1:]) for l in f] def transform_image(img_path): img = image.imread(img_path) data = image.resize_short(img, 256) data, _ = image.center_crop(data, (224, 224)) data = data.transpose((2, 0, 1)).expand_dims(axis=0) rgb_mean = nd.array([0.485, 0.456, 0.406]).reshape((1, 3, 1, 1)) rgb_std = nd.array([0.229, 0.224, 0.225]).reshape((1, 3, 1, 1)) data = (data.astype("float32") / 255 - rgb_mean) / rgb_std
def main(): train_data = mx.io.ImageRecordIter( path_imgrec=os.path.join(args.data_dir, "train_256_q90.rec"), label_width=1, data_name='data', label_name='softmax_label', data_shape=(3, 224, 224), batch_size=args.batch_size, pad=0, fill_value=127, # only used when pad is valid rand_crop=True, # max_random_scale = 1.0, # 480 with imagnet # min_random_scale = 0.533, # 256.0/480.0 # max_aspect_ratio = 0.25, # random_h = 36, # 0.4*90 # random_s = 50, # 0.4*127 # random_l = 50, # 0.4*127 max_rotate_angle=0, max_shear_ratio=0, rand_mirror=True, shuffle=True, preprocess_threads=32, prefetch_buffer=32) val_data = mx.io.ImageRecordIter(path_imgrec=os.path.join( args.data_dir, "val_256_q90.rec"), label_width=1, data_name='data', label_name='softmax_label', batch_size=args.batch_size, data_shape=(3, 224, 224), rand_crop=False, rand_mirror=False, preprocess_threads=32, prefetch_buffer=32) ctx = [mx.gpu(int(i)) for i in args.gpus.split(',')] net = vision.resnet50_v2(ctx=ctx) model_list = [ 'resnet50-adam-epoch-50-lr-0.001-wd-1e-05.params', 'resnet50-sgd-epoch-50-lr-0.1-wd-0.0001.params', 'resnet50-signum-epoch-50-lr-0.0001-wd-1e-05.params' ] if not os.path.exists("./results"): os.mkdir("./results") for model in model_list: logging.info("loading model: " + model) net.load_params('models/' + model, ctx=ctx) logging.info("model loaded") logging.info("testing model for sanity") name, val_acc = test(ctx, val_data, net) logging.info('[Epoch %d] validation top1: %s=%f' % (50, name[0], val_acc[0])) logging.info('[Epoch %d] validation top5: %s=%f' % (50, name[1], val_acc[1])) logging.info("finished sanity check, reloading model: " + model) net.load_params('models/' + model, ctx=ctx) logging.info("model reloaded") w_mean, w_var, grad_samples = gradient_utils.welfordGradient( ctx, train_data, net) dim = w_mean.size l1_sq = np.linalg.norm(w_mean, ord=1)**2 l2_sq = np.linalg.norm(w_mean, ord=2)**2 total_var = np.sum(w_var) w_sig = np.sqrt(w_var) l1_sig = np.linalg.norm(w_sig, ord=1)**2 l2_sig = np.linalg.norm(w_sig, ord=2)**2 logging.info("\ndim: {}".format(dim)) logging.info("norm ratio: {}".format(l1_sq / l2_sq)) logging.info("var: {}".format(total_var)) logging.info("sigma ratio: {}\n".format(l1_sig / l2_sig)) f = open('./results/imagenet+{}'.format(model), 'wb') pickle.dump( [dim, l1_sq, l2_sq, total_var, l1_sig, l2_sig, grad_samples], f) f.close()
from mxnet.gluon.model_zoo import vision from mxnet.gluon.utils import download from mxnet import image from mxnet import nd import matplotlib.pyplot as plt import sys net = vision.resnet50_v2(pretrained=True) with open('./synset.txt', 'r', encoding='utf8') as f: text_labels = [l[9:].strip() for l in f] def transform(data): data = data.transpose((2, 0, 1)).expand_dims(axis=0) rgb_mean = nd.array([0.485, 0.456, 0.406]).reshape((1, 3, 1, 1)) rgb_std = nd.array([0.229, 0.224, 0.225]).reshape((1, 3, 1, 1)) return (data.astype('float32') / 255 - rgb_mean) / rgb_std def classify(image_file_path): x = image.imread(image_file_path) x = image.resize_short(x, 256) x, _ = image.center_crop(x, (224, 224)) plt.imshow(x.asnumpy()) plt.show() prob = net(transform(x)).softmax() idx = prob.topk(k=5)[0] print(' prob | name') print(' ------------------') for i in idx: i = int(i.asscalar())
import mxnet as mx from mxnet.gluon.model_zoo import vision resnet = vision.resnet50_v2(pretrained=True, ctx=ctx) resnet.save_parameters('resnetparams')
def check_error(gluon_output, pytorch_output, epsilon=1e-5): pytorch_output = pytorch_output.data.numpy() gluon_output = gluon_output.asnumpy() error = np.max(pytorch_output - gluon_output) print('Error:', error) assert error < epsilon return error if __name__ == '__main__': print('Test resnet50_v2:') from mxnet.gluon.model_zoo import vision as models net = models.resnet50_v2(classes=20) # Make sure it's hybrid and initialized net.hybridize() net.collect_params().initialize() pytorch_model = gluon2pytorch(net, [(1, 3, 224, 224)], dst_dir=None, pytorch_module_name='resnet50_v2') pytorch_model.eval() input_np = np.random.uniform(0, 1, (1, 3, 224, 224)) gluon_output = net(mx.nd.array(input_np)) pytorch_output = pytorch_model(torch.FloatTensor(input_np)) check_error(gluon_output, pytorch_output)
def resnet50mxnetload(): net = vision.resnet50_v2(pretrained=True) net.hybridize() return net