VGG19_LOSS 代码学习研究

 

import torchvision.models as models

PyTorch源码解读之torchvision.models

vgg19 制作vgg loss 代码解读

class Vgg19(nn.Module):
    def __init__(self, args, requires_grad=False):
        super(Vgg19, self).__init__()
        self.args = args
        self.vgg_pretrained_features = models.vgg19(pretrained=True).features
        #pretrained是true,要导入预训练模型
        print(self.vgg_pretrained_features)
        if not requires_grad:
            for param i
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VGG19图像风格迁移是一种通过深度学习实现图像风格转移的方法,其基于深度神经网络VGG19。其代码实现可以分为以下几步: 1. 导入必要的库:包括numpy、tensorflow等。 2. 加载预训练的VGG19模型:使用tensorflow的tf.keras.applications中的VGG19模型进行预训练。 3. 提取内容图片和风格图片的特征:使用预训练的VGG19模型提取内容图片和风格图片的特征。 4. 定义损失函数:将内容损失和风格损失加权求和作为总损失函数。 5. 使用优化器进行训练:通过梯度下降优化器来更新生成图片。 以下是简单的代码实现: ``` import numpy as np import tensorflow as tf from tensorflow.keras.applications import VGG19 from tensorflow.keras.preprocessing.image import load_img, img_to_array # 加载VGG19模型,去掉最后一层输出 vgg = VGG19(include_top=False, weights='imagenet') # 定义内容图片和风格图片的路径 content_path = 'content.jpg' style_path = 'style.jpg' # 加载图片并进行预处理 def load_and_process_img(path): img = load_img(path) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = tf.keras.applications.vgg19.preprocess_input(img) return img # 提取内容图片和风格图片的特征 def get_feature_representations(model, content_path, style_path): content_image = load_and_process_img(content_path) style_image = load_and_process_img(style_path) content_feature_maps = model.predict(content_image) style_feature_maps = model.predict(style_image) return content_feature_maps, style_feature_maps # 计算Gram矩阵 def gram_matrix(input_tensor): channels = int(input_tensor.shape[-1]) a = tf.reshape(input_tensor, [-1, channels]) n = tf.shape(a) gram = tf.matmul(a, a, transpose_a=True) return gram / tf.cast(n, tf.float32) # 定义内容损失 def get_content_loss(base_content, target): return tf.reduce_mean(tf.square(base_content - target)) # 定义风格损失 def get_style_loss(base_style, gram_target): return tf.reduce_mean(tf.square(gram_matrix(base_style) - gram_target)) # 定义总损失函数 def compute_loss(model, loss_weights, init_image, gram_style_features, content_features): style_weight, content_weight = loss_weights model_outputs = model(init_image) style_output_features = model_outputs[:len(gram_style_features)] content_output_features = model_outputs[len(gram_style_features):] style_score = 0 content_score = 0 weight_per_style_layer = 1.0 / float(len(style_output_features)) for target_style, comb_style in zip(gram_style_features, style_output_features): style_score += weight_per_style_layer * get_style_loss(comb_style, target_style) weight_per_content_layer = 1.0 / float(len(content_output_features)) for target_content, comb_content in zip(content_features, content_output_features): content_score += weight_per_content_layer* get_content_loss(comb_content, target_content) style_score *= style_weight content_score *= content_weight loss = style_score + content_score return loss # 定义优化器 def run_style_transfer(content_path, style_path, num_iterations=1000, content_weight=1e3, style_weight=1e-2): # 提取内容图片和风格图片的特征 content_features, style_features = get_feature_representations(vgg, content_path, style_path) gram_style_features = [gram_matrix(feature) for feature in style_features] init_image = load_and_process_img(content_path) init_image = tf.Variable(init_image, dtype=tf.float32) opt = tf.optimizers.Adam(learning_rate=5, beta_1=0.99, epsilon=1e-1) # 定义损失权重 loss_weights = (style_weight, content_weight) # 进行训练 for i in range(num_iterations): with tf.GradientTape() as tape: loss = compute_loss(vgg, loss_weights, init_image, gram_style_features, content_features) grad = tape.gradient(loss, init_image) opt.apply_gradients([(grad, init_image)]) clipped = tf.clip_by_value(init_image, clip_value_min=0.0, clip_value_max=255.0) init_image.assign(clipped) return init_image.numpy() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值