forked from fastai/fastai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vision.gan.html
841 lines (570 loc) · 60.9 KB
/
vision.gan.html
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
---
title: vision.gan
keywords: fastai
sidebar: home_sidebar
summary: "All the modules and callbacks necessary to train a GAN"
---
<!--
#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: docs_src/vision.gan.ipynb
# instructions: https://docs.fast.ai/gen_doc_main.html
-->
<div class="container" id="notebook-container">
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="GANs">GANs<a class="anchor-link" href="#GANs">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>GAN stands for <a href="https://arxiv.org/pdf/1406.2661.pdf">Generative Adversarial Nets</a> and were invented by Ian Goodfellow. The concept is that we will train two models at the same time: a generator and a critic. The generator will try to make new images similar to the ones in our dataset, and the critic's job will try to classify real images from the fake ones the generator does. The generator returns images, the discriminator a feature map (it can be a single number depending on the input size). Usually the discriminator will be trained to return 0. everywhere for fake images and 1. everywhere for real ones.</p>
<p>This module contains all the necessary function to create a GAN.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We train them against each other in the sense that at each step (more or less), we:</p>
<ol>
<li>Freeze the generator and train the discriminator for one step by:<ul>
<li>getting one batch of true images (let's call that <code>real</code>)</li>
<li>generating one batch of fake images (let's call that <code>fake</code>)</li>
<li>have the discriminator evaluate each batch and compute a loss function from that; the important part is that it rewards positively the detection of real images and penalizes the fake ones</li>
<li>update the weights of the discriminator with the gradients of this loss</li>
</ul>
</li>
</ol>
<ol>
<li>Freeze the discriminator and train the generator for one step by:<ul>
<li>generating one batch of fake images</li>
<li>evaluate the discriminator on it</li>
<li>return a loss that rewards posisitivly the discriminator thinking those are real images; the important part is that it rewards positively the detection of real images and penalizes the fake ones</li>
<li>update the weights of the generator with the gradients of this loss</li>
</ul>
</li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h2 id="GANLearner" class="doc_header"><code>class</code> <code>GANLearner</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L204" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANLearner-pytest" style="float:right; padding-right:10px">[test]</a></h2><blockquote><p><code>GANLearner</code>(<strong><code>data</code></strong>:<a href="/basic_data.html#DataBunch"><code>DataBunch</code></a>, <strong><code>generator</code></strong>:<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module"><code>Module</code></a>, <strong><code>critic</code></strong>:<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module"><code>Module</code></a>, <strong><code>gen_loss_func</code></strong>:<code>LossFunction</code>, <strong><code>crit_loss_func</code></strong>:<code>LossFunction</code>, <strong><code>switcher</code></strong>:<a href="/callback.html#Callback"><code>Callback</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>gen_first</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong><code>switch_eval</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>show_img</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>clip</code></strong>:<code>float</code>=<strong><em><code>None</code></em></strong>, <strong>**<code>learn_kwargs</code></strong>) :: <a href="/basic_train.html#Learner"><code>Learner</code></a></p>
</blockquote>
<div class="collapse" id="GANLearner-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANLearner-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>GANLearner</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>A <a href="/basic_train.html#Learner"><code>Learner</code></a> suitable for GANs.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This is the general constructor to create a GAN, you might want to use one of the factory methods that are easier to use. Create a GAN from <a href="/vision.data.html#vision.data"><code>data</code></a>, a <code>generator</code> and a <code>critic</code>. The <a href="/vision.data.html#vision.data"><code>data</code></a> should have the inputs the <code>generator</code> will expect and the images wanted as targets.</p>
<p><code>gen_loss_func</code> is the loss function that will be applied to the <code>generator</code>. It takes three argument <code>fake_pred</code>, <code>target</code>, <code>output</code> and should return a rank 0 tensor. <code>output</code> is the result of the <code>generator</code> applied to the input (the xs of the batch), <code>target</code> is the ys of the batch and <code>fake_pred</code> is the result of the <code>discriminator</code> being given <code>output</code>. <code>output</code>and <code>target</code> can be used to add a specific loss to the GAN loss (pixel loss, feature loss) and for a good training of the gan, the loss should encourage <code>fake_pred</code> to be as close to 1 as possible (the <code>generator</code> is trained to fool the <code>critic</code>).</p>
<p><code>crit_loss_func</code> is the loss function that will be applied to the <code>critic</code>. It takes two arguments <code>real_pred</code> and <code>fake_pred</code>. <code>real_pred</code> is the result of the <code>critic</code> on the target images (the ys of the batch) and <code>fake_pred</code> is the result of the <code>critic</code> applied on a batch of fake, generated byt the <code>generator</code> from the xs of the batch.</p>
<p><code>switcher</code> is a <a href="/callback.html#Callback"><code>Callback</code></a> that should tell the GAN when to switch from critic to generator and vice versa. By default it does 5 iterations of the critic for 1 iteration of the generator. The model begins the training with the <code>generator</code> if <code>gen_first=True</code>. If <code>switch_eval=True</code>, the model that isn't trained is switched on eval mode (left in training mode otherwise, which means some statistics like the running mean in batchnorm layers are updated, or the dropouts are applied).</p>
<p><code>clip</code> should be set to a certain value if one wants to clip the weights (see the <a href="https://arxiv.org/pdf/1701.07875.pdf">Wassertein GAN</a> for instance).</p>
<p>If <code>show_img=True</code>, one image generated by the GAN is shown at the end of each epoch.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Factory-methods">Factory methods<a class="anchor-link" href="#Factory-methods">¶</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANLearner.from_learners" class="doc_header"><code>from_learners</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L217" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANLearner-from_learners-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>from_learners</code>(<strong><code>learn_gen</code></strong>:<a href="/basic_train.html#Learner"><code>Learner</code></a>, <strong><code>learn_crit</code></strong>:<a href="/basic_train.html#Learner"><code>Learner</code></a>, <strong><code>switcher</code></strong>:<a href="/callback.html#Callback"><code>Callback</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>weights_gen</code></strong>:<code>Point</code>=<strong><em><code>None</code></em></strong>, <strong>**<code>learn_kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="GANLearner-from_learners-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANLearner-from_learners-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>from_learners</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Create a GAN from <code>learn_gen</code> and <code>learn_crit</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Directly creates a <a href="/vision.gan.html#GANLearner"><code>GANLearner</code></a> from two <a href="/basic_train.html#Learner"><code>Learner</code></a>: one for the <code>generator</code> and one for the <code>critic</code>. The <code>switcher</code> and all <code>kwargs</code> will be passed to the initialization of <a href="/vision.gan.html#GANLearner"><code>GANLearner</code></a> along with the following loss functions:</p>
<ul>
<li><code>loss_func_crit</code> is the mean of <code>learn_crit.loss_func</code> applied to <code>real_pred</code> and a target of ones with <code>learn_crit.loss_func</code> applied to <code>fake_pred</code> and a target of zeros</li>
<li><code>loss_func_gen</code> is the mean of <code>learn_crit.loss_func</code> applied to <code>fake_pred</code> and a target of ones (to full the discriminator) with <code>learn_gen.loss_func</code> applied to <code>output</code> and <code>target</code>. The weights of each of those contributions can be passed in <code>weights_gen</code> (default is 1. and 1.)</li>
</ul>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANLearner.wgan" class="doc_header"><code>wgan</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L224" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANLearner-wgan-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>wgan</code>(<strong><code>data</code></strong>:<a href="/basic_data.html#DataBunch"><code>DataBunch</code></a>, <strong><code>generator</code></strong>:<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module"><code>Module</code></a>, <strong><code>critic</code></strong>:<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module"><code>Module</code></a>, <strong><code>switcher</code></strong>:<a href="/callback.html#Callback"><code>Callback</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>clip</code></strong>:<code>float</code>=<strong><em><code>0.01</code></em></strong>, <strong>**<code>learn_kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="GANLearner-wgan-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANLearner-wgan-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>wgan</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Create a WGAN from <code>data</code>, <code>generator</code> and <code>critic</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The Wasserstein GAN is detailed in [this article]. <code>switcher</code> and the <code>kwargs</code> will be passed to the <a href="/vision.gan.html#GANLearner"><code>GANLearner</code></a> init, <code>clip</code>is the weight clipping.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Switchers">Switchers<a class="anchor-link" href="#Switchers">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>In any GAN training, you will need to tell the <a href="/basic_train.html#Learner"><code>Learner</code></a> when to switch from generator to critic and vice versa. The two following <a href="/callback.html#Callback"><code>Callback</code></a> are examples to help you with that.</p>
<p>As usual, don't call the <code>on_something</code> methods directly, the fastai library will do it for you during training.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="FixedGANSwitcher" class="doc_header"><code>class</code> <code>FixedGANSwitcher</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L151" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#FixedGANSwitcher-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>FixedGANSwitcher</code>(<strong><code>learn</code></strong>:<a href="/basic_train.html#Learner"><code>Learner</code></a>, <strong><code>n_crit</code></strong>:<code>Union</code>[<code>int</code>, <code>Callable</code>]=<strong><em><code>1</code></em></strong>, <strong><code>n_gen</code></strong>:<code>Union</code>[<code>int</code>, <code>Callable</code>]=<strong><em><code>1</code></em></strong>) :: <a href="/basic_train.html#LearnerCallback"><code>LearnerCallback</code></a></p>
</blockquote>
<div class="collapse" id="FixedGANSwitcher-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#FixedGANSwitcher-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>FixedGANSwitcher</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Switcher to do <code>n_crit</code> iterations of the critic then <code>n_gen</code> iterations of the generator.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="FixedGANSwitcher.on_train_begin" class="doc_header"><code>on_train_begin</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L157" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#FixedGANSwitcher-on_train_begin-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_train_begin</code>(<strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="FixedGANSwitcher-on_train_begin-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#FixedGANSwitcher-on_train_begin-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_train_begin</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Initiate the iteration counts.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="FixedGANSwitcher.on_batch_end" class="doc_header"><code>on_batch_end</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L161" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#FixedGANSwitcher-on_batch_end-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_batch_end</code>(<strong><code>iteration</code></strong>, <strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="FixedGANSwitcher-on_batch_end-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#FixedGANSwitcher-on_batch_end-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_batch_end</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Switch the model if necessary.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="AdaptiveGANSwitcher" class="doc_header"><code>class</code> <code>AdaptiveGANSwitcher</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L175" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#AdaptiveGANSwitcher-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>AdaptiveGANSwitcher</code>(<strong><code>learn</code></strong>:<a href="/basic_train.html#Learner"><code>Learner</code></a>, <strong><code>gen_thresh</code></strong>:<code>float</code>=<strong><em><code>None</code></em></strong>, <strong><code>critic_thresh</code></strong>:<code>float</code>=<strong><em><code>None</code></em></strong>) :: <a href="/basic_train.html#LearnerCallback"><code>LearnerCallback</code></a></p>
</blockquote>
<div class="collapse" id="AdaptiveGANSwitcher-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#AdaptiveGANSwitcher-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>AdaptiveGANSwitcher</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Switcher that goes back to generator/critic when the loss goes below <code>gen_thresh</code>/<code>crit_thresh</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="AdaptiveGANSwitcher.on_batch_end" class="doc_header"><code>on_batch_end</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L181" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#AdaptiveGANSwitcher-on_batch_end-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_batch_end</code>(<strong><code>last_loss</code></strong>, <strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="AdaptiveGANSwitcher-on_batch_end-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#AdaptiveGANSwitcher-on_batch_end-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_batch_end</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Switch the model if necessary.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Discriminative-LR">Discriminative LR<a class="anchor-link" href="#Discriminative-LR">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>If you want to train your critic at a different learning rate than the generator, this will let you do it automatically (even if you have a learning rate schedule).</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="GANDiscriminativeLR" class="doc_header"><code>class</code> <code>GANDiscriminativeLR</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L277" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANDiscriminativeLR-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>GANDiscriminativeLR</code>(<strong><code>learn</code></strong>:<a href="/basic_train.html#Learner"><code>Learner</code></a>, <strong><code>mult_lr</code></strong>:<code>float</code>=<strong><em><code>5.0</code></em></strong>) :: <a href="/basic_train.html#LearnerCallback"><code>LearnerCallback</code></a></p>
</blockquote>
<div class="collapse" id="GANDiscriminativeLR-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANDiscriminativeLR-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>GANDiscriminativeLR</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p><a href="/callback.html#Callback"><code>Callback</code></a> that handles multiplying the learning rate by <code>mult_lr</code> for the critic.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANDiscriminativeLR.on_batch_begin" class="doc_header"><code>on_batch_begin</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L283" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANDiscriminativeLR-on_batch_begin-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_batch_begin</code>(<strong><code>train</code></strong>, <strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="GANDiscriminativeLR-on_batch_begin-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANDiscriminativeLR-on_batch_begin-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_batch_begin</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Multiply the current lr if necessary.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANDiscriminativeLR.on_step_end" class="doc_header"><code>on_step_end</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L287" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANDiscriminativeLR-on_step_end-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_step_end</code>(<strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="GANDiscriminativeLR-on_step_end-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANDiscriminativeLR-on_step_end-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_step_end</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Put the LR back to its value if necessary.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Specific-models">Specific models<a class="anchor-link" href="#Specific-models">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="basic_critic" class="doc_header"><code>basic_critic</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L17" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#basic_critic-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>basic_critic</code>(<strong><code>in_size</code></strong>:<code>int</code>, <strong><code>n_channels</code></strong>:<code>int</code>, <strong><code>n_features</code></strong>:<code>int</code>=<strong><em><code>64</code></em></strong>, <strong><code>n_extra_layers</code></strong>:<code>int</code>=<strong><em><code>0</code></em></strong>, <strong>**<code>conv_kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="basic_critic-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#basic_critic-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>basic_critic</code>:</p><ul><li><code>pytest -sv tests/test_vision_gan.py::test_basic_critic</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_vision_gan.py#L56" class="source_link" style="float:right">[source]</a></li></ul><p>Some other tests where <code>basic_critic</code> is used:</p><ul><li><code>pytest -sv tests/test_vision_gan.py::test_gan_module</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_vision_gan.py#L67" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>A basic critic for images <code>n_channels</code> x <code>in_size</code> x <code>in_size</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This model contains a first 4 by 4 convolutional layer of stride 2 from <code>n_channels</code> to <code>n_features</code> followed by <code>n_extra_layers</code> 3 by 3 convolutional layer of stride 1. Then we put as many 4 by 4 convolutional layer of stride 2 with a number of features multiplied by 2 at each stage so that the <code>in_size</code> becomes 1. <code>kwargs</code> can be used to customize the convolutional layers and are passed to <a href="/layers.html#conv_layer"><code>conv_layer</code></a>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="basic_generator" class="doc_header"><code>basic_generator</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L28" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#basic_generator-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>basic_generator</code>(<strong><code>in_size</code></strong>:<code>int</code>, <strong><code>n_channels</code></strong>:<code>int</code>, <strong><code>noise_sz</code></strong>:<code>int</code>=<strong><em><code>100</code></em></strong>, <strong><code>n_features</code></strong>:<code>int</code>=<strong><em><code>64</code></em></strong>, <strong><code>n_extra_layers</code></strong>=<strong><em><code>0</code></em></strong>, <strong>**<code>conv_kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="basic_generator-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#basic_generator-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>basic_generator</code>:</p><ul><li><code>pytest -sv tests/test_vision_gan.py::test_basic_generator</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_vision_gan.py#L46" class="source_link" style="float:right">[source]</a></li></ul><p>Some other tests where <code>basic_generator</code> is used:</p><ul><li><code>pytest -sv tests/test_vision_gan.py::test_gan_module</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_vision_gan.py#L67" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>A basic generator from <code>noise_sz</code> to images <code>n_channels</code> x <code>in_size</code> x <code>in_size</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This model contains a first 4 by 4 transposed convolutional layer of stride 1 from <code>noise_size</code> to the last numbers of features of the corresponding critic. Then we put as many 4 by 4 transposed convolutional layer of stride 2 with a number of features divided by 2 at each stage so that the image ends up being of height and widht <code>in_size//2</code>. At the end, we add<code>n_extra_layers</code> 3 by 3 convolutional layer of stride 1. The last layer is a transpose convolution of size 4 by 4 and stride 2 followed by <code>tanh</code>. <code>kwargs</code> can be used to customize the convolutional layers and are passed to <a href="/layers.html#conv_layer"><code>conv_layer</code></a>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="gan_critic" class="doc_header"><code>gan_critic</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L260" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#gan_critic-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>gan_critic</code>(<strong><code>n_channels</code></strong>:<code>int</code>=<strong><em><code>3</code></em></strong>, <strong><code>nf</code></strong>:<code>int</code>=<strong><em><code>128</code></em></strong>, <strong><code>n_blocks</code></strong>:<code>int</code>=<strong><em><code>3</code></em></strong>, <strong><code>p</code></strong>:<code>int</code>=<strong><em><code>0.15</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="gan_critic-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#gan_critic-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>gan_critic</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Critic to train a <code>GAN</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h2 id="GANTrainer" class="doc_header"><code>class</code> <code>GANTrainer</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L71" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANTrainer-pytest" style="float:right; padding-right:10px">[test]</a></h2><blockquote><p><code>GANTrainer</code>(<strong><code>learn</code></strong>:<a href="/basic_train.html#Learner"><code>Learner</code></a>, <strong><code>switch_eval</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong><code>clip</code></strong>:<code>float</code>=<strong><em><code>None</code></em></strong>, <strong><code>beta</code></strong>:<code>float</code>=<strong><em><code>0.98</code></em></strong>, <strong><code>gen_first</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong><code>show_img</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>) :: <a href="/basic_train.html#LearnerCallback"><code>LearnerCallback</code></a></p>
</blockquote>
<div class="collapse" id="GANTrainer-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANTrainer-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>GANTrainer</code>:</p><ul><li><code>pytest -sv tests/test_vision_gan.py::test_gan_trainer</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_vision_gan.py#L80" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Handles GAN Training.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><a href="/basic_train.html#LearnerCallback"><code>LearnerCallback</code></a> that will be responsible to handle the two different optimizers (one for the generator and one for the critic), and do all the work behind the scenes so that the generator (or the critic) are in training mode with parameters requirement gradients each time we switch.</p>
<p><code>switch_eval=True</code> means that the <a href="/vision.gan.html#GANTrainer"><code>GANTrainer</code></a> will put the model that isn't training into eval mode (if it's <code>False</code> its running statistics like in batchnorm layers will be updated and dropout will be applied). <code>clip</code> is the clipping applied to the weights (if not <code>None</code>). <code>beta</code> is the coefficient for the moving averages as the <a href="/vision.gan.html#GANTrainer"><code>GANTrainer</code></a>tracks separately the generator loss and the critic loss. <code>gen_first=True</code> means the training begins with the generator (with the critic if it's <code>False</code>). If <code>show_img=True</code> we show a generated image at the end of each epoch.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANTrainer.switch" class="doc_header"><code>switch</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L143" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANTrainer-switch-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>switch</code>(<strong><code>gen_mode</code></strong>:<code>bool</code>=<strong><em><code>None</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="GANTrainer-switch-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANTrainer-switch-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>switch</code>:</p><p>Some other tests where <code>switch</code> is used:</p><ul><li><code>pytest -sv tests/test_vision_gan.py::test_gan_module</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_vision_gan.py#L67" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Switch the model, if <code>gen_mode</code> is provided, in the desired mode.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>If <code>gen_mode</code> is left as <code>None</code>, just put the model in the other mode (critic if it was in generator mode and vice versa).</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANTrainer.on_train_begin" class="doc_header"><code>on_train_begin</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L89" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANTrainer-on_train_begin-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_train_begin</code>(<strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="GANTrainer-on_train_begin-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANTrainer-on_train_begin-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_train_begin</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Create the optimizers for the generator and critic if necessary, initialize smootheners.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANTrainer.on_epoch_begin" class="doc_header"><code>on_epoch_begin</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L126" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANTrainer-on_epoch_begin-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_epoch_begin</code>(<strong><code>epoch</code></strong>, <strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="GANTrainer-on_epoch_begin-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANTrainer-on_epoch_begin-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_epoch_begin</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Put the critic or the generator back to eval if necessary.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANTrainer.on_batch_begin" class="doc_header"><code>on_batch_begin</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L109" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANTrainer-on_batch_begin-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_batch_begin</code>(<strong><code>last_input</code></strong>, <strong><code>last_target</code></strong>, <strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="GANTrainer-on_batch_begin-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANTrainer-on_batch_begin-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_batch_begin</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Clamp the weights with <code>self.clip</code> if it's not None, return the correct input.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANTrainer.on_backward_begin" class="doc_header"><code>on_backward_begin</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L115" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANTrainer-on_backward_begin-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_backward_begin</code>(<strong><code>last_loss</code></strong>, <strong><code>last_output</code></strong>, <strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="GANTrainer-on_backward_begin-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANTrainer-on_backward_begin-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_backward_begin</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Record <code>last_loss</code> in the proper list.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANTrainer.on_epoch_end" class="doc_header"><code>on_epoch_end</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L130" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANTrainer-on_epoch_end-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_epoch_end</code>(<strong><code>pbar</code></strong>, <strong><code>epoch</code></strong>, <strong><code>last_metrics</code></strong>, <strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="GANTrainer-on_epoch_end-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANTrainer-on_epoch_end-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_epoch_end</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Put the various losses in the recorder and show a sample image.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANTrainer.on_train_end" class="doc_header"><code>on_train_end</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L105" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANTrainer-on_train_end-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_train_end</code>(<strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="GANTrainer-on_train_end-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANTrainer-on_train_end-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_train_end</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Switch in generator mode for showing results.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Specific-modules">Specific modules<a class="anchor-link" href="#Specific-modules">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="GANModule" class="doc_header"><code>class</code> <code>GANModule</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L41" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANModule-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>GANModule</code>(<strong><code>generator</code></strong>:<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module"><code>Module</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>critic</code></strong>:<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module"><code>Module</code></a>=<strong><em><code>None</code></em></strong>, <strong><code>gen_mode</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>) :: <a href="/core.html#PrePostInitMeta"><code>PrePostInitMeta</code></a> :: <a href="/torch_core.html#Module"><code>Module</code></a></p>
</blockquote>
<div class="collapse" id="GANModule-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANModule-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>GANModule</code>:</p><ul><li><code>pytest -sv tests/test_vision_gan.py::test_gan_module</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_vision_gan.py#L67" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Wrapper around a <code>generator</code> and a <code>critic</code> to create a GAN.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>If <code>gen_mode</code> is left as <code>None</code>, just put the model in the other mode (critic if it was in generator mode and vice versa).</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANModule.switch" class="doc_header"><code>switch</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L50" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANModule-switch-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>switch</code>(<strong><code>gen_mode</code></strong>:<code>bool</code>=<strong><em><code>None</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="GANModule-switch-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANModule-switch-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>switch</code>:</p><p>Some other tests where <code>switch</code> is used:</p><ul><li><code>pytest -sv tests/test_vision_gan.py::test_gan_module</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_vision_gan.py#L67" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Put the model in generator mode if <code>gen_mode</code>, in critic mode otherwise.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="GANLoss" class="doc_header"><code>class</code> <code>GANLoss</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L54" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANLoss-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>GANLoss</code>(<strong><code>loss_funcG</code></strong>:<code>Callable</code>, <strong><code>loss_funcC</code></strong>:<code>Callable</code>, <strong><code>gan_model</code></strong>:<a href="/vision.gan.html#GANModule"><code>GANModule</code></a>) :: <a href="/core.html#PrePostInitMeta"><code>PrePostInitMeta</code></a> :: <a href="/vision.gan.html#GANModule"><code>GANModule</code></a></p>
</blockquote>
<div class="collapse" id="GANLoss-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANLoss-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>GANLoss</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Wrapper around <code>loss_funcC</code> (for the critic) and <code>loss_funcG</code> (for the generator).</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="AdaptiveLoss" class="doc_header"><code>class</code> <code>AdaptiveLoss</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L291" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#AdaptiveLoss-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>AdaptiveLoss</code>(<strong><code>crit</code></strong>) :: <a href="/core.html#PrePostInitMeta"><code>PrePostInitMeta</code></a> :: <a href="/torch_core.html#Module"><code>Module</code></a></p>
</blockquote>
<div class="collapse" id="AdaptiveLoss-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#AdaptiveLoss-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>AdaptiveLoss</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Expand the <code>target</code> to match the <code>output</code> size before applying <code>crit</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="accuracy_thresh_expand" class="doc_header"><code>accuracy_thresh_expand</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L299" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#accuracy_thresh_expand-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>accuracy_thresh_expand</code>(<strong><code>y_pred</code></strong>:<code>Tensor</code>, <strong><code>y_true</code></strong>:<code>Tensor</code>, <strong><code>thresh</code></strong>:<code>float</code>=<strong><em><code>0.5</code></em></strong>, <strong><code>sigmoid</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>) → <code>Rank0Tensor</code></p>
</blockquote>
<div class="collapse" id="accuracy_thresh_expand-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#accuracy_thresh_expand-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>accuracy_thresh_expand</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Compute accuracy after expanding <code>y_true</code> to the size of <code>y_pred</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Data-Block-API">Data Block API<a class="anchor-link" href="#Data-Block-API">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="NoisyItem" class="doc_header"><code>class</code> <code>NoisyItem</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L229" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#NoisyItem-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>NoisyItem</code>(<strong><code>noise_sz</code></strong>) :: <a href="/core.html#ItemBase"><code>ItemBase</code></a></p>
</blockquote>
<div class="collapse" id="NoisyItem-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#NoisyItem-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>NoisyItem</code>:</p><ul><li><code>pytest -sv tests/test_vision_gan.py::test_noisy_item</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_vision_gan.py#L37" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>An random <a href="/core.html#ItemBase"><code>ItemBase</code></a> of size <code>noise_sz</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h3 id="GANItemList" class="doc_header"><code>class</code> <code>GANItemList</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L235" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANItemList-pytest" style="float:right; padding-right:10px">[test]</a></h3><blockquote><p><code>GANItemList</code>(<strong><code>items</code></strong>, <strong><code>noise_sz</code></strong>:<code>int</code>=<strong><em><code>100</code></em></strong>, <strong>**<code>kwargs</code></strong>) :: <a href="/vision.data.html#ImageList"><code>ImageList</code></a></p>
</blockquote>
<div class="collapse" id="GANItemList-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANItemList-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>GANItemList</code>:</p><p>Some other tests where <code>GANItemList</code> is used:</p><ul><li><code>pytest -sv tests/test_vision_gan.py::test_gan_datasets</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_vision_gan.py#L30" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p><code>ItemList</code> suitable for GANs.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Inputs will be <a href="/vision.gan.html#NoisyItem"><code>NoisyItem</code></a> of <code>noise_sz</code> while the default class for target is <a href="/vision.data.html#ImageList"><code>ImageList</code></a>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANItemList.show_xys" class="doc_header"><code>show_xys</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L247" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANItemList-show_xys-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>show_xys</code>(<strong><code>xs</code></strong>, <strong><code>ys</code></strong>, <strong><code>imgsize</code></strong>:<code>int</code>=<strong><em><code>4</code></em></strong>, <strong><code>figsize</code></strong>:<code>Optional</code>[<code>Tuple</code>[<code>int</code>, <code>int</code>]]=<strong><em><code>None</code></em></strong>, <strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="GANItemList-show_xys-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANItemList-show_xys-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>show_xys</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Shows <code>ys</code> (target images) on a figure of <code>figsize</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GANItemList.show_xyzs" class="doc_header"><code>show_xyzs</code><a href="https://github.com/fastai/fastai/blob/master/fastai/vision/gan.py#L251" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GANItemList-show_xyzs-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>show_xyzs</code>(<strong><code>xs</code></strong>, <strong><code>ys</code></strong>, <strong><code>zs</code></strong>, <strong><code>imgsize</code></strong>:<code>int</code>=<strong><em><code>4</code></em></strong>, <strong><code>figsize</code></strong>:<code>Optional</code>[<code>Tuple</code>[<code>int</code>, <code>int</code>]]=<strong><em><code>None</code></em></strong>, <strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<div class="collapse" id="GANItemList-show_xyzs-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GANItemList-show_xyzs-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>show_xyzs</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Shows <code>zs</code> (generated images) on a figure of <code>figsize</code>.</p>
</div>
</div>
</div>
</div>
</div>
</div>