﻿import tensorflow as tf
import numpy as np
import scipy.io
import scipy.misc
from six.moves import xrange

def read_image(path):
    image = scipy.misc.imread(path)
    image = scipy.misc.imresize(image,(IMAGE_H,IMAGE_W))
    image = image[np.newaxis,:,:,:] 
    image = image - MEAN_VALUES
    return image

def write_image(path, image):
    image = image + MEAN_VALUES
    image = image[0]
    image = np.clip(image, 0, 255).astype('uint8')
    scipy.misc.imsave(path, image)

IMAGE_W = 800 
IMAGE_H = 600 
CONTENT_IMG = '..\\pilatus800.jpg'
STYLE_IMG = '..\\StarryNight.jpg'
INI_NOISE_RATIO = 0.5


MEAN_VALUES = np.array([128, 128, 128]).reshape((1,1,1,3))

def build_net(ntype, nin, nwb=None):
    if ntype == 'conv':
        return tf.nn.relu(tf.nn.conv2d(nin, nwb[0], strides=[1, 1, 1, 1], padding='SAME')+ nwb[1])
    elif ntype == 'pool':
        return tf.nn.avg_pool(nin, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')

def get_weight_bias(vgg_layers, i,):
    weights = vgg_layers[i][0][0][0][0][0]
    weights = tf.constant(weights)
    bias = vgg_layers[i][0][0][0][0][1]
    bias = tf.constant(np.reshape(bias, (bias.size)))
    return weights, bias

def build_vgg19(path):
    net = {}
    vgg_rawnet = scipy.io.loadmat(path)
    vgg_layers = vgg_rawnet['layers'][0]
    net['input'] = tf.Variable(np.zeros((1, IMAGE_H, IMAGE_W, 3)).astype('float32'))
    net['conv1_1'] = build_net('conv',net['input'],get_weight_bias(vgg_layers,0))
    net['conv1_2'] = build_net('conv',net['conv1_1'],get_weight_bias(vgg_layers,2))
    net['pool1']   = build_net('pool',net['conv1_2'])
    net['conv2_1'] = build_net('conv',net['pool1'],get_weight_bias(vgg_layers,5))
    net['conv2_2'] = build_net('conv',net['conv2_1'],get_weight_bias(vgg_layers,7))
    net['pool2']   = build_net('pool',net['conv2_2'])
    net['conv3_1'] = build_net('conv',net['pool2'],get_weight_bias(vgg_layers,10))
    net['conv3_2'] = build_net('conv',net['conv3_1'],get_weight_bias(vgg_layers,12))
    net['conv3_3'] = build_net('conv',net['conv3_2'],get_weight_bias(vgg_layers,14))
    net['conv3_4'] = build_net('conv',net['conv3_3'],get_weight_bias(vgg_layers,16))
    net['pool3']   = build_net('pool',net['conv3_4'])
    net['conv4_1'] = build_net('conv',net['pool3'],get_weight_bias(vgg_layers,19))
    net['conv4_2'] = build_net('conv',net['conv4_1'],get_weight_bias(vgg_layers,21))
    net['conv4_3'] = build_net('conv',net['conv4_2'],get_weight_bias(vgg_layers,23))
    net['conv4_4'] = build_net('conv',net['conv4_3'],get_weight_bias(vgg_layers,25))
    net['pool4']   = build_net('pool',net['conv4_4'])
    net['conv5_1'] = build_net('conv',net['pool4'],get_weight_bias(vgg_layers,28))
    net['conv5_2'] = build_net('conv',net['conv5_1'],get_weight_bias(vgg_layers,30))
    net['conv5_3'] = build_net('conv',net['conv5_2'],get_weight_bias(vgg_layers,32))
    net['conv5_4'] = build_net('conv',net['conv5_3'],get_weight_bias(vgg_layers,34))
    net['pool5']   = build_net('pool',net['conv5_4'])
    return net

def build_content_loss(p, x):
    M = p.shape[1]*p.shape[2]
    N = p.shape[3]
    loss = (1./(2* N**0.5 * M**0.5 )) * tf.reduce_sum(tf.pow((x - p),2))  
    return loss


def gram_matrix(x, area, depth):
    x1 = tf.reshape(x,(area,depth))
    g = tf.matmul(tf.transpose(x1), x1)
    return g

def gram_matrix_val(x, area, depth):
    x1 = x.reshape(area,depth)
    g = np.dot(x1.T, x1)
    return g

def build_style_loss(a, x):
    M = a.shape[1]*a.shape[2]
    N = a.shape[3]
    A = gram_matrix_val(a, M, N )
    G = gram_matrix(x, M, N )
    loss = (1./(4 * N**2 * M**2)) * tf.reduce_sum(tf.pow((G - A),2))
    return loss

STYLE_STRENGTH = 500

def main():
    net = build_vgg19('E:\\MNIST\\imagenet-vgg-verydeep-19.mat')
    sess = tf.Session()
    sess.run(tf.initialize_all_variables())
    noise_img = np.random.uniform(-20, 20, (1, IMAGE_H, IMAGE_W, 3)).astype('float32')
    content_img = read_image(CONTENT_IMG)
    style_img = read_image(STYLE_IMG)

    #CONTENT_LAYERS =[('conv4_2',1.)]
    #CONTENT_LAYERS =[('conv5_4',1.)]
    #STYLE_LAYERS=[('conv1_1',1.),('conv2_1',1.),('conv3_1',1.),('conv4_1',1.),('conv5_1',1.)]
    #STYLE_LAYERS=[('conv1_1',1.),('conv2_1',1.),('conv3_1',1.),('conv4_1',1.),('conv5_1',1.)]
        
    #sess.run([net['input'].assign(content_img)])
    
    #cost_content = 1.0*build_content_loss(sess.run(net['conv5_2']) ,  net['conv5_2'])
    
    sess.run([net['input'].assign(style_img)])
    
    c1 = build_style_loss(sess.run(net['conv1_1']) ,  net['conv1_1'])
    c2 = build_style_loss(sess.run(net['conv2_1']) ,  net['conv2_1'])
    c3 = build_style_loss(sess.run(net['conv3_1']) ,  net['conv3_1'])
    c4 = build_style_loss(sess.run(net['conv4_1']) ,  net['conv4_1'])
    c5 = build_style_loss(sess.run(net['conv5_1']) ,  net['conv5_1'])
    
    
    
    cost_style = ( c1 +
                   c2 +
                   c3 +
                   c4 +
                   c5)
    
    cost_total = cost_style#cost_content + STYLE_STRENGTH * 
    optimizer = tf.train.AdamOptimizer(1)
    
    #取消随机(未随机化的图像，输出更规整，细节上更倾向于原图)
    #去掉一层conv5_2 (没有影响)
    #去掉 cost_content (没有影响)
    #换成高一层的层 _4 （输出的style变了，圈圈没了， 圈圈貌似是在 5_1 里）
    
    
    train = optimizer.minimize(cost_total)
    sess.run(tf.initialize_all_variables())
    
    sess.run(net['input'].assign(content_img))
    #sess.run(net['input'].assign( INI_NOISE_RATIO* noise_img + (1.-INI_NOISE_RATIO) * content_img))


    for i in xrange(0,10000):
        sess.run(train)
        print(i,)
        if(i%20==0):
            print(sess.run(c1),sess.run(c2),sess.run(c3),sess.run(c4),sess.run(c5))
            
            
            result_img = sess.run(net['input'])
            #print(sess.run(cost_total))
            write_image('results'+str(i)+'.png',result_img)

    #write_image(str(i)+'results.png',result_img)
  
main()




2017-11-30 18:09:04.161619: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX
2017-11-30 18:09:04.267619: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1030] Found device 0 with properties: 
name: GeForce GTX 750 Ti major: 5 minor: 0 memoryClockRate(GHz): 1.0845
pciBusID: 0000:01:00.0
totalMemory: 2.00GiB freeMemory: 1.92GiB
2017-11-30 18:09:04.268619: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 750 Ti, pci bus id: 0000:01:00.0, compute capability: 5.0)
WARNING:tensorflow:From C:\Program Files\Python36\lib\site-packages\tensorflow\python\util\tf_should_use.py:107: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
2017-11-30 18:09:13.058619: W C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\bfc_allocator.cc:217] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.07GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.
2017-11-30 18:09:13.225619: W C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\bfc_allocator.cc:217] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.07GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.
0
156225.0
1.56043e+07
6.08612e+06
2.98209e+08
4130.85
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
125227.0
5.04617e+06
1.7988e+06
3.1157e+07
2185.14
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
112138.0
2.69753e+06
848697.0
1.05327e+07
1763.22
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
104731.0
1.93261e+06
554206.0
5.34699e+06
1587.05
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
99696.5
1.5632e+06
428431.0
3.40799e+06
1525.62
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
95784.0
1.33116e+06
353699.0
2.4753e+06
1476.88
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
92476.9
1.17034e+06
304111.0
1.92915e+06
1439.63
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
89539.1
1.0533e+06
269008.0
1.57126e+06
1405.59
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
86867.2
964427.0
242777.0
1.31919e+06
1373.16
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
84394.5
893463.0
221965.0
1.13265e+06
1344.41
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
82076.3
834529.0
204826.0
989882.0
1316.63
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
79885.6
784006.0
190242.0
877483.0
1291.48
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
77799.2
739633.0
177578.0
786543.0
1268.96
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
75800.6
699930.0
166345.0
711634.0
1248.2
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
73878.4
663978.0
156301.0
649141.0
1229.64
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
72025.0
631157.0
147212.0
595984.0
1212.1
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
70232.4
600908.0
138923.0
550062.0
1195.52
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
68496.0
572853.0
131309.0
510033.0
1179.95
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
66810.0
546704.0
124285.0
474855.0
1164.77
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
65170.9
522216.0
117793.0
443753.0
1149.73
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
63574.8
499209.0
111791.0
416102.0
1135.05
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
62021.8
477532.0
106217.0
391329.0
1120.9
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
60509.6
457061.0
101020.0
369048.0
1107.41
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
59036.9
437705.0
96187.1
348911.0
1094.39
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
57602.5
419387.0
91688.4
330641.0
1081.83
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
56204.0
402027.0
87480.2
313942.0
1069.7
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
54843.1
385575.0
83544.4
298682.0
1057.97
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
53517.1
369985.0
79872.9
284683.0
1046.72
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
52227.1
355193.0
76432.2
271842.0
1035.97
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
50970.8
341142.0
73201.2
260002.0
1025.35
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
49748.7
327792.0
70179.0
249030.0
1015.17
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
48559.4
315106.0
67345.2
238844.0
1005.36
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
47401.2
303040.0
64685.7
229393.0
996.023
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
46274.5
291551.0
62182.4
220602.0
987.208
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
45177.7
280604.0
59826.2
212414.0
978.954
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
44110.8
270165.0
57605.3
204764.0
970.611
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
43071.9
260212.0
55511.5
197579.0
962.305
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
42062.0
250716.0
53531.9
190817.0
954.495
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
41079.5
241649.0
51652.4
184450.0
947.089
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
40124.5
232993.0
49868.7
178456.0
939.802
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
39195.6
224732.0
48178.9
172821.0
932.54
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
38292.4
216843.0
46578.2
167499.0
925.372
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
37414.3
209302.0
45058.1
162459.0
918.536
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
36560.5
202092.0
43616.8
157704.0
912.155
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
35730.8
195197.0
42248.4
153210.0
906.159
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
34923.0
188605.0
40948.5
148950.0
900.253
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
34138.6
182298.0
39711.3
144886.0
894.195
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
33376.4
176262.0
38528.0
141013.0
888.409
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
32634.7
170481.0
37395.9
137323.0
882.709
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
31914.0
164941.0
36316.1
133800.0
877.033
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
31212.9
159632.0
35284.7
130424.0
871.438
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
30531.4
154544.0
34295.4
127187.0
865.804
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
29868.3
149665.0
33347.9
124086.0
860.184
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
29223.6
144986.0
32439.3
121119.0
854.709
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
28596.3
140492.0
31565.3
118277.0
849.309
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
27986.3
136177.0
30726.3
115544.0
844.0
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
27392.8
132032.0
29921.5
112916.0
838.712
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
26815.1
128050.0
29150.7
110393.0
833.616
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
26253.4
124219.0
28412.1
107967.0
828.638
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
25706.5
120534.0
27701.9
105639.0
823.571
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
25175.0
116991.0
27018.5
103396.0
818.686
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
24656.9
113583.0
26360.5
101235.0
813.914
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
24153.0
110301.0
25727.1
99151.9
808.987
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
23662.9
107142.0
25115.8
97141.8
804.243
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
23185.6
104102.0
24527.2
95199.7
799.662
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
22720.8
101173.0
23958.9
93324.3
795.244
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
22268.4
98349.2
23409.1
91515.9
790.88
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
21827.4
95625.7
22876.5
89770.6
786.56
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
21398.0
93001.1
22361.2
88086.2
782.335
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
20979.9
90470.8
21862.7
86458.5
778.197
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
20572.4
88031.0
21379.6
84885.2
774.042
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
20175.5
85677.5
20912.7
83363.0
770.037
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
19788.8
83404.9
20460.7
81891.2
766.089
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
19412.1
81212.0
20022.9
80463.3
762.138
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
19044.8
79095.1
19597.7
79077.1
758.245
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
18686.6
77050.3
19185.8
77731.8
754.39
1501
