def __init__( self, policy, env, params, test_env=None): """Initializing the training instance.""" self._params = params self._set_from_params() self._policy = policy self._env = env self._test_env = self._env if test_env is None else test_env args = self._get_args_from_params() # Convolutional Autoencoder: self._CAE = CAE(pooling=self._params["cae"]["pooling"], latent_dim=self._params["cae"]["latent_dim"], input_shape=self._env.workspace.shape, conv_filters=self._params["cae"]["conv_filters"]) self._CAE.build(input_shape=(1, self._env.workspace.shape[0], self._env.workspace.shape[1], 1)) self._CAE.load_weights(filepath=self._params["cae"]["weights_path"]) for layer, _ in self._CAE._get_trainable_state().items(): layer.trainable = False #Initialize array for trajectory storage self.trajectory=[] # Initialize workspace relabeler: self._relabeler = PointrobotRelabeler( ws_shape=(self._env.grid_size, self._env.grid_size), mode=params["trainer"]["relabeling_mode"], remove_zigzaging=params["trainer"]["remove_zigzaging"] ) # prepare log directory self._output_dir = prepare_output_dir( args=args, user_specified_dir=self._logdir, suffix="{}_{}".format(self._policy.policy_name, params["trainer"]["dir_suffix"])) self.logger = initialize_logger( logging_level=logging.getLevelName(params["trainer"]["logging_level"]), output_dir=self._output_dir) if self._save_test_path_sep: sep_logdirs = ['successful_trajs', 'unsuccessful_trajs', 'unfinished_trajs'] for logdir in sep_logdirs: if not os.path.exists(os.path.join(self._logdir, logdir)): os.makedirs(os.path.join(self._logdir, logdir)) if params["trainer"]["mode"] == "evaluate": assert glob.glob(os.path.join(params["trainer"]["model_dir"], '*')) self._set_check_point(params["trainer"]["model_dir"]) # prepare TensorBoard output self.writer = tf.summary.create_file_writer(self._output_dir) self.writer.set_as_default() # relabeling visualization: self._relabel_fig = plt.figure(2)
def test_weight_loading(): model = CAE(pooling='max', latent_dim=16, input_shape=(32, 32), conv_filters=[4, 8, 16]) model.build(input_shape=(1, 32, 32, 1)) model.load_weights(filepath='../models/cae/model_num_5_size_8.h5') for k, _ in model._get_trainable_state().items(): k.trainable = False
def test_autoencoder_training(): parser = CAEtrainer.get_arguments() args = parser.parse_args() args.num_workspaces = 10 args.epochs = 10 args.batch_size = 2 if os.listdir(args.workspace_dir) == 0: args.gen_workspace = True input_shape = (args.grid_size, args.grid_size) model = CAE(args.pooling, args.latent_dim, input_shape, args.conv_filters) optimizer = opt.Adam(learning_rate=args.learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-7) print('optimizer: {}'.format(optimizer)) # loss function. Calculating the positive weights for it: mean_obj_num = (args.num_obj_max + 1) / 2 ratio = args.grid_size**2 / (mean_obj_num * (args.obj_size_avg**2)) beta = ratio loss_func = weighted_cross_entropy(beta=beta) print('Loss function: WCE with beta: {}'.format(beta)) trainer = CAEtrainer(CAE=model, optimizer=optimizer, loss_func=loss_func, args=args) trainer() # Plot results on an unseen workspace: # fig = plt.figure(num=1, figsize=(10, 5)) plt.plot(trainer._train_losses) plt.plot(trainer._val_losses) # check out the model: path = os.path.join('../workspaces/', ('ws_' + str(args.num_workspaces - 1) + '.csv')) x = np.expand_dims(np.loadtxt(path), axis=2).astype('float32') x = np.expand_dims(x, axis=0) x = tf.convert_to_tensor(x) x_hat = tf.cast(trainer._CAE(x) >= 0.5, tf.float32) fig2 = visualize_workspace(x.numpy()[0, :, :, 0], fignum=2) fig3 = visualize_workspace(x_hat.numpy()[0, :, :, 0], fignum=3) plt.show()
def test_cae_initialization(): pooling = 'max' latent_dim = 16 input_shape = (32, 32) conv_filters = [4, 8, 16] model = CAE(pooling, latent_dim, input_shape, conv_filters) x = tf.random.uniform([1, 32, 32, 1]) x_hat = model(x) assert x_hat.shape == (1, 32, 32, 1), "output shape is not (1, 32, 32, 1)" workspace = np.random.uniform(size=(32, 32)) y = model.evaluate(workspace) assert isinstance(y, np.ndarray), "Type of latent output is not np.ndarray" assert y.shape == (16, ), "latent output shape is not (16,)"
def setUp(self): """setup""" self.params = load_params('params/test_params.json') self.env = gym.make(self.params["env"]["name"], params=self.params) self.test_env = gym.make(self.params["env"]["name"], params=self.params) self.policy = DDPG(env=self.env, params=self.params) self.cae = CAE(pooling='max', latent_dim=16, input_shape=(32, 32), conv_filters=[4, 8, 16]) self.cae.build(input_shape=(1, 32, 32, 1)) self.cae.load_weights(filepath='../models/cae/model_num_5_size_8.h5')
import numpy as np import os import matplotlib.pyplot as plt from tensorflow.data import Dataset from tensorflow.keras.losses import BinaryCrossentropy from hwr.cae.cae import CAE from hwr.cae.cae_trainer import CAEtrainer, weighted_cross_entropy from hwr.random_workspace import visualize_workspace """Train a Convolutional Autoencoder.""" parser = CAEtrainer.get_arguments() args = parser.parse_args() input_shape = (args.grid_size, args.grid_size) model = CAE(args.pooling, args.latent_dim, input_shape, args.conv_filters) optimizer = opt.Adam(learning_rate=args.learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-7) print('optimizer: {}'.format(optimizer)) # loss function. Calculating the positive weights for it: mean_obj_num = (args.num_obj_max + 1) / 2 ratio = args.grid_size**2 / (mean_obj_num * (args.obj_size_avg**2)) beta = ratio loss_func = weighted_cross_entropy(beta=beta) print('Loss function: WCE with beta: {}'.format(beta)) trainer = CAEtrainer(CAE=model, optimizer=optimizer,
import tensorflow as tf import numpy as np import os import matplotlib.pyplot as plt from hwr.cae.cae import CAE from hwr.random_workspace import visualize_workspace """Visualize the output of the trained Convolutional Autoencoder""" pooling = 'max' latent_dim = 16 input_shape = (32, 32) conv_filters = [4, 8, 16] model = CAE( pooling=pooling, latent_dim=latent_dim, input_shape=input_shape, conv_filters=conv_filters, ) model.build(input_shape=(1, 32, 32, 1)) model.load_weights(filepath='../models/cae/model_num_5_size_8.h5') # Plot results on an unseen workspace: # path = os.path.join('../workspaces/', ('ws_' + str(9500) + '.csv')) x = np.expand_dims(np.loadtxt(path), axis=2).astype('float32') x = np.expand_dims(x, axis=0) x = tf.convert_to_tensor(x) x_hat = tf.cast(model(x) >= 0.5, tf.float32) fig2 = visualize_workspace(x.numpy()[0, :, :, 0], fignum=2) fig3 = visualize_workspace(x_hat.numpy()[0, :, :, 0], fignum=3)