options.py 5.8 KB
Newer Older
H
hypox64 已提交
1 2
import argparse
import os
H
hypox64 已提交
3
import torch
H
hypox64 已提交
4 5 6 7 8 9 10 11 12

class Options():
    def __init__(self):
        self.parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
        self.initialized = False

    def initialize(self):

        #base
13
        self.parser.add_argument('--use_gpu',type=int,default=0, help='if -1, do not use gpu')
H
hypox64 已提交
14
        # self.parser.add_argument('--use_gpu', action='store_true', help='if input it, use gpu')
15
        self.parser.add_argument('--media_path', type=str, default='./imgs/ruoruo.jpg',help='your videos or images path')
H
HypoX64 已提交
16
        self.parser.add_argument('--mode', type=str, default='auto',help='Program running mode. auto | add | clean | style')
17
        self.parser.add_argument('--model_path', type=str, default='./pretrained_models/mosaic/add_face.pth',help='pretrained model path')
H
hypox64 已提交
18
        self.parser.add_argument('--result_dir', type=str, default='./result',help='output media will be saved here')
H
hypox64 已提交
19
        self.parser.add_argument('--tempimage_type', type=str, default='png',help='type of temp image, png | jpg, png is better but occupy more storage space')
H
hypox64 已提交
20
        self.parser.add_argument('--netG', type=str, default='auto',
21
            help='select model to use for netG(Clean mosaic and Transfer style) -> auto | unet_128 | unet_256 | resnet_9blocks | HD | video')
22
        self.parser.add_argument('--fps', type=int, default=0,help='read and output fps, if 0-> origin')
H
HypoX64 已提交
23
        self.parser.add_argument('--output_size', type=int, default=0,help='size of output media, if 0 -> origin')
H
hypox64 已提交
24
        
H
hypox64 已提交
25
        #AddMosaic
H
hypox64 已提交
26 27
        self.parser.add_argument('--mosaic_mod', type=str, default='squa_avg',help='type of mosaic -> squa_avg | squa_random | squa_avg_circle_edge | rect_avg | random')
        self.parser.add_argument('--mosaic_size', type=int, default=0,help='mosaic size,if 0 auto size')
H
HypoX64 已提交
28
        self.parser.add_argument('--mask_extend', type=int, default=10,help='extend mosaic area')
H
hypox64 已提交
29 30
        self.parser.add_argument('--mask_threshold', type=int, default=64,help='threshold of recognize mosaic position 0~255')
        
H
hypox64 已提交
31
        #CleanMosaic     
32
        self.parser.add_argument('--mosaic_position_model_path', type=str, default='auto',help='name of model use to find mosaic position')
H
HypoX64 已提交
33
        self.parser.add_argument('--traditional', action='store_true', help='if specified, use traditional image processing methods to clean mosaic')
34 35
        self.parser.add_argument('--tr_blur', type=int, default=10, help='ksize of blur when using traditional method, it will affect final quality')
        self.parser.add_argument('--tr_down', type=int, default=10, help='downsample when using traditional method,it will affect final quality')
H
HypoX64 已提交
36 37
        self.parser.add_argument('--no_feather', action='store_true', help='if specified, no edge feather and color correction, but run faster')
        self.parser.add_argument('--all_mosaic_area', action='store_true', help='if specified, find all mosaic area, else only find the largest area')
H
hypox64 已提交
38
        self.parser.add_argument('--medfilt_num', type=int, default=11,help='medfilt window of mosaic movement in the video')
H
hypox64 已提交
39 40 41
        self.parser.add_argument('--ex_mult', type=str, default='auto',help='mosaic area expansion')
        
        #StyleTransfer
42
        self.parser.add_argument('--preprocess', type=str, default='resize', help='resize and cropping of images at load time [ resize | resize_scale_width | edges | gray] or resize,edges(use comma to split)')
H
HypoX64 已提交
43
        self.parser.add_argument('--edges', action='store_true', help='if specified, use edges to generate pictures,(input_nc = 1)')  
H
hypox64 已提交
44
        self.parser.add_argument('--canny', type=int, default=150,help='threshold of canny')
H
HypoX64 已提交
45
        self.parser.add_argument('--only_edges', action='store_true', help='if specified, output media will be edges')
H
hypox64 已提交
46

H
hypox64 已提交
47 48 49 50 51 52 53 54
        self.initialized = True


    def getparse(self):
        if not self.initialized:
            self.initialize()
        self.opt = self.parser.parse_args()

H
hypox64 已提交
55 56
        model_name = os.path.basename(self.opt.model_path)

57
        if torch.cuda.is_available() and self.opt.use_gpu > -1:
H
hypox64 已提交
58 59 60 61
            self.opt.use_gpu = True
        else:
            self.opt.use_gpu = False

H
hypox64 已提交
62
        if self.opt.mode == 'auto':
63
            if 'clean' in model_name or self.opt.traditional:
H
hypox64 已提交
64
                self.opt.mode = 'clean'
65 66
            elif 'add' in model_name:
                self.opt.mode = 'add'
H
hypox64 已提交
67
            elif 'style' in model_name or 'edges' in model_name:
H
hypox64 已提交
68
                self.opt.mode = 'style'
H
hypox64 已提交
69
            else:
H
HypoX64 已提交
70 71 72
                print('Please input running model!')
                input('Please press any key to exit.\n')
                exit(0)
H
hypox64 已提交
73

74 75 76 77 78 79
        if self.opt.output_size == 0 and self.opt.mode == 'style':
            self.opt.output_size = 512

        if 'edges' in model_name or 'edges' in self.opt.preprocess:
            self.opt.edges = True

H
hypox64 已提交
80
        if self.opt.netG == 'auto' and self.opt.mode =='clean':
H
hypox64 已提交
81
            if 'unet_128' in model_name:
H
hypox64 已提交
82
                self.opt.netG = 'unet_128'
H
hypox64 已提交
83
            elif 'resnet_9blocks' in model_name:
H
hypox64 已提交
84
                self.opt.netG = 'resnet_9blocks'
H
HypoX64 已提交
85
            elif 'HD' in model_name and 'video' not in model_name:
H
hypox64 已提交
86
                self.opt.netG = 'HD'
H
hypox64 已提交
87
            elif 'video' in model_name:
H
hypox64 已提交
88
                self.opt.netG = 'video'
H
hypox64 已提交
89 90
            else:
                print('Type of Generator error!')
H
HypoX64 已提交
91 92
                input('Please press any key to exit.\n')
                exit(0)
H
hypox64 已提交
93

H
hypox64 已提交
94 95
        if self.opt.ex_mult == 'auto':
            if 'face' in model_name:
96
                self.opt.ex_mult = 1.1
H
hypox64 已提交
97 98 99 100
            else:
                self.opt.ex_mult = 1.5
        else:
            self.opt.ex_mult = float(self.opt.ex_mult)
H
hypox64 已提交
101

102 103 104
        if self.opt.mosaic_position_model_path == 'auto':
            _path = os.path.join(os.path.split(self.opt.model_path)[0],'mosaic_position.pth')
            self.opt.mosaic_position_model_path = _path
H
hypox64 已提交
105
            # print(self.opt.mosaic_position_model_path)
106

H
hypox64 已提交
107
        return self.opt