sanghoho / Fast-Neural-Style-Transfer-Pytorch

Modification to make 'fast_neural_style' example in Pytorch Official Repo a class

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fast-neural-style ๐ŸŒ‡ ๐Ÿš€

If you want to show original Pytorch Fast-Neural-Style Exmpale, this is the Original Repo

Introduction

๊ธฐ์กด์˜ Pytorch example ๋ ˆํฌ์ง€ํ„ฐ๋ฆฌ์˜ fast_neural_style๋Š” Perceptual Losses for Real-Time Style Transfer and Super-Resolution๋ฅผ ๊ตฌํ˜„ํ•ด๋†“์•˜์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ CLI ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉ๋˜๊ณ , argparse๋กœ ์ธ์ž๋“ค์„ ๋„˜๊ฒจ์ฃผ๊ธฐ ๋•Œ๋ฌธ์—, ์ธ์ž์˜ ์ˆ˜์ •์ด๋‚˜ ์‚ฌ์šฉ์„ฑ ์ธก๋ฉด์—์„œ ๋ถˆํŽธํ•จ์„ ๋Š๊ผˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ฝ”๋“œ๋ฅผ Python์˜ Package๋กœ ๋ฆฌํŒฉํ† ๋ง ํ•˜์˜€์œผ๋ฉฐ, ์ธ์ž๋ฅผ ๋ฐ›๋Š” HyperParameter ํด๋ž˜์Šค๋„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

This repository contains a pytorch implementation of an algorithm for artistic style transfer. The algorithm can be used to mix the content of an image with the style of another image. For example, here is a photograph of a door arch rendered in the style of a stained glass painting.

The model uses the method described in Perceptual Losses for Real-Time Style Transfer and Super-Resolution along with Instance Normalization. The saved-models for examples shown in the README can be downloaded from here.

Usage

HyperParameter calss

class HyperParameter:
  def __init__(self, command, cuda, param_dict):
    # Code

  def set_train_parameter(self, param_dict):
    # Code
    
    def set_eval_parameter(self, param_dict):
      # Code
  • __init__: ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ์“ฐ๋Š”์ง€, GPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€ ๊ทธ๋ฆฌ๊ณ  ์ธ์ž๋“ค์˜ dictionary๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • set_train_parameter: train์„ ํ•  ๊ฒฝ์šฐ, param_dict๋ฅผ ๋„˜๊ฒจ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • set_eval_parameter: eval์„ ํ•  ๊ฒฝ์šฐ, param_dict๋ฅผ ๋„˜๊ฒจ๋ฐ›์Šต๋‹ˆ๋‹ค.

์„ค์ •๋˜์ง€ ์•Š์€ ๊ฐ’ ๋“ค์€ default ๊ฐ’์œผ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ์˜ HyperParameter class ์ฐธ๊ณ 

Stylize image

from ParentDir.core import neural_style

### For Evaluation
param_dict = {
    "content_image": "/path/to/content.jpg",
    "output_image": "/path/to/output.jpg",
    "model": "/path/to/model/checkpoint.pth or mod.model"
}

cuda = 1 # True

param = neural_style.HyperParameter("eval", cuda, param_dict)
neural_style.stylize(param)

ํ•™์Šต ์ค‘ ์–ป์„ ์ˆ˜ ์žˆ๋Š” checkpoint์˜ .pth ํŒŒ์ผ์ด๋‚˜, ํ•™์Šต์ด ๋ชจ๋‘ ๋๋‚œ ๋’ค ์–ป์„ ์ˆ˜ ์žˆ๋Š” .model ํŒŒ์ผ์„ ์ง€์ •ํ•ด์„œ ํ•™์Šต๋œ ์Šคํƒ€์ผ๋กœ content image๋ฅผ ๋ณ€ํ™˜์‹œ์ผœ์ค๋‹ˆ๋‹ค.

Train model

from ParentDir.core import neural_style

### For Train
param_dict = {
    # "transfer_learning": 1,
    # "checkpoint_model_dir": "/path/to/checkpoint/",

    "dataset": "/path/to/COCO",
    "style_image": "/path/to/style.jpg",
    "save_model_dir": "/path/to/save/"

}

cuda = 1 # True

param = neural_style.HyperParameter("train", cuda, param_dict)
neural_style.stylize(param)

train์„ ํ•˜๋Š”๋ฐ, ์ด์ „ ํ•™์Šต์—์„œ ์ด์–ด์„œ ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ transfer_learing ์ธ์ž๋ฅผ 1๋กœ ํ•˜๊ณ , checkpoint ํด๋”๋ฅผ ์ง€์ •ํ•ด๋‘๋ฉด ์ด์–ด์„œ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

I used COCO 2014 Training images dataset [80K/13GB] (download).

  • --style-image: path to style-image.
  • --save-model-dir: path to folder where trained model will be saved.
  • --cuda: set it to 1 for running on GPU, 0 for CPU.

Refer to neural_style/neural_style.py for other command line arguments. For training new models you might have to tune the values of --content-weight and --style-weight. The mosaic style model shown above was trained with --content-weight 1e5 and --style-weight 1e10. The remaining 3 models were also trained with similar order of weight parameters with slight variation in the --style-weight (5e10 or 1e11).

Requirements

The program is written in Python, and uses pytorch, scipy. A GPU is not necessary, but can provide a significant speed up especially for training a new model. Regular sized images can be styled on a laptop or desktop using saved models.

Models

Models for the examples shown below can be downloaded from here or by running the script download_saved_models.py.


About

Modification to make 'fast_neural_style' example in Pytorch Official Repo a class


Languages

Language:Python 100.0%