def predict(sess, image_file): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. """ import os from matplotlib.pyplot import imshow import scipy.misc from keras import backend as K from misc import read_classes, read_anchors, generate_colors, preprocess_image, draw_boxes, scale_boxes from yolo_filter_boxes import yolo_filter_boxes # Preprocess your image image, image_data = preprocess_image("images/" + image_file, model_image_size=(608, 608)) # Run the session with the correct tensors and choose the correct placeholders in the feed_dict. # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0}) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("out", image_file), quality=90) # Display the results in the notebook output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def build_model(self, data, is_training=True): raw_aerial, raw_ground, label_ground = data self.image_aerial = misc.preprocess_image(raw_aerial, self.szs.image_aerial) self.image_ground = misc.preprocess_image(raw_ground, self.szs.image_ground) self.prob_ground = misc.preprocess_label(label_ground, self.num_classes, self.szs.after_transf) self.im_aerial = misc.proprocess_image(self.image_aerial) self.im_ground = misc.proprocess_image(self.image_ground) self.feat_aerial = models.pixelnet(self.image_aerial, self.num_classes, is_training=is_training, batch_norm=self.batch_norm) misc.pprint( self.feat_aerial.get_shape().as_list()) # print the feature size if is_training: feat_aerial_small = self.feat_aerial else: feat_aerial_small = tf.image.resize_bilinear( self.feat_aerial, self.szs.before_transf) weights = models.compute_transfweights(self.szs.before_transf, self.szs.after_transf, self.conditioned, is_training=is_training, batch_norm=self.batch_norm) self.feat_aerial2ground = models.transfnet(feat_aerial_small, weights, self.szs.after_transf) if is_training: self.merged = tf.summary.merge_all() self.summarizer = tf.summary.FileWriter(self.log_dir, self.sess.graph) with tf.name_scope("Loss"): self.loss_class = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits( None, self.prob_ground, self.feat_aerial2ground)) self.loss_reg = tf.add_n( tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) self.loss = self.loss_class + self.loss_reg with tf.name_scope("Optimizer"): with tf.control_dependencies( tf.get_collection(tf.GraphKeys.UPDATE_OPS)): self.step = tf.Variable(0, name='global_step', trainable=False) self.optim = tf.train.AdamOptimizer( tf.train.exponential_decay( 0.001, self.step, 5000, .7, staircase=True ) # not sure if this is necessary for Adam optimizer ).minimize(self.loss, global_step=self.step) self.saver = tf.train.Saver(max_to_keep=10, write_version=tf.train.SaverDef.V2) misc.pprint("[*] build model.") self.transfweights, self.transfbiases = tf.get_collection( 'transformer_weights') self.prob_aerial = tf.nn.softmax(self.feat_aerial) self.prob_aerial2ground = tf.nn.softmax(self.feat_aerial2ground) with tf.name_scope('Vis'): self.visual = [ \ self.image_aerial, self.image_ground, tf.cast(self.prob_aerial, tf.float32)/self.num_classes, tf.cast(self.prob_ground, tf.float32)/self.num_classes, tf.cast(self.prob_aerial2ground, tf.float32)/self.num_classes, self.transfweights]
import cv2 # # This code is only for illustrating how the method works. # # We DO NOT advise to use this code for inference. Instead, use the code # in github.com/sariyanidi/face_alignment_opencv, which is maintained # more frequently and has more flexibility (works for images/videos/image dirs) # and includes face detection etc. (The lines below assume that the # face is cropped appropriately) # # # The model file, readable by OpenCV. You need to creat this file by # running python freeze.py landmark_net = cv2.dnn.readNetFromTensorflow('models/model_FAN_frozen.pb') image_path = 'samples/test.png' # read image im_orig = cv2.imread(image_path) (im, pt_center, scale) = preprocess_image(im_orig, 67, 67, 181, 213) p = get_landmarks(im, landmark_net, pt_center, scale) for ip in range(p.shape[0]): cv2.circle(im_orig, (p[ip, 0], p[ip, 1]), 1, (0, 255, 0), -2) cv2.imshow("Result", im_orig) cv2.waitKey(0) cv2.destroyAllWindows()