Skip to content

KittyKim1106/Style_Transform

Repository files navigation

风格迁移 Neural Style Transfer

基于 Gatys et al. 2016 CVPR 论文,使用 PyTorch 实现的神经风格迁移。

项目结构

Style_Transform/
├── images/
│   ├── content/
│   │   └── tyut.jpg           # 内容图:太原理工大学
│   └── style/
│       ├── starry_night.jpg   # 风格图:梵高星空
│       └── mona_lisa.png      # 风格图:蒙娜丽莎
├── output/                    # 输出结果
├── style_transfer.py          # 主程序
├── requirements.txt           # 依赖
└── README.md

环境要求

  • Python 3.8+
  • PyTorch(建议 GPU 版本)
  • torchvision、Pillow、numpy

在 Google Colab 上运行

1. 打开 Colab

访问 colab.research.google.com,新建 Notebook。

2. 设置 GPU

菜单栏 → 「修改」 → 「笔记本设置」 → 硬件加速器选择 T4 GPU → 保存。

3. 上传项目文件

在 Colab 左侧文件面板中,上传以下文件:

  • style_transfer.py
  • images/ 文件夹(包含 content 和 style 子目录及图片)

或者使用代码上传:

from google.colab import files
uploaded = files.upload()

4. 运行

# 梵高星空风格
!python style_transfer.py \
    --content images/content/tyut.jpg \
    --style images/style/starry_night.jpg

# 蒙娜丽莎风格
!python style_transfer.py \
    --content images/content/tyut.jpg \
    --style images/style/mona_lisa.png

5. 查看结果

结果保存在 output/ 目录中,可在 Colab 文件面板中查看和下载。

本地运行

pip install -r requirements.txt
python style_transfer.py --content images/content/tyut.jpg --style images/style/starry_night.jpg

参数说明

参数 默认值 说明
--content images/content/tyut.jpg 内容图路径
--style images/style/starry_night.jpg 风格图路径
--output_dir output 输出目录
--max_size 400 图像最大边长
--total_step 2000 总迭代步数
--style_weight 100 风格权重(越大风格越强)
--lr 0.003 学习率
--log_step 10 日志打印间隔
--sample_step 500 中间结果保存间隔

参考文献

Gatys, L.A., Ecker, A.S. and Bethge, M., 2016. Image style transfer using convolutional neural networks. In CVPR.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors