class DCGAN():
    def __init__(self):
        self.img_rows = 28
        self.img_cols = 28
        self.channels = 1
        self.img_shape = (self.img_rows, self.img_cols, self.channels)
        self.latent_dim = 100

        # optimizer 정의
        optimizer = Adam(0.00020.5)

        # 판별자 정의
        self.discriminator = self.build_discriminator()
        self.discriminator.compile(loss='binary_crossentropy',optimizer=optimizer,metrics=['accuracy'])

        # 생성자 정의
        self.generator = self.build_generator()

        # 생성자의 입력(noise)과 출력(img)
        noise = Input(shape=(self.latent_dim,))
        img = self.generator(noise)

        # 최종 모델(결합된 모델)의 경우 생성자만 학습
        self.discriminator.trainable = False

        # 판별자는 생성된 이미지를 입력으로 받아, 유효성을 검사
        validity = self.discriminator(img)

        # 최종 모델(결합된 모델)은 생성자와 판별자를 쌓아서 만든 모델임(GAN)
        self.model = Model(noise, validity)
        self.model.compile(loss='binary_crossentropy', optimizer=optimizer)

    # 생성자 정의 함수
    def build_generator(self):

        model = Sequential()
        ##입력 받는 부분
        model.add(Dense(128 * 7 * 7, activation="relu", input_dim=self.latent_dim))
        model.add(Reshape((77128)))

        #### Input : (100)
        #### Output : (28, 28, 1)
        model.add(UpSampling2D())
        model.add(Conv2D(128, kernel_size=3, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))

        model.add(UpSampling2D())
        model.add(Conv2D(64, kernel_size=3, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        
        model.add(Conv2D(self.channels, kernel_size=3, padding="same"))
        model.add(Activation("tanh"))

        model.summary()
        noise = Input(shape=(self.latent_dim,))
        img = model(noise)
        return Model(noise, img)

    # 판별자 정의 함수
    def build_discriminator(self):

        model = Sequential()
        model.add(Conv2D(32, kernel_size=3, strides=2, input_shape=self.img_shape, padding="same"))


        #### Input : (28, 28, 1)
        #### Output : (1)

        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))

        model.add(Conv2D(64, kernel_size=3, strides=2, padding="same"))
        model.add(ZeroPadding2D(padding=((0,1),(0,1))))
        model.add(BatchNormalization(momentum=0.8))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))

        model.add(Conv2D(128, kernel_size=3, strides=2, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))

        model.add(Conv2D(256, kernel_size=3, strides=1, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))

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

        model.summary()
        img = Input(shape=self.img_shape)
        validity = model(img)
        return Model(img, validity)

    # 학습 함수
    def train(selfepochsbatch_size=128sample_interval=50):

        # 데이터셋 로드
        (X_train, _), (_, _) = fashion_mnist.load_data()

        X_train = X_train / 127.5 - 1.
        X_train = np.expand_dims(X_train, axis=3)

        # Adversarial ground truths
        valid = np.ones((batch_size, 1))
        fake = np.zeros((batch_size, 1))
        D_loss_list = []
        G_loss_list = []
        for epoch in range(1,epochs+1):

            # ---------------------
            #  판별자 학습
            # ---------------------

            # 학습에 사용할 이미지 랜덤으로 선택
            idx = np.random.randint(0, X_train.shape[0], batch_size)
            imgs = X_train[idx]

            # 노이즈 생성
            noise = np.random.normal(01, (batch_size, self.latent_dim))

            # 생성자가 이미지 생성
            gen_imgs = self.generator.predict(noise)

            # 판별자 학습
            d_loss_real = self.discriminator.train_on_batch(imgs, valid)
            d_loss_fake = self.discriminator.train_on_batch(gen_imgs, fake)
            d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

            # ---------------------
            #  생성자 학습
            # ---------------------

            # 노이즈 생성
            noise = np.random.normal(01, (batch_size, self.latent_dim))

            # 판별자 레이블 샘플을 유효한 것으로 지정
            g_loss = self.model.train_on_batch(noise, valid)
            G_loss_list.append(g_loss)
            D_loss_list.append(d_loss[0])
            print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss))

            if epoch % sample_interval == 0:
                self.sample_images(epoch)
        self.plotLoss(G_loss_list, D_loss_list, epoch)

    # 그래프를 생성하는 함수
    def plotLoss(selfG_lossD_lossepoch):
        plt.figure(figsize=(108))
        plt.plot(D_loss, label='Discriminitive loss')
        plt.plot(G_loss, label='Generative loss')
        plt.xlabel('BatchCount')
        plt.ylabel('Loss')
        plt.legend()
        plt.savefig('loss_graph/dcgan_loss_epoch_%d.png' % epoch)

    # 이미지를 저장하는 함수
    def sample_images(selfepoch):
        r, c = 55
        noise = np.random.normal(01, (r * c, self.latent_dim))
        gen_imgs = self.generator.predict(noise)

        # Rescale images 0 - 1
        gen_imgs = 0.5 * gen_imgs + 0.5

        fig, axs = plt.subplots(r, c)
        cnt = 0
        for i in range(r):
            for j in range(c):
                axs[i,j].imshow(gen_imgs[cnt, :,:,0], cmap='gray')
                axs[i,j].axis('off')
                cnt += 1
        fig.savefig("images/%d.png" % epoch)
        plt.close()

    # 모델을 로드하는 함수
    def load_model(selfmodel_path='saved_model/model.h5'):
        print('\nload model : \"{}\"'.format(model_path))
        self.model = tf.keras.models.load_model(model_path)

    # 모델을 저장하는 함수
    def save_model(selfmodel_path='saved_model/model.h5'):
        print('\nsave model : \"{}\"'.format(model_path))
        self.model.save(model_path)

if __name__ == '__main__':
    dcgan = DCGAN()
    dcgan.train(epochs=5000, batch_size=32, sample_interval=200)
    dcgan.save_model()


이렇게 만들었는데
원하는 결과물은

2abcde73b58268e87eb1d19528d52703bf3216f3a41a


대충 이런거거든




근데 학습 돌려보니까

78ed8076abc236a14e81d2b628f1716498e092


이렇게 나옴

걍 학습이 안됨 ㅋㅋㅋㅋㅋㅋ


어느부분 잘못짠거 같냐...