完全让ChatGPT写一个风格迁移的例子,不改动任何代码

2024-02-08 00:36

本文主要是介绍完全让ChatGPT写一个风格迁移的例子,不改动任何代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

⭐️ 前言

小编让ChatGPT写一个风格迁移的例子,注意注意,代码无任何改动,直接运行,输出结果。
在这里插入图片描述
额。。。。这不是风格转换后的结果图。

⭐️ 风格迁移基本原理

风格迁移是一种计算机视觉领域的图像处理技术,它的目标是将一张图像的内容与另一张图像的艺术风格相结合,创造出一张新的图像。这一技术通过深度学习的方法,结合卷积神经网络(CNN)和损失函数,实现了在内容和风格之间进行有效迁移。

在这里插入图片描述

下面详细探讨风格迁移的原理:

1. 内容表示:

内容图像: 风格迁移的源,提供图像的内容信息。
内容表示: 使用预训练的卷积神经网络,通常选择网络中的某一层,提取内容图像的特征表示。

2. 风格表示:

风格图像: 风格迁移的目标,提供所需的艺术风格。
风格表示: 同样使用卷积神经网络,选择多个层次的特征表示,以捕捉图像的不同尺度和层次的艺术风格。

在这里插入图片描述

5. 优化过程:

通过调整生成图像的像素值,以最小化总体损失函数,来生成最终的风格迁移图像。这通常通过梯度下降等优化算法来实现。

在这里插入图片描述

代码如下:

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import models, transforms
from PIL import Image
import numpy as np# 加载预训练的VGG模型
def load_vgg_model():vgg = models.vgg19(pretrained=True).featuresfor param in vgg.parameters():param.requires_grad_(False)return vgg# 图像预处理
def load_image(image_path, transform=None, max_size=None, shape=None):image = Image.open(image_path).convert('RGB')if max_size:scale = max_size / max(image.size)size = tuple(int(x * scale) for x in image.size)image = image.resize(size)if shape:image = image.resize(shape)if transform:image = transform(image).unsqueeze(0)return image# 图像后处理
def convert_image(tensor):image = tensor.to("cpu").clone().detach()image = image.numpy().squeeze()image = image.transpose(1,2,0)image = image * np.array((0.229, 0.224, 0.225)) + np.array((0.485, 0.456, 0.406))image = image.clip(0, 1)return image# 定义风格迁移网络
class StyleTransferNet(nn.Module):def __init__(self, content_layers, style_layers):super(StyleTransferNet, self).__init__()self.vgg = load_vgg_model()self.content_layers = content_layersself.style_layers = style_layersdef forward(self, x):content_outputs = []style_outputs = []for i, layer in enumerate(self.vgg):x = layer(x)if i in self.content_layers:content_outputs.append(x)if i in self.style_layers:style_outputs.append(x)return content_outputs, style_outputs# 损失函数
def content_loss(target, generated):return torch.mean((target - generated)**2)def gram_matrix(tensor):_, d, h, w = tensor.size()tensor = tensor.view(d, h * w)gram = torch.mm(tensor, tensor.t())return gramdef style_loss(target, generated):target_gram = gram_matrix(target)generated_gram = gram_matrix(generated)return torch.mean((target_gram - generated_gram)**2)def total_variation_loss(image):return torch.sum(torch.abs(image[:, :, :, :-1] - image[:, :, :, 1:])) + \torch.sum(torch.abs(image[:, :, :-1, :] - image[:, :, 1:, :]))# 风格迁移主函数
def style_transfer(content_path, style_path, output_path, num_steps=10000, content_weight=1, style_weight=1e6, tv_weight=1e-6):device = torch.device("cuda" if torch.cuda.is_available() else "cpu")content_image = load_image(content_path, transform, max_size=400)style_image = load_image(style_path, transform, shape=[content_image.size(2), content_image.size(3)])content_image = content_image.to(device)style_image = style_image.to(device)model = StyleTransferNet(content_layers, style_layers).to(device).eval()# 优化器optimizer = optim.Adam([content_image.requires_grad_(), style_image.requires_grad_()], lr=0.01)for step in range(num_steps):optimizer.zero_grad()content_outputs, style_outputs = model(content_image)content_loss_value = 0for target, generated in zip(content_outputs, model(content_image)[0]):content_loss_value += content_loss(target, generated)style_loss_value = 0for target, generated in zip(style_outputs, model(style_image)[1]):style_loss_value += style_loss(target, generated)tv_loss_value = total_variation_loss(content_image)total_loss = content_weight * content_loss_value + style_weight * style_loss_value + tv_weight * tv_loss_valuetotal_loss.backward()optimizer.step()if step % 50 == 0 or step == num_steps - 1:print(f"Step {step}/{num_steps}, Total Loss: {total_loss.item()}")# 保存生成的图像output_image = convert_image(content_image)Image.fromarray((output_image * 255).astype(np.uint8)).save(output_path)# 主程序
content_image_path = "./content.jpg"
style_image_path = "./style.jpg"
output_image_path = "./image.jpg"transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
])content_layers = [21]
style_layers = [0, 5, 10, 19, 28]style_transfer(content_image_path, style_image_path, output_image_path)

输入的图片是这两张
在这里插入图片描述
在这里插入图片描述

输出的图片是这样(运行了10000轮):

在这里插入图片描述

风格是有了,调整一些参数,结果会有不同。

风格迁移技术的核心思想是通过深度学习网络将图像的内容和风格进行数学建模,然后通过优化损失函数来生成具有目标风格的图像。这使得艺术风格的迁移成为可能,为图像处理领域带来了新的可能性。

笔者水平有限,若有不对的地方欢迎评论指正!

这篇关于完全让ChatGPT写一个风格迁移的例子,不改动任何代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/689425

相关文章

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Spring AI使用tool Calling和MCP的示例详解

《SpringAI使用toolCalling和MCP的示例详解》SpringAI1.0.0.M6引入ToolCalling与MCP协议,提升AI与工具交互的扩展性与标准化,支持信息检索、行动执行等... 目录深入探索 Spring AI聊天接口示例Function CallingMCPSTDIOSSE结束语

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

MySQL进行数据库审计的详细步骤和示例代码

《MySQL进行数据库审计的详细步骤和示例代码》数据库审计通过触发器、内置功能及第三方工具记录和监控数据库活动,确保安全、完整与合规,Java代码实现自动化日志记录,整合分析系统提升监控效率,本文给大... 目录一、数据库审计的基本概念二、使用触发器进行数据库审计1. 创建审计表2. 创建触发器三、Java

三频BE12000国补到手2549元! ROG 魔盒Pro WIFI7电竞AI路由器上架

《三频BE12000国补到手2549元!ROG魔盒ProWIFI7电竞AI路由器上架》近日,华硕带来了ROG魔盒ProWIFI7电竞AI路由器(ROGSTRIXGR7Pro),目前新... 华硕推出了ROG 魔盒Pro WIFI7电竞AI路由器(ROG STRIX GR7 Phttp://www.cppcn

MySQL 迁移至 Doris 最佳实践方案(最新整理)

《MySQL迁移至Doris最佳实践方案(最新整理)》本文将深入剖析三种经过实践验证的MySQL迁移至Doris的最佳方案,涵盖全量迁移、增量同步、混合迁移以及基于CDC(ChangeData... 目录一、China编程JDBC Catalog 联邦查询方案(适合跨库实时查询)1. 方案概述2. 环境要求3.

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性: