def train_test(target_shape, dataset): model = GAN(batch_size=64, target_shape=target_shape, tag=tag) trainer = GANTrainer(model=model, dataset=dataset, num_train_steps=100000, lr=0.0001) trainer.train_model(None) model.batch_size = 100 fid, i_s = trainer.test_gan_all() write_results(fid, i_s, model, dataset, tag)
def main(config_file): data_config, _ = get_config_from_json(config_file) # Load the dataset and send model to CUDA if available training_data = Dataset(data_config, mode='train') device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') print(device) gan = GAN(device=device) # For model evaluation temp = data.DataLoader(training_data, batch_size=8, shuffle=False) _, attr = next(iter(temp)) attr = attr.to(device) z = torch.randn((8, 216, 1, 1), dtype=torch.double, device=device) trainer = GANTrainer(training_data, batch_size=32, model=gan, device=device) abs_it = 0 for epoch_id in range(10): it = trainer.train_epoch(abs_it, epoch_id=epoch_id, z_eval=z, attr_eval=attr) abs_it = it print('Done training.')
def main(FLAGS): # パラメータの取得 save_path = os.getenv("SAVE_FOLDER", "results") print("[LOADING]\tmodel parameters loding") # ファイルのパラメータ folder = FLAGS.folder resize = [FLAGS.resize, FLAGS.resize] gray = FLAGS.gray channel = 1 if gray else 3 # GANのクラスに関するパラメータ type = FLAGS.type layers = [64, 128, 256, 256, 256, 256] max_epoch = FLAGS.max_epoch batch_num = FLAGS.batch_size save_folder = FLAGS.save_folder save_path = save_path + "/" + save_folder # 画像の読み込み if type == "gan": train_image, train_label = imload.make( folder, img_size=resize, gray=gray, ) elif type == "cgan": train_image, train_label = imload.make_labels( folder, img_size=resize, gray=gray, ) # バッチの作成 batch = Batchgen(train_image, train_label) trainer = Trainer(batch_num=batch_num, max_epoch=max_epoch) # モデルの作成 if type == "gan": print("[LOADING]\tGAN") gan = GAN( input_size=resize, channel=channel, layers=layers, save_folder=save_path, ) # 学習の開始 trainer.train(batch, gan) elif type == "cgan": print("[LOADING]\tConditional GAN") print(len(train_label)) cgan = cGAN( input_size=resize, channel=channel, label_num=len(train_label[0]), layers=layers, save_folder=save_path, ) # 学習の開始 trainer.train(batch, cgan, type="cgan")
import tensorflow as tf from sys import path path.append('../data/') from data import data path.append('..') for loc in ["EPO"]: print(loc) for d_type in ["std_anomalies"]: print(d_type) file = "../data/" + d_type + "_" + loc + ".nc" d = data(file) d_train, d_test, d_val = d.get_data() if loc == "EPO": input_dim = (40, 60) else: input_dim = (20, 120) n_features = 3 from models.GAN import GAN l = GAN(input_dim, n_features, batch_norm=True, Dropout=0.1, latent_dim=80, location=loc) losses = l.train(d_train, d_test, num_epochs=200, lr=1e-3) l.save_weights('../models/saved_models/' + loc + '/' + data_type + '/GAN/gan') tf.keras.backend.clear_session()
plt.imshow(x_train[200, :, :, 0], cmap='gray') # ## architecture # In[ ]: gan = GAN(input_dim=(28, 28, 1), discriminator_conv_filters=[64, 64, 128, 128], discriminator_conv_kernel_size=[5, 5, 5, 5], discriminator_conv_strides=[2, 2, 2, 1], discriminator_batch_norm_momentum=None, discriminator_activation='relu', discriminator_dropout_rate=0.4, discriminator_learning_rate=0.0008, generator_initial_dense_layer_size=(7, 7, 64), generator_upsample=[2, 2, 1, 1], generator_conv_filters=[128, 64, 64, 1], generator_conv_kernel_size=[5, 5, 5, 5], generator_conv_strides=[1, 1, 1, 1], generator_batch_norm_momentum=0.9, generator_activation='relu', generator_dropout_rate=None, generator_learning_rate=0.0004, optimiser='rmsprop', z_dim=100) if mode == 'build': gan.save(RUN_FOLDER) else: gan.load_weights(os.path.join(RUN_FOLDER, 'weights/weights.h5'))
from tensorflow.python.keras.datasets import mnist from models.GAN import GAN import tensorflow as tf import numpy as np (x_train, _), (x_test, _) = mnist.load_data() x_train = x_train.astype('float32') / 255. x_test = x_test.astype('float32') / 255. x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) gan = GAN() gan.build() gan.train(x_train, x_test)
investigateAugmentation(X_under, y_under, mix_X, mix_y, colors, artifact, method) # GAN GAN_epochs = 100 class_size = int( sum(y_under) ) # Sum of all the ones. Since data is balanced, the other class is same size # Existing data for class 0 and 1 (Since not yet shuffled) class0 = X_under[:class_size] class1 = X_under[class_size:] # GAN-augmented data, generated from existing data of each class. GAN_class0 = GAN(class0, NtoGenerate=int(ratio * class_size), epochs=GAN_epochs) print("GAN class 0 complete") GAN_class1 = GAN(class1, NtoGenerate=int(ratio * class_size), epochs=GAN_epochs) print("GAN class 1 complete") X_GAN = np.concatenate((GAN_class0, GAN_class1)) y_GAN = np.concatenate( (np.zeros(int(ratio * class_size)), np.ones(int(ratio * class_size)))) X_with_GAN = np.concatenate((X_under, GAN_class0, GAN_class1)) y_with_GAN = np.concatenate((y_under, np.zeros(int(ratio * class_size)), np.ones(int(ratio * class_size))))
def main(inargs): os.environ["CUDA_VISIBLE_DEVICES"] = str(inargs.GPU) limit_mem() # Create GAN object gan = GAN(exp_id=inargs.exp_id, wasserstein=inargs.wasserstein, verbose=inargs.verbose) # Load dataset gan.load_data( inargs.dataset, normalize=inargs.normalize, halve_radar=inargs.halve_radar, data_dir=inargs.data_dir, ) # Define networks if inargs.load_model: s = gan.model_dir + gan.exp_id + '_' gan.G = load_model(s + 'G.h5') gan.D = load_model(s + 'D.h5') train_fn = './saved_models/%s_history.pkl' % gan.exp_id with open(train_fn, 'rb') as f: gan.train_history = pickle.load(f) gan.epoch_counter = len(gan.train_history['train_discriminator_loss']) else: gan.create_generator( filters=inargs.G_filters, first_conv_size=inargs.G_first_conv_size, activation=inargs.G_activation, bn=inargs.G_bn, dr=inargs.G_dr, final_activation=inargs.G_final_activation, final_kernel_size=inargs.G_final_kernel_size, ) gan.create_discriminator( filters=inargs.D_filters, strides=inargs.D_strides, activation=inargs.D_activation, bn=inargs.D_bn, dr=inargs.D_dr, ) gan.G.summary() gan.D.summary() # Compile gan.compile() # Train gan.train( inargs.epochs, inargs.bs, inargs.train_D_separately, inargs.noise_shape, ) # Save model gan.save_models()
def main(): parser = argparse.ArgumentParser() parser.add_argument("--model", choices=["GAN", "WGAN"], required=True, help="GAN | WGAN") parser.add_argument("--dataset", choices=["MNIST", "CIFAR10"], required=True, help="MNIST | CIFAR10") parser.add_argument("--output", default=os.getcwd(), type=str, help="Directory to save the output (if any).") parser.add_argument("--batch_size", default=128, type=int, help="Size of the batch used in training.") parser.add_argument("--noise_dim", default=128, type=int, help="Dimension of the latent noise.") parser.add_argument("--total_epoch", default=100, type=int, help="Number of training epochs.") parser.add_argument("--critic_step", default=1, type=int, help="The number of steps to apply to the discriminator.") parser.add_argument("--visualize", default=True, type=bool, help="Logical indicating whether to visualize the training process and the resulting models.") parser.add_argument("--grad_penalty", default=10.0, type=float, help="Penalty controlling the strength of the gradient regularization in WGAN-LP.") parser.add_argument("--perturb_factor", default=0.5, type=float, help="Factor controlling the standard deviation of perturbation " "for generating samples to compute the gradient penalty in WGAN-LP.") # setup the discriminator optimizer parser.add_argument("--learning_rate_d", default=1e-4, type=float, help="The learning rates of ADAM (discriminator).") parser.add_argument("--beta_1_d", default=0.2, type=float, help="The exponential decay rates for the 1st moment estimates in ADAM (discriminator).") parser.add_argument("--beta_2_d", default=0.999, type=float, help="The exponential decay rates for the 2nd moment estimates in ADAM (discriminator).") parser.add_argument("--epsilon_d", default=1e-7, type=float, help="Small constants for numerical stability of ADAM (discriminator).") parser.add_argument("--amsgrad_d", default=False, type=bool, help="Logical indicating whether to use the AMSGrad variant of ADAM (discriminator).") # setup the generator optimizer parser.add_argument("--learning_rate_g", default=1e-4, type=float, help="The learning rates of ADAM (generator).") parser.add_argument("--beta_1_g", default=0.5, type=float, help="The exponential decay rates for the 1st moment estimates in ADAM (generator).") parser.add_argument("--beta_2_g", default=0.999, type=float, help="The exponential decay rates for the 2nd moment estimates in ADAM (generator).") parser.add_argument("--epsilon_g", default=1e-7, type=float, help="Small constants for numerical stability of ADAM (generator).") parser.add_argument("--amsgrad_g", default=False, type=bool, help="Logical indicating whether to use the AMSGrad variant of ADAM (generator).") model_param = parser.parse_args() if not os.path.exists(model_param.output) or not os.path.isdir(model_param.output): raise OSError("Output path does not exist or is not a directory.") model_param.output = os.path.normpath(model_param.output) if model_param.model == "GAN": model = GAN(vars(model_param)) else: model = WGAN(vars(model_param)) model.train() # randomly generate 100 samples if model_param.visualize: vis_seed = tf.random.uniform([100, model.noise_dim]) vis_gen = model.G(vis_seed, training=False) if model_param.dataset == "MNIST": plt.figure(figsize=(3.45, 3.45)) else: plt.figure(figsize=(3.85, 3.85)) for i in range(vis_gen.shape[0]): x_pos = i % 10 y_pos = int(i / 10) if model_param.dataset == "MNIST": plt.figimage(vis_gen[i, :, :, 0] * 127.5 + 127.5, 10 + x_pos * (28 + 5), 10 + y_pos * (28 + 5), cmap='gray') else: plt.figimage((vis_gen[i, :, :] + 1) / 2, 10 + x_pos * (32 + 5), 10 + y_pos * (32 + 5)) plt.axis('off') plt.savefig(os.path.join(model_param.output, "{}_{}_Example.png".format(model_param.model, model_param.dataset))) # plot median value of the objective functions plt.figure() plt.title("Objective Functions of {} (Dataset: {})".format(model_param.model, model_param.dataset)) plt.xlabel("Epoch") plt.ylabel("Median Value") plt.plot(range(1, 1 + model_param.total_epoch), np.median(model.d_obj, axis=[-0, -1])) plt.plot(range(1, 1 + model_param.total_epoch), np.median(model.g_obj, axis=[-0])) plt.legend(['Discriminator', 'Generator']) plt.savefig(os.path.join(model_param.output, "{}_{}_Objective.png".format(model_param.model, model_param.dataset)))
type=str, default='save', help='Directroy name to save the mdoel') parser.add_argument('--LR_G', type=float, default=0.0002, help='Learning rate for generator optimizer') parser.add_argument('--LR_D', type=float, default=0.0002, help='Learning rate for discriminator optimizer') parser.add_argument('--beta1', type=float, default=0.5, help='Adam beta1') parser.add_argument('--beta2', type=float, default=0.999, help='Adam beta2') parser.add_argument('--CUDA', type=bool, default=False, help='Use GPU') args = parser.parse_args() if args.model_name == 'GAN': model = GAN(args) if args.model_name == 'LSGAN': model = LSGAN(args) if args.model_name == 'WGAN': model = WGAN(args) if args.model_name == 'WGAN-GP': model = WGAN_GP(args) print('Training Starts') model.train() print('Training Finish')