def _read_data(self): cache_path = os.path.join(self.data_folder, self.split) cache_path_labels = cache_path + "_labels.pkl" cache_path_images = cache_path + "_images.npz" if not os.path.exists(cache_path_images): png_pkl = cache_path_images[:-4] + '_png.pkl' if os.path.exists(png_pkl): decompress(cache_path_images, png_pkl) else: FileNotFoundError('file not exists! {}'.format(png_pkl)) assert os.path.exists(cache_path_labels) assert os.path.exists(cache_path_images) print_log("\tRead cached labels from {}".format(cache_path_labels), self.log_file) with open(cache_path_labels, "rb") as f: data = pkl.load(f, encoding='bytes') self._label_specific = data[b"label_specific"] self._label_general = data[b"label_general"] self._label_specific_str = data[b"label_specific_str"] self._label_general_str = data[b"label_general_str"] self.labels_str = self._label_specific_str self.labels = self._label_specific print_log("\tRead cached images from {}".format(cache_path_images), self.log_file) with np.load(cache_path_images, mmap_mode="r", encoding='latin1') as data: self.images = data["images"]
def set_trainable(self, layer_regex, log_file): """called in 'workflow.py' Sets model layers as trainable if their names match the given regular expression. """ # fpn.P5_conv1.bias # fpn.P5_conv2.1.weight # fpn.P5_conv2.1.bias # dev_roi.upsample.4.weight # dev_roi.upsample.4.bias # dev_roi.feat_extract.0.weight # dev_roi.feat_extract.0.bias # fpn.C5.0.bn3.weight # fpn.C5.0.bn3.bias # fpn.C5.0.downsample.0.weight # fpn.C5.0.downsample.0.bias for param in self.named_parameters(): layer_name = param[0] trainable = bool(re.fullmatch(layer_regex, layer_name)) if not trainable: param[1].requires_grad = False else: param[1].requires_grad = True for name, param in self.named_parameters(): utils.print_log('\tlayer name: {}\t\treguires_grad: {}'.format( name, param.requires_grad), log_file, quiet_termi=True)
def __init__(self, root, mode, batchsz, n_way, k_shot, k_query, resize, startidx=0, log_file=None, method=None): """ :param root: root path of mini-imagenet :param mode: train, val or test :param batchsz: batch size of sets, not batch of images :param n_way: :param k_shot: :param k_query: num of query images per class :param resize: :param startidx: start to index label from startidx """ self.method = method self.batchsz = batchsz self.n_way = n_way self.k_shot = k_shot self.k_query = k_query self.setsz = self.n_way * self.k_shot # num of samples per set self.querysz = self.n_way * self.k_query # number of samples per set for evaluation self.resize = resize self.startidx = startidx print_log('\t\t%s, b:%d, %d-way, %d-shot, %d-query, resize:%d' % (mode, batchsz, n_way, k_shot, k_query, resize), log_file) # if mode == 'train': # self.transform = transforms.Compose([lambda x: Image.open(x).convert('RGB'), # transforms.RandomResizedCrop(self.resize, scale=(0.8, 1.0)), # # transforms.RandomHorizontalFlip(), # # transforms.RandomVerticalFlip(), # transforms.RandomRotation(15), # transforms.ColorJitter(0.1, 0.1, 0.2, 0), # transforms.ToTensor(), # transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) # ]) # else: self.transform = T.Compose([ lambda x: Image.open(x).convert('RGB'), T.Resize((self.resize, self.resize)), T.ToTensor(), T.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) ]) self.path = os.path.join(root, 'images') csvdata = self._loadCSV(os.path.join(root, mode + '.csv')) self.data = [] # [[img1, img2, ...], [img111, img222, ...]] self.img2label = {} # {"img_name[:9]": label} for i, (k, v) in enumerate(csvdata.items()): self.data.append(v) self.img2label[k] = i + self.startidx self.cls_num = len(self.data) # len=batchsize, each entry is a list of N_way, each way has N_shot examples self.support_x_batch = [] # support set batch self.query_x_batch = [] # query set batch self._create_batch(self.batchsz)
def display(self, log_file, quiet=False): """Display *final* configuration values.""" print_log("Configurations:", file=log_file, quiet_termi=quiet) for a in dir(self): if not a.startswith("__") and not callable(getattr(self, a)): value = getattr(self, a) if isinstance(value, AttrDict): print_log("{}:".format(a), log_file, quiet_termi=quiet) for _, key in enumerate(value): print_log("\t{:30}\t\t{}".format(key, value[key]), log_file, quiet_termi=quiet) else: print_log("{}\t{}".format(a, value), log_file, quiet_termi=quiet) print_log("\n", log_file, quiet_termi=quiet)
def initialize_buffer(self, log_file): """ called in 'utils.py' """ if self.config.DEV.INIT_BUFFER_WEIGHT == 'scratch': utils.print_log('init buffer from scratch ...', log_file) self.buffer = torch.zeros(self.config.DEV.BUFFER_SIZE, 1024, self.config.DATASET.NUM_CLASSES).cuda() self.buffer_cnt = torch.zeros( self.config.DEV.BUFFER_SIZE, 1, self.config.DATASET.NUM_CLASSES).cuda() elif self.config.DEV.INIT_BUFFER_WEIGHT == 'coco_pretrain': utils.print_log('init buffer from pretrain model ...', log_file) NotImplementedError()
def __init__(self, root, mode, n_way, k_shot, k_query, resize, log_file=None, method=None): self.split_folder = 'dataset/tier_split' self.data_folder = root self.method = method self.split = mode self.log_file = log_file self.n_way = n_way self.k_shot = k_shot self.k_query = k_query self.resize = resize # to generate self.images and self.labels self._read_data() print_log( '\t\t%s set, im_num:%d, %d-way, %d-shot, %d-query, im_resize:%d' % (mode, self.images.shape[0], n_way, k_shot, k_query, resize), log_file) self.transform = T.Compose([ lambda ind: Image.fromarray(self.images[ind]).convert( 'RGB'), # self.images[ind]: array, 0-255 T.Resize((self.resize, self.resize)), T.ToTensor(), T.Normalize( (0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) # TODO: comptute this on tier-imagenet ]) self.cls_num = len(np.unique(self.labels)) self.support_sz = self.n_way * self.k_shot # num of samples per support set self.query_sz = self.n_way * self.k_query # num of samples per query set self.support_x_batch = [] # support set batch self.query_x_batch = [] # query set batch self.support_y_batch, self.query_y_batch = [], [] self._create_batch() print_log( '\t{}_data created!!! Not db. Length: {}'.format( self.split, len(self)), self.log_file)
def build_catcode_to_syncode(self): catcode_to_syncode = {} csv_path = os.path.join(self._splits_folder, self._split + '.csv') print_log('\tcsv path is {}'.format(csv_path), self.log_file) with open(csv_path, 'r') as csvfile: csvreader = csv.reader(csvfile, delimiter=',', quotechar='|') for i, row in enumerate(csvreader): # Sometimes there's an empty row at the bottom if len(row) == 0: break if not row[1] in catcode_to_syncode: # Adding synset label row[0] to the list synsets belonging to category row[1] catcode_to_syncode[row[1]] = [] if not row[0] in catcode_to_syncode[row[1]]: catcode_to_syncode[row[1]].append(row[0]) print_log( "\tCreated mapping from category to their synset codes with {} entries (meta-class)." .format(len(catcode_to_syncode), self.log_file)) return catcode_to_syncode
def read_label_split(self): cache_path_labelsplit = self.get_label_split_path() if os.path.exists(cache_path_labelsplit): self._label_split_idx = np.loadtxt(cache_path_labelsplit, dtype=np.int64) else: if self._split in ['train', 'trainval']: print_log( '\tUse {}% image for labeled split.'.format( int(self._label_ratio * 100)), self.log_file) self._label_split_idx = self.label_split() elif self._split in ['val', 'test']: print_log( '\tUse all image in labeled split, since we are in val/test', self.log_file) self._label_split_idx = np.arange(self._images.shape[0]) else: raise ValueError('Unknown split {}'.format(self._split)) self._label_split_idx = np.array(self.label_split(), dtype=np.int64) self.save_label_split()
def label_split(self): """Gets label/unlabel image splits. Returns: labeled_split: List of int. """ print_log('Label split using seed {:d}'.format(self._seed), self.log_file) rnd = np.random.RandomState(self._seed) num_label_cls = len(self._label_str) num_ex = self._labels.shape[0] ex_ids = np.arange(num_ex) labeled_split = [] for cc in range(num_label_cls): cids = ex_ids[self._labels == cc] rnd.shuffle(cids) labeled_split.extend(cids[:int(len(cids) * self._label_ratio)]) print_log("\tTotal number of classes {}".format(num_label_cls), self.log_file) print_log("\tLabeled split {}".format(len(labeled_split)), self.log_file) print_log("\tTotal image {}".format(num_ex), self.log_file) return sorted(labeled_split)
def read_cache(self): """Reads dataset from cached pkl file.""" cache_path_labels, cache_path_images = self.get_cache_path() # Decompress images. if not os.path.exists(cache_path_images): png_pkl = cache_path_images[:-4] + '_png.pkl' if os.path.exists(png_pkl): decompress(cache_path_images, png_pkl) else: return False if os.path.exists(cache_path_labels) and os.path.exists( cache_path_images): print_log("\tRead cached labels from {}".format(cache_path_labels), self.log_file) try: with open(cache_path_labels, "rb") as f: data = pkl.load(f, encoding='bytes') self._label_specific = data[b"label_specific"] self._label_general = data[b"label_general"] self._label_specific_str = data[b"label_specific_str"] self._label_general_str = data[b"label_general_str"] except: with open(cache_path_labels, "rb") as f: data = pkl.load(f) self._label_specific = data["label_specific"] self._label_general = data["label_general"] self._label_specific_str = data["label_specific_str"] self._label_general_str = data["label_general_str"] self._label_str = self._label_specific_str self._labels = self._label_specific print_log("\tRead cached images from {}".format(cache_path_images), self.log_file) with np.load(cache_path_images, mmap_mode="r", encoding='latin1') as data: self._images = data["images"] print_log("\tself._images.shape {}".format(self._images.shape), self.log_file) self.read_label_split() # to obtain: self._label_split_idx return True else: return False
def main(): # output dir if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) print('# Report : make dir -> [{}]'.format(OUTPUT_DIR)) else: print('# Caution : output dir [{}] already exist.'.format(OUTPUT_DIR)) answer = input('# Asking : continue? [y/n]: ') if answer == 'y': pass else: print('# John Doe: bye.') exit() # make csv file with open(OUTPUT_DIR + '/loss.csv', 'w') as file: writer = csv.writer(file) writer.writerow(['time', 'num_updates', 'all_loss', 'policy_loss', 'value_loss', 'entropy_loss']) print('# Report : make csv file -> [{}]'.format(OUTPUT_DIR + '/loss.csv')) with open(OUTPUT_DIR + '/reward.csv', 'w') as file: writer = csv.writer(file) writer.writerow(['reward']) print('# Report : make csv file -> [{}]'.format(OUTPUT_DIR + '/reward.csv')) print('\n# John Doe: Prepare to train...') # parallelize environment envs = [(lambda _i=i: make_env(ENV_NAME, REPRESENTATION, REWARDS, LEFT_AGENT, RIGHT_AGENT, _i)) for i in range(NUM_ENVS)] envs = SubprocVecEnv(envs, context=None) obs_shape = envs.observation_space.shape[1] # envs.observation_space.shape -> (11, 115) action_space = envs.action_space.nvec[0] # instead of [envs.action_space.n] current_obs = torch.zeros(NUM_ENVS, obs_shape) obs = envs.reset() obs = modify_obs(obs) # add current_obs = convert_tensor_obs(obs, current_obs) # initialize rollouts rollouts = RolloutStorage(PER_STEPS, NUM_ENVS, obs_shape, current_obs, LEFT_AGENT) # load model model = ActorCritic(obs_shape, action_space, MODEL_NAME, LEFT_AGENT) # optimizer optimizer = optim.Adam(model.parameters(), lr=LR, eps=EPS) # Device if DEVICE is not None: model.to(DEVICE) rollouts.cuda(DEVICE) current_obs.to(DEVICE) # logging variables episode_rewards = torch.zeros([NUM_ENVS, LEFT_AGENT]) final_rewards = torch.zeros([NUM_ENVS, LEFT_AGENT]) max_reward = -100 num_updates = int(NUM_STEPS // PER_STEPS // NUM_ENVS) print_log('### Output dir: {}'.format(OUTPUT_DIR), OUTPUT_DIR, mode='w') print_log('### Football', OUTPUT_DIR) print_log('# Environment : {}'.format(ENV_NAME), OUTPUT_DIR) print_log('# Num. of envs : {}'.format(NUM_ENVS), OUTPUT_DIR) print_log('# Representation : {}'.format(REPRESENTATION), OUTPUT_DIR) print_log('# Rewards : {}'.format(REWARDS), OUTPUT_DIR) print_log('# Observation shape : {}'.format(obs_shape), OUTPUT_DIR) print_log('# Action space : {}'.format(action_space), OUTPUT_DIR) print_log('# Num. of left agent : {}'.format(LEFT_AGENT), OUTPUT_DIR) print_log('# Num. of right agent: {}'.format(RIGHT_AGENT), OUTPUT_DIR) print_log('### Model', OUTPUT_DIR) print_log('# Base model : {}'.format(MODEL_NAME), OUTPUT_DIR) print_log('# Learning rage: {}'.format(LR), OUTPUT_DIR) print_log('# Device : {}'.format(DEVICE), OUTPUT_DIR) print_log('### Proximal Policy Optimization', OUTPUT_DIR) print_log('# Num. of steps : {}'.format(NUM_STEPS), OUTPUT_DIR) print_log('# Update per steps : {}'.format(PER_STEPS), OUTPUT_DIR) print_log('# Num. of updates : {}'.format(num_updates), OUTPUT_DIR) print_log('# Dis. rate for reward: {}'.format(GAMMA), OUTPUT_DIR) print_log('# Clip param : {}'.format(CLIP_PARAM), OUTPUT_DIR) print_log('# Max gradient norm : {}'.format(MAX_GRAD_NORM), OUTPUT_DIR) print_log('# Num. of epochs : {}'.format(NUM_EPOCHS), OUTPUT_DIR) print_log('# Batch size : {}'.format(N_MINI_BATCH), OUTPUT_DIR) print('\n# John Doe: Start!') for update_i in range(num_updates): for step in range(PER_STEPS): with torch.no_grad(): obs = rollouts.observations[step] if graph_model: obs = Node_f.get_node_feature(obs) value, action, action_log_prob = model.action(obs) action = action.transpose(1, 0) action_log_prob = action_log_prob.transpose(1, 0) # step obs, reward, done, info = envs.step(action.cpu().numpy()) if reward_ball: reward = my_reward_ball(reward, obs[:, 0, 88]) # convert to pytorch tensor reward = torch.from_numpy(np.stack(reward)).float() masks = torch.FloatTensor([[0.0] if d else [1.0] for d in done]) # update reward info for logging episode_rewards += reward final_rewards *= masks final_rewards += (1 - masks) * episode_rewards if True in done: with open(OUTPUT_DIR + '/reward.csv', 'a') as file: writer = csv.writer(file) writer.writerow(final_rewards.mean(0).numpy()) episode_rewards *= masks # Update current observation tensor current_obs *= masks obs = modify_obs(obs) # add current_obs = convert_tensor_obs(obs, current_obs) rollouts.insert(current_obs, action, action_log_prob, value, reward, masks) with torch.no_grad(): obs = rollouts.observations[-1] if graph_model: obs = Node_f.get_node_feature(obs) next_value = model.get_value(obs).detach() # Generalized advantage estimator rollouts.compute_returns(next_value, GAMMA) # update params policy_loss, value_loss, entropy_loss, all_loss = update_params(rollouts, model, optimizer) rollouts.after_update() mean_reward = final_rewards.mean().item() # logging csv with open(OUTPUT_DIR + '/loss.csv', 'a') as file: writer = csv.writer(file) writer.writerow([datetime.datetime.now(), update_i, all_loss, policy_loss, value_loss, entropy_loss]) if update_i % PRINT_INTERVAL == 0: print_log( '# Log : update: [{}/{}] | policy loss: {:.7f} | value loss: {:.7f} | mean reward: {:.3f}'.format( update_i, num_updates, policy_loss, value_loss, mean_reward), OUTPUT_DIR) if update_i % SAVE_INTERVAL == 0: print('# Report : Save model -> [{}]'.format(OUTPUT_DIR + '/model_%i.pt' % update_i)) torch.save(model.state_dict(), os.path.join(OUTPUT_DIR, 'model_%i.pt' % update_i)) if max_reward < mean_reward: print('# Report : Updated max reward.') print('# Report : Save model -> [{}]'.format(OUTPUT_DIR + '/model_max_reward.pt')) torch.save(model.state_dict(), os.path.join(OUTPUT_DIR, 'model_max_reward.pt')) max_reward = mean_reward print('# Report : max reward: {}'.format(max_reward)) print('# John Doe: bye.')
opts = get_basic_parser(get_parser()).parse_args() opts.method = 'gnn' setup(opts) # CREATE MODEL if opts.dataset == 'omniglot': enc_nn = EmbeddingOmniglot(opts, 64).to(opts.device) elif opts.dataset == 'mini-imagenet' or opts.dataset == 'tier-imagenet': enc_nn = EmbeddingImagenet(opts, 128).to(opts.device) else: raise NameError('unknown dataset') metric_nn = MetricNN(opts, emb_size=enc_nn.emb_size).to(opts.device) # RESUME (fixme with appropriate epoch and iter) if os.path.exists(opts.model_file) and os.path.exists(opts.model_file2): print_log('loading previous best checkpoint [{}] ...'.format(opts.model_file), opts.log_file) enc_nn.load_state_dict(torch.load(opts.model_file)) print_log('loading previous best checkpoint [{}] ...'.format(opts.model_file2), opts.log_file) metric_nn.load_state_dict(torch.load(opts.model_file2)) if opts.multi_gpu: print_log('Wrapping network into multi-gpu mode ...', opts.log_file) enc_nn = torch.nn.DataParallel(enc_nn) metric_nn = torch.nn.DataParallel(metric_nn) # PREPARE DATA train_db, val_db, _, _ = data_loader(opts) # MISC # weight_decay = 0
def __init__(self, folder, split, nway=5, nshot=1, num_unlabel=5, num_distractor=5, num_test=5, label_ratio=0.2, shuffle_episode=False, seed=0, aug_90=False, resize=84, log_file=None, method='gnn'): """Creates a meta dataset. Args: folder: the raw data folder split: train/val/test/etc nway: Int. N way classification problem, default 5. nshot: Int. N-shot classification problem, default 1. num_unlabel: Int. Number of unlabeled examples per class, default 2. num_distractor: Int. Number of distractor classes, default 0. num_test: Int. Number of query images, default 10. aug_90: Bool. Whether to augment the training data by rotating 90 degrees. seed: Int. Random seed. use_specific_labels: bool. Whether to use specific or general labels. """ self._splits_folder = 'dataset/tier_split' # labels/synset info/etc (already in the repo) self._data_folder = folder # raw images print_log('\nsplit set: {}'.format(split)) print_log("\tnum unlabel {}".format(num_unlabel), log_file) print_log("\tnum test {}".format(num_test), log_file) print_log("\tnum distractor {}".format(num_distractor), log_file) self.resize = resize self.log_file = log_file self._rnd = np.random.RandomState(seed) self._seed = seed self._split = split self._nway = nway self._num_distractor = num_distractor self._num_test = num_test self._num_unlabel = num_unlabel self._shuffle_episode = shuffle_episode self._label_ratio = label_ratio # self.transform = T.Compose([ # lambda x: Image.open(x).convert('RGB'), # T.Resize((self.resize, self.resize)), # T.ToTensor(), # T.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) # ]) # generate the following variables: see line 216 # self._label_specific # self._label_general # self._label_specific_str # self._label_general_str # self._label_str # self._labels # self._images # self._label_split_idx self.read_cache() # Build a set for quick query. self._label_split_idx = np.array(self._label_split_idx) self._label_split_idx_set = set(list(self._label_split_idx)) self._unlabel_split_idx = list( filter(lambda _idx: _idx not in self._label_split_idx_set, range(self._labels.shape[0]))) self._unlabel_split_idx = np.array(self._unlabel_split_idx) if len(self._unlabel_split_idx) > 0: self._unlabel_split_idx_set = set(self._unlabel_split_idx) else: self._unlabel_split_idx_set = set() num_label_cls = len(self._label_str) self._num_classes = num_label_cls num_ex = self._labels.shape[0] ex_ids = np.arange(num_ex) self._label_idict = {} cnt = 0 for cc in range(num_label_cls): self._label_idict[cc] = ex_ids[self._labels == cc] cnt += len( self._label_idict[cc] ) # TODO weired: cnt should be labelled samples? or the total self._nshot = nshot # # Dictionary mapping categories to their synsets # self._catcode_to_syncode = self.build_catcode_to_syncode() # self._catcode_to_str = self.build_catcode_to_str() # self._syncode_to_str = self.build_syncode_to_str() # # Inverse dictionaries. num_ex = self._label_specific.shape[0] ex_ids = np.arange(num_ex) num_label_cls_specific = len(self._label_specific_str) self._label_specific_idict = {} cnt = 0 for cc in range(num_label_cls_specific): self._label_specific_idict[cc] = ex_ids[self._label_specific == cc] cnt += len(self._label_specific_idict) # TODO weired
# parser.add_argument('-meta_batchsz_test', type=int, default=200) return parser # PARAMS opts = get_basic_parser(get_parser()).parse_args() opts.method = 'relation' setup(opts) # CREATE MODEL net = Relation(opts).to(opts.device) # RESUME (fixme with appropriate epoch and iter) if os.path.exists(opts.model_file): print_log( 'loading previous best checkpoint [{}] ...'.format(opts.model_file), opts.log_file) net.load_state_dict(torch.load(opts.model_file)) if opts.multi_gpu: print_log('Wrapping network into multi-gpu mode ...', opts.log_file) net = torch.nn.DataParallel(net) model_parameters = filter(lambda p: p.requires_grad, net.parameters()) params = sum([np.prod(p.size()) for p in model_parameters]) print_log('Total params in the network: {}'.format(params), opts.log_file) # PREPARE DATA train_db, val_db, _, _ = data_loader(opts) # MISC
def data_loader(opts): train_db, val_db, test_db, trainval_db = [], [], [], [] print_log('\nPreparing datasets: [{:s}] ...'.format(opts.dataset), opts.log_file) if opts.dataset == 'mini-imagenet': train_data = miniImagenet('dataset/miniImagenet/', mode='train', n_way=opts.n_way, k_shot=opts.k_shot, k_query=opts.k_query, batchsz=opts.meta_batchsz_train, resize=opts.im_size, log_file=opts.log_file, method=opts.method) train_db = DataLoader(train_data, opts.batch_sz, shuffle=True, num_workers=8, pin_memory=True) val_data = miniImagenet('dataset/miniImagenet/', mode='val', n_way=opts.n_way, k_shot=opts.k_shot, k_query=opts.k_query, batchsz=opts.meta_batchsz_test, resize=opts.im_size, log_file=opts.log_file, method=opts.method) val_db = DataLoader(val_data, opts.batch_sz, shuffle=True, num_workers=2, pin_memory=True) elif opts.dataset == 'tier-imagenet': USE_SIMPLE_INTERFACE = True if USE_SIMPLE_INTERFACE: train_data = tierImagenet(root='dataset/tier_imagenet/', mode='train', n_way=opts.n_way, k_shot=opts.k_shot, k_query=opts.k_query, resize=opts.im_size, log_file=opts.log_file, method=opts.method) val_data = tierImagenet(root='dataset/tier_imagenet/', mode='val', n_way=opts.n_way, k_shot=opts.k_shot, k_query=opts.k_query, resize=opts.im_size, log_file=opts.log_file, method=opts.method) else: # OLD interface from authors in tensorflow train_data = TieredImageNetDataset('dataset/tier_imagenet/', 'train', nway=opts.n_way, nshot=opts.k_shot, resize=opts.im_size, log_file=opts.log_file, method=opts.method) val_data = TieredImageNetDataset('dataset/tier_imagenet/', 'val', nway=opts.n_way, nshot=opts.k_query, resize=opts.im_size, log_file=opts.log_file, method=opts.method) train_db = DataLoader(train_data, opts.batch_sz, shuffle=True, num_workers=8, pin_memory=True) val_db = DataLoader(val_data, opts.batch_sz, shuffle=True, num_workers=2, pin_memory=True) elif opts.dataset == 'omniglot': print_log('\ntrain data ...', opts.log_file) train_data = OmniglotDataset( mode='train', root='dataset/omniglot') # mode: train/test/val/trainval n_classes = len(np.unique(train_data.y)) if n_classes < opts.classes_per_it_tr or n_classes < opts.classes_per_it_val: raise Exception( 'There are not enough classes in the dataset in order ' 'to satisfy the chosen classes_per_it. Decrease the ' 'classes_per_it_{tr/val} option and try again.') train_db = DataLoader(train_data, batch_sampler=init_sampler( opts, train_data.y, 'train')) print_log('\nval data ...', opts.log_file) val_data = OmniglotDataset(mode='val', root='dataset/omniglot') val_db = DataLoader(val_data, batch_sampler=init_sampler(opts, val_data.y, 'val')) print_log('\ntest data ...', opts.log_file) test_data = OmniglotDataset(mode='test', root='dataset/omniglot') test_db = DataLoader(test_data, batch_sampler=init_sampler( opts, test_data.y, 'test')) else: raise NameError('Unknown dataset!') return train_db, val_db, test_db, trainval_db
def setup(opt2): np.random.seed(opt2.manual_seed) torch.manual_seed(opt2.manual_seed) torch.cuda.manual_seed(opt2.manual_seed) # opt2.__dict__.update(opt1.__dict__)i # opt2.gpu_id = opt2.device_id opt2.output_folder = os.path.join(opt2.root, opt2.method, opt2.dataset, opt2.exp_name) if not os.path.exists(opt2.output_folder): os.makedirs(opt2.output_folder) # sanity check if not hasattr(opt2, 'n_way'): opt2.n_way = opt2.classes_per_it_tr if opt2.method == 'gnn': opt2.model_file = os.path.join( opt2.output_folder, '{:d}_way_{:d}_shot_enc_nn.hyli'.format(opt2.n_way, opt2.k_shot)) opt2.model_file2 = os.path.join( opt2.output_folder, '{:d}_way_{:d}_shot_metric_nn.hyli'.format(opt2.n_way, opt2.k_shot)) opt2.train_N_way = opt2.n_way opt2.test_N_way = opt2.train_N_way opt2.train_N_shots = opt2.k_shot opt2.test_N_shots = opt2.train_N_shots else: opt2.model_file = os.path.join( opt2.output_folder, '{:d}_way_{:d}_shot.hyli'.format(opt2.n_way, opt2.k_shot)) prefix = '[{:s}]'.format(opt2.model_file.replace(opt2.root+'/', '').replace('.hyli', '')) opt2.loss_vis_str = prefix + ' [ep({}) {:04d} / iter({}) {:06d}] loss: {:.4f}' if opt2.use_tensorboard: opt2.tb_folder = os.path.join(opt2.output_folder, 'runs') if not os.path.exists(opt2.tb_folder): os.makedirs(opt2.tb_folder) opt2.log_file = os.path.join(opt2.output_folder, 'training_dynamic.txt') now = datetime.datetime.now() print_log('\nStart timestamp: {:%Y%m%dT%H%M}'.format(now), file=opt2.log_file, init=True) if isinstance(opt2.gpu_id, int): opt2.gpu_id = [opt2.gpu_id] # int -> list # Detect if cuda is there device = 'cuda' if torch.cuda.is_available() else 'cpu' if opt2.gpu_id[0] == -1 and device == 'cuda': print_log('you have cuda and yet gpu_id is set to -1; launching GPU anyway (use gpu 0) ...', file=opt2.log_file) opt2.gpu_id[0] = 0 elif opt2.gpu_id[0] > -1 and device == 'cpu': print_log('you have cpu only and yet gpu_id is set above -1; launching CPU anyway ...', file=opt2.log_file) opt2.gpu_id[0] = -1 multi_gpu = True if len(opt2.gpu_id) > 1 and device == 'cuda' else False if device == 'cuda': print_log('\nGPU mode, gpu_ids: {}'.format(opt2.gpu_id), file=opt2.log_file) else: print_log('\nCPU mode', file=opt2.log_file) opt2.multi_gpu = multi_gpu opt2.device = device # write params to log file print_log('\nWriting params to log: {}'.format(opt2.log_file), file=opt2.log_file) temp = opt2.__dict__ for k in sorted(temp): print_log('\t{:s}:\t\t{}'.format(k, temp[k]), file=opt2.log_file, quiet_termi=True) return opt2
def prepare_data(config, dataset, results, log_file): print_log('\tUse a few? {}'.format(config.TSNE.A_FEW), log_file) print_log('\tsample choice is [ {} ]'.format(config.TSNE.SAMPLE_CHOICE), log_file) choice = config.TSNE.SAMPLE_CHOICE # Step 0 - get all data num_results = len(results) feat_dim = results[0]['feature'].shape[0] - 1 stats = {'sample_num': np.zeros((80, 3), dtype=np.int32)} data = np.zeros((num_results, feat_dim), dtype=np.float32) y = np.zeros(num_results, dtype=np.int32) # label, start from 1 - 80 area_stats = np.zeros(num_results, dtype=np.float32) for ind, inst in enumerate(results): cat_id = inst['category_id'] internal_id = dataset.map_source_class_id( 'coco.{}'.format(cat_id)) # 1-80 stats['sample_num'][internal_id - 1, 0] += 1 area = inst['feature'][-1] # 0-1 score = inst['score'] # to split more accurate data[ind, :] = inst['feature'][:-1] y[ind] = internal_id area_stats[ind] = area print_log( '\t[ALL DATA] median area size is {:.5f}, min {:.5f}, max {}, among {} samples.' .format(np.median(area_stats), np.min(area_stats), np.max(area_stats), len(results)), log_file) # for num in stats['sample_num'][:, 0]: # print(num) # Step 1 - generate data and y if choice == 'set1': cls_list = [1, 57, 3, 74, 40, 42, 46, 61, 55, 10] label_list = [ 'person', 'chair', 'car', 'book', 'bottle', 'cup', 'bowl', 'dining table', 'donut', 'traffic light' ] sample_per_cls = 100 small_percent = 0.3 large_percent = 0.7 for ind, curr_cls in enumerate(cls_list): curr_data = data[y == curr_cls] curr_data_area = area_stats[y == curr_cls] _chosen_ind = np.random.choice(curr_data.shape[0], sample_per_cls) curr_data = curr_data[_chosen_ind] curr_data_area = curr_data_area[_chosen_ind] sorted_area = np.sort(curr_data_area) small_box_thres = sorted_area[int( np.floor(small_percent * len(_chosen_ind)))] large_box_thres = sorted_area[int( np.floor(large_percent * len(_chosen_ind)))] curr_box_size = np.zeros(len(_chosen_ind), dtype=np.int32) curr_box_size[curr_data_area <= small_box_thres] = -1 curr_box_size[curr_data_area >= large_box_thres] = 1 # NOTE: we change the label here: the list index is the new label curr_label = (ind + 1) * np.ones(curr_data.shape[0]) # accumulate data if ind > 0: new_data = np.vstack((new_data, curr_data)) new_y = np.hstack((new_y, curr_label)) new_box_size = np.hstack((new_box_size, curr_box_size)) else: # init new data here new_data = curr_data new_y = curr_label new_box_size = curr_box_size print_log( '\t[NEW DATA] total_cls: {}, sample_per_cls: {}'.format( len(cls_list), sample_per_cls), log_file) # Step 2 n_points = len(new_y) metric = config.TSNE.METRIC perplexity = config.TSNE.PERPLEXITY # TODO: transfer to GPU tensor # TODO(mid): change to other metric other than euclidean dist2 = pairwise_distances(new_data, metric=metric, squared=True) # This return a n x (n-1) prob array pij = manifold.t_sne._joint_probabilities(dist2, perplexity, False) # Convert to n x n prob array pij2 = squareform(pij) i, j = np.indices(pij2.shape) i, j = i.ravel(), j.ravel() out_p_ij = pij2.ravel().astype('float32') # remove self-indices idx = i != j i, j, out_p_ij = i[idx], j[idx], out_p_ij[idx] print_log('\t[NEW DATA] Done! Ready to train!', log_file) return n_points, out_p_ij, i, j, new_y, new_box_size, label_list