def get_model_vgg19(classes_num, ctx): pretrained_net = models.vgg19(pretrained=True) # print(pretrained_net) finetune_net = models.vgg19(classes=classes_num) # 输出为classes_num个类 finetune_net.features = pretrained_net.features # 特征设置为inceptionv3的特征 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 get_net(style_layers): ctx = mx.gpu(0) vgg16 = models.vgg19(pretrained=True) net = nn.Sequential() for i in range(max(style_layers)+1): net.add(vgg16.features[i]) # net.collect_params().reset_ctx(ctx) return net
def __init__(self): vgg19_model = models.vgg19(pretrained=False) vgg19_model.load_params("model/vgg19.params", ctx=mx.cpu(0)) # pre-trained net is in cpu self.use_cuda = True self.mean = nd.array([0.485, 0.456, 0.406]) self.std = nd.array([0.229, 0.224, 0.225]) self.ctx = mx.gpu(0) self.model = self.get_model(vgg19_model) self.smooth = 0.5
def __init__(self, context) -> None: self.vgg19 = vision.vgg19(pretrained=True, ctx=context) self.vgg_layer = 22 self._l1 = L1Loss()
def __init__(self, context) -> None: # self.resizer_224 = mx.gluon.data.vision.transforms.Resize(224) self.vgg19 = vision.vgg19(pretrained=True, ctx=context) self.vgg_layer = 22 self._l1 = L1Loss()
def preprocess(img, image_shape): img = image.imresize(img, *image_shape) img = (img.astype('float32')/255 - rgb_mean) / rgb_std return img.transpose((2,0,1)).expand_dims(axis=0) def postprocess(img): img = img[0].as_in_context(rgb_std.context) img = (img.transpose((1,2,0))*rgb_std + rgb_mean).clip(0,1) return img, cv2.cvtColor(np.uint8(img.asnumpy()*255),cv2.COLOR_RGB2BGR) from mxnet.gluon.model_zoo import vision as models pretrained_net = models.vgg19(pretrained=True) pretrained_net style_layers = [16,19,21,28] #layer to learn style content_layers = [25] #layer to learn content from mxnet.gluon import nn #we need each output of layer so here build net with pre-trained layer def get_net(pretrained_net, content_layers, style_layers): net = nn.Sequential() for i in range(max(content_layers+style_layers)+1): #pdb.set_trace() print i,pretrained_net.features[i]
def get_styles(image_shape): style_x = preprocess(style_img, image_shape).copyto(ctx) _, style_y = extract_features(style_x, content_layers, style_layers) style_y = [gram(y) for y in style_y] return style_x, style_y style_img = image.imread('style1.jpg') content_img = image.imread('content.jpg') plt.imshow(style_img.asnumpy()) plt.show() plt.imshow(content_img.asnumpy()) plt.show() pretrained_net = models.vgg19(pretrained=True) style_layers = [0, 5, 10, 19, 28] content_layers = [25] net = get_net(pretrained_net, content_layers, style_layers) rgb_mean = nd.array([0.485, 0.456, 0.406]) rgb_std = nd.array([0.229, 0.224, 0.225]) channels = [net[l].weight.shape[0] for l in style_layers] style_weights = [1e4 / n**2 for n in channels] content_weights = [1] tv_weight = 10 a = len(content_img[0]) image_shape = (1440, int(len(content_img) * (1440 / len(content_img[0]))))
filters(num_kernels, e.g. [64, 128, 256, 512, 512]) to define a net. Utlize get_model_file(name, root) to download the trained model. Every time you run the code, it will redownload. So I run pretrained_net = models.vgg19(pretrained=True, ctx=mx.cpu(), root="/home/ly/mxnet/models") only once, in the transforming the content images, change this code like when use trained model: pretrained_net = models.vgg19(pretrained=False) pretrained_net.load_params(filename="/home/ly/mxnet/models/vgg19-f7134366.params", ctx=ctx) The arguments of vgg16(), vgg19() are: pretrained: Whether to load the pretrained weights for model. Here, is True. First, it will download the trained model, then load the pretrained model. ctx: Context, default CPU. ctx=mx.cpu() / mx.gpu(0) root: str, default '~/.mxnet/models'. Location of trained model. return net. ''' # neural style network pretrained_net = models.vgg19(pretrained=False) # Utlize class VGG + layers(11, 13, 16, 19. The number of conv layer) + # filters(num_kernels, e.g. [64, 128, 256, 512, 512]) to define a net. pretrained_net.load_params( filename="/home/ly/mxnet/models/vgg19-f7134366.params", ctx=mx.cpu()) style_layers = [0, 5, 10, 19, 28] content_layers = [25] # content_layers+style_layers is: [25,0,5,10,19,28]. max(content_layers+style_layers) is 28. net = get_net(pretrained_net, content_layers, style_layers) print(net)
def preprocess_vgginput(image): # normalized = mx.image.color_normalize(image, # mean=mx.nd.array([0.485, 0.456, 0.406]), # std=mx.nd.array([0.229, 0.224, 0.225])) # image1 = image / 255 image = color_normalize(image, mean=rgb_mean, std=rgb_std) # data = g.utils.split_and_load(normalized, ctx_list=ctx1, batch_axis=0) # image = mx.nd.transpose(image, (0, 2, 3, 1)) # image = [normalize_image(im) for im in image] # image = mx.nd.transpose(mx.nd.array(np.array(image)), (0, 3, 1, 2)) return image vgg19 = vision.vgg19(pretrained=True) vgg19.collect_params().reset_ctx(ctx=ctx1) x = mx.sym.var('data') y = vgg19(x) print('\n=== the symbolic program of net===') interals = y.get_internals() print(interals.list_outputs()) vgg19_relu5_4 = g.SymbolBlock([interals['vgg0_conv15_fwd_output']], x, params=vgg19.collect_params()) vgg19_relu5_4.hybridize() # d_net = g.SymbolBlock([interals['discriminator0_d_dense0_fwd_output']], x, params=d_net_sigm.collect_params()) # vgg19_relu5_4.collect_params().reset_ctx(ctx=ctx)
if opt.experiment is None: opt.experiment = 'samples' os.system('mkdir {0}'.format(opt.experiment)) if opt.gpu_ids == '-1': context = [mx.cpu()] else: context = [mx.gpu(int(i)) for i in opt.gpu_ids.split(',') if i.strip()] dummy_img = nd.random.uniform( 0, 1, (1, 3, int(opt.fineSize / 4), int(opt.fineSize / 4)), ctx=mx.gpu(0)) netG = SRGenerator() netD = SRDiscriminator() vgg19 = vision.vgg19(pretrained=True, ctx=context) features = vgg19.features[:28] netG.initialize(mx.initializer.Normal(), ctx=mx.gpu(0)) dummy_out = netG(dummy_img) weights_init(netG.collect_params()) netG.collect_params().reset_ctx(context) netD.initialize(ctx=mx.gpu(0)) netD(dummy_out) weights_init(netD.collect_params()) netD.collect_params().reset_ctx(context) dataset = DataSet( opt.dataroot, RandomCrop(opt.fineSize), transforms.Resize(int(opt.fineSize / 4), interpolation=3), transforms.Compose([
def vgg19mxnetload(): net = vision.vgg19(pretrained=True) net.hybridize() return net
tv_loss = beta * net.get_tv_loss(_img) loss = style_loss + content_loss + tv_loss loss.backward() trainer.step(1) print("-" * 30) print("tv_loss:", tv_loss, "\ncontent_loss:", content_loss, "\nstyle_loss:", style_loss, "\n迭代次数:", e, "\nloss:",loss) if e % 100 == 0: tool.save_img(output.data(), output_path + str(e) + ".png") tool.save_img(output.data(), output_path + "result.png") if __name__ == "__main__": # 加载vgg模型 vgg = vision.vgg19(ctx=ctx, pretrained=True, root=param) # 构建风格网络及内容网络 net = FeatureNet(vgg) # 读取图片 content_img = tool.read_img(input_path, ctx=ctx, size=size) style_img = tool.read_img(style_path, ctx=ctx, size=size) # 生成图片 style_transfer(net, content_img, style_img)
#求导的工具 import numpy as np ctx = mx.gpu(0) #指定数据模型存放的地方 rgb_mean = nd.array([0.485, 0.456, 0.406]) rgb_std = nd.array([0.229, 0.224, 0.225]) #vgg读图的均值方差 def preprocess(img, image_shape): img = image.imresize(img, *image_shape) img = (img.astype('float32')/255 - rgb_mean) / rgb_std return img.transpose((2,0,1)).expand_dims(axis=0) #vgg读图的预处理,直接用即可,不是我写的 vgg = models.vgg19(pretrained=True) #调用models拿到预训练的vgg def get_vgg(vgg): net = nn.Sequential()#构建神经网络的high level api用法 for i in range(37): net.add(vgg.features[i]) return net #我们需要用到vgg的前37层,挨个添加进来 class MyNet(nn.Block): def __init__(self, **kwargs):#初始化方法 super(MyNet, self).__init__(**kwargs) with self.name_scope(): #以上代码仿写即可,无需了解意思
def load_vgg_model_features(ctx_list, last_layer): vgg19 = vision.vgg19(pretrained=True, root='/root/.mxnet/models/', ctx=ctx_list) features = vgg19.features[:last_layer] return features