def DetectInit(): global sess, model, mc detect_net = 'squeezeDet' checkpoint = '/home/ubuntu/catkin_ws/src/robo_perception/scripts/weights/model.ckpt-99999' assert detect_net == 'squeezeDet' or detect_net == 'squeezeDet+', \ 'Selected nueral net architecture not supported' tf.Graph().as_default() # Load model if detect_net == 'squeezeDet': mc = kitti_squeezeDet_config() mc.BATCH_SIZE = 1 # model parameters will be restored from checkpoint mc.LOAD_PRETRAINED_MODEL = False model = SqueezeDet(mc, '0') elif detect_net == 'squeezeDet+': mc = kitti_squeezeDetPlus_config() mc.BATCH_SIZE = 1 mc.LOAD_PRETRAINED_MODEL = False model = SqueezeDetPlus(mc, '0') saver = tf.train.Saver(model.model_params) # Use jit xla gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5) config = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_options) config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1 #with tf.Session(config=config) as sess: sess = tf.Session(config=config) saver.restore(sess, checkpoint)
def __init__(self): self.image_pub = rospy.Publisher("rcnn/debug_image", Image, queue_size=1) self.object_pub = rospy.Publisher("rcnn/objects", Detection2DArray, queue_size=1) # Create a supscriber from topic "image_raw" self.bridge = CvBridge() self.image_sub = rospy.Subscriber("image", Image, self.image_callback, queue_size=1, buff_size=2**24) self.sess = tf.Session(graph=detection_graph, config=config) self.DIAMETER_LANDMARCK_M = rospy.get_param('~markerSize_RCNN', 0.137) self.DISTANCE_FOCAL = rospy.get_param('~distance_focal', 720) self.MAX_NUMBER_OF_BOXES = rospy.get_param('~max_number_of_boxes', 1.0) self.MINIMUM_CONFIDENCE = rospy.get_param('~minimum_confidence', 0.85) rospy.logdebug("%s is %s default %f", rospy.resolve_name('~markerSize_RCNN'), self.DIAMETER_LANDMARCK_M, 0.137) rospy.logdebug("%s is %s default %f", rospy.resolve_name('~distance_focal'), self.DISTANCE_FOCAL, 720) rospy.logdebug("%s is %s default %f", rospy.resolve_name('~max_number_of_boxes'), self.MAX_NUMBER_OF_BOXES, 1.0) rospy.logdebug("%s is %s default %f", rospy.resolve_name('~minimum_confidence'), self.MINIMUM_CONFIDENCE, 0.85)
def train(self, num_iterations): ''' Train the network for certain number of iterations param: num_iterations: The number of iterations to train ''' # TODO:: NEED TO LOAD AND SAVE THE TOTAL ITER with tf.Session() as session: if os.path.isfile(self.model_path + '.meta'): # Model has been trained. Restore saver = tf.import_meta_graph(self.model_path + ".meta") saver.restore(session, tf.train.latest_checkpoint(self.model_path)) else: # Otherwise create the graph self.initialize_graph() self.read_total_iter() saver = tf.train.Saver() session.run(tf.global_variables_initializer()) for step in range(self.total_iter, self.total_iter + num_iterations): x_batch_train, y_batch_train = self.data_process.next_training_batch(self.batch_size) x_batch_valid, y_batch_valid = self.data_process.next_validate_batch(self.batch_size) feed_dict_train = {x: x_batch_train y_true: y_batch_train} feed_dict_valid = {x: x_batch_valid, y_true: y_batch_valid} session.run(self.optimizer, feed_dict=feed_dict_train) if step % int(self.data_process.get_training_data_size()/batch_size) == 0: val_loss = session.run(self.cost, feed_dict=feed_dict_valid) epoch = int(step / int(self.data_process.get_training_data_size()/batch_size)) show_progress(epoch, feed_dict_tr, feed_dict_val, val_loss, session) saver.save(session, self.model_path + 'trained_model', global_step=step) total_iterations += num_iteration
def __init__(self): rospy.init_node('tl_detector') self.pos_xy = None self.waypoints = None self.waypoints_tree = None self.camera_image = None self.lights = [] rospy.Subscriber('/current_pose', PoseStamped, self.pose_cb) rospy.Subscriber('/base_waypoints', Lane, self.waypoints_cb) # /vehicle/traffic_lights provides you with the location of all traffic # lights 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. rospy.Subscriber('/vehicle/traffic_lights', TrafficLightArray, self.traffic_cb) rospy.Subscriber('/image_color', Image, self.image_cb) config_string = rospy.get_param("/traffic_light_config") self.config = yaml.load(config_string) self.upcoming_red_light_pub = rospy.Publisher('/traffic_waypoint', Int32, queue_size=1) self.bridge = CvBridge() if TESTING: self.light_classifier = None elif USE_NN: import tensorflow as tf from light_classification.tl_classifier_dl import TLClassifierDL self.session = tf.Session() img_wh = 200, 150 ckpt_prefix = self.config['ckpt_prefix'] rospy.loginfo( 'ckpt_prefix=%s\npwd=%s', ckpt_prefix, os.getcwd() ) ckpt_path = ckpt_prefix self.light_classifier = TLClassifierDL(self.session, ckpt_path, img_wh) rospy.loginfo( 'Done loading tf model' ) # self.listener = tf.TransformListener() else: self.session=None self.light_classifier = TLClassifier() self.state = TrafficLight.UNKNOWN self.last_state = TrafficLight.UNKNOWN self.last_wp = -1 self.state_count = 0 self.img_cnt = 0 rospy.spin()
def prediction(skele, model_name='basic_model'): labels = ['high', 'med', 'low'] session = tf.Session() with session as sess: new_saver = tf.train.import_meta_graph( 'model/{}.meta'.format(model_name)) new_saver.restore(sess, 'model/{}'.format(model_name)) graph = tf.get_default_graph() input_ph = graph.get_tensor_by_name("input_ph:0") pred = graph.get_tensor_by_name("pred:0") predictions = sess.run([pred], feed_dict={input_ph: skele}) return labels[predictions[0][0]]
def init_tensorflow_custom(self): detection_graph = load_graph(CUSTOM_GRAPH_FILE) # The input placeholder for the image. self.input_tensor = detection_graph.get_tensor_by_name( 'input_tensor:0') # The output of the inference self.result_tensor = detection_graph.get_tensor_by_name( 'result_tensor/Reshape:0') self.keras_learning = detection_graph.get_tensor_by_name( 'conv1_bn/keras_learning_phase:0') self.tf_session = tf.Session(graph=detection_graph)
def init_tensorflow_ssd7(self): detection_graph = load_graph(SSD7_GRAPH_FILE) #ops = detection_graph.get_operations() #for ooo in ops: # print (ooo) # The input placeholder for the image. self.input_tensor = detection_graph.get_tensor_by_name('input_1:0') self.keras_learning = detection_graph.get_tensor_by_name( 'keras_learning_phase:0') self.detection = detection_graph.get_tensor_by_name( 'predictions/concat:0') self.tf_session = tf.Session(graph=detection_graph)
def __init__(self): #TODO load classifier # pass self.model = tf.Graph() # create a context manager that makes this model the default one for # execution with self.model.as_default(): # initialize the graph definition graphDef = tf.GraphDef() # load the graph from disk with tf.gfile.GFile("/home/student/Desktop/CarND-Capstone/ros/src/tl_detector/light_classification/graph_optimized.pb", "rb") as f: serializedGraph = f.read() graphDef.ParseFromString(serializedGraph) tf.import_graph_def(graphDef, name="") # create a session to perform inference self.sess = tf.Session(graph=self.model)
def init_tensorflow_ssd(self): detection_graph = load_graph(SSD_GRAPH_FILE) # The input placeholder for the image. 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') # The classification of the object (integer id). self.detection_classes = detection_graph.get_tensor_by_name( 'detection_classes:0') self.tf_session = tf.Session(graph=detection_graph)
import tf as tf dataset1 = tf.data.Dataset.range(100) iterator = dataset1.make_one_shot_iterator() next_element = iterator.get_next() sess = tf.Session() for i in range(100): value = sess.run(next_element) assert value == i max_value = tf.placeholder(tf.int64, shape=[]) dataset = tf.data.Dataset.range(max_value) iterator = dataset.make_initializable_iterator() next_element = iterator.get_next() sess.run(iterator.initializer, feed_dict={max_value: 10}) for i in range(10): value = sess.run(next_element) assert i == value sess.run(iterator.initializer, feed_dict={max_value: 100}) for i in range(100): value = sess.run(next_element) assert i == value
with tf.variable_scope('softmax_linear'): weight = tf.Variable(tf.truncated_normal([512, num_classes], stddev=0.1, dtype=tf.float32), name='weight') bias = tf.Variable(tf.constant(0.1, shape=[num_classes], dtype=tf.float32)) logits = tf.matmul(local3, weight) + bias loss_op = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss_op) prediction = tf.nn.softmax(logits) correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(labels, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for step in range(1, training_steps + 1): batch_x, batch_y = mnist.train.next_batch(batch_size) batch_x = batch_x.reshape((batch_size, 28, 28, 1)) feed_dict = {images: batch_x, labels: batch_y} sess.run(train_op, feed_dict=feed_dict) if step % display_step == 0 or step == 1: # Calculate batch loss and accuracy loss, acc = sess.run([loss_op, accuracy], feed_dict=feed_dict) print("Step " + str(step) + ", Minibatch Loss= " + \ "{:.4f}".format(loss) + ", Training Accuracy= " + \ "{:.3f}".format(acc))
self.environment.run() if not (isLearned) and self.thread_type is 'test': time.sleep(1.0) if isLearned and self.thread_type is 'learning': time.sleep(3.0) if isLearned and self.thread_type is 'test': time.sleep(3.0) self.environment.run() frames = 0 isLearned = False SESS = tf.Session() with tf.device("/cpu:0"): parameter_server = ParameterServer() threads = [] for i in range(N_WORKERS): thread_name = int(i) threads.append( Worker_thread(thread_name=thread_name, thread_type="learning", parameter_server=parameter_server)) # threads.append(Worker_thread(thread_name="test_thread", thread_type="test", parameter_server=parameter_server)) COORD = tf.train.Coordinator()
def get_session(): config = tf.ConfigProto() config.gpu_options.allow_growth = True return tf.Session(config=config)
import tf import os #specify the gpu device id os.environ["CUDA_VISIBLE_DEVICES"] = "2" #specify the fraction of all the gpu memory gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) #specify the allow_growth option gpu_options = tf.GPUOptions(allow_growth=True) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))