def __init__(self, image_size, num_epoch, batch_size, learning_rate, num_digit, num_classes, train_file, test_file, filewriter_path, checkpoint_path, relu_leakiness=0.1, is_restore=True, restore_path=None, device_id='2'): self.image_size = image_size self.num_epoch = num_epoch self.batch_size = batch_size self.train_file = train_file self.val_file = test_file self.train_batches_per_epoch = int(170000 / self.batch_size) self.valid_batches_per_epoch = int(30000 / self.batch_size) self.learning_rate = learning_rate self.num_digit = num_digit self.num_classes = num_classes self.display_step = 20 self.filewriter_path = filewriter_path deldirs(self.filewriter_path) mkdirs(self.filewriter_path) self.checkpoint_path = checkpoint_path print(self.checkpoint_path) mkdirs(self.checkpoint_path) self.relu_leakiness = relu_leakiness if is_restore: if restore_path is None: print("restore path is none") sys.exit() ckpt = tf.train.get_checkpoint_state(restore_path) self.restore_checkpoint = ckpt.model_checkpoint_path else: self.restore_checkpoint = None os.environ['CUDA_VISIBLE_DEVICES'] = device_id self.init_model()
def __init__(self, image_size, num_epochs, batch_sizes, learning_rate, train_file, filewriter_path, checkpoint_path, is_restore, restore_path, device_id): self.image_size = image_size self.num_epoch = num_epochs self.batch_size = batch_sizes self.learing_rate = learning_rate self.train_file = train_file self.filewriter_path = filewriter_path utils.deldirs(self.filewriter_path) utils.mkdirs(self.filewriter_path) self.checkpoint_path = checkpoint_path if not os.path.exists(filewriter_path): os.makedirs(filewriter_path) if not os.path.exists(checkpoint_path): os.makedirs(checkpoint_path) self.display_step = 20 if is_restore and os.path.exists(restore_path): ckpt = tf.train.get_checkpoint_state(restore_path) self.restore_checkpoint = ckpt.model_checkpoint_path else: self.restore_checkpoint = None os.environ['CUDA_VISIBLE_DEVICES'] = device_id self.init_model()
def __init__(self, image_size, batch_size, num_digit, num_classes, test_file, checkpoint_path, relu_leakiness=0, is_restore=True, device_id='2'): self.image_size = image_size self.batch_size = batch_size self.num_digit = num_digit self.num_classes = num_classes self.display_step = 20 self.train_file = test_file self.checkpoint_path = checkpoint_path mkdirs(self.checkpoint_path) self.relu_leakiness = relu_leakiness if is_restore: ckpt = tf.train.get_checkpoint_state(self.checkpoint_path) self.restore_checkpoint = ckpt.model_checkpoint_path else: self.restore_checkpoint = '' os.environ['CUDA_VISIBLE_DEVICES'] = device_id os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' self.init_model()
running_loss = coach.train(trainloader, epoch=epoch) writter.add_scalar("Loss", running_loss, epoch) evaluate(valider=valider, trainloader=trainloader, testloader=testloader, acc_logger=acc_logger, rob_logger=rob_logger, writter=writter, epoch=opts.epochs) acc_logger.plotter.plot() rob_logger.plotter.plot() acc_logger.plotter.save(writter) rob_logger.plotter.save(writter) if __name__ == "__main__": from torch.utils.tensorboard import SummaryWriter from src.utils import mkdirs, readme cfg = load_cfg() mkdirs(cfg.info_path, cfg.log_path) readme(cfg.info_path, opts) readme(cfg.log_path, opts, mode="a") writter = SummaryWriter(log_dir=cfg.log_path, filename_suffix=METHOD) main(**cfg) cfg['coach'].save(cfg.info_path) writter.close()
# { # "Linf": running_distance_linf[epsilon], # "L2": running_distance_l2[epsilon], # }, # epsilon # ) running_accuracy = list(map(lambda x: 1. - x, running_success)) running_accuracy = ', '.join([f"{acc:.3%}" for acc in running_accuracy]) running_distance_linf = ', '.join( [f"{dis_linf:.5f}" for dis_linf in running_distance_linf]) running_distance_l2 = ', '.join( [f"{dis_l2:.5f}" for dis_l2 in running_distance_l2]) print(f"Accuracy: {running_accuracy}") print(f"Distance-Linf: {running_distance_linf}") print(f"Distance-L2: {running_distance_l2}") if __name__ == "__main__": from torch.utils.tensorboard import SummaryWriter from src.utils import mkdirs, readme cfg, log_path = load_cfg() mkdirs(log_path) readme(log_path, opts, mode="a") writter = SummaryWriter(log_dir=log_path, filename_suffix=METHOD) main(**cfg) writter.close()
""" import os, csv, datetime, random import numpy as np import pandas as pd import tensorflow as tf from src.Environment import Environment from src.Agents import DQNAgent from src.utils import mkdirs, save_plot, save_animation tf.random.set_seed(1) gpu_devices = tf.config.experimental.list_physical_devices('GPU') for device in gpu_devices: tf.config.experimental.set_memory_growth(device, True) random.seed(a=0) np.random.seed(0) output_dir = mkdirs(os.path.join(os.path.dirname(__file__), "output")) models_dir = mkdirs(os.path.join(output_dir, "models")) animations_dir = mkdirs(os.path.join(output_dir, "animations")) log_file = os.path.join(output_dir, "log.csv") env = Environment(n_good=0, n_bad=110) agent = DQNAgent(env, n_sectors=4, sector_radius=1.0) save_models = False save_animations = True n_episodes = 1000 iter_max = 2000 n_reward_max = 0 loss = -1 # track loss for episode in range(n_episodes): iter = 0 env.reset() # reset environment