def test_hpf_b3_fixed_covpool(): model = hpf_b3_fixed_covpool(num_classes=4).cuda().eval() input = {INPUT_IMAGE_KEY: torch.randn((2, 3, 512, 512)).cuda()} print(count_parameters(model, keys=KNOWN_KEYS)) output = model(**input) for output_name, output_value in output.items(): print(output_name, output_value.size())
def test_nr_rgb_mixnet_xxl(): model = nr_rgb_mixnet_xxl(num_classes=4).cuda() input = {INPUT_FEATURES_JPEG_FLOAT: torch.randn((2, 3, 512, 512)).cuda()} print(count_parameters(model, keys=KNOWN_KEYS)) output = model(**input) for output_name, output_value in output.items(): print(output_name, output_value.size())
def test_bit_m_rx152_2(): model = bit_m_rx152_2(num_classes=4).cuda() input = {INPUT_IMAGE_KEY: torch.randn((2, 3, 512, 512)).cuda()} print(count_parameters(model, keys=KNOWN_KEYS)) output = model(**input) for output_name, output_value in output.items(): print(output_name, output_value.size())
def test_dct_efficientnet_b6(): model = dct_efficientnet_b6().cuda() input = {INPUT_FEATURES_DCT_KEY: torch.randn((2, 3, 512, 512)).cuda()} print(count_parameters(model, keys=KNOWN_KEYS)) output = model(**input) for output_name, output_value in output.items(): print(output_name, output_value.size())
def test_srnet(): model = srnet().cuda() input = {INPUT_IMAGE_KEY: torch.randn((2, 3, 512, 512)).cuda()} print(count_parameters(model, keys=KNOWN_KEYS)) output = model(**input) for output_name, output_value in output.items(): print(output_name, output_value.size())
def test_dct_seresnext50(): model = dct_seresnext50().cuda() input = { INPUT_FEATURES_DCT_KEY: torch.randn( (2, 64 * 3, 512 // 8, 512 // 8)).cuda() } print(count_parameters(model, keys=KNOWN_KEYS)) output = model(**input) for output_name, output_value in output.items(): print(output_name, output_value.size())
def test_ela_s2d_skresnext50_32x4d(): model = ela_s2d_skresnext50_32x4d().cuda() input = { INPUT_FEATURES_CHANNEL_Y_KEY: torch.randn((2, 1, 512, 512)).cuda(), INPUT_FEATURES_CHANNEL_CB_KEY: torch.randn((2, 1, 512, 512)).cuda(), INPUT_FEATURES_CHANNEL_CR_KEY: torch.randn((2, 1, 512, 512)).cuda(), INPUT_FEATURES_ELA_RICH_KEY: torch.randn((2, 9, 512, 512)).cuda(), } print(count_parameters(model, keys=KNOWN_KEYS)) output = model(**input) for output_name, output_value in output.items(): print(output_name, output_value.size())
def test_ela_wider_resnet38(): model = get_model("ela_wider_resnet38").cuda().eval() image = cv2.imread(os.path.join(TEST_DATA_DIR, "Cover", "00001.jpg")) ela = compute_ela(image) blur = compute_blur_features(image) print(count_parameters(model, keys=KNOWN_KEYS)) input = { INPUT_IMAGE_KEY: tensor_from_rgb_image(image).unsqueeze(0).cuda(), INPUT_FEATURES_BLUR_KEY: tensor_from_rgb_image(blur).unsqueeze(0).cuda().float(), INPUT_FEATURES_ELA_KEY: tensor_from_rgb_image(ela).unsqueeze(0).cuda().float(), } output = model(**input) for output_name, output_value in output.items(): print(output_name, output_value.size())
def test_models_forward(model_name): model = get_model(model_name).cuda().eval() image = cv2.imread(os.path.join(TEST_DATA_DIR, "Cover", "00001.jpg")) dct = compute_dct(cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)) ela = compute_ela(image) blur = compute_blur_features(image) print(count_parameters(model, keys=KNOWN_KEYS)) input = { INPUT_IMAGE_KEY: tensor_from_rgb_image(image).unsqueeze(0).cuda(), INPUT_FEATURES_DCT_KEY: tensor_from_rgb_image(dct).unsqueeze(0).cuda().float(), INPUT_FEATURES_BLUR_KEY: tensor_from_rgb_image(blur).unsqueeze(0).cuda().float(), INPUT_FEATURES_ELA_KEY: tensor_from_rgb_image(ela).unsqueeze(0).cuda().float(), } output = model(**input) for output_name, output_value in output.items(): print(output_name, output_value.size())
def run_stage_training(model: Union[TimmRgbModel, YCrCbModel], config: StageConfig, exp_config: ExperimenetConfig, experiment_dir: str): # Preparing model freeze_model(model, freeze_bn=config.freeze_bn) train_ds, valid_ds, train_sampler = get_datasets( data_dir=exp_config.data_dir, image_size=config.image_size, augmentation=config.augmentations, balance=config.balance, fast=config.fast, fold=exp_config.fold, features=model.required_features, obliterate_p=config.obliterate_p, ) criterions_dict, loss_callbacks = get_criterions( modification_flag=config.modification_flag_loss, modification_type=config.modification_type_loss, embedding_loss=config.embedding_loss, feature_maps_loss=config.feature_maps_loss, num_epochs=config.epochs, mixup=config.mixup, cutmix=config.cutmix, tsa=config.tsa, ) callbacks = loss_callbacks + [ OptimizerCallback(accumulation_steps=config.accumulation_steps, decouple_weight_decay=False), HyperParametersCallback( hparam_dict={ "model": exp_config.model_name, "scheduler": config.schedule, "optimizer": config.optimizer, "augmentations": config.augmentations, "size": config.image_size[0], "weight_decay": config.weight_decay, }), ] if config.show: callbacks += [ ShowPolarBatchesCallback(draw_predictions, metric="loss", minimize=True) ] loaders = collections.OrderedDict() loaders["train"] = DataLoader( train_ds, batch_size=config.train_batch_size, num_workers=exp_config.num_workers, pin_memory=True, drop_last=True, shuffle=train_sampler is None, sampler=train_sampler, ) loaders["valid"] = DataLoader(valid_ds, batch_size=config.valid_batch_size, num_workers=exp_config.num_workers, pin_memory=True) print("Stage :", config.stage_name) print(" FP16 mode :", config.fp16) print(" Fast mode :", config.fast) print(" Epochs :", config.epochs) print(" Workers :", exp_config.num_workers) print(" Data dir :", exp_config.data_dir) print(" Experiment dir :", experiment_dir) print("Data ") print(" Augmentations :", config.augmentations) print(" Obliterate (%) :", config.obliterate_p) print(" Negative images:", config.negative_image_dir) print(" Train size :", len(loaders["train"]), "batches", len(train_ds), "samples") print(" Valid size :", len(loaders["valid"]), "batches", len(valid_ds), "samples") print(" Image size :", config.image_size) print(" Balance :", config.balance) print(" Mixup :", config.mixup) print(" CutMix :", config.cutmix) print(" TSA :", config.tsa) print("Model :", exp_config.model_name) print(" Parameters :", count_parameters(model)) print(" Dropout :", exp_config.dropout) print("Optimizer :", config.optimizer) print(" Learning rate :", config.learning_rate) print(" Weight decay :", config.weight_decay) print(" Scheduler :", config.schedule) print(" Batch sizes :", config.train_batch_size, config.valid_batch_size) print("Losses ") print(" Flag :", config.modification_flag_loss) print(" Type :", config.modification_type_loss) print(" Embedding :", config.embedding_loss) print(" Feature maps :", config.feature_maps_loss) optimizer = get_optimizer( config.optimizer, get_optimizable_parameters(model), learning_rate=config.learning_rate, weight_decay=config.weight_decay, ) scheduler = get_scheduler( config.schedule, optimizer, lr=config.learning_rate, num_epochs=config.epochs, batches_in_epoch=len(loaders["train"]), ) if isinstance(scheduler, CyclicLR): callbacks += [SchedulerCallback(mode="batch")] # model training runner = SupervisedRunner(input_key=model.required_features, output_key=None) runner.train( fp16=config.fp16, model=model, criterion=criterions_dict, optimizer=optimizer, scheduler=scheduler, callbacks=callbacks, loaders=loaders, logdir=os.path.join(experiment_dir, config.stage_name), num_epochs=config.epochs, verbose=config.verbose, main_metric=config.main_metric, minimize_metric=config.main_metric_minimize, checkpoint_data={"config": config}, ) del optimizer, loaders, callbacks, runner best_checkpoint = os.path.join(experiment_dir, config.stage_name, "checkpoints", "best.pth") model_checkpoint = os.path.join(experiment_dir, f"{exp_config.checkpoint_prefix}.pth") clean_checkpoint(best_checkpoint, model_checkpoint) # Restore state of best model if config.restore_best: unpack_checkpoint(load_checkpoint(model_checkpoint), model=model) # Some memory cleanup torch.cuda.empty_cache() gc.collect()
def test_count_meso4_params(self): self.assertTrue(torch.cuda.is_available()) model = meso4() print(count_parameters(model))
def main(): best_checkpoints = glob.glob("runs/**/*best.pth", recursive=True) print(best_checkpoints) df = defaultdict(dict) for checkpoint in best_checkpoints: components = str.split(checkpoint, os.sep) session = components[1] checkpoint_data = torch.load(checkpoint, map_location="cpu") metrics = checkpoint_data["valid_metrics"] b_auc = metrics.get("auc", 0) c_auc = metrics.get("auc_classifier", 0) epoch = checkpoint_data["epoch"] model_name = checkpoint_data["checkpoint_data"]["cmd_args"].get( "model", None) if model_name is None: model_name = infer_model(session) df[session]["session"] = session df[session]["fold"] = infer_fold(session) df[session]["b_auc"] = max(b_auc, df[session].get("b_auc", 0)) df[session]["c_auc"] = max(c_auc, df[session].get("c_auc", 0)) df[session]["model_name"] = model_name df[session]["activation"] = infer_activation(model_name) df[session]["input"] = infer_input(model_name) if "params_count" not in df[session]: df[session]["params_count"] = count_parameters( get_model(model_name, pretrained=False))["total"] print(df[session]) df = list(df.values()) df.append({ "session": "May07_16_48_rgb_resnet34_fold0", "model_name": "rgb_resnet34", "fold": 0, "b_auc": 0.8449, "c_auc": np.nan, "activation": "relu", "input": "RGB", "params_count": count_parameters(get_model("rgb_resnet34", pretrained=False))["total"], }) df.append({ "session": "May07_16_48_rgb_resnet34_fold0", "model_name": "rgb_resnet34", "fold": 0, "b_auc": 0.8451, "c_auc": np.nan, "activation": "relu", "input": "RGB", "params_count": count_parameters(get_model("rgb_resnet34", pretrained=False))["total"], }) df.append({ "session": "May08_22_42_rgb_resnet34_fold1", "model_name": "rgb_resnet34", "fold": 1, "b_auc": 0.8439, "c_auc": np.nan, "activation": "relu", "input": "RGB", "params_count": count_parameters(get_model("rgb_resnet34", pretrained=False))["total"], }) df.append({ "session": "May09_15_13_rgb_densenet121_fold0_fp16", "model_name": "rgb_densenet121", "fold": 0, "b_auc": 0.8658, "c_auc": 0.8660, "activation": "relu", "input": "RGB", "params_count": count_parameters(get_model("rgb_densenet121", pretrained=False))["total"], }) df.append({ "session": "May11_08_49_rgb_densenet201_fold3_fp16", "model_name": "rgb_densenet201", "fold": 3, "b_auc": 0.8402, "c_auc": 0.8405, "activation": "relu", "input": "RGB", "params_count": count_parameters(get_model("rgb_densenet201", pretrained=False))["total"], }) df.append({ "session": "May13_23_00_rgb_skresnext50_32x4d_fold0_fp16", "model_name": "rgb_skresnext50_32x4d", "fold": 0, "b_auc": 0.9032, "c_auc": 0.9032, "activation": "relu", "input": "RGB", "params_count": count_parameters(get_model("rgb_skresnext50_32x4d", pretrained=False))["total"], }) df.append({ "session": "May13_19_06_rgb_skresnext50_32x4d_fold1_fp16", "model_name": "rgb_skresnext50_32x4d", "fold": 1, "b_auc": 0.9055, "c_auc": 0.9055, "activation": "relu", "input": "RGB", "params_count": count_parameters(get_model("rgb_skresnext50_32x4d", pretrained=False))["total"], }) df.append({ "session": "May12_13_01_rgb_skresnext50_32x4d_fold2_fp16", "model_name": "rgb_skresnext50_32x4d", "fold": 2, "b_auc": 0.9049, "c_auc": 0.9048, "activation": "relu", "input": "RGB", "params_count": count_parameters(get_model("rgb_skresnext50_32x4d", pretrained=False))["total"], }) df.append({ "session": "May11_09_46_rgb_skresnext50_32x4d_fold3_fp16", "model_name": "rgb_skresnext50_32x4d", "fold": 3, "b_auc": 0.8700, "c_auc": 0.8699, "activation": "relu", "input": "RGB", "params_count": count_parameters(get_model("rgb_skresnext50_32x4d", pretrained=False))["total"], }) df.append({ "session": "May15_17_03_ela_skresnext50_32x4d_fold1_fp16", "model_name": "ela_skresnext50_32x4d", "fold": 0, "b_auc": 0.9144, "c_auc": 0.9144, "activation": "relu", "input": "RGB+ELA", "params_count": count_parameters(get_model("rgb_skresnext50_32x4d", pretrained=False))["total"], }) df.append({ "session": "May21_13_28_ela_skresnext50_32x4d_fold2_fp16", "model_name": "ela_skresnext50_32x4d", "fold": 0, "b_auc": 0.9164, "c_auc": 0.9163, "activation": "relu", "input": "RGB+ELA", "params_count": count_parameters(get_model("rgb_skresnext50_32x4d", pretrained=False))["total"], }) df.append({ "session": "May18_20_10_ycrcb_skresnext50_32x4d_fold0_fp16", "model_name": "ycrcb_skresnext50_32x4d", "fold": 0, "b_auc": 0.8266, "c_auc": 0.8271, "activation": "relu", "input": "YCrCb", "params_count": count_parameters(get_model("rgb_skresnext50_32x4d", pretrained=False))["total"], }) df = pd.DataFrame.from_records(df) df.to_csv("summarize.csv", index=False)