Ejemplo n.º 1
0
def main(_):
    global PATCH_SIZE
    args = parser.parse_args()

    print(args)
    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print('Found devices:', [x.name for x in local_device_protos])
    # set gpu
    if args.gpu_id is not None:
        print('using gpu %d' % args.gpu_id)
        os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    else:
        print('no gpu specified')
    # load config and run training

    cfg = utils.load_config(args.cfg_dir, nopause=args.nopause)
    adjust_cfg_for_extract(args, cfg)

    print(os.path.join(cfg['log_dir'], "{}_label.png").format(args.stat_type))
    if 'metric_kwargs' in cfg:
        cfg['target_dim_tuple'] = (cfg['metric_kwargs']['output_size'], )
    if type(cfg['target_dim']) == int:
        cfg['target_dim_tuple'] = (cfg['target_dim'], )
    else:
        cfg['target_dim_tuple'] = cfg['target_dim']

    run_training(cfg, args.cfg_dir, args)
def main( _ ):
    args = parser.parse_args()
    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print( 'Found devices:', [ x.name for x in local_device_protos ] )  
    # set GPU id
   
    task_dir = os.path.join('/home/ubuntu/task-taxonomy-331b/experiments', 'class_gt')
    cfg = utils.load_config( task_dir, nopause=args.nopause )
    
    dataset_dir = '/home/ubuntu/s3/meta'
    train = np.load(os.path.join(dataset_dir, 'train_image_split_0.npy'))
    val = np.load(os.path.join(dataset_dir, 'val_image_split_0.npy'))
    total = np.concatenate((train, val))
    import math
    num_split = 200.
    unit_size = math.ceil(len(total) / num_split)
    total = total[args.idx * unit_size: (args.idx + 1) * unit_size]
    # split_file = '/home/ubuntu/this_split.npy'
    # with open(split_file, 'wb') as fp:
        # np.save(fp, total)
    # cfg['preprocess_fn'] = load_and_specify_preprocessors_for_representation_extraction

    # cfg['train_filenames'] = split_file
    # cfg['val_filenames'] = split_file
    # cfg['test_filenames'] = split_file 
    

    cfg['randomize'] = False
    cfg['num_epochs'] = 2
    cfg['num_read_threads'] = 1
    run_extract_representations( args, cfg, total)
Ejemplo n.º 3
0
    def __init__(self,
                 model_file_path,
                 char_dict_path,
                 word_dict_path=None,
                 emb_dim=128,
                 hidden_dim=128):

        self.model_file_path = model_file_path
        self.char_dict_path = char_dict_path
        self.word_dict_path = word_dict_path

        with io.open(self.char_dict_path, 'rb') as f:
            self.char2index = pickle.load(f)

        self.device, gpu_ids = utils.get_available_devices()

        bilstm_crf = BiLSTMCRF(self.device, utils.tag_to_ix,
                               len(self.char2index) + 2, emb_dim, hidden_dim)
        self.segmentor_model = utils.load_model(bilstm_crf,
                                                self.model_file_path,
                                                gpu_ids,
                                                return_step=False)
        self.segmentor_model = self.segmentor_model.to(self.device)
        self.segmentor_model.eval()

        self.userdict = Trie()
        self.userdict.add_dict(self.word_dict_path)
Ejemplo n.º 4
0
def main(_):
    args = parser.parse_args()

    # TODO(apply proposed change)
    # shim to reboot if the transport end point is disconnected
    # We will remove this in the next experiment and add it as a background process launched from user data
    #     subprocess.call(
    # "watch -n 300 'bash /home/ubuntu/task-taxonomy-331b/tools/script/reboot_if_disconnected.sh' &>/dev/null &",
    # shell=True
    # )

    print(args)
    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print('Found devices:', [x.name for x in local_device_protos])
    # set gpu
    if args.gpu_id is not None:
        print('using gpu %d' % args.gpu_id)
        os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    else:
        print('no gpu specified')
    # load config and run training
    cfg = utils.load_config(args.cfg_dir, nopause=args.nopause)
    # cfg['num_read_threads'] = 1
    run_training(cfg, args.cfg_dir)
Ejemplo n.º 5
0
    def __init__(self, device_serial=None, is_emulator=True, output_dir=None,
                 grant_perm=False, telnet_auth_token=None):
        """
        initialize a device connection
        :param device_serial: serial number of target device
        :param is_emulator: boolean, type of device, True for emulator, False for real device
        :return:
        """
        self.logger = logging.getLogger(self.__class__.__name__)

        if device_serial is None:
            import utils
            all_devices = utils.get_available_devices()
            if len(all_devices) == 0:
                self.logger.warning("ERROR: No device connected.")
                sys.exit(-1)
            device_serial = all_devices[0]
        self.serial = device_serial
        self.is_emulator = is_emulator
        self.output_dir = output_dir
        if output_dir is not None:
            if not os.path.isdir(output_dir):
                os.mkdir(output_dir)
        self.grant_perm = grant_perm

        # basic device information
        self.settings = {}
        self.display_info = None
        self.model_number = None
        self.sdk_version = None
        self.release_version = None
        self.ro_debuggable = None
        self.ro_secure = None
        self.connected = True
        self.last_know_state = None
        self.__used_ports = []
        self.pause_sending_event = False

        # adapters
        self.adb = ADB(device=self)
        self.telnet = TelnetConsole(device=self, auth_token=telnet_auth_token)
        self.droidbot_app = DroidBotAppConn(device=self)
        self.minicap = Minicap(device=self)
        self.logcat = Logcat(device=self)
        self.user_input_monitor = UserInputMonitor(device=self)
        self.process_monitor = ProcessMonitor(device=self)
        self.droidbot_ime = DroidBotIme(device=self)

        self.adapters = {
            self.adb: True,
            self.telnet: False,
            self.droidbot_app: True,
            self.minicap: True,
            self.logcat: True,
            self.user_input_monitor: True,
            self.process_monitor: True,
            self.droidbot_ime: True
        }
Ejemplo n.º 6
0
def main(_):
    args = parser.parse_args()

    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print('Found devices:', [x.name for x in local_device_protos])
    # set GPU id
    if args.gpu_id:
        print('using gpu %d' % args.gpu_id)
        os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    else:
        print('no gpu specified')

    cfg = load_config(args.cfg_dir)
    run_training(cfg)
Ejemplo n.º 7
0
def main(_):
    args = parser.parse_args()

    print(args)
    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print('Found devices:', [x.name for x in local_device_protos])
    # set gpu
    if args.gpu_id is not None:
        print('using gpu %d' % args.gpu_id)
        os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    else:
        print('no gpu specified')
    # load config and run training
    cfg = utils.load_config(args.cfg_dir, nopause=args.nopause)
    run_training(cfg, args.cfg_dir)
Ejemplo n.º 8
0
def main(_):
    print('Available devices:', utils.get_available_devices())

    for _ in range(FLAGS.runs):
        # Initialize parameters.
        params = config.GANParams(config.params_defs.initialized())

        # Override parameters by flag values.
        # for k, v in FLAGS.__dict__['__flags'].items():
        #     if v is not None:
        #         params[k] = v

        # Load dataset.
        images, labels = params.dataset.get(params)

        # Run training.
        run(images, labels, params)
Ejemplo n.º 9
0
def main(_):
    args = parser.parse_args()

    print(args)
    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print('Found devices:', [x.name for x in local_device_protos])
    # set gpu
    if args.gpu_id is not None:
        print('using gpu %d' % args.gpu_id)
        os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    else:
        print('no gpu specified')
    # load config and run training
    cfg = utils.load_config(args.cfg_dir, nopause=args.nopause)
    cfg['task_name'] = args.cfg_dir.split('/')[-1]
    cfg['task_name'] = 'class_selected'
    cfg['num_epochs'] = 1
    run_val_test(cfg)
Ejemplo n.º 10
0
def main( _ ):
    args = parser.parse_args()
    #task_list = ["autoencoder", "colorization","curvature", "denoise", "edge2d", "edge3d", "ego_motion", "fix_pose", "impainting", "jigsaw", "keypoint2d", "keypoint3d", "non_fixated_pose", "point_match", "reshade", "rgb2depth", "rgb2mist", "rgb2sfnorm", "room_layout", "segment25d", "segment2d", "vanishing_point"]
    #single channel for colorization !!!!!!!!!!!!!!!!!!!!!!!!! COME BACK TO THIS !!!!!!!!!!!!!!!!!!!!!!!!!!!
    #task_list = [ "point_match"]
    task_list = [ "vanishing_point"]

    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print( 'Found devices:', [ x.name for x in local_device_protos ] )  
    # set GPU id
    if args.gpu_id:
        print( 'using gpu %d' % args.gpu_id )
        os.environ[ 'CUDA_VISIBLE_DEVICES' ] = str( args.gpu_id )
    else:
        print( 'no gpu specified' )
    
    for task in task_list:
        task_dir = os.path.join(args.cfg_dir, task)
        cfg = utils.load_config( task_dir, nopause=args.nopause )
        root_dir = cfg['root_dir']
        cfg['randomize'] = False
        cfg['num_epochs'] = 1
        run_rand_baseline( args, cfg, task )
Ejemplo n.º 11
0
def main( _ ):
    args = parser.parse_args()

    print(args)
    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print( 'Found devices:', [ x.name for x in local_device_protos ] )  
    # set gpu
    if args.gpu_id is not None:
        print( 'using gpu %d' % args.gpu_id )
        os.environ[ 'CUDA_VISIBLE_DEVICES' ] = str( args.gpu_id )
    else:
        print( 'no gpu specified' )
    # load config and run training
    cfg = utils.load_config( args.cfg_dir, nopause=args.nopause )
    cfg['train_filenames'] = cfg['train_filenames'].replace('task-taxonomy-331b/assets/aws_data', 's3/meta') 
    cfg['num_epochs'] = 6
    cfg['learning_rate_schedule_kwargs' ] = {
            'boundaries': [np.int64(0), np.int64(1800000)], # need to be int64 since global step is...
            'values': [cfg['initial_learning_rate'], cfg['initial_learning_rate']/10]
    }     
    cfg['randomize'] = True

    run_training( cfg, args.cfg_dir )
Ejemplo n.º 12
0
    def __init__(self,
                 device_serial=None,
                 device_ip=None,
                 is_emulator=False,
                 output_dir=None,
                 cv_mode=False,
                 grant_perm=False,
                 telnet_auth_token=None,
                 enable_accessibility_hard=False,
                 humanoid=None,
                 ignore_ad=False):
        """
        initialize a device connection
        :param device_serial: serial number of target device
        :param is_emulator: boolean, type of device, True for emulator, False for real device
        :return:
        """
        self.logger = logging.getLogger(self.__class__.__name__)

        if device_serial is None:
            from utils import get_available_devices
            all_devices = get_available_devices()
            if len(all_devices) == 0:
                self.logger.warning("ERROR: No device connected.")
                sys.exit(-1)
            device_serial = all_devices[0]
        if "emulator" in device_serial and not is_emulator:
            self.logger.warning(
                "Seems like you are using an emulator. If so, please add is_emulator option."
            )
        self.serial = device_serial
        self.ip = device_ip
        self.is_emulator = is_emulator
        self.cv_mode = cv_mode
        self.output_dir = output_dir
        if output_dir is not None:
            if not os.path.isdir(output_dir):
                os.makedirs(output_dir)
        self.grant_perm = grant_perm
        self.enable_accessibility_hard = enable_accessibility_hard
        self.humanoid = humanoid
        self.ignore_ad = ignore_ad

        # basic device information
        self.settings = {}
        self.display_info = None
        self.model_number = None
        self.sdk_version = None
        self.release_version = None
        self.ro_debuggable = None
        self.ro_secure = None
        self.connected = True
        self.last_know_state = None
        self.__used_ports = []
        self.pause_sending_event = False

        # adapters
        self.adb = ADB(device=self)
        self.telnet = TelnetConsole(device=self, auth_token=telnet_auth_token)
        self.droidbot_app = DroidBotAppConn(device=self)
        self.minicap = Minicap(device=self)
        self.logcat = Logcat(device=self)
        self.user_input_monitor = UserInputMonitor(device=self)
        self.process_monitor = ProcessMonitor(device=self)
        self.droidbot_ime = DroidBotIme(device=self)

        self.adapters = {
            self.adb: True,
            self.telnet: False,
            self.droidbot_app: True,
            self.minicap: True,
            self.logcat: True,
            self.user_input_monitor: True,
            self.process_monitor: True,
            self.droidbot_ime: True
        }

        # minicap currently not working on emulators
        if self.is_emulator:
            self.logger.info("disable minicap on emulator")
            self.adapters[self.minicap] = False
Ejemplo n.º 13
0
def main(_):
    args = parser.parse_args()
    global TRAIN_MODE
    TRAIN_MODE = args.train_mode
    #task_list = ["autoencoder", "colorization","curvature", "denoise", "edge2d", "edge3d", "ego_motion", "fix_pose", "impainting", "jigsaw", "keypoint2d", "keypoint3d", "non_fixated_pose", "point_match", "reshade", "rgb2depth", "rgb2mist", "rgb2sfnorm", "room_layout", "segment25d", "segment2d", "vanishing_point"]
    #single channel for colorization !!!!!!!!!!!!!!!!!!!!!!!!! COME BACK TO THIS !!!!!!!!!!!!!!!!!!!!!!!!!!!
    #task_list = [ args.task ]
    with open('/home/ubuntu/task-taxonomy-331b/tools/config_list.txt',
              'r') as fp:
        task_list = [x.split(' ')[0] for x in fp.readlines()]
        #config_prefix = 'second_order/DO_NOT_REPLACE_TARGET_DECODER/16k'
        #task_list = ['{}/{}'.format(config_prefix, x) for x in task_list]

    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print('Found devices:', [x.name for x in local_device_protos])
    # set GPU id
    if args.gpu_id:
        print('using gpu %d' % args.gpu_id)
        os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    else:
        print('no gpu specified')

    should_flip_dict = {}
    for task in task_list:
        flip_loss = []
        for flip_num in range(2):
            task_dir = os.path.join(args.cfg_dir, task)
            cfg = utils.load_config(task_dir, nopause=args.nopause)
            root_dir = cfg['root_dir']
            if flip_num == 0:
                cfg['val_representations_file'] = cfg[
                    'val_representations_file'][::-1]

            if args.data_split == 'train':
                split_file = cfg['train_filenames']
            elif args.data_split == 'val':
                split_file = cfg['val_filenames']
            elif args.data_split == 'test':
                split_file = cfg['test_filenames']
            else:
                raise NotImplementedError(
                    "Unknown data split section: {}".format(args.data_split))
            cfg['train_filenames'] = split_file
            cfg['val_filenames'] = split_file
            cfg['test_filenames'] = split_file

            if 'train_list_of_fileinfos' in cfg:
                split_file_ = cfg['{}_representations_file'.format(
                    args.data_split)]
                cfg['train_representations_file'] = split_file_
                cfg['val_representations_file'] = split_file_
                cfg['test_representations_file'] = split_file_

                split_file_ = cfg['{}_list_of_fileinfos'.format(
                    args.data_split)]
                cfg['train_list_of_fileinfos'] = split_file_
                cfg['val_list_of_fileinfos'] = split_file_
                cfg['test_list_of_fileinfos'] = split_file_

            if args.representation_task:
                split_file_ = args.representation_task
                if 'multiple_input_tasks' in cfg:
                    split_file_ = [split_file_]
                cfg['train_representations_file'] = split_file_
                cfg['val_representations_file'] = split_file_
                cfg['test_representations_file'] = split_file_

            # Try latest checkpoint by epoch
            cfg['model_path'] = tf.train.latest_checkpoint(
                os.path.join(cfg['log_root'], 'logs', 'slim-train'))
            # Try latest checkpoint by time
            if cfg['model_path'] is None:
                cfg['model_path'] = tf.train.latest_checkpoint(
                    os.path.join(cfg['log_root'], 'logs', 'slim-train',
                                 'time'))

            # Try to get one saved manually
            if cfg['model_path'] is None:
                continue
                #cfg['model_path'] = os.path.join(cfg['log_root'], task, "model.permanent-ckpt")

            cfg['randomize'] = False
            cfg['num_epochs'] = 3
            cfg['batch_size'] = 32
            cfg['num_read_threads'] = 3
            if 'batch_size' in cfg['encoder_kwargs']:
                cfg['encoder_kwargs']['batch_size'] = cfg['batch_size']
            try:
                cfg['target_cfg']['batch_size'] = cfg['batch_size']
            except:
                pass
            try:
                cfg['target_cfg']['encoder_kwargs']['batch_size'] = cfg[
                    'batch_size']
            except:
                pass

            loss_dir = args.cfg_dir
            curr_loss = run_extract_losses_5_steps(args, cfg, loss_dir, task)
            flip_loss.append(curr_loss)
        should_flip_dict[task] = (flip_loss[0] > flip_loss[1])
        with open(
                '/home/ubuntu/task-taxonomy-331b/tools/second_order_should_flip.pkl',
                'wb') as fp:
            pickle.dump(should_flip_dict, fp)
Ejemplo n.º 14
0
def main(args):

    #denoiser = VQ_CVAE(128, k=512, num_channels=3)
    #denoiser.load_state_dict(torch.load("/mnt/home2/dlongo/eegML/VQ-VAE-master/vq_vae/saved_models/train.pt"))
    #denoiser = torch.no_grad(denoiser)
    #denoiser.cuda()
    #denoiser = nn.DataParallel(denoiser)
    # Get device
    device, args.gpu_ids = utils.get_available_devices()
    args.train_batch_size *= max(1, len(args.gpu_ids))
    args.test_batch_size *= max(1, len(args.gpu_ids))

    # Set random seed
    utils.seed_torch(seed=SEED)

    # Get save directories
    train_save_dir = utils.get_save_dir(args.save_dir, training=True)
    args.train_save_dir = train_save_dir

    # Save args
    args_file = os.path.join(train_save_dir, ARGS_FILE_NAME)
    with open(args_file, 'w') as f:
        json.dump(vars(args), f, indent=4, sort_keys=True)

    # Set up logging and devices
    log = utils.get_logger(train_save_dir, 'train_denoised')
    tbx = SummaryWriter(train_save_dir)
    log.info('Args: {}'.format(dumps(vars(args), indent=4, sort_keys=True)))

    if args.cross_val:
        # Loop over folds
        for fold_idx in range(args.num_folds):
            log.info('Starting fold {}...'.format(fold_idx))

            # Train
            fold_save_dir = os.path.join(train_save_dir,
                                         'fold_' + str(fold_idx))
            if not os.path.exists(fold_save_dir):
                os.makedirs(fold_save_dir)

            # Training on current fold...
            train_fold(args,
                       device,
                       fold_save_dir,
                       log,
                       tbx,
                       cross_val=True,
                       fold_idx=fold_idx)
            best_path = os.path.join(fold_save_dir, 'best.pth.tar')

            # Predict on current fold with best model..
            if args.model_name == 'SeizureNet':
                model = SeizureNet(args)

            model = nn.DataParallel(model, args.gpu_ids)
            model, _ = utils.load_model(model, best_path, args.gpu_ids)

            model.to(device)
            results = evaluate_fold(model,
                                    args,
                                    fold_save_dir,
                                    device,
                                    cross_val=True,
                                    fold_idx=fold_idx,
                                    is_test=True,
                                    write_outputs=True)

            # Log to console
            results_str = ', '.join('{}: {:05.2f}'.format(k, v)
                                    for k, v in results.items())
            print('Fold {} test results: {}'.format(fold_idx, results_str))
            log.info('Finished fold {}...'.format(fold_idx))
    else:
        # no cross-validation
        # Train
        train_fold(args, device, train_save_dir, log, tbx, cross_val=False)
        best_path = os.path.join(train_save_dir, 'best.pth.tar')

        if args.model_name == 'SeizureNet':
            model = SeizureNet(args)

        model = nn.DataParallel(model, args.gpu_ids)
        model, _ = utils.load_model(model, best_path, args.gpu_ids)

        model.to(device)
        results = evaluate_fold(model,
                                args,
                                train_save_dir,
                                device,
                                cross_val=False,
                                fold_idx=None,
                                is_test=True,
                                write_outputs=True)

        # Log to console
        results_str = ', '.join('{}: {:05.2f}'.format(k, v)
                                for k, v in results.items())
        print('Test set prediction results: {}'.format(results_str))
Ejemplo n.º 15
0
if __name__ == '__main__':

    global train_logger
    args.save_dir = get_save_dir(
        args.save_dir,
        args.name + '_lr' + f'{args.lr}' + '_epochs' + f'{args.num_epochs}' +
        '_hidden' + f'{args.HIDDEN_DIM}' + '_embed' + f'{args.EMBEDDING_DIM}' +
        '_batch' + f'{args.batch_size}',
        training=True)
    train_logger = get_logger(args.save_dir, args.name)
    train_logger.info('Args: {}'.format(
        dumps(vars(args), indent=4, sort_keys=True)))

    tbx = SummaryWriter(args.save_dir)
    device, args.gpu_ids = utils.get_available_devices()
    args.batch_size *= 1  # max(1, len(args.gpu_ids))

    set_random()

    train_logger.info('Loading data from file')
    train_data, dev_data, word_to_ix = load_data()

    # print(dev_data)
    train_logger.info('Transferring data into dataset')
    train_dataset = utils.Dataset(train_data)
    train_loader = data.DataLoader(train_dataset,
                                   batch_size=args.batch_size,
                                   shuffle=True,
                                   collate_fn=utils.collate_fn_with_label)
    dev_dataset = utils.Dataset(dev_data)
Ejemplo n.º 16
0
def main(args):
    # Set up logging and devices
    args.save_dir = utils.get_save_dir(args.save_dir, args.name, training=True)
    log = utils.get_logger(args.save_dir, args.name)
    tbx = SummaryWriter(args.save_dir)
    device, args.gpu_ids = utils.get_available_devices()
    log.info(f'Args: {dumps(vars(args), indent=4, sort_keys=True)}')
    args.batch_size *= max(1, len(args.gpu_ids))

    # Set random seed
    log.info(f'Using random seed {args.seed}...')
    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)

    # Get model
    log.info('Building model...')
    model = get_model(args)
    model = nn.DataParallel(model, args.gpu_ids)
    if args.load_path:
        log.info(f'Loading checkpoint from {args.load_path}...')
        model, step = utils.load_model(model, args.load_path, args.gpu_ids)
    else:
        step = 0
    model = model.to(device)
    model.train()

    # Get saver
    saver = utils.CheckpointSaver(args.save_dir,
                                  max_checkpoints=args.max_checkpoints,
                                  metric_name=args.metric_name,
                                  maximize_metric=args.maximize_metric,
                                  log=log)

    # Set optimizer and scheduler
    optimizer_grouped_params = [{
        'params':
        [p for n, p in model.named_parameters() if 'classifier' not in n]
    }, {
        'params':
        [p for n, p in model.named_parameters() if 'classifier' in n],
        'lr':
        args.lr_1
    }]
    optimizer = optim.AdamW(optimizer_grouped_params,
                            args.lr_2,
                            weight_decay=args.l2_wd)
    scheduler = sched.LambdaLR(optimizer, lambda s: 1.)  # Constant LR

    # Get data loader
    log.info('Building dataset...')
    transform = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])
    train_dataset = NEWS(args.train_record_file, transform=transform)
    train_loader = data.DataLoader(train_dataset,
                                   batch_size=args.batch_size,
                                   shuffle=True,
                                   num_workers=args.num_workers,
                                   collate_fn=collate_fn)
    dev_dataset = NEWS(args.dev_record_file, transform=transform)
    dev_loader = data.DataLoader(dev_dataset,
                                 batch_size=args.batch_size,
                                 shuffle=False,
                                 num_workers=args.num_workers,
                                 collate_fn=collate_fn)

    # Train
    log.info('Training...')
    steps_till_eval = args.eval_steps
    epoch = step // len(train_dataset)
    while epoch != args.num_epochs:
        epoch += 1
        log.info(f'Starting epoch {epoch}...')
        with torch.enable_grad(), tqdm(
                total=len(train_loader.dataset)) as progress_bar:
            # for input_idxs, atten_masks, images, ids, y in train_loader:
            for input_idxs, atten_masks, y in train_loader:
                # print(y)
                # Setup for forward
                input_idxs = input_idxs.to(device)
                atten_masks = atten_masks.to(device)
                y = y.to(device)
                batch_size = input_idxs.size(0)
                optimizer.zero_grad()

                # Forward
                log_p = model(input_idxs, atten_masks)
                loss = torch.nn.functional.binary_cross_entropy(
                    log_p,
                    y,
                    weight=None,
                    size_average=None,
                    reduce=None,
                    reduction='mean')
                loss_val = loss.item()

                # Backward
                loss.backward()
                optimizer.step()
                scheduler.step(step // batch_size)

                # Log info
                step += batch_size
                progress_bar.update(batch_size)
                progress_bar.set_postfix(epoch=epoch, NLL=loss_val)
                tbx.add_scalar('train/NLL', loss_val, step)
                tbx.add_scalar('train/LR', optimizer.param_groups[0]['lr'],
                               step)

                steps_till_eval -= batch_size
                if steps_till_eval <= 0:
                    steps_till_eval = args.eval_steps

                    # Evaluate and save checkpoint
                    log.info(f'Evaluating at step {step}...')
                    results, pred_dict = evaluate(model, dev_loader, device)
                    saver.save(step, model, results[args.metric_name], device)

                    # Log to console
                    results_str = ', '.join(f'{k}: {v:05.2f}'
                                            for k, v in results.items())
                    log.info(f'Dev {results_str}')

                    # Log to TensorBoard
                    log.info('Visualizing in TensorBoard...')
                    for k, v in results.items():
                        tbx.add_scalar(f'dev/{k}', v, step)
Ejemplo n.º 17
0
def main(_):
    args = parser.parse_args()
    global TRAIN_MODE
    TRAIN_MODE = args.train_mode
    #task_list = ["autoencoder", "colorization","curvature", "denoise", "edge2d", "edge3d", "ego_motion", "fix_pose", "impainting", "jigsaw", "keypoint2d", "keypoint3d", "non_fixated_pose", "point_match", "reshade", "rgb2depth", "rgb2mist", "rgb2sfnorm", "room_layout", "segment25d", "segment2d", "vanishing_point"]
    #single channel for colorization !!!!!!!!!!!!!!!!!!!!!!!!! COME BACK TO THIS !!!!!!!!!!!!!!!!!!!!!!!!!!!
    task_list = [args.task]

    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print('Found devices:', [x.name for x in local_device_protos])
    # set GPU id
    if args.gpu_id:
        print('using gpu %d' % args.gpu_id)
        os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    else:
        print('no gpu specified')

    for task in task_list:
        to_task = 'class_places'
        if args.pretrain_transfer:
            task_dir = os.path.join(
                args.cfg_dir, args.pretrain_transfer_type,
                'DO_NOT_REPLACE_TARGET_DECODER/16k',
                "{}__{}__8__unlocked".format(task, to_task))
        else:
            task_dir = os.path.join(args.cfg_dir, 'final', task)
        cfg = utils.load_config(task_dir, nopause=args.nopause)

        root_dir = cfg['root_dir']
        split_file = os.path.abspath(
            os.path.join(root_dir, 'assets/aws_data/train_places_info.pkl'))
        cfg['dataset_dir'] = '/home/ubuntu/place'

        cfg['train_filenames'] = split_file
        cfg['val_filenames'] = split_file
        cfg['test_filenames'] = split_file

        if 'train_list_of_fileinfos' in cfg:
            if type(cfg['train_representations_file']) is not list:
                split_file_ = os.path.join(
                    cfg['input_cfg']['log_root'], task,
                    '{task}_train_places_representations.pkl'.format(
                        task=task))
            else:
                split_file_ = []
                for fname in cfg['train_representations_file']:
                    split_file_.append(fname.replace('val', 'train_places'))
                if args.add_places_knowledge:
                    split_file_.append(
                        os.path.join(
                            cfg['input_cfg'][0]['log_root'], 'class_places',
                            'class_places_train_places_representations.pkl'))
                    cfg['representation_dim'] = [16, 16, 8 * len(split_file_)]
                if args.add_alexnet:
                    split_file_.append(
                        os.path.join(cfg['input_cfg'][0]['log_root'], 'alex',
                                     'alex_train_places_representations.pkl'))
                    cfg['representation_dim'] = [
                        16, 16, 8 * (len(split_file_) + 1)
                    ]

            cfg['train_representations_file'] = split_file_
            cfg['val_representations_file'] = split_file_
            cfg['test_representations_file'] = split_file_

            split_file_ = os.path.join(root_dir,
                                       'assets/aws_data/train_places.npy')
            cfg['train_list_of_fileinfos'] = split_file_
            cfg['val_list_of_fileinfos'] = split_file_
            cfg['test_list_of_fileinfos'] = split_file_

        # cfg['resize_interpolation_order'] = 0
        # if cfg['model_path'] is None:
        # cfg['model_path'] = os.path.join(cfg['dataset_dir'], "model_log", task, "model.permanent-ckpt")
        cfg['target_from_filenames'] = class_places
        # Try latest checkpoint by epoch
        cfg['model_path'] = tf.train.latest_checkpoint(
            os.path.join(cfg['log_root'], 'logs', 'slim-train'))

        # Try latest checkpoint by time
        if cfg['model_path'] is None:
            cfg['model_path'] = tf.train.latest_checkpoint(
                os.path.join(cfg['log_root'], 'logs', 'slim-train', 'time'))

        # Try to get one saved manually
        if cfg['model_path'] is None:
            cfg['model_path'] = os.path.join(cfg['log_root'], task,
                                             "model.permanent-ckpt")
            # cfg['model_path'] = os.path.join(cfg['log_root'], 'logs', 'slim-train', 'time', "model.ckpt-1350")

        cfg['randomize'] = False
        cfg['num_epochs'] = args.num_epochs
        cfg['batch_size'] = 32  #if TRAIN_MODE else 1
        cfg['num_read_threads'] = 30
        if 'batch_size' in cfg['encoder_kwargs']:
            cfg['encoder_kwargs']['batch_size'] = cfg['batch_size']
        try:
            cfg['target_cfg']['batch_size'] = cfg['batch_size']
        except:
            pass
        try:
            cfg['target_cfg']['encoder_kwargs']['batch_size'] = cfg[
                'batch_size']
        except:
            pass

        loss_dir = args.cfg_dir
        if args.train_encoder:
            cfg['src_encoder_ckpt'] = tf.train.latest_checkpoint(
                os.path.join(cfg['input_cfg']['log_root'], task, 'logs',
                             'slim-train', 'time'))
            if cfg['src_encoder_ckpt'] is None:
                cfg['src_encoder_ckpt'] = os.path.join(
                    cfg['input_cfg']['log_root'], task, "model.permanent-ckpt")
            cfg['finetune_encoder_imagenet'] = True

        if args.metric_only:
            cfg['metric_net_only'] = True
            #             cfg['target_cfg']['metric_kwargs'] = {
            # 'hidden_size': args.hidden,
            # 'layer_num': args.layers,
            # 'output_size': 63,
            # 'initial_dropout': True,
            # 'dropout':args.dropout
            # }
            cfg['target_cfg']['metric_kwargs'] = {
                'hidden_size': args.hidden,
                'layer_num': args.layers,
                'output_size': 63,
                'dropout': args.dropout
            }
            print(cfg['target_cfg']['metric_kwargs'])
        cfg['data_used'] = args.data_used
        #         cfg['weight_decay'] = 1e-3
        # cfg['encoder_kwargs']['weight_decay'] = cfg['weight_decay']
        # cfg['target_cfg']['weight_decay'] = cfg['weight_decay']

        # cfg['target_cfg']['metric_kwargs']['weight_decay'] = 0.001

        cfg['initial_learning_rate'] = 1e-3
        cfg['learning_rate_schedule_kwargs'] = {
            'boundaries': [np.int64(0),
                           np.int64(20000),
                           np.int64(40000)],
            'values': [
                cfg['initial_learning_rate'],
                cfg['initial_learning_rate'] / 5.,
                cfg['initial_learning_rate'] / 10.
            ]
        }
        if type(cfg['input_cfg']) is not list:
            cfg['input_cfg']['num_input'] = 1
        #cfg['target_cfg']['metric_kwargs']['output_size'] =  cfg['target_dim']
        run_extract_losses(args, cfg, loss_dir, task)
def main( _ ):
    args = parser.parse_args()
    global TRAIN_MODE
    TRAIN_MODE = args.train_mode
    #task_list = ["autoencoder", "colorization","curvature", "denoise", "edge2d", "edge3d", "ego_motion", "fix_pose", "impainting", "jigsaw", "keypoint2d", "keypoint3d", "non_fixated_pose", "point_match", "reshade", "rgb2depth", "rgb2mist", "rgb2sfnorm", "room_layout", "segment25d", "segment2d", "vanishing_point"]
    #single channel for colorization !!!!!!!!!!!!!!!!!!!!!!!!! COME BACK TO THIS !!!!!!!!!!!!!!!!!!!!!!!!!!!
    task_list = [ args.task ]

    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print( 'Found devices:', [ x.name for x in local_device_protos ] )  
    # set GPU id
    if args.gpu_id:
        print( 'using gpu %d' % args.gpu_id )
        os.environ[ 'CUDA_VISIBLE_DEVICES' ] = str( args.gpu_id )
    else:
        print( 'no gpu specified' )
    
    for task in task_list:
        if args.is_selected:
            to_task = 'class_selected'
            target_loading_fn = class_selected_imagenet
        else:
            to_task = 'class_1000'
            target_loading_fn = class_1000_imagenet
        if args.pretrain_transfer:
            task_dir = os.path.join(args.cfg_dir, args.pretrain_transfer_type, "{}__{}__8__unlocked".format(task, to_task))
        else:
            task_dir = os.path.join(args.cfg_dir, 'final', task)
        cfg = utils.load_config( task_dir, nopause=args.nopause )
        
        root_dir = cfg['root_dir']
        if args.is_selected:
            split_file = os.path.abspath( os.path.join( root_dir, 'assets/aws_data/val_selected_imagenet_info.pkl') )
        else:
            split_file = os.path.abspath( os.path.join( root_dir, 'assets/aws_data/val_split_imagenet_info.pkl') )
        cfg['dataset_dir'] = '/home/ubuntu/imagenet'

        cfg['train_filenames'] = split_file
        cfg['val_filenames'] = split_file
        cfg['test_filenames'] = split_file 

        if 'train_list_of_fileinfos' in cfg:
            split_file_ =  os.path.join(
                            cfg['input_cfg']['log_root'], task,
                            '{task}_val_imagenet_representations.pkl'.format( task=task ))
            cfg['train_representations_file'] = split_file_
            cfg['val_representations_file'] = split_file_
            cfg['test_representations_file'] = split_file_

            split_file_ =  os.path.join(root_dir, 'assets/aws_data/val_imagenet.npy')
            cfg['train_list_of_fileinfos'] = split_file_
            cfg['val_list_of_fileinfos'] = split_file_
            cfg['test_list_of_fileinfos'] = split_file_

        # cfg['resize_interpolation_order'] = 0
        # if cfg['model_path'] is None:
            # cfg['model_path'] = os.path.join(cfg['dataset_dir'], "model_log", task, "model.permanent-ckpt") 
        cfg['target_from_filenames'] = target_loading_fn
        # Try latest checkpoint by epoch
        cfg['model_path'] = tf.train.latest_checkpoint(
                os.path.join(
                    cfg['log_root'],
                    'logs',
                    'slim-train'
                ))

        # Try latest checkpoint by time
        if cfg['model_path'] is None:
            cfg['model_path'] = tf.train.latest_checkpoint(
                os.path.join(
                    cfg['log_root'],
                    'logs',
                    'slim-train',
                    'time'
                ))      
 
        # Try to get one saved manually
        if cfg['model_path'] is None:  
            cfg['model_path'] = os.path.join(cfg['log_root'], task, "model.permanent-ckpt") 
            # cfg['model_path'] = os.path.join(cfg['log_root'], 'logs', 'slim-train', 'time', "model.ckpt-1350") 

        finetuned=True
        if finetuned:
            cfg['model_path'] = os.path.join(cfg['log_root'], task, "imagenet_scratch") 

        cfg['randomize'] = False
        cfg['num_epochs'] = 3
        cfg['batch_size'] = 32 if TRAIN_MODE else 1
        cfg['num_read_threads'] = 3
        if 'batch_size' in cfg['encoder_kwargs']:
            cfg['encoder_kwargs']['batch_size'] = cfg['batch_size']
        try:
            cfg['target_cfg']['batch_size'] = cfg['batch_size']
        except:
            pass
        try:
            cfg['target_cfg']['encoder_kwargs']['batch_size'] = cfg['batch_size']
        except:
            pass

        loss_dir = args.cfg_dir
        cfg['finetune_encoder_imagenet'] = True
        cfg['data_used'] = 100000000
        cfg['input_cfg']['num_input'] = 1
        run_extract_losses( args, cfg, loss_dir, task )
Ejemplo n.º 19
0
def main(_):
    args = parser.parse_args()
    global TRAIN_MODE
    TRAIN_MODE = args.train_mode
    #task_list = ["autoencoder", "colorization","curvature", "denoise", "edge2d", "edge3d", "ego_motion", "fix_pose", "impainting", "jigsaw", "keypoint2d", "keypoint3d", "non_fixated_pose", "point_match", "reshade", "rgb2depth", "rgb2mist", "rgb2sfnorm", "room_layout", "segment25d", "segment2d", "vanishing_point"]
    #single channel for colorization !!!!!!!!!!!!!!!!!!!!!!!!! COME BACK TO THIS !!!!!!!!!!!!!!!!!!!!!!!!!!!
    task_list = [args.task]

    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print('Found devices:', [x.name for x in local_device_protos])
    # set GPU id
    if args.gpu_id:
        print('using gpu %d' % args.gpu_id)
        os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    else:
        print('no gpu specified')

    for task in task_list:
        task_dir = os.path.join(args.cfg_dir, task)
        cfg = utils.load_config(task_dir, nopause=args.nopause)
        root_dir = cfg['root_dir']

        if args.gen:
            cfg['train_filenames'] = os.path.abspath(
                os.path.join('/home/ubuntu/s3',
                             'meta/val_split_image_info.pkl'))
            cfg['val_filenames'] = os.path.abspath(
                os.path.join('/home/ubuntu/s3',
                             'meta/test_split_image_info.pkl'))
            cfg['test_filenames'] = os.path.abspath(
                os.path.join('/home/ubuntu/s3',
                             'meta/test_split_image_info.pkl'))

        if args.data_split == 'train':
            split_file = cfg['train_filenames']
        elif args.data_split == 'val':
            split_file = cfg['val_filenames']
        elif args.data_split == 'test':
            split_file = cfg['test_filenames']
        else:
            raise NotImplementedError("Unknown data split section: {}".format(
                args.data_split))
        global SPLIT_FILE
        SPLIT_FILE = split_file
        cfg['train_filenames'] = split_file
        cfg['val_filenames'] = split_file
        cfg['test_filenames'] = split_file

        if 'train_list_of_fileinfos' in cfg:
            split_file_ = cfg['{}_representations_file'.format(
                args.data_split)]
            cfg['train_representations_file'] = split_file_
            cfg['val_representations_file'] = split_file_
            cfg['test_representations_file'] = split_file_

            split_file_ = cfg['{}_list_of_fileinfos'.format(args.data_split)]
            cfg['train_list_of_fileinfos'] = split_file_
            cfg['val_list_of_fileinfos'] = split_file_
            cfg['test_list_of_fileinfos'] = split_file_

        if args.representation_task:
            split_file_ = args.representation_task
            if 'multiple_input_tasks' in cfg:
                split_file_ = [split_file_]
            cfg['train_representations_file'] = split_file_
            cfg['val_representations_file'] = split_file_
            cfg['test_representations_file'] = split_file_

        # Flip order
        # with open('/home/ubuntu/task-taxonomy-331b/tools/second_order_should_flip.pkl', 'rb') as fp:
        #     to_flip_dict = pickle.load(fp)
        # if not to_flip_dict[task.split('/')[-1]]:
        #     cfg['val_representations_file'] = cfg['val_representations_file'][::-1]

        # Try latest checkpoint by epoch
        #if cfg['model_path'] is None:
        cfg['model_path'] = tf.train.latest_checkpoint(
            os.path.join(cfg['log_root'], 'logs', 'slim-train'))

        # Try latest checkpoint by time
        if cfg['model_path'] is None:
            cfg['model_path'] = tf.train.latest_checkpoint(
                os.path.join(cfg['log_root'], 'logs', 'slim-train', 'time'))

        # Try to get one saved manually
        if cfg['model_path'] is None:
            if task.split('/')[0] == 'final':
                cfg['model_path'] = os.path.join(cfg['log_root'],
                                                 task.split('/')[-1],
                                                 "model.permanent-ckpt")
                task = task.split('/')[-1]
            else:
                cfg['model_path'] = os.path.join(cfg['log_root'], task,
                                                 "model.permanent-ckpt")
            # cfg['model_path'] = os.path.join(cfg['log_root'], 'logs', 'slim-train', 'time', "model.ckpt-1350")

        cfg['randomize'] = False
        cfg['num_epochs'] = 1
        cfg['batch_size'] = 32 if TRAIN_MODE else 1
        cfg['num_read_threads'] = 1
        if 'batch_size' in cfg['encoder_kwargs']:
            cfg['encoder_kwargs']['batch_size'] = cfg['batch_size']
        try:
            cfg['target_cfg']['batch_size'] = cfg['batch_size']
        except:
            pass
        try:
            cfg['target_cfg']['encoder_kwargs']['batch_size'] = cfg[
                'batch_size']
        except:
            pass

        loss_dir = args.cfg_dir
        run_extract_losses(args, cfg, loss_dir, task)
Ejemplo n.º 20
0
def main(_):
    args = parser.parse_args()
    #task_list = ["autoencoder", "colorization","curvature", "denoise", "edge2d", "edge3d", "ego_motion", "fix_pose", "impainting", "jigsaw", "keypoint2d", "keypoint3d", "non_fixated_pose", "point_match", "reshade", "rgb2depth", "rgb2mist", "rgb2sfnorm", "room_layout", "segment25d", "segment2d", "vanishing_point"]
    #single channel for colorization !!!!!!!!!!!!!!!!!!!!!!!!! COME BACK TO THIS !!!!!!!!!!!!!!!!!!!!!!!!!!!
    task_list = [args.task]

    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print('Found devices:', [x.name for x in local_device_protos])
    # set GPU id
    if args.gpu_id:
        print('using gpu %d' % args.gpu_id)
        os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    else:
        print('no gpu specified')

    for task in task_list:
        task_dir = os.path.join(args.cfg_dir, task)
        cfg = utils.load_config(task_dir, nopause=args.nopause)
        root_dir = cfg['root_dir']

        transfer = (cfg['model_type'] == architectures.TransferNet)
        if not transfer:
            # For siamese tasks, we only need one representation per image. Pairs are irrelevant.
            if args.imagenet:
                cfg['train_filenames'] = os.path.abspath(
                    os.path.join(
                        root_dir,
                        'assets/aws_data/val_split_imagenet_info.pkl'))
                cfg['val_filenames'] = os.path.abspath(
                    os.path.join(
                        root_dir,
                        'assets/aws_data/val_split_imagenet_info.pkl'))
                cfg['dataset_dir'] = '/home/ubuntu/imagenet'
            elif args.places:
                cfg['train_filenames'] = os.path.abspath(
                    os.path.join(root_dir,
                                 'assets/aws_data/train_places_info.pkl'))
                cfg['val_filenames'] = os.path.abspath(
                    os.path.join(root_dir,
                                 'assets/aws_data/val_places_info.pkl'))
                cfg['dataset_dir'] = '/home/ubuntu/place'
            elif args.vid:
                cfg['train_filenames'] = os.path.abspath(
                    os.path.join(
                        root_dir, 'assets/aws_data/video{}_info.pkl'.format(
                            args.vid_id)))
                cfg['val_filenames'] = os.path.abspath(
                    os.path.join(
                        root_dir, 'assets/aws_data/video{}_info.pkl'.format(
                            args.vid_id)))
                cfg['dataset_dir'] = '/home/ubuntu'
                low_sat_tasks = 'autoencoder curvature denoise edge2d edge3d \
                keypoint2d keypoint3d fix_pose random vanishing_point_well_defined \
                reshade rgb2depth rgb2mist rgb2sfnorm point_match ego_motion \
                room_layout segment25d segment2d non_fixated_pose \
                segmentsemantic_rb class_1000 class_places class_selected \
                ego_motion impainting_whole jigsaw'.split()

                if task.split('/')[-1] in low_sat_tasks:
                    cfg['input_preprocessing_fn'] = load_ops.resize_rescale_image_low_sat_2

                if task.split('/')[-1] == 'colorization':
                    cfg['input_preprocessing_fn'] = load_ops.to_light_low_sat

            elif args.gen:
                cfg['train_filenames'] = os.path.abspath(
                    os.path.join('/home/ubuntu/s3',
                                 'meta/val_split_image_info.pkl'))
                cfg['val_filenames'] = os.path.abspath(
                    os.path.join('/home/ubuntu/s3',
                                 'meta/val_split_image_info.pkl'))
                cfg['test_filenames'] = os.path.abspath(
                    os.path.join('/home/ubuntu/s3',
                                 'meta/test_split_image_info.pkl'))
            else:
                cfg['train_filenames'] = os.path.abspath(
                    os.path.join(root_dir, 'assets/aws_data/train_2.pkl'))
                cfg['val_filenames'] = os.path.abspath(
                    os.path.join(root_dir,
                                 'assets/aws_data/val_split_imag_info.pkl'))
                cfg['test_filenames'] = os.path.abspath(
                    os.path.join(root_dir,
                                 'assets/aws_data/test_split_image_info.pkl'))
            cfg['preprocess_fn'] = load_and_specify_preprocessors_for_representation_extraction
        else:
            if args.imagenet:
                cfg['train_filenames'] = os.path.abspath(
                    os.path.join(
                        root_dir,
                        'assets/aws_data/val_split_imagenet_info.pkl'))
                cfg['val_filenames'] = os.path.abspath(
                    os.path.join(
                        root_dir,
                        'assets/aws_data/val_split_imagenet_info.pkl'))
                cfg['dataset_dir'] = '/home/ubuntu/imagenet'
            elif args.places:
                cfg['train_filenames'] = os.path.abspath(
                    os.path.join(root_dir,
                                 'assets/aws_data/train_places_info.pkl'))
                cfg['val_filenames'] = os.path.abspath(
                    os.path.join(root_dir,
                                 'assets/aws_data/val_places_info.pkl'))
                cfg['dataset_dir'] = '/home/ubuntu/place'
            elif args.vid:
                cfg['train_filenames'] = os.path.abspath(
                    os.path.join(
                        root_dir, 'assets/aws_data/video{}_info.pkl'.format(
                            args.vid_id)))
                cfg['val_filenames'] = os.path.abspath(
                    os.path.join(
                        root_dir, 'assets/aws_data/video{}_info.pkl'.format(
                            args.vid_id)))
                cfg['dataset_dir'] = '/home/ubuntu'
            elif args.gen:
                cfg['train_filenames'] = os.path.abspath(
                    os.path.join('/home/ubuntu/s3',
                                 'meta/val_split_image_info.pkl'))
                cfg['val_filenames'] = os.path.abspath(
                    os.path.join('/home/ubuntu/s3',
                                 'meta/test_split_image_info.pkl'))
            else:
                cfg['train_filenames'] = os.path.abspath(
                    os.path.join(root_dir,
                                 'assets/aws_data/val_split_imag_info.pkl'))
                cfg['val_filenames'] = os.path.abspath(
                    os.path.join(root_dir,
                                 'assets/aws_data/test_split_image_info.pkl'))
            cfg['preprocess_fn'] = load_and_specify_preprocessors_for_representation_extraction

        # pdb.set_trace()
        if args.data_split == 'train':
            split_file = cfg['train_filenames']
        elif args.data_split == 'val':
            split_file = cfg['val_filenames']
        elif args.data_split == 'test':
            split_file = cfg['test_filenames']
        else:
            raise NotImplementedError("Unknown data split section: {}".format(
                args.data_split))
        cfg['train_filenames'] = split_file
        cfg['val_filenames'] = split_file
        cfg['test_filenames'] = split_file
        if 'train_list_of_fileinfos' in cfg:
            split_file_ = cfg['{}_representations_file'.format(
                args.data_split)]
            cfg['train_representations_file'] = split_file_
            cfg['val_representations_file'] = split_file_
            cfg['test_representations_file'] = split_file_

            split_file_ = cfg['{}_list_of_fileinfos'.format(args.data_split)]
            cfg['train_list_of_fileinfos'] = split_file_
            cfg['val_list_of_fileinfos'] = split_file_
            cfg['test_list_of_fileinfos'] = split_file_
        if args.representation_task:
            split_file_ = args.representation_task
            if 'multiple_input_tasks' in cfg:
                split_file_ = [split_file_]
            cfg['train_representations_file'] = split_file_
            cfg['val_representations_file'] = split_file_
            cfg['test_representations_file'] = split_file_

        if len(task.split('/')) > 1 and task.split('/')[-2] == 'final':
            task = task.split('/')[-1]
        perm_ckpt = os.path.join(cfg['log_root'], task,
                                 "model.permanent-ckpt.meta")
        if os.path.exists(perm_ckpt):
            cfg['model_path'] = perm_ckpt.replace(".meta", "")
        else:
            cfg['model_path'] = None

        # Try latest checkpoint by epoch
        if cfg['model_path'] is None:
            cfg['model_path'] = tf.train.latest_checkpoint(
                os.path.join(cfg['log_root'], 'logs', 'slim-train'))

        # Try latest checkpoint by time
        if cfg['model_path'] is None:
            cfg['model_path'] = tf.train.latest_checkpoint(
                os.path.join(cfg['log_root'], 'logs', 'slim-train', 'time'))

        if cfg['input_preprocessing_fn'] == load_ops.generate_jigsaw_input:
            cfg['input_preprocessing_fn'] = load_ops.generate_jigsaw_input_for_representation_extraction

        cfg['randomize'] = False
        cfg['num_epochs'] = 2
        cfg['num_read_threads'] = 1
        global REPRESENTATIONS_SHAPE
        REPRESENTATIONS_SHAPE = tuple([cfg['batch_size']] +
                                      REPRESENTATIONS_SHAPE)
        representation_dir = args.cfg_dir
        run_extract_representations(args, cfg, representation_dir, task)
def main(_):
    args = parser.parse_args()
    #task_list = ["autoencoder", "colorization","curvature", "denoise", "edge2d", "edge3d", "ego_motion", "fix_pose", "impainting", "jigsaw", "keypoint2d", "keypoint3d", "non_fixated_pose", "point_match", "reshade", "rgb2depth", "rgb2mist", "rgb2sfnorm", "room_layout", "segment25d", "segment2d", "vanishing_point"]
    #single channel for colorization !!!!!!!!!!!!!!!!!!!!!!!!! COME BACK TO THIS !!!!!!!!!!!!!!!!!!!!!!!!!!!
    task_list = [args.task]

    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print('Found devices:', [x.name for x in local_device_protos])
    # set GPU id
    if args.gpu_id:
        print('using gpu %d' % args.gpu_id)
        os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    else:
        print('no gpu specified')

    for task in task_list:
        task_dir = os.path.join(args.cfg_dir, task)
        cfg = utils.load_config(task_dir, nopause=args.nopause)
        root_dir = cfg['root_dir']

        transfer = (cfg['model_type'] == architectures.TransferNet)
        if not transfer:
            # For siamese tasks, we only need one representation per image. Pairs are irrelevant.
            cfg['train_filenames'] = os.path.abspath(
                os.path.join(root_dir,
                             'assets/aws_data/train_split_image_info.pkl'))
            cfg['val_filenames'] = os.path.abspath(
                os.path.join(root_dir,
                             'assets/aws_data/val_split_imag_info.pkl'))
            cfg['test_filenames'] = os.path.abspath(
                os.path.join(root_dir,
                             'assets/aws_data/test_split_image_info.pkl'))
            cfg['preprocess_fn'] = load_and_specify_preprocessors_for_representation_extraction
        else:
            cfg['train_filenames'] = os.path.abspath(
                os.path.join(root_dir,
                             'assets/aws_data/val_split_imag_info.pkl'))
            cfg['val_filenames'] = os.path.abspath(
                os.path.join(root_dir,
                             'assets/aws_data/test_split_image_info.pkl'))

        if args.data_split == 'train':
            split_file = cfg['train_filenames']
        elif args.data_split == 'val':
            split_file = cfg['val_filenames']
        elif args.data_split == 'test':
            split_file = cfg['test_filenames']
        else:
            raise NotImplementedError("Unknown data split section: {}".format(
                args.data_split))
        cfg['train_filenames'] = split_file
        cfg['val_filenames'] = split_file
        cfg['test_filenames'] = split_file
        if 'train_list_of_fileinfos' in cfg:
            split_file_ = cfg['{}_representations_file'.format(
                args.data_split)]
            cfg['train_representations_file'] = split_file_
            cfg['val_representations_file'] = split_file_
            cfg['test_representations_file'] = split_file_

            split_file_ = cfg['{}_list_of_fileinfos'.format(args.data_split)]
            cfg['train_list_of_fileinfos'] = split_file_
            cfg['val_list_of_fileinfos'] = split_file_
            cfg['test_list_of_fileinfos'] = split_file_
        if args.representation_task:
            split_file_ = args.representation_task
            if 'multiple_input_tasks' in cfg:
                split_file_ = [split_file_]
            cfg['train_representations_file'] = split_file_
            cfg['val_representations_file'] = split_file_
            cfg['test_representations_file'] = split_file_

        if cfg['model_path'] is None:
            cfg['model_path'] = os.path.join(cfg['dataset_dir'], "model_log",
                                             task, "model.permanent-ckpt")
        if cfg['model_type'] == architectures.TransferNet:
            cfg['model_path'] = os.path.join(cfg['log_root'], task,
                                             "model.permanent-ckpt")
        cfg['randomize'] = False
        cfg['num_epochs'] = 1
        cfg['num_read_threads'] = 1

        cfg['input_dim'] = (32, 32)  # (1024, 1024)
        cfg['input_num_channels'] = 1
        cfg['input_dtype'] = tf.float32
        cfg['input_domain_name'] = 'rgb'
        cfg['input_preprocessing_fn'] = to_light
        cfg['input_preprocessing_fn_kwargs'] = {
            'new_dims': cfg['input_dim'],
            'new_scale': [-1, 1],
            'interp_order': 1
        }

        representation_dir = args.cfg_dir
        run_extract_representations(args, cfg, representation_dir, task)
def main(_):
    global args
    args = parser.parse_args()

    #task_list = ["autoencoder", "colorization","curvature", "denoise", "edge2d", "edge3d", "ego_motion", "fix_pose", "impainting", "jigsaw", "keypoint2d", "keypoint3d", "non_fixated_pose", "point_match", "reshade", "rgb2depth", "rgb2mist", "rgb2sfnorm", "room_layout", "segment25d", "segment2d", "vanishing_point"]
    #single channel for colorization !!!!!!!!!!!!!!!!!!!!!!!!! COME BACK TO THIS !!!!!!!!!!!!!!!!!!!!!!!!!!!
    task_list = [args.task]
    #task_list = [ "vanishing_point"]

    # Get available GPUs
    local_device_protos = utils.get_available_devices()
    print('Found devices:', [x.name for x in local_device_protos])
    # set GPU id
    if args.gpu_id:
        print('using gpu %d' % args.gpu_id)
        os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    else:
        print('no gpu specified')

    for task in task_list:
        task_dir = os.path.join(args.cfg_dir, task)
        cfg = utils.load_config(task_dir, nopause=args.nopause)
        root_dir = cfg['root_dir']

        if args.data_split == 'train':
            split_file = cfg['train_filenames']
        elif args.data_split == 'val':
            split_file = cfg['val_filenames']
        elif args.data_split == 'test':
            split_file = cfg['test_filenames']
        else:
            raise NotImplementedError("Unknown data split section: {}".format(
                args.data_split))
        cfg['train_filenames'] = split_file
        cfg['val_filenames'] = split_file
        cfg['test_filenames'] = split_file
        if 'train_list_of_fileinfos' in cfg:
            split_file_ = cfg['{}_representations_file'.format(
                args.data_split)]
            cfg['train_representations_file'] = split_file_
            cfg['val_representations_file'] = split_file_
            cfg['test_representations_file'] = split_file_

            split_file_ = cfg['{}_list_of_fileinfos'.format(args.data_split)]
            cfg['train_list_of_fileinfos'] = split_file_
            cfg['val_list_of_fileinfos'] = split_file_
            cfg['test_list_of_fileinfos'] = split_file_

        if cfg['model_path'] is None:
            cfg['model_path'] = os.path.join(cfg['dataset_dir'], "model_log",
                                             task, "model.permanent-ckpt")
        if cfg['model_type'] == architectures.TransferNet:
            cfg['model_path'] = os.path.join(cfg['log_root'], task,
                                             "model.permanent-ckpt")
        cfg['randomize'] = False
        cfg['num_epochs'] = 1
        cfg['num_read_threads'] = 20
        cfg['batch_size'] = 1

        loss_dir = args.cfg_dir
        avg_img = load_avg_img(cfg, args)
        # print(np.sum(avg_img, axis=2))
        print(avg_img.max())
        print("Average image size: {}".format(avg_img.shape))
        avg_img = map_img_to_target_range(avg_img, cfg)
        run_extract_losses(avg_img, args, cfg, loss_dir, task)