def continue_recording_and_dump(self, file_name="EEG_session_live_" + time_str() + ".mat"): ### only save data while still keeping the recording thread alive ### the data can then be used to classify the notlive data sio.savemat(os.path.join(self.datapath, file_name), dict(self)) return file_name, self.datapath
def update(self, events): """Updates the game screen""" super().update(events) # kill the start text if it already exists if self.game_started: self.start_text.kill() self.time_text.set_text(time_str(self.current_time())) # do collision detection Collider.collide_all(self.player, self.containers['ENEMY']['BULLET']) Collider.collide_all(self.enemy, self.containers['PLAYER']['BULLET']) Collider.collide_all(self.player, self.containers['ENEMY']['ENTITY']) # transition to end screen if game ended if self.game_ended: self.screen_transition( end_screen.EndScreen(self.game, self.game_won, self.current_time(), self.diff)) # if player is dead, get ready to screen transition next frame if self.player.health.dead(): self.game_ended = True self.full_screen_rectangle((255, 0, 0)) # if enemy is dead, also get ready to transition if self.enemy.health.dead(): self.game_ended = True self.game_won = True self.full_screen_rectangle((255, 255, 255))
def stop_recording_and_dump_live(self, file_name="session_live_" + time_str() + ".mat"): #self.set_trial_start_indexes() #solved by collecting index in add_trial() sio.savemat(file_name, dict(self)) return file_name
def pause_recording_and_dump(self, file_name="EEG_session_live_" + time_str() + ".mat"): ### save data to .mat and pause the recording such that it can be resumed lateron sio.savemat(os.path.join(self.datapath, file_name), dict(self)) self.pause_recording() return file_name, self.datapath
def stop_recording_and_dump(self, file_name="EEG_session_" + time_str() + ".mat"): # finish the recording, save all data to a .mat file self.pause_recording() self.stop_event_con.set() sio.savemat(os.path.join(self.datapath, file_name), dict(self)) print('Recording will shut down.') return file_name, self.datapath
def __init__(self, game, diff=Difficulty.NORMAL): """Create the PlayScreen""" super().__init__(game) self.game_started = False self.game_ended = False self.game_won = False self.start_time = 0 self.diff = diff # create containers self.containers = {} for alignment in ['PLAYER', 'ENEMY']: self.containers[alignment] = {} for obj_type in ['ENTITY', 'BULLET']: self.containers[alignment][obj_type] = pygame.sprite.Group() # create objects self.start_circle = GameStartCircle(self, self.PLAYER_START) self.player = Player(self, self.PLAYER_START, diff) self.start_text = TextSprite( self.everything, "Put your cursor in the circle to start the game!", (320, 330), 16) self.time_text = TextSprite(self.everything, time_str(0), (570, 620), 16) self.enemy = Enemy(self, self.ENEMY_START)
def record(stop_event, channel_data=[], time_stamps=[]): ### this is the old recording function, that is started as a thread in the class RecordData inlet = find_gUSBamp_stream('EEG') inletInfo = inlet.info() print('Connected to:', inletInfo.name(), 'with', inletInfo.channel_count(), 'channels. Fs:', inletInfo.nominal_srate()) ### do recording all the time, but to pause the recording a stop event is set. ### agterwards the thread is newly initialised to restart while not stop_event.is_set(): #True: try: sample, time_stamp = inlet.pull_sample() time_stamp += inlet.time_correction() time_stamps.append(time_stamp) channel_data.append(sample) except KeyboardInterrupt: ### save data and exit on KeybordInterrupt complete_samples = min(len(time_stamps), len(channel_data)) sio.savemat( "recording_" + time_str() + ".mat", { "time_stamps": time_stamps[:complete_samples], "channel_data": channel_data[:complete_samples], }) break
def stop_recording_and_dump(self, file_name="session_" + time_str() + ".mat"): self.set_trial_start_indexes() sio.savemat(file_name, dict(self)) self.serialPort.close() return file_name
def stop_recording_and_dump(self, file_name="EMG_session_" + time_str() + ".mat"): ### finish the recording, save all data to a .mat file, close serial port self.pause_recording() sio.savemat(os.path.join(self.datapath, file_name), dict(self)) print('Recording will shut down.') if self.serialPort: self.serialPort.close() return file_name, self.datapath
def main(): # parse command line input opt = utils.parse_arg() # Set GPU opt.cuda = opt.gpuid >= 0 if opt.cuda: torch.cuda.set_device(opt.gpuid) else: utils.time_str("GPU acceleration is disabled.") # prepare data db = data_prepare.prepare_db(opt) imagenet = data_prepare.ImageNetSmallData(opt, type='centres') # imagenet = None # # # add imagenet dataset db.update({'imagenet': imagenet}) # initialize the model pre_trained_model = model.prepare_model(opt) # prepare M_0(x) model, which is a fixed pre-trained model opt.num_output = 1000 fixed_model = model.prepare_model(opt) # prepare centres if not os.path.exists('../datasets/imagenet/train_centres.txt'): imagenet = data_prepare.ImageNetSmallData(opt, type='all') trainer.prepare_centres(fixed_model, imagenet, opt) # configurate the optimizer optim, sche = optimizer.prepare_optim(pre_trained_model, opt) # train the model trainer.train(pre_trained_model, optim, sche, db, opt, model_0=fixed_model) # trainer.train(pre_trained_model, optim, sche, db, opt, model_0 = None) # save the trained model if opt.save: utils.save_model(pre_trained_model, opt)
def set_finite_extend(self, increment=False): if common.gopts.mode == "updr": return changed = False for tt in sorted(self._sorts, key=str): if tt not in self._sort2fin: continue ri = "1" if (not increment): eprint("Extend %s ? " % (str(tt)), end='') if (len(self._sort2fin) > 1): ri = raw_input("") else: eprint() if ri: try: delta = int(ri) if delta >= 0: sz = self.extend_sort(tt, delta) if delta > 0: eprint(time_str(), "(extended |%s| to %d)" % (str(tt), sz)) print(time_str(), "(extended |%s| to %d)" % (str(tt), sz)) changed = True else: self.unbound_sort(tt) eprint("\t(unbounding %s)" % (str(tt))) changed = True except ValueError: pass if changed: # eprint("Finite sorts (new): #%d" % len(self._sort2fin)) # for tt, vals in self._sort2fin.items(): # eprint("\t|%s| = %s" % (tt, len(self._enumsorts[vals]))) pass
def record(ser, channel_data=[], channel_force=[], time_stamps=[]): streams = pylsl.resolve_stream('type', 'EEG') inlet = pylsl.stream_inlet(streams[0]) while True: try: sample, time_stamp = inlet.pull_sample() time_stamp += inlet.time_correction() time_stamps.append(time_stamp) channel_data.append(sample) #output_str = ser.readline() """ if output_str.endswith('\n'): output_str = output_str.translate(None, ' \n\t\r') try: force_sample = list(map(float, output_str.split(';'))) if len(force_sample) != 5: force_sample = [0, 0, 0, 0, 0] except ValueError: force_sample = [0, 0, 0, 0, 0] else: force_sample = [0, 0, 0, 0, 0] try: channel_force.append(float(output_str)) except ValueError: channel_force.append(0) """ try: channel_force.append(float(ser.readline())) except ValueError: channel_force.append(0) # first col of one row of the record_data matrix is time_stamp, # the following cols are the sampled channels except KeyboardInterrupt: complete_samples = min(len(time_stamps), len(channel_data), len(channel_force)) sio.savemat( "recording_" + time_str() + ".mat", { "time_stamps": time_stamps[:complete_samples], "channel_data": channel_data[:complete_samples], "channel_force": channel_force[:complete_samples] }) break
def main(): # parse command line input opt = utils.parse_arg() # Set GPU opt.cuda = opt.gpuid >= 0 if opt.cuda: torch.cuda.set_device(opt.gpuid) else: utils.time_str("GPU acceleration is disabled.") # prepare data db = data_prepare.prepare_db(opt) # initializa the model pre_trained_model = model.prepare_model(opt) # configurate the optimizer optim, sche = optimizer.prepare_optim(pre_trained_model, opt) # train the model trainer.train(pre_trained_model, optim, sche, db, opt) # save the trained model utils.save_model(pre_trained_model, opt)
def record(ser, stop_event, channel_data=[], channel_force=[], time_stamps=[]): ### this is the recording function, that is started as a thread in the class RecordData inlet = find_gUSBamp_stream('EMG') inletInfo = inlet.info() print('Connected to:', inletInfo.name(), 'with', inletInfo.channel_count(), 'channels. Fs:', inletInfo.nominal_srate()) #inlet2 = find_gUSBamp_stream('EEG') #inletInfo2 = inlet2.info() #print('Connected to:',inletInfo2.name(), 'with', inletInfo2.channel_count(),'channels. Fs:',inletInfo2.nominal_srate()) ### do recording all the time, but to pause the recording a stop event is set. ### agterwards the thread is newly initialised to restart while not stop_event.is_set(): #True: try: sample, time_stamp = inlet.pull_sample() time_stamp += inlet.time_correction() time_stamps.append(time_stamp) channel_data.append(sample) if ser: ### if no arduino for force recording is connected, then fill with 0 try: channel_force.append(float(ser.readline())) except ValueError: channel_force.append(0) except TypeError: channel_force.append(0) else: channel_force.append(0) except KeyboardInterrupt: ### save data and exit on KeybordInterrupt complete_samples = min(len(time_stamps), len(channel_data), len(channel_force)) sio.savemat( "recording_" + time_str() + ".mat", { "time_stamps": time_stamps[:complete_samples], "channel_data": channel_data[:complete_samples], "channel_force": channel_force[:complete_samples] }) break
def record(channel_data=[], time_stamps=[]): streams = pylsl.resolve_stream('type', 'EEG') inlet = pylsl.stream_inlet(streams[0]) while True: try: sample, time_stamp = inlet.pull_sample() time_stamp += inlet.time_correction() time_stamps.append(time_stamp) channel_data.append(sample) # first col of one row of the record_data matrix is time_stamp, # the following cols are the sampled channels except KeyboardInterrupt: complete_samples = min(len(time_stamps), len(channel_data)) sio.savemat( "recording_" + time_str() + ".mat", { "time_stamps": time_stamps[:complete_samples], "channel_data": channel_data[:complete_samples] }) break
def __init__(self, game, game_won, time_taken, diff): """Creates the EndScreen""" super().__init__(game) self.game_won = game_won self.time_taken = time_taken self.diff = diff # display either YOU WIN or YOU LOSE if game_won: TextSprite(self.everything, "YOU WIN", self.LOSE_WIN_LOC, self.LOSE_WIN_SIZE) else: TextSprite(self.everything, "YOU LOSE", self.LOSE_WIN_LOC, self.LOSE_WIN_SIZE, color=(255, 0, 0)) # display time taken TextSprite(self.everything, time_str(time_taken, 0), (320, 240)) # create a continue button Button(self.everything, "CONTINUE", np.array((320, 400)), np.array((200, 100)), self.next_screen)
def record_and_process(stop_event, channel_data=[], time_stamps=[]): ### based on realtime.py # 0. General #verbose = False # 1. Output output_width = 32 output_height = 32 output_stacks = 3 # channels outlet_sendRate = 2 # [Hz] outlet_numChannels = output_width * output_height * output_stacks # 2. Filterbank lowcut = 2 # [Hz] highcut = 60 # [Hz] order = 3 # 3. Spectrogram Generation periods = 1.5 overlapRatio = 0.95 ### initialise data inlet and outlet inlet = find_gUSBamp_stream('EEG') inletInfo = inlet.info() inlet_sampleRate = int(inletInfo.nominal_srate()) inlet_numChannels = int(inletInfo.channel_count()) print("Reported sample rate: %i , number of channels: %i" % (inlet_sampleRate, inlet_numChannels)) outletInfo = pylsl.StreamInfo('PreprocessedEEG', 'EEGPre', outlet_numChannels, outlet_sendRate, 'int8', 'UB-2016.08.03') outlet = pylsl.StreamOutlet(outletInfo) ### initialise processing: Filterbank, Spectrogram generation filterbank = lp.filterbank(lowcut, highcut, order, inlet_sampleRate) specGen = lp.specGen(output_width, output_height, output_stacks, lowcut, periods, overlapRatio, inlet_sampleRate) ### initialise ringbuffer rbuffer = ringbuffer.RingBuffer(size_max=specGen.smpPerSpec) sendEverySmpl = math.ceil(inlet_sampleRate / outlet_sendRate) print("Transmitting every %i samples" % sendEverySmpl) samplesInBuffer = 0 samplesSent = 0 while not stop_event.is_set(): try: sample, time_stamp = inlet.pull_sample() time_stamp += inlet.time_correction() time_stamps.append(time_stamp) channel_data.append(sample) rbuffer.append(sample) samplesInBuffer += 1 except KeyboardInterrupt: ### save data and exit on KeybordInterrupt complete_samples = min(len(time_stamps), len(channel_data)) sio.savemat( "recording_" + time_str() + ".mat", { "time_stamps": time_stamps[:complete_samples], "channel_data": channel_data[:complete_samples], }) break if (rbuffer.full and samplesInBuffer >= sendEverySmpl): ### get from buffer, filter and generate spectrogram specs = specGen.process( filterbank.process(np.array(rbuffer.get())[:, 0:3])) ### convert to uint8 and flatten to send it to the LSL outlet.push_sample(lp.np_to_tn(specs).flatten()) samplesSent += 1 ### indicate that buffer content is used, such that it can be overwritten samplesInBuffer = 0
def update_all_timer(self): s = time_str(self.record_time) self.enrollTime.setText(s) self.recoTime.setText(s) self.convTime.setText(s)
def timer_callback(self): self.record_time += 1 self.status("Recording..." + time_str(self.record_time)) self.update_all_timer()
def main(config): svname = args.name if svname is None: svname = 'classifier_{}'.format(config['train_dataset']) svname += '_' + config['model_args']['encoder'] clsfr = config['model_args']['classifier'] if clsfr != 'linear-classifier': svname += '-' + clsfr if args.tag is not None: svname += '_' + args.tag save_path = os.path.join('./save', svname) utils.ensure_path(save_path) utils.set_log_path(save_path) writer = SummaryWriter(os.path.join(save_path, 'tensorboard')) yaml.dump(config, open(os.path.join(save_path, 'config.yaml'), 'w')) #### Dataset #### # train train_dataset = datasets.make(config['train_dataset'], **config['train_dataset_args']) augmentations = [ transforms.Compose([ transforms.RandomHorizontalFlip(p=0.5), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), transforms.Compose([ transforms.RandomResizedCrop(size=(80, 80), scale=(0.08, 1.0), ratio=(0.75, 1.3333)), transforms.RandomHorizontalFlip(p=0.5), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), transforms.Compose([ transforms.RandomRotation(35), transforms.RandomHorizontalFlip(p=0.5), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), transforms.Compose([ transforms.ColorJitter(0.4, 0.4, 0.4, 0.1), transforms.RandomHorizontalFlip(p=0.5), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), transforms.Compose([ transforms.RandomResizedCrop(size=(80, 80), scale=(0.08, 1.0), ratio=(0.75, 1.3333)), transforms.RandomRotation(35), transforms.RandomHorizontalFlip(p=0.5), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), transforms.Compose([ transforms.RandomRotation(35), transforms.ColorJitter(0.4, 0.4, 0.4, 0.1), transforms.RandomHorizontalFlip(p=0.5), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), transforms.Compose([ transforms.RandomResizedCrop(size=(80, 80), scale=(0.08, 1.0), ratio=(0.75, 1.3333)), transforms.ColorJitter(0.4, 0.4, 0.4, 0.1), transforms.RandomHorizontalFlip(p=0.5), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), transforms.Compose([ transforms.RandomRotation(35), transforms.RandomResizedCrop(size=(80, 80), scale=(0.08, 1.0), ratio=(0.75, 1.3333)), transforms.ColorJitter(0.4, 0.4, 0.4, 0.1), transforms.RandomHorizontalFlip(p=0.5), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) ] train_dataset.transform = augmentations[int(config['_a'])] print(train_dataset.transform) print("_a", config['_a']) input("Continue with these augmentations?") train_loader = DataLoader(train_dataset, config['batch_size'], shuffle=True, num_workers=0, pin_memory=True) utils.log('train dataset: {} (x{}), {}'.format(train_dataset[0][0].shape, len(train_dataset), train_dataset.n_classes)) if config.get('visualize_datasets'): utils.visualize_dataset(train_dataset, 'train_dataset', writer) # val if config.get('val_dataset'): eval_val = True val_dataset = datasets.make(config['val_dataset'], **config['val_dataset_args']) val_loader = DataLoader(val_dataset, config['batch_size'], num_workers=0, pin_memory=True) utils.log('val dataset: {} (x{}), {}'.format(val_dataset[0][0].shape, len(val_dataset), val_dataset.n_classes)) if config.get('visualize_datasets'): utils.visualize_dataset(val_dataset, 'val_dataset', writer) else: eval_val = False # few-shot eval if config.get('fs_dataset'): ef_epoch = config.get('eval_fs_epoch') if ef_epoch is None: ef_epoch = 5 eval_fs = True fs_dataset = datasets.make(config['fs_dataset'], **config['fs_dataset_args']) utils.log('fs dataset: {} (x{}), {}'.format(fs_dataset[0][0].shape, len(fs_dataset), fs_dataset.n_classes)) if config.get('visualize_datasets'): utils.visualize_dataset(fs_dataset, 'fs_dataset', writer) n_way = 5 n_query = 15 n_shots = [1, 5] fs_loaders = [] for n_shot in n_shots: fs_sampler = CategoriesSampler(fs_dataset.label, 200, n_way, n_shot + n_query, ep_per_batch=4) fs_loader = DataLoader(fs_dataset, batch_sampler=fs_sampler, num_workers=0, pin_memory=True) fs_loaders.append(fs_loader) else: eval_fs = False ######## #### Model and Optimizer #### if config.get('load'): model_sv = torch.load(config['load']) model = models.load(model_sv) else: model = models.make(config['model'], **config['model_args']) if eval_fs: fs_model = models.make('meta-baseline', encoder=None) fs_model.encoder = model.encoder if config.get('_parallel'): model = nn.DataParallel(model) if eval_fs: fs_model = nn.DataParallel(fs_model) utils.log('num params: {}'.format(utils.compute_n_params(model))) optimizer, lr_scheduler = utils.make_optimizer(model.parameters(), config['optimizer'], **config['optimizer_args']) ######## max_epoch = config['max_epoch'] save_epoch = config.get('save_epoch') max_va = 0. timer_used = utils.Timer() timer_epoch = utils.Timer() for epoch in range(1, max_epoch + 1 + 1): if epoch == max_epoch + 1: if not config.get('epoch_ex'): break train_dataset.transform = train_dataset.default_transform print(train_dataset.transform) train_loader = DataLoader(train_dataset, config['batch_size'], shuffle=True, num_workers=0, pin_memory=True) timer_epoch.s() aves_keys = ['tl', 'ta', 'vl', 'va'] if eval_fs: for n_shot in n_shots: aves_keys += ['fsa-' + str(n_shot)] aves = {k: utils.Averager() for k in aves_keys} # train model.train() writer.add_scalar('lr', optimizer.param_groups[0]['lr'], epoch) for data, label in tqdm(train_loader, desc='train', leave=False): # for data, label in train_loader: data, label = data.cuda(), label.cuda() logits = model(data) loss = F.cross_entropy(logits, label) acc = utils.compute_acc(logits, label) optimizer.zero_grad() loss.backward() optimizer.step() aves['tl'].add(loss.item()) aves['ta'].add(acc) logits = None loss = None # eval if eval_val: model.eval() for data, label in tqdm(val_loader, desc='val', leave=False): data, label = data.cuda(), label.cuda() with torch.no_grad(): logits = model(data) loss = F.cross_entropy(logits, label) acc = utils.compute_acc(logits, label) aves['vl'].add(loss.item()) aves['va'].add(acc) if eval_fs and (epoch % ef_epoch == 0 or epoch == max_epoch + 1): fs_model.eval() for i, n_shot in enumerate(n_shots): np.random.seed(0) for data, _ in tqdm(fs_loaders[i], desc='fs-' + str(n_shot), leave=False): x_shot, x_query = fs.split_shot_query(data.cuda(), n_way, n_shot, n_query, ep_per_batch=4) label = fs.make_nk_label(n_way, n_query, ep_per_batch=4).cuda() with torch.no_grad(): logits = fs_model(x_shot, x_query).view(-1, n_way) acc = utils.compute_acc(logits, label) aves['fsa-' + str(n_shot)].add(acc) # post if lr_scheduler is not None: lr_scheduler.step() for k, v in aves.items(): aves[k] = v.item() t_epoch = utils.time_str(timer_epoch.t()) t_used = utils.time_str(timer_used.t()) t_estimate = utils.time_str(timer_used.t() / epoch * max_epoch) if epoch <= max_epoch: epoch_str = str(epoch) else: epoch_str = 'ex' log_str = 'epoch {}, train {:.4f}|{:.4f}'.format( epoch_str, aves['tl'], aves['ta']) writer.add_scalars('loss', {'train': aves['tl']}, epoch) writer.add_scalars('acc', {'train': aves['ta']}, epoch) if eval_val: log_str += ', val {:.4f}|{:.4f}'.format(aves['vl'], aves['va']) writer.add_scalars('loss', {'val': aves['vl']}, epoch) writer.add_scalars('acc', {'val': aves['va']}, epoch) if eval_fs and (epoch % ef_epoch == 0 or epoch == max_epoch + 1): log_str += ', fs' for n_shot in n_shots: key = 'fsa-' + str(n_shot) log_str += ' {}: {:.4f}'.format(n_shot, aves[key]) writer.add_scalars('acc', {key: aves[key]}, epoch) if epoch <= max_epoch: log_str += ', {} {}/{}'.format(t_epoch, t_used, t_estimate) else: log_str += ', {}'.format(t_epoch) utils.log(log_str) if config.get('_parallel'): model_ = model.module else: model_ = model training = { 'epoch': epoch, 'optimizer': config['optimizer'], 'optimizer_args': config['optimizer_args'], 'optimizer_sd': optimizer.state_dict(), } save_obj = { 'file': __file__, 'config': config, 'model': config['model'], 'model_args': config['model_args'], 'model_sd': model_.state_dict(), 'training': training, } if epoch <= max_epoch: torch.save(save_obj, os.path.join(save_path, 'epoch-last.pth')) if (save_epoch is not None) and epoch % save_epoch == 0: torch.save( save_obj, os.path.join(save_path, 'epoch-{}.pth'.format(epoch))) if aves['va'] > max_va: max_va = aves['va'] torch.save(save_obj, os.path.join(save_path, 'max-va.pth')) else: torch.save(save_obj, os.path.join(save_path, 'epoch-ex.pth')) writer.flush()
if IS_DEBUGGING: config.eps_anneal_period = 1e5 config.replay_buffer_size = int(1e3) ## Derived settings prepro = utils.Preprocessor_2d(config.num_state, gray=True) env = utils.EnvironmentInterface(config, prepro, action_repeats=4, obs_buffer_size=4) for i in range(1): print('i', i) # run_name = experiment_name or utils.time_str() run_name = experiment_name + utils.time_str() logdir = './logdir/' + config.env_name + '/DQN/' + run_name print('logdir\t\t', logdir) if IS_DEBUGGING: logdir += '_debug' ## Setup tf.reset_default_graph() random_seed = int(time.time()) tf.set_random_seed(random_seed) np.random.seed(random_seed) print('random seed\t', random_seed) print('Creating new model') agent = modelDQN.DQN(config, env, logdir,
def test_time_str_no_ljust(self): res = time_str(0, ljust=0) self.assertEqual("Time: 0", res) res = time_str(142, ljust=0) self.assertEqual("Time: 142", res)
def save_model_and_loss(model, moder_root, losses, loss_root): t_str = time_str() torch.save(model.state_dict(), join(moder_root, 'model' + t_str + '.pth')) show_losses(join(loss_root, 'loss' + t_str + '.png'), losses=losses) logging.info('Checkpoint & Losses saved !')
def main(config): svname = args.name if svname is None: svname = 'meta_{}-{}shot'.format(config['train_dataset'], config['n_shot']) svname += '_' + config['model'] if config['model_args'].get('encoder'): svname += '-' + config['model_args']['encoder'] if config['model_args'].get('prog_synthesis'): svname += '-' + config['model_args']['prog_synthesis'] svname += '-seed' + str(args.seed) if args.tag is not None: svname += '_' + args.tag save_path = os.path.join(args.save_dir, svname) utils.ensure_path(save_path, remove=False) utils.set_log_path(save_path) writer = SummaryWriter(os.path.join(save_path, 'tensorboard')) yaml.dump(config, open(os.path.join(save_path, 'config.yaml'), 'w')) logger = utils.Logger(file_name=os.path.join(save_path, "log_sdout.txt"), file_mode="a+", should_flush=True) #### Dataset #### n_way, n_shot = config['n_way'], config['n_shot'] n_query = config['n_query'] if config.get('n_train_way') is not None: n_train_way = config['n_train_way'] else: n_train_way = n_way if config.get('n_train_shot') is not None: n_train_shot = config['n_train_shot'] else: n_train_shot = n_shot if config.get('ep_per_batch') is not None: ep_per_batch = config['ep_per_batch'] else: ep_per_batch = 1 random_state = np.random.RandomState(args.seed) print('seed:', args.seed) # train train_dataset = datasets.make(config['train_dataset'], **config['train_dataset_args']) utils.log('train dataset: {} (x{})'.format(train_dataset[0][0].shape, len(train_dataset))) if config.get('visualize_datasets'): utils.visualize_dataset(train_dataset, 'train_dataset', writer) train_sampler = BongardSampler(train_dataset.n_tasks, config['train_batches'], ep_per_batch, random_state.randint(2**31)) train_loader = DataLoader(train_dataset, batch_sampler=train_sampler, num_workers=8, pin_memory=True) # tvals tval_loaders = {} tval_name_ntasks_dict = { 'tval': 2000, 'tval_ff': 600, 'tval_bd': 480, 'tval_hd_comb': 400, 'tval_hd_novel': 320 } # numbers depend on dataset for tval_type in tval_name_ntasks_dict.keys(): if config.get('{}_dataset'.format(tval_type)): tval_dataset = datasets.make( config['{}_dataset'.format(tval_type)], **config['{}_dataset_args'.format(tval_type)]) utils.log('{} dataset: {} (x{})'.format(tval_type, tval_dataset[0][0].shape, len(tval_dataset))) if config.get('visualize_datasets'): utils.visualize_dataset(tval_dataset, 'tval_ff_dataset', writer) tval_sampler = BongardSampler( tval_dataset.n_tasks, n_batch=tval_name_ntasks_dict[tval_type] // ep_per_batch, ep_per_batch=ep_per_batch, seed=random_state.randint(2**31)) tval_loader = DataLoader(tval_dataset, batch_sampler=tval_sampler, num_workers=8, pin_memory=True) tval_loaders.update({tval_type: tval_loader}) else: tval_loaders.update({tval_type: None}) # val val_dataset = datasets.make(config['val_dataset'], **config['val_dataset_args']) utils.log('val dataset: {} (x{})'.format(val_dataset[0][0].shape, len(val_dataset))) if config.get('visualize_datasets'): utils.visualize_dataset(val_dataset, 'val_dataset', writer) val_sampler = BongardSampler(val_dataset.n_tasks, n_batch=900 // ep_per_batch, ep_per_batch=ep_per_batch, seed=random_state.randint(2**31)) val_loader = DataLoader(val_dataset, batch_sampler=val_sampler, num_workers=8, pin_memory=True) ######## #### Model and optimizer #### if config.get('load'): print('loading pretrained model: ', config['load']) model = models.load(torch.load(config['load'])) else: model = models.make(config['model'], **config['model_args']) if config.get('load_encoder'): print('loading pretrained encoder: ', config['load_encoder']) encoder = models.load(torch.load(config['load_encoder'])).encoder model.encoder.load_state_dict(encoder.state_dict()) if config.get('load_prog_synthesis'): print('loading pretrained program synthesis model: ', config['load_prog_synthesis']) prog_synthesis = models.load( torch.load(config['load_prog_synthesis'])) model.prog_synthesis.load_state_dict(prog_synthesis.state_dict()) if config.get('_parallel'): model = nn.DataParallel(model) utils.log('num params: {}'.format(utils.compute_n_params(model))) optimizer, lr_scheduler = utils.make_optimizer(model.parameters(), config['optimizer'], **config['optimizer_args']) ######## max_epoch = config['max_epoch'] save_epoch = config.get('save_epoch') max_va = 0. timer_used = utils.Timer() timer_epoch = utils.Timer() aves_keys = ['tl', 'ta', 'vl', 'va'] tval_tuple_lst = [] for k, v in tval_loaders.items(): if v is not None: loss_key = 'tvl' + k.split('tval')[-1] acc_key = ' tva' + k.split('tval')[-1] aves_keys.append(loss_key) aves_keys.append(acc_key) tval_tuple_lst.append((k, v, loss_key, acc_key)) trlog = dict() for k in aves_keys: trlog[k] = [] for epoch in range(1, max_epoch + 1): timer_epoch.s() aves = {k: utils.Averager() for k in aves_keys} # train model.train() if config.get('freeze_bn'): utils.freeze_bn(model) writer.add_scalar('lr', optimizer.param_groups[0]['lr'], epoch) for data, label in tqdm(train_loader, desc='train', leave=False): x_shot, x_query = fs.split_shot_query(data.cuda(), n_train_way, n_train_shot, n_query, ep_per_batch=ep_per_batch) label_query = fs.make_nk_label(n_train_way, n_query, ep_per_batch=ep_per_batch).cuda() if config['model'] == 'snail': # only use one selected label_query query_dix = random_state.randint(n_train_way * n_query) label_query = label_query.view(ep_per_batch, -1)[:, query_dix] x_query = x_query[:, query_dix:query_dix + 1] if config['model'] == 'maml': # need grad in maml model.zero_grad() logits = model(x_shot, x_query).view(-1, n_train_way) loss = F.cross_entropy(logits, label_query) acc = utils.compute_acc(logits, label_query) optimizer.zero_grad() loss.backward() optimizer.step() aves['tl'].add(loss.item()) aves['ta'].add(acc) logits = None loss = None # eval model.eval() for name, loader, name_l, name_a in [('val', val_loader, 'vl', 'va') ] + tval_tuple_lst: if config.get('{}_dataset'.format(name)) is None: continue np.random.seed(0) for data, _ in tqdm(loader, desc=name, leave=False): x_shot, x_query = fs.split_shot_query( data.cuda(), n_way, n_shot, n_query, ep_per_batch=ep_per_batch) label_query = fs.make_nk_label( n_way, n_query, ep_per_batch=ep_per_batch).cuda() if config[ 'model'] == 'snail': # only use one randomly selected label_query query_dix = random_state.randint(n_train_way) label_query = label_query.view(ep_per_batch, -1)[:, query_dix] x_query = x_query[:, query_dix:query_dix + 1] if config['model'] == 'maml': # need grad in maml model.zero_grad() logits = model(x_shot, x_query, eval=True).view(-1, n_way) loss = F.cross_entropy(logits, label_query) acc = utils.compute_acc(logits, label_query) else: with torch.no_grad(): logits = model(x_shot, x_query, eval=True).view(-1, n_way) loss = F.cross_entropy(logits, label_query) acc = utils.compute_acc(logits, label_query) aves[name_l].add(loss.item()) aves[name_a].add(acc) # post if lr_scheduler is not None: lr_scheduler.step() for k, v in aves.items(): aves[k] = v.item() trlog[k].append(aves[k]) t_epoch = utils.time_str(timer_epoch.t()) t_used = utils.time_str(timer_used.t()) t_estimate = utils.time_str(timer_used.t() / epoch * max_epoch) log_str = 'epoch {}, train {:.4f}|{:.4f}, val {:.4f}|{:.4f}'.format( epoch, aves['tl'], aves['ta'], aves['vl'], aves['va']) for tval_name, _, loss_key, acc_key in tval_tuple_lst: log_str += ', {} {:.4f}|{:.4f}'.format(tval_name, aves[loss_key], aves[acc_key]) writer.add_scalars('loss', {tval_name: aves[loss_key]}, epoch) writer.add_scalars('acc', {tval_name: aves[acc_key]}, epoch) log_str += ', {} {}/{}'.format(t_epoch, t_used, t_estimate) utils.log(log_str) writer.add_scalars('loss', { 'train': aves['tl'], 'val': aves['vl'], }, epoch) writer.add_scalars('acc', { 'train': aves['ta'], 'val': aves['va'], }, epoch) if config.get('_parallel'): model_ = model.module else: model_ = model training = { 'epoch': epoch, 'optimizer': config['optimizer'], 'optimizer_args': config['optimizer_args'], 'optimizer_sd': optimizer.state_dict(), } save_obj = { 'file': __file__, 'config': config, 'model': config['model'], 'model_args': config['model_args'], 'model_sd': model_.state_dict(), 'training': training, } torch.save(save_obj, os.path.join(save_path, 'epoch-last.pth')) torch.save(trlog, os.path.join(save_path, 'trlog.pth')) if (save_epoch is not None) and epoch % save_epoch == 0: torch.save(save_obj, os.path.join(save_path, 'epoch-{}.pth'.format(epoch))) if aves['va'] > max_va: max_va = aves['va'] torch.save(save_obj, os.path.join(save_path, 'max-va.pth')) writer.flush() print('finished training!') logger.close()
def stop_recording_and_dump_live(self, file_name="EEG_session_live_" + time_str() + ".mat"): ### still there for historic reasons, to support run_session by Mirjam Hemberger return self.continue_recording_and_dump()
def main(config): svname = args.name if svname is None: svname = 'meta' if args.tag is not None: svname += '_' + args.tag save_path = os.path.join('./save', svname) utils.ensure_path(save_path) utils.set_log_path(save_path) writer = SummaryWriter(os.path.join(save_path, 'tensorboard')) yaml.dump(config, open(os.path.join(save_path, 'config.yaml'), 'w')) #### Dataset #### if args.dataset == 'all': train_lst = ['ilsvrc_2012', 'omniglot', 'aircraft', 'cu_birds', 'dtd', 'quickdraw', 'fungi', 'vgg_flower'] eval_lst = ['ilsvrc_2012'] else: train_lst = [args.dataset] eval_lst = [args.dataset] if config.get('no_train') == True: train_iter = None else: trainset = make_md(train_lst, 'episodic', split='train', image_size=126) train_iter = trainset.make_one_shot_iterator().get_next() if config.get('no_val') == True: val_iter = None else: valset = make_md(eval_lst, 'episodic', split='val', image_size=126) val_iter = valset.make_one_shot_iterator().get_next() testset = make_md(eval_lst, 'episodic', split='test', image_size=126) test_iter = testset.make_one_shot_iterator().get_next() sess = tf.Session() ######## #### Model and optimizer #### if config.get('load'): model_sv = torch.load(config['load']) model = models.load(model_sv) else: model = models.make(config['model'], **config['model_args']) if config.get('load_encoder'): encoder = models.load(torch.load(config['load_encoder'])).encoder model.encoder.load_state_dict(encoder.state_dict()) if config.get('_parallel'): model = nn.DataParallel(model) utils.log('num params: {}'.format(utils.compute_n_params(model))) optimizer, lr_scheduler = utils.make_optimizer( model.parameters(), config['optimizer'], **config['optimizer_args']) ######## max_epoch = config['max_epoch'] save_epoch = config.get('save_epoch') max_va = 0. timer_used = utils.Timer() timer_epoch = utils.Timer() aves_keys = ['tl', 'ta', 'tvl', 'tva', 'vl', 'va'] trlog = dict() for k in aves_keys: trlog[k] = [] def process_data(e): e = list(e[0]) transform = transforms.Compose([ transforms.ToPILImage(), transforms.Resize(146), transforms.CenterCrop(128), transforms.ToTensor(), transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) for ii in [0, 3]: e[ii] = ((e[ii] + 1.0) * 0.5 * 255).astype('uint8') tmp = torch.zeros(len(e[ii]), 3, 128, 128).float() for i in range(len(e[ii])): tmp[i] = transform(e[ii][i]) e[ii] = tmp.cuda() e[1] = torch.from_numpy(e[1]).long().cuda() e[4] = torch.from_numpy(e[4]).long().cuda() return e for epoch in range(1, max_epoch + 1): timer_epoch.s() aves = {k: utils.Averager() for k in aves_keys} # train model.train() if config.get('freeze_bn'): utils.freeze_bn(model) writer.add_scalar('lr', optimizer.param_groups[0]['lr'], epoch) if config.get('no_train') == True: pass else: for i_ep in tqdm(range(config['n_train'])): e = process_data(sess.run(train_iter)) loss, acc = model(e[0], e[1], e[3], e[4]) optimizer.zero_grad() loss.backward() optimizer.step() aves['tl'].add(loss.item()) aves['ta'].add(acc) loss = None # eval model.eval() for name, ds_iter, name_l, name_a in [ ('tval', val_iter, 'tvl', 'tva'), ('val', test_iter, 'vl', 'va')]: if config.get('no_val') == True and name == 'tval': continue for i_ep in tqdm(range(config['n_eval'])): e = process_data(sess.run(ds_iter)) with torch.no_grad(): loss, acc = model(e[0], e[1], e[3], e[4]) aves[name_l].add(loss.item()) aves[name_a].add(acc) # post if lr_scheduler is not None: lr_scheduler.step() for k, v in aves.items(): aves[k] = v.item() trlog[k].append(aves[k]) _sig = 0 t_epoch = utils.time_str(timer_epoch.t()) t_used = utils.time_str(timer_used.t()) t_estimate = utils.time_str(timer_used.t() / epoch * max_epoch) utils.log('epoch {}, train {:.4f}|{:.4f}, tval {:.4f}|{:.4f}, ' 'val {:.4f}|{:.4f}, {} {}/{} (@{})'.format( epoch, aves['tl'], aves['ta'], aves['tvl'], aves['tva'], aves['vl'], aves['va'], t_epoch, t_used, t_estimate, _sig)) writer.add_scalars('loss', { 'train': aves['tl'], 'tval': aves['tvl'], 'val': aves['vl'], }, epoch) writer.add_scalars('acc', { 'train': aves['ta'], 'tval': aves['tva'], 'val': aves['va'], }, epoch) if config.get('_parallel'): model_ = model.module else: model_ = model training = { 'epoch': epoch, 'optimizer': config['optimizer'], 'optimizer_args': config['optimizer_args'], 'optimizer_sd': optimizer.state_dict(), } save_obj = { 'file': __file__, 'config': config, 'model': config['model'], 'model_args': config['model_args'], 'model_sd': model_.state_dict(), 'training': training, } torch.save(save_obj, os.path.join(save_path, 'epoch-last.pth')) torch.save(trlog, os.path.join(save_path, 'trlog.pth')) if (save_epoch is not None) and epoch % save_epoch == 0: torch.save(save_obj, os.path.join(save_path, 'epoch-{}.pth'.format(epoch))) if aves['va'] > max_va: max_va = aves['va'] torch.save(save_obj, os.path.join(save_path, 'max-va.pth')) writer.flush()
def main(config, args): random.seed(0) np.random.seed(0) torch.manual_seed(0) torch.cuda.manual_seed(0) # torch.backends.cudnn.deterministic = True # torch.backends.cudnn.benchmark = False wandb_auth() try: __IPYTHON__ wandb.init(project="NAS", group=f"maml") except: wandb.init(project="NAS", group=f"maml", config=config) ckpt_name = args.name if ckpt_name is None: ckpt_name = config['encoder'] ckpt_name += '_' + config['dataset'].replace('meta-', '') ckpt_name += '_{}_way_{}_shot'.format(config['train']['n_way'], config['train']['n_shot']) if args.tag is not None: ckpt_name += '_' + args.tag ckpt_path = os.path.join('./save', ckpt_name) utils.ensure_path(ckpt_path) utils.set_log_path(ckpt_path) writer = SummaryWriter(os.path.join(ckpt_path, 'tensorboard')) yaml.dump(config, open(os.path.join(ckpt_path, 'config.yaml'), 'w')) ##### Dataset ##### # meta-train train_set = datasets.make(config['dataset'], **config['train']) utils.log('meta-train set: {} (x{}), {}'.format(train_set[0][0].shape, len(train_set), train_set.n_classes)) # meta-val eval_val = False if config.get('val'): eval_val = True val_set = datasets.make(config['dataset'], **config['val']) utils.log('meta-val set: {} (x{}), {}'.format(val_set[0][0].shape, len(val_set), val_set.n_classes)) val_loader = DataLoader(val_set, config['val']['n_episode'], collate_fn=datasets.collate_fn, num_workers=1, pin_memory=True) # if args.split == "traintrain" and config.get('val'): # TODO I dont think this is what they meant by train-train :D # train_set = torch.utils.data.ConcatDataset([train_set, val_set]) train_loader = DataLoader(train_set, config['train']['n_episode'], collate_fn=datasets.collate_fn, num_workers=1, pin_memory=True) ##### Model and Optimizer ##### inner_args = utils.config_inner_args(config.get('inner_args')) if config.get('load') or (args.load is True and os.path.exists(ckpt_path + '/epoch-last.pth')): if config.get('load') is None: config['load'] = ckpt_path + '/epoch-last.pth' ckpt = torch.load(config['load']) config['encoder'] = ckpt['encoder'] config['encoder_args'] = ckpt['encoder_args'] config['classifier'] = ckpt['classifier'] config['classifier_args'] = ckpt['classifier_args'] model = models.load(ckpt, load_clf=(not inner_args['reset_classifier'])) optimizer, lr_scheduler = optimizers.load(ckpt, model.parameters()) start_epoch = ckpt['training']['epoch'] + 1 max_va = ckpt['training']['max_va'] else: config['encoder_args'] = config.get('encoder_args') or dict() config['classifier_args'] = config.get('classifier_args') or dict() config['encoder_args']['bn_args']['n_episode'] = config['train'][ 'n_episode'] config['classifier_args']['n_way'] = config['train']['n_way'] model = models.make(config['encoder'], config['encoder_args'], config['classifier'], config['classifier_args']) optimizer, lr_scheduler = optimizers.make(config['optimizer'], model.parameters(), **config['optimizer_args']) start_epoch = 1 max_va = 0. if args.efficient: model.go_efficient() if config.get('_parallel'): model = nn.DataParallel(model) utils.log('num params: {}'.format(utils.compute_n_params(model))) timer_elapsed, timer_epoch = utils.Timer(), utils.Timer() ##### Training and evaluation ##### # 'tl': meta-train loss # 'ta': meta-train accuracy # 'vl': meta-val loss # 'va': meta-val accuracy aves_keys = ['tl', 'ta', 'vl', 'va'] trlog = dict() for k in aves_keys: trlog[k] = [] for epoch in tqdm(range(start_epoch, config['epoch'] + 1), desc="Iterating over epochs"): timer_epoch.start() aves = {k: utils.AverageMeter() for k in aves_keys} # meta-train model.train() writer.add_scalar('lr', optimizer.param_groups[0]['lr'], epoch) np.random.seed(epoch) all_sotls = 0 all_sovls = 0 for data_idx, data in enumerate( tqdm(train_loader, desc='meta-train', leave=False)): x_shot, x_query, y_shot, y_query = data x_shot, y_shot = x_shot.cuda(), y_shot.cuda() x_query, y_query = x_query.cuda(), y_query.cuda() if inner_args['reset_classifier']: if config.get('_parallel'): model.module.reset_classifier() else: model.reset_classifier() if args.split == "traintrain": x_query = x_shot y_query = y_shot logits, sotl, all_losses = model(x_shot, x_query, y_shot, inner_args, meta_train=True) # print("HAHHA", data_idx, all_losses) # sotl = sum([l[-1] for l in all_losses]) # for l in all_losses[:-1]: # for i in range(len(l)-1): # l[i] = l[i].detach() logits = logits.flatten(0, 1) labels = y_query.flatten() all_sotls += sotl pred = torch.argmax(logits, dim=-1) acc = utils.compute_acc(pred, labels) loss = F.cross_entropy(logits, labels) # all_sovls += loss # TODO I think this causes blowup because it creates new tensors that never get discarded and it maintains the computational graph after? if args.split == "trainval" or ( args.split == "sovl" and not data_idx % args.sotl_freq == 0): aves['tl'].update(loss.item(), 1) aves['ta'].update(acc, 1) optimizer.zero_grad() loss.backward() for param in optimizer.param_groups[0]['params']: nn.utils.clip_grad_value_(param, 10) optimizer.step() elif args.split == "traintrain": aves['tl'].update(loss.item(), 1) aves['ta'].update(acc, 1) # sotl = sum(sotl) + loss optimizer.zero_grad() # sotl.backward() loss.backward() for param in optimizer.param_groups[0]['params']: nn.utils.clip_grad_value_(param, 10) optimizer.step() elif args.split == "sotl" and data_idx % args.sotl_freq == 0: # TODO doesnt work whatsoever aves['tl'].update(loss.item(), 1) aves['ta'].update(acc, 1) optimizer.zero_grad() all_sotls.backward() for param in optimizer.param_groups[0]['params']: nn.utils.clip_grad_value_(param, 10) optimizer.step() all_sotls = 0 # detach elif args.split == "sovl" and data_idx % args.sotl_freq == 0: # TODO doesnt work whatsoever aves['tl'].update(loss.item(), 1) aves['ta'].update(acc, 1) optimizer.zero_grad() all_sovls.backward() for param in optimizer.param_groups[0]['params']: nn.utils.clip_grad_value_(param, 10) optimizer.step() all_sovls = 0 # detach # meta-val if eval_val: model.eval() np.random.seed(0) for data in tqdm(val_loader, desc='meta-val', leave=False): x_shot, x_query, y_shot, y_query = data x_shot, y_shot = x_shot.cuda(), y_shot.cuda() x_query, y_query = x_query.cuda(), y_query.cuda() if inner_args['reset_classifier']: if config.get('_parallel'): model.module.reset_classifier() else: model.reset_classifier() logits, sotl, all_losses = model(x_shot, x_query, y_shot, inner_args, meta_train=False) logits = logits.flatten(0, 1) labels = y_query.flatten() pred = torch.argmax(logits, dim=-1) acc = utils.compute_acc(pred, labels) loss = F.cross_entropy(logits, labels) aves['vl'].update(loss.item(), 1) aves['va'].update(acc, 1) if lr_scheduler is not None: lr_scheduler.step() for k, avg in aves.items(): aves[k] = avg.item() trlog[k].append(aves[k]) t_epoch = utils.time_str(timer_epoch.end()) t_elapsed = utils.time_str(timer_elapsed.end()) t_estimate = utils.time_str(timer_elapsed.end() / (epoch - start_epoch + 1) * (config['epoch'] - start_epoch + 1)) # formats output log_str = 'epoch {}, meta-train {:.4f}|{:.4f}'.format( str(epoch), aves['tl'], aves['ta']) writer.add_scalars('loss', {'meta-train': aves['tl']}, epoch) writer.add_scalars('acc', {'meta-train': aves['ta']}, epoch) if eval_val: log_str += ', meta-val {:.4f}|{:.4f}'.format( aves['vl'], aves['va']) writer.add_scalars('loss', {'meta-val': aves['vl']}, epoch) writer.add_scalars('acc', {'meta-val': aves['va']}, epoch) wandb.log({ "train_loss": aves['tl'], "train_acc": aves['ta'], "val_loss": aves['vl'], "val_acc": aves['va'] }) log_str += ', {} {}/{}'.format(t_epoch, t_elapsed, t_estimate) utils.log(log_str) # saves model and meta-data if config.get('_parallel'): model_ = model.module else: model_ = model training = { 'epoch': epoch, 'max_va': max(max_va, aves['va']), 'optimizer': config['optimizer'], 'optimizer_args': config['optimizer_args'], 'optimizer_state_dict': optimizer.state_dict(), 'lr_scheduler_state_dict': lr_scheduler.state_dict() if lr_scheduler is not None else None, } ckpt = { 'file': __file__, 'config': config, 'encoder': config['encoder'], 'encoder_args': config['encoder_args'], 'encoder_state_dict': model_.encoder.state_dict(), 'classifier': config['classifier'], 'classifier_args': config['classifier_args'], 'classifier_state_dict': model_.classifier.state_dict(), 'training': training, } # 'epoch-last.pth': saved at the latest epoch # 'max-va.pth': saved when validation accuracy is at its maximum torch.save(ckpt, os.path.join(ckpt_path, 'epoch-last.pth')) torch.save(trlog, os.path.join(ckpt_path, 'trlog.pth')) if aves['va'] > max_va: max_va = aves['va'] torch.save(ckpt, os.path.join(ckpt_path, 'max-va.pth')) writer.flush()
def main(config): svname = args.name if svname is None: svname = 'meta_{}-{}shot'.format( config['train_dataset'], config['n_shot']) svname += '_' + config['model'] + '-' + config['model_args']['encoder'] if args.tag is not None: svname += '_' + args.tag save_path = os.path.join('./save', svname) utils.ensure_path(save_path) utils.set_log_path(save_path) writer = SummaryWriter(os.path.join(save_path, 'tensorboard')) yaml.dump(config, open(os.path.join(save_path, 'config.yaml'), 'w')) #### Dataset #### n_way, n_shot = config['n_way'], config['n_shot'] n_query = config['n_query'] if config.get('n_train_way') is not None: n_train_way = config['n_train_way'] else: n_train_way = n_way if config.get('n_train_shot') is not None: n_train_shot = config['n_train_shot'] else: n_train_shot = n_shot if config.get('ep_per_batch') is not None: ep_per_batch = config['ep_per_batch'] else: ep_per_batch = 1 # train train_dataset = datasets.make(config['train_dataset'], **config['train_dataset_args']) utils.log('train dataset: {} (x{}), {}'.format( train_dataset[0][0].shape, len(train_dataset), train_dataset.n_classes)) if config.get('visualize_datasets'): utils.visualize_dataset(train_dataset, 'train_dataset', writer) train_sampler = CategoriesSampler( train_dataset.label, config['train_batches'], n_train_way, n_train_shot + n_query, ep_per_batch=ep_per_batch) train_loader = DataLoader(train_dataset, batch_sampler=train_sampler, num_workers=8, pin_memory=True) # tval if config.get('tval_dataset'): tval_dataset = datasets.make(config['tval_dataset'], **config['tval_dataset_args']) utils.log('tval dataset: {} (x{}), {}'.format( tval_dataset[0][0].shape, len(tval_dataset), tval_dataset.n_classes)) if config.get('visualize_datasets'): utils.visualize_dataset(tval_dataset, 'tval_dataset', writer) tval_sampler = CategoriesSampler( tval_dataset.label, 200, n_way, n_shot + n_query, ep_per_batch=4) tval_loader = DataLoader(tval_dataset, batch_sampler=tval_sampler, num_workers=8, pin_memory=True) else: tval_loader = None # val val_dataset = datasets.make(config['val_dataset'], **config['val_dataset_args']) utils.log('val dataset: {} (x{}), {}'.format( val_dataset[0][0].shape, len(val_dataset), val_dataset.n_classes)) if config.get('visualize_datasets'): utils.visualize_dataset(val_dataset, 'val_dataset', writer) val_sampler = CategoriesSampler( val_dataset.label, 200, n_way, n_shot + n_query, ep_per_batch=4) val_loader = DataLoader(val_dataset, batch_sampler=val_sampler, num_workers=8, pin_memory=True) ######## #### Model and optimizer #### if config.get('load'): model_sv = torch.load(config['load']) model = models.load(model_sv) else: model = models.make(config['model'], **config['model_args']) if config.get('load_encoder'): encoder = models.load(torch.load(config['load_encoder'])).encoder model.encoder.load_state_dict(encoder.state_dict()) if config.get('_parallel'): model = nn.DataParallel(model) utils.log('num params: {}'.format(utils.compute_n_params(model))) optimizer, lr_scheduler = utils.make_optimizer( model.parameters(), config['optimizer'], **config['optimizer_args']) ######## max_epoch = config['max_epoch'] save_epoch = config.get('save_epoch') max_va = 0. timer_used = utils.Timer() timer_epoch = utils.Timer() aves_keys = ['tl', 'ta', 'tvl', 'tva', 'vl', 'va'] trlog = dict() for k in aves_keys: trlog[k] = [] for epoch in range(1, max_epoch + 1): timer_epoch.s() aves = {k: utils.Averager() for k in aves_keys} # train model.train() if config.get('freeze_bn'): utils.freeze_bn(model) writer.add_scalar('lr', optimizer.param_groups[0]['lr'], epoch) np.random.seed(epoch) for data, _ in tqdm(train_loader, desc='train', leave=False): x_shot, x_query = fs.split_shot_query( data.cuda(), n_train_way, n_train_shot, n_query, ep_per_batch=ep_per_batch) label = fs.make_nk_label(n_train_way, n_query, ep_per_batch=ep_per_batch).cuda() logits = model(x_shot, x_query).view(-1, n_train_way) loss = F.cross_entropy(logits, label) acc = utils.compute_acc(logits, label) optimizer.zero_grad() loss.backward() optimizer.step() aves['tl'].add(loss.item()) aves['ta'].add(acc) logits = None; loss = None # eval model.eval() for name, loader, name_l, name_a in [ ('tval', tval_loader, 'tvl', 'tva'), ('val', val_loader, 'vl', 'va')]: if (config.get('tval_dataset') is None) and name == 'tval': continue np.random.seed(0) for data, _ in tqdm(loader, desc=name, leave=False): x_shot, x_query = fs.split_shot_query( data.cuda(), n_way, n_shot, n_query, ep_per_batch=4) label = fs.make_nk_label(n_way, n_query, ep_per_batch=4).cuda() with torch.no_grad(): logits = model(x_shot, x_query).view(-1, n_way) loss = F.cross_entropy(logits, label) acc = utils.compute_acc(logits, label) aves[name_l].add(loss.item()) aves[name_a].add(acc) _sig = int(_[-1]) # post if lr_scheduler is not None: lr_scheduler.step() for k, v in aves.items(): aves[k] = v.item() trlog[k].append(aves[k]) t_epoch = utils.time_str(timer_epoch.t()) t_used = utils.time_str(timer_used.t()) t_estimate = utils.time_str(timer_used.t() / epoch * max_epoch) utils.log('epoch {}, train {:.4f}|{:.4f}, tval {:.4f}|{:.4f}, ' 'val {:.4f}|{:.4f}, {} {}/{} (@{})'.format( epoch, aves['tl'], aves['ta'], aves['tvl'], aves['tva'], aves['vl'], aves['va'], t_epoch, t_used, t_estimate, _sig)) writer.add_scalars('loss', { 'train': aves['tl'], 'tval': aves['tvl'], 'val': aves['vl'], }, epoch) writer.add_scalars('acc', { 'train': aves['ta'], 'tval': aves['tva'], 'val': aves['va'], }, epoch) if config.get('_parallel'): model_ = model.module else: model_ = model training = { 'epoch': epoch, 'optimizer': config['optimizer'], 'optimizer_args': config['optimizer_args'], 'optimizer_sd': optimizer.state_dict(), } save_obj = { 'file': __file__, 'config': config, 'model': config['model'], 'model_args': config['model_args'], 'model_sd': model_.state_dict(), 'training': training, } torch.save(save_obj, os.path.join(save_path, 'epoch-last.pth')) torch.save(trlog, os.path.join(save_path, 'trlog.pth')) if (save_epoch is not None) and epoch % save_epoch == 0: torch.save(save_obj, os.path.join(save_path, 'epoch-{}.pth'.format(epoch))) if aves['va'] > max_va: max_va = aves['va'] torch.save(save_obj, os.path.join(save_path, 'max-va.pth')) writer.flush()
def main(config): svname = config.get('sv_name') if args.tag is not None: svname += '_' + args.tag config['sv_name'] = svname save_path = os.path.join('./save', svname) utils.ensure_path(save_path) utils.set_log_path(save_path) utils.log(svname) writer = SummaryWriter(os.path.join(save_path, 'tensorboard')) yaml.dump(config, open(os.path.join(save_path, 'config.yaml'), 'w')) #### Dataset #### n_way, n_shot = config['n_way'], config['n_shot'] n_query = config['n_query'] n_pseudo = config['n_pseudo'] ep_per_batch = config['ep_per_batch'] if config.get('test_batches') is not None: test_batches = config['test_batches'] else: test_batches = config['train_batches'] for s in ['train', 'val', 'tval']: if config.get(f"{s}_dataset_args") is not None: config[f"{s}_dataset_args"]['data_dir'] = os.path.join(os.getcwd(), os.pardir, 'data_root') # train train_dataset = CustomDataset(config['train_dataset'], save_dir=config.get('load_encoder'), **config['train_dataset_args']) if config['train_dataset_args']['split'] == 'helper': with open(os.path.join(save_path, 'train_helper_cls.pkl'), 'wb') as f: pkl.dump(train_dataset.dataset_classes, f) train_sampler = EpisodicSampler(train_dataset, config['train_batches'], n_way, n_shot, n_query, n_pseudo, episodes_per_batch=ep_per_batch) train_loader = DataLoader(train_dataset, batch_sampler=train_sampler, num_workers=4, pin_memory=True) # tval if config.get('tval_dataset'): tval_dataset = CustomDataset(config['tval_dataset'], **config['tval_dataset_args']) tval_sampler = EpisodicSampler(tval_dataset, test_batches, n_way, n_shot, n_query, n_pseudo, episodes_per_batch=ep_per_batch) tval_loader = DataLoader(tval_dataset, batch_sampler=tval_sampler, num_workers=4, pin_memory=True) else: tval_loader = None # val val_dataset = CustomDataset(config['val_dataset'], **config['val_dataset_args']) val_sampler = EpisodicSampler(val_dataset, test_batches, n_way, n_shot, n_query, n_pseudo, episodes_per_batch=ep_per_batch) val_loader = DataLoader(val_dataset, batch_sampler=val_sampler, num_workers=4, pin_memory=True) #### Model and optimizer #### if config.get('load'): model_sv = torch.load(config['load']) model = models.load(model_sv) else: model = models.make(config['model'], **config['model_args']) if config.get('load_encoder'): encoder = models.load(torch.load(config['load_encoder'])).encoder model.encoder.load_state_dict(encoder.state_dict()) if config.get('freeze_encoder'): for param in model.encoder.parameters(): param.requires_grad = False if config.get('_parallel'): model = nn.DataParallel(model) utils.log('num params: {}'.format(utils.compute_n_params(model))) optimizer, lr_scheduler = utils.make_optimizer( model.parameters(), config['optimizer'], **config['optimizer_args']) ######## max_epoch = config['max_epoch'] save_epoch = config.get('save_epoch') max_va = 0. timer_used = utils.Timer() timer_epoch = utils.Timer() aves_keys = ['tl', 'ta', 'tvl', 'tva', 'vl', 'va'] trlog = dict() for k in aves_keys: trlog[k] = [] for epoch in range(1, max_epoch + 1): timer_epoch.s() aves = {k: utils.Averager() for k in aves_keys} # train model.train() if config.get('freeze_bn'): utils.freeze_bn(model) writer.add_scalar('lr', optimizer.param_groups[0]['lr'], epoch) np.random.seed(epoch) for data in tqdm(train_loader, desc='train', leave=False): x_shot, x_query, x_pseudo = fs.split_shot_query( data.cuda(), n_way, n_shot, n_query, n_pseudo, ep_per_batch=ep_per_batch) label = fs.make_nk_label(n_way, n_query, ep_per_batch=ep_per_batch).cuda() logits = model(x_shot, x_query, x_pseudo) logits = logits.view(-1, n_way) loss = F.cross_entropy(logits, label) acc = utils.compute_acc(logits, label) optimizer.zero_grad() loss.backward() optimizer.step() aves['tl'].add(loss.item()) aves['ta'].add(acc) logits = None; loss = None # eval model.eval() for name, loader, name_l, name_a in [ ('tval', tval_loader, 'tvl', 'tva'), ('val', val_loader, 'vl', 'va')]: if (config.get('tval_dataset') is None) and name == 'tval': continue np.random.seed(0) for data in tqdm(loader, desc=name, leave=False): x_shot, x_query, x_pseudo = fs.split_shot_query( data.cuda(), n_way, n_shot, n_query, n_pseudo, ep_per_batch=ep_per_batch) label = fs.make_nk_label(n_way, n_query, ep_per_batch=ep_per_batch).cuda() with torch.no_grad(): logits = model(x_shot, x_query, x_pseudo) logits = logits.view(-1, n_way) loss = F.cross_entropy(logits, label) acc = utils.compute_acc(logits, label) aves[name_l].add(loss.item()) aves[name_a].add(acc) # post if lr_scheduler is not None: lr_scheduler.step() for k, v in aves.items(): aves[k] = v.item() trlog[k].append(aves[k]) t_epoch = utils.time_str(timer_epoch.t()) t_used = utils.time_str(timer_used.t()) t_estimate = utils.time_str(timer_used.t() / epoch * max_epoch) utils.log('epoch {}, train {:.4f}|{:.4f}, tval {:.4f}|{:.4f}, ' 'val {:.4f}|{:.4f}, {} {}/{}'.format( epoch, aves['tl'], aves['ta'], aves['tvl'], aves['tva'], aves['vl'], aves['va'], t_epoch, t_used, t_estimate)) writer.add_scalars('loss', { 'train': aves['tl'], 'tval': aves['tvl'], 'val': aves['vl'], }, epoch) writer.add_scalars('acc', { 'train': aves['ta'], 'tval': aves['tva'], 'val': aves['va'], }, epoch) if config.get('_parallel'): model_ = model.module else: model_ = model training = { 'epoch': epoch, 'optimizer': config['optimizer'], 'optimizer_args': config['optimizer_args'], 'optimizer_sd': optimizer.state_dict(), } save_obj = { 'file': __file__, 'config': config, 'model': config['model'], 'model_args': config['model_args'], 'model_sd': model_.state_dict(), 'training': training, } torch.save(save_obj, os.path.join(save_path, 'epoch-last.pth')) torch.save(trlog, os.path.join(save_path, 'trlog.pth')) if (save_epoch is not None) and epoch % save_epoch == 0: torch.save(save_obj, os.path.join(save_path, 'epoch-{}.pth'.format(epoch))) if aves['va'] > max_va: max_va = aves['va'] torch.save(save_obj, os.path.join(save_path, 'max-va.pth')) writer.flush()
import modelPolicyGradient import utils import Pong_config as config ## Settings and parameters experiment_name = None # experiment_name = '2017-07-13-(05-16-25)' # Complete shit # experiment_name = '2017-07-13-(05-59-03)' # Complete shit # experiment_name = '2017-07-12-(17-09-11)' # Almost shit ## Training settings learning_rate = 1e-3 max_train_frame = 5e7 ## Derived settings run_name = experiment_name or utils.time_str() logdir = './logdir/' + config.env_name + '/PG/' + run_name print('logdir\t\t', logdir) num_run = 1 # env = gym.make(config.env_name) prepro = utils.Preprocessor_2d(config.num_state, gray=True) env = utils.EnvironmentInterface(config, prepro, action_repeats=4, obs_buffer_size=4) for i in range(num_run): run_name = experiment_name or utils.time_str() logdir = './logdir/' + config.env_name + '/PG/' + run_name