def __set_conditions(self):
     current_map_name = self.connection.world.get_map().name
     # Loading correct map
     if current_map_name != MAP_NAME:
         logger.info(f'Loading map: {MAP_NAME} <- {current_map_name}')
         try:
             self.connection.world = self.connection.client.load_world(
                 MAP_NAME)
         except RuntimeError as r:
             logger.critical(f'{r}')
             raise r
     else:
         # Destroying old actors
         actors = self.connection.world.get_actors()
         for actor in actors.filter('vehicle.*.*'):
             actor.destroy()
         for actor in actors.filter('sensor.*.*'):
             actor.destroy()
         if len(actors.filter('vehicle.*.*')) > 0 and len(
                 actors.filter('sensor.*.*')) > 0:
             logger.debug('Cleaned up old actors')
         else:
             logger.warning('Issues while cleaning up old actors')
     # Setting nice weather
     self.__set_weather()
 def setup(self):
     logger.debug('Environment setup')
     self.connection.connect()
     self.__set_conditions()
     # self.__update_path()
     self.__spawn()
     self.reset()
Exemplo n.º 3
0
    def check(self, environment):
        finished = False
        successful = False
        reason = 'UNKNOWN'

        coll = environment.data.get(DataKey.SENSOR_COLLISION)

        pos = environment.data.get(DataKey.SENSOR_POSITION)
        if pos is not None:
            pos = [pos[0], pos[1]]
            d = environment.path.distance(pos)
        else:
            pos = None
            d = -1.0

        if coll is not None:
            finished = True
            reason = 'Collision'
        if d > MAX_OFF_DISTANCE:
            finished = True
            reason = f'Too far from path: {MAX_OFF_DISTANCE} meters'
            logger.debug(pos)
            logger.debug(environment.path.find_segment(pos).start)
        if pos is not None and distance(
                environment.path.find_segment(pos).end,
                environment.path.end) < 0.1:
            finished = True
            successful = True
            reason = 'At finish'
        self.finished = finished
        self.successful = successful
        self.reason = reason
        self.dist = distance(pos, environment.path.start)
        self.traveled = environment.path.distance_along_path(pos)
Exemplo n.º 4
0
def main():
    logger.debug('main starting')

    if ENVIRONMENT_TYPE is EnvironmentTypes.CARLA:
        env = CarlaEnvironment()
    elif ENVIRONMENT_TYPE is EnvironmentTypes.Replay:
        env = ReplayEnvironment()
    else:
        logger.critical('Unknown environment type in main, raising error')
        raise RuntimeError('Unknown environment type in main')

    dashboard = DashboardThread()

    log_settings(env)

    try:
        env.setup()
        env.start()
        dashboard.start()

        # normal_main_loop(env, dashboard)
        train_multiple_main_loop(env, dashboard)

    finally:
        if env is not None:
            for actor in env.actors:
                actor.destroy()
            del env  # Make sure to forget env after we cleaned up it's actors
        logger.debug('Cleaned up')
 def run(self):
     self.beginning()
     while not self.stop:
         while not self.data.get(DataKey.THREAD_HALT) and not self.stop:
             self.loop()
         logger.debug(f'{self.__class__.__name__} sleeping')
         time.sleep(1.0)
     self.finish()
 def run(self):
     self.beginning()
     while not self.stop:
         while not self.stop:
             self.loop()
         logger.debug(f'{self.__class__.__name__} sleeping')
         time.sleep(1.0)
     self.finish()
 def reset(self):
     logger.debug('Resetting actors')
     self.clear()
     logger.warning('Halting threads')
     self.data.put(DataKey.THREAD_HALT, True)
     self.vehicle.apply_control(icarla.vehicle_control(throttle=0, steer=0))
     icarla.set_velocity(self.vehicle, icarla.vector3d())
     logger.debug('Environment reset successful')
Exemplo n.º 8
0
 def load(self):
     if os.path.isfile(
             f'{file_name}.data-00000-of-00001'):  # search for actual file
         self.network.load_weights(file_name)
         logger.debug(
             f'Successfully loaded {file_name} model in KerasModel.load()')
     else:
         logger.error(f'{file_name} file not found in KerasModel.load()')
    def __set_weather(self):
        weather = self.connection.world.get_weather()

        weather.precipitation = 0.0
        weather.precipitation_deposits = 0.0
        weather.wetness = 0.0

        self.connection.world.set_weather(weather)
        logger.debug('Applied nice weather')
 def __spawn(self):
     logger.debug('Spawning actors, sensors')
     if self.path is None:
         spawn_vehicle(self, [0.0, 0.0], [0.0, 0.0, 0.0])
     else:
         spawn_vehicle(self, self.path.start, self.path.direction())
     spawn_camera(self)
     spawn_radar(self)
     spawn_collision(self)
     spawn_obstacle(self)
Exemplo n.º 11
0
def balance(trainables):
    leftchoices = []
    rightchoices = []
    for trainable in trainables:
        if np.argmax(trainable[1]) == 0:
            leftchoices.append(trainable)
        elif np.argmax(trainable[1]) == 1:
            rightchoices.append(trainable)
    leftchoices = leftchoices[:min(len(leftchoices), len(rightchoices))]
    rightchoices = rightchoices[:min(len(leftchoices), len(rightchoices))]
    logger.debug(
        f'Trainables: {len(trainables)} -> {len(leftchoices)+len(rightchoices)}'
    )
    return leftchoices + rightchoices
Exemplo n.º 12
0
 def train_on_memory(self, memory):
     x = 0
     r = [[], []]
     for (prev_state, prev_action, state) in memory:
         x += 1
         reward = self.optimize(state, prev_state, prev_action)
         r[prev_action].append(reward)
     logger.debug(f'Successfully trained {x} times')
     for i, action_rewards in enumerate(r):
         logger.debug(
             f'Action rewards (ID, AVG, AMOUNT) '
             f'-:- {i}; {np.average(action_rewards)}; {len(action_rewards)}'
         )
     self.model.reset()
     self.save()
Exemplo n.º 13
0
def do_run(agent, env, dashboard, statefilters, outputfilters, memory):
    env.clear()
    status = Status()
    prev_state = None
    prev_action = None

    # This is the loop of a run
    while status.finished is False:
        frame_start = time.time_ns()

        # Get data from environment and format it
        data, path, starting_dir = env.pull()
        state = convert(repack(data, path, starting_dir))

        # Apply filters to state
        for sf in statefilters:
            state = sf.filter(state)

        if TRAIN and TRAIN_PER_DECISION and prev_state is not None:
            agent.optimize(state)

        # Agent predicts
        action, out = agent.predict(state)

        # Apply filters to output
        for of in outputfilters:
            action, out = of.filter(action, out)

        # Update dashboard
        dashboard.handle(state, out)

        # Tell env about the choice
        if out is not None:
            env.put(DataKey.CONTROL_OUT, out)

        # Update memory
        if prev_state is not None and prev_action is not None and state is not None:
            memory.append([prev_state, prev_action, state])
        prev_state = state
        prev_action = action

        apply_frame_time(frame_start)

        status = env.check()
    logger.debug(f'{status}')
    return status.traveled
Exemplo n.º 14
0
    def create(self):
        # TODO (5) check validity of model architecture
        network = keras.Sequential([
            # Input      ex.: INPUT_SHAPE = 80, 60, 1
            keras.Input(shape=INPUT_SHAPE),
            # Layer (1)
            layers.Conv2D(16, 7, strides=4, activation='relu', padding='same'),
            # layers.MaxPooling2D(3, strides=2), # TODO (4) valueerror
            layers.Lambda(tf.nn.local_response_normalization),
            # Layer (2)
            layers.Conv2D(32, 5, strides=4, activation='relu', padding='same'),
            # layers.MaxPooling2D(3, strides=2),
            layers.Lambda(tf.nn.local_response_normalization
                          ),  # TODO (3) change to BatchNormalization()
            # Layer (3)
            layers.Conv2D(32, 3, activation='relu', padding='same'),
            # # Layer (4)
            # layers.Conv2D(64, 3, activation='relu', padding='same'),
            # # Layer (5)
            # layers.Conv2D(256, 3, activation='relu', padding='same'),
            layers.MaxPooling2D(2, strides=2),
            layers.Lambda(tf.nn.local_response_normalization),
            layers.Flatten(),  # I added this
            # Layer (6) # I changed 4096->2048->256
            # layers.Dense(128, activation='tanh'),
            # layers.Dropout(0.5),
            # Layer (7) # I changed 4096->512->32
            layers.Dense(8, activation='tanh'),
            # layers.Dropout(0.5),
            # Layer (8)
            layers.Dense(choices_count, activation='softmax'),
        ])

        loss = tf.keras.losses.BinaryCrossentropy()

        network.compile(loss=loss,
                        optimizer=SGD(lr=LEARNING_RATE),
                        metrics=['accuracy'])

        logger.debug('Skipping kerasmodel summary')
        # network.summary()

        self.network = network
Exemplo n.º 15
0
def nameof(var, outside_locals):
    if var is None:
        logger.debug(f'Not searching for name of None var in nameof()')
        return 'None'
    if issubclass(type(var), Iterable) and all(v is None for v in var):
        logger.debug(f'Not searching for name of Nones var in nameof()')
        return 'Nones'
    for string, val in outside_locals.items():
        if type(var) == type(val) and var == val:
            return string
        elif isinstance(val, object):
            try:
                string = nameof(var, val.__dict__)
                if string is not None:
                    return string
            except AttributeError:
                pass

    logger.warning(f'Did not find name of var {var} in nameof()')
    return None
Exemplo n.º 16
0
def set_velocity(actor, velocity, safe=True):
    """
    Sets the velocity of the actor to a given speed

    | If safe mode is enabled, set_velocity becomes a blocking function which retries if it failed"""
    if not safe:
        actor.set_target_velocity(velocity)
    if safe:
        first = True
        while (actor.get_velocity().x**2+actor.get_velocity().y**2+actor.get_velocity().z**2)**0.5 > 0.1:
            if not first:
                logger.debug('Previous attempt at setting vehicle velocity unsuccessful')
            logger.debug('Setting vehicle velocity to 0...')
            actor.set_target_velocity(velocity)
            time.sleep(0.5)
            first = False
        logger.debug('Velocity is approx. 0')
Exemplo n.º 17
0
# based on kerasmodel.py from Tamás Visy's temalab_gta repo
import gc

from settings import choices_count, AGENT_IM_WIDTH, AGENT_IM_HEIGHT
from support.logger import logger

# TODO (2) move and update warnings - best into readme or somewhere where they are actually read

logger.debug('Use a NumPy version with allow_pickle enabled')

logger.warning('Tensorflow warnings and info suppressed')
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

logger.debug('Tensorflow 2 requires 64 bit Python')

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.optimizers import SGD
from tensorflow.lite.python.lite import TFLiteConverterV2
import numpy as np
import os

logger.debug('Keras imports finished')

INPUT_SHAPE = [AGENT_IM_WIDTH, AGENT_IM_HEIGHT, 1]  # [WIDTH, HEIGHT, 1]
LEARNING_RATE = 0.1
PATH_PREFIX = ''
METRIC = 'val_loss'
Exemplo n.º 18
0
 def optimize(self, new_state):
     logger.debug('OmniscientAgent does not optimize')
Exemplo n.º 19
0
import math
import numpy as np

from support.logger import logger

PATH_FILE_NAME = 'files/path.npy'

path_file_points = np.load(PATH_FILE_NAME)
logger.debug(f'Loaded file {PATH_FILE_NAME} for path.py')


class Path:
    # a 2D path consisting of segments

    def __init__(self, points):
        self.points = None
        self.start = None
        self.end = None
        self.__set_points(points)

    def __set_points(self, points):
        self.points = points
        self.start = self.points[0]
        self.end = self.points[-1]

    def slice(self, start=None, to=None):
        if start is None:
            start = 0
        if to is None:
            to = len(self.points)
        self.__set_points(self.points[start:to])
 def clear(self):
     # Clears all data -> removes thread_halt -> threads can resume
     logger.debug('Clearing data')
     self.data.clear()
Exemplo n.º 21
0
 def clear(self):
     if self.window is not None:
         self.window.clear()
     else:
         logger.debug('Window is None, can not handle clear')
 def beginning(self):
     logger.debug(f'{self.__class__.__name__} started')
Exemplo n.º 23
0
 def set_stop(self):
     if self.window is not None:
         self.window.add_event(QUIT)
     else:
         logger.debug('Window is None, can not stop')
Exemplo n.º 24
0
 def handle(self, state, out=None):
     if self.window is not None:
         self.window.handle(state, out)
     else:
         logger.debug('Window is None, can not handle incoming')
Exemplo n.º 25
0
 def save(self):
     logger.debug('OmniscientAgent does not save')
Exemplo n.º 26
0
    def train(self, train, test, epochs=None):
        # awaiting as {ndarray: (X,)}
        # inside: x {ndarray: (2,)}
        # inside: {ndarray: (width, height)}* and float?         *or other way (height, width)
        logger.debug(
            f'Num GPUs Available: {len(tf.config.experimental.list_physical_devices("GPU"))}'
        )

        model_checkpoint_callback = keras.callbacks.ModelCheckpoint(
            filepath=checkpoint_file_names,
            save_weights_only=True,
            monitor=METRIC,
            mode='min',
            save_best_only=False)  # revert to True if needed

        initial_epoch = 0
        found = False
        path = ''
        for i in list(range(1000))[::-1]:
            path = checkpoint_file_names.format(epoch=i)
            if not found and os.path.isfile(path):
                logger.debug(f'File exists, loading checkpoint {path}')
                self.network.load_weights(path)
                if epochs is None:
                    initial_epoch = i
                found = True
        if not found:
            logger.warning(f'No checkpoint found at {path}')

        logger.debug('Reshaping data...')

        train_x = []
        train_y = []
        for i in train:
            train_x.append(i[0])
            train_y.append(i[1])

        train_x = np.array(train_x).reshape(
            [-1, INPUT_SHAPE[0], INPUT_SHAPE[1], INPUT_SHAPE[2]])
        train_y = np.array(train_y)

        logger.debug('Training data finished')

        test_x = np.array([i[0] for i in test]).reshape(
            [-1, INPUT_SHAPE[0], INPUT_SHAPE[1], INPUT_SHAPE[2]])
        test_y = np.array([i[1] for i in test])

        logger.debug(
            'Starting training on {} data with {} data as test set'.format(
                len(train_x), len(test_x)))

        if epochs is None:
            e = 10
        else:
            e = epochs
        # (4.) Training the CNN
        logger.debug('Not printing epoch training data in KerasModel')
        self.network.fit(train_x,
                         train_y,
                         validation_data=(test_x, test_y),
                         epochs=e,
                         initial_epoch=initial_epoch,
                         callbacks=[model_checkpoint_callback],
                         verbose=0)

        # Saving the weights
        self.network.save_weights(file_name)
        logger.debug('Finished training, cleaning up...')
        # Long training session can crash
        tf.keras.backend.clear_session()  # TODO (4) check whether it helps
        gc.collect()  # TODO (2) remove if doesn't help
        logger.debug('Done')
Exemplo n.º 27
0
 def load(self):
     logger.debug('OmniscientAgent does not load')
Exemplo n.º 28
0
 def run(self):
     logger.debug(f'{self.__class__.__name__} started')
     self.window = Window()
     logger.debug(f'{self.__class__.__name__} ready')
     self.window.work()
     logger.debug(f'{self.__class__.__name__} finished')