def process_img(image: numpy.ndarray): # Подготавливается конфигурация config = PredictionConfig() config.display() # Производится определение модели model = MaskRCNN(mode='inference', model_dir='./', config=config) # Производится загрузка весов model.load_weights( WEIGHT_FILE, by_name=True ) # Производим сопоставление имен идентификаторов(Поскольку обучаем на части объектов) objs_ids = dict() objs_names = dict() for i, class_id in enumerate(CLASS_IDS, start=1): # print(i) objs_ids[i] = int(OBJECTS[class_id].split('_')[0]) objs_names[i] = OBJECTS[class_id] print(objs_ids) print(objs_names) identified_objects = list() r = model.detect([image], verbose=0)[0] # Если на кадре найдены объекты, то проводим их обработку if len(r['class_ids']): for num, object_id in enumerate(r['class_ids']): box = r['rois'][num] identified_object = IdentifiedObject( class_id=objs_ids[object_id], name=objs_names[object_id], mask=r['masks'][:, :, num], box=Box(box[1], box[0], box[3], box[2]), score=r['scores'][num] ) print(identified_object.__dict__) identified_objects.append(identified_object) return identified_objects
def train(): config = DeepFashionTrainConfig() config.display() model = MaskRCNN(mode="training", config=config, model_dir="checkpoints") # Load coco pretrained weights coco_weights = "models/mask_rcnn_coco.h5" # As we don't want to detect default coco classes, we want to exclude the last layers. # If not model requires matching number of coco classes model.load_weights(coco_weights, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask" ]) dataset_train = DeepFashionDataset() dataset_train.load_coco(config.TRAIN_IMG_DIR, config.TRAIN_ANNOTATIONS_PATH) dataset_train.prepare() dataset_valid = DeepFashionDataset() dataset_valid.load_coco(config.VALID_IMG_DIR, config.VALID_ANNOTATIONS_PATH) dataset_valid.prepare() model.train(dataset_train, dataset_valid, learning_rate=config.LEARNING_RATE, epochs=30, layers='heads') print("Finish")
def instance_segment(image_path, model_path): # define the model rcnn = MaskRCNN(mode='inference', model_dir='mask_rcnn', config=TestConfig()) # load coco model weights # Epochs 30 # Size (3000 train and 30000 validation)\ # Steps Per Epoch 1000 rcnn.load_weights(model_path, by_name=True) # load photograph img = load_img(image_path) img = img_to_array(img) # make prediction results = rcnn.detect([img], verbose=0) # get dictionary for first prediction r = results[0] # show photo with bounding boxes, masks, class labels and scores display_instances(img, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']) return r
app.config['JSON_SORT_KEYS'] = False app.config["IMAGE_UPLOADS"] = "images" # define the test configuration class TestConfig(Config): NAME = "test" GPU_COUNT = 1 IMAGES_PER_GPU = 1 NUM_CLASSES = 1 + 13 model_path = '../models/mask_rcnn_deep_fashion_0050.h5' global rcnn rcnn = MaskRCNN(mode='inference', model_dir='../mask_rcnn', config=TestConfig()) rcnn.load_weights(model_path, by_name=True) global graph graph = tf.get_default_graph() @app.route("/detect", methods=["POST"]) # @cross_origin() def upload_image(): if request.files: image = request.files["image"] # load photograph img = cv2.imdecode(np.frombuffer(image.read(), dtype=np.uint8), -1) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) result = extract_attributes(img)
config_proto = ConfigProto() # config_proto.gpu_options.per_process_gpu_memory_fraction = 0.8 config_proto.gpu_options.allow_growth = True session = InteractiveSession(config=config_proto) # Подготавливаем тренировочный и тестовый набор данных # train_set = DataSetFactory.new_instance('data/img') # test_set = DataSetFactory.new_instance('data/val') train_set = CocoSetFactory.new_instance(COCO_PATH, 'train', 2017, CLASS_IDS) test_set = CocoSetFactory.new_instance(COCO_PATH, 'val', 2017, CLASS_IDS) # Определяется конфигурация config = DetectConfig() config.display() # Определяется модель model = MaskRCNN(mode='training', model_dir='./', config=config) # Произвести загрузку стартовой модели model.load_weights(START_MODEL_PATH, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask" ]) # augmentation = imgaug.augmenters.Fliplr(0.5) # Запуск тренировки тестовой модели model.train(train_set, test_set, learning_rate=config.LEARNING_RATE, epochs=EPOCHS,
rect = Rectangle((x1, y1), width, height, fill=False, color='red') # draw the box ax.add_patch(rect) # show the figure pyplot.show() # train_set = CocoSetFactory.new_instance(COCO_PATH, 'train', 2017, CLASS_IDS) # test_set = CocoSetFactory.new_instance(COCO_PATH, 'val', 2017, CLASS_IDS) train_set = VGGCocoSetFactory.new_instance(COCO_PATH, 'train', 1004, CLASS_IDS) test_set = VGGCocoSetFactory.new_instance(COCO_PATH, 'val', 1004, CLASS_IDS) # create config cfg = DetectConfig() # define the model model = MaskRCNN(mode='inference', model_dir='./', config=cfg) # load model weights model.load_weights(weight_path, by_name=True) # evaluate model on training dataset print( f"Start evaluate model train :{datetime.now()} weight_file:{weight_path}") train_mAP = evaluate_model(train_set, model, cfg) print("Train mAP: %.3f" % train_mAP) # evaluate model on test dataset print(f"Start evaluate model test:{datetime.now()}") test_mAP = evaluate_model(test_set, model, cfg) print("Test mAP: %.3f" % test_mAP) print(f"End:{datetime.now()}") # # plot predictions for train dataset
# WEIGHT_FILE = 'detect_cfg20200513T1306/mask_rcnn_detect_cfg_0080.h5' # WEIGHT_FILE = 'mask_rcnn_detect_cfg_0082.h5' # WEIGHT_FILE = 'detect_cfg20200618T1641/mask_rcnn_detect_cfg_0052.h5' WEIGHT_FILE = 'detect_cfg20200618T1641/mask_rcnn_detect_cfg_0099.h5' # VIDEO_PATH = 'flights1.mov' # VIDEO_PATH = 'test/test.mp4' # VIDEO_PATH = 'video/профессия нефтяник.mp4' # VIDEO_PATH = 'video/test.mp4' # VIDEO_PATH = 'main_test_video.mp4' VIDEO_PATH = 'test_video/6.mp4' OUTPUT_FILE = "result/result_{:%Y%m%dT%H%M%S}.avi".format( datetime.datetime.now()) # Производится определение параметров использования видеокарты define_video_config() # Подготавливается конфигурация config = PredictionConfig() config.display() # Производится определение модели model = MaskRCNN(mode='inference', model_dir='./', config=config) # Производится загрузка весов model.load_weights(WEIGHT_FILE, by_name=True) # Определяется помошник для работы с видео video_helper = VideoHelper(model, VIDEO_PATH, OUTPUT_FILE) # Производится запуск video_helper.run()