vidscbvsdCHEN commited on
Commit
bd55f43
·
1 Parent(s): bf4d58e
Files changed (1) hide show
  1. app.py +360 -0
app.py ADDED
@@ -0,0 +1,360 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ from tqdm import tqdm
4
+ import tensorflow as tf
5
+ import typing
6
+ import wmi
7
+ import psutil
8
+ from pynvml import *
9
+
10
+
11
+ print("import done")
12
+
13
+ w = wmi.WMI()
14
+ global list1
15
+ list1=[]
16
+ def info():
17
+ list1.append("电脑信息")
18
+ for BIOSs in w.Win32_ComputerSystem():
19
+ list1.append("电脑名称: %s" %BIOSs.Caption)
20
+ list1.append("使 用 者: %s" %BIOSs.UserName)
21
+ for address in w.Win32_NetworkAdapterConfiguration(ServiceName = "e1dexpress"):
22
+ list1.append("IP地址: %s" % address.IPAddress[0])
23
+ list1.append("MAC地址: %s" % address.MACAddress)
24
+ for BIOS in w.Win32_BIOS():
25
+ list1.append("使用日期: %s" %BIOS.Description)
26
+ list1.append("主板型号: %s" %BIOS.SerialNumber)
27
+ for processor in w.Win32_Processor():
28
+ list1.append("CPU型号: %s" % processor.Name.strip())
29
+ for memModule in w.Win32_PhysicalMemory():
30
+ totalMemSize=int(memModule.Capacity)
31
+ list1.append("内存厂商: %s" %memModule.Manufacturer)
32
+ list1.append("内存型号: %s" %memModule.PartNumber)
33
+ list1.append("内存大小: %.2fGB" %(totalMemSize/1024**3))
34
+ for disk in w.Win32_DiskDrive(InterfaceType = "IDE"):
35
+ diskSize=int(disk.size)
36
+ list1.append("磁盘名称: %s" %disk.Caption)
37
+ list1.append("磁盘大小: %.2fGB" %(diskSize/1024**3))
38
+ for xk in w.Win32_VideoController():
39
+ list1.append("显卡名称: %s" %xk.name)
40
+ info()
41
+ for li in list1:
42
+ print(li)
43
+
44
+
45
+ def get_cpu_mem_info():
46
+ """
47
+ 获取当前机器的内存信息, 单位 MB
48
+ :return: mem_total 当前机器所有的内存 mem_free 当前机器可用的内存 mem_process_used 当前进程使用的内存
49
+ """
50
+ mem_total = round(psutil.virtual_memory().total / 1024 / 1024, 2)
51
+ mem_free = round(psutil.virtual_memory().available / 1024 / 1024, 2)
52
+ mem_process_used = round(psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024, 2)
53
+ # return mem_total, mem_free, mem_process_used
54
+ print(r'当前机器内存使用情况:总共 {} MB, 剩余 {} MB, 当前进程使用的内存 {} MB'.format(mem_total, mem_free, mem_process_used))
55
+
56
+
57
+ def get_gpu_mem_info():
58
+ nvmlInit()
59
+ print("Driver Version:", nvmlSystemGetDriverVersion())#显卡驱动版本
60
+ deviceCount = nvmlDeviceGetCount()#几块显卡
61
+ for i in range(deviceCount):
62
+ handle = nvmlDeviceGetHandleByIndex(i)
63
+ print("Device", i, ":", nvmlDeviceGetName(handle)) #具体是什么显卡
64
+ handler = nvmlDeviceGetHandleByIndex(i)
65
+ meminfo = nvmlDeviceGetMemoryInfo(handler)
66
+ total = round(meminfo.total / 1024 / 1024, 2)
67
+ used = round(meminfo.used / 1024 / 1024, 2)
68
+ free = round(meminfo.free / 1024 / 1024, 2)
69
+ print(r'当前显卡显存使用情况:总共 {} MB, 已经使用 {} MB, 剩余 {} MB'
70
+ .format(total, used, free))
71
+
72
+
73
+ get_cpu_mem_info()
74
+ get_gpu_mem_info()
75
+
76
+
77
+
78
+ #settings.py迁移
79
+ # 内容特征层及loss加权系数
80
+ CONTENT_LAYERS = {'block4_conv2': 0.5, 'block5_conv2': 0.5}
81
+ # 风格特征层及loss加权系数
82
+ STYLE_LAYERS = {'block1_conv1': 0.2, 'block2_conv1': 0.2, 'block3_conv1': 0.2, 'block4_conv1': 0.2,
83
+ 'block5_conv1': 0.2}
84
+ # 内容图片路径
85
+ #CONTENT_IMAGE_PATH = './images/content.jpg'
86
+ CONTENT_IMAGE_PATH = input("image path:")
87
+ # 风格图片路径
88
+ # STYLE_IMAGE_PATH = './images/style.jpg'
89
+ STYLE_IMAGE_PATH = input('style image path:')
90
+ # 生成图片的保存目录
91
+ # OUTPUT_DIR = './output'
92
+ OUTPUT_DIR = input('output path:')
93
+
94
+ # 内容loss总加权系数
95
+ CONTENT_LOSS_FACTOR = 1
96
+ # 风格loss总加权系数
97
+ STYLE_LOSS_FACTOR = 100
98
+
99
+ # 图片宽度
100
+ WIDTH = 450
101
+ # 图片高度
102
+ HEIGHT = 300
103
+
104
+ # 训练epoch数
105
+ EPOCHS = 20
106
+ # 每个epoch训练多少次
107
+ STEPS_PER_EPOCH = 100
108
+ # 学习率
109
+ LEARNING_RATE = 0.03
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+ #utils.py迁移
118
+ # 我们准备使用经典网络在imagenet数据集上的与训练权重,所以归一化时也要使用imagenet的平均值和标准差
119
+ print("utils")
120
+ image_mean = tf.constant([0.485, 0.456, 0.406])
121
+ image_std = tf.constant([0.299, 0.224, 0.225])
122
+
123
+
124
+ def normalization(x):
125
+ """
126
+ 对输入图片x进行归一化,返回归一化的值
127
+ """
128
+ return (x - image_mean) / image_std
129
+
130
+
131
+ def load_images(image_path, width=WIDTH, height=HEIGHT):
132
+ """
133
+ 加载并处理图片
134
+ :param image_path: 图片路径
135
+ :param width: 图片宽度
136
+ :param height: 图片长度
137
+ :return: 一个张量
138
+ """
139
+ # 加载文件
140
+ x = tf.io.read_file(image_path)
141
+ # 解码图片
142
+ x = tf.image.decode_jpeg(x, channels=3)
143
+ # 修改图片大小
144
+ x = tf.image.resize(x, [height, width])
145
+ x = x / 255.
146
+ # 归一化
147
+ x = normalization(x)
148
+ x = tf.reshape(x, [1, height, width, 3])
149
+ # 返回结果
150
+ return x
151
+
152
+
153
+ def save_image(image, filename):
154
+ x = tf.reshape(image, image.shape[1:])
155
+ x = x * image_std + image_mean
156
+ x = x * 255.
157
+ x = tf.cast(x, tf.int32)
158
+ x = tf.clip_by_value(x, 0, 255)
159
+ x = tf.cast(x, tf.uint8)
160
+ x = tf.image.encode_jpeg(x)
161
+ tf.io.write_file(filename, x)
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+ #model.py迁移
171
+ print("models.py")
172
+ def get_vgg19_model(layers):
173
+ """
174
+ 创建并初始化vgg19模型
175
+ :return:
176
+ """
177
+ # 加载imagenet上预训练的vgg19
178
+ vgg = tf.keras.applications.VGG19(include_top=False, weights='imagenet')
179
+ # 提取需要被用到的vgg的层的output
180
+ outputs = [vgg.get_layer(layer).output for layer in layers]
181
+ # 使用outputs创建新的模型
182
+ model = tf.keras.Model([vgg.input, ], outputs)
183
+ # 锁死参数,不进行训练
184
+ model.trainable = False
185
+ return model
186
+
187
+
188
+ class NeuralStyleTransferModel(tf.keras.Model):
189
+
190
+ def __init__(self, content_layers: typing.Dict[str, float] = CONTENT_LAYERS,
191
+ style_layers: typing.Dict[str, float] = STYLE_LAYERS):
192
+ super(NeuralStyleTransferModel, self).__init__()
193
+ # 内容特征层字典 Dict[层名,加权系数]
194
+ self.content_layers = content_layers
195
+ # 风格特征层
196
+ self.style_layers = style_layers
197
+ # 提取需要用到的所有vgg层
198
+ layers = list(self.content_layers.keys()) + list(self.style_layers.keys())
199
+ # 创建layer_name到output索引的映射
200
+ self.outputs_index_map = dict(zip(layers, range(len(layers))))
201
+ # 创建并初始化vgg网络
202
+ self.vgg = get_vgg19_model(layers)
203
+
204
+ def call(self, inputs, training=None, mask=None):
205
+ """
206
+ 前向传播
207
+ :return
208
+ typing.Dict[str,typing.List[outputs,加权系数]]
209
+ """
210
+ outputs = self.vgg(inputs)
211
+ # 分离内容特征层和风格特征层的输出,方便后续计算 typing.List[outputs,加权系数]
212
+ content_outputs = []
213
+ for layer, factor in self.content_layers.items():
214
+ content_outputs.append((outputs[self.outputs_index_map[layer]][0], factor))
215
+ style_outputs = []
216
+ for layer, factor in self.style_layers.items():
217
+ style_outputs.append((outputs[self.outputs_index_map[layer]][0], factor))
218
+ # 以字典的形式返回输出
219
+ return {'content': content_outputs, 'style': style_outputs}
220
+
221
+
222
+
223
+
224
+
225
+
226
+
227
+
228
+ # 创建模型
229
+ model = NeuralStyleTransferModel()
230
+
231
+ print("进入主程序")
232
+
233
+ # 加载内容图片
234
+ content_image = load_images(CONTENT_IMAGE_PATH)
235
+ # 风格图片
236
+ style_image = load_images(STYLE_IMAGE_PATH)
237
+
238
+ # 计算出目标内容图片的内容特征备用
239
+ target_content_features = model([content_image, ])['content']
240
+ # 计算目标风格图片的风格特征
241
+ target_style_features = model([style_image, ])['style']
242
+
243
+ M = WIDTH * HEIGHT
244
+ N = 3
245
+
246
+
247
+ def _compute_content_loss(noise_features, target_features):
248
+ """
249
+ 计算指定层上两个特征之间的内容loss
250
+ :param noise_features: 噪声图片在指定层的特征
251
+ :param target_features: 内容图片在指定层的特征
252
+ """
253
+ content_loss = tf.reduce_sum(tf.square(noise_features - target_features))
254
+ # 计算系数
255
+ x = 2. * M * N
256
+ content_loss = content_loss / x
257
+ return content_loss
258
+
259
+
260
+ def compute_content_loss(noise_content_features):
261
+ """
262
+ 计算并当前图片的内容loss
263
+ :param noise_content_features: 噪声图片的内容特征
264
+ """
265
+ # 初始化内容损失
266
+ content_losses = []
267
+ # 加权计算内容损失
268
+ for (noise_feature, factor), (target_feature, _) in zip(noise_content_features, target_content_features):
269
+ layer_content_loss = _compute_content_loss(noise_feature, target_feature)
270
+ content_losses.append(layer_content_loss * factor)
271
+ return tf.reduce_sum(content_losses)
272
+
273
+
274
+ def gram_matrix(feature):
275
+ """
276
+ 计算给定特征的格拉姆矩阵
277
+ """
278
+ # 先交换维度,把channel维度提到最前面
279
+ x = tf.transpose(feature, perm=[2, 0, 1])
280
+ # reshape,压缩成2d
281
+ x = tf.reshape(x, (x.shape[0], -1))
282
+ # 计算x和x的逆的乘积
283
+ return x @ tf.transpose(x)
284
+
285
+
286
+ def _compute_style_loss(noise_feature, target_feature):
287
+ """
288
+ 计算指定层上两个特征之间的风格loss
289
+ :param noise_feature: 噪声图片在指定层的特征
290
+ :param target_feature: 风格图片在指定层的特征
291
+ """
292
+ noise_gram_matrix = gram_matrix(noise_feature)
293
+ style_gram_matrix = gram_matrix(target_feature)
294
+ style_loss = tf.reduce_sum(tf.square(noise_gram_matrix - style_gram_matrix))
295
+ # 计算系数
296
+ x = 4. * (M ** 2) * (N ** 2)
297
+ return style_loss / x
298
+
299
+
300
+ def compute_style_loss(noise_style_features):
301
+ """
302
+ 计算并返回图片的风格loss
303
+ :param noise_style_features: 噪声图片的风格特征
304
+ """
305
+ style_losses = []
306
+ for (noise_feature, factor), (target_feature, _) in zip(noise_style_features, target_style_features):
307
+ layer_style_loss = _compute_style_loss(noise_feature, target_feature)
308
+ style_losses.append(layer_style_loss * factor)
309
+ return tf.reduce_sum(style_losses)
310
+
311
+
312
+ def total_loss(noise_features):
313
+ """
314
+ 计算总��失
315
+ :param noise_features: 噪声图片特征数据
316
+ """
317
+ content_loss = compute_content_loss(noise_features['content'])
318
+ style_loss = compute_style_loss(noise_features['style'])
319
+ return content_loss * CONTENT_LOSS_FACTOR + style_loss * STYLE_LOSS_FACTOR
320
+
321
+
322
+ # 使用Adma优化器
323
+ optimizer = tf.keras.optimizers.Adam(LEARNING_RATE)
324
+
325
+ # 基于内容图片随机生成一张噪声图片
326
+ noise_image = tf.Variable((content_image + np.random.uniform(-0.2, 0.2, (1, HEIGHT, WIDTH, 3))) / 2)
327
+
328
+
329
+ # 使用tf.function加速训练
330
+ @tf.function
331
+ def train_one_step():
332
+ """
333
+ 一次迭代过程
334
+ """
335
+ # 求loss
336
+ with tf.GradientTape() as tape:
337
+ noise_outputs = model(noise_image)
338
+ loss = total_loss(noise_outputs)
339
+ # 求梯度
340
+ grad = tape.gradient(loss, noise_image)
341
+ # 梯度下降,更新噪声图片
342
+ optimizer.apply_gradients([(grad, noise_image)])
343
+ return loss
344
+
345
+
346
+ # 创建保存生成图片的文件夹
347
+ if not os.path.exists(OUTPUT_DIR):
348
+ os.mkdir(OUTPUT_DIR)
349
+
350
+ # 共训练EPOCHS个epochs
351
+ for epoch in range(EPOCHS):
352
+ # 使用tqdm提示训练进度
353
+ with tqdm(total=STEPS_PER_EPOCH, desc='Epoch {}/{}'.format(epoch + 1, EPOCHS)) as pbar:
354
+ # 每个epoch训练STEPS_PER_EPOCH次
355
+ for step in range(STEPS_PER_EPOCH):
356
+ _loss = train_one_step()
357
+ pbar.set_postfix({'loss': '%.4f' % float(_loss)})
358
+ pbar.update(1)
359
+ # 每个epoch保存一次图片
360
+ save_image(noise_image, '{}/{}.jpg'.format(OUTPUT_DIR, epoch + 1))