Example #1
0
    def __init__(self, args):
        self.log_path = args.log_path
        self.device = torch.device("cuda:0" if args.cuda else "cpu")
        self.img_size = args.img_size
        self.sample_num = args.sample_num
        self.transform = transforms.Compose([
            transforms.ToPILImage(),
            transforms.Resize([64, 64], 1),
            transforms.ToTensor(),
            transforms.Normalize([.5], [.5])
        ])
        self.pil_transform = transforms.ToPILImage(mode="RGB")

        self.norm_scale = np.loadtxt(os.path.join(args.config_path,
                                                  "norm_scale.txt"),
                                     dtype=np.float32,
                                     delimiter=",")[None]
        self.norm_min = np.loadtxt(os.path.join(args.config_path,
                                                "norm_min.txt"),
                                   dtype=np.float32,
                                   delimiter=",")[None]
        self.pb_list = torch.from_numpy(
            np.loadtxt(os.path.join(args.config_path, "pb_list.txt"),
                       dtype=np.float32,
                       delimiter=","))

        self.kmeans = KMeans(n_clusters=2)
        self.kmeans.fit(self.pb_list)

        print("=" * 5, "Init LSTMPB", "=" * 5)
        self.rnn = LSTMPB(args, pb_unit=self.pb_list[5][None])
        pt_file = load_model(args.model_load_path, "*/*LSTMPB*.pt")
        self.rnn.load_state_dict(torch.load(pt_file))

        print("=" * 5, "Init VAE", "=" * 5)
        self.vae = VAE(img_size=args.img_size, z_dim=args.vae_z_dims)
        pt_file = load_model(args.model_load_path, "*/VAE*.pt")
        self.vae.load_state_dict(torch.load(pt_file))
        self.vae.eval()

        print("=" * 5, "Init CVAE", "=" * 5)
        self.cvae = CVAE(img_size=args.img_size, z_dim=args.cvae_z_dims)
        pt_file = load_model(args.model_load_path, "*/*CVAE*.pt")
        self.cvae.load_state_dict(torch.load(pt_file))
        self.cvae.eval()

        self.norm_mode = {
            "joint": [0, 1, 2, 3, 4],
            "visual": [5, 6, 7, 8, 9, 10, 11]
        }
        self.norm_mode[
            "all"] = self.norm_mode["joint"] + self.norm_mode["visual"]

        self.global_step = 0
        self.his_log = HistoryWindow(maxlen=args.window_size)

        #visualize current goal
        _, goal = self.vae.decoder(self.denorm(self.goal, "visual"))
        goal = ((goal[0] * .5 + .5) * 255).to(torch.int8)
        self.goal_img = self.pil_transform(goal)
Example #2
0
def train_conditional(data, optimizer):
    (X_train, y_train), (X_test, y_test) = data
    lb = LabelBinarizer()
    y_train = lb.fit_transform(y_train)
    y_test = lb.transform(y_test)
    fname = os.path.join(data_dir, 'cvae_train.csv')
    logger = tf.keras.callbacks.CSVLogger(filename=fname)
    encoder, decoder, cvae = CVAE(input_dim=X_train.shape[-1],
                                  latent_dim=latent_dim,
                                  aux_dim=y_train.shape[-1],
                                  beta=beta,
                                  output_activation='sigmoid').build()
    print(cvae.summary())
    cvae.compile(optimizer, loss=None)
    cvae.fit(x=[X_train, y_train],
             y=None,
             validation_data=([X_test, y_test], None),
             epochs=epochs,
             batch_size=bs,
             shuffle=True,
             verbose=2,
             callbacks=[logger])
    print('\nfinished training, saving models...')
    cvae.save(os.path.join(model_dir, 'cvae.h5'))
    encoder.save(os.path.join(model_dir, 'conditional_encoder.h5'))
    decoder.save(os.path.join(model_dir, 'conditional_decoder.h5'))
Example #3
0
def main(args):
    transform = transforms.Compose([
        transforms.Resize([64, 64], 1),
        transforms.ToTensor(),
        transforms.Normalize([.5], [.5])
    ])
    dataset = datasets.ImageFolder(root=args.data_path, transform=transform)

    data = []
    for i, j in dataset:
        data.append(i)
    data = torch.stack(data)

    cvae = CVAE(img_size=data.shape[1:], z_dim=args.z_dim)
    cvae.eval()
    pt_files = glob.glob(os.path.join(args.model_load_path, "*.pt"))
    pt_files.sort()
    #data_1 = data[0][None, :, :, :].repeat([20, 1, 1, 1])
    #data_2 = data[1][None, :, :, :].repeat([20, 1, 1, 1])

    for i in range(len(pt_files)):
        print(pt_files[i])
        cvae.load_state_dict(torch.load(pt_files[i]))

        #z_data = [torch.randn(data.shape[0], args.z_dim), data]
        z_data = [
            torch.randn(32, args.z_dim), data[None, 0].repeat([32, 1, 1, 1])
        ]
        _, rec_img = cvae.decoder(*z_data)
        grid_img = make_result_img(
            [data[None, 0].repeat([32, 1, 1, 1]), rec_img],
            normalize=True,
            range=(-1., 1.))
        utils.save_image(
            grid_img, "{}CVAE_gen_result_{:0>2d}.png".format(args.log_path, i))
Example #4
0
def model(tree_def, cond_tree_def, activation):
    return CVAE(
        FLAGS.embedding_size,
        det_encoder=Encoder(
            tree_def=tree_def,
            embedding_size=FLAGS.embedding_size,
            cut_arity=FLAGS.cut_arity,
            max_arity=FLAGS.max_arity,
            variable_arity_strategy=FLAGS.enc_variable_arity_strategy,
            cellsbuilder=EncoderCellsBuilder(
                EncoderCellsBuilder.simple_cell_builder(
                    hidden_coef=FLAGS.hidden_cell_coef,
                    activation=activation,
                    gate=FLAGS.encoder_gate),
                EncoderCellsBuilder.simple_dense_embedder_builder(
                    activation=activation),
                EncoderCellsBuilder.simple_categorical_merger_builder(
                    hidden_coef=FLAGS.hidden_cell_coef, activation=activation),
            ),
            name='encoder'),
        det_decoder=Decoder(
            tree_def=tree_def,
            embedding_size=FLAGS.embedding_size,
            max_node_count=FLAGS.max_node_count,
            max_depth=FLAGS.max_depth,
            max_arity=FLAGS.max_arity,
            cut_arity=FLAGS.cut_arity,
            cellbuilder=DecoderCellsBuilder(
                DecoderCellsBuilder.simple_distrib_cell_builder(
                    FLAGS.hidden_cell_coef, activation=activation),
                DecoderCellsBuilder.simple_1ofk_value_inflater_builder(
                    0.5, activation=tf.tanh),
                DecoderCellsBuilder.simple_node_inflater_builder(
                    FLAGS.hidden_cell_coef,
                    activation=activation,
                    gate=FLAGS.decoder_gate)),
            variable_arity_strategy=FLAGS.dec_variable_arity_strategy,
            attention=False),
        cond_encoder=Encoder(
            tree_def=cond_tree_def,
            embedding_size=FLAGS.embedding_size,
            cut_arity=FLAGS.cut_arity,
            cellsbuilder=EncoderCellsBuilder(
                EncoderCellsBuilder.simple_cell_builder(
                    hidden_coef=FLAGS.hidden_cell_coef,
                    activation=activation,
                    gate=FLAGS.encoder_gate),
                EncoderCellsBuilder.simple_categorical_embedder_builder(
                    FLAGS.hidden_cell_coef, activation=activation),
                EncoderCellsBuilder.simple_categorical_merger_builder(
                    hidden_coef=FLAGS.hidden_cell_coef,
                    activation=activation)),
            max_arity=FLAGS.max_arity,
            name="condition_encoder",
            variable_arity_strategy=FLAGS.enc_variable_arity_strategy))
Example #5
0
    def __init__(self, env, silent=False):
        super().__init__(env)

        from vae import CVAE
        self.vae = CVAE()
        self.vae.set_weights(np.load("vae_weights.npy", allow_pickle=True))

        self.observation_space = Box(low=float("-inf"),
                                     high=float("inf"),
                                     shape=(40, ))
        self.silent = silent
Example #6
0
def main(args):
    transform = transforms.Compose([
        transforms.Resize([64, 64], 1),
        transforms.ToTensor(),
        transforms.Normalize([.5], [.5])
    ])
    dataset = datasets.ImageFolder(root=args.data_path, transform=transform)

    data = []
    for i, j in dataset:
        data.append(i)
    data = torch.stack(data)

    cvae = CVAE(img_size=data.shape[1:], z_dim=args.z_dim)
    pt_file = load_model(args.model_load_path, "*10.pt")
    cvae.load_state_dict(torch.load(pt_file))
    cvae.eval()

    if args.decode:
        #z_data = [torch.randn(data.shape[0], args.z_dim), data]
        #_, rec_img = cvae.decoder(*z_data)
        #grid_img = make_result_img([data, rec_img], normalize=True, range=(-1., 1.))
        #utils.save_image(grid_img, "{}CVAE_gen_result.png".format(args.log_path))

        cond_img = data[None, 0].repeat([32, 1, 1, 1])
        z_data = [torch.randn(cond_img.shape[0], args.z_dim), cond_img]
        _, rec_img = cvae.decoder(*z_data)
        #grid_img = make_result_img([rec_img], normalize=True, range=(-1., 1.))
        #utils.save_image(grid_img, "{}CVAE_gen_result_same_cond.png".format(args.log_path))
        for i in range(rec_img.shape[0]):
            utils.save_image(rec_img[i],
                             "{}goal{:0>6d}.png".format(args.log_path, i),
                             normalize=True,
                             range=(-1., 1.))

    if args.gen_seq:
        for i, d in enumerate(data):
            cond_img = data[None, i]
            z_data = [torch.randn(1, args.z_dim), cond_img]
            _, rec_img = cvae.decoder(*z_data)
            grid_img = make_result_img([cond_img, rec_img],
                                       normalize=True,
                                       range=(-1., 1.))
            utils.save_image(
                grid_img, "{}res_state-goal/CVAE_gen_{:0>6d}.png".format(
                    args.log_path, i))
            utils.save_image(rec_img,
                             "{}res_goal/CVAE_gen_{:0>6d}.png".format(
                                 args.log_path, i),
                             normalize=True,
                             range=(-1., 1.))
Example #7
0
        test_size=validation_set_percent,
        stratify=X_autoencoder_tumor_type,
        random_state=42)

    # Order the features correctly before training
    X_train = X_train.reindex(sorted(X_train.columns), axis="columns")
    X_val = X_val.reindex(sorted(X_val.columns), axis="columns")

    #Train the Model
    cvae = CVAE(original_dim=X_autoencoder_train.shape[1],
                intermediate_dim=hidden_dim,
                latent_dim=latent_dim,
                cond_dim=32,
                epochs=epochs,
                batch_size=batch_size,
                learning_rate=learning_rate,
                dropout_rate_input=dropout_input,
                dropout_rate_hidden=dropout_hidden,
                dropout_decoder=dropout_decoder,
                freeze_weights=freeze_weights,
                classifier_use_z=classifier_use_z,
                rec_loss=reconstruction_loss)

    cvae.initialize_model()
    cvae.train_vae(
        train_df=X_autoencoder_train,
        train_cond_df=pd.get_dummies(X_autoencoder_tumor_type_train),
        val_df=X_autoencoder_val,
        val_cond_df=pd.get_dummies(X_autoencoder_tumor_type_val))

    # Build and train stacked classifier
Example #8
0
train_unlabeled = pickle.load(open("train_unlabeled.p", "rb"))
train_unlabeled.train_labels = torch.ones([47000])
train_unlabeled.k = 47000
val_data = pickle.load(open("validation.p", "rb"))
#test_data = pickle.load(open("test.p","rb"))

train_loader_unlabeled = torch.utils.data.DataLoader(train_unlabeled,
                                                     batch_size=64,
                                                     shuffle=True)
train_loader_labeled = torch.utils.data.DataLoader(train_labeled,
                                                   batch_size=64,
                                                   shuffle=True)
val_loader = torch.utils.data.DataLoader(val_data, batch_size=64, shuffle=True)
#test_loader = torch.utils.data.DataLoader(test_data, batch_size=64, shuffle=True)

vae = CVAE()

model = CNN()

#----------------------------------------
#Unsupevised VAE training
#----------------------------------------

opt = optim.Adam(vae.parameters(), lr=0.001)
mse = nn.MSELoss()
bce = nn.BCELoss()


def test_vae():
    vae.eval()
    test_loss = 0
Example #9
0
    print('[!] Error: Unable to continue - missing file ' + WEIGHTS)
    print(
        '[*] To generate the missing file, please train the model by running vae.py'
    )
    exit()

# Encoding the entire dataset, if not already encoded
if not isfile(ENCODED_DATASET):
    print(
        '[!] Warning: Missing file {}. Generating...'.format(ENCODED_DATASET))
    dataset = glob(PATH)
    if not dataset:
        print('[!] Error: Unable to encode dataset - missing files at ' + PATH)
        exit()
    else:
        net = CVAE()
        net.vae.load_weights(WEIGHTS)
        encode(ENCODED_DATASET, dataset, net)

# Generating the Eigen values and vectors, if they do not already exist.
if not isfile(EIGENVALUES) or not isfile(EIGENVECTORS):
    print('[!] Warning: Missing files {},{}. Generating...'.format(
        EIGENVALUES, EIGENVECTORS))
    pca(ENCODED_DATASET)

# Searching for the optional cascade files
cascades = [EYE_CASCADE, MOUTH_CASCADE, FACE_CASCADE]
DEMO_FLAG = True
if not all([isfile(x) for x in cascades]):
    print('[!] Warning: Missing the following files:')
    print(*cascades, sep=', ')
Example #10
0
import tensorflow as tf
import numpy as np

from vae import CVAE
from data.circle_generator import gen_circles

model = CVAE(2)
model.load_weights('../saved_models/vae/vae_weights')

numTests = 10
size = 28

dataset = tf.data.Dataset.from_generator(
    gen_circles,
    args=[3, size],
    output_types=(tf.float32),
    output_shapes=((size, size, 1))
)

for img in dataset:
    
Example #11
0

@tf.function
def compute_apply_gradients(model, x, optimizer):
    with tf.GradientTape() as tape:
        loss = compute_loss(model, x)
    gradients = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(gradients, model.trainable_variables))


epochs = 75
if challenge == 'position':
    latent_dim = 2
elif challenge == 'with_size':
    latent_dim = 3
model = CVAE(size, latent_dim)

for epoch in range(1, epochs + 1):
    start_time = time.time()
    for train_x in train_dataset:
        compute_apply_gradients(model, train_x, optimizer)
    end_time = time.time()

    if epoch % 1 == 0:
        loss = tf.keras.metrics.Mean()
        for test_x in test_dataset:
            loss(compute_loss(model, test_x))
        elbo = -loss.result()
        display.clear_output(wait=False)
        print('Epoch: {}, Test set ELBO: {}, '
              'time elapse for current epoch {}'.format(