dataset_train.load_rice(FOOD_DIR, "train") dataset_train.prepare() # Validation dataset dataset_val = RiceDataset() dataset_val.load_rice(FOOD_DIR, "val") dataset_val.prepare() image, gt_class_ids, gt_boxes, gt_masks = mutils.load_image_gt(dataset_train, config, image_id=0, augment=None, augmentation=None, use_mini_mask=config.USE_MINI_MASK) # image = image[:, :, ::-1] # visualize.display_instances(image, gt_boxes, gt_masks, gt_class_ids, dataset_train.class_names) # image = cv2.cvtColor(cv2.resize(image, (224, 224)), cv2.COLOR_BGR2RGB) model = modellib.MaskYOLO(mode="inference", config=config, yolo_pretrain_dir=None, yolo_trainable=True) # print(model.keras_model.summary()) model.load_weights('./saved_model_Jan06-18-03.h5') # model.infer_yolo(image, './yolo_model_Jan04-19-37.h5') # model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=20, layers='all') # image = cv2.imread('./test.jpg') # image = cv2.cvtColor(cv2.resize(image, (224, 224)), cv2.COLOR_BGR2RGB) model.detect(image, './saved_model_Jan06-18-03.h5')
# file = open('./rice_boxes', 'w') image, gt_class_ids, gt_boxes, gt_masks = mutils.load_image_gt( dataset_train, config, image_id=0, augment=None, augmentation=None, use_mini_mask=config.USE_MINI_MASK) # file.write(str(gt_boxes[0]) + '\n') # file.close() # visualize.display_instances(image, gt_boxes, gt_masks, gt_class_ids, dataset_train.class_names) image = cv2.cvtColor(cv2.resize(image, (224, 224)), cv2.COLOR_BGR2RGB) image = image[:, :, ::-1] model = modellib.MaskYOLO(mode="training", config=config, yolo_pretrain_dir=None, yolo_trainable=True) print(model.keras_model.summary()) # model.load_weights('./saved_model_Jan02-09-54.h5') # model.infer_yolo(image, './saved_model_Jan01-21-14.h5') # model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=10, layers='all') # image = cv2.imread('./test.jpg') # image = cv2.cvtColor(cv2.resize(image, (224, 224)), cv2.COLOR_BGR2RGB) model.detect(image, './saved_model_Jan02-10-04.h5') # image, gt_class_ids, gt_boxes, gt_masks = mutils.load_image_gt(dataset_train, config, image_id=440, augment=None, # augmentation=None, # use_mini_mask=config.USE_MINI_MASK) # #
"""Return a Matplotlib Axes array to be used in all visualizations in the notebook. Provide a central point to control graph sizes. Change the default size attribute to control the size of rendered images """ _, ax = plt.subplots(rows, cols, figsize=(size * cols, size * rows)) return ax inference_config = ShapesConfig() inference_config.BATCH_SIZE = 1 inference_config.display() ''' Recreate the model in inference mode ''' model = modellib.MaskYOLO(mode="inference", config=inference_config) ''' Load trained weights ''' model.load_weights('./model_1216.h5', by_name=True) ''' Training dataset ''' dataset_train = ShapesDataset() dataset_train.load_shapes(500, inference_config.IMAGE_SHAPE[0], inference_config.IMAGE_SHAPE[1]) dataset_train.prepare() ''' Validation dataset ''' dataset_val = ShapesDataset() dataset_val.load_shapes(50, inference_config.IMAGE_SHAPE[0], inference_config.IMAGE_SHAPE[1]) dataset_val.prepare() ''' Test on a random image ''' image_id = random.choice(dataset_val.image_ids) original_image, gt_class_id, gt_bbox, gt_mask = mutils.load_image_gt(
# boxes.append([y - s, x - s, y + s, x + s]) # Apply non-max suppression wit 0.3 threshold to avoid # shapes covering each other keep_ixs = utils.non_max_suppression(np.array(boxes), np.arange(N), 0.3) shapes = [s for i, s in enumerate(shapes) if i in keep_ixs] return bg_color, shapes if __name__ == '__main__': config = ShapesConfig() config.display() # Training dataset dataset_train = ShapesDataset() dataset_train.load_shapes(500, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1]) dataset_train.prepare() # Validation dataset dataset_val = ShapesDataset() dataset_val.load_shapes(50, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1]) dataset_val.prepare() image_ids = np.random.choice(dataset_train.image_ids, 4) for image_id in image_ids: image = dataset_train.load_image(image_id) mask, class_ids = dataset_train.load_mask(image_id) # visualize.display_top_masks(image, mask, class_ids, dataset_train.class_names) box = extract_bboxes(mask) model = modellib.MaskYOLO(mode="training", config=config)