class DriveRun: ########################################################################### # model_path = 'path_to_pretrained_model_name' excluding '.h5' or 'json' # data_path = 'path_to_drive_data' e.g. ../data/2017-09-22-10-12-34-56' def __init__(self, model_path): self.config = Config() self.net_model = NetModel(model_path) self.net_model.load() ########################################################################### # def run(self, image): npimg = np.expand_dims(image, axis=0) measurements = self.net_model.model.predict(npimg) #measurements = measurements / self.config.raw_scale return measurements
class DriveRun: ########################################################################### # model_path = 'path_to_pretrained_model_name' excluding '.h5' or 'json' # data_path = 'path_to_drive_data' e.g. ../data/2017-09-22-10-12-34-56' def __init__(self, model_path): #self.config = Config() self.net_model = NetModel(model_path) self.net_model.load() ########################################################################### # def run(self, input): # input is (image, (vel)) image = input[0] if Config.neural_net['num_inputs'] == 2: velocity = input[1] np_img = np.expand_dims(image, axis=0) #np_img = np.array(np_img).reshape(-1, # Config.neural_net['input_image_height'], # Config.neural_net['input_image_width'], # Config.neural_net['input_image_depth']) if Config.neural_net['num_inputs'] == 2: velocity = np.array(velocity).reshape(-1, 1) predict = self.net_model.model.predict([np_img, velocity]) else: predict = self.net_model.model.predict(np_img) # calc scaled steering angle steering_angle = predict[0][0] steering_angle /= Config.neural_net['steering_angle_scale'] predict[0][0] = steering_angle return predict
class DriveView: ########################################################################### # model_path = 'path_to_pretrained_model_name' excluding '.h5' or 'json' # data_path = 'path_to_drive_data' e.g. ../data/2017-09-22-10-12-34-56' # target_path = path/to/save/view e.g. ../target/ # def __init__(self, model_path, data_path, target_path): # remove the last '/' in data and target path if exists if data_path[-1] == '/': data_path = data_path[:-1] if target_path[-1] == '/': target_path = target_path[:-1] loc_slash = data_path.rfind('/') if loc_slash != -1: # there is '/' in the data path data_name = data_path[loc_slash + 1:] # get folder name #model_name = model_name.strip('/') else: data_name = data_path csv_path = data_path + '/' + data_name + const.DATA_EXT self.data_name = data_name self.data_path = data_path self.target_path = target_path + '/' + data_name + '/' if os.path.isdir(target_path) is False: os.mkdir(target_path) if os.path.isdir(self.target_path) is False: os.mkdir(self.target_path) self.drive_data = DriveData(csv_path) self.drive_data.read(normalize=False) self.data_len = len(self.drive_data.image_names) self.net_model = None self.model_path = None if model_path is not None: from net_model import NetModel self.net_model = NetModel(model_path) self.net_model.load() self.model_path = model_path self.image_process = ImageProcess() self.display = DisplaySettings() def _print_info(self, i, draw, input_image, steering_angle, degree_angle): if self.net_model is not None and Config.neural_net['lstm'] is True: images = [] lstm_time_step = 1 ######################## # inference included if self.net_model is not None: # convert PIL image to numpy array image = np.asarray(input_image) # don't forget OSCAR's default color space is BGR (cv2's default) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # if collected data is not cropped then crop here # otherwise do not crop. if Config.data_collection['crop'] is not True: image = image[Config.data_collection['image_crop_y1']:Config. data_collection['image_crop_y2'], Config.data_collection['image_crop_x1']:Config. data_collection['image_crop_x2']] image = cv2.resize(image, (Config.neural_net['input_image_width'], Config.neural_net['input_image_height'])) image = self.image_process.process(image) ###################### # infer using neural net if Config.neural_net['lstm'] is True: images.append(image) if lstm_time_step >= Config.neural_net['lstm_timestep']: trans_image = np.array(images).reshape( -1, Config.neural_net['lstm_timestep'], Config.neural_net['input_image_height'], Config.neural_net['input_image_width'], Config.neural_net['input_image_depth']) predict = self.net_model.model.predict(trans_image) pred_steering_angle = predict[0][0] pred_steering_angle = pred_steering_angle / Config.neural_net[ 'steering_angle_scale'] del images[0] lstm_time_step += 1 else: # not lstm -- normal cnn npimg = np.expand_dims(image, axis=0) predict = self.net_model.model.predict(npimg) pred_steering_angle = predict[0][0] pred_steering_angle = pred_steering_angle / Config.neural_net[ 'steering_angle_scale'] ##################### # display degree_angle = pred_steering_angle * Config.data_collection[ 'steering_angle_max'] rotated_img = self.display.infer_wheel.image.rotate(degree_angle) input_image.paste(rotated_img, self.display.infer_wheel.image_pos, rotated_img) draw.text(self.display.infer_wheel.label_pos, "Angle: {:.2f}".format(degree_angle), font=self.display.font, fill=self.display.font_color) if self.net_model is not None: diff = abs(pred_steering_angle - self.drive_data.measurements[i][0]) if Config.data_collection['brake'] is True: draw.multiline_text( self.display.info_pos, "Input: {}\nThrottle: {}\nBrake: {}\nSteering: {}\nPredicted: {}\nAbs Diff: {}\nVelocity: {:.2f}\nPosition: (x:{:.2f}, y:{:.2f}, z:{:.2f})" .format( self.drive_data.image_names[i], self.drive_data.measurements[i][1], self.drive_data.measurements[i][2], # steering angle: -1 to 1 scale self.drive_data.measurements[i][0], pred_steering_angle, diff, self.drive_data.velocities[i], self.drive_data.positions_xyz[i][0], self.drive_data.positions_xyz[i][1], self.drive_data.positions_xyz[i][2]), font=self.display.font, fill=self.display.font_color) else: draw.multiline_text( self.display.info_pos, "Input: {}\nThrottle: {}\nSteering: {}\nPredicted: {}\nAbs Diff: {}\nVelocity: {:.2f}\nPosition: (x:{:.2f}, y:{:.2f}, z:{:.2f})" .format( self.drive_data.image_names[i], self.drive_data.measurements[i][1], # steering angle: -1 to 1 scale self.drive_data.measurements[i][0], pred_steering_angle, diff, self.drive_data.velocities[i], self.drive_data.positions_xyz[i][0], self.drive_data.positions_xyz[i][1], self.drive_data.positions_xyz[i][2]), font=self.display.font, fill=self.display.font_color) loc_dot = self.drive_data.image_names[i].rfind('.') target_img_name = "{}_{:.2f}_{:.2f}{}".format( self.drive_data.image_names[i][:loc_dot], pred_steering_angle, degree_angle, const.IMAGE_EXT) else: if Config.data_collection['brake'] is True: draw.multiline_text( self.display.info_pos, "Input: {}\nThrottle: {}\nBrake: {}\nSteering: {}\nVelocity: {:.2f}\nPosition: (x:{:.2f}, y:{:.2f}, z:{:.2f})" .format( self.drive_data.image_names[i], self.drive_data.measurements[i][1], self.drive_data.measurements[i][2], # steering angle: -1 to 1 scale self.drive_data.measurements[i][0], self.drive_data.velocities[i], self.drive_data.positions_xyz[i][0], self.drive_data.positions_xyz[i][1], self.drive_data.positions_xyz[i][2]), font=self.display.font, fill=self.display.font_color) else: draw.multiline_text( self.display.info_pos, "Input: {}\nThrottle: {}\nSteering: {}\nVelocity: {:.2f}\nPosition: (x:{:.2f}, y:{:.2f}, z:{:.2f})" .format( self.drive_data.image_names[i], self.drive_data.measurements[i][1], # steering angle: -1 to 1 scale self.drive_data.measurements[i][0], self.drive_data.velocities[i], self.drive_data.positions_xyz[i][0], self.drive_data.positions_xyz[i][1], self.drive_data.positions_xyz[i][2]), font=self.display.font, fill=self.display.font_color) loc_dot = self.drive_data.image_names[i].rfind('.') target_img_name = "{}_{:.2f}_{:.2f}{}".format( self.drive_data.image_names[i][:loc_dot], steering_angle, degree_angle, const.IMAGE_EXT) # save it input_image.save(self.target_path + target_img_name) ########################################################################### # def run(self): bar = ProgressBar() ############################ # steering angle raw value: # -1 to 1 (0 --> 1: left, 0 --> -1: right) for i in bar(range(self.data_len)): abs_path_image = self.data_path + '/' + self.drive_data.image_names[ i] input_image = Image.open(abs_path_image) steering_angle = self.drive_data.measurements[i][ 0] # -1 to 1 scale degree_angle = steering_angle * Config.data_collection[ 'steering_angle_max'] rotated_img = self.display.label_wheel.image.rotate(degree_angle) input_image.paste(rotated_img, self.display.label_wheel.image_pos, rotated_img) # logo input_image.paste(self.display.logo.image, self.display.logo.image_pos, self.display.logo.image) draw = ImageDraw.Draw(input_image) draw.text(self.display.label_wheel.label_pos, "Angle: {:.2f}".format(degree_angle), font=self.display.font, fill=self.display.font_color) self._print_info(i, draw, input_image, steering_angle, degree_angle)
class DriveTest: ########################################################################### # model_path = 'path_to_pretrained_model_name' excluding '.h5' or 'json' # data_path = 'path_to_drive_data' e.g. ../data/2017-09-22-10-12-34-56' def __init__(self, model_path): self.test_generator = None self.data_path = None self.num_test_samples = 0 self.config = Config() self.net_model = NetModel(model_path) self.net_model.load() self.image_process = ImageProcess() ########################################################################### # def _prepare_data(self, data_path): self.data_path = data_path folder_name = data_path[data_path.rfind('/'):] # get folder name folder_name = folder_name.strip('/') csv_path = data_path + '/' + folder_name + '.csv' # use it for csv file name self.drive = DriveData(csv_path) self.drive.read() self.test_data = list( zip(self.drive.image_names, self.drive.measurements)) self.num_test_samples = len(self.test_data) print('\nTest samples: ', self.num_test_samples) ########################################################################### # def _prep_generator(self): if self.data_path == None: raise NameError('data_path must be set.') def _generator(samples, batch_size=self.config.batch_size): num_samples = len(samples) while True: # Loop forever so the generator never terminates bar = ProgressBar() samples = sklearn.utils.shuffle(samples) for offset in bar(range(0, num_samples, batch_size)): batch_samples = samples[offset:offset + batch_size] images = [] measurements = [] for image_name, measurement in batch_samples: image_path = self.data_path + '/' + image_name + \ self.config.fname_ext image = cv2.imread(image_path) image = cv2.resize(image, (self.config.image_size[0], self.config.image_size[1])) image = self.image_process.process(image) images.append(image) steering_angle, throttle = measurement #angles.append(float(steering_angle)) #measurements.append(steering_angle) measurements.append(steering_angle * self.config.raw_scale) X_train = np.array(images) y_train = np.array(measurements) yield sklearn.utils.shuffle(X_train, y_train) self.test_generator = _generator(self.test_data) ########################################################################### # def _start_test(self): if (self.test_generator == None): raise NameError('Generators are not ready.') print("\nEvaluating the model with test data sets ...") ## Note: Do not use multiprocessing or more than 1 worker. ## This will genereate threading error!!! score = self.net_model.model.evaluate_generator( self.test_generator, self.num_test_samples // self.config.batch_size) #workers=1) print("\nLoss: ", score) #[0], "Accuracy: ", score[1]) #print("\nLoss: ", score[0], "rmse: ", score[1]) ########################################################################### # def test(self, data_path): self._prepare_data(data_path) self._prep_generator() self._start_test()
from matplotlib import pyplot as plt from net_model import NetModel from config import Config #from keras.preprocessing.image import img_to_array import sys import os sys.path.append(str(os.environ['HOME']) + ('/keras-vis')) from vis.utils import utils from vis.visualization import visualize_saliency, overlay import cv2 from scipy import misc model_path = '/home/mir-lab/Ninad_Thesis/new_weights/Training_2_2/2019-02-27-17-31-47' config = Config() net_model = NetModel(model_path) net_model.load() img = utils.load_img('/home/mir-lab/Ninad_Thesis/Test_Salient/2019-02-19-14-30-58-971818.jpg', target_size=(config.image_size[1], config.image_size[0])) print(img.shape) #cv2.imshow('image',img) misc.imsave('original.jpg', img) # Convert to BGR, create input with batch_size: 1. #bgr_img = utils.bgr2rgb(img) img_input = np.expand_dims(img, axis=0) pred = net_model.model.predict(img_input)[0][0] print('Predicted {}'.format(pred)) titles = ['right steering', 'left steering', 'maintain steering'] modifiers = [None, 'negate', 'small_values'] for i, modifier in enumerate(modifiers): heatmap = visualize_saliency(net_model.model, layer_idx=-1, filter_indices=0, seed_input=img, grad_modifier=modifier, keepdims=True)
class DriveTest: ########################################################################### # model_path = 'path_to_pretrained_model_name' excluding '.h5' or 'json' # data_path = 'path_to_drive_data' e.g. ../data/2017-09-22-10-12-34-56' def __init__(self, model_path, data_path): if data_path[-1] == '/': data_path = data_path[:-1] loc_slash = data_path.rfind('/') if loc_slash != -1: # there is '/' in the data path model_name = data_path[loc_slash+1:] # get folder name #model_name = model_name.strip('/') else: model_name = data_path csv_path = data_path + '/' + model_name + const.DATA_EXT self.drive = DriveData(csv_path) self.test_generator = None self.num_test_samples = 0 #self.config = Config() self.net_model = NetModel(model_path) self.net_model.load() self.image_process = ImageProcess() self.data_path = data_path ########################################################################### # def _prepare_data(self): self.drive.read() self.test_data = list(zip(self.drive.image_names, self.drive.measurements)) self.num_test_samples = len(self.test_data) print('Test samples: {0}'.format(self.num_test_samples)) ########################################################################### # def _prep_generator(self): if self.data_path == None: raise NameError('data_path must be set.') def _generator(samples, batch_size=Config.config['batch_size']): num_samples = len(samples) while True: # Loop forever so the generator never terminates bar = ProgressBar() #samples = sklearn.utils.shuffle(samples) for offset in bar(range(0, num_samples, batch_size)): batch_samples = samples[offset:offset+batch_size] images = [] measurements = [] for image_name, measurement in batch_samples: image_path = self.data_path + '/' + image_name image = cv2.imread(image_path) image = cv2.resize(image, (Config.config['input_image_width'], Config.config['input_image_height'])) image = self.image_process.process(image) images.append(image) steering_angle, throttle = measurement measurements.append( steering_angle*Config.config['steering_angle_scale']) X_train = np.array(images) y_train = np.array(measurements) if Config.config['lstm'] is True: X_train = np.array(images).reshape(-1, 1, Config.config['input_image_height'], Config.config['input_image_width'], Config.config['input_image_depth']) y_train = np.array(measurements).reshape(-1, 1, 1) if Config.config['lstm'] is False: yield sklearn.utils.shuffle(X_train, y_train) else: yield X_train, y_train self.test_generator = _generator(self.test_data) ########################################################################### # def _start_test(self): if (self.test_generator == None): raise NameError('Generators are not ready.') print('Evaluating the model with test data sets ...') ## Note: Do not use multiprocessing or more than 1 worker. ## This will genereate threading error!!! score = self.net_model.model.evaluate_generator(self.test_generator, self.num_test_samples//Config.config['batch_size']) #workers=1) print('Loss: {0}'.format(score)) #[0], "Accuracy: ", score[1]) #print("\nLoss: ", score[0], "rmse: ", score[1]) ########################################################################### # def test(self): self._prepare_data() self._prep_generator() self._start_test() Config.summary()
class DriveBatch: ########################################################################### # model_path = 'path_to_pretrained_model_name' excluding '.h5' or 'json' # data_path = 'path_to_drive_data' e.g. ../data/2017-09-22-10-12-34-56' def __init__(self, model_path): self.model = None self.num_test_samples = 0 self.config = Config() self.net_model = NetModel(model_path) self.net_model.load() self.image_process = ImageProcess() ########################################################################### # def _prepare_data(self, data_path): folder_name = data_path[data_path.rfind('/'):] # get folder name folder_name = folder_name.strip('/') csv_path = data_path + '/' + folder_name + '.csv' # use it for csv file name self.drive = DriveData(csv_path) self.drive.read() self.test_data = list( zip(self.drive.image_names, self.drive.measurements)) self.num_test_samples = len(self.test_data) print('\nTest samples: ', self.num_test_samples) ########################################################################### # def run(self, data_path): self._prepare_data(data_path) fname = data_path + '_log.csv' file = open(fname, 'w') #print('image_name', 'label', 'predict', 'abs_error') bar = ProgressBar() file.write('image_name, label, predict, abs_error\n') for image_name, measurement in bar(self.test_data): image_fname = data_path + '/' + image_name + self.config.fname_ext image = cv2.imread(image_fname) image = cv2.resize( image, (self.config.image_size[0], self.config.image_size[1])) image = self.image_process.process(image) npimg = np.expand_dims(image, axis=0) predict = self.net_model.model.predict(npimg) predict = predict / self.config.raw_scale #print(image_name, measurement[0], predict[0][0],\ # abs(measurement[0]-predict[0][0])) log = image_name+','+str(measurement[0])+','+str(predict[0][0])\ +','+str(abs(measurement[0]-predict[0][0])) file.write(log + '\n') file.close() print(fname, 'created.')
class DriveTest: ########################################################################### # model_path = 'path_to_pretrained_model_name' excluding '.h5' or 'json' # data_path = 'path_to_drive_data' e.g. ../data/2017-09-22-10-12-34-56' def __init__(self, model_path, data_path): if data_path[-1] == '/': data_path = data_path[:-1] loc_slash = data_path.rfind('/') if loc_slash != -1: # there is '/' in the data path model_name = data_path[loc_slash+1:] # get folder name #model_name = model_name.strip('/') else: model_name = data_path csv_path = data_path + '/' + model_name + const.DATA_EXT self.data = DriveData(csv_path) self.test_generator = None self.num_test_samples = 0 #self.config = Config() self.net_model = NetModel(model_path) self.net_model.load() self.image_process = ImageProcess() self.data_path = data_path ########################################################################### # def _prepare_data(self): self.data.read() samples = list(zip(self.data.image_names, self.data.velocities, self.data.measurements)) if config['lstm'] is True: self.test_data = self._prepare_lstm_data(samples) else: self.test_data = samples self.num_test_samples = len(self.test_data) print('Test samples: ', self.num_test_samples) ########################################################################### # group the samples by the number of timesteps def _prepare_lstm_data(self, samples): num_samples = len(samples) # get the last index number steps = 1 last_index = (num_samples - config['lstm_timestep'])//steps image_names = [] velocities = [] measurements = [] for i in range(0, last_index, steps): sub_samples = samples[ i : i+config['lstm_timestep'] ] # print('num_batch_sample : ',len(batch_samples)) sub_image_names = [] sub_velocities = [] sub_measurements = [] for image_name, measurment in sub_samples: sub_image_names.append(image_name) sub_velocities.append(velocity) sub_measurements.append(measurment) image_names.append(sub_image_names) velocities.append(sub_velocities) measurements.append(sub_measurements) return list(zip(image_names, velocities, measurements)) ########################################################################### # def _prep_generator(self): if self.data_path == None: raise NameError('data_path must be set.') def _prepare_batch_samples(batch_samples): images = [] velocities = [] measurements = [] for image_name, velocity, measurement in batch_samples: image_path = self.data_path + '/' + image_name image = cv2.imread(image_path) # if collected data is not cropped then crop here # otherwise do not crop. if Config.data_collection['crop'] is not True: image = image[Config.data_collection['image_crop_y1']:Config.data_collection['image_crop_y2'], Config.data_collection['image_crop_x1']:Config.data_collection['image_crop_x2']] image = cv2.resize(image, (config['input_image_width'], config['input_image_height'])) image = self.image_process.process(image) images.append(image) velocities.append(velocity) steering_angle, throttle, brake = measurement if abs(steering_angle) < config['steering_angle_jitter_tolerance']: steering_angle = 0 if config['num_outputs'] == 2: measurements.append((steering_angle*config['steering_angle_scale'], throttle)) else: measurements.append(steering_angle*config['steering_angle_scale']) ## data augmentation <-- doesn't need since this is not training #append, image, steering_angle = _data_augmentation(image, steering_angle) #if append is True: # images.append(image) # measurements.append(steering_angle*config['steering_angle_scale']) return images, velocities, measurements def _prepare_lstm_batch_samples(batch_samples): images = [] velocities = [] measurements = [] for i in range(0, config['batch_size']): images_timestep = [] velocities_timestep = [] measurements_timestep = [] for j in range(0, config['lstm_timestep']): image_name = batch_samples[i][0][j] image_path = self.data_path + '/' + image_name image = cv2.imread(image_path) # if collected data is not cropped then crop here # otherwise do not crop. if Config.data_collection['crop'] is not True: image = image[Config.data_collection['image_crop_y1']:Config.data_collection['image_crop_y2'], Config.data_collection['image_crop_x1']:Config.data_collection['image_crop_x2']] image = cv2.resize(image, (config['input_image_width'], config['input_image_height'])) image = self.image_process.process(image) images_timestep.append(image) velocity = batch_samples[i][1][j] velocities_timestep.append(velocity) if j is config['lstm_timestep']-1: measurement = batch_samples[i][2][j] # if no brake data in collected data, brake values are dummy steering_angle, throttle, brake = measurement if abs(steering_angle) < config['steering_angle_jitter_tolerance']: steering_angle = 0 if config['num_outputs'] == 2: measurements.append((steering_angle*config['steering_angle_scale'], throttle)) else: measurements.append(steering_angle*config['steering_angle_scale']) # data augmentation? """ append, image, steering_angle = _data_augmentation(image, steering_angle) if append is True: images_timestep.append(image) measurements_timestep.append(steering_angle*config['steering_angle_scale']) """ images.append(images_timestep) velocities.append(velocities_timestep) measurements.append(measurements_timestep) return images, velocities, measurements def _generator(samples, batch_size=config['batch_size']): num_samples = len(samples) while True: # Loop forever so the generator never terminates bar = ProgressBar() if config['lstm'] is True: for offset in bar(range(0, (num_samples//batch_size)*batch_size, batch_size)): batch_samples = samples[offset:offset+batch_size] images, measurements = _prepare_lstm_batch_samples(batch_samples) X_train = np.array(images) y_train = np.array(measurements) # reshape for lstm X_train = X_train.reshape(-1, config['lstm_timestep'], config['input_image_height'], config['input_image_width'], config['input_image_depth']) y_train = y_train.reshape(-1, 1) if config['num_inputs'] == 2: X_train_vel = np.array(velocities).reshape(-1, 1) X_train = [X_train, X_train_vel] if config['num_outputs'] == 2: y_train = np.stack([steering_angles, throttles], axis=1).reshape(-1,2) yield X_train, y_train else: samples = sklearn.utils.shuffle(samples) for offset in bar(range(0, num_samples, batch_size)): batch_samples = samples[offset:offset+batch_size] images, velocities, measurements = _prepare_batch_samples(batch_samples) X_train = np.array(images).reshape(-1, config['input_image_height'], config['input_image_width'], config['input_image_depth']) y_train = np.array(measurements) y_train = y_train.reshape(-1, 1) if config['num_inputs'] == 2: X_train_vel = np.array(velocities).reshape(-1, 1) X_train = [X_train, X_train_vel] #if config['num_outputs'] == 2: # y_train = np.stack([steering_angles, throttles], axis=1).reshape(-1,2) #print(y_train) yield X_train, y_train self.test_generator = _generator(self.test_data) ########################################################################### # def _start_test(self): if (self.test_generator == None): raise NameError('Generators are not ready.') print('Evaluating the model with test data sets ...') ## Note: Do not use multiprocessing or more than 1 worker. ## This will genereate threading error!!! score = self.net_model.model.evaluate_generator(self.test_generator, self.num_test_samples//config['batch_size']) #workers=1) print('Loss: {0}'.format(score)) #[0], "Accuracy: ", score[1]) #print("\nLoss: ", score[0], "rmse: ", score[1]) ########################################################################### # def test(self): self._prepare_data() self._prep_generator() self._start_test() Config.summary()
class DriveLog: ########################################################################### # model_path = 'path_to_pretrained_model_name' excluding '.h5' or 'json' # data_path = 'path_to_drive_data' e.g. ../data/2017-09-22-10-12-34-56' def __init__(self, model_path, data_path): if data_path[-1] == '/': data_path = data_path[:-1] loc_slash = data_path.rfind('/') if loc_slash != -1: # there is '/' in the data path model_name = data_path[loc_slash + 1:] # get folder name #model_name = model_name.strip('/') else: model_name = data_path csv_path = data_path + '/' + model_name + const.DATA_EXT self.data_path = data_path self.data = DriveData(csv_path) self.test_generator = None self.num_test_samples = 0 #self.config = Config() self.net_model = NetModel(model_path) self.net_model.load() self.model_path = model_path self.image_process = ImageProcess() self.measurements = [] self.predictions = [] self.differences = [] self.squared_differences = [] ########################################################################### # def _prepare_data(self): self.data.read(normalize=False) self.test_data = list( zip(self.data.image_names, self.data.velocities, self.data.measurements)) self.num_test_samples = len(self.test_data) print('Test samples: {0}'.format(self.num_test_samples)) ########################################################################### # def _savefigs(self, plt, filename): plt.savefig(filename + '.png', dpi=150) plt.savefig(filename + '.pdf', dpi=150) print('Saved ' + filename + '.png & .pdf.') ########################################################################### # def _plot_results(self): plt.figure() # Plot a histogram of the prediction errors num_bins = 25 hist, bins = np.histogram(self.differences, num_bins) center = (bins[:-1] + bins[1:]) * 0.5 plt.bar(center, hist, width=0.05) #plt.title('Historgram of Predicted Errors') plt.xlabel('Steering Angle') plt.ylabel('Number of Predictions') plt.xlim(-1.0, 1.0) plt.plot(np.min(self.differences), np.max(self.differences)) plt.tight_layout() self._savefigs(plt, self.data_path + '_err_hist') plt.figure() # Plot a Scatter Plot of the Error plt.scatter(self.measurements, self.predictions) #plt.title('Scatter Plot of Errors') plt.xlabel('True Values') plt.ylabel('Predictions') plt.axis('equal') plt.axis('square') plt.xlim([-1.0, 1.0]) plt.ylim([-1.0, 1.0]) plt.plot([-1.0, 1.0], [-1.0, 1.0], color='k', linestyle='-', linewidth=.1) plt.tight_layout() self._savefigs(plt, self.data_path + '_scatter') plt.figure() # Plot a Side-By-Side Comparison plt.plot(self.measurements) plt.plot(self.predictions) mean = sum(self.differences) / len(self.differences) variance = sum([((x - mean)**2) for x in self.differences]) / len(self.differences) std = variance**0.5 plt.title('MAE: {0:.3f}, STDEV: {1:.3f}'.format(mean, std)) #plt.title('Ground Truth vs. Prediction') plt.ylim([-1.0, 1.0]) plt.xlabel('Time Step') plt.ylabel('Steering Angle') plt.legend(['ground truth', 'prediction'], loc='upper right') plt.tight_layout() self._savefigs(plt, self.data_path + '_comparison') # show all figures #plt.show() ########################################################################### # def run(self): self._prepare_data() #fname = self.data_path + const.LOG_EXT fname = self.data_path + const.LOG_EXT # use model name to save log file = open(fname, 'w') #print('image_name', 'label', 'predict', 'abs_error') bar = ProgressBar() file.write( 'image_name, label_steering_angle, pred_steering_angle, abs_error, squared_error\n' ) if Config.neural_net['lstm'] is True: images = [] #images_names = [] cnt = 1 for image_name, velocity, measurement in bar(self.test_data): image_fname = self.data_path + '/' + image_name image = cv2.imread(image_fname) # if collected data is not cropped then crop here # otherwise do not crop. if Config.data_collection['crop'] is not True: image = image[ Config.data_collection['image_crop_y1']:Config. data_collection['image_crop_y2'], Config.data_collection['image_crop_x1']:Config. data_collection['image_crop_x2']] image = cv2.resize(image, (Config.neural_net['input_image_width'], Config.neural_net['input_image_height'])) image = self.image_process.process(image) images.append(image) #images_names.append(image_name) if cnt >= Config.neural_net['lstm_timestep']: trans_image = np.array(images).reshape( -1, Config.neural_net['lstm_timestep'], Config.neural_net['input_image_height'], Config.neural_net['input_image_width'], Config.neural_net['input_image_depth']) predict = self.net_model.model.predict(trans_image) pred_steering_angle = predict[0][0] pred_steering_angle = pred_steering_angle / Config.neural_net[ 'steering_angle_scale'] if Config.neural_net['num_outputs'] == 2: pred_throttle = predict[0][1] label_steering_angle = measurement[ 0] # labeled steering angle self.measurements.append(label_steering_angle) self.predictions.append(pred_steering_angle) diff = abs(label_steering_angle - pred_steering_angle) self.differences.append(diff) self.squared_differences.append(diff * 2) log = image_name+','+str(label_steering_angle)+','+str(pred_steering_angle)\ +','+str(diff)\ +','+str(diff**2) file.write(log + '\n') # delete the 1st element del images[0] cnt += 1 else: for image_name, velocity, measurement in bar(self.test_data): image_fname = self.data_path + '/' + image_name image = cv2.imread(image_fname) # if collected data is not cropped then crop here # otherwise do not crop. if Config.data_collection['crop'] is not True: image = image[ Config.data_collection['image_crop_y1']:Config. data_collection['image_crop_y2'], Config.data_collection['image_crop_x1']:Config. data_collection['image_crop_x2']] image = cv2.resize(image, (Config.neural_net['input_image_width'], Config.neural_net['input_image_height'])) image = self.image_process.process(image) npimg = np.expand_dims(image, axis=0) predict = self.net_model.model.predict(npimg) pred_steering_angle = predict[0][0] pred_steering_angle = pred_steering_angle / Config.neural_net[ 'steering_angle_scale'] if Config.neural_net['num_outputs'] == 2: pred_throttle = predict[0][1] label_steering_angle = measurement[0] self.measurements.append(label_steering_angle) self.predictions.append(pred_steering_angle) diff = abs(label_steering_angle - pred_steering_angle) self.differences.append(diff) self.squared_differences.append(diff**2) #print(image_name, measurement[0], predict,\ # abs(measurement[0]-predict)) log = image_name+','+str(label_steering_angle) + ',' + str(pred_steering_angle)\ +','+str(diff)\ +','+str(diff**2) file.write(log + '\n') file.close() print('Saved ' + fname + '.') self._plot_results()