def gan_model_test(): latent_dim = 10 input_dim = 5 generator = model_generator(input_dim=input_dim, latent_dim=latent_dim) discriminator = model_discriminator(input_dim=input_dim) gan = simple_gan(generator, discriminator, normal_latent_sampling((latent_dim,))) # build adversarial model model = AdversarialModel(base_model=gan, player_params=[generator.trainable_weights, discriminator.trainable_weights], player_names=["generator", "discriminator"]) adversarial_optimizer = AdversarialOptimizerSimultaneous() opt_g = Adam(1e-4) opt_d = Adam(1e-3) loss = 'binary_crossentropy' model.adversarial_compile(adversarial_optimizer=adversarial_optimizer, player_optimizers=[opt_g, opt_d], loss=loss) # train model batch_size = 32 n = batch_size * 8 x = np.random.random((n, input_dim)) y = gan_targets(n) fit(model, x, y, nb_epoch=3, batch_size=batch_size)
def main(): latent_dim = 100 input_shape = (1, 28, 28) generator = model_generator() discriminator = model_discriminator(input_shape=input_shape) gan = simple_gan(generator, discriminator, normal_latent_sampling((latent_dim, ))) generator.summary() discriminator.summary() gan.summary() model = AdversarialModel(base_model=gan, player_params=[ generator.trainable_weights, discriminator.trainable_weights ], player_names=["generator", "discriminator"]) model.adversarial_compile( adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=[Adam(1e-4, decay=1e-4), Adam(1e-3, decay=1e-4)], loss='binary_crossentropy') generator_cb = ImageGridCallback( "output/gan_convolutional/epoch-{:03d}.png", generator_sampler(latent_dim, generator)) xtrain, xtest = mnist_data() xtrain = dim_ordering_fix(xtrain.reshape((-1, 1, 28, 28))) xtest = dim_ordering_fix(xtest.reshape((-1, 1, 28, 28))) y = gan_targets(xtrain.shape[0]) ytest = gan_targets(xtest.shape[0]) history = model.fit(x=xtrain, y=y, validation_data=(xtest, ytest), callbacks=[generator_cb], nb_epoch=100, batch_size=32) df = pd.DataFrame(history.history) df.to_csv("output/gan_convolutional/history.csv") generator.save("output/gan_convolutional/generator.h5") discriminator.save("output/gan_convolutional/discriminator.h5")
def main(): # Uncomment this for debugging # sess = K.get_session() # sess = tf_debug.LocalCLIDebugWrapperSession(sess) # K.set_session(sess) xtrain = imageloader.preprocess( imageloader.filter_paintings(imageloader.load_training_data())) y = gan_targets(xtrain.shape[0]) y[-1] -= 0.1 # 1-sided label smoothing "hack" z = np.random.normal(size=(xtrain.shape[0], latent_dim)) catalog_file = catalog.load_catalog() numerical_categories = catalog.transform_categorical_to_numerical( catalog.types(catalog_file)) one_hots = transform_to_one_hot_vectors(numerical_categories) current_epoch, discriminator, generator = load_models() generator.summary() discriminator.summary() gan = simple_gan(generator=generator, discriminator=discriminator, latent_sampling=None) # build adversarial model model = AdversarialModel(base_model=gan, player_params=[ generator.trainable_weights, discriminator.trainable_weights ], player_names=["generator", "discriminator"]) model.adversarial_compile(adversarial_optimizer=optimizer, player_optimizers=[opt_g, opt_d], loss={ "yfake": "binary_crossentropy", "yreal": "binary_crossentropy", "yreal_label": "categorical_crossentropy", "yfake_label": "categorical_crossentropy" }) callbacks = initialize_callbacks(path, generator, discriminator, latent_dim) y = y[:2] + [one_hots] * 2 + y[2:] + [one_hots] * 2 history = fit( model, x=[z, numerical_categories[imageloader.painting_filter()], xtrain], y=y, callbacks=callbacks, nb_epoch=nb_epoch, initial_epoch=current_epoch, batch_size=32)
def train_on_data_batch(model, data_file, print_interval): data = np.load(open_file_in_bucket(data_file, 'song-embeddings-dataset')) num_batches = data.shape[0]/batch_size + 1 batch_losses = [] batch_indices = [ get_batch_range(i, batch_size, data.shape[0]) for i in range(num_batches) ] for i in range(len(batch_indices)): start, end = batch_indices[i] #batch = rescale_batch(data[start:end, :, :, None]) batch = data[start:end, :, :, None] targets = gan_targets(end - start) targets[0] *= np.random.uniform(0.7, 0.9, end - start)[:, None] targets[3] *= np.random.uniform(0.7, 0.9, end - start)[:, None] losses = model.train_on_batch(batch, targets) batch_losses.append(losses) if i % print_interval == 0: print losses print np.mean(np.reshape(model.predict(data[start:end, :, :, None]), (4, -1)), axis=1) return batch_losses
def main(): # set path root_dir = os.path.abspath('.') data_dir = os.path.join(root_dir, 'MData') # load data train = pd.read_csv(os.path.join(data_dir, 'Train', 'train.csv')) # test = pd.read_csv(os.path.join(data_dir, 'test.csv')) temp = [] for img_name in train.filename: image_path = os.path.join(data_dir, 'Train', 'Images', 'train', img_name) img = imread(image_path, flatten=True) img = img.astype('float32') temp.append(img) train_x = np.stack(temp) train_x = train_x / 255 epochs = 1 batch_size = 128 model_1 = model_generator_cifar() model_2 = model_discriminator_cifar() # gan = simple_gan(model_1, model_2, normal_latent_sampling((100,))) latent_dim = 100 gan = simple_gan(model_1, model_2, latent_sampling=normal_latent_sampling((latent_dim,))) model = AdversarialModel(base_model=gan,player_params=[model_1.trainable_weights, model_2.trainable_weights]) model.adversarial_compile(adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=['adam', 'adam'], loss='binary_crossentropy') history = model.fit(x=train_x, y=gan_targets(train_x.shape[0]), epochs=epochs, batch_size=batch_size) zsamples = np.random.normal(size=(10, 100)) pred = model_1.predict(zsamples) for i in range(pred.shape[0]): plt.imshow(pred[i, :], cmap='gray') plt.savefig('out/animals/'+str(i)+'.png')
model = AdversarialModel(base_model=gan, player_params=[ generator.trainable_weights, descriminator.trainable_weights ], player_names=["generator", "discrminator"]) model.adversarial_compile( adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=[Adam(1e-4, decay=1e-4), Adam(1e-3, decay=1e-4)], loss='binary_crossentropy') #train model generator_cb = ImageGridCallback( 'output/gan_convolutional/epoch-{:03d}.png', generator_samples(latent_dim, generator)) xtrain, xtest = mnist_data() xtrain = dim_ordering_fix(xtrain.reshape((-1, 1, 28, 28))) xtest = dim_ordering_fix(xtest.reshape((-1, 1, 28, 28))) y = gan_targets(xtest.shape[0]) xtest = gan_targets(xtest.shape[0]) history = model.fit(x=xtrain, y=y, validation_data=(xtest, y), callbacks=[generator_cb], nb_epoch=10, batch_size=32) df = pd.DataFrame(history.history) df.to_csv('output/gan_convolutional/history.csv') generator.save("output/gan_convolutional/generator.h5") descriminator.save("output/gan_convolutional/desrimiknator.h5")
gerador.add(Reshape((28, 28))) # Discriminador discriminador = Sequential() discriminador.add(InputLayer(input_shape=(28, 28))) discriminador.add(Flatten()) discriminador.add( Dense(units=500, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5))) discriminador.add( Dense(units=500, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5))) discriminador.add( Dense(units=1, activation='sigmoid', kernel_regularizer=L1L2(1e-5, 1e-5))) gan = simple_gan(gerador, discriminador, normal_latent_sampling((100, ))) modelo = AdversarialModel( base_model=gan, player_params=[gerador.trainable_weights, discriminador.trainable_weights]) modelo.adversarial_compile( adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=['adam', 'adam'], loss='binary_crossentropy') modelo.fit(x=previsores_treinamento, y=gan_targets(60000), epochs=100, batch_size=256) amostras = np.random.normal(size=(20, 100)) previsao = gerador.predict(amostras) for i in range(previsao.shape[0]): plt.imshow(previsao[i, :], cmap='gray') plt.show()
kernel_regularizer=L1L2(1e-5, 1e-5)), Dense(units=d_output_num_units, activation='sigmoid', kernel_regularizer=L1L2(1e-5, 1e-5)), ]) """ print(model_1.summary()) print(model_2.summary()) """ from keras_adversarial import AdversarialModel, simple_gan, gan_targets from keras_adversarial import AdversarialOptimizerSimultaneous, normal_latent_sampling gan = simple_gan(model_1, model_2, normal_latent_sampling((100, ))) model = AdversarialModel( base_model=gan, player_params=[model_1.trainable_weights, model_2.trainable_weights]) model.adversarial_compile( adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=['adam', 'adam'], loss='binary_crossentropy') print(gan.summary()) history = model.fit(x=train_x, y=gan_targets(train_x.shape[0]), epochs=10, batch_size=batch_size) print(gan.summary()) plt.plot(history.history['player_0_loss']) plt.plot(history.history['player_1_loss']) plt.plot(history.history['loss'])
def gan(): # define variables # 初始化一些参数 g_input_shape = 100 # 生成器输入层节点数 d_input_shape = (28, 28) # 辨别器输入层节点数 hidden_1_num_units = 500 hidden_2_num_units = 500 g_output_num_units = 784 # 生成器输出层节点数28*28 d_output_num_units = 1 # 辨别器输出层节点数1个,辨别是否是真实图片 epochs = 100 batch_size = 128 # 定义生成器,用于生成图片 model_g = Sequential([ Dense(units=hidden_1_num_units, input_dim=g_input_shape, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5)), Dense(units=hidden_2_num_units, activation='relu', kernel_regularizer=L1L2(1E-5, 1E-5)), Dense(units=g_output_num_units, activation='sigmoid', kernel_regularizer=L1L2(1E-5, 1E-5)), Reshape(d_input_shape) ]) # 定义分辨器,用于辨别图片 model_d = Sequential([ InputLayer(input_shape=d_input_shape), Flatten(), Dense(units=hidden_1_num_units, activation='relu', kernel_regularizer=L1L2(1E-5, 1E-5)), Dense(units=hidden_2_num_units, activation='relu', kernel_regularizer=L1L2(1E-5, 1E-5)), Dense(units=d_output_num_units, activation='sigmoid', kernel_regularizer=L1L2(1E-5, 1E-5)) ]) # model_g.summary() # model_d.summary() from keras_adversarial import AdversarialModel, simple_gan, gan_targets from keras_adversarial import AdversarialOptimizerSimultaneous, normal_latent_sampling # 开始训练gan网络 gan = simple_gan(model_g, model_d, normal_latent_sampling((100, ))) # gan.summary() # 在keras2.2.x版本中,下面的代码会报错,keras2.1.2中不会 model = AdversarialModel( base_model=gan, player_params=[model_g.trainable_weights, model_d.trainable_weights]) model.adversarial_compile( adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=['adam', 'adam'], loss='binary_crossentropy') # 使用训练数据进行训练 # 把keras_adversarial clone到了本地,然后替换掉了pip安装的keras_adversarial # 解决了这个报错AttributeError: 'AdversarialModel' object has no attribute '_feed_output_shapes' history = model.fit(x=train_x, y=gan_targets(train_x.shape[0]), epochs=epochs, batch_size=batch_size) # 保存为h5文件 model_g.save_weights('gan1_g.h5') model_d.save_weights('gan1_d.h5') model.save_weights('gan1.h5') # 绘制训练结果的loss plt.plot(history.history['player_0_loss'], label='player_0_loss') plt.plot(history.history['player_1_loss'], label='player_1_loss') plt.plot(history.history['loss'], label='loss') plt.show() # 训练之后100次之后生成的图像 # 随机生成10组数据,生成10张图像 zsample = np.random.normal(size=(10, 100)) pred = model_g.predict(zsample) print(pred.shape) # (10,28,28) for i in range(pred.shape[0]): plt.imshow(pred[i, :], cmap='gray') plt.show()
def main(): # z \in R^100 latent_dim = 100 # x \in R^{28x28} input_shape = (28, 28) # generator (z -> x) generator = model_generator(latent_dim, input_shape) # encoder (x ->z) encoder = model_encoder(latent_dim, input_shape) # autoencoder (x -> x') autoencoder = Model(encoder.inputs, generator(encoder(encoder.inputs))) # discriminator (x -> y) discriminator = model_discriminator(latent_dim, input_shape) # bigan (x - > yfake, yreal), z generated on GPU bigan = simple_bigan(generator, encoder, discriminator, normal_latent_sampling((latent_dim, ))) generative_params = generator.trainable_weights + encoder.trainable_weights # print summary of models generator.summary() encoder.summary() discriminator.summary() bigan.summary() autoencoder.summary() # build adversarial model model = AdversarialModel( base_model=bigan, player_params=[generative_params, discriminator.trainable_weights], player_names=["generator", "discriminator"]) model.adversarial_compile( adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=[Adam(1e-4, decay=1e-4), Adam(1e-3, decay=1e-4)], loss='binary_crossentropy') # train model xtrain, xtest = mnist_data() def generator_sampler(): zsamples = np.random.normal(size=(10 * 10, latent_dim)) return generator.predict(zsamples).reshape((10, 10, 28, 28)) generator_cb = ImageGridCallback("output/bigan/generated-epoch-{:03d}.png", generator_sampler) def autoencoder_sampler(): xsamples = n_choice(xtest, 10) xrep = np.repeat(xsamples, 9, axis=0) xgen = autoencoder.predict(xrep).reshape((10, 9, 28, 28)) xsamples = xsamples.reshape((10, 1, 28, 28)) x = np.concatenate((xsamples, xgen), axis=1) return x autoencoder_cb = ImageGridCallback( "output/bigan/autoencoded-epoch-{:03d}.png", autoencoder_sampler) y = gan_targets(xtrain.shape[0]) ytest = gan_targets(xtest.shape[0]) history = model.fit(x=xtrain, y=y, validation_data=(xtest, ytest), callbacks=[generator_cb, autoencoder_cb], nb_epoch=100, batch_size=32) df = pd.DataFrame(history.history) df.to_csv("output/bigan/history.csv") encoder.save("output/bigan/encoder.h5") generator.save("output/bigan/generator.h5") discriminator.save("output/bigan/discriminator.h5")
# discriminator model_2 = Sequential([ InputLayer(input_shape=d_input_shape), Flatten(), Dense(units=hidden_1_num_units, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5)), Dense(units=hidden_2_num_units, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5)), Dense(units=d_output_num_units, activation='sigmoid', kernel_regularizer=L1L2(1e-5, 1e-5)), ]) print model_1.summary() print model_2.summary() from keras_adversarial import AdversarialModel, simple_gan, gan_targets from keras_adversarial import AdversarialOptimizerSimultaneous, normal_latent_sampling gan = simple_gan(model_1, model_2, normal_latent_sampling((100,))) model = AdversarialModel(base_model=gan,player_params=[model_1.trainable_weights, model_2.trainable_weights]) model.adversarial_compile(adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=['adam', 'adam'], loss='binary_crossentropy') print gan.summary() history = model.fit(x=train_x, y=gan_targets(train_x.shape[0]), epochs=10, batch_size=batch_size)
# print summary of models generator.summary() discriminator.summary() gan.summary() # build adversarial model model = AdversarialModel(base_model=gan, player_params=[generator.trainable_weights, discriminator.trainable_weights], player_names=["generator", "discriminator"]) model.adversarial_compile(adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=[Adam(1e-4, decay=1e-4), Adam(1e-3, decay=1e-4)], loss='binary_crossentropy') # train model generator_cb = ImageGridCallback("output/gan_convolutional/epoch-{:03d}.png", generator_sampler(latent_dim, generator)) xtrain, xtest = mnist_data() xtrain = dim_ordering_fix(xtrain.reshape((-1, 1, 28, 28))) xtest = dim_ordering_fix(xtest.reshape((-1, 1, 28, 28))) y = gan_targets(xtrain.shape[0]) ytest = gan_targets(xtest.shape[0]) history = model.fit(x=xtrain, y=y, validation_data=(xtest, ytest), callbacks=[generator_cb], nb_epoch=100, batch_size=32) df = pd.DataFrame(history.history) df.to_csv("output/gan_convolutional/history.csv") generator.save("output/gan_convolutional/generator.h5") discriminator.save("output/gan_convolutional/discriminator.h5")
def test_generator(): g = test_datagen.flow(xtest, batch_size=batch_size) for d in g: yield (d, gan_targets(batch_size), None)
def train_generator(): g = train_datagen.flow(xtrain, batch_size=batch_size) for d in g: yield (d, gan_targets(batch_size), None)
def main(): # z \in R^100 latent_dim = 100 # x \in R^{28x28} input_shape = (28, 28) # generator (z -> x) generator = model_generator(latent_dim, input_shape) # encoder (x ->z) encoder = model_encoder(latent_dim, input_shape) # autoencoder (x -> x') autoencoder = Model(encoder.inputs, generator(encoder(encoder.inputs))) # discriminator (x -> y) discriminator = model_discriminator(latent_dim, input_shape) # bigan (x - > yfake, yreal), z generated on GPU bigan = simple_bigan(generator, encoder, discriminator, normal_latent_sampling((latent_dim,))) generative_params = generator.trainable_weights + encoder.trainable_weights # print summary of models generator.summary() encoder.summary() discriminator.summary() bigan.summary() autoencoder.summary() # build adversarial model model = AdversarialModel(base_model=bigan, player_params=[generative_params, discriminator.trainable_weights], player_names=["generator", "discriminator"]) model.adversarial_compile(adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=[Adam(1e-4, decay=1e-4), Adam(1e-3, decay=1e-4)], loss='binary_crossentropy') # train model xtrain, xtest = mnist_data() def generator_sampler(): zsamples = np.random.normal(size=(10 * 10, latent_dim)) return generator.predict(zsamples).reshape((10, 10, 28, 28)) generator_cb = ImageGridCallback("output/bigan/generated-epoch-{:03d}.png", generator_sampler) def autoencoder_sampler(): xsamples = n_choice(xtest, 10) xrep = np.repeat(xsamples, 9, axis=0) xgen = autoencoder.predict(xrep).reshape((10, 9, 28, 28)) xsamples = xsamples.reshape((10, 1, 28, 28)) x = np.concatenate((xsamples, xgen), axis=1) return x autoencoder_cb = ImageGridCallback("output/bigan/autoencoded-epoch-{:03d}.png", autoencoder_sampler) y = gan_targets(xtrain.shape[0]) ytest = gan_targets(xtest.shape[0]) history = model.fit(x=xtrain, y=y, validation_data=(xtest, ytest), callbacks=[generator_cb, autoencoder_cb], nb_epoch=100, batch_size=32) df = pd.DataFrame(history.history) df.to_csv("output/bigan/history.csv") encoder.save("output/bigan/encoder.h5") generator.save("output/bigan/generator.h5") discriminator.save("output/bigan/discriminator.h5")
def main(): data_dir = "goldens_filtered_32x32_gray/" out_dir = "m_gan_out/" epochs = 1 batch_size = 64 # TODO: Research why these values were chosen opt_g = Adam(1e-4, decay=1e-5) opt_d = Adam(1e-3, decay=1e-5) loss = 'binary_crossentropy' latent_dim = 100 adversarial_optimizer = AdversarialOptimizerSimultaneous() # My simple models # generator = get_generator() # discriminator = get_discriminator() # CIFAR example convolutional models generator = get_generator_cifar() discriminator = get_discriminator_cifar() gan = simple_gan(generator, discriminator, normal_latent_sampling((latent_dim, ))) # print summary of models generator.summary() discriminator.summary() gan.summary() # build adversarial model model = AdversarialModel(base_model=gan, player_params=[ generator.trainable_weights, discriminator.trainable_weights ], player_names=["generator", "discriminator"]) model.adversarial_compile(adversarial_optimizer=adversarial_optimizer, player_optimizers=[opt_g, opt_d], loss=loss) temp = [] for img_name in os.listdir(data_dir): image_path = data_dir + img_name img = imread(image_path) img = img.astype('float32') temp.append(img) train_x = np.stack(temp) train_x = train_x / 255 # Side effects model.fit(x=train_x, y=gan_targets(train_x.shape[0]), epochs=epochs, batch_size=batch_size) zsamples = np.random.normal(size=(10, latent_dim)) pred = generator.predict(zsamples) for i in range(pred.shape[0]): plt.imshow(pred[i, :]) plt.savefig(out_dir + str(i) + '.png')
def example_bigan(path, adversarial_optimizer): # z \in R^100 latent_dim = 25 # x \in R^{28x28} input_shape = (28, 28) # generator (z -> x) generator = model_generator(latent_dim, input_shape) # encoder (x ->z) encoder = model_encoder(latent_dim, input_shape) # autoencoder (x -> x') autoencoder = Model(encoder.inputs, generator(encoder(encoder.inputs))) # discriminator (x -> y) discriminator_train, discriminator_test = model_discriminator( latent_dim, input_shape) # bigan (z, x - > yfake, yreal) bigan_generator = simple_bigan(generator, encoder, discriminator_test) bigan_discriminator = simple_bigan(generator, encoder, discriminator_train) # z generated on GPU based on batch dimension of x x = bigan_generator.inputs[1] z = normal_latent_sampling((latent_dim, ))(x) # eliminate z from inputs bigan_generator = Model([x], fix_names(bigan_generator([z, x]), bigan_generator.output_names)) bigan_discriminator = Model([x], fix_names(bigan_discriminator([z, x]), bigan_discriminator.output_names)) generative_params = generator.trainable_weights + encoder.trainable_weights # print summary of models generator.summary() encoder.summary() discriminator_train.summary() bigan_discriminator.summary() autoencoder.summary() # build adversarial model model = AdversarialModel( player_models=[bigan_generator, bigan_discriminator], player_params=[ generative_params, discriminator_train.trainable_weights ], player_names=["generator", "discriminator"]) model.adversarial_compile( adversarial_optimizer=adversarial_optimizer, player_optimizers=[Adam(1e-4, decay=1e-4), Adam(1e-3, decay=1e-4)], loss='binary_crossentropy') # load mnist data xtrain, xtest = mnist_data() # callback for image grid of generated samples def generator_sampler(): zsamples = np.random.normal(size=(10 * 10, latent_dim)) return generator.predict(zsamples).reshape((10, 10, 28, 28)) generator_cb = ImageGridCallback( os.path.join(path, "generated-epoch-{:03d}.png"), generator_sampler) # callback for image grid of autoencoded samples def autoencoder_sampler(): xsamples = n_choice(xtest, 10) xrep = np.repeat(xsamples, 9, axis=0) xgen = autoencoder.predict(xrep).reshape((10, 9, 28, 28)) xsamples = xsamples.reshape((10, 1, 28, 28)) x = np.concatenate((xsamples, xgen), axis=1) return x autoencoder_cb = ImageGridCallback( os.path.join(path, "autoencoded-epoch-{:03d}.png"), autoencoder_sampler) # train network y = gan_targets(xtrain.shape[0]) ytest = gan_targets(xtest.shape[0]) history = model.fit(x=xtrain, y=y, validation_data=(xtest, ytest), callbacks=[generator_cb, autoencoder_cb], nb_epoch=100, batch_size=32) # save history df = pd.DataFrame(history.history) df.to_csv(os.path.join(path, "history.csv")) # save model encoder.save(os.path.join(path, "encoder.h5")) generator.save(os.path.join(path, "generator.h5")) discriminator_train.save(os.path.join(path, "discriminator.h5"))
# Gerador gerador = Sequential() gerador.add(Dense(units = 500, input_dim = 100, activation = 'relu', kernel_regularizer = L1L2(1e-5, 1e-5))) gerador.add(Dense(units = 500, activation = 'relu', kernel_regularizer = L1L2(1e-5, 1e-5))) gerador.add(Dense(units = 784, activation = 'sigmoid', kernel_regularizer = L1L2(1e-5, 1e-5))) gerador.add(Reshape((28,28))) # Discriminador discriminador = Sequential() discriminador.add(InputLayer(input_shape=(28,28))) discriminador.add(Flatten()) discriminador.add(Dense(units = 500, activation = 'relu', kernel_regularizer = L1L2(1e-5, 1e-5))) discriminador.add(Dense(units = 500, activation = 'relu', kernel_regularizer = L1L2(1e-5, 1e-5))) discriminador.add(Dense(units = 1, activation = 'sigmoid', kernel_regularizer = L1L2(1e-5, 1e-5))) gan = simple_gan(gerador, discriminador, normal_latent_sampling((100,))) modelo = AdversarialModel(base_model = gan, player_params = [gerador.trainable_weights, discriminador.trainable_weights]) modelo.adversarial_compile(adversarial_optimizer = AdversarialOptimizerSimultaneous(), player_optimizers = ['adam', 'adam'], loss = 'binary_crossentropy') modelo.fit(x = previsores_treinamento, y = gan_targets(60000), epochs = 100, batch_size = 256) amostras = np.random.normal(size = (20,100)) previsao = gerador.predict(amostras) for i in range(previsao.shape[0]): plt.imshow(previsao[i, :], cmap='gray') plt.show()
def main(): # to stop potential randomness seed = 128 rng = np.random.RandomState(seed) # set path root_dir = os.path.abspath('.') data_dir = os.path.join(root_dir, 'Data') # load data train = pd.read_csv(os.path.join(data_dir, 'Train', 'train.csv')) # test = pd.read_csv(os.path.join(data_dir, 'test.csv')) temp = [] for img_name in train.filename: image_path = os.path.join(data_dir, 'Train', 'Images', 'train', img_name) img = imread(image_path, flatten=True) img = img.astype('float32') temp.append(img) train_x = np.stack(temp) train_x = train_x / 255 # print image img_name = rng.choice(train.filename) filepath = os.path.join(data_dir, 'Train', 'Images', 'train', img_name) img = imread(filepath, flatten=True) # pylab stuff, who f****n knows # pylab.imshow(img, cmap='gray') # pylab.axis('off') # pylab.show() # Levers g_input_shape = 100 d_input_shape = (28, 28) hidden_1_num_units = 500 hidden_2_num_units = 500 g_output_num_units = 784 d_output_num_units = 1 epochs = 25 batch_size = 128 # generator model_1 = Sequential([ Dense(units=hidden_1_num_units, input_dim=g_input_shape, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5)), Dense(units=hidden_2_num_units, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5)), Dense(units=g_output_num_units, activation='sigmoid', kernel_regularizer=L1L2(1e-5, 1e-5)), Reshape(d_input_shape), ]) # discriminator model_2 = Sequential([ InputLayer(input_shape=d_input_shape), Flatten(), Dense(units=hidden_1_num_units, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5)), Dense(units=hidden_2_num_units, activation='relu', kernel_regularizer=L1L2(1e-5, 1e-5)), Dense(units=d_output_num_units, activation='sigmoid', kernel_regularizer=L1L2(1e-5, 1e-5)), ]) gan = simple_gan(model_1, model_2, normal_latent_sampling((100, ))) model = AdversarialModel( base_model=gan, player_params=[model_1.trainable_weights, model_2.trainable_weights]) model.adversarial_compile( adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=['adam', 'adam'], loss='binary_crossentropy') history = model.fit(x=train_x, y=gan_targets(train_x.shape[0]), epochs=10, batch_size=batch_size) zsamples = np.random.normal(size=(10, 100)) pred = model_1.predict(zsamples) for i in range(pred.shape[0]): plt.imshow(pred[i, :], cmap='gray') plt.savefig('out/numbers/' + str(i) + '.png')
kernel_regularizer=L1L2(1e-5, 1e-5), bias_initializer='ones', bias_constraint=non_neg())) gan = simple_gan(generator, discriminator, normal_latent_sampling((25, ))) model = AdversarialModel(base_model=gan, player_params=[ generator.trainable_weights, discriminator.trainable_weights ]) model.adversarial_compile( adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=['adam', 'adam'], loss='binary_crossentropy') test = model.fit(x=x_train, y=gan_targets(np.array(x_train).shape[0]), epochs=100, batch_size=50, shuffle=True) discriminator.save('discriminator.h5') generator.save('generator.h5') generator = load_model('generator.h5') houseTest = [] pred = generator.predict(np.random.uniform(-1.0, 1.0, size=(1, 25))) for i in range(len(pred)): houseTest.append(normalizationVector[i] * pred[i]) print(houseTest)
# print(generator.summary()) # print(discriminator.summary()) # Build a GAN gan = simple_gan(generator=generator, discriminator=discriminator, latent_sampling=normal_latent_sampling((100, ))) model = AdversarialModel( base_model=gan, player_params=[ generator.trainable_weights, discriminator.trainable_weights ], ) model.adversarial_compile( adversarial_optimizer=AdversarialOptimizerSimultaneous(), player_optimizers=['adam', 'adam'], loss='binary_crossentropy') history = model.fit(train_data, gan_targets(train_data.shape[0]), epochs=10, batch_size=batch_size) sample = np.random.normal(size=(10, 100)) pred = generator.predict(sample) for i in range(pred.shape[0]): plt.imshow(pred[i, :], cmap='gray') plt.show() # print(model.summary())
def driver_gan(path, adversarial_optimizer): # z \in R^100 latent_dim = 3 # x \in R^{28x28} input_shape = (15, 6) # generator (z -> x) generator = model_generator(latent_dim, input_shape) # encoder (x ->z) encoder = model_encoder(latent_dim, input_shape) # autoencoder (x -> x') autoencoder = Model(encoder.inputs, generator(encoder(encoder.inputs))) # discriminator (x -> y) discriminator_train, discriminator_test = model_discriminator(latent_dim, input_shape) # bigan (z, x - > yfake, yreal) bigan_generator = simple_bigan(generator, encoder, discriminator_test) bigan_discriminator = simple_bigan(generator, encoder, discriminator_train) # z generated on GPU based on batch dimension of x x = bigan_generator.inputs[1] z = normal_latent_sampling((latent_dim,))(x) # eliminate z from inputs bigan_generator = Model([x], fix_names(bigan_generator([z, x]), bigan_generator.output_names)) bigan_discriminator = Model([x], fix_names(bigan_discriminator([z, x]), bigan_discriminator.output_names)) # Merging encoder weights and generator weights generative_params = generator.trainable_weights + encoder.trainable_weights # print summary of models generator.summary() encoder.summary() discriminator_train.summary() bigan_discriminator.summary() autoencoder.summary() # build adversarial model model = AdversarialModel(player_models=[bigan_generator, bigan_discriminator], player_params=[generative_params, discriminator_train.trainable_weights], player_names=["generator", "discriminator"]) model.adversarial_compile(adversarial_optimizer=adversarial_optimizer, player_optimizers=[Adam(1e-7, decay=1e-7), Adam(1e-6, decay=1e-7)], loss='binary_crossentropy') # load driver data train_dataset = [1,2,5] test_dataset = [3,4] train_reader = data_base(train_dataset) test_reader = data_base(test_dataset) xtrain, xtest = train_reader.read_files(),test_reader.read_files() # --------------------------------------------------------------------------------- # callback for image grid of generated samples def generator_sampler(): zsamples = np.random.normal(size=(1 * 1, latent_dim)) #---------------------------------> (10,10) return generator.predict(zsamples).reshape((1, 1, 15, 6))# confused ***********************************default (10,10,28,28) # callback for image grid of autoencoded samples def autoencoder_sampler(): xsamples = n_choice(xtest, 10) # the number of testdata set xrep = np.repeat(xsamples, 5, axis=0) # the number of train dataset xgen = autoencoder.predict(xrep).reshape((1, 1, 15, 6)) xsamples = xsamples.reshape((1, 1, 15, 6)) x = np.concatenate((xsamples, xgen), axis=1) return x # train network y = gan_targets(xtrain.shape[0]) ytest = gan_targets(xtest.shape[0]) history = model.fit(x=xtrain, y=y, validation_data=(xtest, ytest), nb_epoch=25, batch_size=10, verbose=0) # save history df = pd.DataFrame(history.history) df.to_csv(os.path.join(path, "history.csv")) # save model encoder.save(os.path.join(path, "encoder.h5")) generator.save(os.path.join(path, "generator.h5")) discriminator_train.save(os.path.join(path, "discriminator.h5"))
dictio = pickle.load(fp) data = dictio['SSTMW'] x = data[:, :92, :92].astype(np.float32) xtrain = x[:-10] xtest = x[-10:] mini = np.min(xtrain.ravel()) maxi = np.max(xtrain.ravel()) xtrain = (xtrain - mini) / (maxi - mini) xtest = (xtest - mini) / (maxi - mini) xtrain = dim_ordering_fix(xtrain.reshape((-1, 1, 92, 92))) xtest = dim_ordering_fix(xtest.reshape((-1, 1, 92, 92))) y = gan_targets(xtrain.shape[0]) ytest = gan_targets(xtest.shape[0]) history = model.fit(x=xtrain, y=y, validation_data=(xtest, ytest), callbacks=[generator_cb], nb_epoch=100, batch_size=10) df = pd.DataFrame(history.history) df.to_csv("output/gan_convolutional/history.csv") generator.save("output/gan_convolutional/generator.h5") discriminator.save("output/gan_convolutional/discriminator.h5") # print(xtrain[0]) """
def example_bigan(path, adversarial_optimizer): # z \in R^100 latent_dim = 25 # x \in R^{28x28} input_shape = (28, 28) # generator (z -> x) generator = model_generator(latent_dim, input_shape) # encoder (x ->z) encoder = model_encoder(latent_dim, input_shape) # autoencoder (x -> x') autoencoder = Model(encoder.inputs, generator(encoder(encoder.inputs))) # discriminator (x -> y) discriminator_train, discriminator_test = model_discriminator(latent_dim, input_shape) # bigan (z, x - > yfake, yreal) bigan_generator = simple_bigan(generator, encoder, discriminator_test) bigan_discriminator = simple_bigan(generator, encoder, discriminator_train) # z generated on GPU based on batch dimension of x x = bigan_generator.inputs[1] z = normal_latent_sampling((latent_dim,))(x) # eliminate z from inputs bigan_generator = Model([x], fix_names(bigan_generator([z, x]), bigan_generator.output_names)) bigan_discriminator = Model([x], fix_names(bigan_discriminator([z, x]), bigan_discriminator.output_names)) generative_params = generator.trainable_weights + encoder.trainable_weights # print summary of models generator.summary() encoder.summary() discriminator_train.summary() bigan_discriminator.summary() autoencoder.summary() # build adversarial model model = AdversarialModel(player_models=[bigan_generator, bigan_discriminator], player_params=[generative_params, discriminator_train.trainable_weights], player_names=["generator", "discriminator"]) model.adversarial_compile(adversarial_optimizer=adversarial_optimizer, player_optimizers=[Adam(1e-4, decay=1e-4), Adam(1e-3, decay=1e-4)], loss='binary_crossentropy') # load mnist data xtrain, xtest = mnist_data() # callback for image grid of generated samples def generator_sampler(): zsamples = np.random.normal(size=(10 * 10, latent_dim)) return generator.predict(zsamples).reshape((10, 10, 28, 28)) generator_cb = ImageGridCallback(os.path.join(path, "generated-epoch-{:03d}.png"), generator_sampler) # callback for image grid of autoencoded samples def autoencoder_sampler(): xsamples = n_choice(xtest, 10) xrep = np.repeat(xsamples, 9, axis=0) xgen = autoencoder.predict(xrep).reshape((10, 9, 28, 28)) xsamples = xsamples.reshape((10, 1, 28, 28)) x = np.concatenate((xsamples, xgen), axis=1) return x autoencoder_cb = ImageGridCallback(os.path.join(path, "autoencoded-epoch-{:03d}.png"), autoencoder_sampler) # train network y = gan_targets(xtrain.shape[0]) ytest = gan_targets(xtest.shape[0]) history = model.fit(x=xtrain, y=y, validation_data=(xtest, ytest), callbacks=[generator_cb, autoencoder_cb], nb_epoch=100, batch_size=32) # save history df = pd.DataFrame(history.history) df.to_csv(os.path.join(path, "history.csv")) # save model encoder.save(os.path.join(path, "encoder.h5")) generator.save(os.path.join(path, "generator.h5")) discriminator_train.save(os.path.join(path, "discriminator.h5"))