def _build_samples(self, split, day, label_resource_id): assert day > 0 samples = [] for (category, wordnet_synset) in _icubworld28_labels_to_wordnet: for individual in range(1, 4 + 1): sample_dir = os.path.join( self.base_path, split, f"day{day}", category, f"{category}{individual}", ) for filename in sorted(os.listdir(sample_dir)): samples += [ sample.Sample( source=self.__class__.__name__, uid=f"{_namespace_uid}::{split}:{day}:" + f"{category}{individual}:{filename}", ).add_resource( self.__class__.__name__, label_resource_id, f"{_namespace_uid}::{category}{individual}", ).add_resource( self.__class__.__name__, "image_input_np", imageio.imread(os.path.join(sample_dir, filename)), ) ] return samples
def _build_sample(self, image_id, label_id, label_resource_id, split): sample_ = sample.Sample( source=self.__class__.__name__, uid=f"{_namespace_uid}::{split}:{image_id}").add_resource( self.__class__.__name__, label_resource_id, self.uid_for_label_id[label_id], ) if self.use_lazy_mode: sample_ = sample_.add_resource( self.__class__.__name__, "image_location", os.path.join(self.base_path, "images", self.image_location_for_image_id[image_id]), ).add_lazy_resource(self.__class__.__name__, "input_img_np", self._load_from_location) else: sample_ = sample_.add_resource( self.__class__.__name__, "image_location", os.path.join(self.base_path, "images", self.image_location_for_image_id[image_id]), ) sample_ = sample_.add_resource( self.__class__.__name__, "input_img_np", self._load_from_location(sample_), ) return sample_
def restore_inner(self, path): if not os.path.exists(path + "_dfnpool.pkl"): print("Falling back to dfnstate.pkl") with open(path + "_dfnstate.pkl", "rb") as target: (X, y) = pkl.load(target) for i, (Xi, yi) in enumerate(zip(X, y)): self.rehearsal_pool.append( sample.Sample(self.__class__.__name__, uid=f"DFNImport_{i:05d}").add_resource( self.__class__.__name__, "input_img_np", Xi).add_resource( self.__class__.__name__, "zDFN.label", yi)) else: with open(path + "_dfnpool.pkl", "rb") as target: self.rehearsal_pool = pkl.load(target)
def build_sample(self, image_dict, label_resource_id, annotations): image_filename = image_dict["file_name"] image_id = image_dict["id"] sample_ = sample.Sample(source=self.__class__.__name__, uid=f"{_namespace_uid}::{image_filename}") sample_ = sample_.add_resource( self.__class__.__name__, label_resource_id, self._id_to_class[annotations[image_id]], ) sample_ = sample_.add_resource( self.__class__.__name__, "image_location", os.path.join(self.base_path, image_filename), ).add_lazy_resource(self.__class__.__name__, "input_img_np", self._load_from_location) return sample_
def _build_samples(self, X, y, data_range, label_resource_id, prefix): assert X.shape[0] == len(data_range) samples = [] for i, data_id in enumerate(data_range): class_label = y[i, 0] np_image = X[i] samples += [ sample.Sample( source=self.__class__.__name__, uid=f"{_namespace_uid}::{prefix}.{data_id}", ).add_resource( self.__class__.__name__, "input_img_np", np_image).add_resource( self.__class__.__name__, label_resource_id, f"{_namespace_uid}::{_label_names[int(class_label)]}", ) ] return samples
def get_pool_for(self, scenario, run, batch, label_resource_id): # Find the data scenario = str(scenario).lower() assert scenario in ["ni", "nc", "nic"] filelist_path = os.path.join( self.base_path, "batches_filelists", f"{scenario.upper()}_inc", f"run{run:d}", f"{batch}_filelist.txt", ) # Find appropriate label map label_map = self.labels_to_names[scenario][run] samples = [] with open(filelist_path) as filelist: for line in filelist: path, class_id = line.strip().split(" ") samples += [ sample.Sample( source=self.__class__.__name__, uid=f"{_namespace_uid}::{path}").add_resource( self.__class__.__name__, "input_img_np", self.imgs[self.path_to_index[path]], ).add_resource( self.__class__.__name__, label_resource_id, f"{_namespace_uid}::{label_map[int(class_id)]}", ) ] return samples
def _build_sample(self, class_, filename, label_resource_id, individuals): # Open and resize image the_image = Image.open( os.path.join(self.base_path, f"{int(class_['folder']):02d}", filename)) the_image = np.asarray( the_image.resize((self.side_length, self.side_length), Image.ANTIALIAS)) if individuals: label_string = (f"{_namespace_uid}::{class_['class_name']}" + f"{int(class_['individual_id']):02d}") else: label_string = f"{_namespace_uid}::{class_['class_name']}" # Build sample the_sample = (sample.Sample( source=self.__class__.__name__, uid=f"{_namespace_uid}::{class_}.{filename}", ).add_resource(self.__class__.__name__, "input_img_np", the_image).add_resource(self.__class__.__name__, label_resource_id, label_string)) return the_sample