Exemplo n.º 1
0
class NeuralControl:
    def __init__(self):
        rospy.init_node('BMW_controller')
        self.rate = rospy.Rate(10)
        self.ic = ImageConverter()
        self.image_process = ImageProcess()
        self.drive = DriveRun(sys.argv[1])

        rospy.Subscriber('/image_topic_2', Image, self.controller_cb)
        rospy.Subscriber('/usb_cam/image_raw', Image, self.recorder1)
        self.image = None
        self.image_processed = False

    def recorder1(self, image):
        cam_img = self.ic.imgmsg_to_opencv(image)
        cropImg = cam_img[yMin:yMax, xMin:xMax]
        global newimg
        newimg = cv2.resize(cropImg, (160, 50))

    def controller_cb(self, image):
        img = self.ic.imgmsg_to_opencv(image)
        img_resize = cv2.resize(img, (160, 50))
        stacked_img = np.concatenate((img_resize, newimg), axis=0)

        self.image = self.image_process.process(stacked_img)
        self.image_processed = True
Exemplo n.º 2
0
def main():
    config = Config()
    image_process = ImageProcess()
    
    try:
        if (len(sys.argv) != 3):
            print('Use model_path image_file_name.')
            return
        
        image = cv2.imread(sys.argv[2])
        image = cv2.resize(image, (config.image_size[0],
                                   config.image_size[1]))
        image = image_process.process(image)

        
        if (len(image) == 0):
            print('File open error: ', sys.argv[2])
            return
        
        drive_run = DriveRun(sys.argv[1])
        measurments = drive_run.run(image) 
        print(measurments)

    except KeyboardInterrupt:
        print ('\nShutdown requested. Exiting...')
Exemplo n.º 3
0
def main():

    if (len(sys.argv) != 2):
        print('Give the model path.')
        return

    drive = DriveRun(sys.argv[1])
    config = Config()
    csv_fname = '/home/mir-alb/Ninad_Thesis/Test/Test.csv'
    csv_header = ['image_fname', 'steering_angle']
    df = pd.read_csv(csv_fname, names=csv_header, index_col=False)
    num_data = len(df)
    text = open('/home/mir-lab/Ninad_Thesis/Test/Salient/Salient.txt', 'w+')
    bar = ProgressBar()
    image_process = ImageProcess()

    for i in bar(range(num_data)):
        image_name = df.loc[i]['image_fname']
        steering = df.loc[i]['steering_angle']
        image_path = '/home/mir-lab/Ninad_Thesis/Test/' + image_name + '.jpg'
        image = utils.load_img(image_path, target_size=(config.image_size[1],
                                                        config.image_size[0]))
        image = image_process.process(image)
        prediction = drive.run(image)
        text.write(str(image_name) + '\t' + str(steering) + '\t' + str(prediction))
        
        modifiers = [None, 'negate', 'small_values']
        for i, modifier in enumerate(modifiers):
            heatmap = visualize_saliency(drive.net_model.model, layer_idx=-1,
                                         filter_indices=0, seed_input=image,
                                         grad_modifier=modifier, keepdims=True)
            final = overlay(image, heatmap, alpha=0.5)
            cv2.imwrite('/home/mir-lab/Ninad_Thesis/Test/Salient/' + image_name + '_' + str(i) + '.jpg', final)
Exemplo n.º 4
0
class NeuralControl:
    def __init__(self):
        rospy.init_node('controller')
        self.ic = ImageConverter()
        self.image_process = ImageProcess()
        self.rate = rospy.Rate(30)
        self.drive = DriveRun(sys.argv[1])
        rospy.Subscriber('/bolt/front_camera/image_raw', Image,
                         self.controller_cb)
        self.image = None
        self.image_processed = False
        self.config = Config()

    def controller_cb(self, image):
        img = self.ic.imgmsg_to_opencv(image)
        cropImg = img[self.config.capture_area[1]:self.config.capture_area[3],
                      self.config.capture_area[0]:self.config.capture_area[2]]
        #cropImg = img[yMin:yMax,xMin:xMax]
        img = cv2.resize(
            cropImg, (self.config.image_size[0], self.config.image_size[1]))
        self.image = self.image_process.process(img)

        if self.config.typeofModel == 4 or self.config.typeofModel == 5:
            self.image = np.array(self.image).reshape(
                1, self.config.image_size[1], self.config.image_size[0],
                self.config.image_size[2])
        self.image_processed = True
Exemplo n.º 5
0
def predict_from_camera(drive):
    check, frame = video.read()
    cropImg = frame[yMin:yMax,xMin:xMax]
    newimg = cv2.resize(frame,(160,70))
    image_process = ImageProcess()                              #normalize the image
    IImage = cv2.cvtColor(newimg, cv2.COLOR_RGB2BGR)  
    IImage = image_process.process(IImage)
    prediction= drive.run(IImage)
    print(prediction)
    steering_commands(prediction)
Exemplo n.º 6
0
class NeuralControl:
    def __init__(self):
        rospy.init_node('BMW_controller')
        self.ic = ImageConverter()
        self.image_process = ImageProcess()
        self.rate = rospy.Rate(10)
        self.drive= DriveRun(sys.argv[1])
        rospy.Subscriber('/image_topic_2', Image, self.controller_cb)
        self.image = None
        self.image_processed = False

    def controller_cb(self, image): 
        img = self.ic.imgmsg_to_opencv(image)
        img = cv2.resize(img,(160,70))
        self.image = self.image_process.process(img)
        self.image_processed = True
Exemplo n.º 7
0
class NeuralControl:
    def __init__(self):
        rospy.init_node('controller')
        self.ic = ImageConverter()
        self.image_process = ImageProcess()
        self.rate = rospy.Rate(10)
        self.drive= DriveRun(sys.argv[1])
        rospy.Subscriber('/bulldogbolt/camera_left/image_raw_left', Image, self.controller_cb)
        self.image = None
        self.image_processed = False

    def controller_cb(self, image): 
        img = self.ic.imgmsg_to_opencv(image)
	cropImg = img[yMin:yMax,xMin:xMax]
        img = cv2.resize(cropImg,(160,70))
        self.image = self.image_process.process(img)
        self.image_processed = True
Exemplo n.º 8
0
class NeuralControl:
    def __init__(self, weight_file_name):
        rospy.init_node('run_neural')
        self.ic = ImageConverter()
        self.image_process = ImageProcess()
        self.rate = rospy.Rate(30)
        self.drive = DriveRun(weight_file_name)
        rospy.Subscriber(Config.data_collection['camera_image_topic'], Image,
                         self._controller_cb)
        self.image = None
        self.image_processed = False
        #self.config = Config()
        self.braking = False

    def _controller_cb(self, image):
        img = self.ic.imgmsg_to_opencv(image)
        cropped = img[Config.data_collection['image_crop_y1']:Config.
                      data_collection['image_crop_y2'],
                      Config.data_collection['image_crop_x1']:Config.
                      data_collection['image_crop_x2']]

        img = cv2.resize(
            cropped,
            (config['input_image_width'], config['input_image_height']))

        self.image = self.image_process.process(img)

        ## this is for CNN-LSTM net models
        if config['lstm'] is True:
            self.image = np.array(self.image).reshape(
                1, config['input_image_height'], config['input_image_width'],
                config['input_image_depth'])
        self.image_processed = True

    def _timer_cb(self):
        self.braking = False

    def apply_brake(self):
        self.braking = True
        timer = threading.Timer(Config.run_neural['brake_apply_sec'],
                                self._timer_cb)
        timer.start()
Exemplo n.º 9
0
def main(model_path, input):
    image_file_name = input[0]
    if Config.neural_net['num_inputs'] == 2:
        velocity = input[1]

    image = cv2.imread(image_file_name)
    image_process = ImageProcess()

    # show the image
    fig, (ax1, ax2) = plt.subplots(1, 2)

    ax1.imshow(image)
    ax1.set(title='original image')

    # 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 = image_process.process(image)

    drive_run = DriveRun(model_path)
    if Config.neural_net['num_inputs'] == 2:
        predict = drive_run.run((image, velocity))
        steering_angle = predict[0][0]
        throttle = predict[0][1]
        fig.suptitle('pred-steering:{} pred-throttle:{}'.format(
            steering_angle, throttle))
    else:
        predict = drive_run.run((image, ))
        steering_angle = predict[0][0]
        fig.suptitle('pred_steering:{}'.format(steering_angle))

    ax2.imshow(image)
    ax2.set(title='resize and processed')

    plt.show()
Exemplo n.º 10
0
def taker(data):
    joy_pub = rospy.Publisher('/joy', Joy, queue_size=10)
    joy_data = Joy()
    img = ic.imgmsg_to_opencv(data)
    config = Config()
    image_process = ImageProcess()
    IImage = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    IImage = cv2.resize(IImage, (config.image_size[0], config.image_size[1]))
    IImage = image_process.process(IImage)
    #cv2.imwrite('balu.jpg', IImage)
    #drive_run = DriveRun(sys.agrv[1])
    drive_run = DriveRun(
        '/home/mir-lab/Desktop/Balu_Autodrive/2018-06-14-15-16-03')
    prediction = drive_run.run(IImage)
    #print(prediction)
    #print(np.shape(prediction))
    #print(type(prediction))
    if (prediction[0][0] > 0.3):
        #print("1")
        prediction[0][0] = 1
    elif (prediction[0][0] < -0.3):
        #print("2")
        prediction[0][0] = -1
    else:
        #print("3")
        prediction[0][0] = 0
    #new_pred = prediction.astype(np.float)
    #new_predd = np.asscalar(np.array([prediction]))
    #print(type(new_predd))
    #print(np.shape(new_predd))
    #new_predd = joy_data.axes.append(0)
    #0.25 = joy_data.axes.append(3)
    #print(new_predd)
    #print(prediction[0][0])
    joy_data.axes = [prediction[0][0], 0, 0, 0.05, 0, 0]
    joy_pub.publish(joy_data)
Exemplo n.º 11
0
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)
Exemplo n.º 12
0
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()
Exemplo n.º 13
0
class DriveTrain:
    
    ###########################################################################
    # data_path = 'path_to_drive_data'  e.g. ../data/2017-09-22-10-12-34-56/'
    def __init__(self, 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  # use it for csv file name 
        
        self.csv_path = csv_path
        self.train_generator = None
        self.valid_generator = None
        self.train_hist = None
        self.drive = None
        
        #self.config = Config() #model_name)
        
        self.data_path = data_path
        #self.model_name = model_name
        
        self.drive = DriveData(self.csv_path)
        self.net_model = NetModel(data_path)
        self.image_process = ImageProcess()
        self.data_aug = DataAugmentation()
        
        
    ###########################################################################
    #
    def _prepare_data(self):
    
        self.drive.read()
        
        from sklearn.model_selection import train_test_split
        
        samples = list(zip(self.drive.image_names, self.drive.measurements))
        self.train_data, self.valid_data = train_test_split(samples, 
                                   test_size=Config.config['validation_rate'])
        
        self.num_train_samples = len(self.train_data)
        self.num_valid_samples = len(self.valid_data)
        
        print('Train samples: ', self.num_train_samples)
        print('Valid samples: ', self.num_valid_samples)
    
                                          
    ###########################################################################
    #
    def _build_model(self, show_summary=True):

        def _generator(samples, batch_size=Config.config['batch_size']):
            num_samples = len(samples)
            while True: # Loop forever so the generator never terminates
               
                if Config.config['lstm'] is False:
                    samples = sklearn.utils.shuffle(samples)

                for offset in 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
                        
                        if abs(steering_angle) < Config.config['steering_angle_jitter_tolerance']:
                            steering_angle = 0
                        
                        measurements.append(steering_angle*Config.config['steering_angle_scale'])
                        
                        if Config.config['data_aug_flip'] is True:    
                            # Flipping the image
                            flip_image, flip_steering = self.data_aug.flipping(image, steering_angle)
                            images.append(flip_image)
                            measurements.append(flip_steering*Config.config['steering_angle_scale'])
    
                        if Config.config['data_aug_bright'] is True:    
                            # Changing the brightness of image
                            if steering_angle > Config.config['steering_angle_jitter_tolerance'] or \
                                steering_angle < -Config.config['steering_angle_jitter_tolerance']:
                                bright_image = self.data_aug.brightness(image)
                                images.append(bright_image)
                                measurements.append(steering_angle*Config.config['steering_angle_scale'])
    
                        if Config.config['data_aug_shift'] is True:    
                            # Shifting the image
                            shift_image, shift_steering = self.data_aug.shift(image, steering_angle)
                            images.append(shift_image)
                            measurements.append(shift_steering*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.train_generator = _generator(self.train_data)
        self.valid_generator = _generator(self.valid_data)
        
        if (show_summary):
            self.net_model.model.summary()
    
    ###########################################################################
    #
    def _start_training(self):
        
        if (self.train_generator == None):
            raise NameError('Generators are not ready.')
        
        ######################################################################
        # callbacks
        from keras.callbacks import ModelCheckpoint, EarlyStopping
        
        # checkpoint
        callbacks = []
        #weight_filename = self.net_model.name + '_' + const.CONFIG_YAML + '_ckpt'
        weight_filename = self.data_path + '_' + const.CONFIG_YAML + '_ckpt'
        checkpoint = ModelCheckpoint(weight_filename+'.h5',
                                     monitor='val_loss', 
                                     verbose=1, save_best_only=True, mode='min')
        callbacks.append(checkpoint)
        
        # early stopping
        earlystop = EarlyStopping(monitor='val_loss', min_delta=0, patience=3, 
                                  verbose=1, mode='min')
        callbacks.append(earlystop)
        
        self.train_hist = self.net_model.model.fit_generator(
                self.train_generator, 
                steps_per_epoch=self.num_train_samples//Config.config['batch_size'], 
                epochs=Config.config['num_epochs'], 
                validation_data=self.valid_generator,
                validation_steps=self.num_valid_samples//Config.config['batch_size'],
                verbose=1, callbacks=callbacks)
    

    ###########################################################################
    #
    def _plot_training_history(self):
    
        print(self.train_hist.history.keys())
        
        ### plot the training and validation loss for each epoch
        plt.plot(self.train_hist.history['loss'])
        plt.plot(self.train_hist.history['val_loss'])
        plt.ylabel('mse loss')
        plt.xlabel('epoch')
        plt.legend(['training set', 'validatation set'], loc='upper right')
        plt.show()
        
        
    ###########################################################################
    #
    def train(self, show_summary=True):
        
        self._prepare_data()
        self._build_model(show_summary)
        self._start_training()
        self.net_model.save()
        self._plot_training_history()
        Config.summary()
Exemplo n.º 14
0
def main(model_path, image_file_path):
    image_process = ImageProcess()

    image = cv2.imread(image_file_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.neural_net['input_image_width'],
                               Config.neural_net['input_image_height']))
    image = image_process.process(image)

    drive_run = DriveRun(model_path)
    measurement = drive_run.run((image, ))
    """ grad modifier doesn't work somehow
    fig, axs = plt.subplots(1, 3)
    fig.suptitle('Saliency Visualization' + str(measurement))
    titles = ['left steering', 'right steering', 'maintain steering']
    modifiers = [None, 'negate', 'small_values']

    for i, modifier in enumerate(modifiers):
        layer_idx = utils.find_layer_idx(drive_run.net_model.model, 'conv2d_last')
        heatmap = visualize_cam(drive_run.net_model.model, layer_idx, 
                    filter_indices=None, seed_input=image, backprop_modifier='guided', 
                    grad_modifier=modifier)

        axs[i].set(title = titles[i])
        axs[i].imshow(image)
        axs[i].imshow(heatmap, cmap='jet', alpha=0.3)
    """
    plt.figure()
    #plt.title('Saliency Visualization' + str(measurement))
    plt.title('Steering Angle Prediction: ' + str(measurement[0][0]))
    layer_idx = utils.find_layer_idx(drive_run.net_model.model, 'conv2d_last')
    heatmap = visualize_cam(drive_run.net_model.model,
                            layer_idx,
                            filter_indices=None,
                            seed_input=image,
                            backprop_modifier='guided')

    plt.imshow(image)
    plt.imshow(heatmap, cmap='jet', alpha=0.5)

    # file name
    loc_slash = image_file_path.rfind('/')
    if loc_slash != -1:  # there is '/' in the data path
        image_file_name = image_file_path[loc_slash + 1:]

    saliency_file_path = model_path + '_' + image_file_name + '_saliency.png'
    saliency_file_path_pdf = model_path + '_' + image_file_name + '_saliency.pdf'

    plt.tight_layout()
    # save fig
    plt.savefig(saliency_file_path, dpi=150)
    plt.savefig(saliency_file_path_pdf, dpi=150)

    print('Saved ' + saliency_file_path + ' & .pdf')
Exemplo n.º 15
0
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()
Exemplo n.º 16
0
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()
Exemplo n.º 17
0
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.')
Exemplo n.º 18
0
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()
Exemplo n.º 19
0
class DriveTrain:

    ###########################################################################
    # data_path = 'path_to_drive_data'  e.g. ../data/2017-09-22-10-12-34-56/'
    def __init__(self, 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  # use it for csv file name

        self.csv_path = csv_path
        self.train_generator = None
        self.valid_generator = None
        self.train_hist = None
        self.data = None

        #self.config = Config() #model_name)

        self.data_path = data_path
        #self.model_name = model_name

        self.model_name = data_path + '_' + Config.neural_net_yaml_name \
            + '_N' + str(config['network_type'])
        self.model_ckpt_name = self.model_name + '_ckpt'

        self.data = DriveData(self.csv_path)
        self.net_model = NetModel(data_path)
        self.image_process = ImageProcess()
        self.data_aug = DataAugmentation()

    ###########################################################################
    #
    def _prepare_data(self):

        self.data.read()

        # put velocities regardless we use them or not for simplicity.
        samples = list(
            zip(self.data.image_names, self.data.velocities,
                self.data.measurements))

        if config['lstm'] is True:
            self.train_data, self.valid_data = self._prepare_lstm_data(samples)
        else:
            self.train_data, self.valid_data = train_test_split(
                samples, test_size=config['validation_rate'])

        self.num_train_samples = len(self.train_data)
        self.num_valid_samples = len(self.valid_data)

        print('Train samples: ', self.num_train_samples)
        print('Valid samples: ', self.num_valid_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, velocity, measurement in sub_samples:
                sub_image_names.append(image_name)
                sub_velocities.append(velocity)
                sub_measurements.append(measurement)

            image_names.append(sub_image_names)
            velocities.append(sub_velocities)
            measurements.append(sub_measurements)

        samples = list(zip(image_names, velocities, measurements))
        return train_test_split(samples,
                                test_size=config['validation_rate'],
                                shuffle=False)

    ###########################################################################
    #
    def _build_model(self, show_summary=True):
        def _data_augmentation(image, steering_angle):
            if config['data_aug_flip'] is True:
                # Flipping the image
                return True, self.data_aug.flipping(image, steering_angle)

            if config['data_aug_bright'] is True:
                # Changing the brightness of image
                if steering_angle > config['steering_angle_jitter_tolerance'] or \
                    steering_angle < -config['steering_angle_jitter_tolerance']:
                    image = self.data_aug.brightness(image)
                return True, image, steering_angle

            if config['data_aug_shift'] is True:
                # Shifting the image
                return True, self.data_aug.shift(image, steering_angle)

            return False, image, steering_angle

        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)

                # 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.append(image)
                    velocities.append(velocity)

                    if config['num_outputs'] == 2:
                        measurements.append(
                            (steering_angle * config['steering_angle_scale'],
                             throttle))
                    else:
                        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_timestep.append(
                                (steering_angle *
                                 config['steering_angle_scale'], throttle))
                        else:
                            measurements_timestep.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

                if config['lstm'] is True:
                    for offset in range(0, (num_samples // batch_size) *
                                        batch_size, batch_size):
                        batch_samples = samples[offset:offset + batch_size]

                        images, velocities, measurements = _prepare_lstm_batch_samples(
                            batch_samples)

                        X_train = np.array(images)
                        y_train = np.array(measurements)

                        if config['num_inputs'] == 2:
                            X_train_vel = np.array(velocities).reshape(
                                -1, config['lstm_timestep'], 1)
                            X_train = [X_train, X_train_vel]
                        if config['num_outputs'] == 2:
                            y_train = np.stack(measurements).reshape(
                                -1, config['num_outputs'])

                        yield X_train, y_train

                else:
                    samples = sklearn.utils.shuffle(samples)

                    for offset in 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]

                        yield X_train, y_train

        self.train_generator = _generator(self.train_data)
        self.valid_generator = _generator(self.valid_data)

        if (show_summary):
            self.net_model.model.summary()

    ###########################################################################
    #
    def _start_training(self):

        if (self.train_generator == None):
            raise NameError('Generators are not ready.')

        ######################################################################
        # callbacks
        from keras.callbacks import ModelCheckpoint, EarlyStopping, TensorBoard

        # checkpoint
        callbacks = []
        #weight_filename = self.data_path + '_' + Config.config_yaml_name \
        #    + '_N' + str(config['network_type']) + '_ckpt'
        checkpoint = ModelCheckpoint(self.model_ckpt_name +
                                     '.{epoch:02d}-{val_loss:.2f}.h5',
                                     monitor='val_loss',
                                     verbose=1,
                                     save_best_only=True,
                                     mode='min')
        callbacks.append(checkpoint)

        # early stopping
        patience = config['early_stopping_patience']
        earlystop = EarlyStopping(monitor='val_loss',
                                  min_delta=0,
                                  patience=patience,
                                  verbose=1,
                                  mode='min')
        callbacks.append(earlystop)

        # tensor board
        logdir = config['tensorboard_log_dir'] + datetime.now().strftime(
            "%Y%m%d-%H%M%S")
        tensorboard = TensorBoard(log_dir=logdir)
        callbacks.append(tensorboard)

        self.train_hist = self.net_model.model.fit_generator(
            self.train_generator,
            steps_per_epoch=self.num_train_samples // config['batch_size'],
            epochs=config['num_epochs'],
            validation_data=self.valid_generator,
            validation_steps=self.num_valid_samples // config['batch_size'],
            verbose=1,
            callbacks=callbacks,
            use_multiprocessing=True)

    ###########################################################################
    #
    def _plot_training_history(self):

        print(self.train_hist.history.keys())

        plt.figure()  # new figure window
        ### plot the training and validation loss for each epoch
        plt.plot(self.train_hist.history['loss'][1:])
        plt.plot(self.train_hist.history['val_loss'][1:])
        #plt.title('Mean Squared Error Loss')
        plt.ylabel('mse loss')
        plt.xlabel('epoch')
        plt.legend(['training set', 'validatation set'], loc='upper right')
        plt.tight_layout()
        #plt.show()
        plt.savefig(self.model_name + '_model.png', dpi=150)
        plt.savefig(self.model_name + '_model.pdf', dpi=150)

    ###########################################################################
    #
    def train(self, show_summary=True):

        self._prepare_data()
        self._build_model(show_summary)
        self._start_training()
        self.net_model.save(self.model_name)
        self._plot_training_history()
        Config.summary()
Exemplo n.º 20
0
import cv2
import pandas as pd
import sys
import numpy as np
from progressbar import ProgressBar

from data_augmentation import DataAugmentation
from image_process import ImageProcess

image_process = ImageProcess()
data_aug = DataAugmentation()

csv_fname = '/home/mir-lab/Ninad_Thesis/Test/Test.csv'
csv_header = ['image_fname', 'steering_angle']
df = pd.read_csv(csv_fname, names=csv_header, index_col=False)
num_data = len(df)
text = open('/home/mir-lab/Ninad_Thesis/Test/Shift/Shift.txt', 'w+')
bar = ProgressBar()
for i in bar(range(num_data)):
    image_name = df.loc[i]['image_fname']
    steering = df.loc[i]['steering_angle']
    image_path = '/home/mir-lab/Ninad_Thesis/Test/' + image_name + '.jpg'
    image = cv2.imread(image_path)
    image = cv2.resize(image, (160, 70))
    image = image_process.process(image)
    shift_image, shift_steering = data_aug.shift(image, steering)
    cv2.imwrite(
        '/home/mir-lab/Ninad_Thesis/Test/Shift/S_' + image_name + '.jpg',
        shift_image)
    text.write('S_' + str(image_name) + '\t' + str(shift_steering) + '\r' '\n')
Exemplo n.º 21
0
class DriveTrain:

    ###########################################################################
    # data_path = 'path_to_drive_data'  e.g. ../data/2017-09-22-10-12-34-56'
    def __init__(self, data_path):

        model_name = data_path[data_path.rfind('/'):]  # get folder name
        model_name = model_name.strip('/')
        csv_path = data_path + '/' + model_name + '.csv'  # use it for csv file name

        self.csv_path = csv_path
        self.train_generator = None
        self.valid_generator = None
        self.train_hist = None
        self.drive = None

        self.config = Config()  #model_name)

        self.data_path = data_path
        #self.model_name = model_name

        self.drive = DriveData(self.csv_path)
        self.net_model = NetModel(data_path)
        self.image_process = ImageProcess()
        self.data_aug = DataAugmentation()

    ###########################################################################
    #
    def _prepare_data(self):

        self.drive.read()

        from sklearn.model_selection import train_test_split

        samples = list(zip(self.drive.image_names, self.drive.measurements))
        self.train_data, self.valid_data = train_test_split(
            samples, test_size=self.config.valid_rate)

        self.num_train_samples = len(self.train_data)
        self.num_valid_samples = len(self.valid_data)

        print('Train samples: ', self.num_train_samples)
        print('Valid samples: ', self.num_valid_samples)

    ###########################################################################
    #
    def _build_model(self, show_summary=True):
        def _generator(samples, batch_size=self.config.batch_size):
            num_samples = len(samples)
            while True:  # Loop forever so the generator never terminates
                samples = sklearn.utils.shuffle(samples)

                for offset in 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

                        #if abs(steering_angle) < self.config.jitter_tolerance:
                        #    steering_angle = 0

                        measurements.append(steering_angle)
                        #measurements.append(steering_angle*self.config.raw_scale)

                        ###-----------------------Flipping the image-----------------------###
                        flip_image, flip_steering = self.data_aug.flipping(
                            image, steering_angle)
                        images.append(flip_image)
                        measurements.append(flip_steering)
                        '''
                        # add the flipped image of the original
                        images.append(cv2.flip(image,1))
                        #measurement = (steering_angle*-1.0, measurement[1]) 
                        measurements.append(steering_angle*-1.0)
                        #measurements.append(steering_angle*self.config.raw_scale*-1.0)
                        '''
                        ###----------------Changing the brightness of image----------------###
                        if steering_angle > 0.01 or steering_angle < -0.015:
                            bright_image = self.data_aug.brightness(image)
                            images.append(bright_image)
                            measurements.append(steering_angle)

                        ###-----------------------Shifting the image-----------------------###
                        shift_image, shift_steering = self.data_aug.shift(
                            image, steering_angle)
                        images.append(shift_image)
                        measurements.append(shift_steering)

                    X_train = np.array(images)
                    y_train = np.array(measurements)

                    if self.config.typeofModel == 4 or self.config.typeofModel == 5:
                        X_train = np.array(images).reshape(
                            -1, 1, self.config.image_size[1],
                            self.config.image_size[0],
                            self.config.image_size[2])
                        y_train = np.array(measurements).reshape(-1, 1, 1)

                    yield sklearn.utils.shuffle(X_train, y_train)

        self.train_generator = _generator(self.train_data)
        self.valid_generator = _generator(self.valid_data)

        if (show_summary):
            self.net_model.model.summary()

    ###########################################################################
    #
    def _start_training(self):

        if (self.train_generator == None):
            raise NameError('Generators are not ready.')

        ######################################################################
        # callbacks
        from keras.callbacks import ModelCheckpoint, EarlyStopping

        # checkpoint
        callbacks = []
        checkpoint = ModelCheckpoint(self.net_model.name + '.h5',
                                     monitor='val_loss',
                                     verbose=1,
                                     save_best_only=True,
                                     mode='min')
        callbacks.append(checkpoint)

        # early stopping
        earlystop = EarlyStopping(monitor='val_loss',
                                  min_delta=0,
                                  patience=0,
                                  verbose=1,
                                  mode='min')
        callbacks.append(earlystop)

        self.train_hist = self.net_model.model.fit_generator(
            self.train_generator,
            steps_per_epoch=self.num_train_samples // self.config.batch_size,
            epochs=self.config.num_epochs,
            validation_data=self.valid_generator,
            validation_steps=self.num_valid_samples // self.config.batch_size,
            verbose=1,
            callbacks=callbacks)

    ###########################################################################
    #
    def _plot_training_history(self):

        print(self.train_hist.history.keys())

        ### plot the training and validation loss for each epoch
        plt.plot(self.train_hist.history['loss'])
        plt.plot(self.train_hist.history['val_loss'])
        plt.ylabel('mse loss')
        plt.xlabel('epoch')
        plt.legend(['training set', 'validatation set'], loc='upper right')
        plt.show()

    ###########################################################################
    #
    def train(self, show_summary=True):

        self._prepare_data()
        self._build_model(show_summary)
        self._start_training()
        self.net_model.save()
        self._plot_training_history()