def __init__(self): #TODO load classifier self.config_path = 'config.json' # Loading config file for tiny-yolo processing # Check if weight file is exist or not, if not # create merge weight file from archive files inside # folder "yolo_weight_archive" if os.path.isfile('tiny_yolo_finalweight.h5'): self.weights_path = 'tiny_yolo_finalweight.h5' else: os.system('cat yolo_weight_archive/tiny_yolo_archivea* > tiny_yolo_finalweight.h5') self.weights_path = 'tiny_yolo_finalweight.h5' print(os.getcwd()) with open(self.config_path) as config_buffer: config = json.load(config_buffer) # ============================================== # Initialize Yolo model = # ============================================== self.yolo = YOLO(backend=config['model']['backend'], input_size = config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) # Load the weight self.yolo.load_weights(self.weights_path) self.graph = tf.get_default_graph() print('Final yolo weight is loaded')
def load_model(args): config_path = args.conf weights_path = args.weights with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### yolo = YOLO(architecture=config['model']['architecture'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) ############################### # Load trained weights ############################### print(weights_path) yolo.load_weights(weights_path) return yolo, config
def _main_(args): config_path = args.conf weights_path = args.weights output_path = args.output with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # load the model ############################### # keras.backend.set_session(K.tf.Session(config=K.tf.ConfigProto(intra_op_parallelism_threads=8, inter_op_parallelism_threads=8))) yolo = YOLO(backend=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) yolo.load_weights(weights_path) yolo_inf = yolo.get_inference_model() yolo_inf.load_weights(weights_path) frozen_graph = freeze_session( K.get_session(), output_names=[out.op.name for out in yolo_inf.outputs]) tf.train.write_graph(frozen_graph, output_path, "convertedModel.pb", as_text=False)
def _main_(args): config_path = args.conf weights_path = args.weights image_path = args.input with open(config_path) as config_buffer: config = json.load(config_buffer) #model yolo = YOLO(backend = config['model']['backend'], input_size = config['model']['input_size'], labels = config['model']['labels'], max_box_per_image = config['model']['max_box_per_image'], anchors = config['model']['anchors']) #Load weights yolo.load_weights(weights_path) # Predict image = cv2.imread(image_path) boxes = yolo.predict(image) image = draw_boxes(image, boxes, config['model']['labels']) print(len(boxes), 'boxes :') cv2.imwrite(image_path[:-4] + '_detected' + image_path[-4:], image)
def __init__(self, weights_path, config_path): import json import os from frontend import YOLO os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ["CUDA_VISIBLE_DEVICES"] = "0" with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### self.yolo = YOLO( backend=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) ############################### # Load trained weights ############################### self.yolo.load_weights(weights_path) self.labels = config['model']['labels']
def _main_(args): config_path = args.conf weights_path = args.weights image_path = args.input show_stats = args.stats with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### yolo = YOLO(backend=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) ############################### # Load trained weights ############################### yolo.load_weights(weights_path) ############################### # Predict bounding boxes ############################### if os.path.isdir(image_path): for img in os.listdir(image_path): predict_file(os.path.join(image_path, img), yolo, config, show_stats) else: predict_file(image_path, yolo, config, show_stats)
def _detection_thread(): global detected_image, loaded print('Creating model...', end = ' ') yolo = YOLO(architecture='Tiny Yolo', input_size=416, labels=['cube'], max_box_per_image=3, anchors=[0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828]) yolo.load_weights('save_tiny.h5') print('Done!') loaded = True while True: webcam_lock.acquire() img = webcam_image webcam_lock.release() if img is not None: start = time.time() boxes = yolo.predict(img, nms_threshold=0.5, bgr=False) drawn_img = draw_boxes(img, boxes, ['cube']) end = time.time() fps = 1.0/(end-start) past_fps.append(fps) while len(past_fps) > 10: del past_fps[0] avg_fps = sum(past_fps)/len(past_fps) print('\rFPS: {:.2f}'.format(avg_fps), end='') detected_lock.acquire() detected_image = drawn_img detected_lock.release() if should_stop: break
def test(argparser,test_path): args=argparser.parse_args() config_path=args.conf weights_path=args.weights with open(config_path) as config_buffer: config=json.loads(config_buffer.read()) os.environ["CUDA_VISIBLE_DEVICES"]=config["env"]["gpu"] yolo=YOLO(architecture=config["model"]["architecture"], input_size=config["model"]["input_size"], labels=config["model"]["labels"], max_box_per_img=config["model"]["max_box_per_image"], anchors=config["model"]["anchors"]) yolo.load_weights(weights_path) if test_path[-3:]=="mp4": pass else: video_writer=cv2.VideoWriter(video_out,cv2.VideoWriter_fourcc(*'MPEG'),50.0,(w,h)) start=time.time() for f in os.listdir(test_path): print(f) f_path=os.path.join(test_path,f) # print(f_path) img=cv2.imread(f_path) boxes=yolo.predict(img) img=draw_boxes(boxes,img,config["model"]["labels"]) # cv2.imwrite(out_dir+f_path[-9:],img) video_writer.write(np.uint8(img)) print(time.time()-start) video_writer.release()
def _main_(args): config_path = args.conf weights_path = args.weights image_path = args.input keras.backend.tensorflow_backend.set_session(get_session()) with open(config_path) as config_buffer: config = json.load(config_buffer) if weights_path == '': weights_path = config['train']['saved_weights_name'] ############################### # Make the model ############################### yolo = YOLO(backend=config['model']['backend'], input_size=(config['model']['input_size_h'], config['model']['input_size_w']), labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors'], gray_mode=config['model']['gray_mode']) ############################### # Load trained weights ############################### yolo.load_weights(weights_path) inference_model = yolo.get_inference_model() inference_model.save("inference.h5")
def _main_(args): config_path = args.conf weights_path = args.weights keras.backend.tensorflow_backend.set_session(get_session()) with open(config_path) as config_buffer: config = json.load(config_buffer) if weights_path == '': weights_path = config['train']['saved_weights_name'] yolo = YOLO(backend=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) yolo.load_weights(weights_path) inference_model = yolo.get_inference_model() inference_model.save("{}_inference.h5".format( os.path.split(weights_path)[0])) print("done")
def _main_(args): config_path = args.conf weights_path = args.weights image_path = args.input with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### yolo = YOLO(backend = config['model']['backend'], input_size = config['model']['input_size'], labels = config['model']['labels'], max_box_per_image = config['model']['max_box_per_image'], anchors = config['model']['anchors']) ############################### # Load trained weights ############################### yolo.load_weights(weights_path) ############################### # Predict bounding boxes ############################### if image_path[-4:] == '.mp4': video_out = image_path[:-4] + '_detected' + image_path[-4:] video_reader = cv2.VideoCapture(image_path) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) video_writer = cv2.VideoWriter(video_out, cv2.VideoWriter_fourcc(*'MPEG'), 50.0, (frame_w, frame_h)) for i in tqdm(range(nb_frames)): _, image = video_reader.read() boxes = yolo.predict(image) image = draw_boxes(image, boxes, config['model']['labels']) video_writer.write(np.uint8(image)) video_reader.release() video_writer.release() else: image = cv2.imread(image_path) boxes = yolo.predict(image) image = draw_boxes(image, boxes, config['model']['labels']) print(len(boxes), 'boxes are found') cv2.imwrite(image_path[:-4] + '_detected' + image_path[-4:], image)
def _main_(args): config_path = args.conf weights_path = args.weights image_path = args.input with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### yolo = YOLO(architecture=config['model']['architecture'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) ############################### # Load trained weights ############################### print(weights_path) yolo.load_weights(weights_path) ############################### # Predict bounding boxes ############################### pattern = '*.jpg' log_file = 'log_' + datetime.datetime.now().strftime( "%Y_%m_%d_%H_%M_%S") + '.txt' log_str = '' for path, subdirs, files in os.walk(image_path): for name in files: if fnmatch(name, pattern): filename = os.path.join(path, name) print(filename) log_str += path + ',' + name image = cv2.imread(filename) boxes = yolo.predict(image) image = draw_boxes(image, boxes, config['model']['labels']) print(len(boxes), 'boxes are found') for box in boxes: box_label = config['model']['labels'][box.get_label()] box_score = box.get_score() log_str += ',' + box_label + ',' + str(box_score) cv2.imwrite(os.path.join(path, "detected_" + name), image) log_str += '\n' text_file = open(log_file, "w") text_file.write(log_str) text_file.close()
def main(): config_path = "config.json" weights_path = "../large_weights/full_yolo_whale.h5" directory_in_str = "../large_dataset/whale_files/test" df = pd.read_csv("../large_dataset/whale_files/sample_submission.csv", header=None, names=["Image", "Id", "Xmin", "Ymin", "Xmax", "Ymax"]) df = df.drop([0]) manual_check = pd.DataFrame( columns=["Image", "Id", "Xmin", "Ymin", "Xmax", "Ymax"]) with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### yolo = YOLO(backend=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) ############################### # Load trained weights ############################### yolo.load_weights(weights_path) ############################### # Predict bounding boxes ############################### directory = os.fsencode(directory_in_str) for file in tqdm(os.listdir(directory)): filename = os.fsdecode(file) if filename.endswith(".jpg"): image = cv2.imread(os.path.join(directory_in_str, filename)) image_h, image_w, _ = image.shape boxes = yolo.predict(image) if len(boxes) == 1: df.at[df.Image == filename, 'Xmin'] = max(floor(boxes[0].xmin * image_w), 0) df.at[df.Image == filename, 'Ymin'] = max(floor(boxes[0].ymin * image_h), 0) df.at[df.Image == filename, 'Xmax'] = min(ceil(boxes[0].xmax * image_w), image_w) df.at[df.Image == filename, 'Ymax'] = min(ceil(boxes[0].ymax * image_h), image_h) else: s = df[df.Image == filename] manual_check = pd.concat([manual_check, s]) else: continue df.to_csv('test_box.csv', encoding='utf-8', index=False) manual_check.to_csv('manual_check_test.csv', encoding='utf-8', index=False)
class YOLO(): def __init__(self, yaml_fp): yf_ = open(yaml_fp, 'r') yaml_ = yaml.load(yf_) config_path = yaml_['config_path'] weights_path = yaml_['weights_path'] self._score = yaml_['score'] self._nms = yaml_['nms'] yf_.close() os.chdir(config_path[:config_path.rfind('/') + 1]) with open(config_path) as config_buffer: self._config = json.load(config_buffer) self._yolo = YOLO_( backend=self._config['model']['backend'], input_size=self._config['model']['input_size'], labels=self._config['model']['labels'], max_box_per_image=self._config['model']['max_box_per_image'], anchors=self._config['model']['anchors']) self._yolo.load_weights(weights_path) self._labels = self._config['model']['labels'] # for some reason, need to run once here from numpy import empty esz_ = [ self._config['model']['input_size'], self._config['model']['input_size'], 3 ] self.detect_img(empty(esz_)) print(' YOLO object initialized!') def detect_img(self, img): boxes_ = self._yolo.predict(img, self._score, self._nms) msgs_ = [] for box in boxes_: msg_ = YoloObj() msg_.box = bb_arr(box, img.shape) msg_.score = box.get_score() msg_.id = box.get_label() msg_.label = self._labels[msg_.id] msgs_.append(msg_) return YoloResponse(objects=msgs_) def annotate_img(self, img, draw_classes): boxes_ = self._yolo.predict(img, self._score, self._nms) if len(draw_classes): all_boxes_ = boxes_ boxes_ = [] for box in all_boxes_: if self._labels[box.get_label()] in draw_classes: boxes_.append(box) return draw_boxes(img, boxes_, self._config['model']['labels'])
def _main_(args): ############################### # Prepare data to be detected ############################### # data_folder = "/home/peng/data/good_rolo_data/" data_folder = "/home/peng/data/sort_data/images/" # data_folder = "/home/peng/data/sort_data/images/" video_folders_list = sorted(glob.glob(data_folder + '*')) sort_nicely(video_folders_list) config_path = args.conf weights_path = args.weights with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### yolo = YOLO(architecture = config['model']['architecture'], input_size = config['model']['input_size'], labels = config['model']['labels'], max_box_per_image = config['model']['max_box_per_image'], anchors = config['model']['anchors']) ############################### # Load trained weights ############################### print("Weights path:", weights_path) yolo.load_weights(weights_path) ############################### # Predict bounding boxes ############################### for video_folder in video_folders_list: video_name = basename(video_folder) print("Processing %s." % video_name) image_paths = sorted(glob.glob(os.path.join(video_folder, '*jpg'))) sort_nicely(image_paths) with open('det_mot/' + video_name + '.txt', 'w') as out_file: for i in tqdm(range(len(image_paths))): image = cv2.imread(image_paths[i]) boxes = yolo.predict(image) for box in boxes: x1 = (box.x - box.w/2) * 1280 y1 = (box.y - box.h/2) * 720 w = box.w * 1280 h = box.h * 720 print('%d,-1,%.2f,%.2f,%.2f,%.2f,%.6f,-1,-1,-1' % (i+1, x1, y1, w, h, box.c), file=out_file)
def create_graph(): global yolo, config with open(config_path) as config_buffer: config = json.load(config_buffer) yolo = YOLO(architecture=config['model']['architecture'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) print weights_path yolo.load_weights(weights_path)
def _main_(args): config_path = args.conf weights_path = args.weights image_path = args.input runID = args.runID with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### yolo = YOLO(backend=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) ############################### # Load trained weights ############################### yolo.load_weights(weights_path) ############################### # Predict bounding boxes ############################### outputdir = os.path.join( '/flashblade/lars_data/2018_Cyclomedia_panoramas/project_folder/YOLO/false_positives_dir', runID) if os.path.exists(outputdir): print('path exists') else: os.makedirs(outputdir) print(image_path) imlist = os.listdir(image_path) print(len(imlist)) for file in imlist: if file.endswith('jpg'): print(file) image = cv2.imread(os.path.join(image_path, file)) boxes = yolo.predict(image) image = draw_boxes(image, boxes, config['model']['labels']) print(len(boxes), 'boxes are found') if len(boxes) > 0: filename = file[:-4] + '_detected' + file[-4:] output = os.path.join(outputdir, filename) print(output) cv2.imwrite(output, image)
def __init__(self, config_path, weights_path): with open(config_path) as config_buffer: config = json.loads(config_buffer.read()) self.labels = config['model']['labels'] self.yolo = YOLO( architecture=config['model']['architecture'], input_size=config['model']['input_size'], labels=self.labels, max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) self.yolo.load_weights(weights_path)
def __init__(self, config_path, weights_path): self.config_path = config_path self.weights_path = weights_path with open(config_path) as config_buffer: self.config = json.load(config_buffer) self.yolo = YOLO( backend=self.config['model']['backend'], input_size=self.config['model']['input_size'], labels=self.config['model']['labels'], max_box_per_image=self.config['model']['max_box_per_image'], anchors=self.config['model']['anchors']) self.yolo.load_weights(self.weights_path)
def _main_(args): with open(args.conf) as config_buffer: config = json.load(config_buffer) ############################### # Load the model ############################### # keras.backend.set_session(K.tf.Session(config=K.tf.ConfigProto(intra_op_parallelism_threads=8, inter_op_parallelism_threads=8))) yolo = YOLO(backend=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) yolo.load_weights(args.weights) ############################### # Decide what to predict on ############################### if args.input[-4:] in ['.mp4', '.avi', '.mov']: predictOnVideo(args.input, args.output, config, maxFramesToRunOn=5000, runFrameSelection=args.frame_select, detectionModel=yolo) else: first_file_name = os.listdir(args.input)[0] # predict on folder of images and save images with overlayed bboxes if first_file_name[-4:] in [".jpg", ".png"]: predictOnImageDir(args.input, args.output, config, savePredictionsAsXmlToo=args.save_soft, detectionModel=yolo) elif first_file_name[-4:] in [".h5", ".hdf5"]: predictOnH5Dir(args.input, args.output, config, obj_threshold=obj_thresh, detectionModel=yolo) else: print('input -i argument extension did not match what we expected')
def _main_(args): config_path = args.conf weights_path = args.weights image_path = args.input with open(config_path) as config_buffer: config = json.load(config_buffer) K.set_learning_phase(0) ############################### # Make the model ############################### yolo = YOLO(backend=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) ############################### # Load trained weights ############################### print(weights_path) yolo.load_weights(weights_path) export_base = 'tfexport' export_version = 1 export_path = os.path.join(tf.compat.as_bytes(export_base), tf.compat.as_bytes(str(export_version))) builder = saved_model_builder.SavedModelBuilder(export_path) print(yolo.model.inputs[0]) signature = predict_signature_def( inputs={ "input_image": yolo.model.inputs[0], "true_boxes": yolo.model.inputs[1] }, outputs={"outputs": yolo.model.outputs[0]}) with K.get_session() as sess: builder.add_meta_graph_and_variables( sess=sess, tags=[tag_constants.SERVING], signature_def_map={'predict': signature}) builder.save()
def __init__(self, yaml_fp): yf_ = open(yaml_fp, 'r') yaml_ = yaml.load(yf_) config_path = yaml_['config_path'] weights_path = yaml_['weights_path'] self._score = yaml_['score'] self._nms = yaml_['nms'] yf_.close() os.chdir(config_path[:config_path.rfind('/') + 1]) with open(config_path) as config_buffer: self._config = json.load(config_buffer) self._yolo = YOLO_( backend=self._config['model']['backend'], input_size=self._config['model']['input_size'], labels=self._config['model']['labels'], max_box_per_image=self._config['model']['max_box_per_image'], anchors=self._config['model']['anchors']) self._yolo.load_weights(weights_path) self._labels = self._config['model']['labels'] # for some reason, need to run once here from numpy import empty esz_ = [ self._config['model']['input_size'], self._config['model']['input_size'], 3 ] self.detect_img(empty(esz_)) print(' YOLO object initialized!')
def classifier(self): if not self._classifier: from frontend import YOLO # Khởi tạo Yolo self._classifier = YOLO( backend='Full Yolo', input_size=416, anchors=[ 0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828 ], max_box_per_image=10, labels=['Pothole'], ) # Load dữ liệu cho model nhận diện self._classifier.load_weights('%s/classifier.h5' % NanoPotholeDetection.DATA_DIR) return self._classifier
def get_keras_model(self): config_path = 'ncsmodel/config.json' weights_path = 'ncsmodel/openimages_best3.h5' def weights_name(layer_list, name): for l in layer_list: if l.name == name: return l.get_weights() with open(config_path) as config_buffer: config = json.load(config_buffer) yolo = YOLO(backend=config['model']['backend'], input_size=(config['model']['input_size_h'], config['model']['input_size_w']), labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors'], gray_mode=config['model']['gray_mode']) # Loading weights: yolo.load_weights(weights_path) ymodel = yolo.model ylayers = ymodel.layers[1].layers for l in ymodel.layers[2:]: ylayers.append(l) print("Layers:") for i, l in enumerate(ylayers): print(i, l.name) # Save weights to numpy - 9 conv layers # 1 based index q_conv_bn = 6 layer_weights = [] for i in range(q_conv_bn): weights = weights_name(ylayers, 'conv_{}'.format(i + 1))[0] biases = weights_name(ylayers, 'norm_{}'.format(i + 1)) layer_weights.append((biases, weights)) weights, biases = weights_name(ylayers, 'DetectionLayer') layer_weights.append((biases, weights)) return layer_weights
def _main_(args): config_path = args.conf weights_path = args.weights image_path = args.input with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### model_load_time = time.time() yolo = YOLO(backend=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) ############################### # Load trained weights ############################### yolo.load_weights(weights_path) print('Model load time is ' + str(time.time() - model_load_time)) ############################### # Predict bounding boxes ############################### inference_time = [] for filename in os.listdir(image_path): img_path = os.path.join(image_path, filename) inference_start_time = time.time() image = cv2.imread(img_path) boxes = yolo.predict(image) inference_time.append(time.time() - inference_start_time) print(len(boxes), 'boxes are found') image = draw_boxes(image, boxes, config['model']['labels']) cv2.imwrite(img_path[:-4] + '_detected' + img_path[-4:], image) print('Avg inference time is ' + str(np.mean(inference_time)) + '+/-' + str(np.std(inference_time)))
def _main_(args): config_path = 'config.json' weights_path = 'tiny_yolo_trafficred3.h5' image_path = args.input with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### yolo = YOLO(backend=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) ############################### # Load trained weights ############################### yolo.load_weights(weights_path) ############################### # Predict bounding boxes ############################### image = cv2.imread(image_path) for item in range(10): start_time = time.time() boxes = yolo.predict(image) duration = time.time() - start_time print('Prediction time %5.2f' % (duration)) image = draw_boxes(image, boxes, config['model']['labels']) print(len(boxes), 'boxes are found') cv2.imwrite(image_path[:-4] + '_detected' + image_path[-4:], image)
def _main_(args): config_path = args.conf weights_path = args.weights input = args.input camera = args.camera with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### yolo = YOLO(backend=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors'], threshold=config['predict']['threshold'], max_sur=config["predict"]["max_sur"]) ############################### # Load trained weights ############################### yolo.load_weights(weights_path) ############################### # Predict bounding boxes ############################### if camera: ph.predict_with_camera(yolo) elif input[-4:] == '.mp4': ph.predict_with_video(yolo, input, config['predict']['saved']) else: image = cv2.imread(input) boxes = yolo.predict(image) image = draw_boxes(image, boxes, config['model']['labels']) #print(len(boxes), 'boxes are found') cv2.imwrite(input[:-4] + '_detected' + input[-4:], image)
class DetectorAPI: def __init__(self, weights_path, config_path): import json import os from frontend import YOLO os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ["CUDA_VISIBLE_DEVICES"] = "0" with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### self.yolo = YOLO( backend=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) ############################### # Load trained weights ############################### self.yolo.load_weights(weights_path) self.labels = config['model']['labels'] def processFrame(self, image): import numpy as np import time # Expand dimensions since the trained_model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(image, axis=0) # Actual detection. start_time = time.time() boxes = self.yolo.predict(image) end_time = time.time() print("Elapsed Time:", end_time - start_time) return boxes
class Predictor(): def __init__(self, config_path, weights_path): self.config_path = config_path self.weights_path = weights_path with open(config_path) as config_buffer: self.config = json.load(config_buffer) self.yolo = YOLO( backend=self.config['model']['backend'], input_size=self.config['model']['input_size'], labels=self.config['model']['labels'], max_box_per_image=self.config['model']['max_box_per_image'], anchors=self.config['model']['anchors']) self.yolo.load_weights(self.weights_path) def predict(self, image): boxes = self.yolo.predict(image) image = draw_boxes(image, boxes, self.config['model']['labels']) return boxes, image
def _main_(args): config_path = args.conf image_path = args.input with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### yolo = YOLO(backend=config['model']['backend'], input_width=config['model']['input_width'], input_height=config['model']['input_height'], input_channel=config['model']['input_channel'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors'], saved_config_name=config['train']['saved_config_name']) ############################### # Load trained weights ############################### print(config['train']['saved_weights_name']) yolo.load_weights(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### if image_path[-4:] == '.mp4': video_out = image_path[:-4] + '_detected' + image_path[-4:] video_reader = cv2.VideoCapture(image_path) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) video_writer = cv2.VideoWriter(video_out, cv2.VideoWriter_fourcc(*'MPEG'), 50.0, (frame_w, frame_h)) for i in tqdm(range(nb_frames)): _, image = video_reader.read() boxes = yolo.predict(image) image = draw_boxes(image, boxes, config['model']['labels']) video_writer.write(np.uint8(image)) video_reader.release() video_writer.release() else: image = cv2.imread(image_path) boxes = yolo.predict(image) image = draw_boxes(image, boxes, config['model']['labels']) print(len(boxes), 'boxes are found') cv2.imwrite(image_path[:-4] + '_detected' + image_path[-4:], image)
def _main_(args): config_path = args.conf with open(config_path) as config_buffer: config = json.loads(config_buffer.read()) test_imgs, test_labels = parse_annotation( config['test']['test_annot_folder'], config['test']['test_image_folder'], config['model']['labels']) ############################### # Construct the model ############################### yolo = YOLO(feature_extractor=config['model']['backend'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) yolo.load_weights(config['train']['saved_weights_name']) print('weights loaded from {}'.format( config['train']['saved_weights_name'])) yolo.evaluate(test_imgs[:500], iou_threshold=0.3, obj_threshold=0.2, nms_threshold=0.2)
def _main_(args): config_path = args.conf with open(config_path) as config_buffer: config = json.loads(config_buffer.read()) ############################### # Parse the annotations ############################### # parse annotations of the training set train_imgs, train_labels = parse_annotation(config['train']['train_annot_folder'], config['train']['train_image_folder'], config['model']['labels']) # parse annotations of the validation set, if any, otherwise split the training set if os.path.exists(config['valid']['valid_annot_folder']): valid_imgs, valid_labels = parse_annotation(config['valid']['valid_annot_folder'], config['valid']['valid_image_folder'], config['model']['labels']) else: train_valid_split = int(0.8*len(train_imgs)) np.random.shuffle(train_imgs) valid_imgs = train_imgs[train_valid_split:] train_imgs = train_imgs[:train_valid_split] if len(config['model']['labels']) > 0: overlap_labels = set(config['model']['labels']).intersection(set(train_labels.keys())) print 'Seen labels:\t', train_labels print 'Given labels:\t', config['model']['labels'] print 'Overlap labels:\t', overlap_labels if len(overlap_labels) < len(config['model']['labels']): print 'Some labels have no annotations! Please revise the list of labels in the config.json file!' return else: print 'No labels are provided. Train on all seen labels.' config['model']['labels'] = train_labels.keys() ############################### # Construct the model ############################### yolo = YOLO(architecture = config['model']['architecture'], input_size = config['model']['input_size'], labels = config['model']['labels'], max_box_per_image = config['model']['max_box_per_image'], anchors = config['model']['anchors']) ############################### # Load the pretrained weights (if any) ############################### if os.path.exists(config['train']['pretrained_weights']): print "Loading pre-trained weights in", config['train']['pretrained_weights'] yolo.load_weights(config['train']['pretrained_weights']) ############################### # Start the training process ############################### yolo.train(train_imgs = train_imgs, valid_imgs = valid_imgs, train_times = config['train']['train_times'], valid_times = config['valid']['valid_times'], nb_epoch = config['train']['nb_epoch'], learning_rate = config['train']['learning_rate'], batch_size = config['train']['batch_size'], warmup_epochs = config['train']['warmup_epochs'], object_scale = config['train']['object_scale'], no_object_scale = config['train']['no_object_scale'], coord_scale = config['train']['coord_scale'], class_scale = config['train']['class_scale'], saved_weights_name = config['train']['saved_weights_name'], debug = config['train']['debug'])