-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcycleGAN.txt
1847 lines (1770 loc) · 53.7 KB
/
cycleGAN.txt
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks
Jun-Yan Zhu
empty
Taesung Park1
Phillip Isola
Alexei A. Efros
Berkeley AI Research (BAIR) laboratory, UC Berkeley
Abstract
Image-to-image translation is a class of vision and graphics problems where the goal is to learn the mapping between an input image and an output image using a training set of aligned image pairs. However, for many tasks, paired training data will not be available. We present an approach for learning to translate an image from a source domain
�
to a target domain
�
in the absence of paired examples. Our goal is to learn a mapping
�
:
�
→
�
such that the distribution of images from
�
(
�
)
is indistinguishable from the distribution
�
using an adversarial loss. Because this mapping is highly under-constrained, we couple it with an inverse mapping
�
:
�
→
�
and introduce a cycle consistency loss to enforce
�
(
�
(
�
)
)
≈
�
(and vice versa). Qualitative results are presented on several tasks where paired training data does not exist, including collection style transfer, object transfiguration, season transfer, photo enhancement, etc. Quantitative comparisons against several prior methods demonstrate the superiority of our approach.
[Uncaptioned image]
Figure 1: Given any two unordered image collections
�
and
�
, our algorithm learns to automatically “translate” an image from one into the other and vice versa: (left) Monet paintings and landscape photos from Flickr; (center) zebras and horses from ImageNet; (right) summer and winter Yosemite photos from Flickr. Example application (bottom): using a collection of paintings of famous artists, our method learns to render natural photographs into the respective styles.
1Introduction
What did Claude Monet see as he placed his easel by the bank of the Seine near Argenteuil on a lovely spring day in 1873 (Figure 1, top-left)? A color photograph, had it been invented, may have documented a crisp blue sky and a glassy river reflecting it. Monet conveyed his impression of this same scene through wispy brush strokes and a bright palette.†
What if Monet had happened upon the little harbor in Cassis on a cool summer evening (Figure 1, bottom-left)? A brief stroll through a gallery of Monet paintings makes it possible to imagine how he would have rendered the scene: perhaps in pastel shades, with abrupt dabs of paint, and a somewhat flattened dynamic range.
Refer to caption
Figure 2:Paired training data (left) consists of training examples
{
�
�
,
�
�
}
�
=
1
�
, where the correspondence between
�
�
and
�
�
exists [22]. We instead consider unpaired training data (right), consisting of a source set
{
�
�
}
�
=
1
�
(
�
�
∈
�
) and a target set
{
�
�
}
�
=
1
�
(
�
�
∈
�
), with no information provided as to which
�
�
matches which
�
�
.
We can imagine all this despite never having seen a side by side example of a Monet painting next to a photo of the scene he painted. Instead, we have knowledge of the set of Monet paintings and of the set of landscape photographs. We can reason about the stylistic differences between these two sets, and thereby imagine what a scene might look like if we were to “translate” it from one set into the other.
In this paper, we present a method that can learn to do the same: capturing special characteristics of one image collection and figuring out how these characteristics could be translated into the other image collection, all in the absence of any paired training examples.
This problem can be more broadly described as image-to-image translation [22], converting an image from one representation of a given scene,
�
, to another,
�
, e.g., grayscale to color, image to semantic labels, edge-map to photograph. Years of research in computer vision, image processing, computational photography, and graphics have produced powerful translation systems in the supervised setting, where example image pairs
{
�
�
,
�
�
}
�
=
1
�
are available (Figure 2, left), e.g., [11, 19, 22, 23, 28, 33, 45, 56, 58, 62]. However, obtaining paired training data can be difficult and expensive. For example, only a couple of datasets exist for tasks like semantic segmentation (e.g., [4]), and they are relatively small. Obtaining input-output pairs for graphics tasks like artistic stylization can be even more difficult since the desired output is highly complex, typically requiring artistic authoring. For many tasks, like object transfiguration (e.g., zebra
↔
horse, Figure 1 top-middle), the desired output is not even well-defined.
We therefore seek an algorithm that can learn to translate between domains without paired input-output examples (Figure 2, right). We assume there is some underlying relationship between the domains – for example, that they are two different renderings of the same underlying scene – and seek to learn that relationship. Although we lack supervision in the form of paired examples, we can exploit supervision at the level of sets: we are given one set of images in domain
�
and a different set in domain
�
. We may train a mapping
�
:
�
→
�
such that the output
�
^
=
�
(
�
)
,
�
∈
�
, is indistinguishable from images
�
∈
�
by an adversary trained to classify
�
^
apart from
�
. In theory, this objective can induce an output distribution over
�
^
that matches the empirical distribution
�
�
�
�
�
(
�
)
(in general, this requires
�
to be stochastic) [16]. The optimal
�
thereby translates the domain
�
to a domain
�
^
distributed identically to
�
. However, such a translation does not guarantee that an individual input
�
and output
�
are paired up in a meaningful way – there are infinitely many mappings
�
that will induce the same distribution over
�
^
. Moreover, in practice, we have found it difficult to optimize the adversarial objective in isolation: standard procedures often lead to the well-known problem of mode collapse, where all input images map to the same output image and the optimization fails to make progress [15].
These issues call for adding more structure to our objective. Therefore, we exploit the property that translation should be “cycle consistent”, in the sense that if we translate, e.g., a sentence from English to French, and then translate it back from French to English, we should arrive back at the original sentence [3]. Mathematically, if we have a translator
�
:
�
→
�
and another translator
�
:
�
→
�
, then
�
and
�
should be inverses of each other, and both mappings should be bijections. We apply this structural assumption by training both the mapping
�
and
�
simultaneously, and adding a cycle consistency loss [64] that encourages
�
(
�
(
�
)
)
≈
�
and
�
(
�
(
�
)
)
≈
�
. Combining this loss with adversarial losses on domains
�
and
�
yields our full objective for unpaired image-to-image translation.
We apply our method to a wide range of applications, including collection style transfer, object transfiguration, season transfer and photo enhancement. We also compare against previous approaches that rely either on hand-defined factorizations of style and content, or on shared embedding functions, and show that our method outperforms these baselines. We provide both PyTorch and Torch implementations. Check out more results at our website.
Refer to caption
Figure 3:(a) Our model contains two mapping functions
�
:
�
→
�
and
�
:
�
→
�
, and associated adversarial discriminators
�
�
and
�
�
.
�
�
encourages
�
to translate
�
into outputs indistinguishable from domain
�
, and vice versa for
�
�
and
�
. To further regularize the mappings, we introduce two cycle consistency losses that capture the intuition that if we translate from one domain to the other and back again we should arrive at where we started: (b) forward cycle-consistency loss:
�
→
�
(
�
)
→
�
(
�
(
�
)
)
≈
�
, and (c) backward cycle-consistency loss:
�
→
�
(
�
)
→
�
(
�
(
�
)
)
≈
�
2Related work
Generative Adversarial Networks (GANs) [16, 63] have achieved impressive results in image generation [6, 39], image editing [66], and representation learning [39, 43, 37]. Recent methods adopt the same idea for conditional image generation applications, such as text2image [41], image inpainting [38], and future prediction [36], as well as to other domains like videos [54] and 3D data [57]. The key to GANs’ success is the idea of an adversarial loss that forces the generated images to be, in principle, indistinguishable from real photos. This loss is particularly powerful for image generation tasks, as this is exactly the objective that much of computer graphics aims to optimize. We adopt an adversarial loss to learn the mapping such that the translated images cannot be distinguished from images in the target domain.
Image-to-Image Translation The idea of image-to-image translation goes back at least to Hertzmann et al.’s Image Analogies [19], who employ a non-parametric texture model [10] on a single input-output training image pair. More recent approaches use a dataset of input-output examples to learn a parametric translation function using CNNs (e.g., [33]). Our approach builds on the “pix2pix” framework of Isola et al. [22], which uses a conditional generative adversarial network [16] to learn a mapping from input to output images. Similar ideas have been applied to various tasks such as generating photographs from sketches [44] or from attribute and semantic layouts [25]. However, unlike the above prior work, we learn the mapping without paired training examples.
Unpaired Image-to-Image Translation Several other methods also tackle the unpaired setting, where the goal is to relate two data domains:
�
and
�
. Rosales et al. [42] propose a Bayesian framework that includes a prior based on a patch-based Markov random field computed from a source image and a likelihood term obtained from multiple style images. More recently, CoGAN [32] and cross-modal scene networks [1] use a weight-sharing strategy to learn a common representation across domains. Concurrent to our method, Liu et al. [31] extends the above framework with a combination of variational autoencoders [27] and generative adversarial networks [16]. Another line of concurrent work [46, 49, 2] encourages the input and output to share specific “content” features even though they may differ in “style“. These methods also use adversarial networks, with additional terms to enforce the output to be close to the input in a predefined metric space, such as class label space [2], image pixel space [46], and image feature space [49].
Unlike the above approaches, our formulation does not rely on any task-specific, predefined similarity function between the input and output, nor do we assume that the input and output have to lie in the same low-dimensional embedding space. This makes our method a general-purpose solution for many vision and graphics tasks. We directly compare against several prior and contemporary approaches in Section 5.1.
Cycle Consistency The idea of using transitivity as a way to regularize structured data has a long history. In visual tracking, enforcing simple forward-backward consistency has been a standard trick for decades [24, 48]. In the language domain, verifying and improving translations via “back translation and reconciliation” is a technique used by human translators [3] (including, humorously, by Mark Twain [51]), as well as by machines [17]. More recently, higher-order cycle consistency has been used in structure from motion [61], 3D shape matching [21], co-segmentation [55], dense semantic alignment [65, 64], and depth estimation [14]. Of these, Zhou et al. [64] and Godard et al. [14] are most similar to our work, as they use a cycle consistency loss as a way of using transitivity to supervise CNN training. In this work, we are introducing a similar loss to push
�
and
�
to be consistent with each other. Concurrent with our work, in these same proceedings, Yi et al. [59] independently use a similar objective for unpaired image-to-image translation, inspired by dual learning in machine translation [17].
Neural Style Transfer [13, 23, 52, 12] is another way to perform image-to-image translation, which synthesizes a novel image by combining the content of one image with the style of another image (typically a painting) based on matching the Gram matrix statistics of pre-trained deep features. Our primary focus, on the other hand, is learning the mapping between two image collections, rather than between two specific images, by trying to capture correspondences between higher-level appearance structures. Therefore, our method can be applied to other tasks, such as painting
→
photo, object transfiguration, etc. where single sample transfer methods do not perform well. We compare these two methods in Section 5.2.
3Formulation
Our goal is to learn mapping functions between two domains
�
and
�
given training samples
{
�
�
}
�
=
1
�
where
�
�
∈
�
and
{
�
�
}
�
=
1
�
where
�
�
∈
�
1. We denote the data distribution as
�
∼
�
�
�
�
�
(
�
)
and
�
∼
�
�
�
�
�
(
�
)
. As illustrated in Figure 3 (a), our model includes two mappings
�
:
�
→
�
and
�
:
�
→
�
. In addition, we introduce two adversarial discriminators
�
�
and
�
�
, where
�
�
aims to distinguish between images
{
�
}
and translated images
{
�
(
�
)
}
; in the same way,
�
�
aims to discriminate between
{
�
}
and
{
�
(
�
)
}
. Our objective contains two types of terms: adversarial losses [16] for matching the distribution of generated images to the data distribution in the target domain; and cycle consistency losses to prevent the learned mappings
�
and
�
from contradicting each other.
3.1Adversarial Loss
We apply adversarial losses [16] to both mapping functions. For the mapping function
�
:
�
→
�
and its discriminator
�
�
, we express the objective as:
ℒ
GAN
(
�
,
�
�
,
�
,
�
)
=
𝔼
�
∼
�
data
(
�
)
[
log
�
�
(
�
)
]
+
𝔼
�
∼
�
data
(
�
)
[
log
(
1
−
�
�
(
�
(
�
)
)
]
,
(1)
where
�
tries to generate images
�
(
�
)
that look similar to images from domain
�
, while
�
�
aims to distinguish between translated samples
�
(
�
)
and real samples
�
.
�
aims to minimize this objective against an adversary
�
that tries to maximize it, i.e.,
min
�
max
�
�
ℒ
GAN
(
�
,
�
�
,
�
,
�
)
. We introduce a similar adversarial loss for the mapping function
�
:
�
→
�
and its discriminator
�
�
as well: i.e.,
min
�
max
�
�
ℒ
GAN
(
�
,
�
�
,
�
,
�
)
.
Refer to caption
Figure 4:The input images
�
, output images
�
(
�
)
and the reconstructed images
�
(
�
(
�
)
)
from various experiments. From top to bottom: photo
↔
Cezanne, horses
↔
zebras, winter
→
summer Yosemite, aerial photos
↔
Google maps.
3.2Cycle Consistency Loss
Adversarial training can, in theory, learn mappings
�
and
�
that produce outputs identically distributed as target domains
�
and
�
respectively (strictly speaking, this requires
�
and
�
to be stochastic functions) [15]. However, with large enough capacity, a network can map the same set of input images to any random permutation of images in the target domain, where any of the learned mappings can induce an output distribution that matches the target distribution. Thus, adversarial losses alone cannot guarantee that the learned function can map an individual input
�
�
to a desired output
�
�
. To further reduce the space of possible mapping functions, we argue that the learned mapping functions should be cycle-consistent: as shown in Figure 3 (b), for each image
�
from domain
�
, the image translation cycle should be able to bring
�
back to the original image, i.e.,
�
→
�
(
�
)
→
�
(
�
(
�
)
)
≈
�
. We call this forward cycle consistency. Similarly, as illustrated in Figure 3 (c), for each image
�
from domain
�
,
�
and
�
should also satisfy backward cycle consistency:
�
→
�
(
�
)
→
�
(
�
(
�
)
)
≈
�
. We incentivize this behavior using a cycle consistency loss:
ℒ
cyc
(
�
,
�
)
=
𝔼
�
∼
�
data
(
�
)
[
∥
�
(
�
(
�
)
)
−
�
∥
1
]
+
𝔼
�
∼
�
data
(
�
)
[
∥
�
(
�
(
�
)
)
−
�
∥
1
]
.
(2)
In preliminary experiments, we also tried replacing the L1 norm in this loss with an adversarial loss between
�
(
�
(
�
)
)
and
�
, and between
�
(
�
(
�
)
)
and
�
, but did not observe improved performance.
The behavior induced by the cycle consistency loss can be observed in Figure 4: the reconstructed images
�
(
�
(
�
)
)
end up matching closely to the input images
�
.
3.3Full Objective
Our full objective is:
ℒ
(
�
,
�
,
�
�
,
�
�
)
=
、
ℒ
GAN
(
�
,
�
�
,
�
,
�
)
+
ℒ
GAN
(
�
,
�
�
,
�
,
�
)
+
�
ℒ
cyc
(
�
,
�
)
,
(3)
where
�
controls the relative importance of the two objectives. We aim to solve:
�
∗
,
�
∗
=
arg
min
�
,
�
max
�
�
,
�
�
ℒ
(
�
,
�
,
�
�
,
�
�
)
.
(4)
Notice that our model can be viewed as training two “autoencoders” [20]: we learn one autoencoder
�
∘
�
:
�
→
�
jointly with another
�
∘
�
:
�
→
�
. However, these autoencoders each have special internal structures: they map an image to itself via an intermediate representation that is a translation of the image into another domain. Such a setup can also be seen as a special case of “adversarial autoencoders” [34], which use an adversarial loss to train the bottleneck layer of an autoencoder to match an arbitrary target distribution. In our case, the target distribution for the
�
→
�
autoencoder is that of the domain
�
.
In Section 5.1.4, we compare our method against ablations of the full objective, including the adversarial loss
ℒ
GAN
alone and the cycle consistency loss
ℒ
cyc
alone, and empirically show that both objectives play critical roles in arriving at high-quality results. We also evaluate our method with only cycle loss in one direction and show that a single cycle is not sufficient to regularize the training for this under-constrained problem.
4Implementation
Network Architecture
We adopt the architecture for our generative networks from Johnson et al. [23] who have shown impressive results for neural style transfer and super-resolution. This network contains three convolutions, several residual blocks [18], two fractionally-strided convolutions with stride
1
2
, and one convolution that maps features to RGB. We use
6
blocks for
128
×
128
images and
9
blocks for
256
×
256
and higher-resolution training images. Similar to Johnson et al. [23], we use instance normalization [53]. For the discriminator networks we use
70
×
70
PatchGANs [22, 30, 29], which aim to classify whether
70
×
70
overlapping image patches are real or fake. Such a patch-level discriminator architecture has fewer parameters than a full-image discriminator and can work on arbitrarily-sized images in a fully convolutional fashion [22].
Training details
We apply two techniques from recent works to stabilize our model training procedure. First, for
ℒ
GAN
(Equation 1), we replace the negative log likelihood objective by a least-squares loss [35]. This loss is more stable during training and generates higher quality results. In particular, for a GAN loss
ℒ
GAN
(
�
,
�
,
�
,
�