您当前的位置: 首页 > 

星夜孤帆

暂无认证

  • 3浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Keras_dcgan生成自己的数据

星夜孤帆 发布时间:2018-09-29 16:29:22 ,浏览量:3

from __future__ import print_function, division

from keras.datasets import mnist
from keras.layers import Input, Dense, Reshape, Flatten, Dropout
from keras.layers import BatchNormalization, Activation, ZeroPadding2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D, Conv2D
from keras.models import Sequential, Model
from keras.optimizers import Adam
import os
import matplotlib.pyplot as plt

import sys

import numpy as np

class DCGAN():
    def __init__(self):
        # Input shape
        self.img_rows = 4
        self.img_cols = 60
        self.channels = 1
        self.img_shape = (self.img_rows, self.img_cols, self.channels)
        self.latent_dim = 100

        optimizer = Adam(0.0002, 0.5)

        # Build and compile the discriminator
        self.discriminator = self.build_discriminator()
        self.discriminator.compile(loss='binary_crossentropy',
            optimizer=optimizer,
            metrics=['accuracy'])

        # Build the generator
        self.generator = self.build_generator()

        # The generator takes noise as input and generates imgs
        z = Input(shape=(self.latent_dim,))
        img = self.generator(z)

        # For the combined model we will only train the generator
        self.discriminator.trainable = False

        # The discriminator takes generated images as input and determines validity
        valid = self.discriminator(img)

        # The combined model  (stacked generator and discriminator)
        # Trains the generator to fool the discriminator
        self.combined = Model(z, valid)
        self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)

    def build_generator(self):

        model = Sequential()

        model.add(Dense(512 * 2 * 2, activation="relu", input_dim=self.latent_dim))
        model.add(Reshape((2, 2, 512)))
        
        model.add(UpSampling2D(size=(1,2)))
        model.add(Conv2D(512, kernel_size=(2,2), padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        
        model.add(UpSampling2D(size=(1,2)))
        model.add(Conv2D(256, kernel_size=(2,2), padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        
        model.add(UpSampling2D(size=(1,2)))
        model.add(Conv2D(128, kernel_size=(1,2), padding="valid"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        
        model.add(UpSampling2D(size=(1,2)))
        model.add(Conv2D(64, kernel_size=(2,2), padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        
        model.add(UpSampling2D(size=(2,2)))
        model.add(Conv2D(32, kernel_size=(2,2), padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        
        model.add(Conv2D(self.channels, kernel_size=(2,2), 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=2, strides=2, input_shape=self.img_shape, padding="same"))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        
        model.add(Conv2D(64, kernel_size=2, strides=(1,2), padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        
        model.add(Conv2D(128, kernel_size=2, strides=(1,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=2, strides=(1,2), padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        
        model.add(Conv2D(512, kernel_size=2, strides=(1,2), 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(self, epochs, batch_size=128, sample_interval=50):

        ############################################################
        #自己数据集此部分需要更改
        # 加载数据集
        data = np.load('data/分叉_图.npy')  
        print(data.shape)
        # 归一化到-1到1
        data = data * 2 - 1
        data = np.expand_dims(data, axis=3)
        ############################################################

        # Adversarial ground truths
        valid = np.ones((batch_size, 1))
        fake = np.zeros((batch_size, 1))

        for epoch in range(epochs):

            # ---------------------
            #  Train Discriminator
            # ---------------------

            # Select a random half of images
            idx = np.random.randint(0, data.shape[0], batch_size)
            imgs = data[idx]

            # Sample noise and generate a batch of new images
            noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
            gen_imgs = self.generator.predict(noise)

            # Train the discriminator (real classified as ones and generated as zeros)
            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)

            # ---------------------
            #  Train Generator
            # ---------------------

            # Train the generator (wants discriminator to mistake images as real)
            g_loss = self.combined.train_on_batch(noise, valid)

            # Plot the progress
            print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss))

            # If at save interval => save generated image samples
            if epoch % sample_interval == 0:
                self.sample_images(epoch)
                if not os.path.exists("keras_model"):
                    os.makedirs("keras_model")
                self.generator.save_weights("keras_model/G_model%d.hdf5" % epoch,True)
                self.discriminator.save_weights("keras_model/D_model%d.hdf5" %epoch,True)

    def sample_images(self, epoch):
        r, c = 10, 10
        # 重新生成一批噪音,维度为(100,100)
        noise = np.random.normal(0, 1, (r * c, self.latent_dim))
        gen_imgs = self.generator.predict(noise)

        # 将生成的图片重新归整到0-1之间
        gen = 0.5 * gen_imgs + 0.5
        gen = gen.reshape(-1,4,60)
        
        fig,axs = plt.subplots(r,c)  
        cnt = 0  
        for i in range(r):  
            for j in range(c):  
                xy = gen[cnt]  
                for k in range(len(xy)):  
                    x = xy[k][0:30]  
                    y = xy[k][30:60]  
                    if k == 0:  
                        axs[i,j].plot(x,y,color='blue')  
                    if k == 1:  
                        axs[i,j].plot(x,y,color='red')  
                    if k == 2:  
                        axs[i,j].plot(x,y,color='green')  
                        plt.xlim(0.,1.)
                        plt.ylim(0.,1.)
                        plt.xticks(np.arange(0,1,0.1))
                        plt.xticks(np.arange(0,1,0.1))
                        axs[i,j].axis('off')
                cnt += 1  
        if not os.path.exists("keras_imgs"):
            os.makedirs("keras_imgs")
        fig.savefig("keras_imgs/%d.png" % epoch)
        plt.close()
        
    def test(self,gen_nums=100,save=False):
        self.generator.load_weights("keras_model/G_model14000.hdf5",by_name=True)
        self.discriminator.load_weights("keras_model/D_model14000.hdf5",by_name=True)
        noise = np.random.normal(0,1,(gen_nums,self.latent_dim))
        gen = self.generator.predict(noise)
        gen = 0.5 * gen + 0.5
        gen = gen.reshape(-1,4,60)
        print(gen.shape)
        print(gen[0])
        ###############################################################
        #直接可视化生成图片
        if save:
            for i in range(0,len(gen)):
                plt.figure(figsize=(128,128),dpi=1)
                plt.plot(gen[i][0][0:30],gen[i][0][30:60],color='blue',linewidth=300)
                plt.plot(gen[i][1][0:30],gen[i][1][30:60],color='red',linewidth=300)
                plt.plot(gen[i][2][0:30],gen[i][2][30:60],color='green',linewidth=300)
                plt.axis('off')
                plt.xlim(0.,1.)
                plt.ylim(0.,1.)
                plt.xticks(np.arange(0,1,0.1))
                plt.yticks(np.arange(0,1,0.1))
                if not os.path.exists("keras_gen"):
                    os.makedirs("keras_gen")
                plt.savefig("keras_gen"+os.sep+str(i)+'.jpg',dpi=1)
                plt.close()
        ##################################################################
        #重整图片到0-1
        else:
            for i in range(len(gen)):
                plt.plot(gen[i][0][0:30],gen[i][0][30:60],color='blue')
                plt.plot(gen[i][1][0:30],gen[i][1][30:60],color='red')
                plt.plot(gen[i][2][0:30],gen[i][2][30:60],color='green')
                plt.xlim(0.,1.)
                plt.ylim(0.,1.)
                plt.xticks(np.arange(0,1,0.1))
                plt.xticks(np.arange(0,1,0.1))
                plt.show()



if __name__ == '__main__':
    dcgan = DCGAN()
    dcgan.train(epochs=40000, batch_size=64, sample_interval=1000)
#     dcgan.test()
from __future__ import print_function, division
 
from keras.datasets import mnist
from keras.layers import Input, Dense, Reshape, Flatten, Dropout
from keras.layers import BatchNormalization, Activation, ZeroPadding2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D, Conv2D
from keras.models import Sequential, Model
from keras.optimizers import Adam
import numpy as np
import os 
import random
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
np.set_printoptions(threshold=np.inf) #输出全部矩阵不带省略号
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0" # 选择使用的GPU
 
import sys
 
import numpy as np
 
class DCGAN():
    def __init__(self):
        # Input shape
        self.img_rows = 10
        self.img_cols = 20
        self.channels = 1
        self.img_shape = (self.img_rows, self.img_cols, self.channels)
        self.latent_dim = 100
 
        optimizer = Adam(0.0002, 0.5)
 
        # Build and compile the discriminator
        self.discriminator = self.build_discriminator()
        self.discriminator.compile(loss='binary_crossentropy',
            optimizer=optimizer,
            metrics=['accuracy'])
 
        # Build the generator
        self.generator = self.build_generator()
 
        # The generator takes noise as input and generates imgs
        z = Input(shape=(self.latent_dim,))
        img = self.generator(z)
 
        # For the combined model we will only train the generator
        self.discriminator.trainable = False
 
        # The discriminator takes generated images as input and determines validity
        valid = self.discriminator(img)
 
        # The combined model  (stacked generator and discriminator)
        # Trains the generator to fool the discriminator
        self.combined = Model(z, valid)
        self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)
 
    def build_generator(self):
 
        model = Sequential()
 
        model.add(Dense(512 * 5 * 5, activation="relu", input_dim=self.latent_dim))
        model.add(Reshape((5, 5, 512)))
        
        model.add(UpSampling2D(size=(1,1)))
        model.add(Conv2D(512, kernel_size=(2,2), padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        
        model.add(UpSampling2D(size=(1,1)))
        model.add(Conv2D(256, kernel_size=(2,2), padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        
        model.add(UpSampling2D(size=(1,1)))
        model.add(Conv2D(128, kernel_size=(1,2), padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        
        model.add(UpSampling2D(size=(1,2)))
        model.add(Conv2D(64, kernel_size=(2,2), padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        
        model.add(UpSampling2D(size=(2,2)))
        model.add(Conv2D(32, kernel_size=(2,2), padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        
        model.add(Conv2D(self.channels, kernel_size=(2,2), 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,2), input_shape=self.img_shape, padding="same"))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        
        model.add(Conv2D(64, kernel_size=3, strides=(1,2), padding="same"))
        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=(1,1), 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,1), padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        
        model.add(Conv2D(512, kernel_size=3, strides=(1,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(self, epochs, batch_size=128, sample_interval=50):
 
        ############################################################
        #自己数据集此部分需要更改
        # 加载数据集
        data = np.load('data/Men_546_Convergence.npy')  
        print(data.shape)
        # 归一化到-1到1
        data = data * 2 - 1
        data = np.expand_dims(data, axis=3)
        ############################################################
 
        # Adversarial ground truths
        valid = np.ones((batch_size, 1))
        fake = np.zeros((batch_size, 1))
 
        for epoch in range(epochs):
 
            # ---------------------
            #  Train Discriminator
            # ---------------------
 
            # Select a random half of images
            idx = np.random.randint(0, data.shape[0], batch_size)
            imgs = data[idx]
 
            # Sample noise and generate a batch of new images
            noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
            gen_imgs = self.generator.predict(noise)
 
            # Train the discriminator (real classified as ones and generated as zeros)
            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)
 
            # ---------------------
            #  Train Generator
            # ---------------------
 
            # Train the generator (wants discriminator to mistake images as real)
            g_loss = self.combined.train_on_batch(noise, valid)
 
            # Plot the progress
            print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss))
 
            # If at save interval => save generated image samples
            if epoch % sample_interval == 0:
                self.sample_images(epoch)
                if not os.path.exists("keras_model"):
                    os.makedirs("keras_model")
                self.generator.save_weights("keras_model/G_model%d.hdf5" % epoch,True)
                self.discriminator.save_weights("keras_model/D_model%d.hdf5" %epoch,True)
    def select_axis(self,data):
      branch = []
      data = data.reshape(-1,10,20)
      data = data.tolist()
      for i in range(len(data)):
        zhu_x = data[i][0]
        zuo_x = data[i][1]
        you_x = data[i][2]
        zhu_y = data[i][3]
        zuo_y = data[i][4]
        you_y = data[i][5]
        zhu_diam = data[i][6]
        zuo_diam = data[i][7]
        you_diam = data[i][8]
        zhu_x = zhu_x[0:zhu_diam[1::].index(max(zhu_diam[1::]))+1]
        zuo_x = zuo_x[0:zuo_diam[1::].index(max(zuo_diam[1::]))+1]
        you_x = you_x[0:you_diam[1::].index(max(you_diam[1::]))+1]
        zhu_y = zhu_y[0:zhu_diam[1::].index(max(zhu_diam[1::]))+1]
        zuo_y = zuo_y[0:zuo_diam[1::].index(max(zuo_diam[1::]))+1]
        you_y = you_y[0:you_diam[1::].index(max(you_diam[1::]))+1]
        zhu_xy = zhu_x + zhu_y
        zuo_xy = zuo_x + zuo_y
        you_xy = you_x + you_y
        zhu = zhu_xy + [zhu_diam[0]]
        zuo = zuo_xy + [zuo_diam[0]]
        you = you_xy + [you_diam[0]]
        fencha = [zhu] + [zuo] + [you]
        branch.append(fencha)
      return branch
    
    def sample_images(self, epoch):
        r, c = 10, 10
        # 重新生成一批噪音,维度为(100,100)
        noise = np.random.normal(0, 1, (r * c, self.latent_dim))
        gen_imgs = self.generator.predict(noise)
 
        # 将生成的图片重新归整到0-1之间
        gen = 0.5 * gen_imgs + 0.5
        gen = gen.reshape(-1,10,20)
        gen_data = self.select_axis(gen)
        fig,axs = plt.subplots(r,c)  
        cnt = 0  
        for i in range(r):  
            for j in range(c):  
              gen = gen_data[cnt]
              zhu = gen[0]
              zuo = gen[1]
              you = gen[2]
              zhu_diam = [zhu[-1]]
              zuo_diam = [zuo[-1]]
              you_diam = [you[-1]]
              zhu_x = zhu[0:len(zhu)//2]
              zuo_x = zuo[0:len(zuo)//2]
              you_x = you[0:len(you)//2]
              zhu_y = zhu[len(zhu)//2:(len(zhu)-1)]
              zuo_y = zuo[len(zuo)//2:(len(zuo)-1)]
              you_y = you[len(you)//2:(len(you)-1)]
              axs[i,j].plot(zhu_x,zhu_y, color='red',linewidth=10*zhu_diam[0])
              axs[i,j].plot(zuo_x,zuo_y, color='green',linewidth=10*zuo_diam[0])
              axs[i,j].plot(you_x,you_y, color='blue',linewidth=10*you_diam[0])
              plt.xlim(0.,1.)
              plt.ylim(0.,1.)
              plt.xticks(np.arange(0,1,0.1))
              plt.yticks(np.arange(0,1,0.1))
              axs[i,j].axis('off')
              cnt += 1
        if not os.path.exists("keras_imgs"):
            os.makedirs("keras_imgs")
        fig.savefig("keras_imgs/%d.png" % epoch)
        plt.close()
        
    def test(self,gen_nums=100,save=False):
        self.generator.load_weights("keras_model/G_model14000.hdf5",by_name=True)
        self.discriminator.load_weights("keras_model/D_model14000.hdf5",by_name=True)
        noise = np.random.normal(0,1,(gen_nums,self.latent_dim))
        gen = self.generator.predict(noise)
        gen = 0.5 * gen + 0.5
        gen = gen.reshape(-1,4,60)
        print(gen.shape)
        print(gen[0])
        ###############################################################
        #直接可视化生成图片
        if save:
            for i in range(0,len(gen)):
                plt.figure(figsize=(128,128),dpi=1)
                plt.plot(gen[i][0][0:30],gen[i][0][30:60],color='blue',linewidth=300)
                plt.plot(gen[i][1][0:30],gen[i][1][30:60],color='red',linewidth=300)
                plt.plot(gen[i][2][0:30],gen[i][2][30:60],color='green',linewidth=300)
                plt.axis('off')
                plt.xlim(0.,1.)
                plt.ylim(0.,1.)
                plt.xticks(np.arange(0,1,0.1))
                plt.yticks(np.arange(0,1,0.1))
                if not os.path.exists("keras_gen"):
                    os.makedirs("keras_gen")
                plt.savefig("keras_gen"+os.sep+str(i)+'.jpg',dpi=1)
                plt.close()
        ##################################################################
        #重整图片到0-1
        else:
            for i in range(len(gen)):
                plt.plot(gen[i][0][0:30],gen[i][0][30:60],color='blue')
                plt.plot(gen[i][1][0:30],gen[i][1][30:60],color='red')
                plt.plot(gen[i][2][0:30],gen[i][2][30:60],color='green')
                plt.xlim(0.,1.)
                plt.ylim(0.,1.)
                plt.xticks(np.arange(0,1,0.1))
                plt.xticks(np.arange(0,1,0.1))
                plt.show()
 
 
 
if __name__ == '__main__':
    dcgan = DCGAN()
    dcgan.train(epochs=500000, batch_size=64, sample_interval=1000)
#     dcgan.test()

 

关注
打赏
1636984416
查看更多评论
立即登录/注册

微信扫码登录

0.1291s