def __init__(self, graph, labels):
        num_classes = 14

        label_map = label_map_util.load_labelmap(labels)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=num_classes, use_display_name=True)
        self.category_index = label_map_util.create_category_index(categories)

        # Load Tensorflow model into memory
        self.detection_graph = tf.Graph()
        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(graph, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            self.sess = tf.Session(graph=self.detection_graph)
            self.image_tensor = self.detection_graph.get_tensor_by_name(
                'image_tensor:0')
            self.detection_boxes = self.detection_graph.get_tensor_by_name(
                'detection_boxes:0')
            self.detection_scores = self.detection_graph.get_tensor_by_name(
                'detection_scores:0')
            self.detection_classes = self.detection_graph.get_tensor_by_name(
                'detection_classes:0')
            self.num_detections = self.detection_graph.get_tensor_by_name(
                'num_detections:0')
Пример #2
0
    def __init__(self, draw_box=False):
        self.draw_box = draw_box
        self.score_thresh = 0.5
        self.skip_cnt = 0
        self.max_cnt = 4
        self.last_light = TrafficLight.UNKNOWN

        curr_dir = os.path.dirname(os.path.realpath(__file__))
        model = curr_dir + '/model/frozen_inference_graph.pb'
        labels_file = curr_dir + '/model/label_map.pbtxt'

        label_map = label_map_util.load_labelmap(labels_file)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=4, use_display_name=True)
        self.category_index = label_map_util.create_category_index(categories)

        self.graph = tf.Graph()

        with self.graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(model, 'rb') as fid:
                od_graph_def.ParseFromString(fid.read())
                tf.import_graph_def(od_graph_def, name='')

        self.sess = tf.Session(graph=self.graph)
        self.image_tensor = self.graph.get_tensor_by_name('image_tensor:0')
        self.boxes = self.graph.get_tensor_by_name('detection_boxes:0')
        self.scores = self.graph.get_tensor_by_name('detection_scores:0')
        self.classes = self.graph.get_tensor_by_name('detection_classes:0')
        self.num_detections = self.graph.get_tensor_by_name('num_detections:0')

        print("Loaded frozen model graph")
Пример #3
0
    def __init__(self):
        #TODO load classifier
        self.current_light = TrafficLight.UNKNOWN

        cwd = os.path.dirname(
            os.path.realpath(__file__))  # Current working directory
        self.simulation = True

        if self.simulation is True:
            CKPT = cwd + '/graphs/simulator_graph.pb'
        else:
            CKPT = cwd + '/graphs/test_site_graph.pb'

        PATH_TO_LABELS = cwd + '/graphs/label_map.pbtxt'
        NUM_CLASSES = 4

        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        self.category_index = label_map_util.create_category_index(categories)

        ##### Build network
        self.image_np_deep = None
        self.detection_graph = tf.Graph()

        # https://github.com/tensorflow/tensorflow/issues/6698
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        # end

        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()

            with tf.gfile.GFile(CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            self.sess = tf.Session(graph=self.detection_graph, config=config)

        # Definite input and output Tensors for detection_graph
        self.image_tensor = self.detection_graph.get_tensor_by_name(
            'image_tensor:0')

        # Each box represents a part of the image where a particular object was detected.
        self.detection_boxes = self.detection_graph.get_tensor_by_name(
            'detection_boxes:0')

        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        self.detection_scores = self.detection_graph.get_tensor_by_name(
            'detection_scores:0')
        self.detection_classes = self.detection_graph.get_tensor_by_name(
            'detection_classes:0')
        self.num_detections = self.detection_graph.get_tensor_by_name(
            'num_detections:0')

        print("Detection graph loaded")
Пример #4
0
    def __init__(self):

        # set default value for no detection
        self.current_light = TrafficLight.UNKNOWN
        curr_dir = os.path.dirname(os.path.realpath(__file__))
        model = curr_dir + '/sim-udacity-model/frozen_inference_graph.pb'

        ## this is for selecting model trained on simulator or real images:
        #if sim:
        #    model = curr_dir + '/sim-udacity_model/frozen_inference_graph.pb'
        #else:
        #    model = curr_dir + '/real-udacity_model/frozen_inference_graph.pb'

        labels_file = curr_dir + '/labelmap.pbtxt'
        num_classes = 4  #14

        label_map = label_map_util.load_labelmap(labels_file)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=num_classes, use_display_name=True)
        self.category_index = label_map_util.create_category_index(categories)

        self.image_np_deep = None
        self.detection_graph = tf.Graph()

        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True

        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()

            with tf.gfile.GFile(model, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            self.sess = tf.Session(graph=self.detection_graph, config=config)

        # Definite input and output Tensors for detection_graph
        self.image_tensor = self.detection_graph.get_tensor_by_name(
            'image_tensor:0')

        # Each box represents a part of the image where a particular object was detected.
        self.detection_boxes = self.detection_graph.get_tensor_by_name(
            'detection_boxes:0')

        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        self.detection_scores = self.detection_graph.get_tensor_by_name(
            'detection_scores:0')
        self.detection_classes = self.detection_graph.get_tensor_by_name(
            'detection_classes:0')
        self.num_detections = self.detection_graph.get_tensor_by_name(
            'num_detections:0')

        print("Loaded frozen model graph")
Пример #5
0
    def __init__(self):
        #self.simulator = True #Set to false for real car.
        self.simulator = rospy.get_param(
            "/simulator")  #Set to false for real car.

        cwd = os.path.dirname(os.path.realpath(__file__))
        if self.simulator == True:
            print("In SIMULATOR")
            CKPT = cwd + "/graphs/frozen_inference_graph.pb"
        else:
            print("In CAR")
            CKPT = cwd + "/graphs/frozen_inference_graph_real.pb"

        PATH_TO_LABELS = cwd + '/graphs/object-detection.pbtxt'
        NUM_CLASSES = 3  # 3, as pretrained on udacity real car dataset with 3 classes

        #Label maps map indices to category names
        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        self.category_index = label_map_util.create_category_index(categories)

        #Build network
        self.detection_graph = tf.Graph()

        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()

            with tf.gfile.GFile(CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            self.sess = tf.Session(graph=self.detection_graph)

        # Definite input and output Tensors for detection_graph
        self.image_tensor = self.detection_graph.get_tensor_by_name(
            'image_tensor:0')

        # Each box represents a part of the image where a particular object was detected.
        self.detection_boxes = self.detection_graph.get_tensor_by_name(
            'detection_boxes:0')

        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        self.detection_scores = self.detection_graph.get_tensor_by_name(
            'detection_scores:0')
        self.detection_classes = self.detection_graph.get_tensor_by_name(
            'detection_classes:0')
        self.num_detections = self.detection_graph.get_tensor_by_name(
            'num_detections:0')

        print("Loaded model")
    def __init__(self, *args):

        self.current_light = TrafficLight.RED
        cwd = os.path.dirname(os.path.realpath(__file__))

        # Path to frozen detection graph. This is the actual model that is used for the object detection.
        base_path = os.path.dirname(os.path.abspath(__file__))
        PATH_TO_CKPT = os.path.join(base_path, 'model',
                                    'frozen_inference_graph.pb')

        # Load label map
        PATH_TO_LABELS = os.path.join(base_path, 'model',
                                      'traffic_lights_label_map.pbtxt')
        NUM_CLASSES = 14
        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=NUM_CLASSES, use_display_name=True)

        self.category_index = label_map_util.create_category_index(categories)

        # Build network
        self.detection_graph = tf.Graph()
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True  # https://github.com/tensorflow/tensorflow/issues/6698

        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()

            with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            self.sess = tf.Session(graph=self.detection_graph, config=config)

        # Definite input and output Tensors for detection_graph
        self.image_tensor = self.detection_graph.get_tensor_by_name(
            'image_tensor:0')
        # Each box represents a part of the image where a particular object was detected.
        self.detection_boxes = self.detection_graph.get_tensor_by_name(
            'detection_boxes:0')
        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        self.detection_scores = self.detection_graph.get_tensor_by_name(
            'detection_scores:0')
        self.detection_classes = self.detection_graph.get_tensor_by_name(
            'detection_classes:0')
        self.num_detections = self.detection_graph.get_tensor_by_name(
            'num_detections:0')

        print("Classifier initialisation complete!")
Пример #7
0
    def __init__(self, is_site):
        # set default value for no detection
        self.current_light = TrafficLight.UNKNOWN
        curr_dir = os.path.dirname(os.path.realpath(__file__))
        model_dir = curr_dir + '/../../../../image_classification_model/'
        if is_site:
            model = model_dir + '/protobuf/frozen_inference_graph_ssd_real.pb'
        else:
            model = model_dir + '/protobuf/frozen_inference_graph_ssd_sim.pb'

        num_classes = 4
        labels_file = model_dir + '/label_map.pbtxt'
        label_map = label_map_util.load_labelmap(labels_file)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=num_classes, use_display_name=True)
        self.category_index = label_map_util.create_category_index(categories)

        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True

        self.detection_graph = tf.Graph()
        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(model, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            self.sess = tf.Session(graph=self.detection_graph, config=config)

        # Input tensor
        self.image_tensor = self.detection_graph.get_tensor_by_name(
            'image_tensor:0')

        # Each box represents a part of the image where a particular object was detected.
        self.detection_boxes = self.detection_graph.get_tensor_by_name(
            'detection_boxes:0')
        # Each score represents the confidence level for each object
        self.detection_scores = self.detection_graph.get_tensor_by_name(
            'detection_scores:0')
        self.detection_classes = self.detection_graph.get_tensor_by_name(
            'detection_classes:0')
        self.num_detections = self.detection_graph.get_tensor_by_name(
            'num_detections:0')

        self.image_classified = None
        print("Frozen graph loaded! model: {}".format(model))
Пример #8
0
    def __init__(self, threshold=0.5):
        dir_path = os.path.dirname(os.path.realpath(__file__))

        self.threshold = threshold

        self.model_name = 'ssd_mobilenet_v1_udacity_all_1.4'
        self.model_path = dir_path + '/models/' + self.model_name + \
                                     '/frozen_inference_graph.pb'
        self.labels_name = 'udacity_all'
        self.labels_path = dir_path + '/labels/' + self.labels_name + ".pbtxt"

        # add classification boxes to image
        label_map = label_map_util.load_labelmap(self.labels_path)
        categories = label_map_util.convert_label_map_to_categories(label_map,
                                                                    max_num_classes=3, use_display_name=True)
        self.category_index = label_map_util.create_category_index(categories)

        self.detection_graph = self.load_tf_model()

        self.classified_image = None
Пример #9
0
    def __init__(self,model_path):
        

        self.current_light = TrafficLight.UNKNOWN  # Default value if pass on network / or nothing detected.

        cwd = os.path.dirname(os.path.realpath(__file__))
     
        self.simulation = True  # Set to false for real  (sim one somehow works sorta for real too it's spooky)
        self.faster = True  # 10 for faster run time or anything else for larger network.

        # Default to Simulation with 10 regions.
	    #print("cwd: {}".format(cwd))
        if self.simulation is True:
            if self.faster is True:
                CKPT = cwd+'/../../../../classifier/faster-R-CNN/checkpoints/frozen_inference_graph.pb'
            else:
                CKPT = cwd+'/../../../../classifier/faster-R-CNN/checkpoints/frozen_inference_graph.pb'
        else:
            CKPT = cwd+'/../../../../classifier/faster-R-CNN/checkpoints/frozen_inference_graph.pb'

        #print("cwd: {}".format(cwd))
        PATH_TO_LABELS = cwd+'/../../../../classifier/label_map.pbtxt'
        NUM_CLASSES = 14

        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        self.category_index = label_map_util.create_category_index(categories)

        ##### Build network

        self.image_np_deep = None
        self.detection_graph = tf.Graph()

        # https://github.com/tensorflow/tensorflow/issues/6698
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        # end

        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()

            with tf.gfile.GFile(CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            self.sess = tf.Session(graph=self.detection_graph, config=config)

        # Definite input and output Tensors for detection_graph
        self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')

        # Each box represents a part of the image where a particular object was detected.
        self.detection_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')

        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        self.detection_scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
        self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
        self.num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')

        print("Loaded graph")
    def __init__(self, *args):

        #print("Classifier launched in site mode : ", args[0])

        #self.current_light = TrafficLight.RED
        self.current_light = 0
        #cwd = os.path.dirname(os.path.realpath(__file__))

        # Path to frozen detection graph. This is the actual model that is used for the object detection.
        base_path = '/home/ashis/ashis/Udacity/SDC/Term_3/TL_Model_Test/CarND-Capstone-Ashis-master/ros/src/tl_detector/light_classification/'  #os.path.dirname(os.path.abspath(__file__))
        #MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017'
        MODEL_NAME = 'ssd_mobilenet_tl'
        PATH_TO_CKPT = os.path.join(base_path, MODEL_NAME,
                                    'frozen_inference_graph.pb')
        #CHUNK_SIZE = 10485760  # 10MB
        #PATH_TO_CHUNKS = os.path.join(base_path, MODEL_NAME, 'chunks')

        #print('checkpoint', PATH_TO_CKPT)
        #print('chunks', PATH_TO_CHUNKS)

        # If the frozen model does not exist trying creating it from file chunks
        #if not os.path.exists(PATH_TO_CKPT):  #(MODEL_NAME + '/frozen_inference_graph.pb'):
        #    print("frozen inference graph not found - building from chunks")
        #    output = open(PATH_TO_CKPT, 'wb')
        #    chunks = os.listdir(PATH_TO_CHUNKS)
        #    chunks.sort()
        #    for fname in chunks:
        #        fpath = os.path.join(PATH_TO_CHUNKS, fname)
        #        with open(fpath, 'rb') as fileobj:
        #            for chunk in iter(lambda: fileobj.read(CHUNK_SIZE), b''):
        #                output.write(chunk)
        #    output.close()

        # Load label map

        PATH_TO_LABELS = os.path.join(base_path, 'data',
                                      'traffic_lights_label_map.pbtxt')
        NUM_CLASSES = 14

        #label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        label_map = label_map_util.load_labelmap(
            '/home/ashis/ashis/Udacity/SDC/Term_3/TL_Model_Test/CarND-Capstone-Ashis-master/ros/src/tl_detector/light_classification/ssd_mobilenet_tl/traffic_lights_label_map.pbtxt'
        )
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=NUM_CLASSES, use_display_name=True)

        self.category_index = label_map_util.create_category_index(categories)

        # Build network
        self.detection_graph = tf.Graph()
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True  # https://github.com/tensorflow/tensorflow/issues/6698

        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()

            with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            self.sess = tf.Session(graph=self.detection_graph, config=config)

        # Definite input and output Tensors for detection_graph
        self.image_tensor = self.detection_graph.get_tensor_by_name(
            'image_tensor:0')
        # Each box represents a part of the image where a particular object was detected.
        self.detection_boxes = self.detection_graph.get_tensor_by_name(
            'detection_boxes:0')
        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        self.detection_scores = self.detection_graph.get_tensor_by_name(
            'detection_scores:0')
        self.detection_classes = self.detection_graph.get_tensor_by_name(
            'detection_classes:0')
        self.num_detections = self.detection_graph.get_tensor_by_name(
            'num_detections:0')

        print("Classifier initialisation complete!")
Пример #11
0
    def load_graph(self):
        dir_path = os.path.dirname(os.path.realpath(__file__))
        dir_path = os.path.join(dir_path, 'frozen_models')

        # The trained tf model

        # Accurate
        MODEL_NAME = 'frozen_resnet101'

        # Fast
        # MODEL_NAME = 'frozen_ssd_mobilenet'

        MODEL_NAME += ('_sim' if self.on_sim else '_real')
        MODEL_NAME = os.path.join(dir_path, MODEL_NAME)

        # Path to frozen detection graph. This is the actual model that is used for the object detection.
        PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

        # List of the strings that is used to add correct label for each box.
        PATH_TO_LABELS = MODEL_NAME + '/label_map.pbtxt'

        NUM_CLASSES = 14

        ###
        # Load a (frozen) Tensorflow model into memory.
        detection_graph = tf.Graph()
        with detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

        ###
        # Loading label map
        # label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        # categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        # category_index = label_map_util.create_category_index(categories)

        # category_index = {
        # category_index[1] = {'id': 1, 'name': TrafficLight.GREEN, 'description': 'Green'}
        # category_index[2] = {'id': 2, 'name': TrafficLight.RED, 'description': 'Red'}
        # category_index[3] = {'id': 3, 'name': TrafficLight.YELLOW, 'description': 'Yellow'}
        # category_index[4] = {'id': 4, 'name': TrafficLight.UNKNOWN, 'description': 'off'}}


        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
                                                                    use_display_name=True)
        self.category_index = label_map_util.create_category_index(categories)
        rospy.loginfo('%s', self.category_index)

        rospy.loginfo('Loaded model: %s', PATH_TO_CKPT)

        with detection_graph.as_default():
            # Definite input and output Tensors for detection_graph
            self.image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # Each box represents a part of the image where a particular object was detected.
            self.detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class label.
            self.detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
            self.detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
            self.num_detections = detection_graph.get_tensor_by_name('num_detections:0')

            self.sess = tf.Session(graph=detection_graph)

        self.detection_graph = detection_graph