-
Notifications
You must be signed in to change notification settings - Fork 0
/
DGP_untrain.py
188 lines (158 loc) · 7.25 KB
/
DGP_untrain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import faulthandler
faulthandler.enable()
import os
import sys
# from collections import OrderedDict
import tensorflow as tf
import torch
import torchvision.utils as vutils
import utils
from models import QDGP_G
# from torchsummary import summary
sys.path.append("./")
from str2bool import str2bool
## QNN啥都不需要输入,输入是分层噪声与类别标签
def add_qdgp_parser(parser):
parser.add_argument(
'--dist', action='store_true', default=False,
help='Train with distributed implementation (default: %(default)s)')
parser.add_argument(
'--exp_path', type=str, default='',
help='Experiment path (default: %(default)s)')
parser.add_argument(
'--root_dir', type=str, default='',
help='Root path of dataset (default: %(default)s)')
parser.add_argument(
'--list_file', type=str, default='',
help='List file of the dataset (default: %(default)s)')
parser.add_argument(
'--resolution', type=int, default=128,
help='Resolution to resize the input image (default: %(default)s)')
parser.add_argument(
'--dgp_mode', type=str, default='reconstruct',
help='DGP mode (default: %(default)s)')
parser.add_argument(
'--random_G', action='store_true', default=False,
help='Use randomly initialized generator? (default: %(default)s)')
parser.add_argument(
'--update_G', action='store_true', default=True,
help='Finetune Generator? (default: %(default)s)')
parser.add_argument(
'--update_embed', action='store_true', default=True,
help='Finetune class embedding? (default: %(default)s)')
parser.add_argument(
'--save_G', action='store_true', default=False,
help='Save fine-tuned generator and latent vector? (default: %(default)s)')
parser.add_argument(
'--print_interval', type=int, default=500, nargs='+',
help='Number of iterations to print training loss (default: %(default)s)')
parser.add_argument(
'--save_interval', type=int, default=None, nargs='+',
help='Number of iterations to save image')
parser.add_argument(
'--lr_ratio', type=float, default=[1.0, 1.0, 1.0, 1.0], nargs='+',
help='Decreasing ratio for learning rate in blocks (default: %(default)s)')
parser.add_argument(
'--select_num', type=int, default=500,
help='Number of image pool to select from (default: %(default)s)')
parser.add_argument(
'--iterations', type=int, default=[2000, 2000, 1000, 1000], nargs='+',
help='Training iterations for all stages')
parser.add_argument(
'--G_lrs', type=float, default=[1e-5, 1e-3, 1e-4, 1e-4], nargs='+',
help='Learning rate steps of Generator')
parser.add_argument(
'--sample_std', type=float, default=0.3,
help='sampling standard deviation')
parser.add_argument(
'--z_lrs', type=float, default=[1e-3, 1e-3, 1e-4, 1e-4], nargs='+',
help='Learning rate steps of latent code z')
parser.add_argument(
'--warm_up', type=int, default=100,
help='Number of warmup iterations (default: %(default)s)')
parser.add_argument( ## 这个必须
'--use_in', type=str2bool, default=[False, False, False, False, False], nargs='+',
help='Whether to use instance normalization in generator')
return parser
def add_example_parser(parser):
parser.add_argument(
'--image_path', type=str, default='',
help='Path of the image to be processed (default: %(default)s)')
parser.add_argument(
'--bucket_path', type=str, default='',
help='Path of the experimental bucket to be processed (default: %(default)s)'
)
parser.add_argument(
'--pattern_path', type=str, default='',
help='Path of the patterns to be processed (default: %(default)s)'
)
parser.add_argument(
'--class', type=int, default=-1,
help='class index of the image (default: %(default)s)')
parser.add_argument('--dims', type=int, default=128, metavar='D',
help="dimension of the bucket data")
parser.add_argument('--object', type=str, default="2",
help="image object")
return parser
# prepare arguments and save in config
parser = utils.prepare_parser()
parser = add_qdgp_parser(parser)
parser = add_example_parser(parser)
config = vars(parser.parse_args())
utils.dgp_update_config(config)
print("dims:", config["dims"], flush=True)
print("object:", config["object"], flush=True)
# set random seed
utils.seed_rng(config['seed'])
source_dir = os.path.dirname(os.path.abspath(__file__))
config['exp_path'] = source_dir
# config['exp_path'] = '/home/xtl/Documents/PythonFiles/SPI_QML/untrain_QuGe/deep-generative-prior'
#
# if not os.path.exists('{}/images'.format(config['exp_path'])):
# os.makedirs('{}/images'.format(config['exp_path']))
# if not os.path.exists('{}/images_sheet'.format(config['exp_path'])):
# os.makedirs('{}/images_sheet'.format(config['exp_path']))
# initialize DGP model
# print(qdgp.G, flush=True) # good, no problem
# target image path (original)
config['image_path'] = source_dir + '/data_zhai/shot/{}.png'.format(config['object'])
config['bucket_path'] = source_dir + '/data_zhai/128/128/buckets/{}.npy'.format(config['object'])
config['pattern_path'] = source_dir + '/data_zhai/128/128/randomP/randomP.npy'
# prepare the target image
# img = utils.get_img(config['image_path'], config['resolution']).cuda()
bucket_target = torch.Tensor(utils.get_bucket_128(config['bucket_path'], config['dims'])).cuda()
# config['image_path'] = source_dir + '/data_zhai/shot/{}.png'.format(config['object'])
#
# config['bucket_path'] = source_dir + '/data_zhai/bucket_randomP/64/{}.npy'.format(config['object'])
# config['pattern_path'] = source_dir + '/data_zhai/randomP/randomP.npy'
# # prepare the target image
# img = utils.get_img(config['image_path'], config['resolution']).cuda()
# bucket_target = torch.Tensor(utils.get_bucket(config['bucket_path'], config['dims'])).cuda()
patterns = torch.Tensor(utils.get_pattern(config['pattern_path'], config['dims'])).cuda()
category = torch.Tensor([config['class']]).long().cuda() # 默认是-1
import numpy as np
def test_code(orignal_image, patterns, data_array):
orignal_image = torch.Tensor(orignal_image).cuda()
# patterns = torch.Tensor(patterns, dtype=torch.float32) # with shape (1024*3, 64, 64)
I_orginal = torch.Tensor([torch.sum(torch.multiply(orignal_image, patt)) for patt in patterns]).cuda()
# I_orginal = torch.unsqueeze(I_orginal, dim=1)
print(I_orginal, flush=True)
print(torch.squeeze(data_array), flush=True)
test_result = I_orginal - torch.Tensor(np.squeeze(data_array)).cuda()
print(test_result, flush=True)
# import cv2
# orignal = cv2.imread(config['image_path'], 0)/255.0
#
# test_code(orignal, patterns, bucket_target)
# initialize DGP model
dgp = QDGP_G(config)
dgp.set_target(bucket_target, category)
# we need to ensure the shapes of the tensor
# prepare initial latent vector
dgp.select_z(patterns, select_y=True if config['class'] < 0 else False)
# start reconstruction
# 代码是有问题的。如何衔接起来。
loss_dict = dgp.run(patterns, bucket_target) # 这一步已经优化完成了
# save_imgs = img.clone().cpu()