Ejemplo n.º 1
0
 def __init__(self,
              channels = 2,
              unet_inchannels = 1,
              unet_outchannels = 1,
              use_cpu = False
              ):
     super(Spleeter, self).__init__()
     self.channels = Config.channels
     self.cnt = 0
     self.sigmoid = nn.Sigmoid()
     # self.mmask = Mmasker()
     for channel in range(channels):
         if(Config.model_name == "Unet"):model = UNet(n_channels = unet_inchannels,n_classes = unet_outchannels)
         elif(Config.model_name == "Demixer"):model = Demixer(n_channels = unet_inchannels,n_classes = unet_outchannels)
         elif(Config.model_name == "DenseUnet"):model = DenseUnet(n_channels_in=unet_inchannels,
                                                                  n_channel_out=unet_outchannels,
                                                                  block_number=Config.dense_block,
                                                                  denselayers=Config.dense_layers,
                                                                  bn_size=Config.dense_bn,
                                                                  growth_rate=Config.dense_growth_rate,
                                                                  dropout=Config.drop_rate)
         elif(Config.model_name == "MbandUnet"):model = MbandUnet(inchannel=unet_inchannels,outchannel=unet_outchannels)
         else:raise ValueError("Error: Non-exist model name")
         if(use_cpu):exec("self.unet{}=model".format(channel))
         else:exec("self.unet{}=model.cuda(Config.device)".format(channel))
Ejemplo n.º 2
0
    "Input file could either be a directory with multiple audio files or just one single audio file"
)

ARGS = parser.parse_args()

log = Logger("PREDICT", ARGS.debug, ARGS.log_dir)
"""
Main function to compute prediction by using a trained model together with the given input
"""
if __name__ == "__main__":

    if ARGS.checkpoint_path is not None:
        log.info("Restoring checkpoint from {} instead of using a model file.".
                 format(ARGS.checkpoint_path))
        checkpoint = torch.load(ARGS.checkpoint_path)
        model = UNet(1, 1, bilinear=False)
        model.load_state_dict(checkpoint["modelState"])
        log.warning(
            "Using default preprocessing options. Provide Model file if they are changed"
        )
        dataOpts = DefaultSpecDatasetOps
    else:
        if ARGS.jit_load:
            extra_files = {}
            extra_files['dataOpts'] = ''
            model = torch.jit.load(ARGS.model_path, _extra_files=extra_files)
            unetState = model.state_dict()
            dataOpts = eval(extra_files['dataOpts'])
            log.debug("Model successfully load via torch jit: " +
                      str(ARGS.model_path))
        else:
Ejemplo n.º 3
0
    patience_lr = math.ceil(ARGS.lr_patience_epochs / ARGS.epochs_per_eval)

    patience_lr = int(max(1, patience_lr))

    log.debug("dataOpts: " + json.dumps(dataOpts, indent=4))

    sequence_len = int(
        float(ARGS.sequence_len) / 1000 * dataOpts["sr"] /
        dataOpts["hop_length"])
    log.debug("Training with sequence length: {}".format(sequence_len))
    input_shape = (ARGS.batch_size, 1, dataOpts["n_freq_bins"], sequence_len)

    log.info("Setting up model")

    unet = UNet(n_channels=1, n_classes=1, bilinear=False)

    log.debug("Model: " + str(unet))
    model = nn.Sequential(OrderedDict([("unet", unet)]))

    split_fracs = {"train": .7, "val": .15, "test": .15}
    input_data = DatabaseCsvSplit(split_fracs,
                                  working_dir=ARGS.data_dir,
                                  split_per_dir=True)

    audio_files = get_audio_files()

    noise_files_train = [
        str(p) for p in pathlib.Path(ARGS.noise_dir_train).glob("*.wav")
    ]
    noise_files_val = [
Ejemplo n.º 4
0
 def __init__(self, z_dim=16, nc=3):
     super(MONet, self).__init__()
     self.z_dim = z_dim
     self.nc = nc
     self.component_vae = Spatial_Decoder(self.z_dim, self.nc + 1)
     self.attention_net = UNet(self.nc + 1, 1)
Ejemplo n.º 5
0
if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO,
                        format='%(levelname)s: %(message)s')
    args = get_args()
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    logging.info(f'Using device {device}')

    # Change here to adapt to your data
    # n_channels=3 for RGB images
    # n_classes is the number of probabilities you want to get per pixel
    #   - For 1 class and background, use n_classes=1
    #   - For 2 classes, use n_classes=1
    #   - For N > 2 classes, use n_classes=N
    if args.model == 'unet':
        if args.dataset == 'TSDDataset_bin':
            net = UNet(n_channels=3, n_classes=1, bilinear=True)
        elif args.dataset == 'TSDDataset_mul':
            net = UNet(n_channels=3, n_classes=3, bilinear=True)

    elif args.model == 'gcn':
        if args.dataset == 'TSDDataset_bin':
            net = FCN_GCN(n_channels=3, n_classes=1, bilinear=True)
        elif args.dataset == 'TSDDataset_mul':
            net = FCN_GCN(n_channels=3, n_classes=3, bilinear=True)

    elif args.model == 'ours':
        if args.dataset == 'TSDDataset_bin':
            net = Road_Seg(n_channels=3, n_classes=1, bilinear=True)
        elif args.dataset == 'TSDDataset_mul':
            net = Road_Seg(n_channels=3, n_classes=3, bilinear=True)
Ejemplo n.º 6
0
                                num_workers=0)
b_val_dataloader = DataLoader(b_val_dataset,
                              batch_size=int(args.batch_size),
                              shuffle=True,
                              num_workers=0)
o_train_dataloader = DataLoader(o_train_dataset,
                                batch_size=int(args.batch_size),
                                shuffle=True,
                                num_workers=0)
o_val_dataloader = DataLoader(o_val_dataset,
                              batch_size=int(args.batch_size),
                              shuffle=True,
                              num_workers=0)

# Load the model
unet = UNet()
segmenter = segmenter()
domain_pred = domain_predictor(2)

if cuda:
    unet = unet.cuda()
    segmenter = segmenter.cuda()
    domain_pred = domain_pred.cuda()

# Make everything parallelisable
unet = nn.DataParallel(unet)
segmenter = nn.DataParallel(segmenter)
domain_pred = nn.DataParallel(domain_pred)

if LOAD_PATH_UNET:
    print('Loading Weights')
Ejemplo n.º 7
0
    def __init__(self, args):
        self.args = args
        self.args.start_epoch = 0
        self.args.cuda = True
        # data transforms
        input_transform = transform.Compose([
            transform.ToTensor(),
            transform.Normalize([.490, .490, .490], [.247, .247, .247])
        ])  # TODO: change mean and std

        # dataset
        train_chain = Compose([
            HorizontalFlip(p=0.5),
            OneOf([
                ElasticTransform(
                    alpha=300, sigma=300 * 0.05, alpha_affine=300 * 0.03),
                GridDistortion(),
                OpticalDistortion(distort_limit=2, shift_limit=0.5),
            ],
                  p=0.3),
            RandomSizedCrop(
                min_max_height=(900, 1024), height=1024, width=1024, p=0.5),
            ShiftScaleRotate(rotate_limit=20, p=0.5),
            Resize(self.args.size, self.args.size)
        ],
                              p=1)

        val_chain = Compose([Resize(self.args.size, self.args.size)], p=1)
        num_fold = self.args.num_fold
        df_train = pd.read_csv(os.path.join(args.imagelist_path, 'train.csv'))
        df_val = pd.read_csv(os.path.join(args.imagelist_path, 'val.csv'))
        df_full = pd.concat((df_train, df_val), ignore_index=True, axis=0)
        df_full['lbl'] = (df_full['mask_name'].astype(str) == '-1').astype(int)
        skf = StratifiedKFold(8, shuffle=True, random_state=777)
        train_ids, val_ids = list(
            skf.split(df_full['mask_name'], df_full['lbl']))[num_fold]

        df_test = pd.read_csv(
            os.path.join(args.imagelist_path, 'test_true.csv'))

        df_new_train = pd.concat((df_full.iloc[train_ids], df_test),
                                 ignore_index=True,
                                 axis=0,
                                 sort=False)
        df_new_val = df_full.iloc[val_ids]

        df_new_train.to_csv(f'/tmp/train_new_pneumo_{num_fold}.csv')
        df_new_val.to_csv(f'/tmp/val_new_pneumo_{num_fold}.csv')

        trainset = SegmentationDataset(f'/tmp/train_new_pneumo_{num_fold}.csv',
                                       args.image_path,
                                       args.masks_path,
                                       input_transform=input_transform,
                                       transform_chain=train_chain,
                                       base_size=1024)
        testset = SegmentationDataset(f'/tmp/val_new_pneumo_{num_fold}.csv',
                                      args.image_path,
                                      args.masks_path,
                                      input_transform=input_transform,
                                      transform_chain=val_chain,
                                      base_size=1024)

        imgs = trainset.mask_img_map[:, [0, 3]]
        weights = make_weights_for_balanced_classes(imgs, 2)
        weights = torch.DoubleTensor(weights)
        train_sampler = (torch.utils.data.sampler.WeightedRandomSampler(
            weights, len(weights)))

        # dataloader
        kwargs = {'num_workers': args.workers, 'pin_memory': True}
        self.trainloader = data.DataLoader(
            trainset,
            batch_size=args.batch_size,
            drop_last=True,
            sampler=train_sampler,  #shuffle=True, 
            **kwargs)
        self.valloader = data.DataLoader(testset,
                                         batch_size=args.batch_size,
                                         drop_last=False,
                                         shuffle=False,
                                         **kwargs)

        self.nclass = 1
        if self.args.model == 'unet':
            model = UNet(n_classes=self.nclass, norm_layer=SyncBatchNorm)
            params_list = [
                {
                    'params': model.parameters(),
                    'lr': args.lr
                },
            ]
        elif self.args.model == 'encnet':
            model = EncNet(
                nclass=self.nclass,
                backbone=args.backbone,
                aux=args.aux,
                se_loss=args.se_loss,
                norm_layer=SyncBatchNorm  #nn.BatchNorm2d
            )

            # optimizer using different LR
            params_list = [
                {
                    'params': model.pretrained.parameters(),
                    'lr': args.lr
                },
            ]
            if hasattr(model, 'head'):
                params_list.append({
                    'params': model.head.parameters(),
                    'lr': args.lr * 10
                })
            if hasattr(model, 'auxlayer'):
                params_list.append({
                    'params': model.auxlayer.parameters(),
                    'lr': args.lr * 10
                })

        print(model)
        optimizer = torch.optim.SGD(params_list,
                                    lr=args.lr,
                                    momentum=0.9,
                                    weight_decay=args.wd)

        # criterions
        if self.nclass == 1:
            self.criterion = SegmentationLossesBCE(se_loss=args.se_loss,
                                                   aux=args.aux,
                                                   nclass=self.nclass,
                                                   se_weight=args.se_weight,
                                                   aux_weight=args.aux_weight,
                                                   use_dice=args.use_dice)
        else:
            self.criterion = SegmentationLosses(
                se_loss=args.se_loss,
                aux=args.aux,
                nclass=self.nclass,
                se_weight=args.se_weight,
                aux_weight=args.aux_weight,
            )
        self.model, self.optimizer = model, optimizer

        self.best_pred = 0.0
        self.model = DataParallelModel(self.model).cuda()
        self.criterion = DataParallelCriterion(self.criterion).cuda()

        # resuming checkpoint
        if args.resume is not None:
            if not os.path.isfile(args.resume):
                raise RuntimeError("=> no checkpoint found at '{}'".format(
                    args.resume))
            checkpoint = torch.load(args.resume)  #, map_location='cpu')
            self.args.start_epoch = checkpoint['epoch']
            state_dict = {k: v for k, v in checkpoint['state_dict'].items()}
            self.model.load_state_dict(state_dict)
            self.optimizer.load_state_dict(checkpoint['optimizer'])
            for g in self.optimizer.param_groups:
                g['lr'] = args.lr
            self.best_pred = checkpoint['best_pred']
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
            print(f'Best dice: {checkpoint["best_pred"]}')
            print(f'LR: {get_lr(self.optimizer):.5f}')

        self.scheduler = ReduceLROnPlateau(self.optimizer,
                                           mode='min',
                                           factor=0.8,
                                           patience=4,
                                           threshold=0.001,
                                           threshold_mode='abs',
                                           min_lr=0.00001)
        self.logger = Logger(args.logger_dir)
        self.step_train = 0
        self.best_loss = 20
        self.step_val = 0