๐Ÿ“Œ GAN ๋ญํ•˜๋Š” ์• ์•ผ?

๊น€๋‹น๊ทผยท2022๋…„ 3์›” 6์ผ
0

AIFFEL

๋ชฉ๋ก ๋ณด๊ธฐ
10/14

[GAN] (Generative Adversarial Network)

https://www.youtube.com/watch?v=BZwUR9hvBPE

GAN: Generative Adversarial Networks (๊ผผ๊ผผํ•œ ๋”ฅ๋Ÿฌ๋‹ ๋…ผ๋ฌธ ๋ฆฌ๋ทฐ์™€ ์ฝ”๋“œ ์‹ค์Šต)

์ƒ์„ฑ์  ์ ๋Œ€ ์‹ ๊ฒฝ๋ง์ด๋ผ๊ณ  ํ•œ๋‹ค.

๊ฒฝ์Ÿ์„ ํ†ตํ•ด์„œ ๋ฐฐ์šฐ๋Š” ๊ฐœ๋…์˜ ๊ธฐ์ˆ ์ด๋‹ค.

[https://youtu.be/N9ewzLUZhL8](https://youtu.be/N9ewzLUZhL8)

https://youtu.be/N9ewzLUZhL8

[https://youtu.be/8L11aMN5KY8](https://youtu.be/8L11aMN5KY8)

https://youtu.be/8L11aMN5KY8

[https://youtu.be/8L11aMN5KY8](https://youtu.be/8L11aMN5KY8)

https://youtu.be/8L11aMN5KY8

์‹ค์ œ ์žˆ์„๊ฒƒ ๊ฐ™์€ ๋ญ”๊ฐ€๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค ๊ด€์ ์—์„œ

์ƒ์„ฑ์„ ์ž˜ ํ•ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋ผ๋Š” ๊ด€์ ์œผ๋กœ ๋ณธ๋‹ค๋ฉด

Pix2Pix๋ผ๋Š” ๋ง์„ ์ด์šฉํ•˜๊ฒŒ ๋˜๋ฉด

์Šคํฌ๋ฆฐ์ƒท 2022-01-23 ์˜คํ›„ 11.50.10.png

Pix2Pix๋ผ๋Š” ๋„คํŠธ์›์ด ๋” ๋ฐœ์ „๋œ [CycleGAN]

์Šคํฌ๋ฆฐ์ƒท 2022-01-23 ์˜คํ›„ 11.56.11.png

์Šคํฌ๋ฆฐ์ƒท 2022-01-23 ์˜คํ›„ 11.57.33.png

์Šคํฌ๋ฆฐ์ƒท 2022-01-23 ์˜คํ›„ 11.57.49.png

์Šคํฌ๋ฆฐ์ƒท 2022-01-23 ์˜คํ›„ 11.59.41.png

NG = No Good

ํŒ๋ณ„์ž(๋ถ„๋ฅ˜)์™€ ์ƒ์„ฑ์ž(ํด๋Ÿฌ์Šคํ„ฐ๋ง)

์Šคํฌ๋ฆฐ์ƒท 2022-01-24 ์˜ค์ „ 12.04.40.png

์ƒ์„ฑ๋ชจ๋ธ์˜ ๋ชฉํ‘œ

  • ๋ชจ๋ธ G์˜ ํ•™์Šต์ด ์ž˜ ๋˜์—ˆ๋‹ค๋ฉด ์›๋ณธ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ๋ฅผ ๊ทผ์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ํ•™์Šต์ด ์ž˜ ๋˜์—ˆ๋‹ค๋ฉด ํ†ต๊ณ„์ ์œผ๋กœ ํ‰๊ท ์ ์ธ ํŠน์ง•์„ ๊ฐ€์ง€๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ์ƒ์„ฑ ํ•  ์ˆ˜ ์žˆ๋‹ค.

[DCGAN] (Deep Convolutional Generative Adversarial Network)

Generative Adversarial์ ๋Œ€์ ์ธ Network-2014๋…„ Ian Goodfellow

์ƒ์„ฑ์ž(Generator)๋Š” ์•„๋ฌด ์˜๋ฏธ ์—†๋Š” ๋žœ๋ค ๋…ธ์ด์ฆˆ๋กœ๋ถ€ํ„ฐ ์‹ ๊ฒฝ๋ง์—์„œ์˜ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์ด๋ฏธ์ง€ ํ˜•์ƒ์˜ ๋ฒกํ„ฐ๋ฅผ ์ƒ์„ฑํ•ด ๋ƒ…๋‹ˆ๋‹ค. ์ฆ‰, ๋ฌด์—์„œ ์œ ๋ฅผ ์ฐฝ์กฐํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค

Generative Model

.ํŒ๋ณ„์ž(Discriminator)๋Š” ๊ธฐ์กด์— ์žˆ๋˜ ์ง„์งœ ์ด๋ฏธ์ง€์™€ ์ƒ์„ฑ์ž๊ฐ€ ๋งŒ๋“ค์–ด๋‚ธ ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๊ฐ ์ด๋ฏธ์ง€๊ฐ€ Real ์ธ์ง€, Fake ์ธ์ง€์— ๋Œ€ํ•œ ํŒ๋‹จ ์ •๋„๋ฅผ ์‹ค์ˆซ๊ฐ’์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

Discriminative Model

์œ„์กฐํ™”ํ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฒ”์ธ๊ณผ ๊ฒฝ์ฐฐ์ด ์‹ธ์šฐ๋“ฏ์ด. ๊ฒŒ์ž„์˜ ๊ฒฝ์Ÿ์„ ํ†ตํ•ด์„œ ์œ„์กฐํ’ˆ์ด ์ง„ํ’ˆ์ฒ˜๋Ÿผ ๋งŒ๋“ค๋•Œ๊นŒ์ง€ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•œ๋‹ค.

DCGAN(Deep Convolutional GAN)- 2015

def make_generator_model():

    # Start
    model = tf.keras.Sequential()

    # First: Dense layer
    model.add(layers.Dense(7*7*256, use_bias=False, input_shape=(100,)))
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())
		# input_shape=(100,))) ๋ฐฐ์น˜์‚ฌ์ด์ฆˆ(1),100 ์˜ ํ•œ์ค„๋ฒกํ„ฐ ์ƒ์„ฑ
		#๊ทธํ›„ 7x7x256 12544 ๊ฐœ ๋ด์Šค๋ ˆ์ด์–ด๋ฅผ ๊ฑฐ์ณ์„œ batch,12544
		#๊ทธํ›„ 3์ฐจ์›์˜ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜

    # Second: Reshape layer
    model.add(layers.Reshape((7, 7, 256)))

    # Third: Conv2DTranspose layer
    model.add(layers.Conv2DTranspose(128, kernel_size=(5, 5), strides=(1, 1), padding='same', use_bias=False))
		 7, 7, 128

    # Fourth: Conv2DTranspose layer
    model.add(layers.Conv2DTranspose(64, kernel_size=(5, 5), strides=(2, 2), padding='same', use_bias=False))
		#(14, 14, 64)

    # Fifth: Conv2DTranspose layer
    model.add(layers.Conv2DTranspose(1, kernel_size=(5, 5), strides=(2, 2), padding='same', use_bias=False, \
                                     activation='tanh'))
		#(28, 28, 1)

    return model

Untitled

What is Transposed Convolutional Layer?

[1(๋ฐฐ์น˜์‚ฌ์ด์ฆˆ), 28, 28, 1] 1์ฑ„๋„ ์ด๋ฏธ์ง€ ์ƒ์„ฑ๋จ.

BatchNormalizationย ๋ ˆ์ด์–ด๋Š” ์‹ ๊ฒฝ๋ง์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ํญ๋ฐœํ•˜์ง€ ์•Š๋„๋ก ๊ฐ€์ค‘์น˜ ๊ฐ’์„ ์ •๊ทœํ™”

์Šคํฌ๋ฆฐ์ƒท 2022-01-20 ์˜คํ›„ 8.17.15.png

๋…ธ๋ฉ€๋ฆฌ์ œ์ด์…˜์€ ์ „์ฒด ๋ฐฐ์น˜ = ์ „์ฒด ์ง‘ํ•ฉ์œผ๋กœ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜์ง€๋งŒ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋กœ ์ž…๋ ฅํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์ด ํฌ์ธํŠธ ๋ฏธ๋‹ˆ๋ฐฐ์น˜๋กœ ์ž…๋ ฅํ–ˆ์ง€๋งŒ nn์€ ์ „์ฒด ์ง‘ํ•ฉ์„ ๋Œ€ํ‘œํ•˜๋Š”๊ฒƒ์ฒ˜๋Ÿผ ์ƒ๊ฐํ•œ๋‹ค.

r b๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์œผ๋ฏ€๋กœ rb๋ฅผ ๊ธฐ์–ตํ•ด๋†“์œผ๋ฉด ์›๋ž˜ ํ˜•ํƒœ๋กœ ๋˜๋Œ๋ฆฌ๊ธฐ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

BN์€ ์‹ ๊ฒฝ๋ง ๊ณ„์—ด๋กœ ์—ญ์ „ํŒŒ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

์Šคํฌ๋ฆฐ์ƒท 2022-01-20 ์˜คํ›„ 8.19.49.png

ํŒ๋ณ„์ž ๋งŒ๋“ค๊ธฐ

def make_discriminator_model():

    # Start
    model = tf.keras.Sequential()

		#28,28,1์ด๋ฏธ์ง€๋ฅผ ๋ฐ›์•„์„œ 
		#๋ฐ˜๋Œ€๋กœ ํ•ด๊ฐ (28, 28, 1) โ†’ (14, 14, 64) โ†’ (7, 7, 128)

    # First: Conv2D Layer
    model.add(layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same', input_shape=[28, 28, 1]))
    model.add(layers.LeakyReLU())
    model.add(layers.Dropout(0.3))

    # Second: Conv2D Layer
    model.add(layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same'))

    # Third: Flatten Layer
    model.add(layers.Flatten())
		#Flatten ์ธต์„ ์‚ฌ์šฉํ•ด 3์ฐจ์› ์ด๋ฏธ์ง€๋ฅผ 1์ฐจ์›์œผ๋กœ ์ญ‰ ํŽด์„œ 7x7x128=6272, ์ฆ‰ (1, 6272)๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ฌ

    # Fourth: Dense Layer
    model.add(layers.Dense(1))# ์ง„์งœ ๊ฐ€์งœ 1๊ฐœ๋งŒ ๊ฒฐ๊ณผ๋กœํ•จ.

    return model

ํ•™์Šต ์ž˜ํ•˜๋Š”๋ฒ•

์†์‹คํ•จ์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ €(์ตœ์ ํ™”)ํ•จ์ˆ˜ ๋ฐ”๊ฟ”๋ณด๊ธฐ

์†์‹ค = ๊ต์ฐจ ์—”ํŠธ๋กœํ”ผ

๋‘๊ฐ’์˜ ์ฐจ์ด๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋‚˜๋Š”์ง€๋ฅผ ์ •๋Ÿ‰์ ์œผ๋กœ ๋ณด์—ฌ์ค„์ˆ˜์žˆ๋‹ค.

ํ•œ ๊ฐœ์˜ ์ด๋ฏธ์ง€๊ฐ€ ๊ฐ€์งœ์ธ์ง€ ์ง„์งœ์ธ์ง€ ๋‚˜ํƒ€๋‚ด๋Š” 2๊ฐœ ํด๋ž˜์Šค ๊ฐ„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ ํ•˜๋ฏ€๋กœ, ์ด์ง„ ๊ต์ฐจ ์—”ํŠธ๋กœํ”ผ(binary cross entropy)๋ฅผ ์‚ฌ์šฉ

์ƒ์„ฑ์ž : ํŒ๋ณ„์ž๊ฐ€ Fake Image์— ๋Œ€ํ•ด ํŒ๋ณ„ํ•œ ๊ฐ’, ์ฆ‰ย D(fake_image)ย ๊ฐ’์ดย 1์— ๊ฐ€๊นŒ์›Œ์ง€๋Š” ๊ฒƒ

ํŒ๋ณ„์ž : Real Image ํŒ๋ณ„๊ฐ’, ์ฆ‰ย D(real_image)๋Š”ย 1์—, Fake Image ํŒ๋ณ„๊ฐ’, ์ฆ‰ย D(fake_image)๋Š”ย 0์— ๊ฐ€๊นŒ์›Œ์ง€๋Š” ๊ฒƒ

์†์‹คํ•จ์ˆ˜์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์€ ๋ชจ๋‘ ํŒ๋ณ„์ž์˜ ํŒ๋ณ„๊ฐ’

tf.keras.losses์˜ BinaryCrossEntropy ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณธ์ธ์—๊ฒŒ ๋“ค์–ด์˜ค๋Š” ์ธํ’‹๊ฐ’์ด 0-1 ์‚ฌ์ด์— ๋ถ„ํฌํ•˜๋Š” ํ™•๋ฅ ๊ฐ’์ด๋ผ๊ณ  ๊ฐ€์ •

cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)

(from_logits=True) ์˜ต์…˜์„ ์“ฐ๋ฉด๋œ๋‹ค. sigmoid ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด 0~1์œผ๋กœ ์ •๊ทœํ™”ํ›„ ๊ฐ’์ด ๋“ค์–ด๊ฐ.

?#๋ ˆ์ด์–ด์— ์‹œ๊ทธ๋ชจ์ด๋“œ ๋–„๋ ค๋„๋˜๋‚˜?#

fake_outputย : ์ƒ์„ฑ์ž๊ฐ€ ์ƒ์„ฑํ•œ Fake Image๋ฅผ ๊ตฌ๋ถ„์ž์— ์ž…๋ ฅ์‹œ์ผœ์„œ ํŒ๋ณ„๋œ ๊ฐ’, ์ฆ‰ย D(fake_image)

real_outputย : ๊ธฐ์กด์— ์žˆ๋˜ Real Image๋ฅผ ๊ตฌ๋ถ„์ž์— ์ž…๋ ฅ์‹œ์ผœ์„œ ํŒ๋ณ„๋œ ๊ฐ’, ์ฆ‰ย D(real_image)

ํŒ๋ณ„์ž๊ฐ€ 1.0์œผ๋กœ ๊ฐ€๊นŒ์›Œ์ง€๊ฐ€ ๋‘˜๋‹ค ํŒ๋‹จํ•˜๋ฉด ํŒ๋ณ„์ž๊ฐ€ ๋„ˆ๋ฌด ์ผ์„ ์ž˜ํ•˜๊ณ ์žˆ์–ด์„œ ๋ฌธ์ œ

์ƒ์„ฑ์ž๊ฐ€ ์ œ๋ฐ๋กœ ์ผ์„ ๋ชปํ•˜๊ณ ์žˆ๋‹ค.

real accuracy, fake accuracy 0.5๋กœ ๋‚˜์˜ค๋Š”๊ฒŒ ์ด์ƒ์ •์ด๋‹ค. ๋Œ€์ถฉ์ฐ์–ด๋„ ๋‚˜์˜ค๋Š” ํ™•๋ฅ ์ด๊ธฐ๋•Œ๋ฌธ์—.

์˜ตํ‹ฐ ๋‹น์—ฐํžˆ ์•„๋‹ด.

@tf.function
def train_step(images):  #(1) ์ž…๋ ฅ๋ฐ์ดํ„ฐ
    noise = tf.random.normal([BATCH_SIZE, noise_dim])  #(2) ์ƒ์„ฑ์ž ์ž…๋ ฅ ๋…ธ์ด์ฆˆ

    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:  #(3) tf.GradientTape() ์˜คํ”ˆ
        generated_images = generator(noise, training=True)  #(4) generated_images ์ƒ์„ฑ

        #(5) discriminator ํŒ๋ณ„
        real_output = discriminator(images, training=True)
        fake_output = discriminator(generated_images, training=True)

        #(6) loss ๊ณ„์‚ฐ
        gen_loss = generator_loss(fake_output)
        disc_loss = discriminator_loss(real_output, fake_output)

        #(7) accuracy ๊ณ„์‚ฐ
        real_accuracy, fake_accuracy = discriminator_accuracy(real_output, fake_output) 
    
    #(8) gradient ๊ณ„์‚ฐ
    gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)
    gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)

    #(9) ๋ชจ๋ธ ํ•™์Šต
    generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
    discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))

    return gen_loss, disc_loss, real_accuracy, fake_accuracy  #(10) ๋ฆฌํ„ด๊ฐ’

(1) ์ž…๋ ฅ ๋ฐ์ดํ„ฐ: Real Image ์—ญํ• ์„ ํ• ย imagesย ํ•œ ์„ธํŠธ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Œ

(2) ์ƒ์„ฑ์ž ์ž…๋ ฅ ๋…ธ์ด์ฆˆ : generator๊ฐ€ FAKE IMAGE๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œย noise๋ฅผย imagesย ํ•œ ์„ธํŠธ์™€ ๊ฐ™์€ ํฌ๊ธฐ์ธย BATCH_SIZEย ๋งŒํผ ์ƒ์„ฑํ•จ

(3)ย tf.GradientTape()๋Š” ๊ฐ€์ค‘์น˜ ๊ฐฑ์‹ ์„ ์œ„ํ•œ Gradient๋ฅผ ์ž๋™ ๋ฏธ๋ถ„์œผ๋กœ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ดย withย ๊ตฌ๋ฌธ ์—ด๊ธฐ

(4) generated_images ์ƒ์„ฑ : generator๊ฐ€ย noise๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ํ›„ย generated_imagesย ์ƒ์„ฑ

(5) discriminator ํŒ๋ณ„ : discriminator๊ฐ€ Real Image์ธย images์™€ Fake Image์ธย generated_images๋ฅผ ๊ฐ๊ฐ ์ž…๋ ฅ๋ฐ›์€ ํ›„ย real_output,ย fake_outputย ์ถœ๋ ฅ(

6) loss ๊ณ„์‚ฐ :ย fake_output,ย real_output์œผ๋กœ generator์™€ discriminator ๊ฐ๊ฐ์˜ loss ๊ณ„์‚ฐ

(7) accuracy ๊ณ„์‚ฐ :ย fake_output,ย real_output์œผ๋กœ discriminator์˜ ์ •ํ™•๋„ ๊ณ„์‚ฐ

(8) gradient ๊ณ„์‚ฐ :ย gen_tape์™€ย disc_tape๋ฅผ ํ™œ์šฉํ•ด gradient๋ฅผ ์ž๋™์œผ๋กœ ๊ณ„์‚ฐ

(9) ๋ชจ๋ธ ํ•™์Šต : ๊ณ„์‚ฐ๋œ gradient๋ฅผ optimizer์— ์ž…๋ ฅํ•ด ๊ฐ€์ค‘์น˜ ๊ฐฑ์‹ (10) ๋ฆฌํ„ด๊ฐ’ : ์ด๋ฒˆ ์Šคํ…์— ๊ณ„์‚ฐ๋œ loss์™€ accuracy๋ฅผ ๋ฆฌํ„ด

profile
ํ™”์„ฑ์— ๊ฐ€๊ณ ํ”ˆ AI ๊ฐœ๋ฐœ์ž uni ์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€