def test_detection_demo(device_id): if cntk_device(device_id).type() != DeviceKind_GPU: pytest.skip('test only runs on GPU') # it runs very slow in CPU try_set_default_device(cntk_device(device_id)) from prepare_test_data import prepare_Grocery_data, prepare_alexnet_v0_model grocery_path = prepare_Grocery_data() prepare_alexnet_v0_model() from FastRCNN.install_data_and_model import create_grocery_mappings create_grocery_mappings(grocery_path) from DetectionDemo import get_configuration import utils.od_utils as od cfg = get_configuration('FasterRCNN') cfg["CNTK"].FORCE_DETERMINISTIC = True cfg["CNTK"].DEBUG_OUTPUT = False cfg["CNTK"].MAKE_MODE = False cfg["CNTK"].FAST_MODE = False cfg.CNTK.E2E_MAX_EPOCHS = 3 cfg.CNTK.RPN_EPOCHS = 2 cfg.CNTK.FRCN_EPOCHS = 2 cfg.IMAGE_WIDTH = 400 cfg.IMAGE_HEIGHT = 400 cfg["CNTK"].TRAIN_E2E = True cfg.USE_GPU_NMS = False cfg.VISUALIZE_RESULTS = False cfg["DATA"].MAP_FILE_PATH = grocery_path externalData = 'CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY' in os.environ if externalData: extPath = os.environ['CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY'] cfg['BASE_MODEL_PATH'] = os.path.join(extPath, "PreTrainedModels", "AlexNet", "v1", "AlexNet_ImageNet_Caffe.model") else: cfg['BASE_MODEL_PATH'] = os.path.join( abs_path, *"../../../../PretrainedModels/AlexNet_ImageNet_Caffe.model".split( "/")) # train and test eval_model = od.train_object_detector(cfg) eval_results = od.evaluate_test_set(eval_model, cfg) meanAP = np.nanmean(list(eval_results.values())) print('meanAP={}'.format(meanAP)) assert meanAP > 0.01 # detect objects in single image img_path = os.path.join(grocery_path, "testImages", "WIN_20160803_11_28_42_Pro.jpg") regressed_rois, cls_probs = od.evaluate_single_image( eval_model, img_path, cfg) bboxes, labels, scores = od.filter_results(regressed_rois, cls_probs, cfg) assert bboxes.shape[0] == labels.shape[0]
def test_detection_demo(device_id): if cntk_device(device_id).type() != DeviceKind_GPU: pytest.skip('test only runs on GPU') # it runs very slow in CPU try_set_default_device(cntk_device(device_id)) from prepare_test_data import prepare_Grocery_data, prepare_alexnet_v0_model grocery_path = prepare_Grocery_data() prepare_alexnet_v0_model() from FastRCNN.install_data_and_model import create_grocery_mappings create_grocery_mappings(grocery_path) from DetectionDemo import get_configuration import utils.od_utils as od cfg = get_configuration('FasterRCNN') cfg["CNTK"].FORCE_DETERMINISTIC = True cfg["CNTK"].DEBUG_OUTPUT = False cfg["CNTK"].MAKE_MODE = False cfg["CNTK"].FAST_MODE = False cfg.CNTK.E2E_MAX_EPOCHS = 3 cfg.CNTK.RPN_EPOCHS = 2 cfg.CNTK.FRCN_EPOCHS = 2 cfg.IMAGE_WIDTH = 400 cfg.IMAGE_HEIGHT = 400 cfg["CNTK"].TRAIN_E2E = True cfg.USE_GPU_NMS = False cfg.VISUALIZE_RESULTS = False cfg["DATA"].MAP_FILE_PATH = grocery_path externalData = 'CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY' in os.environ if externalData: extPath = os.environ['CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY'] cfg['BASE_MODEL_PATH'] = os.path.join(extPath, "PreTrainedModels", "AlexNet", "v1", "AlexNet_ImageNet_Caffe.model") else: cfg['BASE_MODEL_PATH'] = os.path.join(abs_path, *"../../../../PretrainedModels/AlexNet_ImageNet_Caffe.model".split("/")) # train and test eval_model = od.train_object_detector(cfg) eval_results = od.evaluate_test_set(eval_model, cfg) meanAP = np.nanmean(list(eval_results.values())) print('meanAP={}'.format(meanAP)) assert meanAP > 0.01 # detect objects in single image img_path = os.path.join(grocery_path, "testImages", "WIN_20160803_11_28_42_Pro.jpg") regressed_rois, cls_probs = od.evaluate_single_image(eval_model, img_path, cfg) bboxes, labels, scores = od.filter_results(regressed_rois, cls_probs, cfg) assert bboxes.shape[0] == labels.shape[0]
from utils.configs.Grocery_config import cfg as dataset_cfg return merge_configs( [detector_cfg, network_cfg, dataset_cfg, { 'DETECTOR': detector_name }]) if __name__ == '__main__': # Currently supported detectors: 'FastRCNN', 'FasterRCNN' args = sys.argv detector_name = get_detector_name(args) cfg = get_configuration(detector_name) # train and test eval_model = od.train_object_detector(cfg) eval_results = od.evaluate_test_set(eval_model, cfg) # write AP results to output for class_name in eval_results: print('AP for {:>15} = {:.4f}'.format(class_name, eval_results[class_name])) print('Mean AP = {:.4f}'.format(np.nanmean(list(eval_results.values())))) # detect objects in single image img_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), r"../DataSets/Grocery/testImages/WIN_20160803_11_28_42_Pro.jpg") regressed_rois, cls_probs = od.evaluate_single_image( eval_model, img_path, cfg) bboxes, labels, scores = od.filter_results(regressed_rois, cls_probs, cfg)
# for AlexNet base model use: from utils.configs.AlexNet_config import cfg as network_cfg from utils.configs.AlexNet_config import cfg as network_cfg # for Pascal VOC 2007 data set use: from utils.configs.Pascal_config import cfg as dataset_cfg # for the Grocery data set use: from utils.configs.Grocery_config import cfg as dataset_cfg from utils.configs.Grocery_config import cfg as dataset_cfg return merge_configs([detector_cfg, network_cfg, dataset_cfg, {'DETECTOR': detector_name}]) if __name__ == '__main__': # Currently supported detectors: 'FastRCNN', 'FasterRCNN' args = sys.argv detector_name = get_detector_name(args) cfg = get_configuration(detector_name) # train and test eval_model = od.train_object_detector(cfg) eval_results = od.evaluate_test_set(eval_model, cfg) # write AP results to output for class_name in eval_results: print('AP for {:>15} = {:.4f}'.format(class_name, eval_results[class_name])) print('Mean AP = {:.4f}'.format(np.nanmean(list(eval_results.values())))) # detect objects in single image img_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), r"../DataSets/Grocery/testImages/WIN_20160803_11_28_42_Pro.jpg") regressed_rois, cls_probs = od.evaluate_single_image(eval_model, img_path, cfg) bboxes, labels, scores = od.filter_results(regressed_rois, cls_probs, cfg) # write detection results to output fg_boxes = np.where(labels > 0) print("#bboxes: before nms: {}, after nms: {}, foreground: {}".format(len(regressed_rois), len(bboxes), len(fg_boxes[0]))) for i in fg_boxes[0]: print("{:<12} (label: {:<2}), score: {:.3f}, box: {}".format(
workingDir = os.path.dirname(os.path.abspath(__file__)) detector_name = 'FasterRCNN' def get_configuration(detector_name): # load configs for detector, base network and data set from FasterRCNN.FasterRCNN_config import cfg as detector_cfg # for AlexNet base model use: from utils.configs.AlexNet_config import cfg as network_cfg from utils.configs.AlexNet_config import cfg as network_cfg # for the Fire data set use: from utils.configs.Prometheus_config import cfg as dataset_cfg from utils.configs.Prometheus_config import cfg as dataset_cfg return merge_configs( [detector_cfg, network_cfg, dataset_cfg, { 'DETECTOR': detector_name }]) if __name__ == '__main__': cfg = get_configuration(detector_name) # train and test eval_model = od.train_object_detector(cfg, "./PretrainedModels") eval_results = od.evaluate_test_set(eval_model, cfg) # write AP results to output for class_name in eval_results: print('AP for {:>15} = {:.4f}'.format(class_name, eval_results[class_name])) print('Mean AP = {:.4f}'.format(np.nanmean(list(eval_results.values()))))
def get_model(): cfg = get_configuration('FasterRCNN') #os.environ["DETECTOR_NAME"]) # train and test eval_model = train_object_detector(cfg) return eval_model