def batch_ood_generator( list_samples, ood_paths, batch_size=32, pre_processing_function=None, resize_size=(128, 128), augment=False, n_label=102, base_path="", ): seq = get_seq() pre_processing_function = (pre_processing_function if pre_processing_function is not None else preprocess_input) while True: shuffle(list_samples) shuffle(ood_paths) for batch_samples, ood_samples in zip( chunks(list_samples, size=batch_size // 2), chunks(ood_paths, size=batch_size // 2), ): images = [ read_img_from_path(base_path + sample.path) for sample in batch_samples ] ood_images = [ read_img_from_path(sample.path) for sample in ood_samples ] if augment: images = seq.augment_images(images=images) ood_images = seq.augment_images(images=ood_images) images = [ resize_img(x, h=resize_size[0], w=resize_size[1]) for x in images ] ood_images = [ resize_img(x, h=resize_size[0], w=resize_size[1]) for x in ood_images ] images = [pre_processing_function(a) for a in images] ood_images = [pre_processing_function(a) for a in ood_images] targets = [sample.label for sample in batch_samples] X = np.array(images + ood_images) Y1 = np.array(targets) - 1 Y1 = np.eye(n_label)[Y1, :].tolist() Y2 = [[1 / n_label for _ in range(n_label)]] * len(ood_samples) Y = np.array(Y1 + Y2) yield X, Y
def batch_generator( list_samples, batch_size=32, pre_processing_function=None, resize_size=(128, 128), augment=False, ): seq = get_seq() pre_processing_function = (pre_processing_function if pre_processing_function is not None else preprocess_input) while True: shuffle(list_samples) for batch_samples in chunks(list_samples, size=batch_size): images = [ read_img_from_path(sample.path) for sample in batch_samples ] if augment: images = seq.augment_images(images=images) images = [ resize_img(x, h=resize_size[0], w=resize_size[1]) for x in images ] images = [pre_processing_function(a) for a in images] targets = [sample.target_vector for sample in batch_samples] X = np.array(images) Y = np.array(targets) yield X, Y
def predict_from_array(self, arr): arr = resize_img(arr, h=self.resize_size[0], w=self.resize_size[1]) arr = self.pre_processing_function(arr) pred = self.model.predict(arr[np.newaxis, ...]).ravel() label, max_score = np.argmax(pred), np.max(pred) label_name = (self.class_display_names[label] if max_score > 0.15 else "Don't Know") # label = label + 1 # labels are 1-indexed while arrays are 0-indexed ..; return {"label": label_name, "score": float(max_score)}
def batch_generator( list_samples, batch_size=32, pre_processing_function=None, resize_size=(128, 128), augment=False, max_value_labels=19982, embedding_size=100, base_path="", ): seq = get_seq() pre_processing_function = (pre_processing_function if pre_processing_function is not None else preprocess_input) while True: shuffle(list_samples) for batch_samples in chunks(list_samples, size=batch_size): images = [ read_img_from_path(base_path + sample.path) for sample in batch_samples ] if augment: images = seq.augment_images(images=images) images = [ resize_img(x, h=resize_size[0], w=resize_size[1]) for x in images ] images = [pre_processing_function(a) for a in images] targets_positive = [ choice(sample.labels) for sample in batch_samples ] targets_negative = targets_positive[::-1] targets_negative = [ x if x not in sample.labels and np.random.uniform(0, 1) < 0.9 else np.random.randint(0, max_value_labels + 1) for x, sample in zip(targets_negative, batch_samples) ] X = np.array(images) Y1 = np.array(targets_positive)[..., np.newaxis] Y2 = np.array(targets_negative)[..., np.newaxis] yield [X, Y1, Y2], np.zeros( (len(batch_samples), 3 * embedding_size))
def predict_from_array(self, arr): arr = resize_img(arr, h=self.resize_size[0], w=self.resize_size[1]) arr = self.pre_processing_function(arr) pred = self.model.predict(arr[np.newaxis, ...]).ravel().tolist() pred = [round(x, 3) for x in pred] pred = {k: v for k, v in zip(self.targets, pred)} pred = { k: v for k, v in sorted( pred.items(), key=lambda item: item[1], reverse=True) } out = dict(itertools.islice(pred.items(), 3)) names, percentages = zip(*out.items()) #pred = sorted(pred, reverse=True) #pred = [pred[i]for i in range(3)] #namesStr = "what" return pred #names, percentages
resize_img, ) if __name__ == "__main__": import argparse parser = argparse.ArgumentParser() parser.add_argument( "--fold", help="fold", default="train_3", ) args = parser.parse_args() with open("../example/training_config.yaml", "r") as f: training_config = yaml.load(f, yaml.SafeLoader) fold = args.fold folder = training_config["data_path"] + fold + "_small" shutil.rmtree(folder, ignore_errors=True) os.makedirs(folder, exist_ok=True) images = glob(training_config["data_path"] + fold + "/*.jpg") for img_path in tqdm(images): img = read_img_from_path(img_path) img = resize_img(img, h=224, w=224) img = img.astype(np.uint8) imwrite(img_path.replace(fold, fold + "_small"), img)
def predict_from_array(self, arr): arr = resize_img(arr, h=self.resize_size[0], w=self.resize_size[1]) arr = self.pre_processing_function(arr) pred = self.model.predict(arr[np.newaxis, ...]).ravel() return pred