示例#1
0
def _scale_lights_new_youtube(path: str) -> Dataset:  # noqa
    full = _scale_common("full",
                         path,
                         "light_labeling",
                         batch="autoronto-lights-boston-chicago")
    splits = full.split([0.9, 0.1])
    youtube_dataset = splits[0]
    youtube_dataset._name = NEW_LIGHTS_YOUTUBE_NAME  # noqa
    test_dataset = splits[1] + Preprocessor.preprocess(NEW_LIGHTS_TRC_NAME)
    test_dataset._name = NEW_LIGHTS_TEST_NAME  # noqa
    Preprocessor.cache_preprocessed_data(NEW_LIGHTS_TEST_NAME, test_dataset)
    return youtube_dataset
示例#2
0
    def _acquire_data(self,
                      dataset: Optional[Union[Dataset, str]],
                      _processor: Type[Processor[ProcessedDataType]],
                      forcePreprocessing=False) -> None:
        if dataset:
            # Get preprocess data if required
            if isinstance(dataset, str):
                self.__dataset = Preprocessor.preprocess(dataset)
            elif isinstance(dataset, Dataset):
                self.__dataset = dataset
            else:
                raise ValueError(
                    f"<dataset> may only be `str` or `Dataset`, not {type(dataset)}"
                )
            # Get processed data from the preprocessed dataset
            self.__data = _processor.process(self.__dataset,
                                             force=forcePreprocessing)

            with open(self._paths["_dataset"], "wb") as dataset_file:
                pickle.dump(self.__dataset, dataset_file)
            with open(self._paths["_data"], "wb") as data_file:
                pickle.dump(self.__data, data_file)

        elif os.path.isfile(self._paths["_dataset"]) and os.path.isfile(
                self._paths["_data"]):
            with open(self._paths["_dataset"], "rb") as dataset_file:
                self.__dataset = pickle.load(dataset_file)
            with open(self._paths["_data"], "rb") as data_file:
                self.__data = pickle.load(data_file)
            print("Data loaded from trainer cache.")
示例#3
0
def run_process(datasets: List[str]) -> None:
    """Processes the data from  the preprocessor so that it can be used
        with the training script immediatelt for yolov3.

        Input: List of dataset names.
        Writes to annotations.txt and classes.txt.
    """

    final_annotations = open("annotations.txt", "w")
    final_classes = open("classes.txt", "w")

    for dataset in datasets:
        data = Preprocessor.preprocess(dataset)

        for img, boxes in data.annotations.items():
            print("Working on image: {}              \r".format(img), end="")
            final_annotations.write("{} ".format(img))

            for box in boxes:
                final_annotations.write("{},{},{},{},{} ".format(
                    box["x_min"], box["y_min"], box["x_max"], box["y_max"],
                    box["class"]))

            final_annotations.write("\n")

    final_annotations.close()
    final_classes.write("\n".join(data.classes))
    final_classes.close()
    print("\n***DONE***\n")
示例#4
0
def confusion(
        model: Model,
        dataset: Union[str, Dataset],
        class_mapping: Callable[[int], int] = lambda x: x,
        num_to_sample: Optional[int] = None,
        iou_threshold: float = 0.25,
        score_threshold: Union[float,
                               List[float]] = 0.5) -> List[ConfusionMatrix]:
    if isinstance(dataset, str):
        dataset = Preprocessor.preprocess(dataset)

    num_classes = len(dataset.classes)
    score_thresholds = [score_threshold] if isinstance(
        score_threshold, float) else score_threshold
    mat = np.zeros((len(score_thresholds), num_classes + 1, num_classes + 1),
                   dtype=np.int32)

    anns = _sample_annotations(dataset, num_to_sample)

    for img_path, labels in tqdm(anns.items()):
        preds = model.predict_path(img_path)

        for threshold_i, threshold in enumerate(score_thresholds):
            preds_filtered = list(
                filter(lambda pred: pred.score >= threshold, preds))

            label_detected = np.zeros(len(labels), dtype=np.bool)
            pred_associated = np.zeros(len(preds_filtered), dtype=np.bool)

            for i, label in enumerate(labels):
                for j, pred in enumerate(preds_filtered):
                    if pred_associated[j]:
                        continue

                    label_class = label.class_index
                    pred_class = class_mapping(pred.class_index)

                    if iou(pred.bounds, label.bounds) >= iou_threshold:
                        mat[threshold_i, label_class, pred_class] += 1
                        if label_class == pred_class:
                            label_detected[i] = True
                            pred_associated[j] = True

            for i, label in enumerate(labels):
                if not label_detected[i]:
                    mat[threshold_i, label.class_index, -1] += 1
            for j, pred in enumerate(preds_filtered):
                if not pred_associated[j]:
                    mat[threshold_i, -1, class_mapping(pred.class_index)] += 1

    return mat
示例#5
0
def get_classes_to_classify(
        y4_signs_folder='Y4Signs_filtered_1036_584_train_split'):
    """
    Function to merge datasets and get classes indices to train
    """
    dataset_y4signs = Preprocessor.preprocess(y4_signs_folder)
    dataset_scalesigns = Preprocessor.preprocess('ScaleSigns')
    dataset = dataset_y4signs + dataset_scalesigns

    NRT_TEXT_IDX, NLT_TEXT_IDX = dataset.classes.index(
        'No_Right_Turn_Text'), dataset.classes.index('No_Left_Turn_Text')
    RTO_TEXT_IDX, LTO_TEXT_IDX = dataset.classes.index(
        'Right Turn Only (words)'), dataset.classes.index(
            'Left Turn Only (words)')

    # classify_nrt = (NRT_TEXT_IDX, (NLT_TEXT_IDX, RTO_TEXT_IDX, LTO_TEXT_IDX))
    to_classify = [NRT_TEXT_IDX, NLT_TEXT_IDX, RTO_TEXT_IDX, LTO_TEXT_IDX]
    # classify_nlt = (NLT_TEXT_IDX, (NRT_TEXT_IDX, RTO_TEXT_IDX, LTO_TEXT_IDX))
    # classify_rto = (RTO_TEXT_IDX, (NLT_TEXT_IDX, NRT_TEXT_IDX, LTO_TEXT_IDX))
    # classify_lto = (LTO_TEXT_IDX, (NLT_TEXT_IDX, RTO_TEXT_IDX, NRT_TEXT_IDX))
    # to_classify = [classify_nrt, classify_nlt, classify_rto, classify_lto]

    return dataset, to_classify
示例#6
0
def latency(model: Model,
            dataset: Union[str, Dataset],
            num_to_sample: Optional[int] = None) -> List[float]:
    if isinstance(dataset, str):
        dataset = Preprocessor.preprocess(dataset)

    anns = _sample_annotations(dataset, num_to_sample)
    times = np.empty(len(anns))

    for i, img_path in enumerate(tqdm(anns)):
        img = cv2.imread(img_path)
        start_time = time.time()
        model.predict(img)
        times[i] = time.time() - start_time

    return times
示例#7
0
    def process(cls,
                dataset: Union[str, Dataset],
                *,
                force: bool = False) -> HaarData:
        """Process all required data from the given <dataset>.

        Generates a processed data object and returns it.

        If <force> is set to `True` then the method will force a processing of
        the dataset even if previous data have been cached.

        Raises `NoPreprocessorException` if a preprocessor for the given
        <dataset> does not exist.
        """
        # Force a processing if the dataset is dynamically generated
        # if isinstance(dataset, Dataset) and dataset.dynamic:
        #     force = True

        # Uses memoization to speed up processing acquisition
        name = dataset if isinstance(dataset, str) else dataset.name

        if not force and name in cls._processed_data:
            print(f"Getting data {name} from processed data cache.")
            return cls._processed_data[name]

        if isinstance(dataset, str):
            dataset = Preprocessor.preprocess(dataset)

        processed_data_path = os.path.join(cls.get_processed_data_path(),
                                           dataset.name)
        if os.path.exists(processed_data_path):
            shutil.rmtree(processed_data_path)
        os.makedirs(processed_data_path)
        processed_data: HaarData = cls._process(dataset)

        cls.cache_processed_data(dataset.name, processed_data)
        return processed_data
示例#8
0
        print(f"{name}".ljust(spaces), end="")
        for value in stat.values():
            print(f"{value:.5f}".ljust(spaces), end="")
        print("")

    print("Total precision: " +
          str(aggregate_true_positive /
              (aggregate_true_positive + aggregate_false_positive)))
    print("Total recall: " +
          str(aggregate_true_positive /
              (aggregate_true_positive + aggregate_false_negative)))


if __name__ == '__main__':
    from lns.common.preprocess import Preprocessor
    scale_lights = Preprocessor.preprocess("scale_lights")
    dataset = scale_lights
    dataset = dataset.merge_classes({
        "green": [
            "GreenLeft", "Green", "GreenRight", "GreenStraight",
            "GreenStraightRight", "GreenStraightLeft", "Green traffic light"
        ],
        "red": [
            "Yellow", "RedLeft", "Red", "RedRight", "RedStraight",
            "RedStraightLeft", "Red traffic light", "Yellow traffic light"
        ],
        "off": ["off"]
    })
    dataset = dataset.minimum_area(0.0005)
    dataset = dataset.remove_perpendicular_lights(0.7)
    for stat_name in ("class", "precision", "recall", "f1"):
        print(f"{stat_name}".ljust(spaces), end="")
    print("")
    for name, stat in stats.items():
        print(f"{name}".ljust(spaces), end="")
        for value in stat.values():
            print(f"{value:.5f}".ljust(spaces), end="")
        print("")

    print("Total precision: " + str(aggregate_true_positive/(aggregate_true_positive + aggregate_false_positive)))
    print("Total recall: " + str(aggregate_true_positive/(aggregate_true_positive + aggregate_false_negative)))


if __name__ == '__main__':
    from lns.common.preprocess import Preprocessor
    dataset_all = Preprocessor.preprocess('ScaleLights')
    '''
    dataset_utias = Preprocessor.preprocess('ScaleLights_New_Utias')
    dataset_youtube = Preprocessor.preprocess('ScaleLights_New_Youtube')

    dataset_scale_utias = dataset_scale.__add__(dataset_utias)
    dataset_all = dataset_scale_utias.__add__(dataset_youtube)
    dataset_all = dataset_all.merge_classes({
    "green": ["goLeft", "Green", "GreenLeft", "GreenStraightRight", "go", "GreenStraightLeft", "GreenRight", "GreenStraight", "3-green", "4-green", "5-green"],
    "yellow": ["warning", "Yellow", "warningLeft", "3-yellow", "4-yellow", "5-yellow"],
    "red": ["stop", "stopLeft", "RedStraightLeft", "Red", "RedLeft", "RedStraight", "RedRight", "3-red", "4-red", "5-red"],
    "off": ["OFF", "off", "3-off", "3-other", "4-off", "4-other", "5-off", "5-other"]
    })
    '''
    '''
    scale_lights = Preprocessor.preprocess("scale_lights")
示例#10
0
import numpy as np

from lns.common.preprocess import Preprocessor
from lns.yolo.train import YoloTrainer
from lns.common import evaluation
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "2"

trainer = YoloTrainer("new_dataset_ac_21")
dataset = Preprocessor.preprocess("ScaleLights_New_Youtube")
# dataset = dataset.merge_classes({
#     "green": ["goLeft", "Green", "GreenLeft", "GreenStraightRight", "go", "GreenStraightLeft", "GreenRight", "GreenStraight", "3-green", "4-green", "5-green"],
#     "yellow": ["warning", "Yellow", "warningLeft", "3-yellow", "4-yellow", "5-yellow"],
#     "red": ["stop", "stopLeft", "RedStraightLeft", "Red", "RedLeft", "RedStraight", "RedRight", "3-red", "4-red", "5-red", "4-red-green", "5-red-green", "5-red-yellow"],
#     "off": ["OFF", "off", "3-off", "3-other", "4-off", "4-other", "5-off", "5-other"]
# })
dataset = dataset.merge_classes({
  "green": ["goLeft", "Green", "GreenLeft", "GreenStraightRight", "go", "GreenStraightLeft", "GreenRight", "GreenStraight", "3-green", "4-green", "5-green"],
  "yellow": ["warning", "Yellow", "warningLeft", "3-yellow", "4-yellow", "5-yellow"],
  "red": ["stop", "stopLeft", "RedStraightLeft", "Red", "RedLeft", "RedStraight", "RedRight", "3-red", "4-red", "5-red"],
  "off": ["OFF", "off", "3-off", "3-other", "4-off", "4-other", "5-off", "5-other"]
})
print(dataset.classes)
print(trainer.dataset.classes)

class_mapping = {trainer.dataset.classes.index(name): dataset.classes.index(name) for name in dataset.classes}
print(class_mapping)

model = trainer.model
class_mapping_fn = lambda x: class_mapping[x]
示例#11
0
    DATASET_DEST_TRAIN = '/home/od/.lns-training/resources/data/Y4Signs_filtered_{0}_{1}_whole'.format(
        IMG_WIDTH, IMG_HEIGHT)
else:
    DATASET_DEST_TRAIN = '/home/od/.lns-training/resources/data/Y4Signs_filtered_{0}_{1}_train_split'.format(
        IMG_WIDTH, IMG_HEIGHT)
    DATASET_DEST_TEST = '/home/od/.lns-training/resources/data/Y4Signs_filtered_{0}_{1}_test_split'.format(
        IMG_WIDTH, IMG_HEIGHT)

DATASET_DEST_TRAIN = OUTPUT_PATH
DATASET_DEST_TEST = DATASET_DEST_TRAIN
#-----------------

random.seed(10)

print('preprocessing raw dataset...')
Preprocessor.preprocess(DATASET_SRC, force=True)
# preprocessor = Y4signs(dataset_name = DATASET_SRC, per_class_limit=0, img_width = IMG_WIDTH, img_height = IMG_HEIGHT)
preprocessor = Y4Signs_2x(dataset_name=DATASET_SRC)
os.makedirs(DATASET_DEST_TRAIN, exist_ok=True)
# if (not os.path.exists(DATASET_DEST_TRAIN)) and (not os.path.exists(DATASET_DEST_TEST)):
#     # os.mkdir(DATASET_DEST_TRAIN)
#     os.makedirs(DATASET_DEST_TRAIN, exist_ok=True)
#     if not DO_NOT_SPLIT:
#         # os.mkdir(DATASET_DEST_TEST)
#         os.makedirs(DATASET_DEST_TEST, exist_ok=True)
# else:
#     print("Destination folders already exist. Please delete those or rename destination folders.")
#     exit(1)

raw_dataset: Dataset = preprocessor.getDataset(
    '/home/od/.lns-training/resources/data/Y4Signs_filtered_1036_584_train_split'
示例#12
0
          str(aggregate_true_positive /
              (aggregate_true_positive + aggregate_false_negative)))


if __name__ == '__main__':
    from lns.common.preprocess import Preprocessor
    from lns.yolo.train import YoloTrainer
    from lns.yolo.settings import YoloSettings
    import os

    os.environ["CUDA_VISIBLE_DEVICES"] = "1"  #choose which GPU to use

    print('processing Dataset')

    #choose which dataset to use
    dataset = Preprocessor.preprocess('SCALE')
    dataset = dataset.merge_classes({"pedestrian": ["ped", "Pedestrian"]})

    splits = dataset.split(
        [0.1,
         0.9])  #can run on subset of full SCALE, since it might take too long
    scale_validation_set = splits[0]
    print(len(scale_validation_set), "images in the data set")
    #scale_validation_set._name = SCALE_SUBSET  # noqa
    # dataset = dataset.minimum_area(0.0005)
    # dataset = dataset.remove_perpendicular_lights(0.7)

    #from lns.common.cv_lights import LightStateModel
    print('importing model')

    trainer = YoloTrainer("yolo_ped_mbd_trial_16",
示例#13
0
    images = dataset.images
    annotations = dataset.annotations

    for image in images:
        distances[image] = []
        image_area = img_area(image)

        for detection in annotations[image]:
            distances[image].append((detection.class_index, detection.bounds.area / image_area))

    return distances


if __name__ == "__main__":
    from lns.common.preprocess import Preprocessor
    Preprocessor.init_cached_preprocessed_data()
    for dataset_name in config.POSSIBLE_DATASETS:
        if dataset_name in ["mocked", "ScaleSigns", "ScaleObjects"]:
            # TODO: ScaleSigns and ScaleObjects haven't been cached yet
            continue
        print(dataset_name)

        dataset = Preprocessor.preprocess(dataset_name).prune(config.THRESHOLDS[dataset_name])
        result = distance(dataset)

        # aggregate statistics over dataset by class
        dists: Dict[int, List[float]] = defaultdict(list)
        for img in result:
            for bb in result[img]:
                dists[bb[0]].append(bb[1])
示例#14
0
# from lns.common.visualization import visualize
import sys
from lns.common.preprocess import Preprocessor
from lns.yolo.train import YoloTrainer
from lns.yolo.settings import YoloSettings
from lns.yolo.settings import LearningRateType
from lns.yolo.settings import Optimizer

#which datasets to include
include_NUScenes = True
include_JAAD = True
include_SCALE = True

if include_NUScenes:
    dataset = Preprocessor.preprocess ('nuscenes')

    pedestrian_id = dataset.classes.index("pedestrian")

    dead_keys = []
    for img, labels in dataset.annotations.items():
        labels = list(filter(lambda label: label.class_index == pedestrian_id, labels))
        if not labels:
            dead_keys.append(img)
        else:
            dataset.annotations[img] = labels
    for dead_key in dead_keys:
        del dataset.annotations[dead_key]
        dataset.images.remove(dead_key)

    dataset = dataset.merge_classes({"pedestrian": dataset.classes})
示例#15
0
    for image_path in image_paths:
        if frame_count < num_frames:
            frame_stream.append(visualize_image(os.path.join(frame_folder_path,image_path),
                                                model=model, visualize_model=True,
                                                threshold=threshold,
                                                classes=class_label, color_mapping=trainer_color_mapping,crop = crop))
            frame_count += 1
        else:
            break
    video_writer = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'XVID'), fps, size)

    for frame in frame_stream:
        video_writer.write(cv2.resize(frame, size))

    print("Processed all image frames and annotated them")
    print(f"Processed {frame_count} frames")
    video_writer.release()
    print("Video stream written!")


if __name__ == '__main__':
    from lns.common.preprocess import Preprocessor
    SCALE = Preprocessor.preprocess("SCALE")
    DATASET = SCALE

    from lns.yolo import YoloTrainer
    TRAINER = YoloTrainer('pedestrian_5')

    generate_video_stream(DATASET, trainer=TRAINER)
示例#16
0
def _scale_lights_new_test(path: str) -> Dataset:  # noqa
    youtube = Preprocessor.preprocess(NEW_LIGHTS_YOUTUBE_NAME)  # noqa
    dataset = Preprocessor.preprocess(NEW_LIGHTS_TEST_NAME)
    return dataset
示例#17
0
from lns.haar.svm_train import SVMTrainer
from lns.common.preprocess import Preprocessor

model_path = '/home/od/.lns-training/resources/trainers/haar'
processed_path_train = '/home/od/.lns-training/resources/processed/haar/y4Signs_train_svm_noise'
processed_path_test = '/home/od/.lns-training/resources/processed/haar/y4Signs_test_svm_noise'
nlrt_signs_data = 'No_Right_Turn_Sym_No_Left_Turn_Sym/data.npy'
nlrt_signs_label = 'No_Right_Turn_Sym_No_Left_Turn_Sym/labels.npy'
nlrt_text_data = 'No_Right_Turn_Text_No_Left_Turn_Text/data.npy'
nlrt_text_label = 'No_Right_Turn_Text_No_Left_Turn_Text/labels.npy'
PROCESS_TRAIN, PROCESS_TEST = False, False
TRAIN_SYM, TRAIN_TEXT = True, True
TEST_SYM, TEST_TEXT = True, True

if PROCESS_TRAIN:
    dataset = Preprocessor.preprocess('Y4Signs_filtered_1036_584_train_split',
                                      force=True)

    to_classify = [(0, 2), (1, 3)]  # class indices to differentiate between
    # (0, 2) means that we are classifying between no_Right_Turn_Sym and no_Left_Turn_Sym

    for a, b in to_classify:
        print("Found classes to differentiate between: {0} and {1}".format(
            a, b))

    processor = SVMProcessor(processed_path_train, dataset, to_classify)
    processor.preprocess(force=True)
    processor.save_np_arrays()

if TRAIN_SYM:
    print("Training No_Right_Turn_Sym_No_Left_Turn_Sym")
    trainer = SVMTrainer(os.path.join(processed_path_train, nlrt_signs_data),
示例#18
0
import cv2

from lns.common.preprocess import Preprocessor
from lns.common.visualization import visualize_image
# from lns.yolo.train import YoloTrainer

dataset = Preprocessor.preprocess("nuscenes")
# dataset = dataset.merge_classes({
#     "green": ["goLeft", "Green", "GreenLeft", "GreenStraightRight", "go", "GreenStraightLeft", "GreenRight", "GreenStraight", "3-green", "4-green", "5-green"],
#     "yellow": ["warning", "Yellow", "warningLeft", "3-yellow", "4-yellow", "5-yellow"],
#     "red": ["stop", "stopLeft", "RedStraightLeft", "Red", "RedLeft", "RedStraight", "RedRight", "3-red", "4-red", "5-red"],
#     "off": ["OFF", "off", "3-off", "3-other", "4-off", "4-other", "5-off", "5-other"]
# })

# trainer = YoloTrainer("new_dataset_ac_21")
print(dataset.classes)
for image in dataset.images:
    img = visualize_image(image,
                          labels=dataset.annotations[image],
                          classes=dataset.classes,
                          show_labels=True)
    cv2.imshow("image", img)
    cv2.waitKey(0)
# visualize(dataset, show_truth=True)
示例#19
0
#     "green": ["goLeft", "Green", "GreenLeft", "GreenStraightRight", "go", "GreenStraightLeft", "GreenRight", "GreenStraight", "3-green", "4-green", "5-green"],
#     "yellow": ["warning", "Yellow", "warningLeft", "3-yellow", "4-yellow", "5-yellow"],
#     "red": ["stop", "stopLeft", "RedStraightLeft", "Red", "RedLeft", "RedStraight", "RedRight", "3-red", "4-red", "5-red"],
#     "off": ["OFF", "off", "3-off", "3-other", "4-off", "4-other", "5-off", "5-other"]
#     })

# from lns.squeezedet.train import SqueezedetTrainer
# trainer = SqueezedetTrainer('squeezedet_fullres_tiffany',dataset_all)

# trainer.train()

#### train haar on lisa and scale
from lns.common.preprocess import Preprocessor
from lns.haar.train import HaarTrainer

dataset_lisa = Preprocessor.preprocess('lisa_signs')
dataset_scale = Preprocessor.preprocess('ScaleSigns', force=True)
print(dataset_scale)
dataset = dataset_lisa.__add__(dataset_scale)
# dataset = dataset.merge_classes({

# })
print(dataset.classes)
exit()
trainer = HaarTrainer('tiffany_test_all', dataset)
trainer.setup()
trainer.train()
"""
===== TRAINING 29-stage =====
<BEGIN
POS count : consumed   1000 : 1149
示例#20
0
from lns.common.preprocess import Preprocessor

dataset_scale = Preprocessor.preprocess('ScaleLights')
dataset_utias = Preprocessor.preprocess('ScaleLights_New_Utias')
dataset_youtube = Preprocessor.preprocess('ScaleLights_New_Youtube')
dataset_all = dataset_scale + dataset_utias + dataset_youtube
dataset_all = dataset_all.merge_classes({
    "green": [
        "goLeft", "Green", "GreenLeft", "GreenStraightRight", "go",
        "GreenStraightLeft", "GreenRight", "GreenStraight", "3-green",
        "4-green", "5-green"
    ],
    "yellow":
    ["warning", "Yellow", "warningLeft", "3-yellow", "4-yellow", "5-yellow"],
    "red": [
        "stop", "stopLeft", "RedStraightLeft", "Red", "RedLeft", "RedStraight",
        "RedRight", "3-red", "4-red", "5-red"
    ],
    "off":
    ["OFF", "off", "3-off", "3-other", "4-off", "4-other", "5-off", "5-other"]
})

import seaborn as sns
import matplotlib.pyplot as plt
from tqdm import tqdm

sns.set_theme()

all_widths = []
all_heights = []
all_list_obj2d = list(dataset_all.annotations.values())
示例#21
0
# from lns.common.visualization import visualize
from lns.common.preprocess import Preprocessor
from lns.yolo.train import YoloTrainer
from lns.yolo.settings import YoloSettings

dataset = Preprocessor.preprocess("nuscenes")
pedestrian_id = dataset.classes.index("pedestrian")

dead_keys = []
for img, labels in dataset.annotations.items():
    labels = list(
        filter(lambda label: label.class_index == pedestrian_id, labels))
    if not labels:
        dead_keys.append(img)
    else:
        dataset.annotations[img] = labels
for dead_key in dead_keys:
    del dataset.annotations[dead_key]
    dataset.images.remove(dead_key)

dataset = dataset.merge_classes({"ped": dataset.classes})

dataset = dataset + Preprocessor.preprocess("SCALE")
dataset = dataset.merge_classes({"pedestrian": ["ped", "Pedestrian"]})
print(len(dataset))
print(dataset.classes)

# scale = Preprocessor.preprocess("ScaleLights")
# scale_utias = Preprocessor.preprocess("ScaleLights_New_Utias")
# scale_yt = Preprocessor.preprocess("ScaleLights_New_Youtube")
# dataset = scale + scale_utias + scale_yt
from lns.common.preprocess import Preprocessor
from lns.haar.train import HaarTrainer
from lns.haar.eval import evaluate
from lns.haar.settings import HaarSettings

model_name = input("Enter model name: ")
dataset_y4signs = Preprocessor.preprocess('Y4Signs_1036_584_train_manav',
                                          force=True)
print(f"Classes before merge: {dataset_y4signs.classes}")

class_to_classify = ['nrt_nlt_sym', 'nrt_nlt_text', 'Stop',
                     'Yield'][HaarSettings.class_index]

dataset_y4signs = dataset_y4signs.merge_classes({
    "nrt_nlt_text": ['No_Right_Turn_Text', 'No_Left_Turn_Text'],
    "nrt_nlt_sym": ['No_Right_Turn_Sym', 'No_Left_Turn_Sym']
})
# dataset_y4signs.classes[0] = 'nrt_nlt_sym' # 4000 num_pos
# dataset_y4signs.classes[1] = 'nrt_nlt_text' # 4000 num_pos
# dataset_y4signs.classes[2] = 'Stop' # 2000
# dataset_y4signs.classes[3] = 'Yield' #2000
HaarSettings.class_index = dataset_y4signs.classes.index(class_to_classify)
index = dataset_y4signs.classes.index(class_to_classify)

print(f"Classes after merge: {dataset_y4signs.classes}")
print('Training model for: ' +
      dataset_y4signs.classes[HaarSettings.class_index])

trainer = HaarTrainer(name=model_name,
                      class_index=index,
                      dataset=dataset_y4signs,
示例#23
0
# Must be False if you don't want to disrupt any other training.
# Can only be True if no other training instances are running
FORCE_PREPROCESSING = True

# class_index 0 = 'nrt_nlt_sym' # 4000 num_pos
# class_index 1 = 'nrt_nlt_rto_lto_text' # 5000 num_pos
# class_index 2 = 'Stop' # 2000 num_pos
# class_index 3 = 'Yield' # 2000 num_pos
class_to_classify = ['nrt_nlt_sym', 'nrt_nlt_rto_lto_text', 'Stop',
                     'Yield'][HaarSettings.class_index]
model_name = input("Enter model name: ")

# Get data
dataset_y4signs = Preprocessor.preprocess(
    'Y4Signs_filtered_1036_584_train_split',
    force=True)  # force = True will give stats
dataset_scalesigns = Preprocessor.preprocess('ScaleSigns')

# Keep only RTO and LTO in ScaleSigns
indexes_to_keep = {
    dataset_scalesigns.classes.index("Right Turn Only (words)"),
    dataset_scalesigns.classes.index("Left Turn Only (words)")
}
irrelevant_imgs = []
for img, labels in dataset_scalesigns.annotations.items():
    labels = list(
        filter(lambda label: label.class_index in indexes_to_keep, labels))
    if not labels:
        irrelevant_imgs.append(img)
    else: