def initialize(weights_path, hypes_path, options=None): """Initialize prediction process. All long running operations like TensorFlow session start and weights loading are made here. Args: weights_path (string): The path to the model weights file. hypes_path (string): The path to the hyperparameters file. options (dict): The options dictionary with parameters for the initialization process. Returns (dict): The dict object which contains `sess` - TensorFlow session, `pred_boxes` - predicted boxes Tensor, `pred_confidences` - predicted confidences Tensor, `x_in` - input image Tensor, `hypes` - hyperparametets dictionary. """ H = prepare_options(hypes_path, options) #manual fix H['grid_height'] = 15 H['grid_width'] = 20 tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas \ = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], H['num_classes']])), [grid_area, H['rnn_len'], H['num_classes']]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) config = tf.ConfigProto(log_device_placement=True) #modification config.gpu_options.allow_growth = True saver = tf.train.Saver() sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) saver.restore(sess, weights_path) return { 'sess': sess, 'pred_boxes': pred_boxes, 'pred_confidences': pred_confidences, 'x_in': x_in, 'hypes': H }
def create_meta_graph(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) tf.add_to_collection('placeholders', x_in) tf.add_to_collection('vars', pred_boxes) tf.add_to_collection('vars', pred_confidences) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.save(sess, args.output) tf.train.write_graph(sess.graph.as_graph_def(), '', "{}.pb".format(args.output))
def create_graph(args, H): tf.reset_default_graph() H["grid_width"] = H["image_width"] / H["region_size"] H["grid_height"] = H["image_height"] / H["region_size"] # x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 1]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) tf.add_to_collection('placeholders', x_in) tf.add_to_collection('vars', pred_boxes) tf.add_to_collection('vars', pred_confidences) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) metafile_path = args.input if args.frozen else args.output metafile_path = metafile_path.split('.pb')[0] saver.save(sess, metafile_path) tf.train.write_graph(sess.graph.as_graph_def(), '', args.input if args.frozen else args.output)
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name="x_in", shape=[H["image_height"], H["image_width"], 3]) googlenet = googlenet_load.init(H) if H["use_rezoom"]: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), googlenet, "test", reuse=None ) grid_area = H["grid_height"] * H["grid_width"] pred_confidences = tf.reshape( tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H["rnn_len"], 2])), [grid_area, H["rnn_len"], 2] ) if H["reregress"]: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), googlenet, "test", reuse=None ) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() true_annolist = al.parse(args.test_idl) data_dir = os.path.dirname(args.test_idl) image_dir = get_image_dir(args) subprocess.call("mkdir -p %s" % image_dir, shell=True) for i in range(len(true_annolist)): true_anno = true_annolist[i] orig_img = imread("%s/%s" % (data_dir, true_anno.imageName))[:, :, :3] img = imresize(orig_img, (H["image_height"], H["image_width"]), interp="cubic") feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles( H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H["rnn_len"], min_conf=0.2, tau=args.tau, ) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes( (H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1] ) pred_annolist.append(pred_anno) imname = "%s/%s" % (image_dir, os.path.basename(true_anno.imageName)) misc.imsave(imname, new_img) if i % 25 == 0: print(i) return pred_annolist, true_annolist
def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget @type QWidget """ QMainWindow.__init__(self, parent) self.setupUi(self) self.status = "IDLE" self.timeFlag = True self.videoSourse = "./data/test.avi" self.frame_id = 1 self.stop = False self.clear = False self.reset = False self.start = True self.videoCapture = cv2.VideoCapture("video/test.avi") self._timer = QtCore.QTimer(self) self._timer.timeout.connect(self.playVideo) self._timer.start(500) self.video_output_height = 640 #850 self.video_output_width = 1000 #1400 with open(hypes_file, 'r') as f: self.H = json.load(f) tf.reset_default_graph() self.x_in = tf.placeholder(tf.float32, name='x_in', shape=[self.H['image_height'], self.H['image_width'], 3]) if self.H['use_rezoom']: self.pred_boxes, self.pred_logits, self.pred_confidences, self.pred_confs_deltas, self.pred_boxes_deltas = build_forward(self.H, tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), 'test', reuse=None) grid_area = self.H['grid_height'] * self.H['grid_width'] self.pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(self.pred_confs_deltas, [grid_area * self.H['rnn_len'], 2])), [grid_area, self.H['rnn_len'], 2]) if self.H['reregress']: self.pred_boxes = self.pred_boxes + self.pred_boxes_deltas else: self.pred_boxes, self.pred_logits, self.pred_confidences = build_forward(self.H, tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), tf.expand_dims(self.x_in, 0), 'test', reuse=None) saver = tf.train.Saver() self.sess = tf.Session() self.sess.run(tf.initialize_all_variables()) saver.restore(self.sess, weights_file)
def infer_all(args, H, db_args): VERSION = md5.new(args.weights).hexdigest() conn = psycopg2.connect(**db_args) tf.reset_default_graph() H["grid_width"] = H["image_width"] / H["region_size"] H["grid_height"] = H["image_height"] / H["region_size"] x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() queue = Queue() ts = datetime.now().isoformat() ps = [] for _ in range(4): processor = Process(target=process_results, args=(queue, H, args, db_args, '../data', ts)) processor.start() ps.append(processor) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, args.weights) area_to_cover = None if args.boundary: area_to_cover = shape(json.load(open(args.boundary))) generator = InferenceGenerator(conn, args.country, area_to_cover=area_to_cover, data_dir='../data', threads=8) for orig_img, meta in generator: img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} result = sess.run([pred_boxes, pred_confidences], feed_dict=feed) queue.put((result, meta, VERSION)) for p in ps: queue.put(None) p.join()
def initialize(weights_path, hypes_path, options=None): """Initialize prediction process. All long running operations like TensorFlow session start and weights loading are made here. Args: weights_path (string): The path to the model weights file. hypes_path (string): The path to the hyperparameters file. options (dict): The options dictionary with parameters for the initialization process. Returns (dict): The dict object which contains `sess` - TensorFlow session, `pred_boxes` - predicted boxes Tensor, `pred_confidences` - predicted confidences Tensor, `x_in` - input image Tensor, `hypes` - hyperparametets dictionary. """ H = prepare_options(hypes_path, options) if H is None: return None tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas \ = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], H['num_classes']])), [grid_area, H['rnn_len'], H['num_classes']]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() sess = tf.Session() sess.run(tf.initialize_all_variables()) saver.restore(sess, weights_path) return {'sess': sess, 'pred_boxes': pred_boxes, 'pred_confidences': pred_confidences, 'x_in': x_in, 'hypes': H}
def get_results(args, H): tf.reset_default_graph() H["grid_width"] = H["image_width"] / H["region_size"] H["grid_height"] = H["image_height"] / H["region_size"] x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, args.weights) orig_img = imread('data/tshirtslayer/tss-images/wearing.jpg') img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} now = time.time() (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) print(time.time() - now) pred_anno = al.Annotation() pred_anno.imageName = "image.jpg" new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) misc.imsave("data/xxx.jpg", new_img)
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: # sess.run(tf.initialize_all_variables()) sess.run(tf.global_variables_initializer()) # saver.restore(sess, './output/lstm_rezoom_lung_2017_01_17_18.24/save.ckpt-1000000') print('args.weights: %s' % (args.weights,)) saver.restore(sess, args.weights) print('run') pred_annolist = al.AnnoList() true_annolist = al.parse(args.test_boxes) data_dir = os.path.dirname(args.test_boxes) image_dir = get_image_dir(args) subprocess.call('mkdir -p %s' % image_dir, shell=True) for i in range(len(true_annolist)): true_anno = true_annolist[i] orig_img = imread('%s/%s' % (data_dir, true_anno.imageName))[:,:,:3] img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=0.2, tau=args.tau) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) imname = '%s/%s' % (image_dir, os.path.basename(true_anno.imageName)) misc.imsave(imname, new_img) if i % 25 == 0: print(i) return pred_annolist, true_annolist
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) H["grid_width"] = H["image_width"] / H["region_size"] H["grid_height"] = H["image_height"] / H["region_size"] if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() image_dir = args.logdir i=0 subprocess.call('mkdir -p %s' % image_dir, shell=True) for img_list in os.listdir(args.input_images): img_path=os.path.join(args.input_images,img_list) orig_img = imread(img_path) #img=orig_img[0:480,640:1280] img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = img_list new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) pred_anno.rects = rects pred_anno.imagePath = args.input_images pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) imname = '%s/%s' % (args.logdir, img_list) misc.imsave(imname, new_img) i +=1 if i % 25 == 0: print(i) return pred_annolist
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) #写真のInput詳細がわからない if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() true_annolist = al.parse(args.test_boxes) data_dir = os.path.dirname(args.test_boxes) image_dir = get_image_dir(args) subprocess.call('mkdir -p %s' % image_dir, shell=True) for i in range(len(true_annolist)): true_anno = true_annolist[i] orig_img = imread('%s/%s' % (data_dir, true_anno.imageName))[:,:,:3] img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') #画像のResize feed = {x_in: img} #一写真一Step (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, #画像に予測した四角形を追加する use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) #予測値(四角形のxy座標)を画像のsizeに合わせる? pred_annolist.append(pred_anno) imname = '%s/%s' % (image_dir, os.path.basename(true_anno.imageName)) misc.imsave(imname, new_img) if i % 25 == 0: print(i) #pred_annolist:予測値(確信度score:全部、予測のx1,y1,x2,y2) #true_annolist:evalファイルのx1,y1,x2,y2のまま。 return pred_annolist, true_annolist
def __init__(self, hypes_file): tf.reset_default_graph() #hypes_file = 'trainer/hypes.json' print(hypes_file) with open(hypes_file, 'r') as f: self.H = json.load(f) self.H["grid_width"] = int(self.H["image_width"] / self.H["region_size"]) self.H["grid_height"] = int(self.H["image_height"] / self.H["region_size"]) self.x_in = tf.placeholder(tf.float32, name='x_in', shape=[self.H['image_height'], self.H['image_width'], 3]) if self.H['use_rezoom']: self.pred_boxes, pred_logits, self.pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(self.H, tf.expand_dims(self.x_in, 0), 'test', reuse=None) grid_area = self.H['grid_height'] * self.H['grid_width'] self.pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * self.H['rnn_len'], 2])), [grid_area, self.H['rnn_len'], 2]) if self.H['reregress']: self.pred_boxes = self.pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(self.H, tf.expand_dims(self.x_in, 0), 'test', reuse=None) self.sess = tf.Session() saver = tf.train.Saver() self.sess.run(tf.global_variables_initializer()) saver.restore(self.sess, "/home/Charlie/ThermalLeakageDetector/algorithms/door_detection/cnn_detector/trainer/output_model/thermal_window_setup_2018_04_27_05.15/save.ckpt-3200")
def __init__( self, save_dir, iteration, batch_size=1 # Batch sizes greater than 1 will change results due to batch norm in inception_v1 ): self.save_dir = save_dir self.iteration = iteration self.hypes = self._get_hypes() self.hypes['batch_size'] = batch_size self.input_shape = [ self.hypes['image_height'], self.hypes['image_width'], self.hypes['image_channels'] ] # type: Tuple[float, float, float] self.graph = tf.Graph() with self.graph.as_default(): self.x_in = tf.placeholder(tf.float32, name='x_in', shape=self.input_shape) assert (self.hypes['use_rezoom']) pred_boxes, self.pred_logits, self.pred_confidences, self.pred_confs_deltas, pred_boxes_deltas = \ train.build_forward(self.hypes, tf.expand_dims(self.x_in, 0), 'test', reuse=None) self.pred_boxes = pred_boxes + pred_boxes_deltas grid_area = self.hypes['grid_height'] * self.hypes['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(self.pred_confs_deltas, [grid_area * self.hypes['rnn_len'], 2])), [grid_area, self.hypes['rnn_len'], 2]) assert (self.hypes['reregress']) self.sess = tf.Session() self.sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() model_weights = self._get_weights() saver.restore(self.sess, model_weights)
def setup_network(self): tf.reset_default_graph() self.x_in = tf.placeholder( tf.float32, name='x_in', shape=[self.H['image_height'], self.H['image_width'], 3]) if self.H['use_rezoom']: self.pred_boxes, pred_logits, self.pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( self.H, tf.expand_dims(self.x_in, 0), 'test', reuse=None) grid_area = self.H['grid_height'] * self.H['grid_width'] self.pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * self.H['rnn_len'], 2])), [grid_area, self.H['rnn_len'], 2]) if self.H['reregress']: self.pred_boxes = self.pred_boxes + pred_boxes_deltas else: self.pred_boxes, pred_logits, self.pred_confidences = build_forward( self.H, tf.expand_dims(self.x_in, 0), 'test', reuse=None) saver = tf.train.Saver() self.sess = tf.Session() self.sess.run(tf.global_variables_initializer()) #Do I need this? saver.restore(self.sess, self.weight_args)
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() true_annolist = al.parse(args.test_boxes) data_dir = os.path.dirname(args.test_boxes) image_dir = get_image_dir(args) subprocess.call('mkdir -p %s' % image_dir, shell=True) for i in range(len(true_annolist)): true_anno = true_annolist[i] #orig_img = imread('%s/%s' % (data_dir, true_anno.imageName))[:,:,:3] orig_img = imread('%s' % (true_anno.imageName)) img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') x = np.reshape(img, (H['image_height'], H['image_width'], 1)) new_x = np.zeros((H['image_height'], H['image_width'], 3)) for first in range(0, H['image_height']): for second in range(0, H['image_width']): new_x[first][second] = [ x[first][second], x[first][second], x[first][second] ] feed = {x_in: new_x} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles( H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) imname = '%s/%s' % (image_dir, os.path.basename( true_anno.imageName)) misc.imsave(imname, new_img) for i in range(0, len(rects)): if rects[i].score > 0.1: print pred_anno.imageName print "%s %s %s %s %s" % (rects[i].x1, rects[i].x2, rects[i].y1, rects[i].y2, rects[i].score) # print r.writeJSON(rects[i]) #if i % 25 == 0: # print(i) return pred_annolist, true_annolist
def get_results(args, H): tf.reset_default_graph() H["grid_width"] = H["image_width"] / H["region_size"] H["grid_height"] = H["image_height"] / H["region_size"] x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() all_preditions = [] with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() true_annolist = al.parse(args.test_boxes) data_dir = os.path.join(os.path.dirname(args.test_boxes)) false_positives, false_negatives, true_positives = 0, 0, 0 total_time = 0.0 image_dir = get_image_dir(args) subprocess.call('mkdir -p %s' % image_dir, shell=True) for i in range(len(true_annolist)): true_anno = true_annolist[i] orig_img = imread('%s/%s' % (data_dir, true_anno.imageName))[:, :, :3] img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} t0 = time.time() (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) total_time += time.time() - t0 pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects, all_rects = add_rectangles( H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) all_preditions.extend([[r.x1, r.y1, r.x2, r.y2, r.score, i] for r in all_rects]) prediction = np.array([[r.x1, r.y1, r.x2, r.y2, r.score] for r in rects]) targets = np.array([[r.x1, r.y1, r.x2, r.y2] for r in true_anno.rects]) fp, fn, tp, jaccard = get_metrics(targets, prediction) false_positives += fp false_negatives += fn true_positives += tp precision = np.float64(true_positives) / (true_positives + false_positives) recall = np.float64(true_positives) / (true_positives + false_negatives) print( '[%d/%d]: False positives: %d, False negatives: %d, True positives: %d, Precision: %f, Recall: %f' % (i, len(true_annolist), false_positives, false_negatives, true_positives, precision, recall)) df = pandas.DataFrame(all_preditions) df.columns = ['x1', 'y1', 'x2', 'y2', 'score', 'image_id'] print('Total time: %.4f seconds, per image: %.4f' % (total_time, total_time / len(true_annolist))) return df
def sample(args, H, conn): tf.reset_default_graph() H["grid_width"] = H["image_width"] / H["region_size"] H["grid_height"] = H["image_height"] / H["region_size"] x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, args.weights) img_iter = RandomSampler(conn, args.country) img, orig, (roff, coff, filename, whole_img, img_geom) = next(img_iter) current = { 'filename': filename, 'whole_img': whole_img, 'img_geom': img_geom, 'boxes': [] } cur = conn.cursor() upload_samples = 0 while upload_samples < args.max_samples: if current['filename'] and current['filename'] != filename: res = process_file(current['filename'], np.array(current['boxes']), current['whole_img'], args.country, current['img_geom']) cur.execute( "UPDATE buildings.images SET done=true WHERE project=%s AND filename=%s", (args.country, current['filename'])) conn.commit() print('Done with %s' % current['filename']) current = { 'filename': filename, 'whole_img': whole_img, 'img_geom': img_geom, 'boxes': [] } upload_samples += 1 if res else 0 img = imresize(img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() new_img, rects, _ = add_rectangles( H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) pred_anno.rects = rects pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig.shape[0], orig.shape[1]) for r in rects: current['boxes'].append( map(int, [r.x1 + coff, r.y1 + roff, r.x2 + coff, r.y2 + roff])) img, orig, (roff, coff, filename, whole_img, img_geom) = next(img_iter)
def __init__(self, weights = None, cuda = True): if weights is None: if not os.path.exists('weights'): os.mkdir('weights') download_url = 'https://github.com/ArnholdInstitute/ColdSpots/releases/download/1.0/tensorbox.zip' if not os.path.exists('weights/tensorbox'): print('Downloading weights for tensorbox') if not os.path.exists(os.path.join('weights/tensorbox.zip')): check_output(['wget', download_url, '-O', 'weights/tensorbox.zip']) print('Unzipping...') check_output(['unzip', 'weights/tensorbox.zip', '-d', 'weights']) description = json.load(open('weights/tensorbox/description.json')) weights = os.path.join('weights/tensorbox', description['weights']) print('Building model...') self.weights = weights hypes_file = '%s/hypes.json' % os.path.dirname(weights) with open(hypes_file, 'r') as f: self.H = json.load(f) tf.reset_default_graph() self.H["grid_width"] = self.H["image_width"] / self.H["region_size"] self.H["grid_height"] = self.H["image_height"] / self.H["region_size"] self.x_in = tf.placeholder(tf.float32, name='x_in', shape=[self.H['image_height'], self.H['image_width'], 3]) pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(self.H, tf.expand_dims(self.x_in, 0), 'test', reuse=None) grid_area = self.H['grid_height'] * self.H['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * self.H['rnn_len'], 2])), [grid_area, self.H['rnn_len'], 2]) if self.H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas self.pred_boxes = pred_boxes self.pred_confidences = pred_confidences saver = tf.train.Saver() self.session = tf.Session() self.session.run(tf.global_variables_initializer()) saver.restore(self.session, weights)
def still_image_TENSORBOX_multiclass(frames_list,path_video_folder,hypes_file,weights_file,pred_idl): from train import build_forward print("Starting DET Phase") if not os.path.exists(path_video_folder+'/'+folder_path_det_frames): os.makedirs(path_video_folder+'/'+folder_path_det_frames) print("Created Folder: %s"%path_video_folder+'/'+folder_path_det_frames) if not os.path.exists(path_video_folder+'/'+folder_path_det_result): os.makedirs(path_video_folder+'/'+folder_path_det_result) print("Created Folder: %s"% path_video_folder+'/'+folder_path_det_result) det_frames_list=[] #### START TENSORBOX CODE ### idl_filename=path_video_folder+'/'+path_video_folder+'.idl' ### Opening Hypes file for parameters with open(hypes_file, 'r') as f: H = json.load(f) ### Building Network tf.reset_default_graph() googlenet = googlenet_load.init(H) x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), googlenet, 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], H['num_classes']])), [grid_area, H['rnn_len'], H['num_classes']]) pred_logits = tf.reshape(tf.nn.softmax(tf.reshape(pred_logits, [grid_area * H['rnn_len'], H['num_classes']])), [grid_area, H['rnn_len'], H['num_classes']]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), googlenet, 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver.restore(sess, weights_file )##### Restore a Session of the Model to get weights and everything working annolist = al.AnnoList() #### Starting Evaluating the images lenght=int(len(frames_list)) print("%d Frames to DET"%len(frames_list)) progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()]) frameNr=0 skipped=0 for i in progress(range(0, len(frames_list))): if Utils_Image.isnotBlack(frames_list[i]) & Utils_Image.check_image_with_pil(frames_list[i]): img = imread(frames_list[i]) feed = {x_in: img} (np_pred_boxes,np_pred_logits, np_pred_confidences) = sess.run([pred_boxes,pred_logits, pred_confidences], feed_dict=feed) # print_logits(np_pred_confidences) pred_anno = al.Annotation() #pred_anno.imageName = test_anno.imageName # print "np_pred_confidences shape" + str(np_pred_confidences.shape) # print "np_pred_boxes shape" + str(np_pred_boxes.shape) # for i in range(0, np_pred_confidences.shape[0]): # print np_pred_confidences[i] # for j in range(0, np_pred_confidences.shape[2]): # print np_pred_confidences[i][0][j] rects = get_multiclass_rectangles(H, np_pred_confidences, np_pred_boxes, rnn_len=H['rnn_len']) pred_anno.rects = rects pred_anno.imageName = frames_list[i] pred_anno.frameNr = frameNr frameNr=frameNr+1 det_frames_list.append(frames_list[i]) pick = NMS(rects) draw_rectangles(frames_list[i],frames_list[i], pick) annolist.append(pred_anno) else: skipped=skipped+1 saveTextResults(idl_filename,annolist) annolist.save(pred_idl) print("Skipped %d Black Frames"%skipped) #### END TENSORBOX CODE ### return det_frames_list
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() true_annolist = al.parse(args.test_boxes) data_dir = os.path.dirname(args.test_boxes) image_dir = get_image_dir(args) os.makedirs(image_dir, exist_ok=True) print('Outputs will be stored in {}'.format(image_dir)) for i in range(len(true_annolist)): try: true_anno = true_annolist[i] orig_img = imread('%s/%s' % (data_dir, true_anno.imageName))[:, :, :3] img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run( [pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles( H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) rects = [r for r in rects if r.x1 < r.x2 and r.y1 < r.y2] pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes( (H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1], test=True) pred_annolist.append(pred_anno) imname = '%s/%s' % (image_dir, os.path.basename(true_anno.imageName)) misc.imsave(imname, new_img) except FileNotFoundError: pass if i % 25 == 0: print(i) return pred_annolist, true_annolist
def run(self): print 'run' imgs = get_test_src(self.data_dir) tf.reset_default_graph() x_in = tf.placeholder( tf.float32, name='x_in', shape=[self.H['image_height'], self.H['image_width'], 3]) #Tensorbox initialization pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = self.H['grid_height'] * self.H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) pred_boxes = pred_boxes + pred_boxes_deltas saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, checkpoint) for ix, i in enumerate(imgs[:10]): img = Image.open(i) img = img.resize( (self.H['image_width'], self.H['image_height'])) img = np.array(img) feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run( [pred_boxes, pred_confidences], feed_dict=feed) new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=0.7, show_suppressed=False) self.createnewjson(i, rects)
hypes_file = './hypes/lstm_rezoom.json' iteration = 10000 with open(hypes_file, 'r') as f: H = json.load(f) true_json = './new_labels/alb.json' pred_json = './output/%d_val_%s.json' % ( iteration, os.path.basename(hypes_file).replace('.json', '')) true_annos = al.parse(true_json) tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims( x_in, 0), 'test', reuse=None) saver = tf.train.Saver()
def still_image_TENSORBOX(idl_filename, frames_list,folder_path_det_frames,folder_path_det_result,folder_path_frames,path_video_folder,hypes_file,weights_file,pred_idl): print("Starting DET Phase") if not os.path.exists(path_video_folder+'/'+folder_path_det_frames): os.makedirs(path_video_folder+'/'+folder_path_det_frames) print("Created Folder: %s"%path_video_folder+'/'+folder_path_det_frames) if not os.path.exists(path_video_folder+'/'+folder_path_det_result): os.makedirs(path_video_folder+'/'+folder_path_det_result) print("Created Folder: %s"% path_video_folder+'/'+folder_path_det_result) det_frames_list=[] #### START TENSORBOX CODE ### ### Opening Hypes file for parameters with open(hypes_file, 'r') as f: H = json.load(f) ### Get Annotation List of all the image to test test_annos = al.parse(idl_filename) ### Building Network tf.reset_default_graph() googlenet = googlenet_load.init(H) x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['arch']['image_height'], H['arch']['image_width'], 3]) if H['arch']['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), googlenet, 'test', reuse=None) grid_area = H['arch']['grid_height'] * H['arch']['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['arch']['rnn_len'], 2])), [grid_area, H['arch']['rnn_len'], 2]) if H['arch']['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), googlenet, 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver.restore(sess, weights_file )##### Restore a Session of the Model to get weights and everything working annolist = al.AnnoList() import time; t = time.time() #### Starting Evaluating the images lenght=int(len(frames_list)) print("%d Frames to DET"%len(frames_list)) progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()]) for i in progress(range(0, len(frames_list)-1)): img = imread(frames_list[i]) feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() #pred_anno.imageName = test_anno.imageName new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes,H["arch"], use_stitching=True, rnn_len=H['arch']['rnn_len'], min_conf=0.5) pred_anno.rects = rects bb_img = Image.open(frames_list[i]) for bb_rect in rects: ################ Adding Rectangle ################### dr = ImageDraw.Draw(bb_img) cor = (bb_rect.x1,bb_rect.y1,bb_rect.x2 ,bb_rect.y2) # DA VERIFICARE Try_2 (x1,y1, x2,y2) cor = (bb_rect.left() ,bb_rect.right(),bb_rect.bottom(),bb_rect.top()) Try_1 dr.rectangle(cor, outline="red") bb_img_det_name = frames_list[i].replace(folder_path_frames,folder_path_det_frames) bb_img.save(bb_img_det_name) det_frames_list.append(bb_img_det_name) annolist.append(pred_anno) annolist.save(pred_idl) #### END TENSORBOX CODE ### return det_frames_list
def bbox_det_TENSORBOX_multiclass(frames_list,path_video_folder,hypes_file,weights_file,pred_idl): from train import build_forward print("Starting DET Phase") #### START TENSORBOX CODE ### lenght=int(len(frames_list)) video_info = [] ### Opening Hypes file for parameters with open(hypes_file, 'r') as f: H = json.load(f) ### Building Network tf.reset_default_graph() googlenet = googlenet_load.init(H) x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), googlenet, 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], H['num_classes']])), [grid_area, H['rnn_len'], H['num_classes']]) pred_logits = tf.reshape(tf.nn.softmax(tf.reshape(pred_logits, [grid_area * H['rnn_len'], H['num_classes']])), [grid_area, H['rnn_len'], H['num_classes']]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), googlenet, 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver.restore(sess, weights_file )##### Restore a Session of the Model to get weights and everything working #### Starting Evaluating the images print("%d Frames to DET"%len(frames_list)) progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()]) frameNr=0 skipped=0 for i in progress(range(0, len(frames_list))): current_frame = frame.Frame_Info() current_frame.frame=frameNr current_frame.filename=frames_list[i] if utils_image.isnotBlack(frames_list[i]) & utils_image.check_image_with_pil(frames_list[i]): img = imread(frames_list[i]) # test(frames_list[i]) feed = {x_in: img} (np_pred_boxes,np_pred_logits, np_pred_confidences) = sess.run([pred_boxes,pred_logits, pred_confidences], feed_dict=feed) _,rects = get_multiclass_rectangles(H, np_pred_confidences, np_pred_boxes, rnn_len=H['rnn_len']) if len(rects)>0: # pick = NMS(rects) pick = rects print len(rects),len(pick) current_frame.rects=pick frameNr=frameNr+1 video_info.insert(len(video_info), current_frame) print len(current_frame.rects) else: skipped=skipped+1 else: skipped=skipped+1 print("Skipped %d Black Frames"%skipped) #### END TENSORBOX CODE ### return video_info
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) p1_x_in = tf.placeholder(tf.float32, name='p1_x_in', shape=[H['image_height'], H['image_width'], 3]) p2_x_in = tf.placeholder(tf.float32, name='p2_x_in', shape=[H['image_height'], H['image_width'], 3]) p3_x_in = tf.placeholder(tf.float32, name='p3_x_in', shape=[H['image_height'], H['image_width'], 3]) p4_x_in = tf.placeholder(tf.float32, name='p4_x_in', shape=[H['image_height'], H['image_width'], 3]) p5_x_in = tf.placeholder(tf.float32, name='p5_x_in', shape=[H['image_height'], H['image_width'], 3]) p6_x_in = tf.placeholder(tf.float32, name='p6_x_in', shape=[H['image_height'], H['image_width'], 3]) p7_x_in = tf.placeholder(tf.float32, name='p7_x_in', shape=[H['image_height'], H['image_width'], 3]) p8_x_in = tf.placeholder(tf.float32, name='p8_x_in', shape=[H['image_height'], H['image_width'], 3]) f_x_in = tf.placeholder(tf.float32, name='f_x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), tf.expand_dims(p1_x_in, 0), tf.expand_dims(p2_x_in, 0), tf.expand_dims(p3_x_in, 0), tf.expand_dims(p4_x_in, 0), tf.expand_dims(p5_x_in, 0), tf.expand_dims(p6_x_in, 0), tf.expand_dims(p7_x_in, 0), tf.expand_dims(p8_x_in, 0), tf.expand_dims(f_x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() true_annolist = al.parse(args.test_boxes) data_dir = os.path.dirname(args.test_boxes) image_dir = get_image_dir(args) subprocess.call('mkdir -p %s' % image_dir, shell=True) count_error = list() for i in range(20): count_error.append(0) for i in range(len(true_annolist)): true_anno = true_annolist[i] orig_img = imread('%s/%s' % (data_dir, true_anno.imageName))[:, :, :3] dir_path = os.path.dirname(true_anno.imageName) file_name = true_anno.imageName.split('/')[-1] (shotname, extension) = os.path.splitext(file_name) p1_image_path = data_dir + "/" + dir_path + "/" + ( str(int(shotname) - 1)).zfill(4) + ".png" p2_image_path = data_dir + "/" + dir_path + "/" + ( str(int(shotname) - 2)).zfill(4) + ".png" p3_image_path = data_dir + "/" + dir_path + "/" + ( str(int(shotname) - 3)).zfill(4) + ".png" p4_image_path = data_dir + "/" + dir_path + "/" + ( str(int(shotname) - 4)).zfill(4) + ".png" p5_image_path = data_dir + "/" + dir_path + "/" + ( str(int(shotname) - 5)).zfill(4) + ".png" p6_image_path = data_dir + "/" + dir_path + "/" + ( str(int(shotname) - 6)).zfill(4) + ".png" p7_image_path = data_dir + "/" + dir_path + "/" + ( str(int(shotname) - 7)).zfill(4) + ".png" p8_image_path = data_dir + "/" + dir_path + "/" + ( str(int(shotname) - 8)).zfill(4) + ".png" f_image_path = data_dir + "/" + dir_path + "/" + ( str(int(shotname) + 1)).zfill(4) + ".png" if not os.path.exists(p1_image_path): print "File not exists: %s" % p1_image_path exit() if not os.path.exists(p2_image_path): print "File not exists: %s" % p2_image_path exit() if not os.path.exists(f_image_path): print "File not exists: %s" % f_image_path exit() p1_img = imread(p1_image_path) p2_img = imread(p2_image_path) p3_img = imread(p3_image_path) p4_img = imread(p4_image_path) p5_img = imread(p5_image_path) p6_img = imread(p6_image_path) p7_img = imread(p7_image_path) p8_img = imread(p8_image_path) f_img = imread(f_image_path) img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = { x_in: img, p1_x_in: p1_img, p2_x_in: p2_img, p3_x_in: p3_img, p4_x_in: p4_img, p5_x_in: p5_img, p6_x_in: p6_img, p7_x_in: p7_img, p8_x_in: p8_img, f_x_in: f_img } (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName true_count = len(true_anno.rects) # print true_count for j in range(20): min_confidence = (j * 1.0) / 20.0 new_img, rects, count = add_rectangles( H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=min_confidence, tau=args.tau, show_suppressed=args.show_suppressed) count_error[j] += abs(count - true_count) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) # imname = '%s/%s' % (image_dir, os.path.basename(true_anno.imageName)) # misc.imsave(imname, new_img) if i % 25 == 0: print(i) print "Count error: %s" % str(min(count_error) / 350.0) return pred_annolist, true_annolist
def bbox_det_TENSORBOX_multiclass(frames_list, path_video_folder, hypes_file, weights_file, pred_idl): from train import build_forward print("Starting DET Phase") #### START TENSORBOX CODE ### lenght = int(len(frames_list)) video_info = [] ### Opening Hypes file for parameters with open(hypes_file, 'r') as f: H = json.load(f) ### Building Network tf.reset_default_graph() googlenet = googlenet_load.init(H) x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), googlenet, 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], H['num_classes']])), [grid_area, H['rnn_len'], H['num_classes']]) pred_logits = tf.reshape( tf.nn.softmax( tf.reshape(pred_logits, [grid_area * H['rnn_len'], H['num_classes']])), [grid_area, H['rnn_len'], H['num_classes']]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), googlenet, 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver.restore( sess, weights_file ) ##### Restore a Session of the Model to get weights and everything working #### Starting Evaluating the images print(("%d Frames to DET" % len(frames_list))) progress = progressbar.ProgressBar(widgets=[ progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage(), ' ', progressbar.ETA() ]) frameNr = 0 skipped = 0 for i in progress(list(range(0, len(frames_list)))): current_frame = frame.Frame_Info() current_frame.frame = frameNr current_frame.filename = frames_list[i] if utils_image.isnotBlack( frames_list[i]) & utils_image.check_image_with_pil( frames_list[i]): img = imread(frames_list[i]) # test(frames_list[i]) feed = {x_in: img} (np_pred_boxes, np_pred_logits, np_pred_confidences) = sess.run( [pred_boxes, pred_logits, pred_confidences], feed_dict=feed) _, rects = get_multiclass_rectangles(H, np_pred_confidences, np_pred_boxes, rnn_len=H['rnn_len']) if len(rects) > 0: # pick = NMS(rects) pick = rects print((len(rects), len(pick))) current_frame.rects = pick frameNr = frameNr + 1 video_info.insert(len(video_info), current_frame) print((len(current_frame.rects))) else: skipped = skipped + 1 else: skipped = skipped + 1 print(("Skipped %d Black Frames" % skipped)) #### END TENSORBOX CODE ### return video_info
def tb_predict(self, imgs): # rest graph tf.reset_default_graph() # input placeholder x_in = tf.placeholder( tf.float32, name='x_in', shape=[self.H['image_height'], self.H['image_width'], 3]) #Tensorbox initialization pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( self.H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = self.H['grid_height'] * self.H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * self.H['rnn_len'], 2])), [grid_area, self.H['rnn_len'], 2]) pred_boxes = pred_boxes + pred_boxes_deltas #saver saver = tf.train.Saver() # predicted images pred_imgs = [] # predicted rects pred_rects = [] with tf.Session() as sess: # init vars sess.run(tf.global_variables_initializer()) #restore checkpoint saver.restore(sess, self.checkpoint) for ix, i in enumerate(imgs): print 'predicting box for image: ', i #resize for ratio img = resizeImageToRatio(i) #resize for Tensorbox img_r = img.resize( (self.H['image_width'], self.H['image_height'])) # convert to np.array img_r = np.array(img_r) # feed dict feed = {x_in: img_r} # run Tensorbox (np_pred_boxes, np_pred_confidences) = sess.run( [pred_boxes, pred_confidences], feed_dict=feed) # stitch rect, create image with rect img_n, rects = add_rectangles(self.H, [img_r], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=self.H['rnn_len'], min_conf=0.7, show_suppressed=False) pred_imgs.append(img_n) pred_rects.append((i, img, rects)) return pred_imgs, pred_rects
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() data_dir = os.path.dirname(args.test_boxes) image_dir = get_image_dir(args) subprocess.call('mkdir -p %s' % image_dir, shell=True) memory = sysv_ipc.SharedMemory(123463) memory2 = sysv_ipc.SharedMemory(123464) size = 768, 1024, 3 pedal = PyMouse() pedal.press(1) road_center = 320 while True: cv2.waitKey(1) frameCount = bytearray(memory.read()) curve = bytearray(memory2.read()) curve = str(struct.unpack('i',curve)[0]) m = np.array(frameCount, dtype=np.uint8) orig_img = m.reshape(size) img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) flag = 0 road_center = 320 + int(curve) print(road_center) for rect in rects: print(rect.x1, rect.x2, rect.y2) if (rect.x1 < road_center and rect.x2 > road_center and rect.y2 > 200) and (rect.x2 - rect.x1 > 30): flag = 1 if flag is 1: pedal.press(2) print("break!") else: pedal.release(2) pedal.press(1) print("acceleration!") pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) cv2.imshow('.jpg', new_img) return none;
def main(args, logger): # setup logger.info(args) os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = args.gpu_fraction # path path_hypes_file = '{}/hypes.json'.format(os.path.dirname(args.weights)) with open(path_hypes_file, 'r') as f: H = json.load(f) expname = args.expname + '_' if args.expname else '' # graph tf.reset_default_graph() H['grid_width'] = H['image_width'] / H['region_size'] H['grid_height'] = H['image_height'] / H['region_size'] X = tf.placeholder(tf.float32, name='input', shape=(H['image_height'], H['image_width'], 3)) if H['use_rezoom']: (pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas) = build_forward(H, tf.expand_dims(X, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] reshape_shape = [grid_area * H['rnn_len'], 2] pred_confidences = tf.reshape( tf.nn.softmax(tf.reshape(pred_confs_deltas, reshape_shape)), reshape_shape) pred_boxes = pred_boxes + pred_boxes_deltas if H[ 'reregress'] else pred_boxes else: (pred_boxes, pred_logits, pred_confidences) = build_forward(H, tf.expand_dims(X, 0), 'test', reuse=None) # load checkopint saver = tf.train.Saver() with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() # get all video candidate video_paths = glob( os.path.join(args.video_root, '*.{}'.format(args.video_type))) for v in video_paths: video_fullname = '.'.join(v.split('.')[:-1]) video_name = video_fullname.split('/')[-1] txtname = video_fullname + '_detection.txt' txtname = '/'.join( [args.outputdir, video_name, txtname.split('/')[-1]]) if os.path.isfile(txtname): logger.info('{} existed, pass'.format(txtname)) continue if not os.path.exists(os.path.dirname(txtname)): os.makedirs(os.path.dirname(txtname)) logger.info('Predicting {}'.format(os.path.basename(v))) # video operation cap = cv2.VideoCapture(v) total_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) fourcc = cv2.VideoWriter_fourcc(*'XVID') resolution = tuple(map(int, (cap.get(3), cap.get(4)))) filename = 'detected_{}'.format(os.path.basename(v)) # output video if args.output_video: outputdir = os.path.join( args.outputdir, '{}-skip-{}-count-{}'.format( datetime.now().strftime('%Y%m%d'), args.skip_nframe, args.frame_count or 'all')) if not os.path.exists(outputdir): os.makedirs(outputdir) out = cv2.VideoWriter(os.path.join(outputdir, filename), fourcc, 15, resolution) data = [] logger.info('total {} skip {}'.format(total_frame, args.skip_nframe)) for frame_idx in tqdm(range(0, total_frame, args.skip_nframe)): if args.frame_count and len(data) > args.frame_count: break if not cap.isOpened(): logger.error('{} is close'.format(os.path.basename(v))) ok, frame = cap.read() if ok: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = cv2.resize(frame, (H['image_width'], H['image_height'])) (np_pred_boxes, np_pred_confidences) = sess.run( [pred_boxes, pred_confidences], feed_dict={X: image}) pred_anno = al.Annotation() new_img, rects = add_rectangles( H, [image], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.suppressed) pred_anno.rects = rects pred_anno = rescale_boxes( (H["image_height"], H["image_width"]), pred_anno, frame.shape[0], frame.shape[1]) results = [] for r in pred_anno.rects: results.append([ max(r.y1, 0), max(r.x1, 0), max(r.y2, 0), max(r.x2, 0), r.score ]) data.append(str([frame_idx + 1, results]) + '\n') pred_annolist.append(pred_anno) if args.output_video: out.write(new_img) else: logger.warning('cannot read frame {}'.format(frame_idx)) cap.release() if args.output_video: out.release() with open(txtname, 'w+') as f: f.writelines(data)
def get_results(args, H): tf.reset_default_graph() H["grid_width"] = H["image_width"] / H["region_size"] H["grid_height"] = H["image_height"] / H["region_size"] x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() true_annolist = al.parse(args.test_boxes) data_dir = os.path.dirname(args.test_boxes) image_dir = get_image_dir(args) subprocess.call('mkdir -p %s' % image_dir, shell=True) for i in range(len(true_annolist)): true_anno = true_annolist[i] orig_img = cv2.imread(os.path.join(data_dir, true_anno.imageName))[:, :, :3] img = cv2.resize(orig_img, (H["image_width"], H["image_height"]), interpolation=cv2.INTER_CUBIC) feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles( H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) imname = os.path.join(image_dir, os.path.basename(true_anno.imageName)) cv2.imwrite(imname, new_img) if i % 25 == 0: print(i) return pred_annolist, true_annolist
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() data_dir = os.path.dirname(args.test_boxes) image_dir = get_image_dir(args) subprocess.call('mkdir -p %s' % image_dir, shell=True) memory = sysv_ipc.SharedMemory(123463) memory2 = sysv_ipc.SharedMemory(123464) size = 768, 1024, 3 pedal = PyMouse() pedal.press(1) road_center = 320 while True: cv2.waitKey(1) frameCount = bytearray(memory.read()) curve = bytearray(memory2.read()) curve = str(struct.unpack('i', curve)[0]) m = np.array(frameCount, dtype=np.uint8) orig_img = m.reshape(size) img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() new_img, rects = add_rectangles( H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) flag = 0 road_center = 320 + int(curve) print(road_center) for rect in rects: print(rect.x1, rect.x2, rect.y2) if (rect.x1 < road_center and rect.x2 > road_center and rect.y2 > 200) and (rect.x2 - rect.x1 > 30): flag = 1 if flag is 1: pedal.press(2) print("break!") else: pedal.release(2) pedal.press(1) print("acceleration!") pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) cv2.imshow('.jpg', new_img) return none
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() true_annolist = al.parse(args.test_boxes) data_dir = os.path.dirname(args.test_boxes) image_dir = get_image_dir(args) #subprocess.call('mkdir -p %s' % image_dir, shell=True) #ivc = cv2.VideoCapture('/home/caucse/images/ets.mp4') #c=1 #if vc.isOpened(): # rval , frame = vc.read() #else: # rval = False memory = sysv_ipc.SharedMemory(123463) memory2 = sysv_ipc.SharedMemory(123464) size = 768, 1024, 3 pedal = PyMouse() pedal.press(1) road_center = 320 while True: #rval, frame = vc.read() #c = c + 1 #if c % 6 is 0: # c = c + 1 #time.sleep(0.5) cv2.waitKey(1) frameCount = bytearray(memory.read()) curve = bytearray(memory2.read()) curve = str(struct.unpack('i',curve)[0]) m = np.array(frameCount, dtype=np.uint8) orig_img = m.reshape(size) #print orig_img[0] #cv2.imshow('1', m) #true_anno = true_annolist[i] #orig_img = imread('%s/%s' % (data_dir, true_anno.imageName))[:,:,:3] #orig_img = imread('/home/caucse/images/1.jpg') #orig_img = m img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() #pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) flag = 0 road_center = 320 + int(curve) print(road_center) for rect in rects: print(rect.x1, rect.x2, rect.y2) if (rect.x1 < road_center and rect.x2 > road_center and rect.y2 > 200) and (rect.x2 - rect.x1 > 30): flag = 1 if flag is 1: pedal.press(2) print("break!") else: pedal.release(2) pedal.press(1) print("acceleration!") pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) #imname = '%s/%s' % (image_dir, os.path.basename(true_anno.imageName)) #imname = '/home/caucse/images/_%s.jpg' % (c) cv2.imshow('.jpg', new_img) #misc.imsave(imname, new_img) #if c % 25 == 0: #print(c) for i in range(len(true_annolist)): true_anno = true_annolist[i] #orig_img = imread('%s/%s' % (data_dir, true_anno.imageName))[:,:,:3] orig_img = imread('/home/caucse/images/1.jpg') img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) for rect in rects: print(rect.x1, rect.y1, rect.x2, rect.y2) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) #imname = '%s/%s' % (image_dir, os.path.basename(true_anno.imageName)) imname = '/home/caucse/images/_1.jpg' misc.imsave(imname, new_img) if i % 25 == 0: print(i) return pred_annolist, true_annolist