Exemple #1
0
 def test_generate_report(self):
     num_clickables = {
         'unexamined': 5,
         'true':  10,
         'false': 30
     }
     form1 = FormField('form1')
     form1.add_input(InputField('username', '//*[@id="username"]', 'castman'))
     form1.add_input(InputField('password', '', 'p@ssw0rd'))
     form_list = [{
         'state': 1,
         'form': form1,
         'execution_seq': [Clickable('exe1', '//html/body/button[1]'), Clickable('exe2', '//html/body/button[1]')],
         'clickable': [Clickable('btn1', '//html/body/button[1]'), Clickable('btn2', '//html/body/button[1]')]
     }]
     inv_violation = [{
         'state': 2,
         'name': '{"name": "file-not-found"}',
         'sequence': [Clickable('inv-btn1', '//html/body/button[1]'), Clickable('inv-btn2', '//html/body/button[1]')]
     }]
     Visualizer.generate_report(
         'web',
         'trace/example-app-4-webide',
         'automata.json',
         3, num_clickables, form_list, inv_violation, 9.987
     )
Exemple #2
0
 def __init__(self, args):
     Visualizer.__init__(self, args)
     self.safe_width = int(self.width * (1 - APPEND_MARGIN - PREPEND_MARGIN))
     self.prepend_margin_width = int(self.width * PREPEND_MARGIN)
     self.files = {}
     self._smoothed_min_filenum = Smoother()
     self._smoothed_max_filenum = Smoother()
Exemple #3
0
 def __init__(self, args):
     Visualizer.__init__(self, args)
     self.inner_margin_width = int(self.width * INNER_MARGIN)
     self.safe_width = self.width - self.inner_margin_width * 2
     self.files = {}
     self._smoothed_min_filenum = Smoother()
     self._smoothed_max_filenum = Smoother()
def debugTestMain(folderpath, dirname):
    logging.info(" setting config...")
    config = SeleniumConfiguration(Browser.FireFox, "http://140.112.42.145:2000/demo/nothing/main.html")
    config.set_max_depth(1)
    config.set_max_length(5)
    config.set_trace_amount(1)
    config.set_max_states(100)
    config.set_folderpath(folderpath)
    config.set_dirname(dirname)
    config.set_automata_fname('automata.json')
    config.set_traces_fname('traces.json')
    config.set_frame_tags(['iframe'])
    config.set_dom_inside_iframe(True)
    config.set_simple_clickable_tags()
    config.set_simple_inputs_tags()
    config.set_simple_normalizers()

    logging.info(" setting executor...")
    executor = SeleniumExecutor(config.get_browserID(), config.get_url())

    logging.info(" setting crawler...")
    automata = Automata(config)
    databank = InlineDataBank("140.112.42.145:2000", "jeff", "zj4bj3jo37788", "test")
    algorithm = MonkeyCrawler() #DFScrawler()
    crawler = SeleniumCrawler(config, executor, automata, databank, algorithm)

    logging.info(" crawler start run...")
    crawler.run_algorithm()

    logging.info(" end! save automata...")
    algorithm.save_traces()
    automata.save_automata(config.get_automata_fname())
    Visualizer.generate_html('web', os.path.join(config.get_path('root'), config.get_automata_fname()))
    config.save_config('config.json')
def SeleniumMain(web_submit_id, folderpath=None, dirname=None):
    logging.info(" connect to mysql")
    print("connect to sql")
    databank = MysqlDataBank("localhost", "root", "", "test")
    url, deep, time = databank.get_websubmit(web_submit_id)

    logging.info(" setting config...")
    print(" setting config...")

    config = SeleniumConfiguration(Browser.PhantomJS, url, folderpath, dirname)
    config.set_max_depth(deep)
    config.set_max_time(int(time)*60)
    config.set_simple_clickable_tags()
    config.set_simple_inputs_tags()
    config.set_simple_normalizers()
    config.set_frame_tags(['iframe'])
    
    logging.info(" setting executor...")
    executor = SeleniumExecutor(config.get_browserID(), config.get_url())
    
    logging.info(" setting crawler...")
    automata = Automata()
    crawler = SeleniumCrawler(config, executor, automata, databank)
    
    logging.info(" crawler start run...")
    automata = crawler.run()
    crawler.close()
    
    logging.info(" end! save automata...")
    automata.save_automata(config)
    automata.save_traces(config)
    Visualizer.generate_html('web', os.path.join(config.get_path('root'), config.get_automata_fname()))
    config.save_config('config.json')
    def run_algorithm(self):
        # repeat for trace_amount times
        for i in range( self.configuration.get_trace_amount() ):

            self.initial()

            while self.action_events:
                #check time
                if (time.time() - self.time_start) > self.configuration.get_max_time():
                    logging.info("|||| TIMO OUT |||| end crawl ")
                    break

                string = ''.join([ str(action['action']['clickable'].get_id())+str(action['depth'])+str(action['state'].get_id()) for action in self.action_events ])
                logging.info(' action_events : '+string )

                state, action, depth = self.get_next_action()
                self.change_state(state, action, depth)
                edge = self.trigger_action(state, action, depth)
                self.update_states(state, edge, action, depth)

            self.close()

            self.algorithm.save_traces()
            self.automata.save_automata(self.configuration.get_automata_fname())
            Visualizer.generate_html('web', os.path.join(self.configuration.get_path('root'), self.configuration.get_automata_fname()))
        
        return self.automata
def main(m=5, r=2, window_size=20, batch_size=2):
    gen = SimpleGenerator(num=m)
    bed = TestBed(m=m, r=r, window_size=window_size, batch_size=batch_size)
    vis = Visualizer()

    for i in xrange(10):
        bed.supply(gen.next())

    for i,y in enumerate(gen):
        if i % window_size == 0:
            # pretrain
            avg_cost = bed.pretrain(10, pretraining_lr=0.1)
            print("   pretrain cost: {}".format(avg_cost))

        # predict
        y_pred = bed.predict()
        print("{}: y={}, y_pred={}".format(i, y, y_pred))
        vis.append(y, y_pred)

        # finetune
        bed.supply(y)
        avg_cost = bed.finetune(10, finetunning_lr=0.1)
        # bed.finetune(100, finetunning_lr=0.01)
        # bed.finetune(100, finetunning_lr=0.001)
        print("   train cost: {}".format(avg_cost))
        time.sleep(.1)
def CBTestMain(folderpath, dirname,web_submit_id):
    logging.info(" Type: Cross Browser Testing")
    logging.info(" connect to mysql")
    print("")
    print("connect to sql")
    databank = MysqlDataBank("localhost", "root", "", "test")
    url, deep, time, b1, b2 = databank.get_websubmit(int(web_submit_id))
    basic_browserID = str(b1)
    other_browserID = str(b2)
    depth = int(deep)

    logging.info(" A new CBT begings...")
    logging.info(" setting config...")
    config = SeleniumConfiguration(int(basic_browserID),int(other_browserID), url)
    # max 3
    config.set_max_depth(int(depth))
    # max 3
    config.set_max_length(int(depth))
    # should be 1
    config.set_trace_amount(1)
    # should be 100 no use?
    config.set_max_states(5)
    config.set_folderpath(folderpath)
    config.set_dirname(dirname)
    config.set_automata_fname('automata.json')
    config.set_traces_fname('traces.json')
    #config.set_frame_tags(['iframe'])
    config.set_dom_inside_iframe(True)
    config.set_simple_clickable_tags()
    config.set_simple_inputs_tags()
    config.set_simple_normalizers()

    logging.info(" setting executor...")
    #nothing here
    executor = CBTExecutor(config.get_browserID(), config.get_url())

    logging.info(" setting crawler...")
    automata = Automata(config)
    #databank = InlineDataBank("140.112.42.145:2000", "jeff", "zj4bj3jo37788", "test")
    databank = InlineDataBank("localhost", "B00901138", "R124249166", "test")
    
    print('start Cross Browser Testing...')
    #acually it's CBT algorithm
    algorithm = CBTCrawler(int(other_browserID),url)

    crawler = SeleniumCrawler(config, executor, automata, databank, algorithm)

    logging.info(" crawler start runing...")
    crawler.run_algorithm()
    
    print(" end! save automata...")
    logging.info(" end! save automata...")
    algorithm.save_traces()
    automata.save_automata(config.get_automata_fname())
    Visualizer.generate_html('web', os.path.join(config.get_path('root'), config.get_automata_fname()))
    config.save_config('config.json')
class PhysPopulation(AnnPopulation):

    """Population class for the physical EvoFab system. Is used to
    wrap up a significant amount of information important for the
    execution of the GA within members
    """

    def __init__(self, random_seed, printer_runtime, size, mutation_rate, mutation_range, crossover_rate, replacement_number, num_input, num_hidden, num_output, serial_port, sensor_serial_port, conveyor_port, z_port, camera, outputfolder, crop=True, is_visual=True, dump_to_files=False):
        super(PhysPopulation, self).__init__(random_seed, printer_runtime, size, mutation_rate, mutation_range, crossover_rate, replacement_number, num_input, num_hidden, num_output, outputfolder, is_visual=is_visual, dump_to_files=dump_to_files)
        self.genotype_factory = PhysGenotypeFactory(self)
        #TODO: should probably test that sensor and controller serial ports are valid
        self.controller = EvoController(serial_port)
        self.sense = EvoArray(sensor_serial_port)
        self.camera = EvoCamera(camera, crop)
        self.conveyor = EvoConveyor(conveyor_port)
        self.visualizer = Visualizer([self.sense.getNext() for x in range(10)])
        self.visualizer.update(self.sense.getNext())
        self.z_axis = EvoZAxis(z_port)
        listener = threading.Thread(target=kbdListener)
        listener.start()
Exemple #10
0
    def setup(self, window_size=20, t_in=2, w=10, h=10, d=1, t_out=1, hidden_layers_sizes=[100], pretrain_step=1):
        self.bed = TestBed(window_size=window_size, t_in=t_in, w=w, h=h, d=d, t_out=t_out, hidden_layers_sizes=hidden_layers_sizes)
        self.gen = SinGenerator(w=w, h=h, d=1)
        # self.gen = RadarGenerator('../data/radar', w=w, h=h, left=0, top=80)
        self.vis = Visualizer(w=w, h=h, t_out=t_out)
        self.pretrain_step = pretrain_step

        # fill the window with data
        for i in xrange(window_size):
            y = self.gen.next()
            self.bed.supply(y)
def main():
	cerebellum.registerMessageHandler(TestHandler())
	
	staticMap = StaticMap()
	cerebellum.registerMessageHandler(staticMap)
	
	logic = SimpleStackLogic(cerebellum,staticMap)
	cerebellum.registerMessageHandler(logic)

	if visualize:
		from visualizer import Visualizer
		import simple_stack_visualizer
		vis = Visualizer(cerebellum, staticMap)
		vis.registerDrawer(simple_stack_visualizer.createDrawer(logic))
		vis.start()
		vis.registerDrawer(cerebellum.insurance.draw)
	
	mainLoop()
	
	if visualize:
	    vis.terminate = True
	    vis.join()
 def __init__(self, random_seed, printer_runtime, size, mutation_rate, mutation_range, crossover_rate, replacement_number, num_input, num_hidden, num_output, serial_port, sensor_serial_port, conveyor_port, z_port, camera, outputfolder, crop=True, is_visual=True, dump_to_files=False):
     super(PhysPopulation, self).__init__(random_seed, printer_runtime, size, mutation_rate, mutation_range, crossover_rate, replacement_number, num_input, num_hidden, num_output, outputfolder, is_visual=is_visual, dump_to_files=dump_to_files)
     self.genotype_factory = PhysGenotypeFactory(self)
     #TODO: should probably test that sensor and controller serial ports are valid
     self.controller = EvoController(serial_port)
     self.sense = EvoArray(sensor_serial_port)
     self.camera = EvoCamera(camera, crop)
     self.conveyor = EvoConveyor(conveyor_port)
     self.visualizer = Visualizer([self.sense.getNext() for x in range(10)])
     self.visualizer.update(self.sense.getNext())
     self.z_axis = EvoZAxis(z_port)
     listener = threading.Thread(target=kbdListener)
     listener.start()
def main():
    # get settings from command line arguments
    settings = CommandLineParser().parse_args()
        
    # create problem
    problem = GrayScottProblem(settings.size, coefficients=settings.coefficients)
    
    # create visualizer
    visualizer = Visualizer(problem.problem_size(), \
                            export=settings.export, \
                            keepalive=settings.keepalive, \
                            show=(not settings.noshow))

    # create step generator
    stop_point_generator = TimeStepper(settings.timesteps, \
                                       settings.outputs, mode='linear')

    # evolution loop
    stop_point = next(stop_point_generator)
    for step in range(settings.timesteps + 1):        
        # print progress message
        if settings.verbose == True:
            progress = 100 * step / settings.timesteps
            print('{:3.0f}% finished'.format(progress), end='\r')

        # trigger visualization
        if step == stop_point:
            visualizer.update(problem.v)
            try:
                stop_point = next(stop_point_generator)
            except StopIteration:
                pass

        # evolve problem
        problem.evolve()
    else:
        if settings.verbose == True:
            print('\nEvolution finished')
        visualizer.close()
def SeleniumMutationTrace(folderpath, dirname, config_fname, traces_fname, trace_id, method_id, modes):
    logging.info(" loading config...")
    config = load_config(config_fname)
    config.set_folderpath(folderpath)
    config.set_dirname(dirname)
    config.set_mutation_trace(traces_fname, trace_id)
    config.set_mutation_method(method_id)
    config.set_mutation_modes(modes)

    logging.info(" setting executor...")
    executor = SeleniumExecutor(config.get_browserID(), config.get_url())

    logging.info(" setting crawler...")
    automata = Automata()
    databank = MysqlDataBank("localhost", "B00901138", "R124249166", "test")
    crawler = SeleniumCrawler(config, executor, automata, databank)

    logging.info(" crawler start run...")
    crawler.run_mutant()

    logging.info(" end! save automata...")
    automata.save_traces(config)
    automata.save_automata(config)    
    Visualizer.generate_html('web', os.path.join(config.get_path('root'), config.get_automata_fname()))
Exemple #15
0
    def __init__(self, vis=False):
        """
        Creates a new Driver.

        Args:
            vis: Boolean. Whether or not to show visualization of the simulation
                runs using matplotlib.

        Returns:
            A new Driver instance.

        """

        self.vis = vis

        # If visualization is selected, show it.
        if self.vis:
            series = ('Population Count',
                      'Adult Count',
                      'Caloric Requirements (Mcal)',
                      'Produced Food (Mcal)',
                      'Air (kg O2)',
                      'Power Consumption (kWh)')
            self.vis = Visualizer(series=series)
Exemple #16
0
import pygame
import sys
import numpy as np
import pickle
import time
from pathplan import PathPlanner
from visualizer import Visualizer
from collisiondetect import CollisionDetector
sys.setrecursionlimit(5000)

# Initial Config
q_init = (100.0, 500.0, 0.0)
q_goal = (700.0, 500.0, -2.15)

# Define and convert obstacles
vizer = Visualizer()
vizer.draw_square(q_init)
vizer.draw_square(q_goal, color=vizer.RED)
obstcls = vizer.define_obstacles()
cd = CollisionDetector(obstcls)
obstcls_aabb = cd.compute_AABB()

# Plan path using q_init and obstacles
planner = PathPlanner(q_init, cd)

start = time.time()
# Call algorithm
rrt_tree = planner.build_rrtstar(K=2000, epsilon=5)
end = time.time()
print('Time taken: %f' % (end - start))
Exemple #17
0
    dataset_size = len(trainset)
    trainloader = torch.utils.data.DataLoader(trainset,
                                              batch_size=opt.batch_size,
                                              shuffle=True,
                                              num_workers=opt.nThreads,
                                              drop_last=True)
    log_string('#training point clouds = %d' % dataset_size)
    testloader = torch.utils.data.DataLoader(testset,
                                             batch_size=opt.batch_size,
                                             shuffle=False,
                                             num_workers=opt.nThreads,
                                             drop_last=True)
    log_string('#testing point clouds = %d' % len(testset))

    visualizer = Visualizer(opt)
    model = Model(opt)
    if opt.pretrain_path is not None:
        checkpoint = torch.load(opt.pretrain_path)
        model.network.load_state_dict(checkpoint['state_dict'])
        model.optimizer.load_state_dict(checkpoint['optimizer'])

    print('Training begins..')
    best_iou = 0
    for epoch in range(opt.max_epoch):

        epoch_iter = 0
        for i, data in enumerate(trainloader):
            iter_start_time = time.time()
            epoch_iter += opt.batch_size
Exemple #18
0
class Worker(QtCore.QThread):

    started = QtCore.Signal()
    updated = QtCore.Signal(numpy.ndarray, numpy.ndarray)
    stopped = QtCore.Signal()

    def __init__(self, parent=None):
        super(Worker, self).__init__(parent)
        self.bed = None
        self.gen = None

        self.delay = 1.0
        self.stop_flg = False
        self.mutex = QtCore.QMutex()

    def setup(self, window_size=20, t_in=2, w=10, h=10, d=1, t_out=1, hidden_layers_sizes=[100], pretrain_step=1):
        self.bed = TestBed(window_size=window_size, t_in=t_in, w=w, h=h, d=d, t_out=t_out, hidden_layers_sizes=hidden_layers_sizes)
        self.gen = SinGenerator(w=w, h=h, d=1)
        # self.gen = RadarGenerator('../data/radar', w=w, h=h, left=0, top=80)
        self.vis = Visualizer(w=w, h=h, t_out=t_out)
        self.pretrain_step = pretrain_step

        # fill the window with data
        for i in xrange(window_size):
            y = self.gen.next()
            self.bed.supply(y)

    def setGeneratorParams(self, k, n):
        pass

    def setDelay(self, delay):
        self.delay = delay

    def setLearningParams(self, params):
        self.finetune_epochs = params['finetune_epochs']
        self.finetune_lr = params['finetune_lr']
        self.finetune_batch_size = params['finetune_batch_size']
        self.pretrain_epochs = params['pretrain_epochs']
        self.pretrain_lr = params['pretrain_lr']
        self.pretrain_batch_size = params['pretrain_batch_size']

    def stop(self):
        with QtCore.QMutexLocker(self.mutex):
            self.stop_flg = True

    def run(self):
        print("Worker: started")
        with QtCore.QMutexLocker(self.mutex):
            self.stop_flg = False
        self.started.emit()

        for i,yt in enumerate(self.gen):
            # predict
            y_preds = self.bed.predict()
            print("{0}: yt={1}, y_pred={2}".format(i, yt, y_preds))

            self.bed.supply(yt)
            self.vis.append_data(yt, y_preds)

            if i % self.pretrain_step == 0 and 0 < self.pretrain_epochs:
                # pretrain
                avg_cost = self.bed.pretrain(self.pretrain_epochs, learning_rate=self.pretrain_lr, batch_size=self.pretrain_batch_size)
                print("   pretrain cost: {0}".format(avg_cost))
                pass

            # finetune
            costs = self.bed.finetune(self.finetune_epochs, learning_rate=self.finetune_lr, batch_size=self.finetune_batch_size)
            train_cost, valid_cost, test_cost = costs
            print("   train cost: {0}".format(train_cost))

            self.vis.append_cost(train_cost, valid_cost, test_cost)

            self.updated.emit(yt, y_preds)

            time.sleep(self.delay)

            if self.stop_flg:
                print(' --- iteration end ---')
                break

        self.stopped.emit()
 def __init__(self, args):
     Visualizer.__init__(self, args, Chunk)
     self.files = {}
     self.peers = {}
Exemple #20
0
def main():
    opts = get_parser().parse_args()
    os.environ['CUDA_VISIBLE_DEVICES'] = opts.gpu_id
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    vis = Visualizer(port=opts.vis_port, env='kd')

    # Set up random seed
    torch.manual_seed(opts.random_seed)
    torch.cuda.manual_seed(opts.random_seed)
    np.random.seed(opts.random_seed)
    random.seed(opts.random_seed)

    cur_epoch = 0
    best_score = 0.0
    ckpt_dir = './checkpoints'
    latest_ckpt = 'checkpoints/kd_resnet34_latest.pth'
    best_ckpt = 'checkpoints/kd_resnet34_best.pth'

    #  Set up dataloader
    transforms_train = transforms.Compose([
        transforms.Resize(size=224),
        transforms.RandomCrop(size=(224, 224)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])
    transforms_val = transforms.Compose([
        transforms.Resize(size=224),
        transforms.CenterCrop(size=(224, 224)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])

    cub_root = os.path.join(opts.data_root, 'cub200')
    train_cub = CUB200(root=cub_root,
                       split='train',
                       transforms=transforms_train,
                       download=opts.download,
                       offset=0)
    val_cub = CUB200(root=cub_root,
                     split='test',
                     transforms=transforms_val,
                     download=False,
                     offset=0)

    dogs_root = os.path.join(opts.data_root, 'dogs')
    train_dogs = StanfordDogs(root=dogs_root,
                              split='train',
                              transforms=transforms_train,
                              download=opts.download,
                              offset=200)
    val_dogs = StanfordDogs(root=dogs_root,
                            split='test',
                            transforms=transforms_val,
                            download=False,
                            offset=200)  # add offset

    train_dst = data.ConcatDataset([train_cub, train_dogs])
    val_dst = data.ConcatDataset([val_cub, val_dogs])

    train_loader = data.DataLoader(train_dst,
                                   batch_size=opts.batch_size,
                                   drop_last=True,
                                   shuffle=True,
                                   num_workers=4)
    val_loader = data.DataLoader(val_dst,
                                 batch_size=opts.batch_size,
                                 drop_last=True,
                                 shuffle=False,
                                 num_workers=4)

    # pretrained teachers
    print("Loading pretrained teachers ...")
    cub_teacher_ckpt = 'checkpoints/cub200_resnet18_best.pth'
    dogs_teacher_ckpt = 'checkpoints/dogs_resnet34_best.pth'
    t_cub = resnet18(num_classes=200).to(device)
    t_dogs = resnet34(num_classes=120).to(device)
    t_cub.load_state_dict(torch.load(cub_teacher_ckpt)['model_state'])
    t_dogs.load_state_dict(torch.load(dogs_teacher_ckpt)['model_state'])
    t_cub.eval()
    t_dogs.eval()

    num_classes = 120 + 200
    stu = resnet34(pretrained=True, num_classes=num_classes).to(device)
    metrics = StreamClsMetrics(num_classes)

    params_1x = []
    params_10x = []
    for name, param in stu.named_parameters():
        if 'fc' in name:
            params_10x.append(param)
        else:
            params_1x.append(param)
    optimizer = torch.optim.Adam([
        {
            'params': params_1x,
            'lr': opts.lr
        },
        {
            'params': params_10x,
            'lr': opts.lr * 10
        },
    ],
                                 lr=opts.lr,
                                 weight_decay=1e-4)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                step_size=30,
                                                gamma=0.1)

    criterion_ce = SoftCELoss(T=1.0)

    def save_ckpt(path):
        """ save current model
        """
        state = {
            "epoch": cur_epoch,
            "model_state": stu.state_dict(),
            "optimizer_state": optimizer.state_dict(),
            "scheduler_state": scheduler.state_dict(),
            "best_score": best_score,
        }
        torch.save(state, path)
        print("Model saved as %s" % path)

    print("Training ...")
    # ===== Train Loop =====#
    while cur_epoch < opts.epochs:
        stu.train()
        epoch_loss = kd(cur_epoch=cur_epoch,
                        criterion_ce=criterion_ce,
                        model=stu,
                        teachers=[t_cub, t_dogs],
                        optim=optimizer,
                        train_loader=train_loader,
                        device=device,
                        scheduler=scheduler,
                        vis=vis,
                        trace_name='kd')

        print("End of Epoch %d/%d, Average Loss=%f" %
              (cur_epoch, opts.epochs, epoch_loss))

        # =====  Latest Checkpoints  =====
        save_ckpt(latest_ckpt)
        #
        # =====  Validation  =====
        print("validate on val set...")
        stu.eval()
        val_score = validate(model=stu,
                             loader=val_loader,
                             device=device,
                             metrics=metrics)
        print(metrics.to_str(val_score))
        #
        # =====  Save Best Model  =====
        if val_score['Overall Acc'] > best_score:  # save best model
            best_score = val_score['Overall Acc']
            save_ckpt(best_ckpt)


#
        vis.vis_scalar('Overall Acc', 'kd', cur_epoch + 1,
                       val_score['Overall Acc'])
        cur_epoch += 1
class Manager:
	"""Frame of options to manage a monster."""

	def __init__(self, name, master, total_var):
		self.root = LabelFrame(master, text = name)
		self.visualizer = Visualizer(name, self.root)
		self.monster = Monster(name)
		self.stats = Stat_Tracker()

		self.total_var = total_var
		self.total_var.set(total_var.get() + 1)

		self.visualizer.monster_image.grid(row = 0, column = 6, rowspan = 3)
		self.visualizer.mood_image.grid(row = 0, column = 7, rowspan = 3)

		# controls whether the button's perform their action, for use when
		# a monster is dead or a minigame is playing
		self.button_bool = True

		########## age ##################################
		self.age_label = Label(self.root, text = "Age:  ")
		self.age_label.grid(row = 0, column = 2)
		
		self.age_state = StringVar()
		self.update_age()
		Label(self.root, textvariable = self.age_state).grid(row = 0, column = 3)

		########### mood ############################################
		self.mood_label = Label(self.root, text = "Mood:  ")
		self.mood_label.grid(row = 0, column = 0)

		self.mood_state = StringVar()
		self.update_mood()
		Label(self.root, textvariable = self.mood_state).grid(row = 0, column = 1)

		######### hunger ############################################
		self.hunger_label = Label(self.root, text = "Hunger:  ")
		self.hunger_label.grid(row = 1, column = 0)

		self.hunger_state = StringVar()
		self.update_hunger()
		Label(self.root, textvariable = self.hunger_state).grid(row = 1, column = 1)

		self.hunger_button = Button(self.root, text = "Feed", command = self.feed)
		self.hunger_button.grid(row = 1, column = 2)

		######## sleepiness ############################################
		self.sleep_label = Label(self.root, text = "Sleepiness:  ")
		self.sleep_label.grid(row = 2, column = 0)

		self.sleep_state = StringVar()
		self.update_sleepiness()
		Label(self.root, textvariable = self.sleep_state).grid(row = 2, column = 1)

		self.sleep_button = Button(self.root, text = "Nap", command = self.nap)
		self.sleep_button.grid(row = 2, column = 2)

		######### boredom ############################################
		self.boredom_label = Label(self.root, text = "Boredom:  ")
		self.boredom_label.grid(row = 1, column = 3)

		self.boredom_state = StringVar()
		self.update_boredom()
		Label(self.root, textvariable = self.boredom_state).grid(row = 1, column = 4)

		self.boredom_button = Button(self.root, text = "Play", command = self.play)
		self.boredom_button.grid(row = 1, column = 5)

		######### dirtiness ############################################
		self.dirt_label = Label(self.root, text = "Dirtiness:  ")
		self.dirt_label.grid(row = 2, column = 3)

		self.dirt_state = StringVar()
		self.update_dirtiness()
		Label(self.root, textvariable = self.dirt_state).grid(row = 2, column = 4)

		self.dirt_button = Button(self.root, text = "Clean", command = self.clean)
		self.dirt_button.grid(row = 2, column = 5)
		
		self.root.pack()

	def update_hunger(self):
		"""Updates the hunger label by generating a string from the monster's hunger field."""
		value = self.monster.hunger	
		if value >= 2 * Monster.HUNGRY:
			text = "High"
		elif value >= Monster.HUNGRY:
			text = "Medium"
		else:
			text = "Low"
		self.hunger_state.set(text)

	def feed(self):
		"""Feeds the monster."""
		if self.button_bool:
			self.stats.update_stats("feed", self.monster.mood, self.monster.age)
			self.monster.feed()
			self.update_all()

	def update_sleepiness(self):
		"""Updates the sleepiness label by generating a string from the monster's sleepiness field."""
		value = self.monster.sleepiness
		if value >= 2 * Monster.SLEEPY:
			text = "High"
		elif value >= Monster.SLEEPY:
			text = "Medium"
		else:
			text = "Low"
		self.sleep_state.set(text)

	def nap(self):
		"""Puts the monster down for a nap."""
		if self.button_bool:
			self.stats.update_stats("nap", self.monster.mood, self.monster.age)
			self.monster.nap()
			self.update_all()

	def update_boredom(self):
		"""Updates the boredom label by generating a string from the monster's boredom field."""
		value = self.monster.boredom
		if value >= 2 * Monster.BORED:
			text = "High"
		elif value >= Monster.BORED:
			text = "Medium"
		else:
			text = "Low"
		self.boredom_state.set(text)

	def play(self):
		"""Entertains the monster."""
		if self.button_bool:
			self.stats.update_stats("play", self.monster.mood, self.monster.age)
			self.monster.play()
			self.update_all()

	def update_dirtiness(self):
		"""Updates the dirtiness label by generating a string from the monster's dirtiness field."""
		value = self.monster.dirtiness
		if value >= 2 * Monster.DIRTY:
			text = "High"
		elif value >= Monster.DIRTY:
			text = "Medium"
		else:
			text = "Low"
		self.dirt_state.set(text)

	def clean(self):
		"""Cleans the monster."""
		if self.button_bool:
			self.stats.update_stats("clean", self.monster.mood, self.monster.age)
			self.monster.clean()	
			self.update_all()

	def update_mood(self):
		"""Updates the mood state label."""
		self.mood_state.set(self.monster.mood)

	def update_age(self):
		"""Updates the age label by generating a string based on the monster's age property."""
		if self.monster.age == "Dead":
			self.button_bool = False
			self.stats.monster_death(self)
		self.age_state.set(self.monster.age)

	def euthanize(self):
		"""Euthanizes the monster, removing it's manager frame from the window."""
		self.total_var.set(self.total_var.get() - 1)
		self.root.destroy()

	def update_all(self):
		"""Calls all the update methods for the state labels."""
		self.update_age()
		self.update_mood()
		self.update_hunger()
		self.update_sleepiness()
		self.update_boredom()
		self.update_dirtiness()	

		self.visualizer.update_mood(self.monster.mood)
		self.visualizer.update_age(self.monster.age)
Exemple #22
0
 def visualize(self):
     Visualizer(self.goal, self.progressive_total_points).generate()
Exemple #23
0
class Demo(BaseDemo):
    def __init__(self, args):
        super(Demo, self).__init__(args)
        self.model, self.model_gt = self.init_model(self.data.m_kernel)
        self.visualizer = Visualizer(args, self.data.reverse_m_dict)

    def init_model(self, m_kernel):
        self.model = Net(self.im_size, self.im_size, self.im_channel,
                         self.num_frame - 1, m_kernel.shape[1], self.m_range,
                         m_kernel)
        self.model_gt = GtNet(self.im_size, self.im_size, self.im_channel,
                              self.num_frame - 1, m_kernel.shape[1],
                              self.m_range, m_kernel)
        if torch.cuda.is_available():
            # model = torch.nn.DataParallel(model).cuda()
            self.model = self.model.cuda()
            self.model_gt = self.model_gt.cuda()
        if self.init_model_path is not '':
            self.model.load_state_dict(torch.load(self.init_model_path))
        return self.model, self.model_gt

    def train(self):
        optimizer = optim.Adam(self.model.parameters(), lr=self.learning_rate)
        base_loss, train_loss = [], []
        for epoch in range(self.train_epoch):
            optimizer.zero_grad()
            if self.data.name in ['box', 'mnist', 'box_complex']:
                im, _, _, _ = self.data.get_next_batch(self.data.train_images)
            elif self.data.name in ['box2', 'mnist2']:
                im, _, _ = self.data.get_next_batch(self.data.train_images)
            elif self.data.name in [
                    'robot64', 'mpii64', 'mpi128', 'nyuv2', 'robot128',
                    'viper64', 'viper128'
            ]:
                im = self.data.get_next_batch(self.data.train_images)
            else:
                logging.error('%s data not supported' % self.data.name)
                sys.exit()
            im_input = im[:, :-1, :, :, :].reshape(self.batch_size, -1,
                                                   self.im_size, self.im_size)
            im_output = im[:, -1, :, :, :]
            im_input = Variable(torch.from_numpy(im_input).float())
            im_output = Variable(torch.from_numpy(im_output).float())
            if torch.cuda.is_available():
                im_input, im_output = im_input.cuda(), im_output.cuda()
            im_pred, m_mask, d_mask, appear = self.model(im_input)
            # im_diff = im_pred - im_output
            # loss = torch.abs(im_diff).sum()
            im_pred_grad = im_pred[:, :, :-1, :] - im_pred[:, :, 1:, :]
            im_output_grad = im_output[:, :, :-1, :] - im_output[:, :, 1:, :]
            # loss = loss + torch.abs(im_pred_grad - im_output_grad).sum()
            loss = torch.abs(im_pred_grad - im_output_grad).sum()
            im_pred_grad = im_pred[:, :, :, :-1] - im_pred[:, :, :, 1:]
            im_output_grad = im_output[:, :, :, :-1] - im_output[:, :, :, 1:]
            loss = loss + torch.abs(im_pred_grad - im_output_grad).sum()
            loss.backward()
            optimizer.step()

            train_loss.append(loss.data[0])
            if len(train_loss) > 100:
                train_loss.pop(0)
            ave_train_loss = sum(train_loss) / float(len(train_loss))
            # base = torch.abs(im_input[:, -self.im_channel:, :, :] - im_output).sum()
            im_input_grad = im_input[:, -self.im_channel:, :
                                     -1, :] - im_input[:, -self.im_channel:,
                                                       1:, :]
            im_output_grad = im_output[:, :, :-1, :] - im_output[:, :, 1:, :]
            # base = base + torch.abs(im_input_grad - im_output_grad).sum()
            base = torch.abs(im_input_grad - im_output_grad).sum()
            im_input_grad = im_input[:, -self.im_channel:, :, :
                                     -1] - im_input[:, -self.im_channel:, :,
                                                    1:]
            im_output_grad = im_output[:, :, :, :-1] - im_output[:, :, :, 1:]
            base = base + torch.abs(im_input_grad - im_output_grad).sum()
            base_loss.append(base.data[0])
            if len(base_loss) > 100:
                base_loss.pop(0)
            ave_base_loss = sum(base_loss) / float(len(base_loss))
            logging.info(
                'epoch %d, train loss: %.2f, average train loss: %.2f, base loss: %.2f',
                epoch, loss.data[0], ave_train_loss, ave_base_loss)
            if (epoch + 1) % self.test_interval == 0:
                logging.info('epoch %d, testing', epoch)
                self.validate()

    def test(self):
        base_loss, test_loss = [], []
        test_epe = []
        motion = None
        for epoch in range(self.test_epoch):
            if self.data.name in ['box', 'mnist', 'box_complex']:
                im, motion, _, _ = self.data.get_next_batch(
                    self.data.test_images)
            elif self.data.name in ['box2', 'mnist2']:
                im, motion, _ = self.data.get_next_batch(self.data.test_images)
            elif self.data.name in [
                    'robot64', 'mpii64', 'mpi128', 'nyuv2', 'robot128',
                    'viper64', 'viper128'
            ]:
                im, motion = self.data.get_next_batch(
                    self.data.test_images), None
            elif self.data.name in ['mpii64_sample']:
                im, motion = self.data.get_next_batch(
                    self.data.test_images), None
                im = im[:, -self.num_frame:, :, :, :]
            else:
                logging.error('%s data not supported' % self.data.name)
                sys.exit()
            im_input = im[:, :-1, :, :, :].reshape(self.batch_size, -1,
                                                   self.im_size, self.im_size)
            im_output = im[:, -1, :, :, :]
            im_input = Variable(torch.from_numpy(im_input).float(),
                                volatile=True)
            im_output = Variable(torch.from_numpy(im_output).float(),
                                 volatile=True)
            if torch.cuda.is_available():
                im_input, im_output = im_input.cuda(), im_output.cuda()
            im_pred, m_mask, d_mask, appear = self.model(im_input)
            # im_diff = im_pred - im_output
            # loss = torch.abs(im_diff).sum()
            im_pred_grad = im_pred[:, :, :-1, :] - im_pred[:, :, 1:, :]
            im_output_grad = im_output[:, :, :-1, :] - im_output[:, :, 1:, :]
            # loss = loss + torch.abs(im_pred_grad - im_output_grad).sum()
            loss = torch.abs(im_pred_grad - im_output_grad).sum()
            im_pred_grad = im_pred[:, :, :, :-1] - im_pred[:, :, :, 1:]
            im_output_grad = im_output[:, :, :, :-1] - im_output[:, :, :, 1:]
            loss = loss + torch.abs(im_pred_grad - im_output_grad).sum()

            test_loss.append(loss.data[0])
            # base = torch.abs(im_input[:, -self.im_channel:, :, :] - im_output).sum()
            im_input_grad = im_input[:, -self.im_channel:, :
                                     -1, :] - im_input[:, -self.im_channel:,
                                                       1:, :]
            im_output_grad = im_output[:, :, :-1, :] - im_output[:, :, 1:, :]
            # base = base + torch.abs(im_input_grad - im_output_grad).sum()
            base = torch.abs(im_input_grad - im_output_grad).sum()
            im_input_grad = im_input[:, -self.im_channel:, :, :
                                     -1] - im_input[:, -self.im_channel:, :,
                                                    1:]
            im_output_grad = im_output[:, :, :, :-1] - im_output[:, :, :, 1:]
            base = base + torch.abs(im_input_grad - im_output_grad).sum()
            base_loss.append(base.data[0])
            flow = self.motion2flow(m_mask)
            depth = self.mask2depth(d_mask)

            if motion is None:
                gt_motion = None
            else:
                gt_motion = motion[:, -2, :, :, :]
                gt_motion = Variable(torch.from_numpy(gt_motion).float())
                if torch.cuda.is_available():
                    gt_motion = gt_motion.cuda()
                epe = (flow - gt_motion) * (flow - gt_motion)
                epe = torch.sqrt(epe.sum(1))
                epe = epe.sum() / epe.numel()
                test_epe.append(epe.cpu().data[0])
            if self.display:
                self.visualizer.visualize_result(im_input, im_output, im_pred,
                                                 flow, gt_motion, depth,
                                                 appear, 'test_%d.png' % epoch)
            if self.display_all:
                for i in range(self.batch_size):
                    self.visualizer.visualize_result(im_input, im_output,
                                                     im_pred, flow, gt_motion,
                                                     depth, appear,
                                                     'test_%d.png' % i, i)
        test_loss = numpy.mean(numpy.asarray(test_loss))
        base_loss = numpy.mean(numpy.asarray(base_loss))
        improve_loss = base_loss - test_loss
        improve_percent = improve_loss / (base_loss + 1e-5)
        logging.info('average test loss: %.2f, base loss: %.2f', test_loss,
                     base_loss)
        logging.info('improve_loss: %.2f, improve_percent: %.2f', improve_loss,
                     improve_percent)
        if motion is not None:
            test_epe = numpy.mean(numpy.asarray(test_epe))
            logging.info('average test endpoint error: %.2f', test_epe)
        return improve_percent

    def test_gt(self):
        base_loss, test_loss = [], []
        test_epe = []
        for epoch in range(self.test_epoch):
            if self.data.name in ['box', 'mnist', 'box_complex']:
                im, motion, motion_label, depth = self.data.get_next_batch(
                    self.data.test_images)
                gt_motion_label = motion_label[:, -2, :, :, :]
                gt_motion_label = Variable(torch.from_numpy(gt_motion_label))
                if torch.cuda.is_available():
                    gt_motion_label = gt_motion_label.cuda()
            elif self.data.name in ['box2', 'mnist2']:
                im, motion, depth = self.data.get_next_batch(
                    self.data.test_images)
            else:
                logging.error('%s data not supported in test_gt' %
                              self.data.name)
                sys.exit()
            im_input = im[:, :-1, :, :, :].reshape(self.batch_size, -1,
                                                   self.im_size, self.im_size)
            im_output = im[:, -1, :, :, :]
            gt_motion = motion[:, -2, :, :, :]
            gt_depth = depth[:, -2, :, :, :]
            im_input = Variable(torch.from_numpy(im_input).float(),
                                volatile=True)
            im_output = Variable(torch.from_numpy(im_output).float(),
                                 volatile=True)
            gt_motion = Variable(torch.from_numpy(gt_motion).float())
            gt_depth = Variable(torch.from_numpy(gt_depth).float())
            if torch.cuda.is_available():
                im_input, im_output = im_input.cuda(), im_output.cuda()
                gt_motion = gt_motion.cuda()
                gt_depth = gt_depth.cuda()
            if self.data.name in ['box', 'mnist', 'box_complex']:
                im_pred, m_mask, d_mask, appear = self.model_gt(
                    im_input, gt_motion_label, gt_depth, 'label')
            elif self.data.name in ['box2', 'mnist2']:
                im_pred, m_mask, d_mask, appear = self.model_gt(
                    im_input, gt_motion, gt_depth)
            # im_diff = im_pred - im_output
            # loss = torch.abs(im_diff).sum()
            im_pred_grad = im_pred[:, :, :-1, :] - im_pred[:, :, 1:, :]
            im_output_grad = im_output[:, :, :-1, :] - im_output[:, :, 1:, :]
            # loss = loss + torch.abs(im_pred_grad - im_output_grad).sum()
            loss = torch.abs(im_pred_grad - im_output_grad).sum()
            im_pred_grad = im_pred[:, :, :, :-1] - im_pred[:, :, :, 1:]
            im_output_grad = im_output[:, :, :, :-1] - im_output[:, :, :, 1:]
            loss = loss + torch.abs(im_pred_grad - im_output_grad).sum()

            test_loss.append(loss.data[0])
            # base = torch.abs(im_input[:, -self.im_channel:, :, :] - im_output).sum()
            im_input_grad = im_input[:, -self.im_channel:, :
                                     -1, :] - im_input[:, -self.im_channel:,
                                                       1:, :]
            im_output_grad = im_output[:, :, :-1, :] - im_output[:, :, 1:, :]
            # base = base + torch.abs(im_input_grad - im_output_grad).sum()
            base = torch.abs(im_input_grad - im_output_grad).sum()
            im_input_grad = im_input[:, -self.im_channel:, :, :
                                     -1] - im_input[:, -self.im_channel:, :,
                                                    1:]
            im_output_grad = im_output[:, :, :, :-1] - im_output[:, :, :, 1:]
            base = base + torch.abs(im_input_grad - im_output_grad).sum()
            base_loss.append(base.data[0])
            flow = self.motion2flow(m_mask)
            depth = self.mask2depth(d_mask)
            epe = (flow - gt_motion) * (flow - gt_motion)
            epe = torch.sqrt(epe.sum(1))
            epe = epe.sum() / epe.numel()
            test_epe.append(epe.cpu().data[0])
            if self.display:
                self.visualizer.visualize_result(im_input, im_output, im_pred,
                                                 flow, gt_motion, depth,
                                                 appear, 'test_gt.png')
            if self.display_all:
                for i in range(self.batch_size):
                    self.visualizer.visualize_result(im_input, im_output,
                                                     im_pred, flow, gt_motion,
                                                     depth, appear,
                                                     'test_gt_%d.png' % i, i)
        test_loss = numpy.mean(numpy.asarray(test_loss))
        base_loss = numpy.mean(numpy.asarray(base_loss))
        improve_loss = base_loss - test_loss
        improve_percent = improve_loss / (base_loss + 1e-5)
        logging.info('average ground truth test loss: %.2f, base loss: %.2f',
                     test_loss, base_loss)
        logging.info('improve_loss: %.2f, improve_percent: %.2f', improve_loss,
                     improve_percent)
        test_epe = numpy.mean(numpy.asarray(test_epe))
        logging.info('average ground truth test endpoint error: %.2f',
                     test_epe)
        return improve_percent

    @staticmethod
    def mask2depth(d_mask):
        [batch_size, num_depth, height, width] = d_mask.size()
        depth_number = Variable(
            torch.zeros(batch_size, num_depth, height, width))
        if torch.cuda.is_available():
            depth_number = depth_number.cuda()
        for i in range(num_depth):
            depth_number[:, i, :, :] = i
        depth = Variable(torch.zeros(batch_size, 1, height, width))
        if torch.cuda.is_available():
            depth = depth.cuda()
        depth[:, 0, :, :] = (d_mask * depth_number).sum(1)
        return depth

    def motion2flow(self, m_mask):
        reverse_m_dict = self.data.reverse_m_dict
        [batch_size, num_class, height, width] = m_mask.size()
        kernel_x = Variable(torch.zeros(batch_size, num_class, height, width))
        kernel_y = Variable(torch.zeros(batch_size, num_class, height, width))
        if torch.cuda.is_available():
            kernel_x = kernel_x.cuda()
            kernel_y = kernel_y.cuda()
        for i in range(num_class):
            (m_x, m_y) = reverse_m_dict[i]
            kernel_x[:, i, :, :] = m_x
            kernel_y[:, i, :, :] = m_y
        flow = Variable(torch.zeros(batch_size, 2, height, width))
        if torch.cuda.is_available():
            flow = flow.cuda()
        flow[:, 0, :, :] = (m_mask * kernel_x).sum(1) / m_mask.sum(1)
        flow[:, 1, :, :] = (m_mask * kernel_y).sum(1) / m_mask.sum(1)
        return flow
Exemple #24
0
def main():
    # train logistic regression with stocastic gradient Langevin Gradient

    if not os.path.isdir("log/"):
        os.makedirs("log/")
    now = datetime.datetime.today()
    logdir = "log/log%s/" % now.strftime("%Y%m%d-%H%M")
    os.makedirs(logdir)

    # tensorboard
    writer = tf.contrib.summary.create_file_writer(logdir)
    global_step=tf.train.get_or_create_global_step()
    writer.set_as_default()

    # read hyperparameters from file
    hparams = tf.contrib.training.HParams(
                lr=0.1,
                model="SGLD_LR",
                epoch=10,
                batch_size=10)

    if args.hparams:
        shutil.copyfile(args.hparams, logdir + "params")
        hparams_from_file = ""
        with open(args.hparams, "r") as f:
            for l in f.readlines():
                hparams_from_file += l

    hparams.parse(hparams_from_file)

    # choose model
    if hparams.model == "SGLD_LR":
        nn = model.SGLD_LR(hparams)
        train_dataset, train_dataset_size = get_dataset(hparams.model, "train")
        val_dataset, val_dataset_size = get_dataset(hparams.model, "validation")
    else:
        raise "Invalid parameter for hparams.model"

    visualizer = Visualizer()

    # train
    epsilon_ = hparams.lr
    step = 0
    for epoch in range(hparams.epoch):
        train_dataset_iter = train_dataset.shuffle(train_dataset_size).batch(hparams.batch_size)

        for batch, data in enumerate(train_dataset_iter):
            global_step.assign_add(1)
            step += 1

            epsilon_ = hparams.lr / (1 + 0.05 * step)
            epsilon = tf.convert_to_tensor(epsilon_, tf.float32)

            loss = nn.loss(data["data"], data["label"]).numpy()
            accuracy = nn.accuracy(data["data"], data["label"]).numpy()

            visualizer.store_results(nn)

            nn.update(data["data"], data["label"], epsilon, train_dataset_size)

            with tf.contrib.summary.record_summaries_every_n_global_steps(10):
                tf.contrib.summary.scalar('loss', loss)
                tf.contrib.summary.scalar('accuracy', accuracy)
                tf.contrib.summary.scalar('epsilon', epsilon)

                grads_vars = nn.grads_variances()
                for i in range(len(grads_vars)):
                    tf.contrib.summary.scalar('grads_var%d' % (i+1), grads_vars[i])

        print("epoch %3d\tbatch %4d\tloss %.4f\taccuracy %.4f" % (epoch+1, batch+1, loss, accuracy))

    for l_epoch in range(100):
        print("langevin epoch %3d" % (l_epoch+1))
        train_dataset_iter = train_dataset.shuffle(train_dataset_size).batch(hparams.batch_size)

        for batch, data in enumerate(train_dataset_iter):
            visualizer.store_results(nn)
            nn.update(data["data"], data["label"], epsilon, train_dataset_size)

    # visualize
    visualizer.save_results(logdir, train_dataset)
Exemple #25
0
def learning_loop(env_id, episode_count, model, visual, n_options):
    env = gym.make(env_id)
    outdir = '/tmp/random-agent-results'
    env = wrappers.Monitor(env, directory=outdir, force=True)
    env.seed(0)
    agent = OptionCriticAgent(env.action_space, env.observation_space,
                              n_options)
    option_label = ["opt {}".format(str(i + 1)) for i in range(n_options)]
    vis = Visualizer(["ACC_X", "ACC_Y", "DEC_X", "DEC_Y", "NONE"],
                     option_label)
    if model:
        agent.load_model(model)
    reward = 0
    done = False
    total_reward_list = []
    steps_list = []
    max_q_list = []
    max_q_episode_list = []
    max_q = 0.0

    for i in trange(episode_count):
        total_reward = 0
        n_steps = 0
        ob = env.reset()
        option = agent.get_option(ob)
        pre_option = option
        action = agent.act(ob, option)
        pre_action = action
        agent.set_last_q_omega(option, ob)
        is_render = False
        while True:
            if (i + 1) % 20 == 0 and visual:
                env.render()
                is_render = True
            pre_obs = ob
            ob, reward, done, _ = env.step(action)
            n_steps += 1
            rand_basis = np.random.uniform()
            if agent.get_terminate(ob, option) > rand_basis:
                pre_option = option
                option = agent.get_option(ob)
            pre_action = action
            action = agent.act(ob, option)
            agent.update(pre_option, pre_obs, pre_action, option, ob, action,
                         reward, done)
            total_reward += reward
            tmp_max_q = agent.get_max_q_u(ob, option)
            max_q_list.append(tmp_max_q)
            max_q = tmp_max_q if tmp_max_q > max_q else max_q
            if done:
                print("episode: {}, steps: {}, total_reward: {}, max_q_u: {}".
                      format(i, n_steps, total_reward, max_q_list[-1]))
                total_reward_list.append(total_reward)
                steps_list.append(n_steps)
                break

            if is_render:
                vis.set_action_dist(agent.vis_action_dist, action)
                vis.set_option_q(agent.vis_option_q, option)
                vis.pause(.0001)

            # Note there's no env.render() here. But the environment still can open window and
            # render if asked by env.monitor: it calls env.render('rgb_array') to record video.
            # Video is not recorded every episode, see capped_cubic_video_schedule for details.
        max_q_episode_list.append(max_q)

    date = datetime.now().strftime("%Y%m%d")
    time = datetime.now().strftime("%H%M")
    saved_dir = os.path.join("data", date, time)
    # export process
    saved_res_dir = os.path.join(saved_dir, 'res')
    export_csv(saved_res_dir, "total_reward.csv", total_reward_list)
    td_error_list = agent.td_error_list
    td_error_list_meta = agent.td_error_list_meta
    export_csv(saved_res_dir, "td_error.csv", td_error_list)
    total_reward_list = np.array(total_reward_list)
    steps_list = np.array(steps_list)
    max_q_list = np.array(max_q_list)
    print("Average return: {}".format(np.average(total_reward_list)))

    # save model
    saved_model_dir = os.path.join(saved_dir, 'model')
    agent.save_model(saved_model_dir)

    # output graph
    x = list(range(len(total_reward_list)))
    plt.subplot(4, 2, 1)
    y = moved_average(total_reward_list, 10)
    plt.plot(x, total_reward_list)
    plt.plot(x, y, 'r--')
    plt.title("total_reward")
    plt.subplot(4, 2, 2)
    y = moved_average(steps_list, 10)
    plt.plot(x, steps_list)
    plt.plot(x, y, 'r--')
    plt.title("the number of steps until goal")
    plt.subplot(4, 1, 2)
    y = moved_average(td_error_list, 1000)
    x = list(range(len(td_error_list)))
    plt.plot(x, td_error_list, 'k-')
    plt.plot(x, y, 'r--', label='average')
    plt.title("intra-option Q critic td error")
    plt.legend()
    plt.subplot(4, 1, 3)
    y = moved_average(td_error_list_meta, 1000)
    x = list(range(len(td_error_list_meta)))
    plt.plot(x, td_error_list_meta, 'k-')
    plt.plot(x, y, 'r--', label='average')
    plt.title("intra-option Q learning td error")
    plt.legend()
    plt.subplot(4, 1, 4)
    y = moved_average(max_q_episode_list, 1000)
    x = list(range(len(max_q_episode_list)))
    plt.plot(x, max_q_episode_list, 'k-')
    # plt.plot(x, y, 'r--', label='average')
    plt.title("max q_u value")
    plt.legend()
    plt.tight_layout()
    plt.savefig(os.path.join(saved_res_dir, "plot.png"))
    plt.show()
    plt.savefig(os.path.join(saved_res_dir, "res_plot.png"))
    env.close()
 def __init__(self, args):
     super(Demo, self).__init__(args)
     self.model, self.model_gt = self.init_model(self.data.m_kernel)
     self.visualizer = Visualizer(args, self.data.reverse_m_dict)
class Demo(BaseDemo):
    def __init__(self, args):
        super(Demo, self).__init__(args)
        self.model, self.model_gt = self.init_model(self.data.m_kernel)
        self.visualizer = Visualizer(args, self.data.reverse_m_dict)

    def init_model(self, m_kernel):
        self.model = Net(self.im_size, self.im_size, 3, self.num_frame - 1,
                         m_kernel.shape[1], self.m_range, m_kernel)
        self.model_gt = GtNet(self.im_size, self.im_size, 3,
                              self.num_frame - 1, m_kernel.shape[1],
                              self.m_range, m_kernel)
        if torch.cuda.is_available():
            # model = torch.nn.DataParallel(model).cuda()
            self.model = self.model.cuda()
            self.model_gt = self.model_gt.cuda()
        if self.init_model_path is not '':
            self.model.load_state_dict(torch.load(self.init_model_path))
        return self.model, self.model_gt

    def train_unsupervised(self):
        optimizer = optim.Adam(self.model.parameters(), lr=self.learning_rate)
        base_loss, train_loss = [], []
        for epoch in range(self.train_epoch):
            optimizer.zero_grad()
            im, _, _, _ = self.data.get_next_batch(self.data.train_images)
            im_input = im[:, :-1, :, :, :].reshape(self.batch_size, -1,
                                                   self.im_size, self.im_size)
            im_output = im[:, -1, :, :, :]
            im_input = Variable(torch.from_numpy(im_input).float())
            im_output = Variable(torch.from_numpy(im_output).float())
            if torch.cuda.is_available():
                im_input, im_output = im_input.cuda(), im_output.cuda()
            im_pred, m_mask, occlude, unocclude = self.model(im_input)
            im_diff = unocclude.expand_as(im_output) * (im_pred - im_output)
            im_diff = im_diff / unocclude.sum(3).sum(2).expand_as(im_diff)
            loss = torch.abs(im_diff).sum() * im_diff.size(2) * im_diff.size(3)
            # loss = torch.abs(im_diff).sum()
            loss = loss + 0.1 * occlude.sum()
            loss.backward()
            optimizer.step()

            train_loss.append(loss.data[0])
            if len(train_loss) > 100:
                train_loss.pop(0)
            ave_train_loss = sum(train_loss) / float(len(train_loss))
            base_loss.append(
                torch.abs(im_input[:, -3:, :, :] - im_output).sum().data[0])
            if len(base_loss) > 100:
                base_loss.pop(0)
            ave_base_loss = sum(base_loss) / float(len(base_loss))
            logging.info(
                'epoch %d, train loss: %.2f, average train loss: %.2f, base loss: %.2f',
                epoch, loss.data[0], ave_train_loss, ave_base_loss)
            if (epoch + 1) % self.test_interval == 0:
                logging.info('epoch %d, testing', epoch)
                self.validate()

    def test_unsupervised(self):
        base_loss, test_loss = [], []
        test_epe = []
        for epoch in range(self.test_epoch):
            im, motion, _, _ = self.data.get_next_batch(self.data.test_images)
            im_input = im[:, :-1, :, :, :].reshape(self.batch_size, -1,
                                                   self.im_size, self.im_size)
            im_output = im[:, -1, :, :, :]
            gt_motion = motion[:, -2, :, :, :]
            im_input = Variable(torch.from_numpy(im_input).float())
            im_output = Variable(torch.from_numpy(im_output).float())
            gt_motion = Variable(torch.from_numpy(gt_motion).float())
            if torch.cuda.is_available():
                im_input, im_output = im_input.cuda(), im_output.cuda()
                gt_motion = gt_motion.cuda()
            im_pred, m_mask, occlude, unocclude = self.model(im_input)
            im_diff = unocclude.expand_as(im_output) * (im_pred - im_output)
            im_diff = im_diff / unocclude.sum(3).sum(2).expand_as(im_diff)
            loss = torch.abs(im_diff).sum() * im_diff.size(2) * im_diff.size(3)
            # loss = torch.abs(im_diff).sum()
            loss = loss + 0.1 * occlude.sum()

            test_loss.append(loss.data[0])
            base_loss.append(
                torch.abs(im_input[:, -3:, :, :] - im_output).sum().data[0])
            flow = self.motion2flow(m_mask)
            epe = (flow - gt_motion) * (flow - gt_motion)
            epe = torch.sqrt(epe.sum(1))
            epe = epe.sum() / epe.numel()
            test_epe.append(epe.cpu().data[0])
            if self.display:
                self.visualizer.visualize_result(im_input, im_output, im_pred,
                                                 flow, gt_motion, unocclude,
                                                 occlude,
                                                 'test_%d.png' % epoch)
        test_loss = numpy.mean(numpy.asarray(test_loss))
        base_loss = numpy.mean(numpy.asarray(base_loss))
        improve_loss = base_loss - test_loss
        improve_percent = improve_loss / (base_loss + 1e-5)
        logging.info('average test loss: %.2f, base loss: %.2f', test_loss,
                     base_loss)
        logging.info('improve_loss: %.2f, improve_percent: %.2f', improve_loss,
                     improve_percent)
        test_epe = numpy.mean(numpy.asarray(test_epe))
        logging.info('average test endpoint error: %.2f', test_epe)
        return improve_percent

    def test_gt_unsupervised(self):
        base_loss, test_loss = [], []
        test_epe = []
        for epoch in range(self.test_epoch):
            im, motion, motion_label, _ = self.data.get_next_batch(
                self.data.test_images)
            im_input = im[:, :-1, :, :, :].reshape(self.batch_size, -1,
                                                   self.im_size, self.im_size)
            im_output = im[:, -1, :, :, :]
            gt_motion = motion[:, -2, :, :, :]
            gt_motion_label = motion_label[:, -2, :, :, :]
            im_input = Variable(torch.from_numpy(im_input).float())
            im_output = Variable(torch.from_numpy(im_output).float())
            gt_motion = Variable(torch.from_numpy(gt_motion).float())
            gt_motion_label = Variable(torch.from_numpy(gt_motion_label))
            if torch.cuda.is_available():
                im_input, im_output = im_input.cuda(), im_output.cuda()
                gt_motion = gt_motion.cuda()
                gt_motion_label = gt_motion_label.cuda()
            im_pred, m_mask, occlude, unocclude = self.model_gt(
                im_input, gt_motion_label)
            im_diff = unocclude.expand_as(im_output) * (im_pred - im_output)
            im_diff = im_diff / unocclude.sum(3).sum(2).expand_as(im_diff)
            loss = torch.abs(im_diff).sum() * im_diff.size(2) * im_diff.size(3)
            # loss = torch.abs(im_diff).sum()
            loss = loss + 0.1 * occlude.sum()

            test_loss.append(loss.data[0])
            base_loss.append(
                torch.abs(im_input[:, -3:, :, :] - im_output).sum().data[0])
            flow = self.motion2flow(m_mask)
            epe = (flow - gt_motion) * (flow - gt_motion)
            epe = torch.sqrt(epe.sum(1))
            epe = epe.sum() / epe.numel()
            test_epe.append(epe.cpu().data[0])
            if self.display:
                self.visualizer.visualize_result(im_input, im_output, im_pred,
                                                 flow, gt_motion, unocclude,
                                                 occlude, 'test_gt.png')
        test_loss = numpy.mean(numpy.asarray(test_loss))
        base_loss = numpy.mean(numpy.asarray(base_loss))
        improve_loss = base_loss - test_loss
        improve_percent = improve_loss / (base_loss + 1e-5)
        logging.info('average ground truth test loss: %.2f, base loss: %.2f',
                     test_loss, base_loss)
        logging.info('improve_loss: %.2f, improve_percent: %.2f', improve_loss,
                     improve_percent)
        test_epe = numpy.mean(numpy.asarray(test_epe))
        logging.info('average ground truth test endpoint error: %.2f',
                     test_epe)
        return improve_percent
Exemple #28
0
            src_loc = tuple(event_data['src_loc'])
            dst_loc = tuple(event_data['dst_loc'])
            call = Call(event_data['src_number'], event_data['dst_number'],
                        time, dur, src_loc, dst_loc)
            # check the none return part for customer
            caller = find_customer_by_number(event_data['src_number'],
                                             customer_list)
            receiver = find_customer_by_number(event_data['dst_number'],
                                               customer_list)

            caller.make_call(call)
            receiver.receive_call(call)


if __name__ == '__main__':
    v = Visualizer()
    print("Toronto map coordinates:")
    print("  Lower-left corner: -79.697878, 43.576959")
    print("  Upper-right corner: -79.196382, 43.799568")

    input_dictionary = import_data()
    customers = create_customers(input_dictionary)
    process_event_history(input_dictionary, customers)

    # ----------------------------------------------------------------------
    # NOTE: You do not need to understand any of the implementation below,
    # to be able to solve this assignment. However, feel free to
    # read it anyway, just to get a sense of how the application runs.
    # ----------------------------------------------------------------------

    # Gather all calls to be drawn on screen for filtering, but we only want
Exemple #29
0
def main():
    numexe = 100
    numclusters = 3

    viz = Visualizer()
    clustering = Clustering()
    appList = AppList()
    appList.get_data("AppStore/AppleStore.csv", numexe + 1)

    titles = [
        "Clusters after PCA (2D), kmeans, without normalization, clusters #: "
        + str(numclusters),
        "Clusters after PCA (3D), kmeans, without normalization, clusters #: "
        + str(numclusters),
        "Clusters after PCA (2D), AHC, without normalization, clusters #: " +
        str(numclusters),
        "Clusters after PCA (3D), AHC, without normalization, clusters #: " +
        str(numclusters),
        "Clusters before PCA (2D), kmeans, without normalization, clusters #: "
        + str(numclusters),
        "Clusters before PCA (2D), AHC, without normalization, clusters #: " +
        str(numclusters),
        "Clusters after PCA (2D), kmeans, with normalization, clusters #: " +
        str(numclusters),
        "Clusters after PCA (3D), kmeans, with normalization, clusters #: " +
        str(numclusters),
        "Clusters after PCA (2D), AHC, with normalization, clusters #: " +
        str(numclusters),
        "Clusters after PCA (3D), AHC, with normalization, clusters #: " +
        str(numclusters),
        "Clusters before PCA (2D), kmeans, with normalization, clusters #: " +
        str(numclusters),
        "Clusters before PCA (2D), AHC, with normalization, clusters #: " +
        str(numclusters)
    ]

    arr = np.asarray(appList.retrieve_table(False))
    viz.reduce_dim(2, numexe, arr[1:],
                   clustering.kmeans_fit(numclusters, (arr[1:]).T), titles[0])
    viz.reduce_dim(3, numexe, arr[1:],
                   clustering.kmeans_fit(numclusters, (arr[1:]).T), titles[1])
    viz.reduce_dim(
        2, numexe, arr[1:],
        clustering.ahc_fit(numclusters, (arr[1:]).T,
                           titles[2] + ", Dendrogram"), titles[2])
    viz.reduce_dim(
        3, numexe, arr[1:],
        clustering.ahc_fit(numclusters, (arr[1:]).T,
                           titles[3] + ", Dendrogram"), titles[3])
    viz.visualize(2, numexe, arr[2:],
                  clustering.kmeans_fit(numclusters, (arr[1:]).T), titles[4])
    viz.visualize(
        2, numexe, arr[2:],
        clustering.ahc_fit(numclusters, (arr[1:]).T,
                           titles[5] + ", Dendrogram"), titles[5])

    arr = np.asarray(appList.retrieve_table(True))
    viz.reduce_dim(2, numexe, arr[1:],
                   clustering.kmeans_fit(numclusters, (arr[1:]).T), titles[6])
    viz.reduce_dim(3, numexe, arr[1:],
                   clustering.kmeans_fit(numclusters, (arr[1:]).T), titles[7])
    viz.reduce_dim(
        2, numexe, arr[1:],
        clustering.ahc_fit(numclusters, (arr[1:]).T,
                           titles[8] + ", Dendrogram"), titles[8])
    viz.reduce_dim(
        3, numexe, arr[1:],
        clustering.ahc_fit(numclusters, (arr[1:]).T,
                           titles[9] + ", Dendrogram"), titles[9])
    viz.visualize(2, numexe, arr[2:],
                  clustering.kmeans_fit(numclusters, (arr[1:]).T), titles[10])
    viz.visualize(
        2, numexe, arr[2:],
        clustering.ahc_fit(numclusters, (arr[1:]).T,
                           titles[11] + ", Dendrogram"), titles[11])
import argparse
from episode_builder import EpisodeBuilder
from visualizer import Visualizer

DEFAULT_FILE_PATH = "data/template.json"

parser = argparse.ArgumentParser(description="Visualize Conway's Game of Life")
parser.add_argument("-file_path", default=DEFAULT_FILE_PATH)
args = parser.parse_args()

file_path = args.file_path

episode_builder = EpisodeBuilder(file_path)
episode = episode_builder.get_episode()

visualizer = Visualizer(episode)
visualizer.visualize()
Exemple #31
0
    # inputs
    v_c = 1 + 0.5 * np.cos(2 * np.pi * 0.2 * time)
    w_c = -0.2 + 2 * np.cos(2 * np.pi * 0.6 * time)

    ## system
    turtlebot = TurtleBot(alpha, Q, x0=x0, ts=ts, landmarks=landmarks)

    ## extended kalman filter
    ekf = EKF(alpha, Q, sigma0=sigma, mu0=xhat0, ts=ts, landmarks=landmarks)

    # plotting
    lims = [-10, 10, -10, 10]
    viz = Visualizer(limits=lims,
                     x0=x0,
                     xhat0=xhat0,
                     sigma0=sigma,
                     landmarks=landmarks,
                     live='True')

    # run simulation
    for i, t in enumerate(time):
        if i == 0:
            continue
        # input commands
        u = [v_c[i], w_c[i]]
        if load:
            un = [vn_c[i], wn_c[i]]
        else:
            un = u

        # propagate actual system
Exemple #32
0
    d = device.GearVR
    devices.append(d(loc))
for i in xrange(3, 6):
    d = device.OculusRift
    devices.append(d(loc))
for i in xrange(6, 11):
    d = device.PlayStationVR
    devices.append(d(loc))

# Initialize cloud
traffic_level = 'low'
cloud_loc = loc
timeout = 10000  # 10 seconds
num_players = len(devices)
cloud = Cloud(traffic_level, cloud_loc, timeout, num_players)

# Initialize network
packet_loss_probability = 0  # % chance of network-related dropped packet
network = net.UDP(packet_loss_probability)

sim = Simulator(cloud, network, devices)

sim.runFor(10 * 60 * 1000)  # Number of milliseconds

results = sim.getResults()

viz = Visualizer(results, map(lambda n: "Device " + str(n), range(1, 11)))
viz.plotAverageLatency()

# TODO: Be run with passed configurations, create simulator, and produce results
Exemple #33
0
class Demo(BaseDemo):
    def __init__(self, args):
        super(Demo, self).__init__(args)
        self.model, self.model_gt = self.init_model(self.data.m_kernel)
        self.visualizer = Visualizer(args, self.data.reverse_m_dict)

    def init_model(self, m_kernel):
        self.model = Net(self.im_size, self.im_size, self.im_channel,
                         self.num_frame, m_kernel.shape[1], self.m_range,
                         m_kernel)
        self.model_gt = GtNet(self.im_size, self.im_size, self.im_channel,
                              self.num_frame, m_kernel.shape[1], self.m_range,
                              m_kernel)
        if torch.cuda.is_available():
            # model = torch.nn.DataParallel(model).cuda()
            self.model = self.model.cuda()
            self.model_gt = self.model_gt.cuda()
        if self.init_model_path is not '':
            self.model.load_state_dict(torch.load(self.init_model_path))
        return self.model, self.model_gt

    def train(self):
        optimizer = optim.Adam(self.model.parameters(), lr=self.learning_rate)
        base_loss, train_loss = [], []
        for epoch in range(self.train_epoch):
            optimizer.zero_grad()
            if self.data.name in ['box', 'mnist', 'box_complex']:
                im, _, _, _ = self.data.get_next_batch(self.data.train_images)
            elif self.data.name in ['box2', 'mnist2']:
                im, _, _ = self.data.get_next_batch(self.data.train_images)
            elif self.data.name in ['robot64', 'mpii64', 'nyuv2']:
                im = self.data.get_next_batch(self.data.train_meta)
            else:
                logging.error('%s data not supported' % self.data.name)
                sys.exit()
            im_input = im[:, :-1, :, :, :].reshape(self.batch_size, -1,
                                                   self.im_size, self.im_size)
            im_output = im[:, -1, :, :, :]
            im_input = Variable(torch.from_numpy(im_input).float())
            im_output = Variable(torch.from_numpy(im_output).float())
            if torch.cuda.is_available():
                im_input, im_output = im_input.cuda(), im_output.cuda()
            im_pred, m_mask, o_mask, appear = self.model(im_input, im_output)
            im_diff = (1 - appear) * (im_pred - im_output)
            im_diff = im_diff / (
                1 - appear).sum(3).sum(2).unsqueeze(2).unsqueeze(3)
            loss = torch.abs(im_diff).sum() * im_diff.size(2) * im_diff.size(3)
            loss.backward()
            optimizer.step()

            train_loss.append(loss.data[0])
            if len(train_loss) > 100:
                train_loss.pop(0)
            ave_train_loss = sum(train_loss) / float(len(train_loss))
            base_loss.append(
                torch.abs(im_input[:, -self.im_channel:, :, :] -
                          im_output).sum().data[0])
            if len(base_loss) > 100:
                base_loss.pop(0)
            ave_base_loss = sum(base_loss) / float(len(base_loss))
            logging.info(
                'epoch %d, train loss: %.2f, average train loss: %.2f, base loss: %.2f',
                epoch, loss.data[0], ave_train_loss, ave_base_loss)
            if (epoch + 1) % self.test_interval == 0:
                logging.info('epoch %d, testing', epoch)
                self.validate()

    def test(self):
        base_loss, test_loss = [], []
        test_epe = []
        for epoch in range(self.test_epoch):
            if self.data.name in ['box', 'mnist', 'box_complex']:
                im, motion, _, _ = self.data.get_next_batch(
                    self.data.test_images)
            elif self.data.name in ['box2', 'mnist2']:
                im, motion, _ = self.data.get_next_batch(self.data.test_images)
            elif self.data.name in ['robot64', 'mpii64', 'nyuv2']:
                im, motion = self.data.get_next_batch(
                    self.data.test_meta), None
            elif self.data.name in ['mpii64_sample']:
                im, motion = self.data.get_next_batch(
                    self.data.test_meta), None
                im = im[:, -self.num_frame:, :, :, :]
            else:
                logging.error('%s data not supported' % self.data.name)
                sys.exit()
            im_input = im[:, :-1, :, :, :].reshape(self.batch_size, -1,
                                                   self.im_size, self.im_size)
            im_output = im[:, -1, :, :, :]
            im_input = Variable(torch.from_numpy(im_input).float())
            im_output = Variable(torch.from_numpy(im_output).float())
            if torch.cuda.is_available():
                im_input, im_output = im_input.cuda(), im_output.cuda()
            im_pred, m_mask, o_mask, appear = self.model(im_input, im_output)
            im_diff = (1 - appear) * (im_pred - im_output)
            im_diff = im_diff / (
                1 - appear).sum(3).sum(2).unsqueeze(2).unsqueeze(3)
            loss = torch.abs(im_diff).sum() * im_diff.size(2) * im_diff.size(3)

            test_loss.append(loss.data[0])
            base_loss.append(
                torch.abs(im_input[:, -self.im_channel:, :, :] -
                          im_output).sum().data[0])
            flow = self.motion2flow(m_mask)

            if motion is None:
                gt_motion = None
            else:
                gt_motion = motion[:, -2, :, :, :]
                gt_motion = Variable(torch.from_numpy(gt_motion).float())
                if torch.cuda.is_available():
                    gt_motion = gt_motion.cuda()
                epe = (flow - gt_motion) * (flow - gt_motion)
                epe = torch.sqrt(epe.sum(1))
                epe = epe.sum() / epe.numel()
                test_epe.append(epe.cpu().data[0])
            if self.display:
                self.visualizer.visualize_result(im_input, im_output, im_pred,
                                                 flow, gt_motion, appear,
                                                 'test_%d.png' % epoch)
            if self.display_all:
                for i in range(self.batch_size):
                    self.visualizer.visualize_result(im_input, im_output,
                                                     im_pred, flow, gt_motion,
                                                     appear, 'test_%d.png' % i,
                                                     i)
        test_loss = numpy.mean(numpy.asarray(test_loss))
        base_loss = numpy.mean(numpy.asarray(base_loss))
        improve_loss = base_loss - test_loss
        improve_percent = improve_loss / (base_loss + 1e-5)
        logging.info('average test loss: %.2f, base loss: %.2f', test_loss,
                     base_loss)
        logging.info('improve_loss: %.2f, improve_percent: %.2f', improve_loss,
                     improve_percent)
        if gt_motion is not None:
            test_epe = numpy.mean(numpy.asarray(test_epe))
            logging.info('average test endpoint error: %.2f', test_epe)
        return improve_percent

    def test_gt(self):
        base_loss, test_loss = [], []
        test_epe = []
        for epoch in range(self.test_epoch):
            if self.data.name in ['box', 'mnist', 'box_complex']:
                im, motion, motion_label, _ = self.data.get_next_batch(
                    self.data.test_images)
                gt_motion_label = motion_label[:, -2, :, :, :]
                gt_motion_label = Variable(torch.from_numpy(gt_motion_label))
                if torch.cuda.is_available():
                    gt_motion_label = gt_motion_label.cuda()
            elif self.data.name in ['box2', 'mnist2']:
                im, motion, _ = self.data.get_next_batch(self.data.test_images)
            else:
                logging.error('%s data not supported in test_gt' %
                              self.data.name)
                sys.exit()
            im_input = im[:, :-1, :, :, :].reshape(self.batch_size, -1,
                                                   self.im_size, self.im_size)
            im_output = im[:, -1, :, :, :]
            gt_motion = motion[:, -2, :, :, :]
            im_input = Variable(torch.from_numpy(im_input).float())
            im_output = Variable(torch.from_numpy(im_output).float())
            gt_motion = Variable(torch.from_numpy(gt_motion).float())
            if torch.cuda.is_available():
                im_input, im_output = im_input.cuda(), im_output.cuda()
                gt_motion = gt_motion.cuda()
            if self.data.name in ['box', 'mnist', 'box_complex']:
                im_pred, m_mask, o_mask, appear = self.model_gt(
                    im_input, im_output, gt_motion_label, 'label')
            elif self.data.name in ['box2', 'mnist2']:
                im_pred, m_mask, o_mask, appear = self.model_gt(
                    im_input, im_output, gt_motion)
            im_diff = (1 - appear) * (im_pred - im_output)
            im_diff = im_diff / (
                1 - appear).sum(3).sum(2).unsqueeze(2).unsqueeze(3)
            loss = torch.abs(im_diff).sum() * im_diff.size(2) * im_diff.size(3)

            test_loss.append(loss.data[0])
            base_loss.append(
                torch.abs(im_input[:, -self.im_channel:, :, :] -
                          im_output).sum().data[0])
            flow = self.motion2flow(m_mask)
            epe = (flow - gt_motion) * (flow - gt_motion)
            epe = torch.sqrt(epe.sum(1))
            epe = epe.sum() / epe.numel()
            test_epe.append(epe.cpu().data[0])
            if self.display:
                self.visualizer.visualize_result(im_input, im_output, im_pred,
                                                 flow, gt_motion, appear,
                                                 'test_gt.png')
            if self.display_all:
                for i in range(self.batch_size):
                    self.visualizer.visualize_result(im_input, im_output,
                                                     im_pred, flow, gt_motion,
                                                     appear,
                                                     'test_gt_%d.png' % i, i)
        test_loss = numpy.mean(numpy.asarray(test_loss))
        base_loss = numpy.mean(numpy.asarray(base_loss))
        improve_loss = base_loss - test_loss
        improve_percent = improve_loss / (base_loss + 1e-5)
        logging.info('average ground truth test loss: %.2f, base loss: %.2f',
                     test_loss, base_loss)
        logging.info('improve_loss: %.2f, improve_percent: %.2f', improve_loss,
                     improve_percent)
        test_epe = numpy.mean(numpy.asarray(test_epe))
        logging.info('average ground truth test endpoint error: %.2f',
                     test_epe)
        return improve_percent
Exemple #34
0
class Demo(BaseDemo):
    def __init__(self, args):
        super(Demo, self).__init__(args)
        if args.data == 'box':
            self.data = BoxDataBidirect(args)
        elif args.data == 'mnist':
            self.data = MnistDataBidirect(args)
        self.model, self.model_gt = self.init_model(self.data.m_kernel)
        self.visualizer = Visualizer(args, self.data.reverse_m_dict)
        self.num_inputs = (self.num_frame - 1) / 2

    def init_model(self, m_kernel):
        num_inputs = (self.num_frame - 1) / 2
        self.model = Net(self.im_size, self.im_size, 3, num_inputs,
                         m_kernel.shape[1], self.m_range, m_kernel)
        self.model_gt = GtNet(self.im_size, self.im_size, 3, num_inputs,
                              m_kernel.shape[1], self.m_range, m_kernel)
        if torch.cuda.is_available():
            # model = torch.nn.DataParallel(model).cuda()
            self.model = self.model.cuda()
            self.model_gt = self.model_gt.cuda()
        if self.init_model_path is not '':
            self.model.load_state_dict(torch.load(self.init_model_path))
        return self.model, self.model_gt

    def train_unsupervised(self):
        optimizer = optim.Adam(self.model.parameters(), lr=self.learning_rate)
        base_loss, train_loss = [], []
        for epoch in range(self.train_epoch):
            optimizer.zero_grad()
            im, motion, motion_r = self.data.get_next_batch(
                self.data.train_images)
            im_input_f = im[:, :self.num_inputs, :, :, :].reshape(
                self.batch_size, -1, self.im_size, self.im_size)
            im_input_b = im[:, :self.num_inputs:-1, :, :, :].reshape(
                self.batch_size, -1, self.im_size, self.im_size)
            im_output = im[:, self.num_inputs, :, :, :]
            im_input_f = Variable(torch.from_numpy(im_input_f).float())
            im_input_b = Variable(torch.from_numpy(im_input_b).float())
            im_output = Variable(torch.from_numpy(im_output).float())
            if torch.cuda.is_available():
                im_input_f, im_input_b = im_input_f.cuda(), im_input_b.cuda()
                im_output = im_output.cuda()
            im_pred, m_mask_f, disappear_f, attn_f, m_mask_b, disappear_b, attn_b = \
                self.model(im_input_f, im_input_b)
            im_diff = im_pred - im_output
            loss = torch.abs(im_diff).sum()
            loss.backward()
            optimizer.step()

            train_loss.append(loss.data[0])
            if len(train_loss) > 100:
                train_loss.pop(0)
            ave_train_loss = sum(train_loss) / float(len(train_loss))
            base_loss.append(
                torch.abs(im_input_f[:, -3:, :, :] - im_output).sum().data[0])
            base_loss.append(
                torch.abs(im_input_b[:, -3:, :, :] - im_output).sum().data[0])
            if len(base_loss) > 100:
                base_loss.pop(0)
            ave_base_loss = sum(base_loss) / float(len(base_loss))
            logging.info(
                'epoch %d, train loss: %.2f, average train loss: %.2f, base loss: %.2f',
                epoch, loss.data[0], ave_train_loss, ave_base_loss)
            if (epoch + 1) % self.test_interval == 0:
                logging.info('epoch %d, testing', epoch)
                self.validate()

    def test_unsupervised(self):
        base_loss, test_loss = [], []
        test_accuracy = []
        for epoch in range(self.test_epoch):
            im, motion, motion_r = self.data.get_next_batch(
                self.data.test_images)
            im_input_f = im[:, :self.num_inputs, :, :, :].reshape(
                self.batch_size, -1, self.im_size, self.im_size)
            im_input_b = im[:, :self.num_inputs:-1, :, :, :].reshape(
                self.batch_size, -1, self.im_size, self.im_size)
            im_output = im[:, self.num_inputs, :, :, :]
            gt_motion_f = motion[:, self.num_inputs - 1, :, :, :]
            gt_motion_b = motion_r[:, self.num_inputs + 1, :, :, :]
            im_input_f = Variable(torch.from_numpy(im_input_f).float())
            im_input_b = Variable(torch.from_numpy(im_input_b).float())
            im_output = Variable(torch.from_numpy(im_output).float())
            gt_motion_f = Variable(torch.from_numpy(gt_motion_f))
            gt_motion_b = Variable(torch.from_numpy(gt_motion_b))
            if torch.cuda.is_available():
                im_input_f, im_input_b = im_input_f.cuda(), im_input_b.cuda()
                im_output = im_output.cuda()
                gt_motion_f, gt_motion_b = gt_motion_f.cuda(
                ), gt_motion_b.cuda()
            im_pred, m_mask_f, disappear_f, attn_f, m_mask_b, disappear_b, attn_b = \
                self.model(im_input_f, im_input_b)
            im_diff = im_pred - im_output
            loss = torch.abs(im_diff).sum()

            test_loss.append(loss.data[0])
            base_loss.append(
                torch.abs(im_input_f[:, -3:, :, :] - im_output).sum().data[0])
            base_loss.append(
                torch.abs(im_input_b[:, -3:, :, :] - im_output).sum().data[0])
            pred_motion_f = m_mask_f.max(1)[1]
            pred_motion_b = m_mask_b.max(1)[1]
            accuracy_f = pred_motion_f.eq(
                gt_motion_f).float().sum() / gt_motion_f.numel()
            accuracy_b = pred_motion_b.eq(
                gt_motion_b).float().sum() / gt_motion_b.numel()
            test_accuracy.append(accuracy_f.cpu().data[0])
            test_accuracy.append(accuracy_b.cpu().data[0])
            if self.display:
                flow_f = self.motion2flow(m_mask_f)
                flow_b = self.motion2flow(m_mask_b)
                self.visualizer.visualize_result_bidirect(
                    im_input_f, im_input_b, im_output, im_pred, flow_f,
                    gt_motion_f, disappear_f, attn_f, flow_b, gt_motion_b,
                    disappear_b, attn_b, 'test_%d.png' % epoch)
        test_loss = numpy.mean(numpy.asarray(test_loss))
        base_loss = numpy.mean(numpy.asarray(base_loss))
        improve_loss = base_loss - test_loss
        improve_percent = improve_loss / (base_loss + 1e-5)
        logging.info('average test loss: %.2f, base loss: %.2f', test_loss,
                     base_loss)
        logging.info('improve_loss: %.2f, improve_percent: %.2f', improve_loss,
                     improve_percent)
        test_accuracy = numpy.mean(numpy.asarray(test_accuracy))
        logging.info('average test accuracy: %.2f', test_accuracy)
        return improve_percent

    def test_gt_unsupervised(self):
        base_loss, test_loss = [], []
        test_accuracy = []
        for epoch in range(self.test_epoch):
            im, motion, motion_r = self.data.get_next_batch(
                self.data.test_images)
            im_input_f = im[:, :self.num_inputs, :, :, :].reshape(
                self.batch_size, -1, self.im_size, self.im_size)
            im_input_b = im[:, :self.num_inputs:-1, :, :, :].reshape(
                self.batch_size, -1, self.im_size, self.im_size)
            im_output = im[:, self.num_inputs, :, :, :]
            gt_motion_f = motion[:, self.num_inputs - 1, :, :, :]
            gt_motion_b = motion_r[:, self.num_inputs + 1, :, :, :]
            im_input_f = Variable(torch.from_numpy(im_input_f).float())
            im_input_b = Variable(torch.from_numpy(im_input_b).float())
            im_output = Variable(torch.from_numpy(im_output).float())
            gt_motion_f = Variable(torch.from_numpy(gt_motion_f))
            gt_motion_b = Variable(torch.from_numpy(gt_motion_b))
            if torch.cuda.is_available():
                im_input_f, im_input_b = im_input_f.cuda(), im_input_b.cuda()
                im_output = im_output.cuda()
                gt_motion_f, gt_motion_b = gt_motion_f.cuda(
                ), gt_motion_b.cuda()
            im_pred, m_mask_f, disappear_f, attn_f, m_mask_b, disappear_b, attn_b = \
                self.model_gt(im_input_f, im_input_b, gt_motion_f, gt_motion_b)
            im_diff = im_pred - im_output
            loss = torch.abs(im_diff).sum()

            test_loss.append(loss.data[0])
            base_loss.append(
                torch.abs(im_input_f[:, -3:, :, :] - im_output).sum().data[0])
            base_loss.append(
                torch.abs(im_input_b[:, -3:, :, :] - im_output).sum().data[0])
            pred_motion_f = m_mask_f.max(1)[1]
            pred_motion_b = m_mask_b.max(1)[1]
            accuracy_f = pred_motion_f.eq(
                gt_motion_f).float().sum() / gt_motion_f.numel()
            accuracy_b = pred_motion_b.eq(
                gt_motion_b).float().sum() / gt_motion_b.numel()
            test_accuracy.append(accuracy_f.cpu().data[0])
            test_accuracy.append(accuracy_b.cpu().data[0])
            if self.display:
                flow_f = self.motion2flow(m_mask_f)
                flow_b = self.motion2flow(m_mask_b)
                self.visualizer.visualize_result_bidirect(
                    im_input_f, im_input_b, im_output, im_pred, flow_f,
                    gt_motion_f, disappear_f, attn_f, flow_b, gt_motion_b,
                    disappear_b, attn_b, 'test_gt.png')
        test_loss = numpy.mean(numpy.asarray(test_loss))
        base_loss = numpy.mean(numpy.asarray(base_loss))
        improve_loss = base_loss - test_loss
        improve_percent = improve_loss / (base_loss + 1e-5)
        logging.info('average groundtruth test loss: %.2f, base loss: %.2f',
                     test_loss, base_loss)
        logging.info('improve_loss: %.2f, improve_percent: %.2f', improve_loss,
                     improve_percent)
        test_accuracy = numpy.mean(numpy.asarray(test_accuracy))
        logging.info('average groundtruth test accuracy: %.2f', test_accuracy)
        return improve_percent

    def motion2flow(self, m_mask):
        reverse_m_dict = self.data.reverse_m_dict
        [batch_size, num_class, height, width] = m_mask.size()
        kernel_x = Variable(torch.zeros(batch_size, num_class, height, width))
        kernel_y = Variable(torch.zeros(batch_size, num_class, height, width))
        if torch.cuda.is_available():
            kernel_x = kernel_x.cuda()
            kernel_y = kernel_y.cuda()
        for i in range(num_class):
            (m_x, m_y) = reverse_m_dict[i]
            kernel_x[:, i, :, :] = m_x
            kernel_y[:, i, :, :] = m_y
        flow = Variable(torch.zeros(batch_size, 2, height, width))
        flow[:, 0, :, :] = (m_mask * kernel_x).sum(1)
        flow[:, 1, :, :] = (m_mask * kernel_y).sum(1)
        return flow
Exemple #35
0
# Scaling the data
X_train, X_test = preprocess.scaling(X_train,
                                     X_test,
                                     scale_type='Standard Scaler')

# ----------------------------Classifying the data----------------------------
if method_identifier == 1:

    from classifier import Classifier

    classifier = Classifier(algorithm_name)
    y_predicted = classifier.classify(X_train, y_train, X_test, y_test)
    classifier_accuracy = classifier.get_accuracy(y_test, y_predicted)

    # Visualizing the results
    visualizer = Visualizer()
    visualizer.plot_classifier_regressor(y_test, y_predicted,
                                         method_identifier)

    print('The accuracy is: ' + str(classifier_accuracy) + ' %')
    print(algorithm_name)

# ---------------------Applying Regression to the data--------------------------
elif method_identifier == 2:

    from regressor import Regressor

    regressor = Regressor(algorithm_name)
    y_predicted = regressor.predict(X_train, y_train, X_test)
    regressor_score = regressor.get_score(y_test, y_predicted)
Exemple #36
0
    os.makedirs(opt['checkpoints_path'])
except OSError:
    pass

if opt['manual_seed'] is None:
    opt['manual_seed'] = random.randint(1, 10000)
print("Random Seed: ", opt['manual_seed'])
random.seed(opt['manual_seed'])
torch.manual_seed(opt['manual_seed'])
if opt['cuda']:
    torch.cuda.manual_seed_all(opt['manual_seed'])
    
if torch.cuda.is_available() and not opt['cuda']:
    print("WARNING: You have a CUDA device, so you should probably run with --cuda")

vis = Visualizer(opt['checkpoints_path'], opt['visdom_port'])

def create_df(dataroot, size=-1):
    df_paths = glob(dataroot)
    df = pd.DataFrame({'path': df_paths})
    df['label'] = df.path.apply(lambda x: int(x.split('/')[-1].split('_')[0]))
    return df[:size]

if not opt['market']:
    df_train = create_df(os.path.join(opt['dataroot'], '*.jpg'))
else:
    def create_market_df(x):
        df = create_df(os.path.join(opt['dataroot'], paths[x]))
        df['camera'] = df.path.apply(lambda x: int(x.split('/')[-1].split('_')[1].split('s')[0].split('c')[1]))
        df['name'] = df.path.apply(lambda x: x.split('/')[-1])
        return df
Exemple #37
0
☼☼☼   ╬╬╬╬╬            ╠☼╬╬╬   ☼☼☼
☼          »  •                  ☼
☼            ╞╬╬  ╬╬╡            ☼
☼  ╦╬╬▼ ╬╬╬☼☼╬╬╬  ╬╬╬  ╬╬╬  ╬╬╬  ☼
☼  ╬╬╬  ╬╬╬  ╬╬╬  ╬╬╬  ╬╬╣  ╬╬╬  ☼
☼  ☼╬╬           ╠╬☼☼  ╬╬╬╦╬╬╬╬  ☼
☼  ☼╬╬       ╬╬╬ ╬╬☼   ╬╬╬ ╩╬╬╬ ╬☼
☼  ☼╬╬  ╬╬╬☼☼╬╬╬ ˄╬☼☼  ╬╬╬  ╬╬╬  ☼
☼  ☼☼╬  ╬╬╬  ╬╬╬  ╬╬╬  ╬╬╬  ╬╬╬╬ ☼
☼   ╬╬  ╬╬╬  ╬╬╬  ╬╬╬  ╨╬╬╬ ╬╬╬  ☼
☼    ╬  ╩╬╬  ╬╬╩    ╞  ╬ ╬ •╬╬╬ ╬☼
☼  ╬╬╬   ╡                 •╞╬╬  ☼
☼  ☼☼╬   ╣     ˅             ╬╬  ☼
☼? ☼╬╬        ☼☼☼☼☼☼      ╬ └╬╬  ☼
☼  ☼╬╬   ┐     ˄   ☼      ˂  ╬╬  ☼
☼  ☼     ╬    ╬    ☼         │   ☼
☼        ╣         ☼˅   ┐        ☼
☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼☼
"""

if __name__ == '__main__':
    try:
        from visualizer import Visualizer
        visualizer = Visualizer()
    except Exception:
        from unittest.mock import Mock
        visualizer = Mock()

    player = Player(visualizer)
    response = player.turn(board.replace('\n', ''))
class BaseModel():
    """ Base Model for ganomaly
    """
    def __init__(self, opt, data):
        ##
        # Seed for deterministic behavior
        self.seed(opt.manualseed)

        # Initalize variables.
        self.opt = opt
        self.visualizer = Visualizer(opt)
        self.data = data
        self.trn_dir = os.path.join(self.opt.outfolder, self.opt.name, 'train')
        self.tst_dir = os.path.join(self.opt.outfolder, self.opt.name, 'test')
        self.device = torch.device(
            "cuda:0" if self.opt.device != 'cpu' else "cpu")

    ##
    def seed(self, seed_value):
        """ Seed 

        Arguments:
            seed_value {int} -- [description]
        """
        # Check if seed is default value
        if seed_value == -1:
            return

        # Otherwise seed all functionality
        import random
        random.seed(seed_value)
        torch.manual_seed(seed_value)
        torch.cuda.manual_seed_all(seed_value)
        np.random.seed(seed_value)
        torch.backends.cudnn.deterministic = True

    ##
    def set_input(self, input: torch.Tensor, noise: bool = False):
        """ Set input and ground truth

        Args:
            input (FloatTensor): Input data for batch i.
        """
        with torch.no_grad():
            self.input.resize_(input[0].size()).copy_(input[0])
            self.gt.resize_(input[1].size()).copy_(input[1])
            self.label.resize_(input[1].size())

            # Add noise to the input.
            if noise: self.noise.data.copy_(torch.randn(self.noise.size()))

            # Copy the first batch as the fixed input.
            if self.total_steps == self.opt.batchsize:
                self.fixed_input.resize_(input[0].size()).copy_(input[0])

    ##
    def get_errors(self):
        """ Get netD and netG errors.

        Returns:
            [OrderedDict]: Dictionary containing errors.
        """
        errors = OrderedDict([('err_d', self.err_d.item()),
                              ('err_g', self.err_g.item()),
                              ('err_g_adv', self.err_g_adv.item()),
                              ('err_g_con', self.err_g_con.item()),
                              ('err_g_lat', self.err_g_lat.item())])

        return errors

    ##
    def reinit_d(self):
        """ Initialize the weights of netD
        """
        self.netd.apply(weights_init)
        print('Reloading d net')

    ##
    def get_current_images(self):
        """ Returns current images.

        Returns:
            [reals, fakes, fixed]
        """

        reals = self.input.data
        fakes = self.fake.data
        fixed = self.netg(self.fixed_input)[0].data

        return reals, fakes, fixed

    ##
    def save_weights(self, epoch: int, is_best: bool = False):
        """Save netG and netD weights for the current epoch.

        Args:
            epoch ([int]): Current epoch number.
        """

        weight_dir = os.path.join(self.opt.outfolder, self.opt.name, 'train',
                                  'weights')
        if not os.path.exists(weight_dir):
            os.makedirs(weight_dir)

        if is_best:
            torch.save({
                'epoch': epoch,
                'state_dict': self.netg.state_dict()
            }, f'{weight_dir}/netG_best.pth')
            torch.save({
                'epoch': epoch,
                'state_dict': self.netd.state_dict()
            }, f'{weight_dir}/netD_best.pth')
        else:
            torch.save({
                'epoch': epoch,
                'state_dict': self.netd.state_dict()
            }, f"{weight_dir}/netD_{epoch}.pth")
            torch.save({
                'epoch': epoch,
                'state_dict': self.netg.state_dict()
            }, f"{weight_dir}/netG_{epoch}.pth")

    def load_weights(self, epoch=None, is_best: bool = False, path=None):
        """ Load pre-trained weights of NetG and NetD

        Keyword Arguments:
            epoch {int}     -- Epoch to be loaded  (default: {None})
            is_best {bool}  -- Load the best epoch (default: {False})
            path {str}      -- Path to weight file (default: {None})

        Raises:
            Exception -- [description]
            IOError -- [description]
        """

        if epoch is None and is_best is False:
            raise Exception(
                'Please provide epoch to be loaded or choose the best epoch.')

        if is_best:
            fname_g = f"netG_best.pth"
            fname_d = f"netD_best.pth"
        else:
            fname_g = f"netG_{epoch}.pth"
            fname_d = f"netD_{epoch}.pth"

        if path is None:
            path_g = f"./output/{self.name}/{self.opt.dataset}/train/weights/{fname_g}"
            path_d = f"./output/{self.name}/{self.opt.dataset}/train/weights/{fname_d}"

        # Load the weights of netg and netd.
        print('>> Loading weights...')
        weights_g = torch.load(path_g)['state_dict']
        weights_d = torch.load(path_d)['state_dict']
        try:
            self.netg.load_state_dict(weights_g)
            self.netd.load_state_dict(weights_d)
        except IOError:
            raise IOError("netG weights not found")
        print('   Done.')

    ##
    def train_one_epoch(self):
        """ Train the model for one epoch.
        """

        self.netg.train()
        self.netd.train()
        epoch_iter = 0
        for data in tqdm(self.data.train,
                         leave=False,
                         total=len(self.data.train)):
            self.total_steps += self.opt.batchsize
            epoch_iter += self.opt.batchsize

            self.set_input(data)
            self.optimize_params()

            if self.total_steps % self.opt.print_freq == 0:
                errors = self.get_errors()
                if self.opt.display:
                    counter_ratio = float(epoch_iter) / len(
                        self.data.train.dataset)
                    self.visualizer.plot_current_errors(
                        self.epoch, counter_ratio, errors)

            if self.total_steps % self.opt.save_image_freq == 0:
                reals, fakes, fixed = self.get_current_images()
                self.visualizer.save_current_images(self.epoch, reals, fakes,
                                                    fixed)
                if self.opt.display:
                    self.visualizer.display_current_images(reals, fakes, fixed)

        print(">> Training model %s. Epoch %d/%d" %
              (self.name, self.epoch + 1, self.opt.niter))

    ##
    def train(self):
        """ Train the model
        """

        ##
        # TRAIN
        self.total_steps = 0
        best_auc = 0

        # Train for niter epochs.
        print(
            f">> Training {self.name} on {self.opt.dataset} to detect {self.opt.abnormal_class}"
        )
        for self.epoch in range(self.opt.iter, self.opt.niter):
            self.train_one_epoch()
            res = self.test()
            if res['AUC'] > best_auc:
                best_auc = res['AUC']
                self.save_weights(self.epoch)
            self.visualizer.print_current_performance(res, best_auc)

            loss = 'gloss: ' + str(self.err_g.item()) + ' dloss:' + str(
                self.err_d.item())
            print(loss)
            self.visualizer.write_to_log_file(loss)
        print(">> Training model %s.[Done]" % self.name)

    ##
    def test(self):
        """ Test GANomaly model.

        Args:
            data ([type]): Dataloader for the test set

        Raises:
            IOError: Model weights not found.
        """
        with torch.no_grad():
            # Load the weights of netg and netd.
            if self.opt.load_weights:
                path = "./output/{}/{}/train/weights/netG.pth".format(
                    self.name.lower(), self.opt.dataset)
                pretrained_dict = torch.load(path)['state_dict']

                try:
                    self.netg.load_state_dict(pretrained_dict)
                except IOError:
                    raise IOError("netG weights not found")
                print('   Loaded weights.')

            self.opt.phase = 'test'

            # Create big error tensor for the test set.
            self.an_scores = torch.zeros(size=(len(self.data.valid.dataset), ),
                                         dtype=torch.float32,
                                         device=self.device)
            self.gt_labels = torch.zeros(size=(len(self.data.valid.dataset), ),
                                         dtype=torch.long,
                                         device=self.device)
            self.latent_i = torch.zeros(size=(len(self.data.valid.dataset),
                                              self.opt.nz),
                                        dtype=torch.float32,
                                        device=self.device)
            self.latent_o = torch.zeros(size=(len(self.data.valid.dataset),
                                              self.opt.nz),
                                        dtype=torch.float32,
                                        device=self.device)

            # print("   Testing model %s." % self.name)
            self.times = []
            self.total_steps = 0
            epoch_iter = 0
            for i, data in enumerate(self.data.valid, 0):

                self.total_steps += self.opt.batchsize
                epoch_iter += self.opt.batchsize
                time_i = time.time()
                self.set_input(data)
                self.fake, latent_i, latent_o = self.netg(self.input)

                error = torch.mean(torch.pow((latent_i - latent_o), 2), dim=1)
                time_o = time.time()

                self.an_scores[i * self.opt.batchsize:i * self.opt.batchsize +
                               error.size(0)] = error.reshape(error.size(0))
                self.gt_labels[i * self.opt.batchsize:i * self.opt.batchsize +
                               error.size(0)] = self.gt.reshape(error.size(0))
                self.latent_i[i * self.opt.batchsize:i * self.opt.batchsize +
                              error.size(0), :] = latent_i.reshape(
                                  error.size(0), self.opt.nz)
                self.latent_o[i * self.opt.batchsize:i * self.opt.batchsize +
                              error.size(0), :] = latent_o.reshape(
                                  error.size(0), self.opt.nz)

                self.times.append(time_o - time_i)

                # Save test images.
                if self.opt.save_test_images:
                    dst = os.path.join(self.opt.outfolder, self.opt.name,
                                       'test', 'images')
                    if not os.path.isdir(dst):
                        os.makedirs(dst)
                        real, fake, _ = self.get_current_images()
                        vutils.save_image(real,
                                          '%s/real_%03d.eps' % (dst, i + 1),
                                          normalize=True)
                        vutils.save_image(fake,
                                          '%s/fake_%03d.eps' % (dst, i + 1),
                                          normalize=True)

                        # Measure inference time.
                        self.times = np.array(self.times)
                        self.times = np.mean(self.times[:100] * 1000)

                        # Scale error vector between [0, 1]
            self.an_scores = (self.an_scores - torch.min(self.an_scores)) / (
                torch.max(self.an_scores) - torch.min(self.an_scores))
            auc = evaluate(self.gt_labels,
                           self.an_scores,
                           metric=self.opt.metric)
            performance = OrderedDict([('Avg Run Time (ms/batch)', self.times),
                                       ('AUC', auc)])

            if self.opt.display_id > 0 and self.opt.phase == 'test':
                counter_ratio = float(epoch_iter) / len(
                    self.data.valid.dataset)
                self.visualizer.plot_performance(self.epoch, counter_ratio,
                                                 performance)
            return performance

        ##
        def update_learning_rate(self):
            """ Update learning rate based on the rule provided in options.
            """

            for scheduler in self.schedulers:
                scheduler.step()
            lr = self.optimizers[0].param_groups[0]['lr']
            print('   LR = %.7f' % lr)
	def __init__(self, name, master, total_var):
		self.root = LabelFrame(master, text = name)
		self.visualizer = Visualizer(name, self.root)
		self.monster = Monster(name)
		self.stats = Stat_Tracker()

		self.total_var = total_var
		self.total_var.set(total_var.get() + 1)

		self.visualizer.monster_image.grid(row = 0, column = 6, rowspan = 3)
		self.visualizer.mood_image.grid(row = 0, column = 7, rowspan = 3)

		# controls whether the button's perform their action, for use when
		# a monster is dead or a minigame is playing
		self.button_bool = True

		########## age ##################################
		self.age_label = Label(self.root, text = "Age:  ")
		self.age_label.grid(row = 0, column = 2)
		
		self.age_state = StringVar()
		self.update_age()
		Label(self.root, textvariable = self.age_state).grid(row = 0, column = 3)

		########### mood ############################################
		self.mood_label = Label(self.root, text = "Mood:  ")
		self.mood_label.grid(row = 0, column = 0)

		self.mood_state = StringVar()
		self.update_mood()
		Label(self.root, textvariable = self.mood_state).grid(row = 0, column = 1)

		######### hunger ############################################
		self.hunger_label = Label(self.root, text = "Hunger:  ")
		self.hunger_label.grid(row = 1, column = 0)

		self.hunger_state = StringVar()
		self.update_hunger()
		Label(self.root, textvariable = self.hunger_state).grid(row = 1, column = 1)

		self.hunger_button = Button(self.root, text = "Feed", command = self.feed)
		self.hunger_button.grid(row = 1, column = 2)

		######## sleepiness ############################################
		self.sleep_label = Label(self.root, text = "Sleepiness:  ")
		self.sleep_label.grid(row = 2, column = 0)

		self.sleep_state = StringVar()
		self.update_sleepiness()
		Label(self.root, textvariable = self.sleep_state).grid(row = 2, column = 1)

		self.sleep_button = Button(self.root, text = "Nap", command = self.nap)
		self.sleep_button.grid(row = 2, column = 2)

		######### boredom ############################################
		self.boredom_label = Label(self.root, text = "Boredom:  ")
		self.boredom_label.grid(row = 1, column = 3)

		self.boredom_state = StringVar()
		self.update_boredom()
		Label(self.root, textvariable = self.boredom_state).grid(row = 1, column = 4)

		self.boredom_button = Button(self.root, text = "Play", command = self.play)
		self.boredom_button.grid(row = 1, column = 5)

		######### dirtiness ############################################
		self.dirt_label = Label(self.root, text = "Dirtiness:  ")
		self.dirt_label.grid(row = 2, column = 3)

		self.dirt_state = StringVar()
		self.update_dirtiness()
		Label(self.root, textvariable = self.dirt_state).grid(row = 2, column = 4)

		self.dirt_button = Button(self.root, text = "Clean", command = self.clean)
		self.dirt_button.grid(row = 2, column = 5)
		
		self.root.pack()
Exemple #40
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--epoch_size",
                        type=int,
                        default=10,
                        help="Total training epochs")
    parser.add_argument("--episode_size",
                        type=int,
                        default=50000,
                        help="Training episodes per epoch")
    parser.add_argument("--epsilon",
                        type=float,
                        default=0.1,
                        help="Greedy Epsilon starting value")
    parser.add_argument("--gamma",
                        type=float,
                        default=0.9,
                        help="DQN gamma starting value")
    parser.add_argument("--load_model",
                        action='store_true',
                        default=False,
                        help="Load saved model")
    parser.add_argument("--test",
                        action='store_true',
                        default=False,
                        help="Testing phase")
    parser.add_argument("--nodisplay",
                        action='store_true',
                        default=False,
                        help="Show V-REP display")
    parser.add_argument("--cuda",
                        action='store_true',
                        default=False,
                        help="Use CUDA")
    parser.add_argument("--viz",
                        action='store_true',
                        default=False,
                        help="Use Visdom Visualizer")
    args = parser.parse_args()

    print("Using Parameters:\n")
    print("Epoch Size: %d \n" % args.epoch_size)
    print("Episode Size: %d \n" % args.episode_size)
    print("Epsilon: %f \n" % args.epsilon)
    print("Gamma: %f \n" % args.gamma)
    print("Testing Phase: %s \n" % str(args.test))
    print("Using CUDA: %s\n" % str(args.cuda))

    # Initialize classes
    control_quad = QuadHelper()
    dqn_quad = QuadDQN(args.cuda, args.epoch_size, args.episode_size)
    if args.viz:
        viz = Visualizer()
        main_quad = Quad(dqn_quad=dqn_quad,
                         control_quad=control_quad,
                         visualizer=viz)
    else:
        main_quad = Quad(dqn_quad=dqn_quad,
                         control_quad=control_quad,
                         visualizer=None)

    # Argument parsing
    dqn_quad.eps = args.epsilon
    dqn_quad.gamma = args.gamma
    if args.load_model:
        dqn_quad.load_wts('dqn_quad.pth')
    if args.nodisplay:
        control_quad.display_disabled = True
        main_quad.display_disabled = True

    while (vrep.simxGetConnectionId(control_quad.sim_quad.clientID) != -1):
        with open('dqn_outputs.txt', 'a') as the_file:
            log_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            the_file.write('\n************* SAVE FILE %s *************\n' %
                           log_time)

        if args.test:
            print("Testing Quadrotor")
            # Test our trained quadrotor
            main_quad.mode = 'test'
            dqn_quad.load_wts('dqn_quad.pth')
            main_quad.test_quad()
        else:
            # Train quadcopter
            epoch = 0
            while epoch < dqn_quad.epoch_size:
                main_quad.run_one_epoch(epoch)
                print("Epoch reset")
                epoch += 1
                main_quad.task_every_n_epochs(epoch)
                print('\n')
            print("Finished training")

            # Test quadrotor
            main_quad.mode = "test"
            main_quad.test_quad(dqn_quad, control_quad)

    control_quad.sim_quad.exit_sim()
    print("V-REP Exited...")
Exemple #41
0
class Driver(object):
    def __init__(self, vis=False):
        """
        Creates a new Driver.

        Args:
            vis: Boolean. Whether or not to show visualization of the simulation
                runs using matplotlib.

        Returns:
            A new Driver instance.

        """

        self.vis = vis

        # If visualization is selected, show it.
        if self.vis:
            series = ('Population Count',
                      'Adult Count',
                      'Caloric Requirements (Mcal)',
                      'Produced Food (Mcal)',
                      'Air (kg O2)',
                      'Power Consumption (kWh)')
            self.vis = Visualizer(series=series)

    def drive(self,
              max_iterations=500,
              random_seed=0,
              initial_pop=50):
        """
        Args:
            max_iterations: Integer. The maximum number of iterations the
                simulation should run.
            random_seed: Integer. Seed for the random number generator.
            initial_pop: Integer. The initial population for the population.

        Returns:
            None.
        """

        # Dictionary to keep track of results
        self.results = defaultdict(list)

        # Seed the random number generator.
        random.seed(random_seed)

        # Create a dictionary that will hold the number of newborns that will
        # be added to the simulation.
        people_born = { k: 0 for k in range(9) }

        # Set the maximum number of iterations that the simulation will run.
        max_sim_time = max_iterations

        # Initialize a population.
        population = Population()

        # Initialize an air instance for modeling oxygen consumption.
        air = Air(population)

        # Initialize a power instance for modeling power consumption.
        power = Power(population)

        # Initial Iteration
        cur_sim_time = 0
        start = time.time()

        # Add initial population
        initial_ages = [10, 18, 20, 25, 28, 30, 32, 35, 40, 45, 50, 55]
        for add_count in range (initial_pop):
            init_age = cur_sim_time - initial_ages[add_count % len(initial_ages)]*12.0
            population.add_person(Person(init_age, population.get_rand_death_time(cur_sim_time), random.random()))
        print 'added', people_born.get(cur_sim_time % 9, 0), 'people in', time.time()-start

        start = time.time()
        total_kcal = population.kcal_requirements(cur_sim_time)
        print 'completed total kcal in:', time.time() - start

        # Create a facility for population
        # Crop area of 25000 feeds about 30 people
        facility = Facility(30000.0/30.0*(initial_pop+30), initial_pop+30)

        # Food initialization
        food = Food(facility, total_kcal)

        # Create a disaster object for the population - this models uncertainty
        # events that may adversely affect the population & food
        disaster = Disaster(population, food)

        # Write initial loop results
        self._write_results(population=population.num_people(),
                            food=food.produced_food,
                            kcals=total_kcal,
                            facility_crop=facility.crop_area,
                            facility_personnel=facility.personnel_capacity,
                            air=air.oxygen_consumed(),
                            power=power.power_consumed())

        # Main iteration loop
        for cur_sim_time in range(1, max_sim_time):
            print 'current sim time:', cur_sim_time
            start = time.time()

            # Diasters
            if random.random() <= 0.01:
                #ratio = random.random()/10.0
                #disaster.create_disaster(ratio)
                #print 'DISASTER killed', ratio, 'in:', time.time() - start
                death_from_disaster = random.randint(1,20)
                disaster.create_disaster(death_from_disaster)
                print 'DISASTER killed', death_from_disaster, 'people and', (death_from_disaster+10)*2500.0, 'food in:', time.time() - start
                start = time.time()

            # If enough food, expand facility. Assume 3 month build time
            if food.remaining_food > 2500*10 and (facility.personnel_capacity - population.num_people()) <= 10:
                facility.start_pod_construction(cur_sim_time, 3)
                facility.add_pod(cur_sim_time)

            # Adding newborns
            born_count = 0
            for add_count in range (people_born.get(cur_sim_time % 9, 0)):
                if population.num_people() < facility.personnel_capacity:
                    population.add_person(Person(cur_sim_time, population.get_rand_death_time(cur_sim_time), random.random()))
                    born_count += 1
            print 'added', born_count, 'people in', time.time()-start

            # Removing the dead
            start = time.time()
            people_to_kill = len(population.death_dict.get(cur_sim_time, []))
            population.remove_dead(cur_sim_time)
            print 'removed', people_to_kill, 'people in:', time.time() - start

            # Calculating total kcal
            start = time.time()
            total_kcal = population.kcal_requirements(cur_sim_time)
            print 'completed total kcal in:', time.time() - start

            # Food consumption
            food_delta = food.update_food(total_kcal)
            print 'produced food = ', food.produced_food, '; remaining food = ', food.remaining_food
            # if not enough food
            if food_delta < 0:
                # people who are unfed will die, starting with oldest people
                while (food_delta < 0):
                    food_delta = food_delta + population.people[0].kcal_requirements(cur_sim_time)
                    population.remove_person(0)
                population.generate_death_dict()

            # Calculating how many newborns to be created in 9 months time
            num_people = population.num_people()
            num_adults = population.num_adults(cur_sim_time)
            # newborns based on number of adults. Average US birthrate in 2014: 0.01342 (indexmundi.com)
            people_born[cur_sim_time % 9] = random.randint(np.rint(num_adults*0.01),np.rint(num_adults*0.020))
            print 'total people:', num_people, 'and total adults:', num_adults, 'and total kcal:', total_kcal
            print 'total capacity:', facility.personnel_capacity

            print('-'*100)

            # Record results of the iteration.
            self._write_results(population=num_people,
                                food=food.produced_food,
                                kcals=total_kcal,
                                facility_crop=facility.crop_area,
                                facility_personnel=facility.personnel_capacity,
                                air=air.oxygen_consumed(),
                                power=power.power_consumed())

            # If the visualization option has been selected, plot the results
            # every 10 timesteps.
            if self.vis and cur_sim_time % 10 == 0:

                # Add data for the chart.
                self.vis.add_data(cur_sim_time, {
                    'Population Count': num_people,
                    'Adult Count': num_adults,
                    'Caloric Requirements (Mcal)': total_kcal / 1000.0,
                    'Produced Food (Mcal)': food.produced_food / 1000.0,
                    'Air (kg O2)': air.oxygen_consumed(),
                    'Power Consumption (kWh)': power.power_consumed()
                })

                # Update the chart, re-rendering.
                self.vis.update()

        # If visualization has been selected,
        if self.vis:
            # Save the last rendered chart as a png image.
            self.vis.savefig()

        # Return the results dict.
        return self.results

    # Private

    def _write_results(self, population=0, food=0, kcals=0, facility_crop=0,
                             facility_personnel=0, air=0, power=0):
        """ Writes the results of the simulation to a dictionary. """

        self.results['population'].append(population)
        self.results['food'].append(food)
        self.results['kcals'].append(kcals)
        self.results['facility_crop'].append(facility_crop)
        self.results['facility_personnel'].append(facility_personnel)
        self.results['air'].append(air)
        self.results['power'].append(power)
Exemple #42
0
 def __init__(self, *args, **kwargs):
     self.profile = Profile(db_name=kwargs['db_name'])
     self.all_users = self.profile.user()
     self.visualizer = Visualizer()
Exemple #43
0
import pickle
from visualizer import Visualizer

# Use this to define a scene for the robot

vizer = Visualizer()
obstcls = vizer.define_obstacles()
outfile = open('scene_01.pkl', 'wb')
pickle.dump(obstcls, outfile)
outfile.close()
Exemple #44
0
    d = device.GearVR
    devices.append(d(loc))
for i in xrange(3, 6):
    d = device.OculusRift
    devices.append(d(loc))
for i in xrange(6, 11):
    d = device.PlayStationVR
    devices.append(d(loc))

# Initialize cloud
traffic_level = 'high'
cloud_loc = loc
timeout = 10000 # 10 seconds
num_players = len(devices)
cloud = Cloud(traffic_level, cloud_loc, timeout, num_players)

# Initialize network
packet_loss_probability = 0 # % chance of network-related dropped packet
network = net.UDP(packet_loss_probability);

sim = Simulator(cloud, network, devices)

sim.runFor(10 * 60 * 1000) # Number of milliseconds

results = sim.getResults()

viz = Visualizer(results, map(lambda n: "Device " + str(n), range(1,11)))
viz.plotAverageLatency()

# TODO: Be run with passed configurations, create simulator, and produce results
Exemple #45
0
def main():
    """ Save Path """
    train_writer = None
    valid_writer = None
    test_writer = None

    if args.save == True:
        save_path = '{},{},{}epochs,b{},lr{}'.format(args.model, args.optim,
                                                     args.epochs,
                                                     args.batch_size, args.lr)
        time_stamp = datetime.datetime.now().strftime("%m-%d-%H:%M")
        save_path = os.path.join(time_stamp, save_path)
        save_path = os.path.join(args.dataName, save_path)
        save_path = os.path.join(args.save_path, save_path)
        print('==> Will save Everything to {}', save_path)

        if not os.path.exists(save_path):
            os.makedirs(save_path)

        #""" Setting for TensorboardX """

        train_writer = SummaryWriter(os.path.join(save_path, 'train'))
        valid_writer = SummaryWriter(os.path.join(save_path, 'valid'))
        test_writer = SummaryWriter(os.path.join(save_path, 'test'))
    # output_writer = SummaryWriter(os.path.join(save_path, 'Output_Writer'))
    """ Transforms/ Data Augmentation Tec """
    co_transforms = pc_transforms.Compose([
        #  pc_transforms.Delete(num_points=1466)
        # pc_transforms.Jitter_PC(sigma=0.01,clip=0.05),
        # pc_transforms.Scale(low=0.9,high=1.1),
        #  pc_transforms.Shift(low=-0.01,high=0.01),
        # pc_transforms.Random_Rotate(),
        #  pc_transforms.Random_Rotate_90(),

        # pc_transforms.Rotate_90(args,axis='x',angle=-1.0),# 1.0,2,3,4
        # pc_transforms.Rotate_90(args, axis='z', angle=2.0),
        # pc_transforms.Rotate_90(args, axis='y', angle=2.0),
        # pc_transforms.Rotate_90(args, axis='shape_complete') TODO this is essential for Angela's data set
    ])

    input_transforms = transforms.Compose([
        pc_transforms.ArrayToTensor(),
        #   transforms.Normalize(mean=[0.5,0.5],std=[1,1])
    ])

    target_transforms = transforms.Compose([
        pc_transforms.ArrayToTensor(),
        #  transforms.Normalize(mean=[0.5, 0.5], std=[1, 1])
    ])
    """-----------------------------------------------Data Loader----------------------------------------------------"""

    if (args.net_name == 'auto_encoder'):
        [train_dataset, valid_dataset] = Datasets.__dict__[args.dataName](
            input_root=args.data,
            target_root=None,
            split=args.split_value,
            net_name=args.net_name,
            input_transforms=input_transforms,
            target_transforms=target_transforms,
            co_transforms=co_transforms)
        [test_dataset, _] = Datasets.__dict__[args.dataName](
            input_root=args.datatest,
            target_root=None,
            split=None,
            net_name=args.net_name,
            input_transforms=input_transforms,
            target_transforms=target_transforms,
            co_transforms=co_transforms)

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=args.batch_size,
                                               num_workers=args.workers,
                                               shuffle=True,
                                               pin_memory=True)
    valid_loader = torch.utils.data.DataLoader(valid_dataset,
                                               batch_size=args.batch_size,
                                               num_workers=args.workers,
                                               shuffle=False,
                                               pin_memory=True)
    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=args.batch_size,
                                              num_workers=args.workers,
                                              shuffle=False,
                                              pin_memory=True)
    """----------------------------------------------Model Settings--------------------------------------------------"""

    print('Model:', args.model)

    if args.pretrained:
        network_data = torch.load(args.pretrained)

        args.model = network_data['model']
        print("==> Using Pre-trained Model '{}' saved at {} ".format(
            args.model, args.pretrained))
    else:
        network_data = None

    if (args.model == 'ae_pointnet'):
        model = models.__dict__[args.model](args,
                                            num_points=2048,
                                            global_feat=True,
                                            data=network_data).cuda()
    else:
        model = models.__dict__[args.model](network_data).cuda()

#  model = torch.nn.DataParallel(model.cuda(),device_ids=[0,1]) TODO To make dataparallel run do Nigels Fix """https://github.com/pytorch/pytorch/issues/1637#issuecomment-338268158"""

    params = get_n_params(model)
    print('| Number of parameters [' + str(params) + ']...')
    """-----------------------------------------------Optimizer Settings---------------------------------------------"""

    cudnn.benchmark = True
    print('Settings {} Optimizer'.format(args.optim))

    # param_groups = [{'params': model.module.bias_parameters(), 'weight_decay': args.bias_decay},
    #                  {'params': model.module.weight_parameters(), 'weight_decay':args.weight_decay}
    #                  ]
    if args.optim == 'Adam':
        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=args.lr,
                                     betas=(args.momentum, args.beta))
    scheduler = torch.optim.lr_scheduler.MultiStepLR(
        optimizer, milestones=args.milestones, gamma=args.gamma)
    # scheduler =  torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer)
    """-------------------------------------------------Visualer Initialization-------------------------------------"""

    visualizer = Visualizer(args)

    args.display_id = args.display_id + 10
    args.name = 'Validation'
    vis_Valid = Visualizer(args)
    vis_Valida = []
    args.display_id = args.display_id + 10

    for i in range(1, 20):

        vis_Valida.append(Visualizer(args))
        args.display_id = args.display_id + 10
    """---------------------------------------------------Loss Setting-----------------------------------------------"""

    chamfer = ChamferLoss(args)

    best_loss = -1
    valid_loss = 1000

    if args.test_only == True:
        epoch = 0
        test_loss, _, _ = test(valid_loader, model, epoch, args, chamfer,
                               vis_Valid, vis_Valida, test_writer)
        test_writer.add_scalar('mean Loss', test_loss, epoch)

        print('Average Loss :{}'.format(test_loss))
    else:
        """------------------------------------------------Training and Validation-----------------------------------"""
        for epoch in range(args.start_epoch, args.epochs):

            scheduler.step()

            train_loss, _, _ = train(train_loader, model, optimizer, epoch,
                                     args, chamfer, visualizer, train_writer)
            train_writer.add_scalar('mean Loss', train_loss, epoch)

            valid_loss, _, _ = validation(test_loader, model, epoch, args,
                                          chamfer, vis_Valid, vis_Valida,
                                          valid_writer)
            valid_writer.add_scalar('mean Loss', valid_loss, epoch)

            if best_loss < 0:
                best_loss = valid_loss

            is_best = valid_loss < best_loss

            best_loss = min(valid_loss, best_loss)

            if args.save == True:
                save_checkpoint(
                    {
                        'epoch': epoch + 1,
                        'model': args.model,
                        'state_dict': model.state_dict(
                        ),  # TODO if data parallel is fized write model.module.state_dict()
                        'state_dict_encoder': model.encoder.state_dict(),
                        'state_dict_decoder': model.decoder.state_dict(),
                        'best_loss': best_loss
                    },
                    is_best,
                    save_path)
Exemple #46
0
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        # visualizer
        self.vis = Visualizer(xlim=30)

        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init__
        self.canvas = FigureCanvas(self.vis.getFigure())

        # this is the Navigation widget
        # it takes the Canvas widget and a parent
        # self.toolbar = NavigationToolbar(self.canvas, self)

        # Form
        self.window_size_line_edit = QtGui.QLineEdit("10")
        self.window_size_line_edit.textChanged.connect(self.dnnChanged)
        self.m_line_edit = QtGui.QLineEdit("1")
        self.m_line_edit.textChanged.connect(self.dnnChanged)
        self.r_line_edit = QtGui.QLineEdit("2")
        self.r_line_edit.textChanged.connect(self.dnnChanged)
        self.hidden_layer_sizes_line_edit = QtGui.QLineEdit("10,10,10")
        self.hidden_layer_sizes_line_edit.textChanged.connect(self.dnnChanged)

        self.input_form = QtGui.QFormLayout()
        self.input_form.addRow("Window SIze:", self.window_size_line_edit)
        self.input_form.addRow("m:", self.m_line_edit)
        self.input_form.addRow("r:", self.r_line_edit)
        self.input_form.addRow("Hidden Layer Sizes:", self.hidden_layer_sizes_line_edit)

        self.pretrian_epochs_line_edit = QtGui.QLineEdit("10")
        self.pretrian_epochs_line_edit.textChanged.connect(self.updateWorker)
        self.pretrain_lr_slider = QtGui.QSlider(QtCore.Qt.Horizontal)
        self.pretrain_lr_slider.setRange(1, 10)
        self.pretrain_lr_slider.setValue(1)
        self.pretrain_lr_slider.valueChanged.connect(self.updateWorker)
        self.finetune_epochs_line_edit = QtGui.QLineEdit("10")
        self.finetune_epochs_line_edit.textChanged.connect(self.updateWorker)
        self.finetune_lr_slider = QtGui.QSlider(QtCore.Qt.Horizontal)
        self.finetune_lr_slider.setRange(1, 10)
        self.finetune_lr_slider.setValue(1)
        self.finetune_lr_slider.valueChanged.connect(self.updateWorker)

        self.learn_form = QtGui.QFormLayout()
        self.learn_form.addRow("finetune_epoch", self.finetune_epochs_line_edit)
        self.learn_form.addRow("finetune_lr", self.finetune_lr_slider)
        self.learn_form.addRow("pretrain_epoch", self.pretrian_epochs_line_edit)
        self.learn_form.addRow("pretrain_lr", self.pretrain_lr_slider)

        # A slider to control the plot delay
        self.slider = QtGui.QSlider(QtCore.Qt.Horizontal)
        self.slider.setRange(0, 99)
        self.slider.setValue(25)
        self.slider.valueChanged.connect(self.updateWorker)

        # A slider to control K
        self.k_slider = QtGui.QSlider(QtCore.Qt.Vertical)
        self.k_slider.setRange(0, 100)
        self.k_slider.setValue(0)
        self.k_slider.valueChanged.connect(self.updateWorker)
        self.n_slider = QtGui.QSlider(QtCore.Qt.Vertical)
        self.n_slider.setRange(0, 100)
        self.n_slider.setValue(0)
        self.n_slider.valueChanged.connect(self.updateWorker)

        # Just some button connected to `plot` method
        self.start_stop_button = QtGui.QPushButton("Start")
        self.start_stop_button.clicked.connect(self.start)

        # set the layout
        layout = QtGui.QGridLayout()
        # layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas, 0, 0, 1, 2)
        layout.addWidget(self.k_slider, 0, 2, 1, 1)
        layout.addWidget(self.n_slider, 0, 3, 1, 1)
        layout.addLayout(self.input_form, 1, 0, 1, 1)
        layout.addLayout(self.learn_form, 1, 1, 1, 1)
        layout.addWidget(self.slider, 2, 0)
        layout.addWidget(self.start_stop_button, 2, 1)
        self.setLayout(layout)

        # setup worker
        self.need_setup = True
        self.worker = Worker(self.vis)

        # setup event dispatchers
        self.worker.started.connect(self.workerStarted)
        self.worker.updated.connect(self.updateFigure)
        self.worker.stopped.connect(self.workerStopped)
Exemple #47
0
 def __init__(self, args):
     Visualizer.__init__(self, args)
     self.boids = []
     boid = Boid(PVector(10, 10), 3.0, 3.0)
     boid.arrive(PVector(400, 200))
     self.boids.append(boid)
Exemple #48
0
unicycle_cost.add_cost(unicycle_maxv_cost, "x", 100.0)
unicycle_cost.add_cost(unicycle_minv_cost, "x", 100.0)
unicycle_cost.add_cost(unicycle_proximity_cost, "x", 5.0)

unicycle_player_id = 2
unicycle_cost.add_cost(unicycle_steering_cost, unicycle_player_id, 50.0)
unicycle_cost.add_cost(unicycle_a_cost, unicycle_player_id, 1.0)

# Visualizer.
visualizer = Visualizer(
    [car1_position_indices_in_product_state,
     car2_position_indices_in_product_state,
     unicycle_position_indices_in_product_state],
    [car1_polyline_boundary_cost,
     car1_goal_cost,
     car2_polyline_boundary_cost,
     car2_goal_cost,
     unicycle_goal_cost],
    [".-r", ".-g", ".-b"],
    1,
    False,
    plot_lims=[-5, 25, -5, 35])

# Logger.
if not os.path.exists(LOG_DIRECTORY):
    os.makedirs(LOG_DIRECTORY)

logger = Logger(os.path.join(LOG_DIRECTORY, 'intersection_car_example.pkl'))

# Set up ILQSolver.
solver = ILQSolver(dynamics,
Exemple #49
0
class PolicyLearner:
    # chart_data Environment객체 생시 넣어줌
    def __init__(self,stock_code,chart_data,training_data = None,min_trading_unit=1, max_trading_unit=2,delayed_reward_threshold=.05,lr=0.01):
        #종목코드
        self.stock_code = stock_code
        self.chart_data = chart_data
        #환경 객체
        self.environment = Environment(chart_data)
        #에이전트 객체
        self.agent = Agent(self.environment,min_trading_unit=min_trading_unit,max_trading_unit=max_trading_unit,delayed_reward_threshold=delayed_reward_threshold)
        #학습 데이터
        self.training_data = training_data
        self.sample = None
        self.training_data_idx = -1
        #정책 신경망 : 입력크기 = 학습 데이터의 크기 + 에이전트 상태 크기
        self.num_features = self.training_data.shape[1] + self.agent.STATE_DIM
        self.policy_network = PolicyNetwork(input_dim=self.num_features,output_dim=self.agent.NUM_ACTIONS, lr=lr)
        #가시화 모듈
        self.visualizer = Visualizer()

    #에포크마다 호출하는 reset함수
    def reset(self):
        self.sample = None
        # 학습데이터를 읽어가면서 1씩 증가하는 변수
        self.training_data_idx = -1

    #max_memory 배치 학습 데이터를 만들기 위해 과거 데이터를 저장할 배열 balance 에이전트 초기 투자 자본금
    def fit(self,num_epoches=1000, max_memory=60, balance=1000000, discount_factor=0, start_epsilon=.5, learning= True):
        logger.info("LR:{lr}, DF : {discount_factor}, TU : [{min_trading_unit}, {max_trading_unit}],"
                    "DRT: {delayed_reward_threshold}".format(lr=self.policy_network.lr,discount_factor=discount_factor,min_trading_unit=self.agent.min_trading_unit,max_trading_unit=self.agent.max_trading_unit,
                                                             delayed_reward_threshold=self.agent.delayed_reward_threshold))

        #가시화 준비
        #ckxm 차트 데이터는 변하지 않음으로 미리 가시화
        self.visualizer.prepare(self.environment.chart_data)

        #가시화 결과 저장할 폴더 준비  폴더이름에 시간 날짜를 넣어 중복되는 일이 없도록 함
        epoch_summary_dir = os.path.join(settings.BASE_DIR,'epoch_summary/%s/epoch_summary_%s'%(self.stock_code,settings.timestr))
        if not os.path.isdir(epoch_summary_dir):
            os.makedirs(epoch_summary_dir)

        #에이전트 초기 자본금 설정
        self.agent.set_balance(balance)

        #학습에 대한 정보 초기화
        max_portfolio_value = 0
        epoch_win_cnt = 0

        # 학습 반복
        for epoch in range(num_epoches):
            #에포크 관련 정보 초기화
            loss = 0
            #수행한 에포크 수
            itr_cnt = 0
            #수익이 발생한 에포크 수
            win_cnt = 0
            #무작위 투자를 수행한 횟수
            exploration_cnt = 0
            batch_size = 0
            #수익이 발생하여 긍정적 지연 보상을 준 수
            pos_learning_cnt = 0
            #손실이 발생하여 부정적 지연 보상을 준 수
            neg_learning_cnt = 0

            #메모리 초기화
            memory_sample = []
            memory_action = []
            memory_reward = []
            memory_prob = []
            memory_pv = []
            memory_num_stocks = []
            memory_exp_idx = []
            memory_learning_idx = []

            #환경, 에이전트, 정책 신경망 초기화
            self.environment.reset()
            self.agent.reset()
            self.policy_network.reset()
            self.reset()

            #가시화기 초기화 --> 2,3,4 번 차트 초기화 및 x축차트 범위 파라미터로 삽입
            self.visualizer.clear([0,len(self.chart_data)])

            #학습을 진행할수록 탐험 비율 감소
            if learning:
                epsilon = start_epsilon*(1. - float(epoch)/(num_epoches -1))
            else:
                epsilon = 0

            #하나의 에포크를 수행하는 while
            while True:
                #샘플 생성
                next_sample = self._build_sample()
                if next_sample is None:
                    break

                #정책 신경망 또는 탐험에 의한 행동 결정  return 결정한 행동,결정에 대한 확신도, 무작위 투자 유무
                action, confidence, exploration = self.agent.decide_action(self.policy_network,self.sample,epsilon)

                #결정한 행동을 수행하고 즉시 보상과 지현 보상 획득
                immediate_reward, delayed_reward = self.agent.act(action,confidence)

                #행동 및 행동에 대한 결과를 기억
                memory_sample.append(next_sample)
                memory_action.append(action)
                memory_reward.append(immediate_reward)
                memory_pv.append(self.agent.portfolio_value)
                memory_num_stocks.append(self.agent.num_stocks)
                #학습 데이터의 샘플,에이전트 행동,즉시보상,포트폴리오 가치, 보유 주식수를 저장--> 위에 추가한것들 모아2차원 배열 생성
                memory = [( memory_sample[i],memory_action[i],memory_reward[i])
                          for i in list(range(len(memory_action)))[-max_memory:]]
                if exploration:
                    #무작위 투자인 경우
                    memory_exp_idx.append(itr_cnt)
                    #정책 신경망의 출력을 그대로 저장하는 배열
                    memory_prob.append([np.nan]*Agent.NUM_ACTIONS)
                else:
                    #정책 신경망의 출력을 그대로 저장
                    memory_prob.append(self.policy_network.prob)

                #반복에 대한 정보 갱신
                batch_size += 1
                itr_cnt += 1
                #탐험을 한경우에만 증가
                exploration_cnt += 1 if exploration else  0
                # 지연 보상이 0보다 큰경우에만 1을 증가
                win_cnt += 1 if delayed_reward > 0 else 0

                #지연 보상이 발생한 경웨 학습을 수행하는 부분
                #학습 모드이고 지연 보상이 존재할 경우 정책 신경망 갱신
                if delayed_reward == 0 and batch_size >= max_memory:
                    delayed_reward = immediate_reward
                if learning and delayed_reward != 0:
                    #배치 학습 데이터 크기  max_memory보다 작아야 함
                    batch_size = min(batch_size, max_memory)
                    #배치 학습 데이터 생성
                    x, y = self._get_batch(memory,batch_size,discount_factor,delayed_reward)

                    if len(x) >0:
                        #긍부정 학습횟수 체크
                        if delayed_reward > 0:
                            pos_learning_cnt += 1
                        else:
                            neg_learning_cnt += 1
                        #정책 신경망 갱신
                        # print("x : ",x)
                        # print("y : ",y)
                        # print("len(x) : ",len(x))
                        # print("len(y) : ",len(y))
                        # print("type(x): ",type(x))
                        # print("type(y): ", type(y))
                        # print("loss : ",loss)
                        # 1.123123124
                        # [12.23,54.3]
                        loss += self.policy_network.train_on_batch(x,y)
                        memory_learning_idx.append([itr_cnt,delayed_reward])
                    batch_size =0

            # 에포크 관련 정보 가시화
            #총에포크수 문자열 길이 체크 ex 1000번이면 4
            num_epoches_digit = len(str(num_epoches))
            #현제 에포크수를 num_epoches_digit 자릿수로 만들어줌 4이고 1epoch 이면 0001 이런식으로
            epoch_str = str(epoch + 1).rjust(num_epoches_digit, '0')

            self.visualizer.plot(
                #가시화기의 plot함수를 호출하여 에포크 수행 결과를 가시화
                epoch_str=epoch_str, num_epoches=num_epoches, epsilon=epsilon,
                action_list=Agent.ACTIONS, actions=memory_action,
                num_stocks=memory_num_stocks, outvals=memory_prob,
                exps=memory_exp_idx, learning=memory_learning_idx,
                initial_balance=self.agent.initial_balance, pvs=memory_pv
            )
            #수행 결과를 파일로 저장
            self.visualizer.save(os.path.join(
                epoch_summary_dir, 'epoch_summary_%s_%s.png' % (
                    settings.timestr, epoch_str)))

            # 에포크 관련 정보 로그 기록
            if pos_learning_cnt + neg_learning_cnt > 0:
                loss /= pos_learning_cnt + neg_learning_cnt
            logger.info("[Epoch %s/%s]\tEpsilon:%.4f\t#Expl.:%d/%d\t"
                        "#Buy:%d\t#Sell:%d\t#Hold:%d\t"
                        "#Stocks:%d\tPV:%s\t"
                        "POS:%s\tNEG:%s\tLoss:%10.6f" % (
                            epoch_str, num_epoches, epsilon, exploration_cnt, itr_cnt,
                            self.agent.num_buy, self.agent.num_sell, self.agent.num_hold,
                            self.agent.num_stocks,
                            locale.currency(self.agent.portfolio_value, grouping=True),
                            pos_learning_cnt, neg_learning_cnt, loss))

            # 학습 관련 정보 갱신
            max_portfolio_value = max(
                max_portfolio_value, self.agent.portfolio_value)
            if self.agent.portfolio_value > self.agent.initial_balance:
                epoch_win_cnt += 1

        # 최종 학습 결과 통꼐 정보 로그 기록 부분
        logger.info("Max PV: %s, \t # Win: %d" % (
            locale.currency(max_portfolio_value, grouping=True), epoch_win_cnt))



    #미니 배치 데이터 생성 함수 부분
    def _get_batch(self, memory, batch_size, discount_factor, delayed_reward):
        #일련의 학습 데이터 및 에이전트 상태 배치 데이터 크기,학습 데이터 특징 크기 2차원
        x = np.zeros((batch_size, 1, self.num_features))
        # 일련의 지연보상 데이터 크기, 정책 신경ㅁ아의 결정하는 에이전트의 행동의 수  2차원
        # 배치 데이터 ㅡ키근ㄴ 지연 보상이 발생될 때 결정되기 때문에 17과 2로 고정
        y = np.full((batch_size, self.agent.NUM_ACTIONS), 0.5)

        for i, (sample, action, reward) in enumerate(
                reversed(memory[-batch_size:])):
            x[i] = np.array(sample).reshape((-1, 1, self.num_features))
            y[i, action] = (delayed_reward + 1) / 2
            if discount_factor > 0:
                y[i, action] *= discount_factor ** i
        return x, y

    #학습 데이터 샘플 생성 부분
    def _build_sample(self):
        # 차트 데이터의 현제 인덱스에서 다음 인덱스 데이터를 읽음
        self.environment.observe()
        #학습 데이터의 다음 인덱스가 존재하는지 확인
        if len(self.training_data) > self.training_data_idx + 1:
            self.training_data_idx += 1
            self.sample = self.training_data.iloc[self.training_data_idx].tolist()
            self.sample.extend(self.agent.get_states())
            return self.sample
        return None

    # 투자 시뮬레이션을 하는 trade 함수
    def trade(self, model_path=None, balance=2000000):
        if model_path is None:
            return
        self.policy_network.load_model(model_path=model_path)
        self.fit(balance=balance, num_epoches=1, learning=False)
def run_experiment_fans(with_visualization=False):
    """
    TODO: add experiment description.

    """
    experiment_name = "Back_Front"
    experiment_duration = 10000  # in ms
    dx = 92  # in pixels
    dy = 92  # in pixels
    max_d = 24  # in pixels
    crop_xmin = 25  # in pixels
    crop_ymin = 10  # in pixels

    # Setup the simulation
    Simulation = SNNSimulation(simulation_time=experiment_duration)

    # Define the input source
    path_to_input = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "../data/input/Back_On_Front_Accel_Fixed_even.npz")
    ExternalRetinaInput = ExternalInputReader(file_path=path_to_input,
                                              dim_x=dx,
                                              dim_y=dy,
                                              crop_xmin=crop_xmin,
                                              crop_xmax=crop_xmin + dx,
                                              crop_ymin=crop_ymin,
                                              crop_ymax=crop_ymin + dy,
                                              sim_time=experiment_duration,
                                              is_rawdata_time_in_ms=False)

    # Create two instances of Retinas with the respective inputs
    RetinaL = Retina(label="RetL",
                     dimension_x=dx,
                     dimension_y=dy,
                     spike_times=ExternalRetinaInput.retinaLeft,
                     record_spikes=False,
                     experiment_name=experiment_name)
    RetinaR = Retina(label="RetR",
                     dimension_x=dx,
                     dimension_y=dy,
                     spike_times=ExternalRetinaInput.retinaRight,
                     record_spikes=False,
                     experiment_name=experiment_name)

    # Create a cooperative network for stereo vision from retinal disparity
    SNN_Network = CooperativeNetwork(retinae={
        'left': RetinaL,
        'right': RetinaR
    },
                                     max_disparity=max_d,
                                     record_spikes=True,
                                     record_v=False,
                                     experiment_name=experiment_name)

    # Start the simulation
    Simulation.run()

    # Store the results in a file
    SNN_Network.get_spikes(sort_by_time=True, save_spikes=True)
    # ret_left_spikes = RetinaL.get_spikes(sort_by_time=True, save_spikes=True)
    # ret_right_spikes = RetinaR.get_spikes(sort_by_time=True, save_spikes=True)
    # membrane_potential = SNN_Network.get_v(save_v=True)

    # Finish the simulation
    Simulation.end()

    if with_visualization:
        from visualizer import Visualizer

        network_dimensions = SNN_Network.get_network_dimensions()
        viz = Visualizer(network_dimensions=network_dimensions,
                         experiment_name=experiment_name,
                         spikes_file="./spikes/Back_Front_0_spikes.dat")
        # viz.microensemble_voltage_plot(save_figure=True)
        viz.disparity_histogram(over_time=True, save_figure=True)
#from collector import *
from collector import Node
from visualizer import Visualizer

root = Node.unpickle()
print ("childs = ", len(root.childs))
print(root.childs[0].path)

visu = Visualizer(root)
visu.run()
Exemple #52
0
 def on_resize(self, window, w, h):
     Visualizer.on_resize(self, window, w, h)
     self.trackball.set_window_size(w, h)
Exemple #53
0
        else:
            output = self.main(input)

        return output.view(-1, 1).squeeze(1)


netD = Discriminator(ngpu).to(device)
netD.apply(weights_init)
if opt.netD != '':
    netD.load_state_dict(torch.load(opt.netD))
print(netD)

if not os.path.exists(opt.checkpoints_dir):
    os.mkdir(opt.checkpoints_dir)

vis = Visualizer(opt)
criterion = nn.BCELoss()

fixed_noise = torch.randn(opt.batchSize, nz, 1, 1, device=device)
real_label = 1
fake_label = 0

# setup optimizer
optimizerD = optim.Adam(netD.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))
optimizerG = optim.Adam(netG.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))

for epoch in range(opt.niter):
    for i, data in enumerate(dataloader, 0):
        ############################
        # (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
        ###########################
Exemple #54
0
class Simulation:
    """Runs the core of the simulation through time.

    === Attributes ===
    all_rides:
        A list of all the rides in this simulation.
        Note that not all rides might be used, depending on the timeframe
        when the simulation is run.
    all_stations:
        A dictionary containing all the stations in this simulation.
    visualizer:
        A helper class for visualizing the simulation.
    active_rides:
        A list of all rides currently active
    ride_priority_queue:
        A priority queue for events in the simulation
    """
    all_stations: Dict[str, Station]
    all_rides: List[Ride]
    visualizer: Visualizer
    active_rides: List[Ride]
    ride_priority_queue: PriorityQueue()

    def __init__(self, station_file: str, ride_file: str) -> None:
        """Initialize this simulation with the given configuration settings.
        """
        self.all_stations = create_stations(station_file)
        self.all_rides = create_rides(ride_file, self.all_stations)
        self.visualizer = Visualizer()
        self.active_rides = []
        self.ride_priority_queue = PriorityQueue()

    def run(self, start: datetime, end: datetime) -> None:
        """Run the simulation from <start> to <end>.
        """
        step = timedelta(minutes=1)  # Each iteration spans one minute of time

        time = start

        for ride in self.all_rides:
            if start < ride.start_time < end:
                self.ride_priority_queue.add(
                    RideStartEvent(self, ride.start_time, ride))

        while time < end:
            time += step

            for station in self.all_stations:
                self.all_stations[station].check_space()

            self._update_active_rides_fast(time)

            rides_stations = list(
                self.all_stations.values()) + self.active_rides

            self.visualizer.render_drawables(rides_stations, time)

            # This part was commented out to allow sample tests to work
            # if self.visualizer.handle_window_events():
            #     return  # Stop the simulation

    def _update_active_rides(self, time: datetime) -> None:
        """Update this simulation's list of active rides for the given time.

        REQUIRED IMPLEMENTATION NOTES:
        -   Loop through `self.all_rides` and compare each Ride's start and
            end times with <time>.

            If <time> is between the ride's start and end times (inclusive),
            then add the ride to self.active_rides if it isn't already in that
            list.

            Otherwise, remove the ride from self.active_rides if it is in that
            list.

        -   This means that if a ride started before the simulation's time
            period but ends during or after the simulation's time period,
            it should still be added to self.active_rides.
        """

        for ride in self.all_rides:
            # Only take into account ride that happen during the simulation
            # bracket
            if (ride.start_time <= time <= ride.end_time) and \
                    (ride not in self.active_rides):
                self.active_rides.append(ride)
                ride.start.num_bikes_start += 1

        for ride in self.active_rides:
            # Remove a ride from active rides if the ride is over
            if time > ride.end_time:
                self.active_rides.remove(ride)
                ride.end.num_bikes_end += 1

        for ride in self.active_rides:
            # If a ride starts, remove a bike from its start station
            if time == ride.start_time:
                if ride.start.num_bikes > 0:
                    ride.start.num_bikes -= 1
                else:
                    self.active_rides.remove(ride)

            # If a ride is over, add a bike to its en station and remove it
            # from active rides
            if time == ride.end_time:
                if ride.end.capacity > ride.end.num_bikes:
                    ride.end.num_bikes += 1
                else:
                    self.active_rides.remove(ride)

    def _find_max(self, value: str):
        """Helper function to find the stations with the maximum of the queried
        attribute
        """
        stations = self.all_stations
        maximum = ('', -1)

        for key in stations:
            # Adapt the attribute search for according to the queried one
            if value == 'num_bikes_start':
                station_attribute = stations[key].num_bikes_start
            elif value == 'num_bikes_end':
                station_attribute = stations[key].num_bikes_end
            elif value == 'total_time_low_availability':
                station_attribute = stations[key].total_time_low_availability
            elif value == 'total_time_low_unoccupied':
                station_attribute = stations[key].total_time_low_unoccupied

            # Find maximum value by replacing maximum if the station has a
            # higher value
            if station_attribute > maximum[1]:
                maximum = (stations[key].name, station_attribute)
            elif station_attribute == maximum[1]:
                if stations[key].name < maximum[0]:
                    maximum = (stations[key].name, station_attribute)

        return maximum

    def calculate_statistics(self) -> Dict[str, Tuple[str, float]]:
        """Return a dictionary containing statistics for this simulation.

        The returned dictionary has exactly four keys, corresponding
        to the four statistics tracked for each station:
          - 'max_start'
          - 'max_end'
          - 'max_time_low_availability'
          - 'max_time_low_unoccupied'

        The corresponding value of each key is a tuple of two elements,
        where the first element is the name (NOT id) of the station that
        has the maximum value of the quantity specified by that key,
        and the second element is the value of that quantity.

        For example, the value corresponding to key 'max_start' should be
        the name of the station with the most number of rides started at
        that station, and the number of rides that started at that
        station.
        """

        max_start = self._find_max('num_bikes_start')
        max_end = self._find_max('num_bikes_end')
        max_time_low_availability = \
            self._find_max('total_time_low_availability')
        max_time_low_unoccupied = self._find_max('total_time_low_unoccupied')

        return {
            'max_start': max_start,
            'max_end': max_end,
            'max_time_low_availability': max_time_low_availability,
            'max_time_low_unoccupied': max_time_low_unoccupied
        }

    def _update_active_rides_fast(self, time: datetime) -> None:
        """Update this simulation's list of active rides for the given
        time.

        REQUIRED IMPLEMENTATION NOTES:
        -   see Task 5 of the assignment handout
        """
        p_queue = self.ride_priority_queue
        while not p_queue.is_empty():
            event = p_queue.remove()
            if time < event.time:
                p_queue.add(event)
                return
            else:
                queued_events = event.process()
                for queued_event in queued_events:
                    p_queue.add(queued_event)
#!/usr/bin/python
import psyco
psyco.full()

from misc import *
from controller import connection,cerebellum,visualize,mainLoop,staticMap
##############

cerebellum.registerMessageHandler(TestHandler())

#cerebellum.command = ("moveTo",0,0)

if visualize:
    from visualizer import Visualizer
    vis = Visualizer(cerebellum, staticMap)
    vis.start()

mainLoop()

if visualize:
    vis.terminate = True
    vis.join()
class Simulation:
    """The main simulation class.

    === Attributes ===
    elevators: a list of the elevators in the simulation
    visualizer: the Pygame visualizer used to visualize this simulation
    visualize: True or False, indicates if simulation will be visualized
    waiting: a dictionary of people waiting for an elevator
             (keys are floor numbers, values are the list of waiting people)
    stats: refer to SimulationStatistics class
    parameters: refer to SimulationParameters class
    """
    elevators: List[Elevator]
    visualizer: Visualizer
    visualize: bool
    waiting: Dict[int, List[Person]]
    stats: SimulationStatistics
    parameters: SimulationParameters

    def __init__(self,
                 config: Dict[str, Any]) -> None:
        """Initialize a new simulation using the given configuration."""

        self.visualize = config['visualize']

        self.elevators = []
        for _ in range(config['num_elevators']):
            e = Elevator(config['elevator_capacity'])
            self.elevators.append(e)
        self.waiting = {}
        self.stats = SimulationStatistics()
        self.parameters = SimulationParameters(config)

        # Initialize the visualizer.
        # Note that this should be called *after* the other attributes
        # have been initialized.
        self.visualizer = Visualizer(self.elevators,
                                     self.parameters.num_floors,
                                     config['visualize'])

    ############################################################################
    # Handle rounds of simulation.
    ############################################################################
    def run(self, num_rounds: int) -> Dict[str, Any]:
        """Run the simulation for the given number of rounds.

        Return a set of statistics for this simulation run, as specified in the
        assignment handout.

        Precondition: num_rounds >= 1.

        Note: each run of the simulation starts from the same initial state
        (no people, all elevators are empty and start at floor 1).
        """
        self.parameters.num_iterations = num_rounds
        for n in range(num_rounds):
            self.visualizer.render_header(n)

            # Stage 1: generate new arrivals
            self._generate_arrivals(n)

            # Stage 2: leave elevators
            self._handle_leaving()

            # Stage 3: board elevators
            self._handle_boarding()

            # Stage 4: move the elevators using the moving algorithm
            self._move_elevators()

            # Update wait times
            for floor in self.waiting:
                _update_wait_times(self.waiting[floor])
            for elevator in self.elevators:
                _update_wait_times(elevator.passengers)

            # Pause for 2 seconds
            self.visualizer.wait(2)

        return self._calculate_stats()

    def _generate_arrivals(self, round_num: int) -> None:
        """Generate and visualize new arrivals."""
        new_arrivals = self.parameters.arrival_generator.generate(round_num)
        for floor in new_arrivals:
            try:
                self.waiting[floor].extend(new_arrivals[floor])
            except KeyError:
                self.waiting[floor] = new_arrivals[floor]
            self.stats.total_people += len(new_arrivals[floor])
        if self.visualize:
            self.visualizer.show_arrivals(new_arrivals)

    def _handle_leaving(self) -> None:
        """Handle people leaving elevators."""

        for elevator in self.elevators:
            to_remove = []
            for person in elevator.passengers:
                if person.target == elevator.current_floor:
                    to_remove.append(True)
                    self.stats.people_completed.append(person)
                    if self.visualize:
                        self.visualizer.show_disembarking(person, elevator)
                else:
                    to_remove.append(False)
            new_list = [p for i, p in enumerate(elevator.passengers)
                        if not to_remove[i]]
            elevator.passengers = new_list[:]

    def _handle_boarding(self) -> None:
        """Handle boarding of people and visualize."""
        for i, elevator in enumerate(self.elevators):
            num_people = len(elevator.passengers)
            floor = elevator.current_floor
            try:
                people_waiting = self.waiting[floor]
            except KeyError:
                continue
            while num_people < elevator.capacity and people_waiting:
                # pop(0) returns and removes the first person in waiting list
                person_to_board = people_waiting.pop(0)
                elevator.passengers.append(person_to_board)
                if self.visualize:
                    self.visualizer.show_boarding(person_to_board, elevator)
                num_people = len(elevator.passengers)

    def _move_elevators(self) -> None:
        """Move the elevators in this simulation.

        Use this simulation's moving algorithm to move the elevators.
        """
        directions = self.parameters.moving_algorithm.\
            move_elevators(self.elevators,
                           self.waiting,
                           self.parameters.num_floors)
        for index, elevator in enumerate(self.elevators):
            elevator.current_floor += directions[index].value
            print("elevator", index, "at floor", elevator.current_floor)

        if self.visualize:
            self.visualizer.show_elevator_moves(self.elevators, directions)

    ############################################################################
    # Statistics calculations
    ############################################################################
    def _calculate_stats(self) -> Dict[str, int]:
        """Report the statistics for the current run of this simulation.
        """
        num_iterations = self.parameters.num_iterations
        total_people = self.stats.total_people
        people_completed = len(self.stats.people_completed)
        wait_times = [p.wait_time for p in self.stats.people_completed]
        if not wait_times:
            max_time = -1
            min_time = -1
            avg_time = -1
        else:
            min_time = min(wait_times)
            max_time = max(wait_times)
            avg_time = int(sum(wait_times) / len(wait_times))

        return {
            'num_iterations': num_iterations,
            'total_people': total_people,
            'people_completed': people_completed,
            'max_time': max_time,
            'min_time': min_time,
            'avg_time': avg_time
        }
Exemple #57
0
class Window(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        # visualizer
        self.vis = Visualizer(xlim=30)

        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init__
        self.canvas = FigureCanvas(self.vis.getFigure())

        # this is the Navigation widget
        # it takes the Canvas widget and a parent
        # self.toolbar = NavigationToolbar(self.canvas, self)

        # Form
        self.window_size_line_edit = QtGui.QLineEdit("10")
        self.window_size_line_edit.textChanged.connect(self.dnnChanged)
        self.m_line_edit = QtGui.QLineEdit("1")
        self.m_line_edit.textChanged.connect(self.dnnChanged)
        self.r_line_edit = QtGui.QLineEdit("2")
        self.r_line_edit.textChanged.connect(self.dnnChanged)
        self.hidden_layer_sizes_line_edit = QtGui.QLineEdit("10,10,10")
        self.hidden_layer_sizes_line_edit.textChanged.connect(self.dnnChanged)

        self.input_form = QtGui.QFormLayout()
        self.input_form.addRow("Window SIze:", self.window_size_line_edit)
        self.input_form.addRow("m:", self.m_line_edit)
        self.input_form.addRow("r:", self.r_line_edit)
        self.input_form.addRow("Hidden Layer Sizes:", self.hidden_layer_sizes_line_edit)

        self.pretrian_epochs_line_edit = QtGui.QLineEdit("10")
        self.pretrian_epochs_line_edit.textChanged.connect(self.updateWorker)
        self.pretrain_lr_slider = QtGui.QSlider(QtCore.Qt.Horizontal)
        self.pretrain_lr_slider.setRange(1, 10)
        self.pretrain_lr_slider.setValue(1)
        self.pretrain_lr_slider.valueChanged.connect(self.updateWorker)
        self.finetune_epochs_line_edit = QtGui.QLineEdit("10")
        self.finetune_epochs_line_edit.textChanged.connect(self.updateWorker)
        self.finetune_lr_slider = QtGui.QSlider(QtCore.Qt.Horizontal)
        self.finetune_lr_slider.setRange(1, 10)
        self.finetune_lr_slider.setValue(1)
        self.finetune_lr_slider.valueChanged.connect(self.updateWorker)

        self.learn_form = QtGui.QFormLayout()
        self.learn_form.addRow("finetune_epoch", self.finetune_epochs_line_edit)
        self.learn_form.addRow("finetune_lr", self.finetune_lr_slider)
        self.learn_form.addRow("pretrain_epoch", self.pretrian_epochs_line_edit)
        self.learn_form.addRow("pretrain_lr", self.pretrain_lr_slider)

        # A slider to control the plot delay
        self.slider = QtGui.QSlider(QtCore.Qt.Horizontal)
        self.slider.setRange(0, 99)
        self.slider.setValue(25)
        self.slider.valueChanged.connect(self.updateWorker)

        # A slider to control K
        self.k_slider = QtGui.QSlider(QtCore.Qt.Vertical)
        self.k_slider.setRange(0, 100)
        self.k_slider.setValue(0)
        self.k_slider.valueChanged.connect(self.updateWorker)
        self.n_slider = QtGui.QSlider(QtCore.Qt.Vertical)
        self.n_slider.setRange(0, 100)
        self.n_slider.setValue(0)
        self.n_slider.valueChanged.connect(self.updateWorker)

        # Just some button connected to `plot` method
        self.start_stop_button = QtGui.QPushButton("Start")
        self.start_stop_button.clicked.connect(self.start)

        # set the layout
        layout = QtGui.QGridLayout()
        # layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas, 0, 0, 1, 2)
        layout.addWidget(self.k_slider, 0, 2, 1, 1)
        layout.addWidget(self.n_slider, 0, 3, 1, 1)
        layout.addLayout(self.input_form, 1, 0, 1, 1)
        layout.addLayout(self.learn_form, 1, 1, 1, 1)
        layout.addWidget(self.slider, 2, 0)
        layout.addWidget(self.start_stop_button, 2, 1)
        self.setLayout(layout)

        # setup worker
        self.need_setup = True
        self.worker = Worker(self.vis)

        # setup event dispatchers
        self.worker.started.connect(self.workerStarted)
        self.worker.updated.connect(self.updateFigure)
        self.worker.stopped.connect(self.workerStopped)

    def start(self):
        self.start_stop_button.setText("Stop")
        self.start_stop_button.setEnabled(False)

        window_size = string.atoi(self.window_size_line_edit.text())
        m = string.atoi(self.m_line_edit.text())
        r = string.atoi(self.r_line_edit.text())
        hidden_layer_sizes = self.hidden_layer_sizes_line_edit.text().split(",")
        hidden_layer_sizes = [string.atoi(n) for n in hidden_layer_sizes]

        if self.need_setup:
            self.worker.setup(m=m, r=r, window_size=window_size, hidden_layer_sizes=hidden_layer_sizes, pretrain_step=1)
            self.need_setup = False
        self.updateWorker()
        self.worker.start()

    def stop(self):
        self.start_stop_button.setText("Start")
        self.start_stop_button.setEnabled(False)
        self.worker.stop()

    def dnnChanged(self):
        self.need_setup = True

    def updateWorker(self):
        self.worker.setGeneratorParams(self.getKValue(), self.getNValue())
        self.worker.setDelay(self.getDelayValue())
        self.worker.setLearningParams(self.getLearningParams())

    def workerStarted(self):
        self.start_stop_button.setEnabled(True)
        self.start_stop_button.clicked.connect(self.stop)
        self.window_size_line_edit.setReadOnly(True)
        self.m_line_edit.setReadOnly(True)
        self.r_line_edit.setReadOnly(True)
        self.hidden_layer_sizes_line_edit.setReadOnly(True)

    def updateFigure(self):
        # refresh canvas
        self.canvas.draw()

    def workerStopped(self):
        self.start_stop_button.setEnabled(True)
        self.start_stop_button.clicked.connect(self.start)
        self.window_size_line_edit.setReadOnly(False)
        self.m_line_edit.setReadOnly(False)
        self.r_line_edit.setReadOnly(False)
        self.hidden_layer_sizes_line_edit.setReadOnly(False)

    def getLearningParams(self):
        return {
            "pretrain_epochs": string.atoi(self.pretrian_epochs_line_edit.text()),
            "pretrain_lr": 1.0 / pow(10, self.pretrain_lr_slider.value()),
            "finetune_epochs": string.atoi(self.finetune_epochs_line_edit.text()),
            "finetune_lr": 1.0 / pow(10, self.finetune_lr_slider.value()),
        }

    def getNValue(self):
        return self.n_slider.value() / 100.0

    def getKValue(self):
        return self.k_slider.value() / 100.0

    def getDelayValue(self):
        return self.slider.value() / 100.0
Exemple #58
0
class PolicyLearner:
    def __init__(self,
                 stock_code,
                 chart_data,
                 fig,
                 canvas,
                 training_data=None,
                 min_trading_unit=1,
                 max_trading_unit=2,
                 delayed_reward_threshold=.05,
                 lr=0.01):
        self.stock_code = stock_code  #종목코드
        self.chart_data = chart_data
        self.environment = Environment(chart_data)
        #에이전트 객체
        self.agent = Agent(self.environment,
                           min_trading_unit=min_trading_unit,
                           max_trading_unit=max_trading_unit,
                           delayed_reward_threshold=delayed_reward_threshold)
        self.training_data = training_data
        self.sample = None
        self.training_data_idx = -1
        #정책 신경망;입력 크기 = 학습 데이터의 크기+에이전트 상태 크기
        self.num_features = self.training_data.shape[1] + self.agent.STATE_DIM
        self.policy_network = PolicyNetwork(input_dim=self.num_features,
                                            output_dim=self.agent.NUM_ACTIONS,
                                            lr=lr)
        self.visualizer = Visualizer(fig, canvas)

    #에포크 초기화 함수 부분
    def reset(self):
        self.sample = None
        self.training_data_idx = -1

    def fit(self,
            num_epoches=1000,
            max_memory=60,
            balance=1000000,
            discount_factor=0,
            start_epsilon=5,
            learning=True):
        logger.info(
            "LR: {lr}, DF: {discount_factor}, "
            "TU: [{min_trading_unit}, {max_trading_unit}],"
            "DRT:{delayed_reward_threshold}".format(
                lr=self.policy_network.lr,
                discount_factor=discount_factor,
                min_trading_unit=self.agent.min_trading_unit,
                max_trading_unit=self.agent.max_trading_unit,
                delayed_reward_threshold=self.agent.delayed_reward_threshold))

        #가시화 준비
        #차트 데이터는 변하지 않으므로 미리 가시화
        self.visualizer.prepare(self.environment.chart_data)

        #가시화 결과 저장할 폴더 준비
        epoch_summary_dir = os.path.join(
            settings.BASE_DIR, 'epoch_summary\%s\epoch_summary_%s' %
            (self.stock_code, settings.timestr))
        if not os.path.isdir(epoch_summary_dir):
            os.makedirs(epoch_summary_dir)

        #에이전트 초기 자본금 설정
        self.agent.set_balance(balance)

        #학습에 대한 정보 초기화
        max_portfolio_value = 0
        epoch_win_cnt = 0

        #학습 반복
        for epoch in range(num_epoches):
            #에포크 관련 정보 초기화
            loss = 0.
            itr_cnt = 0
            win_cnt = 0
            exploration_cnt = 0
            batch_size = 0
            pos_learning_cnt = 0
            neg_learning_cnt = 0

            #메모리 초기화
            memory_sample = []
            memory_action = []
            memory_reward = []
            memory_prob = []
            memory_pv = []
            memory_num_stocks = []
            memory_exp_idx = []
            memory_learning_idx = []

            #환경, 에이전트, 정책신경망 초기화
            self.environment.reset()
            self.agent.reset()
            self.policy_network.reset()
            self.reset()

            #가시화기 초기화
            self.visualizer.clear([0, len(self.chart_data)])

            #학습을 진행할수록 탐험 비율 감소
            if learning:
                epsilon = start_epsilon * (1. - float(epoch) /
                                           (num_epoches - 1))
            else:
                epsilon = 0

            while True:
                #샘플 생성
                next_sample = self._build_sample()
                if next_sample is None:
                    break

            #정책 신경망 또는 탐험에 의한 행동 결정
                action, confidence, exploration = self.agent.decide_action(
                    self.policy_network, self.sample, epsilon)

                #결정한 행동을 수행하고 즉시 보상과 지연 보상획득
                immediate_reward, delayed_reward = self.agent.act(
                    action, confidence)

                #행동 및 행동에 대한 결과를 기억
                memory_sample.append(next_sample)
                memory_action.append(action)
                memory_reward.append(immediate_reward)
                memory_pv.append(self.agent.portfolio_value)
                memory_num_stocks.append(self.agent.num_stocks)
                memory = [
                    (memory_sample[i], memory_action[i], memory_reward[i])
                    for i in list(range(len(memory_action)))[-max_memory:]
                ]
                if exploration:
                    memory_exp_idx.append(itr_cnt)
                    memory_prob.append([np.nan] * Agent.NUM_ACTIONS)
                else:
                    memory_prob.append(self.policy_network.prob)

                #반복에 대한 정보갱신
                batch_size += 1
                itr_cnt += 1
                exploration_cnt += 1 if exploration else 0
                win_cnt += 1 if delayed_reward > 0 else 0

                #학습 모드이고 지연 보상이 존재할 경우 정책 신경망 갱신
                if delayed_reward == 0 and batch_size >= max_memory:
                    delayed_reward = immediate_reward
                if learning and delayed_reward != 0:
                    #배치 학습 데이터 크기
                    batch_size = min(batch_size, max_memory)
                    #배치 학습 데이터 생성
                    x, y = self._get_batch(memory, batch_size, discount_factor,
                                           delayed_reward)
                    if len(x) > 0:
                        if delayed_reward > 0:
                            pos_learning_cnt += 1
                        else:
                            neg_learning_cnt += 1
                        #정책 신경망 갱신
                        loss += self.policy_network.train_on_batch(x, y)
                        memory_learning_idx.append([itr_cnt, delayed_reward])
                    batch_size = 0

            #에포크 관련 정보 가시화
            num_epoches_digit = len(str(num_epoches))
            epoch_str = str(epoch + 1).rjust(num_epoches_digit, '0')

            self.visualizer.plot(epoch_str=epoch_str,
                                 num_epoches=num_epoches,
                                 epsilon=epsilon,
                                 action_list=Agent.ACTIONS,
                                 actions=memory_action,
                                 num_stocks=memory_num_stocks,
                                 outvals=memory_prob,
                                 exps=memory_exp_idx,
                                 learning=memory_learning_idx,
                                 initial_balance=self.agent.initial_balance,
                                 pvs=memory_pv)
            # self.visualizer.save(os.path.join(
            #     epoch_summary_dir,'epoch_summary_%s_%s.png' %(
            #         settings.timestr,epoch_str)))
            self.visualizer.show()

            #에포크 관련 정보 로그 기록
            if pos_learning_cnt + neg_learning_cnt > 0:
                loss /= pos_learning_cnt + neg_learning_cnt
            logger.info(
                "[Epoch %s/%s]\tEpsilon:%.4f\t#Expl.:%d/%d\t"
                "#Buy:%d\t#sell:%d\t#Hold:%d\t"
                "#Stocks:%d\tPV:%s\t"
                "#POS:%s\tNEG:%s\tLoss:%10.6f" %
                (epoch_str, num_epoches, epsilon, exploration_cnt, itr_cnt,
                 self.agent.num_buy, self.agent.num_sell, self.agent.num_hold,
                 self.agent.num_stocks,
                 locale.currency(self.agent.portfolio_value, grouping=True),
                 pos_learning_cnt, neg_learning_cnt, loss))

            #학습 관련 정보 갱신
            max_portfolio_value = max(max_portfolio_value,
                                      self.agent.portfolio_value)
            if self.agent.portfolio_value > self.agent.initial_balance:
                epoch_win_cnt += 1

            #학습 관련 정보 로그 기록
            logger.info("Max PV: %s, \t # Win: %d" % (locale.currency(
                max_portfolio_value, grouping=True), epoch_win_cnt))

    def _get_batch(self, memory, batch_size, discount_factor, delayed_reward):
        x = np.zeros((batch_size, 1, self.num_features))
        y = np.full((batch_size, self.agent.NUM_ACTIONS), 0.5)
        for i, (sample, action,
                reward) in enumerate(reversed(memory[-batch_size:])):
            x[i] = np.array(sample).reshape((-1, 1, self.num_features))
            y[i, action] = (delayed_reward + 1) / 2
            if discount_factor > 0:
                y[i, action] *= discount_factor**i
        return x, y

    def _build_sample(self):
        self.environment.observe()
        if len(self.training_data) > self.training_data_idx + 1:
            self.training_data_idx += 1
            self.sample = self.training_data.iloc[
                self.training_data_idx].tolist()
            self.sample.extend(self.agent.get_states())
            return self.sample
        return None

    #투자 시뮬레이션을 하는 trade()함수 부분
    def trade(self, model_path=None, balance=1000000):
        if model_path is None:
            return
        self.policy_network.load_model(model_path=model_path)
        self.fit(balance=balance, num_epoches=1, learning=False)
	def on_resize(self,window,w, h):
		Visualizer.on_resize(self,window,w, h)
		self.trackball.set_window_size(w,h)
Exemple #60
0
    model.load_state_dict(
        torch.load('./checkpoints/' + 'model_{}.pt'.format(opt.load_epoch)))

else:

    def init_weights(m):
        if type(m) == torch.nn.Linear:
            torch.nn.init.xavier_uniform_(m.weight)
            m.bias.data.fill_(0.01)
        elif isinstance(m, torch.nn.Conv2d):
            torch.nn.init.xavier_uniform_(m.weight, gain=np.sqrt(2))

    model.apply(init_weights)

# Visualizer using visdom
vis = Visualizer(opt)

# Loss functons(Cross Entropy)
# Adam optimizer
criterion_sample = torch.nn.CrossEntropyLoss().to(device)
criterion_target = torch.nn.CrossEntropyLoss().to(device)
optimizer = optim.Adam(model.parameters(),
                       lr=opt.lr,
                       weight_decay=opt.weight_decay)

# Variables to store the losses and accuracy in list so that we can plot it later
best_epoch = 0
target_acc_list = []
sample_acc_list = []