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')
 def loop(self):
     if self.spectator is None or self.vehicle is None or self.path is None:
         time.sleep(1.0)
     else:
         try:
             self.update()
         except RuntimeError as r:
             logger.error(f'Error: {r}')
             logger.warning(f'Setting vehicle to None')
             self.vehicle = None
    def predict(self, state):
        if state is None:
            return None, None

        if np.sign(state.side) < 0:
            return 1, choices[1]
        elif np.sign(state.side) > 0:
            return 0, choices[0]
        else:
            logger.warning('OmniscientAgent is confused!')
            return 0, choices[0]
    def __init__(self):
        self.actors = []
        self.data = Data()
        self.vehicle = None
        self.path = None

        logger.warning('Halting threads')
        self.data.put(DataKey.THREAD_HALT, True)

        self.c = ControllerThread(self.data)
        self.p = PollerThread(self.data)
        self.s = SpectatorFollowThread(self.data)

        self.connection = Connection()
示例#5
0
    def work(self):
        # Attention! Contains infinite loop
        while True:
            self.clear_screen()
            if self.state is not None:
                self.update_screen()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    logger.warning('PyWindow closing')
                    return
            self.i += 1
            pygame.time.wait(50)
            pygame.display.update()
示例#6
0
    def loop(self):
        if self.vehicle is None:
            time.sleep(1.0)
        else:
            try:
                self.poll_acceleration()

                self.poll_position_and_direction()

                self.poll_angular_acceleration()

                self.poll_velocity()

                time.sleep(POLL_TIME)
            except RuntimeError as r:
                logger.error(f'Error: {r}')
                logger.warning(f'Setting vehicle to None')
                self.vehicle = None
示例#7
0
 def control(self, throttle, steering):
     if self.vehicle is not None:
         if throttle >= 0.0:
             try:
                 self.vehicle.apply_control(icarla.vehicle_control(throttle=throttle, steer=steering))
             except RuntimeError as r:
                 logger.error(f'Error: {r}')
                 logger.warning(f'Setting vehicle to None')
                 self.vehicle = None
         else:
             try:
                 throttle *= -1.0
                 self.vehicle.apply_control(icarla.vehicle_control(reverse=True, throttle=throttle, steer=steering))
             except RuntimeError as r:
                 logger.error(f'Error: {r}')
                 logger.warning(f'Setting vehicle to None')
                 self.vehicle = None
     else:
         raise RuntimeError('Vehicle not set in ControllerThread')
示例#8
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
示例#9
0
def move(actor, loc, safe=True):
    """
    Moves the actor to a given location

    | If safe mode is enabled, move becomes a blocking function which retries if it failed"""
    actor.set_location(loc)

    if safe:
        time.sleep(0.5)  # for some reason we must wait for the simulator to process this
        d = actor.get_location().distance(loc)
        if d > 1.0:
            logger.warning(f'Failed moving {actor.type_id} to {loc}, retry...')
            logger.info(f'Actors transform is {actor.get_transform()}')
            actor.set_transform(carla.Transform(add_locations(loc, location(0, 0, 1)), rotation([0, 0, 0])))
            time.sleep(0.5)
            d = actor.get_location().distance(loc)
            if d > 3.0:
                logger.error(f'Failed moving {actor.type_id} to {loc}')
                logger.info(f'Actors transform is {actor.get_transform()}')
                return
示例#10
0
        apply_frame_time(frame_start)

        status = env.check()
    logger.debug(f'{status}')
    return status.traveled


def apply_frame_time(frame_start):
    frame_end = time.time_ns()
    diff = None
    if frame_end is not None and frame_start is not None:
        diff = (frame_end - frame_start) // 1_000_000
    if diff is not None and diff // 1_000 < TARGET_FRAME_TIME:
        time.sleep(TARGET_FRAME_TIME - diff // 1_000)
    else:
        logger.warning('Frame time issue')


def train(agent, memory):
    # memory is a list, containing (prev_state, prev_action, state) triplets
    if TRAIN and not TRAIN_PER_DECISION:
        if TRAIN_MEMORY_SIZE is None or len(memory) >= TRAIN_MEMORY_SIZE:
            logger.info(
                f'{type(agent).__name__} is starting training with memory of {len(memory)}'
            )
            agent.train_on_memory(memory)
            memory.clear()
        else:
            logger.info(f'Memory not full, {len(memory)}/{TRAIN_MEMORY_SIZE}')
示例#11
0
 def save(self):
     if self.network is not None:
         self.network.save_weights(file_name)
     else:
         logger.warning('Cannot save network as it is None')
示例#12
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')
示例#13
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'