def vgg(cfg, batch_norm=False, ctx=None): vggnet = vision.vgg16(pretrained=True, ctx=ctx) pretrained_layers = list(vggnet.features) cur = 0 layers = nn.HybridSequential() i = 0 while i < len(cfg): v = cfg[i] if v == 'M': with layers.name_scope(): layers.add(nn.MaxPool2D(pool_size=2, strides=2)) i += 1 elif v == 'C': with layers.name_scope(): layers.add(nn.MaxPool2D(pool_size=2, strides=2, ceil_mode=True)) i += 1 elif isinstance(pretrained_layers[cur], nn.Conv2D): conv2d = pretrained_layers[cur] with layers.name_scope(): layers.add(conv2d) layers.add(nn.Activation('relu')) i += 1 cur += 1 with layers.name_scope(): layers.add(nn.MaxPool2D(pool_size=3, strides=1, padding=1), nn.Conv2D(1024, kernel_size=3, padding=6, dilation=6), nn.Activation('relu'), nn.Conv2D(1024, kernel_size=1), nn.Activation('relu')) layers.hybridize() return layers
def init_vgg(self, cfg, ctx): with self.name_scope(): vggnet = vision.vgg16(pretrained=True, ctx=ctx) pretrained_layers = list(vggnet.features) cur = 0 i = 0 while i < len(cfg): v = cfg[i] if v == 'M': with self.name_scope(): self.vgg.add(nn.MaxPool2D(pool_size=2, strides=2)) i += 1 elif v == 'C': with self.name_scope(): self.vgg.add( nn.MaxPool2D(pool_size=2, strides=2, ceil_mode=True)) i += 1 elif isinstance(pretrained_layers[cur], nn.Conv2D): #conv2d = pretrained_layers[cur] with self.name_scope(): self.vgg.add(nn.Conv2D(v, 3, padding=1)) self.vgg.add(nn.Activation('relu')) i += 1 cur += 1 with self.vgg.name_scope(): self.vgg.add(nn.MaxPool2D(pool_size=3, strides=1, padding=1), nn.Conv2D(1024, kernel_size=3, padding=6, dilation=6), nn.Activation('relu'), nn.Conv2D(1024, kernel_size=1), nn.Activation('relu')) self.vgg.hybridize()
def get_pretrained_model(self, ctx): # TODO: make sure end with which layer pretrained_net = models.vgg16(pretrained=True, ctx=ctx) feature_extractor = nn.HybridSequential() for layer in pretrained_net.features[:30]: feature_extractor.add(layer) return feature_extractor
def test_collect_qparams(): net = vgg16(pretrained=True) convert_model(net) qparams_init(net) print("collect_qparams: ", collect_qparams(net)) print("") print_all_qparams(net)
def __init__(self, fname, compressed_dim): self._ctx = mx.gpu(config.gpu_id) if config.use_gpu else mx.cpu(0) self._vgg16 = vision.vgg16(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) self._avg_pool2d = AvgPool2D()
def __init__(self, fname, compressed_dim,config=otb_deep_config.OTBDeepConfig()): super(VGG16Feature,self).__init__(config) self._ctx = mx.gpu(gpu_config.gpu_id) if gpu_config.use_gpu else mx.cpu(0) self._vgg16 = vision.vgg16(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) self._avg_pool2d = AvgPool2D()
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.net = vision.vgg16(pretrained=True) self.pool2d = AvgPool2D() self.use_feature = (use_for_color and is_color) or (use_for_gray and not is_color) self.nDim = np.array( [64, 512]) #[96 512] net["info"]["dataSize"][layer_dim_ind, 2] self.cell_size = np.array([4, 16]) self.penalty = np.zeros((2, 1)) self.compressed_dim = settings.cnn_params['compressed_dim'] 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 set_network(args): style_layers = [4, 9, 16] # Pixel2pixel networks # netG = UnetGenerator(in_channels=3, num_downs=8) net_label = label_Discriminator(in_channels=1, use_sigmoid=False) netG = STE() netD = Discriminator(in_channels=6, use_sigmoid=False) netvgg = models.vgg16(pretrained=True) net = get_net(netvgg, style_layers) net.collect_params().reset_ctx(ctx) # net.collect_params().setattr('grad_req', 'null') # Initialize parameters netG.initialize(ctx=ctx, init=init.Xavier()) if args.model: netG.collect_params().load(args.model, ctx=ctx) netG.collect_params().reset_ctx(ctx) network_init(netD) net_label.initialize(ctx=ctx, init=mx.initializer.One()) net_label.collect_params().setattr('grad_req', 'null') net_label.collect_params().reset_ctx(ctx) # net.collect_params().setattr('grad_req', 'null') # net.collect_params().reset_ctx(ctx) # trainerG = gluon.Trainer(netG.collect_params(), 'adam', {'learning_rate': args.lr, 'beta': args.beta}) # trainerD = gluon.Trainer(netD.collect_params(), 'adam', {'learning_rate': args.lr, 'beta': args.beta}) # trainerV = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': args.lr, 'beta': args.beta}) # trainerL = gluon.Trainer(net_label.collect_params(), 'adam', {'learning_rate': args.lr, 'beta': args.beta}) trainerG = gluon.Trainer(netG.collect_params(), 'adam', {'learning_rate': args.lr}) trainerD = gluon.Trainer(netD.collect_params(), 'adam', {'learning_rate': args.lr}) trainerV = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': args.lr}) trainerL = gluon.Trainer(net_label.collect_params(), 'adam', {'learning_rate': args.lr}) return netG, netD, net, net_label, trainerG, trainerD, trainerV, trainerL
from mxnet.gluon.model_zoo import vision Net = vision.vgg16(pretrained=False)
def get_contents(net, image_shape, content_layers, style_layers): content_x = preprocess(content_img, image_shape) content_y, _ = extract_features(content_x, net, content_layers, style_layers) return content_x, content_y def get_styles(net, image_shape, content_layers, style_layers): style_x = preprocess(style_img, image_shape) _, style_y = extract_features(style_x, net, content_layers, style_layers) style_y = [gram(y) for y in style_y] return style_x, style_y pretrained_net = models.vgg16(pretrained=True) print(pretrained_net) style_layers = [0, 5, 10, 17, 24] content_layers = [21] net = get_net(pretrained_net, content_layers, style_layers) channels = [net[l].weight.shape[0] for l in style_layers] style_weights = [1e4 / n**2 for n in channels] #1e4 content_weights = [1] tv_weight = 10 def train(x, max_epochs, lr, lr_decay_epoch=200): tic = time() for i in range(max_epochs):
def vgg16mxnetload(): net = vision.vgg16(pretrained=True) net.hybridize() return net
def __init__(self, model_ctx=mx.cpu()): self.model_ctx = model_ctx self.image_net = models.vgg16(pretrained=True) self.image_net.collect_params().reset_ctx(ctx=model_ctx)
def get_kernel(params): return vision.vgg16(pretrained=False)
def init_vgg_params(self): for l1, l2 in zip(self.vgg, vision.vgg16(pretrained=True).features[:-4]): if isinstance(l1, nn.Conv2D): l1.weight.set_data(l2.weight.data()) l1.bias.set_data(l2.bias.data())