def __init__(self, json_path, model_path): self.epoch = 0 self.output_path = 0 self.model_name = 0 self.json_path = json_path self.model_path = model_path self.model = get_Model(training=False)
parser = argparse.ArgumentParser() parser.add_argument("-w", "--weight", help="weight file directory", type=str, default="./checkpoint/2--LSTM+BN5--30--0.222.hdf5") parser.add_argument("-t", "--test_img", help="Test image directory", type=str, default="./test_img/") args = parser.parse_args() # Get CRNN model model = get_Model(training=False) try: model.load_weights(args.weight) print("...Previous weight data...") except: raise Exception("No weight file!") test_dir = args.test_img test_imgs = os.listdir(args.test_img) total = 0 acc = 0 letter_total = 0 letter_acc = 0 start = time.time() for test_img in test_imgs:
from keras.callbacks import * from Model import get_Model from Samples_generation import * K.set_learning_phase(0) # 获取模型 model = get_Model(training=True) # 载入模型权重 try: model.load_weights(model_path) print("...Previous weight data...") except: print("...New weight data...") # 将损失最低的模型保存在名为1.hdf5的文件中 checkpoint = ModelCheckpoint(filepath=model_path, monitor='loss', verbose=1, mode='min', period=1) # 损失函数实际上已经在model内部算好了并作为y_pred输出了,所以这里只需要写这样一个loss就可以了 model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer='adam') model.fit_generator(generator=next_batch(), steps_per_epoch=int(n / batch_size), callbacks=[checkpoint], epochs=3000)
def _main_(args): # ----- Initialization ----------- input_path = args.input output_path = args.output dataset_name = args.dataset trial_version = args.trial_version net_h, net_w = 416, 416 #obj_thresh, nms_thresh = 0.7, 0.7 obj_thresh, nms_thresh = 0.7, 0.7 os.environ['CUDA_VISIBLE_DEVICES'] = "0,1" if trial_version != "WITH_TRAIN" and trial_version != "WITH_TEST": print("Wrong Trial version parameter") return #-START---- Model Loading------ if dataset_name == "parking": try: infer_model = load_model('W/' + trial_version + '/parking_detection_' + trial_version + '.h5') anchors = [ 9, 7, 10, 4, 16, 7, 17, 15, 24, 10, 30, 14, 57, 41, 71, 26, 99, 62 ] print("Parking detection weights loaded") except: print("Could not load parking detection weights") return elif dataset_name == "cctv": try: infer_model = load_model('W/' + trial_version + '/cctv_detection_' + trial_version + '.h5') #anchors = [4,2, 8,7, 9,4, 13,5, 14,11, 19,16, 19,7, 24,10, 29,13] anchors = [ 8, 4, 9, 8, 13, 5, 15, 13, 17, 7, 20, 17, 22, 8, 26, 10, 29, 14 ] print("CCTV detection weights loaded") except: print("Could not load CCTV detection weights") return else: print("Please specify dataset") return model = get_Model(training=False) if dataset_name == "parking": try: model.load_weights('W/' + trial_version + '/parking_recognition_' + trial_version + '.hdf5') print("Parking recognition weights loaded") except: raise Exception("No parking recognition weight file!") return elif dataset_name == "cctv": try: model.load_weights('W/' + trial_version + '/cctv_recognition_' + trial_version + '.hdf5') print("CCTV recognition weights loaded") except: raise Exception("No cctv recognition weight file!") return #-END---- Model Loading------ image_paths = [] for fold in os.listdir(input_path): for fname in os.listdir(os.path.join(input_path, fold)): if fname.endswith('.jpg') or fname.endswith('.png'): image_paths.append(os.path.join(input_path, fold, fname)) image_paths = sorted(image_paths) print("Number of images to test: " + str(len(image_paths))) # ----- Initialization ----------- # -START----- Main loop ------------- total_time = 0 for i, image_path in enumerate(image_paths): #if i == 10: # break print('[{}] / [{}]'.format(i + 1, len(image_paths))) image = cv2.imread(image_path) tic = time.time() boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, anchors, obj_thresh, nms_thresh)[0] if len(boxes) != False: box = boxes[0] else: print("Box not found! " + image_path) continue if box.ymin < 0 or box.ymax < 0 or box.xmin < 0 or box.xmax < 0: print("Negative box found. Cannot proceed! " + image_path) continue label = get_label(image[box.ymin:box.ymax, box.xmin:box.xmax, 0], model) toc = time.time() total_time += toc - tic write_result(output_path, dataset_name, image_path, label, box) # -END----- Main loop ------------- print('Avg. PT: {} ms.'.format(total_time / len(image_paths) * 1000))