<jupyter_start><jupyter_text>Manually testing the models<jupyter_code>#from styx_msgs.msg import TrafficLight
#import rospy


import tensorflow as tf
import cv2
import numpy as np
from collections import Counter
import glob

class TLClassifier(object):
    def __init__(self, model_path, confidence_threshold):
        
        self.confidence_threshold = confidence_threshold

        self.graph = tf.Graph()

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

        with self.graph.as_default():
            graph_def = tf.GraphDef()

            with tf.gfile.GFile(model_path, 'rb') as graph_file:
                read_graph_file = graph_file.read()

                graph_def.ParseFromString(read_graph_file)
                tf.import_graph_def(graph_def, name='')

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

            self.image_tensor = self.graph.get_tensor_by_name('image_tensor:0')
            self.num_detections =self.graph.get_tensor_by_name('num_detections:0')
            # For each detection, it's corresponding bounding box, class and score:
            # self.boxes = self.graph.get_tensor_by_name('detection_boxes:0')
            self.classes = self.graph.get_tensor_by_name('detection_classes:0')
            self.scores =self.graph.get_tensor_by_name('detection_scores:0')


    def get_classification(self, image):
        """Determines the color of the traffic light in the image

        Args:
            image (cv::Mat): image containing the traffic light

        Returns:
            int: ID of traffic light color (specified in styx_msgs/TrafficLight)

        """

        cv2_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        with self.graph.as_default():
            cv2_image_expanded = np.expand_dims(image, axis=0)

            (classes, scores) = self.sess.run(
                [self.classes, self.scores],
                feed_dict={self.image_tensor: cv2_image_expanded})
            classes = np.squeeze(classes)
            scores = np.squeeze(scores)
            
            print(classes)
            print(scores)

#             rospy.loginfo("[TL Detector] Results:")
#             rospy.loginfo("[TL Detector] Classes: {0}".format(classes))
#             rospy.loginfo("[TL Detector] Scores: {0}".format(scores))

#             ##############
#             ## Strategy 1: pick the most frequent prediction (consider replacing 
#             # with weighted frequency)
#             #
#             #
#             class_predictions_above_threshold = []
#             for detected_class, score in zip(classes, scores):
# #                 print (detected_class)
# #                 print (score)
#                 if score > self.confidence_threshold:
#                     class_predictions_above_threshold.append(detected_class)

#             if (len(class_predictions_above_threshold) == 0):
# #                 rospy.loginfo("[Detector] Predicted Class: Unknown")
#                 return 4 # Unknown
                
#             class_predictions = Counter(class_predictions_above_threshold)
#             #print(class_predictions)
#             most_frequent_class = class_predictions.most_common(1)[0][0]

#             if most_frequent_class == 1:
# #                 rospy.loginfo("[Detector] Predicted Class: Red")
#                 return most_frequent_class
#             elif most_frequent_class == 2:
# #                 rospy.loginfo("[Detector] Predicted Class: Yellow")
#                 return most_frequent_class
#             elif most_frequent_class == 3:
# #                 rospy.loginfo("[Detector] Predicted Class: Green")
#                 return most_frequent_class
#             #
#             #
#             ##############


            ##############
            ## Strategy 2: pick the first prediction above threshold
            #
            #
            for detected_class, score in zip(classes, scores):
                if score > self.confidence_threshold:                    
                    if detected_class == 1:
#                         rospy.loginfo("[Detector] Predicted Class: Red")
                        return detected_class
                    elif detected_class == 2:
#                         rospy.loginfo("[Detector] Predicted Class: Yellow")
                        return detected_class
                    elif detected_class == 3:
#                         rospy.loginfo("[Detector] Predicted Class: Green")
                        return detected_class
            #
            #
            ##############

#         rospy.loginfo("[Detector] Predicted Class: Unknown")
        return 4 # Unknown
classifier = TLClassifier(model_path= "../data/saved_models/model_sim/frozen_inference_graph.pb", confidence_threshold=0.02)

images_path = "../data/sample_images/*.jpg"
for image_path in sorted(glob.glob(images_path)):

    print("Image: " + image_path)

    image = cv2.imread(image_path)
    predicted_label = classifier.get_classification(image)

    print("Label: " + str(predicted_label))<jupyter_output>Image: ../data/sample_images/green_sdc-data_85x256-jpg-high-crop_14.jpg
[1. 2. 2. 3. 3. 3. 3. 3. 3. 1. 2. 3. 2. 3. 2. 1. 3. 3. 3. 3. 3. 2. 2. 4.
 2. 3. 1. 3. 2. 3. 3. 2. 2. 3. 3. 2. 3. 3. 3. 2. 2. 4. 3. 3. 4. 2. 3. 3.
 1. 4. 3. 2. 3. 3. 3. 1. 1. 2. 3. 3. 3. 2. 2. 1. 3. 2. 3. 4. 3. 2. 3. 3.
 2. 3. 1. 2. 3. 1. 2. 4. 4. 3. 2. 2. 4. 2. 2. 2. 3. 3. 3. 1. 2. 2. 4. 3.
 2. 1. 3. 2.]
[0.01946918 0.01883261 0.01808292 0.01700245 0.01660627 0.01649474
 0.01611187 0.01599458 0.01565426 0.01555851 0.01523146 0.01520224
 0.01508213 0.01502282 0.01499779 0.01485171 0.01483312 0.01478507
 0.01439098 0.0142599  0.01420549 0.01390461 0.01383304 0.01372231
 0.0136283  0.01348826 0.01348742 0.01341327 0.01340254 0.01340154
 0.01339328 0.01337842 0.01310137 0.01309384 0.01304219 0.01302698
 0.0130214  0.01291622 0.01287638 0.01284852 0.01252232 0.01236002
 0.01235716 0.01233203 0.01230077 0.0122579  0.01224047 0.01220075
 0.01218491 0.01212045 0.01210643 0.01209157 0.01208569 0.01207761
 0.01204387 0.01200[...]
{'fraction_non_alphanumeric': 0.11513859275053305, 'fraction_numerical': 0.1044776119402985, 'mean_word_length': 1.9731838127742565, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '383838', 'license': 'MIT', 'n_tokens_mistral': 2429, 'n_tokens_neox': 1926, 'n_words': 434, 'path': 'ros/src/tl_detector/light_classification/tl_classifier.ipynb', 'repo_name': 'jmichela/CarND-Capstone', 'size': '6097'}