.  
 


 #5
             .             ,         .





 

.  





 1:    



1.1.      (GAN)

  ()      ,          .            ,     ,   ,       .       (GAN)             .

        ,    ,     ,      .      :         , , 3D-    .

         ,      .        ,   ,    ,     .

               . ,   GAN       ,     .

           1940- .          ,         .  1950-  1960-      ,           ,     .

 1986            ,        .         ,    .          .

  2000-           .                .         ,    ,      ,      .

       2014 ,            "Generative Adversarial Networks".         ,     :   .

  GAN      ,       .      ,   ,       . ,   ,      .    ,      ,     .

    GAN      ,    ,  , ,   .     , ,  ,      .

   (GAN)        .                 .         GAN  ,         .


1.2.   GAN      

   (GAN)       ,      2014       .         ,      :   .

  GAN      .            .      ,   ,       . ,   ,      .

  GAN    .              .    ,      ""  "".        ,      .                   .

  ,     ,   ,      .    ,       .  ,    ,    ,         .

  GAN  ,       .          ,    ,   ,         .

 GAN             .         ,       ,    ,  ,   .

 GAN        ,     ,      .     ,          . GAN      ,            .

    GAN        TensorFlow  Keras  Python.      GAN     .     MNIST   .

```python

import numpy as np

import tensorflow as tf

from tensorflow.keras import layers

#   MNIST

(train_images, _), (_, _) = tf.keras.datasets.mnist.load_data()

train_images = train_images.reshape(train_images.shape[0], 28 * 28).astype('float32')

train_images = (train_images  127.5) / 127.5 #     [-1, 1]

# 

random_dim = 100

epochs = 10000

batch_size = 128

#  

def build_generator():

model = tf.keras.Sequential()

model.add(layers.Dense(256, input_dim=random_dim))

model.add(layers.LeakyReLU(0.2))

model.add(layers.BatchNormalization())

model.add(layers.Dense(512))

model.add(layers.LeakyReLU(0.2))

model.add(layers.BatchNormalization())

model.add(layers.Dense(1024))

model.add(layers.LeakyReLU(0.2))

model.add(layers.BatchNormalization())

model.add(layers.Dense(784, activation='tanh'))

model.add(layers.Reshape((28, 28)))

return model

#  

def build_discriminator():

model = tf.keras.Sequential()

model.add(layers.Flatten(input_shape=(28, 28)))

model.add(layers.Dense(1024))

model.add(layers.LeakyReLU(0.2))

model.add(layers.Dense(512))

model.add(layers.LeakyReLU(0.2))

model.add(layers.Dense(256))

model.add(layers.LeakyReLU(0.2))

model.add(layers.Dense(1, activation='sigmoid'))

return model

#    

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

def discriminator_loss(real_output, fake_output):

real_loss = cross_entropy(tf.ones_like(real_output), real_output)

fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)

total_loss = real_loss + fake_loss

return total_loss

def generator_loss(fake_output):

return cross_entropy(tf.ones_like(fake_output), fake_output)

generator_optimizer = tf.keras.optimizers.Adam(learning_rate=0.0002, beta_1=0.5)

discriminator_optimizer = tf.keras.optimizers.Adam(learning_rate=0.0002, beta_1=0.5)

#    

generator = build_generator()

discriminator = build_discriminator()

#   GAN

def train_gan():

for epoch in range(epochs):

#      

noise = np.random.normal(0, 1, size=[batch_size, random_dim])

#    

generated_images = generator(noise)

#       

image_batch = train_images[np.random.randint(0, train_images.shape[0], size=batch_size)]

#       

X = np.concatenate([image_batch, generated_images])

#        

y_dis = np.zeros(2 * batch_size)

y_dis[:batch_size] = 0.9 #     

#    

discriminator.trainable = True

d_loss = discriminator.train_on_batch(X, y_dis)

#  

noise = np.random.normal(0, 1, size=[batch_size, random_dim])

y_gen = np.ones(batch_size)

discriminator.trainable = False

g_loss = gan.train_on_batch(noise, y_gen)

if epoch % 100 == 0:

print(f"Epoch: {epoch}, Discriminator Loss: {d_loss}, Generator Loss: {g_loss}")

#  GAN

gan = tf.keras.Sequential([generator, discriminator])

gan.compile(loss='binary_crossentropy', optimizer=generator_optimizer)

train_gan()

```

       (GAN)        TensorFlow  Keras  Python.      :

1.   MNIST:

   MNIST       `tf.keras.datasets.mnist.load_data()`.

     `train_images`,    (     )    `_`.

         [-1, 1],    .

2.  :

`random_dim`:     ( ),      .

`epochs`:    GAN.

`batch_size`:  ,      .

3.   (`build_generator`):

    ,             .

           LeakyReLU   BatchNormalization   .

      `tanh`,       [-1, 1].

4.   (`build_discriminator`):

    ,        "" (1)  "" (0).

            LeakyReLU.

              "".

5.     :

       - (`BinaryCrossentropy`).

      `Adam`    .

6.  GAN (`train_gan`):

   :

      .

       .

       .

      .

       ""  "" .

       "".

      ,      .

7.  GAN:

GAN         `gan`.

 GAN    `compile`    `binary_crossentropy`   `generator_optimizer`.

 GAN (Generative Adversarial Network)       :  (Generator)   (Discriminator),       .

    GAN,    .             .                  .

  GAN    (loss function),  ,    GAN.   GAN,      - (binary_crossentropy),        .

   (optimizer),              .   ,  `generator_optimizer`     .

 GAN           .            .     ,      ,       ,          ,    .

   GAN       ,           .   GAN    (equilibrium),     ,         .

8.  :

 GAN     `train_gan`,            .

 `train_gan`       GAN (Generative Adversarial Network)           (dataset)      (epochs).  ,        GAN,        `gan`.

  ,     `train_gan`:

1.    :

  ,  GAN    (Generator)   (Discriminator).                .

2.   :

 `train_gan`   ,       (epochs).           .

3.  :

  ,     .  :

    (noise)  .

         (generated_data).

    (batch)    (real_data).

      ,      (   ""  "").

4.  :

  ,   .

     .

        "".      ,  "" ,      "".

5.  :

  ,     (loss)    .       ,    GAN   .

 ,       GAN     ,              .    GAN        ,    .

    , GAN      MNIST      .  ,             . ,                     .

 ,     GAN,     .      ,            . ,    , ,   ,       .

GAN         ,    .   ,     ,      ,            .


1.3.  GAN:   

   (GAN)     :   .            ,             ""   "".

:

     ,          .      ,   ,        .

     ,    .   ,        ,      ()      .             .

,       ,       ,         ,       .

:

   ,      (  )  ,       .          .

         ,         ,   .

       ,     "",      ,     "".          .

   GAN:

  GAN   ,            .       ,           .   ,           .

  GAN    .           ,      .            ,            .

         GAN.   ,           GAN      .       , GAN      ,    , , , ,     .

         ,         ,   TensorFlow  Keras  Python.

1. :

           :

```python

from tensorflow.keras import layers, models

def build_generator(random_dim, image_shape):

model = models.Sequential()

model.add(layers.Dense(256, input_dim=random_dim))

model.add(layers.LeakyReLU(0.2))

model.add(layers.BatchNormalization())

model.add(layers.Dense(512))

model.add(layers.LeakyReLU(0.2))

model.add(layers.BatchNormalization())

model.add(layers.Dense(1024))

model.add(layers.LeakyReLU(0.2))

model.add(layers.BatchNormalization())

model.add(layers.Dense(np.prod(image_shape), activation='tanh'))

model.add(layers.Reshape(image_shape))

return model

#  :

random_dim = 100

image_shape = (28, 28, 1)

generator = build_generator(random_dim, image_shape)

```

2. :

         ""  "":

```python

def build_discriminator(image_shape):

model = models.Sequential()

model.add(layers.Flatten(input_shape=image_shape))

model.add(layers.Dense(1024))

model.add(layers.LeakyReLU(0.2))

model.add(layers.Dense(512))

model.add(layers.LeakyReLU(0.2))

model.add(layers.Dense(256))

model.add(layers.LeakyReLU(0.2))

model.add(layers.Dense(1, activation='sigmoid'))

return model

#  :

discriminator = build_discriminator(image_shape)

```

              LeakyReLU   .    Dense    `tanh`,       [-1, 1].    Reshape,       .

            LeakyReLU.     ,   ,    ,     (   1)   (   0).

 ,     ,                     . ,           (CNN),       .

  GAN: , ,   GAN  .           ,      . ,   ,       ,       .   GAN         GAN   .

     GAN.    GAN           .  ,        ,            .


1.4.     GAN

   ,  (Layer)     ,        .         , ,           .

    ,     ,     .       (weights)   (biases),               .

 GAN (Generative Adversarial Networks)      ,   ,    .       ,    GAN.          GAN:

1.   (Convolutional Layers):

  (Convolutional Layers)           (GAN)   .                 ""  "".   :

      ,   .       ,     ()    ,   ,     .        ,    .

      ,   ,   .      ,       ,     .

      :

 ():   ,       .         .

  (Kernel Size):   ,       .     3x3  5x5.

 (Stride):  ,  ,        .  1  ,   2  .

 (Padding):  ,       .       ,  ,        .

   GAN:

 ,               .               .

 ,         ,       .

  GAN       ,      (CNN),   (CAE)   GAN (cGAN).     ,           .

           GAN          .

2.   (Batch Normalization):

  (Batch Normalization)   ,    ,     (GAN),       .     2015           .

 ,    ,  " " (internal covariate shift).        ,        ,  ,   .     ,      -  .

   :

           -  ,     .        ,    .

        :  (scaling)   (shift).          ,    .

   (    )       ,           ,    .

 GAN,        ,    .           ,        GAN.

 ,         ,       .               .

 ,           .        ,         GAN.

          GAN,          .

 ,     (max pooling  average pooling),     ,        .

4.   (Recurrent Layers):

  (Recurrent Layers)       ,      ,         .    , ,    ,       .

      ,     ,        .                   .

   :

   " " (hidden state),            .       ,       .

           .      ,       .      .

        .            ,       ,        .

    GAN:

 GAN,         ,     . ,  GAN   ,             .         .

 GAN    ,           . ,         ,       .

 ,          ,     ,       .            ,    (Transformer Layers),     ,      .           ,    GAN.

5.    (Transposed Convolutional Layers):

   (Transposed Convolutional Layers),      (Deconvolution Layers),        (GAN),   .           ().

  , ,          GAN:

 ,    GAN,           ,      .         ,    .    ,         .

 ,    ( )       ,           .      .       :    ,   .

  :

       (stride),     .          ,     .

  :

               .  ,      ,        .

      :

       GAN.          ,    ,     ,      .

 ,        GAN,                .

6.   (Activation Layers):

       ,     (GAN).         ,             .  GAN        ,       ,              .

      ,   GAN:

ReLU (Rectified Linear Unit):

ReLU     f(x) = max(0, x).              .           ,        ,      .

LeakyReLU:

LeakyReLU      ReLU       .    f(x) = max(ax, x),  a    ,    (leak). LeakyReLU    " ",      ReLU.

Tanh ( ):

Tanh     f(x) = (e^x  e^(-x)) / (e^x + e^(-x)).       -1  1,        . Tanh     ,            [-1, 1].

Sigmoid:

Sigmoid     f(x) = 1 / (1 + e^(-x)).       0  1.  sigmoid      ,        -    (vanishing gradient problem)    .

    GAN:

             .       ,         .  ,           ,        .       ,     ,    GAN.             .

7.    (Flatten Layers):

   (Flatten Layers)        ,     (GAN).            ,      ,    .

    :

  :

          (Convolutional Layers)    (Recurrent Layers),         . ,      ,      (,      ),          (,      ).

   :

     ,    ,       .       (Flatten Layers).     "" ,     .

  :

       . ,    ,       ,         ,          .

     GAN:

 GAN,    ,  ,     ,   , ,    .                 (Fully Connected Layers)      .

         ,            .    GAN     ,            .

8.   (Fully Connected Layer):

         .        (Dense Layer)    (Linear Layer).             .

                 .         .     N    M  ,   ,    N      M  .

,       :

```

y = activation(W * x + b)

```

:

`x`    ( )

`W`     (N, M),  N    ,  M    

`b`    (bias)  (M)

`activation`   ,          

`y`    (  )

            .        ,    ,   ,     .

     (GAN),                      .        GAN-.

     ,      GAN.   GAN          ,       ,      ,  dropout,  -        (Instance Normalization)  .  GAN                  .

    ,         GAN:




                (GAN).  GAN      ,            .

      ,    GAN,     ,         .             .

   ,     ,     GAN    :

 :           .    , ,      .

 (Transformer Layers):         ,      .

Residual Blocks:        ,         .

  :   Instance Normalization, Layer Normalization  ,         .

  (Attention Layers):             .

 GAN   ,            .          ,       .

          ,   .       ,           .           :

    (Convolutional Layers)




2.     (Recurrent Layers):




3.     (Fully Connected Layers):




      . ,         ,             .    GAN    ,                .




 2:    



2.1.       GAN

         (GAN)     ,     ,         .           ,    ,        .      :

1.     :

       ,        .    ,   , , ,     .

2.   

          (GAN)     .       ,       .     ,      GAN:

  :

      ,    ,   , ,   .    ,      GAN,  CIFAR-10, MNIST, ImageNet  .             .

  :

              ,      . ,     ,      .

   :

   ,     ,          -  API.  -       API,     .

 :

              .          GAN.            ,     .

   :

             GAN.          .

       GAN, ,        ,     -  .     ,     ,        .

3.  

         (GAN)         .         GAN,        :

          GAN.     ,     . ,     , ,          ,    .

       GAN       .      ,        .    ,         .

    .   ,          .        ,          .

           .          .          .

     ,     .       ,      .     ,         .

 ,         ,            .

          GAN         .        ,   GAN      .


2.2.  : ,    

            (GAN).        ,          .       ,   ,   .

1.  (Rescaling):

      ,      .       0  1   -1  1.      ,            .

2.  (Normalization):

           .          .                    .                .

3.  (Centering):

            .    ,          .         .

4.   (Data Augmentation):

    ,         .    ,      , , ,     .     ,         .

5.   (Outlier Removal):

          .               .      .

6.   (Image Transformation):

      , ,      .    , ,             .

         (GAN)        .             .           GAN   .

 

          GAN.        :

      ,   .

         (,  -1  1)   .

      .

       .

    .

***

            :

Pillow    Python    .       ,    ,   .     `resize()`   Pillow       .

OpenCV     ,        .            `cv2.resize()`.

scikit-image    Python   .    `resize()`    .








   Pillow      :

```python

from PIL import Image

#  

image = Image.open("image.jpg")

#      (, 256x256 )

desired_size = (256, 256)

resized_image = image.resize(desired_size)

#   

resized_image.save("resized_image.jpg")

```

 ,             ,     .           ,       .

         .

***

          (,  -1  1)       ,    :

NumPy            .       `numpy.min()`, `numpy.max()`        ,        .

scikit-learn   `MinMaxScaler`,    -        .    `StandardScaler`             .

     , TensorFlow  PyTorch      .  TensorFlow       `tf.keras.layers.BatchNormalization`,   PyTorch    `torch.nn.BatchNorm2d`.

      Pandas    `DataFrame.min()`  `DataFrame.max()`        ,           .

     MinMaxScaler   scikit-learn:

```python

from sklearn.preprocessing import MinMaxScaler

#   ( data   )

data = [[10], [5], [3], [15]]

#   MinMaxScaler   

scaler = MinMaxScaler(feature_range=(-1, 1))

normalized_data = scaler.fit_transform(data)

print(normalized_data)

```

        -1  1.         ,    ,          .

***

            :

OpenCV:

  (`cv2.GaussianBlur`)      .

  (`cv2.medianBlur`)     .

  (`cv2.bilateralFilter`)  ,     .

scikit-image:

  (`skimage.filters.gaussian`)      .

  (`skimage.filters.median`)     .

  (`skimage.restoration.denoise_tv_bregman`)     .

Denoising Autoencoders (DAE):

 ,   TensorFlow  PyTorch,       .

 :

  (`cv2.threshold`)        .

  (`cv2.absdiff`)      .

 :

  (`cv2.erode`, `cv2.dilate`)      .

   (`cv2.fastNlMeansDenoising`)      .

 :

  (`skimage.transform.resize`, `cv2.resize`)       .

   (`cv2.filter2D`, `skimage.filters.unsharp_mask`)    .

     ,     `scikit-image`.  ,     ,  :

```bash

pip install scikit-image

```

,           .        .    :

```python

import numpy as np

import matplotlib.pyplot as plt

from skimage import io, img_as_ubyte, img_as_float

from skimage.filters import gaussian, median

#    

image_with_noise = io.imread('image_with_noise.jpg')

image_with_noise = img_as_float(image_with_noise)

#      

image_gaussian_filtered = gaussian(image_with_noise, sigma=1)

#      

image_median_filtered = median(image_with_noise)

#        

plt.figure(figsize=(10, 4))

plt.subplot(131)

plt.imshow(image_with_noise, cmap='gray')

plt.title('   ')

plt.subplot(132)

plt.imshow(image_gaussian_filtered, cmap='gray')

plt.title(' ')

plt.subplot(133)

plt.imshow(image_median_filtered, cmap='gray')

plt.title(' ')

plt.tight_layout()

plt.show()

```

 ,       ,        ,         ,            .

,  `'image_with_noise.jpg'`       .

***

               Python `PIL` (Python Imaging Library),     `Pillow`. `Pillow`     `PIL`          Python.

             `Pillow`,   `Image.getdata()`  `numpy.array`.  :

```python

from PIL import Image

#  

image = Image.open('example_image.jpg')

#    

pixel_data = list(image.getdata())

#     (    )

#   OCR (Optical Character Recognition)    .

```

 `Image.open()`  ,  `image.getdata()`      .  ,      ,     ,       OCR (, Tesseract  pytesseract).

         ,                  .

***

             ,      .    :

            .      OpenCV  scikit-image:

OpenCV:

 OpenCV       `cv2.GaussianBlur`,           :

```python

import cv2

#  

image = cv2.imread('example_image.jpg')

#      

image_filtered = cv2.GaussianBlur(image, (5, 5), 0)

```

  OpenCV      ,     (`cv2.medianBlur`)    (`cv2.bilateralFilter`),         .

scikit-image:

 scikit-image         `filters`,   `gaussian`, `median`  :

```python

from skimage import io, img_as_ubyte

from skimage.filters import gaussian, median

#  

image = io.imread('example_image.jpg')

image = img_as_ubyte(image)

#      

image_gaussian_filtered = gaussian(image, sigma=1)

#      

image_median_filtered = median(image)

```

    `gaussian`  `median`  `skimage.filters`               .

 ,              ,     .     ,           .

      

            .          ,     .       GAN,       ,       .

       , , 80%    ,   20%   .        -      .

 Python            `scikit-learn`  `tensorflow.keras` (        TensorFlow).     :

  scikit-learn:

```python

from sklearn.model_selection import train_test_split

# X   ( ), y   ( )

X, y =  #   

#       

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

```

      `train_test_split`  `sklearn.model_selection`    `X`  `y`        80:20.  `test_size=0.2`   ,  20%     ,  `random_state=42`    .

  tensorflow.keras:

```python

import tensorflow as tf

# X   ( ), y   ( )

X, y =  #   

#       

X_train, X_test, y_train, y_test = tf.keras.train_test_split(X, y, test_size=0.2, random_state=42)

```

    ,   TensorFlow  Keras,     `train_test_split`  `tensorflow.keras`,    ,    `scikit-learn`.

               GAN,          .

  

  GAN    ,    (data iterator)                .         ,         -     .

      (batch)      ,       .  ,      ,          .  ,  ,     ,    ,       .

 Python       ,   `tensorflow.data.Dataset` (   TensorFlow)  `torch.utils.data.DataLoader` (   PyTorch).

  TensorFlow:

```python

import tensorflow as tf

#       

dataset =  #    , , tf.data.Dataset

#   

batch_size = 32

#   

data_iterator = dataset.batch(batch_size)

#   

for batch in data_iterator:

#      

loss = model.train_on_batch(batch)

```

      `batch()`  `tf.data.Dataset`,    ,       `batch_size`   .                 `train_on_batch()`.

  PyTorch:

```python

import torch

from torch.utils.data import DataLoader

#       

dataset =  #    , , Dataset  torchvision   

#   

batch_size = 32

#   

data_iterator = DataLoader(dataset, batch_size=batch_size, shuffle=True)

#   

for batch in data_iterator:

#     (GPU,  )

inputs, labels = batch

inputs, labels = inputs.to(device), labels.to(device)

#      

loss = model.train_on_batch(inputs, labels)

```

      `DataLoader`  `torch.utils.data`,    ,       `batch_size`   .     ( `shuffle=True`),     .

           GAN       .

  ( )

  (data augmentation)   ,                .       ,        .

  GAN    ,           ,          .              ,        GAN.

   ,        GAN:

 ( ):       .

:     .

:           .

:      .

   :      .

 :     .

:    .

 :   , ,   .

       GAN           .      ,         .

    GAN      ,             .          Python:

`imgaug`       .    ,         . `imgaug`    ,   , , , ,    ,     .

`albumentations`         .     ,     . `albumentations`           API     .

`Augmentor`      ,        ,   , , ,    .           .

    Keras,   `ImageDataGenerator`      .    ,   , ,    .

   PyTorch,   `torchvision.transforms`     ,       .     ,   , ,    .

         ,     .     ,     .             ,         .

   GAN   ,          ,      .   ,        ,             .

  

            GAN.                .   ,          :

,      .        .

  . ,               GAN.

   .  ,     , ,       , ,  0  1   -1  1.

,             .

      ,       (,   . .).

      .        ,       .

      , ,      .             .

     ,           GAN    .

   ( )

   GAN         ,                  .     "  "  "  ".

     ,       ,    . GAN   ,  ,      .  ,     ,           .

  GAN        ,      .    ,      ,       .         ,      ,         .

  GAN       ,     ,       ,      .

 GAN         :

1.  :        ,       . GAN          ,        .

2.   :             . GAN    ,             .

3.    :        GAN       ,       , ,          .

4.       : GAN      ,        , ,    .

  ,   GAN           .         ,      .              .

      GAN    :

            (GAN). GAN     ,        .     ,       .   ,         .

Conditional GAN (cGAN)    GAN,         ()  ,      .    ,        ,    .

  (VAE)      ,       . VAE           ,         .

StyleGAN  StyleGAN2     GAN,     .         ,           .

Deep Convolutional GAN (DCGAN)    GAN,     . DCGAN       ,     .

PGGAN   ,       ,        .         .




  .


   .

   ,     (https://www.litres.ru/book/dzheyd-karter/neyroseti-generaciya-izobrazheniy-69532666/chitat-onlayn/)  .

      Visa, MasterCard, Maestro,    ,   ,     ,  PayPal, WebMoney, ., QIWI ,       .


