def make_prediction(image_path, input_image_name, yolo_model): #Obtaining the dimensions of the input image input_image = Image.open(image_path + input_image_name) width, height = input_image.size width = np.array(width, dtype=float) height = np.array(height, dtype=float) #Assign the shape of the input image to image_shapr variable image_shape = (height, width) #Loading the classes and the anchor boxes that are provided in the model_data folder class_names = read_classes("model_data/yolo_classes.txt") anchors = read_anchors("model_data/yolo_anchors.txt") #Print the summery of the model yolo_model.summary() #Convert final layer features to bounding box parameters yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) #Now yolo_eval function selects the best boxes using filtering and non-max suppression techniques. # If you want to dive in more to see how this works, refer keras_yolo.py file in yad2k/models boxes, scores, classes = yolo_eval(yolo_outputs, image_shape) # Initiate a session sess = K.get_session() #Preprocess the input image before feeding into the convolutional network image, image_data = preprocess_image(image_path + input_image_name, model_image_size = (608, 608)) #Run the session out_scores, out_boxes, out_classes = sess.run( [scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 } ) #Print the results print('Found {} boxes for {}'.format(len(out_boxes), input_image_name)) #Produce the colors for the bounding boxs colors = generate_colors(class_names) #Draw the bounding boxes draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) #Apply the predicted bounding boxes to the image and save it image.save("predictions/" + input_image_name, quality=90) if(len(out_classes) == 0): result = "No box found" elif (out_classes[0] == 0): result = "real" else: result = "fake" return input_image_name, out_scores, result
def __init__(self): self.model = load_model("model_data/yolo.h5") self.class_names = read_classes("model_data/coco_classes.txt") self.anchors = read_anchors("model_data/yolo_anchors.txt") self._create_placeholders() self._evaluate_output() self.scores, self.boxes, self.classes = self._yolo_eval( self.outputs, (720., 1280.))
def test(): sess = K.get_session() class_names = read_classes("model_data/coco_classes.txt") anchors = read_anchors("model_data/yolo_anchors.txt") image_shape = (720., 1280.) yolo_model = load_model("model_data/yolo.h5") yolo_model.summary() yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) 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. """ # 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 out_scores, out_boxes, out_classes = predict(sess, "0018.jpg")
def main(): sess = K.get_session() class_names = read_classes("model_data/coco_classes.txt") anchors = read_anchors("model_data/yolo_anchors.txt") image_shape = (480., 640.) yolo_model = load_model("model_data/yolo.h5") yolo_model.summary() yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) video = cv2.VideoCapture(0) width = video.get(3) # float height = video.get(4) # float print(width, " x ", height) #prepare_yolo_eval(width, height, yolo_outputs) try: while (True): # Read the next frame ret, cv2_im = video.read() k = cv2.waitKey(1) if not ret or k == 27: # press ESC to exit video.release() cv2.destroyAllWindows() print("Released Video Resource") break cv2_im = cv2.cvtColor(cv2_im, cv2.COLOR_BGR2RGB) pil_im = Image.fromarray(cv2_im) predictFromImg(sess, pil_im, scores, boxes, classes, yolo_model, class_names) cv2_im = np.array(pil_im) cv2_im = cv2_im[:, :, ::-1].copy() cv2.imshow("Webcam", cv2_im) except: video.release() cv2.destroyAllWindows() exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(exc_type, fname, exc_obj, exc_tb.tb_lineno) print("Released Video Resource")
def load_model_and_infer(sess, image_file): """ Load the model as a graph in sesssion and Run it 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. """ # Load and preprocess image for inference image, image_data, image_size = preprocess_image("images/" + image_file, model_image_size = (608, 608)) # Define classes, anchors and image shape. class_names = read_classes("model/coco_classes.txt") anchors = read_anchors("model/yolo_anchors.txt") image_shape = (float(image_size[1]),float(image_size[0])) # Load the pretrained model. yolo_model = load_model("model/yolo.h5") # print a summary of the layer's model. yolo_model.summary() # Convert output of the model to bounding box tensors. yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) # Filter boxes. scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) # Infer predictions on the image. 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("output", image_file), quality=90) # Display the results in the notebook output_image = scipy.misc.imread(os.path.join("output", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def main(): with tf.Session() as sess: class_names = read_classes("coco_classes.txt") anchors = read_anchors("yolo_anchors.txt") image_shape = (720., 1280.) yolo_model = load_model("pretrained/yolo.h5") yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) out_scores, out_boxes, out_classes = predict(sess, "giraffe.jpg", scores, boxes, classes, yolo_model, class_names)
def yolo_model_config(input_image_shape, score_threshold, iou_threshold, debug=False): """ This function is used to load the model and get the relative parameters in the model Arguments: input_image_shape (tf placeholder shape: (2, ) dtype: int32): The shape of the image in the form of [frame.size[1], frame.size[0]] score_threshold (float): The score threshold for the maximun score of the output class of each region iou_threshold (float): The threshold of intersction of union in the Non-max suppression debug (bool): The debug flag for me to debug and peek the data and model Return: boxes (tf tensor shape: (2, ) dtype: int32): The tensor ot keep the output boxes after the detection of yolo model scores (tf tensor shape: (2, ) dtype: int32): The tensor ot keep the output scores for each boxes after the detection of yolo model classes (tf tensor shape: (2, ) dtype: int32): The tensor ot keep the output classes for each boxes after the detection of yolo model yolo_model (keras model): The pretrained keras yolo model model_image_size (tuple dtype: int): The input size for the pretrained yolo model class_names (list dtype: string): The class_names of coco datasets """ #Load pretrained model and parameters from yad2k class_names = read_classes("yad2k/model_data/coco_classes.txt") anchors = read_anchors("yad2k/model_data/yolov2_anchors.txt") yolo_model = load_model("yad2k/model_data/yolov2.h5") model_image_size = yolo_model.layers[0].input_shape[1:3] if debug: print('The class names is ', class_names) print('The acnchors are ', anchors) print('model_image_size=', model_image_size) yolo_model.summary() #Get the output boxes, scores, classes tensor to keep the result yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) boxes, scores, classes = yolo_eval(yolo_outputs, input_image_shape, score_threshold=score_threshold, iou_threshold=iou_threshold) return boxes, scores, classes, yolo_model, model_image_size, class_names
def predict_objects(self, img): # t0 = time.time() #self.object_model.summary() c = np.fromstring(bytes(img.data), np.uint8) img = cv2.imdecode(c, cv2.IMREAD_COLOR) #im_pil img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) im_pil = Image.fromarray(img) # weight height of the image_sub width, height = im_pil.size print(width) print(height) width = np.array(width, dtype=float) height = np.array(height, dtype=float) image_shape = (height, width) t0 = time.time() #class names and anchors class_names = read_classes("/lanefollowing/ros2_ws/src/lane_following/model/coco_classes.txt") anchors = read_anchors("/lanefollowing/ros2_ws/src/lane_following/model/yolov2-tiny_anchors.txt") #yolo head yolo_outputs = yolo_head(self.object_model.output, anchors, len(class_names)) # yolo eval boxes, scores, classes = yolo_eval(yolo_outputs, image_shape) # print('boxes') # print(boxes) #yolo preprocess # pil_img, img_data = preprocess_image_object(im_pil, model_image_size = (416, 416)) pil_img, img_data = preprocess_image_object(im_pil, model_image_size = (608, 608)) #yolo predict # print('test1') out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes],feed_dict={self.object_model.input:img_data,K.learning_phase(): 0}) t1 = time.time() self.inference_time = t1 - t0 # print("score = " ) # print(out_scores) # print("\n") # print('test2') print('Found {} boxes for '.format(len(out_boxes))) colors = generate_colors(class_names) draw_boxes(pil_img, out_scores, out_boxes, out_classes, class_names, colors) #might need to add resizing here... image = np.asarray(pil_img) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) cv2.putText(image, "Prediction time: %d ms" % (self.inference_time * 1000), (30, 220), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 2) cv2.putText(image, "Frame speed: %d fps" % (self.fps), (30, 270), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 2) image = cv2.resize(image, (round(image.shape[1] / 2), round(image.shape[0] / 2)), interpolation=cv2.INTER_AREA) cv2.imshow('YOLO Detector', image) cv2.waitKey(1)
def predict(sess, image): """ 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. """ class_names = read_classes("model_data/coco_classes.txt") anchors = read_anchors("model_data/yolo_anchors.txt") image_shape = (720., 1280.) yolo_model = load_model("model_data/yolo.h5") # yolo_model.summary() t = time.time() yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) print("t1:", time.time()-t) t = time.time() scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) print("t2:", time.time() - t) # Preprocess your image image_data = preprocess_image(image, model_image_size=(608, 608)) t = time.time() # Run the session with the correct tensors and choose the correct placeholders in the feed_dict. out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={yolo_model.input: image_data, K.learning_phase(): 0}) print("t3:", time.time() - t) # Print predictions info #print('Found {} boxes'.format(len(out_boxes))) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image result = draw_boxes(image, out_boxes, out_classes, colors) return result
def predict(model, image_file, is_show_info=True, is_plot=True): """ 运行model并输出预测的图和信息 由于练习的使用的时tf2.3版本,代码和博客中的tf1有很大区别,所以和部分想了半天,也算是从头梳理了一遍 注意:博客中没有model这个参数,这里加上是因为模型在函数里加载的话,运行批量绘图时会由于多次加载模型导致显存报错。 Args: model: 用来预测锚框的模型 image_file: images文件夹的图片名称 Returns: out_scores: tensor,维度为(None,)锚框预测的可能值 out_boxes: tensor,维度为(None,4)预测的锚框的位置 out_classes: tensor, 维度为(None,)预测的锚框的分类索引 """ dir = './data/Yolo/yolo_model/model_data/' class_name = yolo_utils.read_classes(dir + 'coco_classes.txt') anchors = yolo_utils.read_anchors(dir + 'yolo_anchors.txt') image_shape = (720., 1280.) # 处理图像,image_data为图像转换为tensor后的数据 image, image_data = yolo_utils.preprocess_image( './data/Yolo/images/' + image_file, model_image_size=(608, 608)) # 预测图像,结果为(1,19,19,425)最后的维度为5个锚框x85个属性 yolo_model_output = model.predict(image_data) # yolo_head将yolo模型的输出进行转换为各个格子中每个锚框的 (坐标、宽高、预测值、分类值) # 原文中yolo_head的输出顺序有误,会导致yolo_eval函数报错,在此已经将yolo_head的输出顺序修改 yolo_outputs = yolo_utils.yolo_head(yolo_model_output, anchors, len(class_name)) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) # 打印预测信息 if is_show_info: print("在" + str(image_file) + "中找到了" + str(len(boxes)) + "个锚框。") # 指定绘制边框的颜色 colors = yolo_utils.generate_colors(class_name) # 绘制边界并保存图片 yolo_utils.draw_boxes(image, scores, boxes, classes, class_name, colors) image.save('./data/Yolo/out/' + image_file, quality=100) # 打印出已经绘制了边界框的图 if is_plot: output_image = plt.imread('./data/Yolo/out/' + image_file) plt.imshow(output_image) plt.show() return scores, boxes, classes
def __init__(self): self.COCO = False self.trainable = True args1 = sys.argv[2] if args1=='COCO': print("-----------COCO-----------") self.COCO = True self.classes_path = path + '/model/coco_classes.txt' self.trainable = False elif args1=='VOC': print("-----------VOC------------") self.classes_path = path + '/model/voc_classes.txt' elif args1=='bdd': print("-----------bdd-----------") self.classes_path = path + '/model/bdd_classes.txt' self.anchors_path = path + '/model/bdd_anchors.txt' elif args1=='kitti': print("-----------kitti-----------") self.classes_path = path + '/model/kitti_classes.txt' self.anchors_path = path + '/model/kitti_anchors.txt' # args = self.argument() # if args.COCO: # print("-----------COCO-----------") # self.COCO = True # self.classes_path = self.PATH + '/model/coco_classes.txt' # self.trainable = False # elif args.VOC: # print("-----------VOC------------") # self.classes_path = self.PATH + '/model/voc_classes.txt' # elif args.bdd: # print("-----------bdd-----------") # self.classes_path = self.PATH + '/model/bdd_classes.txt' self.class_names = read_classes(self.classes_path) self.anchors = read_anchors(self.anchors_path) self.threshold = 0.5# threshold self.ignore_thresh = ignore_thresh self.INPUT_SIZE = (Input_height, Input_width) # fixed size or (None, None) self.is_fixed_size = self.INPUT_SIZE != (None, None) # LOADING SESSION... # for 3D self.boxes, self.scores, self.classes, self.alphas, self.hwls, self.sess = self.load()
def main(): # Load a pre-trained YOLO model yolo_model = load_model("local_data/yolo/yolo.h5") # Define classes, anchors and image shapes class_names = read_classes("local_data/yolo/coco_classes.txt") anchors = read_anchors("local_data/yolo/yolo_anchors.txt") # Image info input_dir = 'images_in/' output_dir = 'images_out/' image_shape = (720, 1280) # original image shape # Loop over all images and generated new ones # Decrease score_th to lower the threshold for an object to be detected for id in np.arange(120): # 120 images in total image_name=str(id+1).zfill(4) +".jpg" predict_image(yolo_model, class_names, anchors, image_shape, input_dir, output_dir, image_name, score_th=0.4)
def __init__(self): self.anchors_path = path + '/model/yolo_anchors.txt' self.COCO = False self.trainable = True args1 = sys.argv[2] if args1=='COCO': print("-----------COCO-----------") self.COCO = True self.classes_path = path + '/model/coco_classes.txt' self.trainable = False elif args1=='VOC': print("-----------VOC------------") self.classes_path = path + '/model/voc_classes.txt' elif args1=='boat': print("-----------boat-----------") self.classes_path = path + '/model/boat_classes.txt' # args = self.argument() # if args.COCO: # print("-----------COCO-----------") # self.COCO = True # self.classes_path = self.PATH + '/model/coco_classes.txt' # self.trainable = False # elif args.VOC: # print("-----------VOC------------") # self.classes_path = self.PATH + '/model/voc_classes.txt' # elif args.boat: # print("-----------boat-----------") # self.classes_path = self.PATH + '/model/boat_classes.txt' self.class_names = read_classes(self.classes_path) self.anchors = read_anchors(self.anchors_path) self.threshold = 0.5# threshold self.ignore_thresh = ignore_thresh self.INPUT_SIZE = (Input_shape, Input_shape) # fixed size or (None, None) self.is_fixed_size = self.INPUT_SIZE != (None, None) # LOADING SESSION... self.boxes, self.scores, self.classes, self.sess = self.load()
def main(): if DO_TEST: test() sess = K.get_session() class_names = read_classes("model_data/coco_classes.txt") anchors = read_anchors("model_data/yolo_anchors.txt") image_shape = (720., 1280.) yolo_model = load_model("model_data/yolo.h5") yolo_model.summary() # The output of yolo_model is a (m, 19, 19, 5, 85) tensor that needs to pass through non-trivial processing and # conversion. The following does that yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) # yolo_outputs gave you all the predicted boxes of yolo_model in the correct format. You're now ready to perform # filtering and select only the best boxes. Lets now call yolo_eval, which you had previously implemented, to do this. scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) out_scores, out_boxes, out_classes = predict(sess, "0099.jpg", yolo_model, scores, boxes, classes, class_names) out_scores, out_boxes, out_classes = predict(sess, "test.jpg", yolo_model, scores, boxes, classes, class_names)
scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes, box_class_probs, score_threshold) boxes = scale_boxes(boxes, image_shape) scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes) return scores, boxes, classes sess = K.get_session() class_names = read_classes("assets/coco_classes.txt") anchors = read_anchors("assets/yolo_anchors.txt") image_shape = (600., 900.) yolo_model = load_model("assets/yolo.h5") yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) 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
import argparse import os import matplotlib.pyplot as plt from matplotlib.pyplot import imshow import scipy.io import scipy.misc import numpy as np import pandas as pd import PIL import tensorflow as tf from keras import backend as K from keras.layers import Input, Lambda, Conv2D from keras.models import load_model, Model from yolo_utils import read_classes, read_anchors, generate_colors, preprocess_image, draw_boxes, scale_boxes from yad2k.models.keras_yolo import yolo_head, yolo_boxes_to_corners, preprocess_true_boxes, yolo_loss, yolo_body sess = K.get_session() class_names = read_classes("model_data/coco_classes.txt") anchors = read_anchors("model_data/yolo_anchors.txt") image_shape = (720., 1280.) yolo_model = load_model("model_data/yolo.h5")
def _main(args): def predict(sess, image): # Preprocess your image image, image_data = preprocess_image(image, model_image_size=(416, 416)) # 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'.format(len(out_boxes))) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file out_image = draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) return out_image, out_scores, out_boxes, out_classes video_path = os.path.join(INPUT_PATH, args.input) input = cv2.VideoCapture(video_path) if (input.isOpened() == False): print("Error opening video file.") return height = input.get(cv2.CAP_PROP_FRAME_HEIGHT) width = input.get(cv2.CAP_PROP_FRAME_WIDTH) fps = input.get(cv2.CAP_PROP_FPS) if not os.path.exists(OUTPUT_PATH): os.mkdir(OUTPUT_PATH) output_file_path = os.path.join(OUTPUT_PATH, args.input) output = cv2.VideoWriter(output_file_path, cv2.VideoWriter_fourcc(*'MP4V'), fps, (int(width), int(height))) image_shape = (height, width) sess = K.get_session() class_names = read_classes(CLASS_LIST_PATH) anchors = read_anchors(YOLO_ANCHORS_PATH) yolo_model, model = create_model(anchors, class_names) yolo_model.load_weights(args.weights) yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) boxes, scores, classes = yolo_eval(yolo_outputs, image_shape, score_threshold=args.score_threshold, iou_threshold=args.iou_threshold) n_frame = 0 while input.isOpened(): ret, original_frame = input.read() if ret: n_frame = n_frame + 1 print("processing frame {} ...".format(n_frame)) process_frame = cv2.cvtColor(original_frame, cv2.COLOR_BGR2RGB) process_frame = Image.fromarray(process_frame) process_frame, out_scores, out_boxes, out_classes = predict( sess, process_frame) process_frame = cv2.cvtColor(process_frame, cv2.COLOR_BGR2RGB) output.write(process_frame) else: break print("Finished. Output file: ", output_file_path) input.release() output.release() cv2.destroyAllWindows()
display = np.array(res) cv.imshow("Yolo", display) out.write(display) # 等待30ms显示图像,若过程中按“Esc”退出 c = cv.waitKey(30) & 0xff if c == 27: capture.release() break # yolo = YOLO() class_names = yolo_utils.read_classes(current_path + "/model_data/voc_classes.txt") anchors = yolo_utils.read_anchors(current_path + "/model_data/yolo_anchors.txt") num_classes = len(class_names) num_anchors = len(anchors) input_shape = (416, 416) image_input = Input(shape=(None, None, 3)) h, w = input_shape model = yolo_body(image_input, num_anchors // 3, num_classes) model.load_weights("logs/ep055-loss18.931-val_loss20.760.h5", by_name=True, skip_mismatch=True) model.summary() model.compile(optimizer='Adam', loss={ 'yolo_loss': lambda y_true, y_pred: y_pred }, metrics=['accuracy'])
if __name__ == '__main__': sess = K.get_session() #########################Change setting here##################################################### fps = 5 yolo_type = "regular" #yolo_type = "tiny" is_Video = True fileName = "storeCounter720" is_Capture = False is_ShowModSum = False ################################################################################################## class_names = read_classes("./model_data/coco_classes.txt") if yolo_type == "regular": anchors = read_anchors("./model_data/yolo_anchors.txt") yolo_model = load_model("./model_data/yolo.h5", compile=False) else: anchors = read_anchors("./model_data/tiny-yolo_anchors.txt") yolo_model = load_model("./model_data/tiny-yolo.h5", compile=False) if is_ShowModSum: yolo_model.summary() frame_Count = 0 if is_Video: cap = cv2.VideoCapture("./Data/" + fileName + ".mp4") else: cap = cv2.VideoCapture(0) if is_Capture: fourcc = cv2.VideoWriter_fourcc(*'XVID')
# tf.random_normal([19, 19, 5, 2], mean=1, stddev=4, seed=1), # tf.random_normal([19, 19, 5, 80], mean=1, stddev=4, seed=1)) # scores, boxes, classes = yolo_eval(yolo_outputs) # # print("scores[2] = " + str(scores[1].eval())) # print("boxes[2] = " + str(boxes[1].eval())) # print("classes[2] = " + str(classes[1].eval())) # print("scores.shape = " + str(scores.eval().shape)) # print("boxes.shape = " + str(boxes.eval().shape)) # print("classes.shape = " + str(classes.eval().shape)) # # test_c.close() sess = K.get_session() class_names = yolo_utils.read_classes("model_data/coco_classes.txt") anchors = yolo_utils.read_anchors("model_data/yolo_anchors.txt") image_shape = (720., 1280.) # 我真的吐了,少个点数据类型不一样了 yolo_model = load_model("model_data/yolov2.h5") # yolo_model.summary() yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) print(str(yolo_outputs)) # box_confidence, box_xy, box_wh,, box_class_probs = yolo_outputs # yolo_outputs = (box_confidence, box_xy, box_wh, box_class_probs) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) # image, image_data = yolo_utils.preprocess_image("images/0002.jpg", model_image_size=(608, 608)) def predict(sess, image_file, is_show_info=True, is_plot=True): # 图像预处理 image, image_data = yolo_utils.preprocess_image("images/" + image_file,
image, image_data = preprocess_image(image_file, model_image_size=(608, 608)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], { yolo_model.input: image_data, K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), image_file)) colors = generate_colors(class_names) draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) image.save(os.path.join(r"C:\Users\User\Desktop\Project2", image_file), quality=90) output_image = scipy.misc.imread( os.path.join(r"C:\Users\User\Desktop\Project2", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes class_names = read_classes("coco_classes.txt") anchors = read_anchors("yolo_anchors.txt") image_shape = (720., 1280.) sess = K.get_session() yolo_model = load_model("yolo.h5") yolo_model.summary() yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) out_scores, out_boxes, out_classes = predict(sess, "0055.jpg")
from keras.models import load_model, Model from yolo_utils import read_classes, read_anchors, generate_colors, preprocess_image, draw_boxes, scale_boxes from yad2k.models.keras_yolo import yolo_head, yolo_boxes_to_corners, preprocess_true_boxes, yolo_loss, yolo_body import yolo_filter_boxes import iou import yolo_non_max_suppression import yolo_eval import predict sess = K.get_session() print('yes') class_names = read_classes( "C:/Users/Vikarn Bhakri/Desktop/model_data/coco_classes.txt") anchors = read_anchors( "C:/Users/Vikarn Bhakri/Desktop/model_data/yolo_anchors.txt") image_shape = (720., 1280.) print('yes1') yolo_model = load_model("C:/Users/Vikarn Bhakri/Desktop/model_data/yolo.h5") print('yes2') yolo_model.summary() print('yes1') yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) print('yes1')
def _main(args): def predict(sess, image): # Preprocess your image image, image_data = preprocess_image(image, model_image_size=(416, 416)) # 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'.format(len(out_boxes))) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file out_image = draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) return out_image, out_scores, out_boxes, out_classes image_path = os.path.join(INPUT_PATH, args.input) input = cv2.imread(image_path) if input is None: print("Error opening image file") return height = float(input.shape[0]) width = float(input.shape[1]) image_shape = (height, width) sess = K.get_session() class_names = read_classes(CLASS_LIST_PATH) anchors = read_anchors(YOLO_ANCHORS_PATH) yolo_model, model = create_model(anchors, class_names) yolo_model.load_weights(args.weights) yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) boxes, scores, classes = yolo_eval(yolo_outputs, image_shape, score_threshold=args.score_threshold, iou_threshold=args.iou_threshold) process_img = cv2.cvtColor(input, cv2.COLOR_BGR2RGB) process_img = Image.fromarray(process_img) process_img, out_scores, out_boxes, out_classes = predict( sess, process_img) output = cv2.cvtColor(process_img, cv2.COLOR_BGR2RGB) cv2.imshow('prediction', output) cv2.waitKey(0) output_file_path = os.path.join(OUTPUT_PATH, args.input) if not os.path.exists(OUTPUT_PATH): os.mkdir(OUTPUT_PATH) cv2.imwrite(output_file_path, output) print("Saved: ", output_file_path)
stddev=4, seed=1), tf.random_normal([19, 19, 5, 2], mean=1, stddev=4, seed=1), tf.random_normal([19, 19, 5, 80], mean=1, stddev=4, seed=1)) scores, boxes, classes = yolo_eval(yolo_outputs) print("scores[2] = " + str(scores[2].eval())) print("boxes[2] = " + str(boxes[2].eval())) print("classes[2] = " + str(classes[2].eval())) print("scores.shape = " + str(scores.eval().shape)) print("boxes.shape = " + str(boxes.eval().shape)) print("classes.shape = " + str(classes.eval().shape)) test_c.close() sess = K.get_session() class_names = yolo_utils.read_classes( "E:\wuenda\model_data/coco_classes.txt") anchors = yolo_utils.read_anchors("E:\wuenda\model_data/yolo_anchors.txt") image_shape = (720., 1280.) print("ztbefore") yolo_model = load_model("E:\wuenda\model_data/yolov2.h5") print("zt") yolo_model.summary()
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 sess = K.get_session() class_names = read_classes('model_data/coco_classes.txt') anchors = read_anchors('model_data/yolo_anchors.txt') image_shape = (720.,1280.) yolo_model = load_model('model_data/yolo.h5') # yolo_model.summary() yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) filelist= [file for file in os.listdir('Images') if file.endswith('.png')] for image_file in filelist: out_scores, out_boxes, out_classes = predict(sess, image_file)
""" 对YOLO的总结: 1、输入图像为(608, 608) 2、输入的图像先要经过一个CNN模型,返回一个(19, 19, 5, 85)的输出 3、再对最后的两维降维,输出变成(19, 19, 5, 425): ·每个19*19的单元格拥有425个数字 ·425=5*85,即每个单元格拥有5个锚框,每个锚框由5个基本信息+80个分类预测构成 ·85=5+80,其中5个基本信息是(Pc,Px,Py,Ph,Pw),剩下的80个就是80个分类预测 4、然后我们会根据一下规则选择锚框: ·预测分数阈值:丢弃分数低于阈值的分类的锚框 ·非最大值抑制:计算交并比,并避免选择重叠的框 5、最后给出YOLO的输出 """ sess = K.get_session() class_names = read_classes('F:\\吴恩达DL作业\课后作业\\代码作业\\第四课第三周编程作业\\Car detection for Autonomous Driving\\model_data\\coco_classes.txt') anchors = read_anchors('F:\\吴恩达DL作业\课后作业\\代码作业\\第四课第三周编程作业\\Car detection for Autonomous Driving\\model_data\\yolo_anchors.txt') image_shape = (720., 1280.) yolo_model = load_model('F:\\吴恩达DL作业\课后作业\\代码作业\\第四课第三周编程作业\\Car detection for Autonomous Driving\\model_data\\yolo.h5') yolo_model.summary() yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) def predict(sess, image_file, is_show_info=True, is_plot=True): """ 运行存储在sess的计算图以预测image_file的边界框,打印出预测图与信息 :param sess: 包含了YOLO计算图的TensorFlow/keras的会话 :param imagefile: 存储images文件下的图片名称 :param is_show_info: :param is_plot: :return:
scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes, max_boxes, iou_threshold) return scores, boxes, classes #3 - Test YOLO pretrained model on images sess = K.get_session() #3.1 - Defining classes, anchors and image shape. class_names = read_classes( 'E:/Spyder/神经网络与深度学习/吴恩达 深度学习 编程作业(4-3)(未完成)- Autonomous driving - Car detection/yad2k/model_data/coco_classes.txt' ) anchors = read_anchors( 'E:/Spyder/神经网络与深度学习/吴恩达 深度学习 编程作业(4-3)(未完成)- Autonomous driving - Car detection/yad2k/model_data/yolo_anchors.txt' ) image_shape = (437., 699.) #3.2 - Loading a pretrained model yolo_model = load_model( 'E:/Spyder/神经网络与深度学习/吴恩达 深度学习 编程作业(4-3)(未完成)- Autonomous driving - Car detection/yad2k/model_data/yolo.h5' ) #yolo_model.summary() #3.3 - Convert output of the model to usable bounding box tensors yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) #3.4 - Filtering boxes scores, boxes, classes = yolo_eval(yolo_outputs, image_shape)
from keras.layers import Input, Lambda, Conv2D from keras.models import load_model, Model from yolo_utils import read_classes, read_anchors, generate_colors, preprocess_image, draw_boxes, scale_boxes from keras_yolo import yolo_head, yolo_boxes_to_corners, preprocess_true_boxes, yolo_loss, yolo_body from io import BytesIO # Emit Bluemix deployment event cf_deployment_tracker.track() app = Flask(__name__) BASE = './assets/' ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif']) class_names = read_classes(BASE + "coco_classes.txt") anchors = read_anchors(BASE + "yolo_anchors.txt") image_shape = (600., 900.) # Model saved with Keras model.save() MODEL_PATH = BASE + "yolo.h5" # Load your trained model yolo_model = load_model(MODEL_PATH) def yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold=.6): """Filters YOLO boxes by thresholding on object and class confidence. Arguments: box_confidence -- tensor of shape (19, 19, 5, 1) boxes -- tensor of shape (19, 19, 5, 4)
def getBoxes(input_image_name, yolo_model): #Loading the classes and the anchor boxes that are provided in the model_data folder class_names = read_classes("coco_classes.txt") anchors = read_anchors("yolo_anchors.txt") #Load the pretrained model. Please refer the README file to get info on how to obtain the yolo.h5 file # yolo_model = load_model("yolo.h5") #Print the summery of the model # yolo_model.summary() # In[10]: # input_image_name = "test.jpg" #Obtaining the dimensions of the input image input_image = Image.open(input_image_name) width, height = input_image.size width = np.array(width, dtype=float) height = np.array(height, dtype=float) #Assign the shape of the input image to image_shapr variable image_shape = (height, width) #Preprocess the input image before feeding into the convolutional network image, image_data = preprocess_image(input_image_name, model_image_size=(608, 608)) #Convert final layer features to bounding box parameters yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) #Now yolo_eval function selects the best boxes using filtering and non-max suppression techniques. # If you want to dive in more to see how this works, refer keras_yolo.py file in yad2k/models boxes, scores, classes = yolo_eval(yolo_outputs, image_shape) # In[11]: # Initiate a session sess = K.get_session() #Run the session out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) # In[12]: # #Print the results # print('Found {} boxes for {}'.format(len(out_boxes), input_image_name)) # #Produce the colors for the bounding boxs # colors = generate_colors(class_names) # #Draw the bounding boxes # draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # #Apply the predicted bounding boxes to the image and save it # image.save(os.path.join(input_image_name), quality=90) # output_image = scipy.misc.imread(os.path.join(input_image_name)) # plt.imshow(output_image) # In[13]: return out_scores, out_boxes, out_classes
# In this part, you are going to use a pretrained model and test it on the car detection dataset. As usual, you start by **creating a session to start your graph**. Run the following cell. # In[16]: sess = K.get_session() # ### 3.1 - Defining classes, anchors and image shape. # Recall that we are trying to detect 80 classes, and are using 5 anchor boxes. We have gathered the information about the 80 classes and 5 boxes in two files "coco_classes.txt" and "yolo_anchors.txt". Let's load these quantities into the model by running the next cell. # # The car detection dataset has 720x1280 images, which we've pre-processed into 608x608 images. # In[17]: class_names = read_classes("model_data/coco_classes.txt") anchors = read_anchors("model_data/yolo_anchors.txt") image_shape = (720., 1280.) # ### 3.2 - Loading a pretrained model # # Training a YOLO model takes a very long time and requires a fairly large dataset of labelled bounding boxes for a large range of target classes. You are going to load an existing pretrained Keras YOLO model stored in "yolo.h5". (These weights come from the official YOLO website, and were converted using a function written by Allan Zelener. References are at the end of this notebook. Technically, these are the parameters from the "YOLOv2" model, but we will more simply refer to it as "YOLO" in this notebook.) Run the cell below to load the model from this file. # In[18]: yolo_model = load_model("model_data/yolo.h5") # This loads the weights of a trained YOLO model. Here's a summary of the layers your model contains. # In[19]: yolo_model.summary()
max_boxes = 14 Input_shape = 128 # multiple of 32 input_shape = (Input_shape, Input_shape) threshold = 0.3 ignore_thresh = 0.5 num_epochs = 3 batch_size = 32 momentum = 0.9 decay = 0.0005 learning_rate = 0.001 path = '/home/minh/PycharmProjects' anchors_paths = PATH + '/model/yolo_anchors.txt' anchors = read_anchors(anchors_paths) annotation_path_train = PATH + '/model/boat_train.txt' data_path_train = PATH + '/model/boat_train.npz' # Load Data image_data, box_data, _, y_true = get_training_data(annotation_path_train, data_path_train, input_shape, anchors, num_classes, max_boxes, load_previous=True) ### model ### ################################################################################################################ # tf Graph input #graph = tf.Graph() #global_step # input