This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cfg.DATASETS.TRAIN = ("faces_train",) | |
cfg.DATASETS.TEST = ("faces_val",) | |
cfg.DATALOADER.NUM_WORKERS = 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cfg = get_cfg() | |
cfg.merge_from_file( | |
model_zoo.get_config_file( | |
"COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml" | |
) | |
) | |
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url( | |
"COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CocoTrainer(DefaultTrainer): | |
@classmethod | |
def build_evaluator(cls, cfg, dataset_name, output_folder=None): | |
if output_folder is None: | |
os.makedirs("coco_eval", exist_ok=True) | |
output_folder = "coco_eval" | |
return COCOEvaluator(dataset_name, cfg, False, output_folder) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for d in ["train", "val"]: | |
DatasetCatalog.register("faces_" + d, lambda d=d: create_dataset_dicts(train_df if d == "train" else test_df, classes)) | |
MetadataCatalog.get("faces_" + d).set(thing_classes=classes) | |
statement_metadata = MetadataCatalog.get("faces_train") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_dataset_dicts(df, classes): | |
dataset_dicts = [] | |
for image_id, img_name in enumerate(df.file_name.unique()): | |
record = {} | |
image_df = df[df.file_name == img_name] | |
file_path = f'{IMAGES_PATH}/{img_name}' | |
record["file_name"] = file_path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
classes = df.class_name.unique().tolist() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df = pd.read_csv('annotations.csv') | |
IMAGES_PATH = f'faces' | |
unique_files = df.file_name.unique() | |
train_files = set(np.random.choice(unique_files, int(len(unique_files) * 0.95), replace=False)) | |
train_df = df[df.file_name.isin(train_files)] | |
test_df = df[~df.file_name.isin(train_files)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def annotate_image(annotations, resize=True): | |
file_name = annotations.file_name.to_numpy()[0] | |
img = cv2.cvtColor(cv2.imread(f'faces/{file_name}'), cv2.COLOR_BGR2RGB) | |
for i, a in annotations.iterrows(): | |
cv2.rectangle(img, (a.x_min, a.y_min), (a.x_max, a.y_max), (0, 255, 0), 2) | |
if not resize: | |
return img |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df.to_csv('annotations.csv', header=True, index=None) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df = pd.DataFrame(dataset) | |
print(df.file_name.unique().shape[0], df.shape[0]) |