Пример #1
0
    def train(self, epochs, batch_size, sample_interval=50):

        dp = data_process('./aug/all/train/number', True)
        dp.point_data_load()
        #dp.image_make()
        #dp.image_read()
        dp.sequence_50()
        dp.data_shuffle()
        #scaler = MinMaxScaler()

        data = dp.point
        data = data.reshape((-1, 1))
        data = self.scaler.fit_transform(data)
        data = data.reshape((-1, 64, 2))
        X_train = data
        # Adversarial ground truths
        valid = -np.ones((batch_size, 1))
        fake = np.ones((batch_size, 1))
        dummy = np.zeros((batch_size, 1))  # Dummy gt for gradient penalty
        for epoch in range(epochs):

            for _ in range(self.n_critic):

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

                # Select a random batch of images
                idx = np.random.randint(0, X_train.shape[0], batch_size)
                imgs = X_train[idx]
                # Sample generator input
                noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
                # Train the critic
                d_loss = self.critic_model.train_on_batch([imgs, noise],
                                                          [valid, fake, dummy])

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

            g_loss = self.generator_model.train_on_batch(noise, valid)

            # Plot the progress
            print("%d [D loss: %f] [G loss: %f]" % (epoch, d_loss[0], g_loss))

            # If at save interval => save generated image samples
            if epoch % sample_interval == 0:
                self.sample_images(epoch)
Пример #2
0
    plt.figure(figsize=(10, 10))
    start_range = digit_size // 2
    end_range = n * digit_size + start_range + 1
    pixel_range = np.arange(start_range, end_range, digit_size)
    sample_range_x = np.round(grid_x, 1)
    sample_range_y = np.round(grid_y, 1)
    plt.xticks(pixel_range, sample_range_x)
    plt.yticks(pixel_range, sample_range_y)
    plt.xlabel("z[0]")
    plt.ylabel("z[1]")
    plt.imshow(figure, cmap='Greys_r')
    plt.savefig(filename)
    plt.show()


dp = data_process('./aug/all/test/number', True)
dp.point_data_load()
#dp.image_make()
#dp.image_read()
dp.sequence_50()
dp.data_shuffle()

x_train = dp.point
# compute the number of labels
num_labels = 10

# network parameters
input_shape = (50, 2)
label_shape = (num_labels, )
batch_size = 128
kernel_size = 3
Пример #3
0
    def train(self, epochs, batch_size=10, sample_interval=50):

        dp = data_process('./aug/all/test/number', True)
        dp.point_data_load()
        #dp.image_make()
        #dp.image_read()
        dp.sequence_50()
        dp.data_shuffle()

        size = int(np.size(dp.point, 0) * 0.7)
        X_train = dp.point
        j = 0
        x_data = []
        y_data = []
        '''
        for k in range(len(dp.point)):
            x_data.append(self.scaler.fit_transform(dp.point[k]))
        
            
        X_train = np.array(x_data)
        Y_DATA = np.array(y_data)

        for i in range(np.size(Y_DATA,0)):
            Y_DATA[i] = np.unique(Y_DATA[i],axis=0)
        Y_DATA = Y_DATA.reshape((np.size(Y_DATA,0),1))
        '''

        # X_train, X_test, Y_train, Y_test = train_test_split(X_DATA, Y_DATA, random_state=42)
        #X_train = keras.preprocessing.sequence.pad_sequences(X_DATA, maxlen=40, padding='post', dtype='float32')

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

        for epoch in range(epochs):

            if (epoch % 50 == 0):
                self.sample_images(1)

            for _ in range(self.n_critic):

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

                # Select a random batch of images
                idx = np.random.randint(0, 16, batch_size)
                imgs = X_train[idx]

                # Sample noise as generator input
                noise = np.random.normal(0, 1, (batch_size, 1000))

                # Generate a batch of new images
                gen_imgs = self.generator.predict(noise)

                # Train the critic
                d_loss_real = self.critic.train_on_batch(imgs, valid)
                d_loss_fake = self.critic.train_on_batch(gen_imgs, fake)
                d_loss = 0.5 * np.add(d_loss_fake, d_loss_real)

                # Clip critic weights
                for l in self.critic.layers:
                    weights = l.get_weights()
                    weights = [
                        np.clip(w, -self.clip_value, self.clip_value)
                        for w in weights
                    ]
                    l.set_weights(weights)

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

            g_loss = self.combined.train_on_batch(noise, valid)

            # Plot the progress
            print("%d [D loss: %f] [G loss: %f]" %
                  (epoch, 1 - d_loss[0], 1 - g_loss[0]))
Пример #4
0
    def call(self, inputs):
        x = inputs[0]
        z_decoded = inputs[1]
        loss = self.vae_loss(x, z_decoded)
        self.add_loss(loss, inputs=inputs)
        return x


y = CustomVariationalLayer()([input_layer, z_decoded])

vae = Model(input_layer, y)
vae.compile(optimizer=optimizers.Adam(), loss=None)
vae.summary()

dp = data_process('./aug/all/train/number', True)
dp.point_data_load()
#dp.image_make()
#dp.image_read()
dp.sequence_50()
#dp.data_shuffle()

x_data = []
scaler = MinMaxScaler()
x_data = dp.point.reshape((-1, 1))
x_data = scaler.fit_transform(x_data)
x_train = x_data.reshape((-1, 50, 2))
#for k in range(len(dp.point)):
#    x_data.append(scaler.fit_transform(dp.point[k]))

#x_train = np.array(x_data)