Example #1
0
    def __init__(self):
        rospy.init_node('tl_detector')

        self.pose = None
        self.waypoints = None
        self.camera_image = None
        self.lights = []

        self.waypoints_2d = None
        self.waypoint_tree = None

        sub1 = rospy.Subscriber('/current_pose', PoseStamped, self.pose_cb)
        sub2 = rospy.Subscriber('/base_waypoints', Lane, self.waypoints_cb)

        '''
        /vehicle/traffic_lights provides you with the location of the traffic light in 3D map space and
        helps you acquire an accurate ground truth data source for the traffic light
        classifier by sending the current color state of all traffic lights in the
        simulator. When testing on the vehicle, the color state will not be available. You'll need to
        rely on the position of the light and the camera image to predict it.
        '''
        sub3 = rospy.Subscriber('/vehicle/traffic_lights', TrafficLightArray, self.traffic_cb)
        sub6 = rospy.Subscriber('/image_color', Image, self.image_cb)

        config_string = rospy.get_param("/traffic_light_config")
        self.config = yaml.load(config_string)

        self.is_site = self.config['is_site']

        #TODO Remove hack to force site mode or ground_truth for testing
        # self.is_site = True
        self.ground_truth = False


        self.upcoming_red_light_pub = rospy.Publisher('/traffic_waypoint', Int32, queue_size=1)

        self.bridge = CvBridge()
        self.light_classifier = TLClassifier()
        self.listener = tf.TransformListener()

        self.state = TrafficLight.RED
        self.last_state = TrafficLight.RED
        self.last_wp = -1
        self.state_count = 0

        self.waypoints_2d = None
        self.waypoint_tree = None
        self.vgg_model = None
        self.graph = None
        self.sess = None
        self.initialized = False

        if self.is_site:
            # Detector Stuff
            self.model_image_size = None
            model_path = os.path.expanduser('./weights/parking_lot.h5')
            anchors_path = os.path.expanduser('./model_data/lisa_anchors.txt')
            classes_path = os.path.expanduser('./model_data/lisa_classes.txt')
            
            self.class_names  = utils.get_classes(classes_path)
 
            anchors = utils.get_anchors(anchors_path)
            if SHALLOW_DETECTOR:
                anchors = anchors * 2
                        
            self.yolo_model, _ = create_model(anchors, self.class_names, load_pretrained=True, 
            feature_extractor=FEATURE_EXTRACTOR, pretrained_path=model_path, freeze_body=True)

            # Check if model is fully convolutional, assuming channel last order.
            self.model_image_size = self.yolo_model.layers[0].input_shape[1:3]

            self.sess = K.get_session()  

            # Generate output tensor targets for filtered bounding boxes.
            self.yolo_outputs = decode_yolo_output(self.yolo_model.output, anchors, len(self.class_names))

            self.input_image_shape = K.placeholder(shape=(2, ))
            self.boxes, self.scores, self.classes = yolo_eval(
                self.yolo_outputs,
                self.input_image_shape,
                score_threshold=.6,
                iou_threshold=.6)

            self.graph = tensorflow.get_default_graph()
        else:
            try:
                model_path = os.path.expanduser('./weights/vgg16_1.h5')
                self.vgg_model = load_model(model_path)
                self.graph = tensorflow.get_default_graph()
            except:
                rospy.logerr(
                    "Could not load model. Have you downloaded the vgg16_1.h5 file to the weights folder? You can download it here: https://s3-eu-west-1.amazonaws.com/sdcnddata/vgg16_1.h5")


        self.initialized = True


        rospy.spin()
Example #2
0
    def __init__(self):

        config_string = rospy.get_param("/traffic_light_config")
        self.config = yaml.load(config_string)

        self.is_site = self.config['is_site']

        if self.is_site:
            # Detector Stuff
            self.model_image_size = None
            self.sess = None
            self.initialized = False
            model_path = os.path.expanduser(
                './weights/mobilenet_s2_best.FalseFalse.h5')
            anchors_path = os.path.expanduser('./model_data/lisa_anchors.txt')
            classes_path = os.path.expanduser('./model_data/lisa_classes.txt')

            class_names = utils.get_classes(classes_path)
            anchors = utils.get_anchors(anchors_path)
            if SHALLOW_DETECTOR:
                anchors = anchors * 2

            print(class_names)
            print(anchors)

            self.yolo_model, yolo_model_for_training = create_model(
                anchors,
                class_names,
                load_pretrained=True,
                feature_extractor=FEATURE_EXTRACTOR,
                pretrained_path=model_path,
                freeze_body=True)

            model_file_basename, file_extension = os.path.splitext(
                os.path.basename(model_path))

            model_input = self.yolo_model.input.name.replace(':0',
                                                             '')  # input_1
            model_output = self.yolo_model.output.name.replace(
                ':0', '')  # conv2d_3/BiasAdd

            sess = K.get_session()
            width, height, channels = int(self.yolo_model.input.shape[2]), int(
                self.yolo_model.input.shape[1]), int(
                    self.yolo_model.input.shape[3])

            # END OF keras specific code

            # Check if model is fully convolutional, assuming channel last order.
            self.model_image_size = self.yolo_model.layers[0].input_shape[1:3]

            self.sess = K.get_session(
            )  # TODO: Remove dependence on Tensorflow session.

            # Generate output tensor targets for filtered bounding boxes.
            self.yolo_outputs = decode_yolo_output(self.yolo_model.output,
                                                   anchors, len(class_names))

            self.input_image_shape = K.placeholder(shape=(2, ))
            self.boxes, self.scores, self.classes = yolo_eval(
                self.yolo_outputs,
                self.input_image_shape,
                score_threshold=.6,
                iou_threshold=.6)

            self.initialized = True

        else:
            cwd = os.path.dirname(os.path.realpath(__file__))
            os.chdir(cwd)
            try:
                self.model = load_model('vgg16_1.h5')
                # this is key : save the graph after loading the model
                self.graph = tf.get_default_graph()
            except:
                rospy.logerr(
                    "Could not load model. Have you downloaded the vgg16_1.h5 file to the light_classification folder? You can download it here: hhtps://s3-eu-west-1.amazonaws.com/sdcnddata/vgg_16_1.h5"
                )
Example #3
0
def _main(args):
    voc_path = os.path.expanduser(args.data_path)
    classes_path = os.path.expanduser(args.classes_path)
    anchors_path = os.path.expanduser(args.anchors_path)
    class_names = get_classes(classes_path)
    anchors = get_anchors(anchors_path)
    num_epoches = args.num_epoches

    if SHRINK_FACTOR == 16:
        anchors = anchors * 2

    print('Prior anchor boxes:')
    print(anchors)
    print('Prior classes:')
    print(class_names)

    num_anchors = len(anchors)
    voc = h5py.File(voc_path, 'r')
    print(voc['train/images'].shape)
    # import pdb; pdb.set_trace()

    test_id = 1
    image = PIL.Image.open(io.BytesIO(voc['train/images'][test_id]))
    # import pdb; pdb.set_trace()
    orig_size = np.array(image.size)
    orig_size = np.expand_dims(orig_size, axis=0)

    net_width = IMAGE_W
    net_height = IMAGE_H
    feats_width = FEAT_W
    feats_height = FEAT_H

    # Image preprocessing.
    image = image.resize((net_width, net_height), PIL.Image.BICUBIC)
    image_data = np.array(image, dtype=np.float)
    image_data /= 255.

    # Box preprocessing.
    # Original boxes stored as 1D list of class, x_min, y_min, x_max, y_max.
    boxes = voc['train/boxes'][test_id]
    boxes = boxes.reshape((-1, 5))
    # Get extents as y_min, x_min, y_max, x_max, class for comparision with
    # model output.
    boxes_extents = boxes[:, [2, 1, 4, 3, 0]]

    # Get box parameters as x_center, y_center, box_width, box_height, class.
    boxes_xy = 0.5 * (boxes[:, 3:5] + boxes[:, 1:3])
    boxes_wh = boxes[:, 3:5] - boxes[:, 1:3]
    boxes_xy = boxes_xy / orig_size
    boxes_wh = boxes_wh / orig_size
    boxes = np.concatenate((boxes_xy, boxes_wh, boxes[:, 0:1]), axis=1)

    # Precompute detectors_mask and matching_true_boxes for training.
    # Detectors mask is 1 for each spatial position in the final conv layer and
    # anchor that should be active for the given boxes and 0 otherwise.
    # Matching true boxes gives the regression targets for the ground truth box
    # that caused a detector to be active or 0 otherwise.
    detectors_mask_shape = (feats_height, feats_width, num_anchors, 1)
    matching_boxes_shape = (feats_height, feats_width, num_anchors, 5)
    detectors_mask, matching_true_boxes = preprocess_true_boxes(
        boxes, anchors, [net_height, net_width], [feats_height, feats_width])

    # Create model input layers.
    image_input = Input(shape=(net_height, net_width, 3))
    boxes_input = Input(shape=(None, 5))

    detectors_mask_input = Input(shape=detectors_mask_shape)
    matching_boxes_input = Input(shape=matching_boxes_shape)

    print('Boxes:')
    print(boxes)
    print('Box corners:')
    print(boxes_extents)
    print('Active detectors:')
    print(np.where(detectors_mask == 1)[:-1])
    print('Matching boxes for active detectors:')
    print(matching_true_boxes[np.where(detectors_mask == 1)[:-1]])

    yolo_model = yolo_body_mobilenet(
        image_input,
        len(anchors),
        len(class_names),
        network_config=[SHALLOW_DETECTOR, USE_X0_FEATURE])

    # yolo_model, model = create_model(anchors, class_names, feature_extractor=FEATURE_EXTRACTOR)

    yolo_model.summary()

    # TODO: Replace Lambda with custom Keras layer for loss.
    model_loss = Lambda(yolo_loss,
                        output_shape=(1, ),
                        name='yolo_loss',
                        arguments={
                            'anchors': anchors,
                            'num_classes': len(class_names)
                        })([
                            yolo_model.output, boxes_input,
                            detectors_mask_input, matching_boxes_input
                        ])

    model = Model(
        [image_input, boxes_input, detectors_mask_input, matching_boxes_input],
        model_loss)

    model.compile(
        optimizer='adam', loss={
            'yolo_loss': lambda y_true, y_pred: y_pred
        })  # This is a hack to use the custom loss function in the last layer.

    # Add batch dimension for training.
    image_data = np.expand_dims(image_data, axis=0)
    boxes = np.expand_dims(boxes, axis=0)
    detectors_mask = np.expand_dims(detectors_mask, axis=0)
    matching_true_boxes = np.expand_dims(matching_true_boxes, axis=0)

    model.fit([image_data, boxes, detectors_mask, matching_true_boxes],
              np.zeros(len(image_data)),
              batch_size=1,
              verbose=1,
              epochs=num_epoches)

    model.save_weights('model_data/overfit.h5')

    # Create output variables for prediction.
    yolo_outputs = decode_yolo_output(yolo_model.output, anchors,
                                      len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       input_image_shape,
                                       score_threshold=.8,
                                       iou_threshold=.9)

    # Run prediction on overfit image.
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.
    out_boxes, out_scores, out_classes = sess.run(
        [boxes, scores, classes],
        feed_dict={
            yolo_model.input: image_data,
            input_image_shape: [image.size[1], image.size[0]],
            K.learning_phase(): 0
        })
    print('Found {} boxes for image.'.format(len(out_boxes)))
    print(out_boxes)

    # Plot image with predicted boxes.
    image_with_boxes = draw_boxes(image_data[0], out_boxes, out_classes,
                                  class_names, out_scores)

    plt.imshow(image_with_boxes, interpolation='nearest')
    plt.show()
Example #4
0
    def __init__(self):
        rospy.init_node('tl_detector')

        self.pose = None
        self.waypoints = None
        self.camera_image = None
        self.lights = []

        sub1 = rospy.Subscriber('/current_pose', PoseStamped, self.pose_cb)
        sub2 = rospy.Subscriber('/base_waypoints', Lane, self.waypoints_cb)
        '''
        /vehicle/traffic_lights provides you with the location of the traffic light in 3D map space and
        helps you acquire an accurate ground truth data source for the traffic light
        classifier by sending the current color state of all traffic lights in the
        simulator. When testing on the vehicle, the color state will not be available. You'll need to
        rely on the position of the light and the camera image to predict it.
        '''
        sub3 = rospy.Subscriber('/vehicle/traffic_lights', TrafficLightArray,
                                self.traffic_cb)
        sub6 = rospy.Subscriber('/image_color', Image, self.image_cb)

        config_string = rospy.get_param("/traffic_light_config")
        self.config = yaml.load(config_string)

        self.is_site = self.config['is_site']

        self.upcoming_red_light_pub = rospy.Publisher('/traffic_waypoint',
                                                      Int32,
                                                      queue_size=1)

        self.bridge = CvBridge()
        self.light_classifier = TLClassifier()
        self.listener = tf.TransformListener()

        self.state = TrafficLight.UNKNOWN
        self.last_state = TrafficLight.UNKNOWN
        self.last_wp = -1
        self.state_count = 0
        self.waypoints_2d = None
        self.waypoint_tree = None
        self.waypoints = None

        self.ground_truth = False

        #Detector Stuff
        self.model_image_size = None
        self.sess = None
        self.initialized = False
        model_path = os.path.expanduser(
            './weights/mobilenet_s2_best.FalseFalse.h5')
        anchors_path = os.path.expanduser('./model_data/lisa_anchors.txt')
        classes_path = os.path.expanduser('./model_data/lisa_classes.txt')

        class_names = utils.get_classes(classes_path)
        anchors = utils.get_anchors(anchors_path)
        if SHALLOW_DETECTOR:
            anchors = anchors * 2

        print(class_names)
        print(anchors)

        self.yolo_model, yolo_model_for_training = create_model(
            anchors,
            class_names,
            load_pretrained=True,
            feature_extractor=FEATURE_EXTRACTOR,
            pretrained_path=model_path,
            freeze_body=True)

        model_file_basename, file_extension = os.path.splitext(
            os.path.basename(model_path))

        model_input = self.yolo_model.input.name.replace(':0', '')  # input_1
        model_output = self.yolo_model.output.name.replace(
            ':0', '')  # conv2d_3/BiasAdd

        sess = K.get_session()
        width, height, channels = int(self.yolo_model.input.shape[2]), int(
            self.yolo_model.input.shape[1]), int(
                self.yolo_model.input.shape[3])

        # END OF keras specific code

        # Check if model is fully convolutional, assuming channel last order.
        self.model_image_size = self.yolo_model.layers[0].input_shape[1:3]

        self.sess = K.get_session(
        )  # TODO: Remove dependence on Tensorflow session.

        # Generate output tensor targets for filtered bounding boxes.
        self.yolo_outputs = decode_yolo_output(self.yolo_model.output, anchors,
                                               len(class_names))

        self.input_image_shape = K.placeholder(shape=(2, ))
        self.boxes, self.scores, self.classes = yolo_eval(
            self.yolo_outputs,
            self.input_image_shape,
            score_threshold=.6,
            iou_threshold=.6)

        self.initialized = True

        rospy.spin()