Ejemplo n.º 1
0
def eval_tta(config, augment):
    augment['num_policy'] = 1  # TODO remove
    C.get()
    C.get().conf = config
    cv_ratio_test, cv_fold, save_path = augment['cv_ratio_test'], augment['cv_fold'], augment['save_path']
    print(augment)
    # setup - provided augmentation rules
    C.get().aug = policy_decoder(augment, augment['num_policy'], augment['num_op'])

    # eval
    ckpt = torch.load(save_path)
    model = get_model(ckpt['model_specs']['name'], len(ckpt['labels']), ckpt['model_specs']['training_configs'], local_rank=ckpt['devices']['gpu_index']) #TODO: get model configuration from Retinanet

    if 'model' in ckpt:
        model.load_state_dict(ckpt['model'])
    else:
        model.load_state_dict(ckpt)
    model.eval()
    dataroot = ckpt['model_specs']['data']['home_path']
    mAPs = []
    start_t = time.time()
    for _ in range(augment['num_policy']):  # TODO
        train_dataset, test_dataset = get_data(ckpt['model_specs']['data']['annotation_type'], dataroot,
                                                  split=cv_ratio_test, split_idx=cv_fold)
        # mAP = evaluate(dataset_val, model)
        mAP = evaluate(train_dataset, model) #TODO: adjust from train to testing on randomely selected perecentage every time
        mAPs.append(mAP)
        del train_dataset, test_dataset

    gpu_secs = (time.time() - start_t) * torch.cuda.device_count()
    # reporter(minus_loss=metrics['minus_loss'], top1_valid=metrics['correct'], elapsed_time=gpu_secs, done=True)
    # track.log(minus_loss=metrics['minus_loss'], top1_valid=metrics['correct'], elapsed_time=gpu_secs, done=True)
    tune.report(top1_valid=np.mean(mAPs))
    return np.mean(mAPs)
Ejemplo n.º 2
0
    def __init__(self, model_path, gpu_id=None):
        '''
        初始化pytorch模型
        :param model_path: 模型地址(可以是模型的参数或者参数和计算图一起保存的文件)
        :param gpu_id: 在哪一块gpu上运行
        '''
        self.gpu_id = gpu_id

        if self.gpu_id is not None and isinstance(self.gpu_id, int) and torch.cuda.is_available():
            self.device = torch.device("cuda:%s" % self.gpu_id)
        else:
            self.device = torch.device("cpu")
        print('device:', self.device)
        checkpoint = torch.load(model_path, map_location=self.device)

        # config = checkpoint['config']
        # config['arch']['args']['pretrained'] = False
        # self.net = get_model(model_name='resnet50')
        # self.net = get_model(model_name='efficientnet-b3')
        self.net = get_model(model_name='tf_efficientnet_b2_ns')
        # self.net = get_model(model_name='efficientnet-b2')
        # self.net = get_model(model_name='resnext50_32x4d')

        # self.img_channel = config['data_loader']['args']['dataset']['img_channel']
        # self.net.load_state_dict(checkpoint['state_dict'],False)
        self.net.load_state_dict(
            {k.replace('module.', ''): v for k, v in checkpoint['state_dict'].items()})

        self.net.to(self.device)


        self.net.eval()
        self.img_channel = 3
Ejemplo n.º 3
0
def main(opt):
    # configuration
    train_loader, test_loader, meta_data = get_data(opt)
    backbone = get_model(opt, meta_data['n_class'])

    # train & test
    pass
Ejemplo n.º 4
0
    def build(self,
              model='retinanet',
              depth=50,
              learning_rate=1e-5,
              ratios=[0.5, 1, 2],
              scales=[2**0, 2**(1.0 / 3.0), 2**(2.0 / 3.0)]):
        # model must be string or a model class
        if not callable(model):
            model = get_model(model_name=model,
                              num_classes=self.dataset_train.num_classes,
                              backbone_depth=depth,
                              ratios=ratios,
                              scales=scales,
                              weights_dir=self.weights_dir_path,
                              pretrained=True)

        self.model = model.to(device=self.device)
        self.model.training = True
        self.optimizer = optim.Adam(self.model.parameters(), lr=learning_rate)
        self.scheduler = optim.lr_scheduler.ReduceLROnPlateau(self.optimizer,
                                                              patience=3,
                                                              verbose=True)

        if self.checkpoint is not None:
            self.model.load_state_dict(self.checkpoint['model'])
            self.optimizer.load_state_dict(self.checkpoint['optimizer'])
            self.scheduler.load_state_dict(self.checkpoint['scheduler'])
        self.ratios = ratios
        self.scales = scales
        self.depth = depth
Ejemplo n.º 5
0
    def build_model(self):
        self.G = get_model(self.arch, pretrained=self.indicator).cuda()

        if self.parallel:
            self.G = nn.DataParallel(self.G)
        # Loss and optimizer
        self.g_optimizer = torch.optim.SGD(filter(
            lambda p: p.requires_grad, self.G.parameters()), self.g_lr, self.momentum, self.weight_decay)
Ejemplo n.º 6
0
def model_trainer(model_name,
                  generator,
                  dropout,
                  kernel_size,
                  x_tr,
                  x_val,
                  y_tr,
                  y_val,
                  opt='adam',
                  saving=True):
    # Model loading.
    # First line option: Create new model. Overwrite last one, if exists.
    # Second line option: Load model trained before.
    model = net.get_model(model_name, dropout, kernel_size)
    # model = load_model("models/validated " + model_name + " " + generator)

    if opt == 'adam':
        op = Adam(lr=0.00025, decay=0.0004)
    else:
        op = Nadam(lr=0.00025)
    model.compile(optimizer=op,
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    # Callback settings.
    callback = []
    if saving:
        callback = [
            ModelCheckpoint(filepath="models/validated " + model_name + " " +
                            generator,
                            save_best_only=True)
        ]

    # training
    history = model.fit(x=x_tr,
                        y=y_tr,
                        epochs=1,
                        verbose=0,
                        callbacks=callback,
                        validation_data=(x_val, y_val),
                        shuffle='batch')

    # Saving model. Depends on option in method call.
    if saving:
        model.save("models/" + model_name + " " + generator)

    # Saving history of files.
    history_path = 'toy_models_data/' + model_name + "_history_" + generator + \
                   ' ' + str(kernel_size) + ' ' + str(dropout) + ' ' + opt + ".p"

    with open(history_path, 'w') as file_pi:
        pickle.dump(history.history, file_pi)
        file_pi.close()

    # Free RAM up
    clear_session()
Ejemplo n.º 7
0
def main(args):
    despath = args.savedir
    if not os.path.exists(despath):
        os.mkdir(despath)

    imagedir = os.path.join(args.datadir, 'image.txt')
    labeldir = os.path.join(args.datadir, 'label.txt')

    transform = Transform_test(args.size)
    dataset_test = NeoData_test(imagedir, labeldir, transform)
    loader = DataLoader(dataset_test,
                        num_workers=4,
                        batch_size=1,
                        shuffle=False)  #test data loader

    #eval the result of IoU
    confMatrix = evalIoU.generateMatrixTrainId(evalIoU.args)
    perImageStats = {}
    nbPixels = 0
    usedLr = 0

    model = get_model(args)
    if args.cuda:
        model = model.cuda()
    model.load_state_dict(torch.load(args.model_dir))
    model.eval()
    count = 0
    for step, colign in enumerate(loader):

        img = colign[2].squeeze(0).numpy()  #image-numpy,original image
        images = colign[0]  #image-tensor
        label = colign[1]  #label-tensor

        if args.cuda:
            images = images.cuda()
        inputs = Variable(images, volatile=True)

        outputs = model(inputs)
        out = outputs[0].cpu().max(0)[1].data.squeeze(
            0).byte().numpy()  #index of max-channel

        add_to_confMatrix(outputs, label, confMatrix, perImageStats,
                          nbPixels)  #add result to confusion matrix

        label2img = label2rgb(out, img, n_labels=args.num_classes
                              )  #merge segmented result with original picture
        Image.fromarray(label2img).save(despath + 'label2img_' + str(count) +
                                        '.jpg')
        count += 1
        print("This is the {}th of image!".format(count))

    iouAvgStr, iouTest, classScoreList = cal_iou(
        evalIoU,
        confMatrix)  #calculate mIoU, classScoreList include IoU for each class
    print("IoU on TEST set : ", iouAvgStr)
Ejemplo n.º 8
0
def model_trainer(model_type, generator, dropout=0.5, kernel_size=(3, 3), saving=True):
    # Figures out which path to use, whether it's from usb or 'data/' sub-folder.
    # Creates path to data.h5 file for a generator chosen above.
    file_path = get_ready_names()[generator]

    # Data loading.
    # ones = 0
    # zeros = 0

    with h5.File(file_path) as hf:
        # ytr = hf['train/y']
        # mask = np.full(ytr, fill_value=False, dtype=bool)
        # for i in range(len(ytr)):
        #     if ytr[i] == 1 and ones < 300000:
        #         mask[i] = True
        #         ones += 1
        #     elif ytr[i] == 0 and zeros < 300000:
        #         mask[i] = True
        #         zeros += 1
        #     elif zeros >= 300000 and ones >= 300000:
        #         break
        xtr = hf['train/x'][()]
        ytr = hf['train/y'][()]

    x_val = HDF5Matrix(file_path, 'val/x')
    y_val = HDF5Matrix(file_path, 'val/y')

    # Model loading.
    # First line option: Create new model. Overwrite last one, if exists.
    # Second line option: Load model trained before.
    model = net.get_model(model_type, dropout, kernel_size)
    # model = load_model("models/validated " + model_name + " " + generator)
    model.summary()

    # Preparing callbacks, in terms of saving
    callback = []
    if saving:
        callback.append(ModelCheckpoint(filepath=model_save_dir + "/validated " + model_type + " " + generator,
                                        save_best_only=True))

    history = model.fit(x=xtr, y=ytr, epochs=20,
                        callbacks=callback, validation_data=(x_val, y_val), shuffle='batch')

    # Saves the model from last epoch
    if saving:
        model.save(model_save_dir + model_type + " " + generator)

    # Saves learning data (accuracy and loss on default)
    if not os.path.exists(model_data_save_dir):
        os.makedirs(model_data_save_dir)
    with open(model_data_save_dir + model_type + "_history_" + generator + ".p", 'wb') as file_pi:
        pickle.dump(history.history, file_pi)

    # Needed for memory leak
    clear_session()
Ejemplo n.º 9
0
def _get_model(model_weight_file):
    loss = { 'reconstruction': losses.mean_squared_error, 'predictions': losses.categorical_crossentropy }
    optimizer = SGD(lr=0.005, momentum=0.9, decay=1e-6, nesterov=True)
    metrics = { 'predictions': [categorical_accuracy] }
    class_weight = { 'reconstruction': 0.05, 'predictions': 1. }

    model = get_model()
    model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
    model.load_weights(model_weight_file)

    return model
Ejemplo n.º 10
0
    def __init__(self, model_path, gpu_id=None):
        '''
        初始化pytorch模型
        :param model_path: 模型地址(可以是模型的参数或者参数和计算图一起保存的文件)
        :param gpu_id: 在哪一块gpu上运行
        '''
        self.gpu_id = gpu_id

        if self.gpu_id is not None and isinstance(self.gpu_id, int) and torch.cuda.is_available():
            self.device = torch.device("cuda:%s" % self.gpu_id)
        else:
            self.device = torch.device("cpu")
        print('device:', self.device)
        checkpoint = torch.load(model_path, map_location=self.device)

        # config = checkpoint['config']
        # config['arch']['args']['pretrained'] = False
        # self.net = get_model(model_name='resnet50')
        # self.net = get_model(model_name='efficientnet-b3')
        self.net = get_model(model_name='tf_efficientnet_b2_ns')
        # self.net = get_model(model_name='tf_efficientnet_b3_ns')
        # self.net = geffnet.create_model('tf_efficientnet_b2_ns', pretrained=False,num_classes=38)
        # m.eval()
        # self.net = get_model(model_name='efficientnet-b2')
        # self.net = get_model(model_name='resnext50_32x4d')

        # self.img_channel = config['data_loader']['args']['dataset']['img_channel']
        # self.net.load_state_dict(checkpoint['state_dict'],False)
        self.net.load_state_dict(
            {k.replace('module.', ''): v for k, v in checkpoint['state_dict'].items()})

        self.net.to(self.device)
        # input_tensor = torch.randn([1, 3, 224, 224])
        # torch_onnx_out = torch.onnx.export(self.net, input_tensor, "model.onnx",
        #                                    export_params=True,
        #                                    verbose=True,
        #                                    input_names=['label'],
        #                                    output_names=["synthesized"],
        #                                    opset_version=13)
        self.net.eval()


        # # model.set_swish(memory_efficient=False)
        # # #set the model to inference mode
        # # model.eval()
        #
        # x = torch.randn(1, *input_shape)
        # torch.onnx.export(self.net,
        #                   x,
        #                   export_onnx_file,
        #                   )
        # torch.save(self.net,'model.pth')
        self.img_channel = 3
Ejemplo n.º 11
0
def model_trainer(model_name, generator, dropout=0.5, kernel_size=(3, 3), dense_size=128,
                  saving=True):
    # Figures out which path to use, whether it's from usb or 'data/' sub-folder.
    # Creates path to data.h5 file for a generator chosen above.
    file_path = get_ready_names()[generator]

    # Data loading.
    xtr = HDF5Matrix(file_path, 'train/x')
    ytr = HDF5Matrix(file_path, 'train/y')
    xval = HDF5Matrix(file_path, 'val/x')
    yval = HDF5Matrix(file_path, 'val/y')

    # Model loading.
    # First line option: Create new model. Overwrite last one, if exists.
    # Second line option: Load model trained before.
    model = net.get_model(model_name, dropout, kernel_size, dense_size=dense_size)
    # model = load_model("models/validated " + model_name + " " + generator)
    model.summary()

    # training
    callback = []
    if saving:
        if not os.path.exists('../toy_models'):
            os.makedirs('../toy_models')
        callback = [ModelCheckpoint(filepath="../toy_models/validated " + model_name + " " +
                                             generator + str(dropout), save_best_only=True)]
    history = model.fit(x=xtr, y=ytr, epochs=20, verbose=2, callbacks=callback, validation_data=(xval, yval),
                        shuffle='batch')

    if saving:
        model.save("../toy_models/" + model_name + " " + generator + str(dropout))

    if os.path.exists('../toy_models_data/' + model_name + "_history_" + generator + str(dropout) + ".p"):
        with open('../toy_models_data/' + model_name + "_history_" + generator + str(dropout) + ".p", 'r') as file_pi:
            previous = pickle.load(file_pi)
            current = combine_dict(previous, history.history)
        with open('../toy_models_data/' + model_name + "_history_" + generator + str(dropout) + ".p", 'wb') as file_pi:
            pickle.dump(current, file_pi)
    else:
        if not os.path.exists('../toy_models_data/'):
            os.makedirs('../toy_models_data')
        with open('../toy_models_data/' + model_name + "_history_" + generator + str(dropout) + ".p", 'wb') as file_pi:
            pickle.dump(history.history, file_pi)
    clear_session()
Ejemplo n.º 12
0
def main(args):
    '''
        Train the model and record training options.
    '''
    savedir = '{}'.format(args.savedir)
    modeltxtpath = os.path.join(savedir, 'model.txt')

    if not os.path.exists(savedir):
        os.makedirs(savedir)
    with open(savedir + '/opts.txt', "w") as myfile:  #record options
        myfile.write(str(args))
    model = get_model(args)  #load model
    with open(modeltxtpath, "w") as myfile:  #record model
        myfile.write(str(model))
    if args.cuda:
        model = model.cuda()
    print("========== TRAINING ===========")
    train(args, model)
    print("========== TRAINING FINISHED ===========")
Ejemplo n.º 13
0
    def __init__(self,
                 scenario,
                 evaluation='difference',
                 dataset='cifar10',
                 random_images=True):
        super(ImageClassificationEnv, self).__init__(scenario, evaluation,
                                                     random_images)

        network_architecture = "resnet34" if dataset == 'cifar10' else "resnet50"
        self.model, self.input_size = networks.get_model(
            network_architecture, dataset)
        self.model.eval()
        self.model.to(networks.get_device())

        # TODO We could even use an unlabelled dataset and compare original and modified output
        if dataset == 'cifar10':
            self.dataset = datasets.CIFAR10(root='cifar10',
                                            train=False,
                                            download=True)
            self.pre_transformation = transforms.Compose([])
            obs_size = 32
        elif dataset == 'imagenet':
            self.dataset = datasets.ImageNet(root='imagenet',
                                             split='val',
                                             download=True)
            self.pre_transformation = transforms.Compose([])
            obs_size = 224

        self.num_distinct_images = len(self.dataset)

        self.model_transformation = transforms.Compose([
            transforms.Resize(self.input_size),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225]),
        ])

        self.observation_space = spaces.Box(low=0,
                                            high=255,
                                            shape=(obs_size, obs_size, 3),
                                            dtype=np.uint8)
Ejemplo n.º 14
0
def main():
    train_dataset = ImagesSequence([os.path.join(images_folder, filename) for filename in os.listdir(images_folder)],
                                   batch)

    model, emb_index = get_model(input_shape, emb_size, images_count)
    model.summary()
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    if not os.path.exists(models_path):
        os.makedirs(models_path)

    model.fit(
        train_dataset,
        epochs=epoch,
        callbacks=[
            keras.callbacks.EarlyStopping(monitor="loss", min_delta=0, patience=4, verbose=0, mode="min"),
            ValidationPrint(emb_index),
            keras.callbacks.ModelCheckpoint(
                filepath=os.path.join(models_path, 'model_best.hdf5'),
                save_weights_only=False,
                monitor='val_score',
                mode='min',
                save_best_only=True
            ),
            keras.callbacks.ReduceLROnPlateau(monitor='loss', mode='min', factor=0.2,
                                              patience=2, min_lr=0.00001)
        ]
    )

    model.save(model_name)
    model = keras.Model(inputs=[model.input], outputs=[model.layers[emb_index].output])
    print("Final validation")
    print('Score:', score_model(model, print_similarities=True))

    model = keras.models.load_model(os.path.join(models_path, 'model_best.hdf5'))
    model = keras.Model(inputs=[model.input], outputs=[model.layers[emb_index].output])
    print("Final best validation")
    print('Score:', score_model(model, print_similarities=True))
Ejemplo n.º 15
0
    def __init__(self, args, config):
        self.device = torch.device(
            "cuda" if torch.cuda.is_available() else 'cpu')

        self.mapping_dict = args.mapping

        net = get_model(args, config)
        self.class_names = net.class_names

        if self.mapping_dict is not None:
            self.included_classes = list(self.mapping_dict.keys())
            class_ids = list(self.mapping_dict.values())
            sorted_unique_class_ids = sorted(np.unique(class_ids))
            self.class_names = [
                self.class_names[i] for i in sorted_unique_class_ids
            ]

        self.model = Detector(model=net, device=self.device)
        self.model.eval()

        for param in self.model.parameters():
            param.requires_grad = False
Ejemplo n.º 16
0
def main():

    train_data, val_data = get_dataloader(data_list=config.data_list,
                                          root_dir=config.root_dir,
                                          bs=config.batchsize,
                                          sz=config.size)
    val_criterion = get_loss()
    criterion = get_loss(training=True)
    snapmix_criterion = get_loss(tag='snapmix')
    model = get_model(model_name='efficientnet-b3')
    # model = get_model(model_name='tf_efficientnet_b2_ns')
    # model = get_model(model_name='resnext50_32x4d')
    trainer = Trainer(
        config,
        model,
        criterion,
        val_criterion,
        snapmix_criterion,
        train_data,
        val_data,
    )
    trainer.train()
Ejemplo n.º 17
0
    parser.add_argument(
        "--ckpt",
        nargs="?",
        type=str,
        help="checkpoint for evaluation",
    )

    parser.add_argument(
        "--mode",
        nargs="?",
        type=str,
        help="mode: [rand] [style] [interp] [text]",
    )

    args = parser.parse_args()
    cfg = yaml2config(args.config)

    model = get_model(cfg.model)(cfg, args.config)
    model.load(args.ckpt, cfg.device)
    if args.mode == 'style':
        model.eval_style()
    elif args.mode == 'rand':
        model.eval_rand()
    elif args.mode == 'interp':
        model.eval_interp()
    elif args.mode == 'text':
        model.eval_text()
    else:
        print('Unsupported mode: {} | [rand] [style] [interp]'.format(
            cfg.mode))
Ejemplo n.º 18
0
 def create_model(self):
     return get_model(self.model_name, self.num_classes, self.use_cuda,
                      self.use_parallel)
Ejemplo n.º 19
0
def main(conf):

    warnings.filterwarnings("ignore")
    best_score = 0.
    val_score = 0
    val_loss = 0
    epoch_start = 0


    # dataloader
    #train_loader,val_loader = get_dataloader(conf)
    train_loader , val_loader = initialize()
    # model
    model = networks.get_model(conf)
    #print(model)
    #	exit(-1)
    model = nn.DataParallel(model).cuda()

    if conf.weightfile is not None:
        wmodel = networks.get_model(conf)
        wmodel = nn.DataParallel(wmodel).cuda()
        checkpoint_dict = load_checkpoint(wmodel, conf.weightfile)
        if 'best_score' in checkpoint_dict:
            print('best score: {}'.format(best_score))
    else:
        wmodel = model

    # training setting
    criterion,optimizer,scheduler = get_train_setting(model,conf)

    # training and evaluate process for each epoch
    train,validate = get_proc(conf)

    if conf.resume:
        checkpoint_dict = load_checkpoint(model, conf.resume)
        epoch_start = checkpoint_dict['epoch']
        if 'best_score' in checkpoint_dict:
            best_score = checkpoint_dict['best_score']
            print('best score: {}'.format(best_score))
        print('Resuming training process from epoch {}...'.format(epoch_start))
        optimizer.load_state_dict(checkpoint_dict['optimizer'])
        scheduler.load_state_dict(checkpoint_dict['scheduler'])
        print('Resuming lr scheduler')
        print(checkpoint_dict['scheduler'])

    if conf.evaluate:
        print( validate(val_loader, model,criterion, conf))
        return

    detach_epoch = conf.epochs + 1
    if 'detach_epoch' in conf:
        detach_epoch = conf.detach_epoch

    start_eval = 0
    if 'start_eval' in conf:
        start_eval = conf.start_eval


    ## ------main loop-----
    for epoch in range(epoch_start, conf.epochs):
        lr = optimizer.param_groups[0]['lr']
        logging.info("Epoch: [{} | {} LR: {}".format(epoch+1,conf.epochs,lr))

        if epoch == detach_epoch:
            model.module.set_detach(False)
#        test(model , train_loader)
        tmp_loss  = train(train_loader, model, criterion, optimizer, conf,wmodel)
        infostr = {'Epoch:  {}   train_loss: {}'.format(epoch+1,tmp_loss)}
        logging.info(infostr)
        scheduler.step()

        if True:
            with torch.no_grad():
                val_score,val_loss,mscore,ascore = validate(val_loader, model,criterion, conf)
                comscore = val_score
                if 'midlevel' in conf:
                    if conf.midlevel:
                        comscore = ascore
                is_best = comscore > best_score
                best_score = max(comscore,best_score)
                infostr = {'Epoch:  {:.4f}   loss: {:.4f},gs: {:.4f},bs:{:.4f} ,ms:{:.4f},as:{:.4f}'.format(epoch+1,val_loss,val_score,best_score,mscore,ascore)}
                logging.info(infostr)
                save_checkpoint(
                        {'epoch': epoch + 1,
                        'state_dict': model.module.state_dict(),
                        'optimizer' : optimizer.state_dict(),
                        'scheduler' : scheduler.state_dict(),
                        'best_score': best_score
                        }, is_best, outdir=conf['outdir'])
        test(model , train_loader)
    print('Best val acc: {}'.format(best_score))
    return 0
from callbacks import get_callbacks
from tensorflow.python.keras.models import load_model

if __name__ == '__main__':
    model_dir = create_model_dir(args=args)
    args.model_dir = model_dir
    if args.only_test is True:
        model = load_model(args.model_dir + '/best.h5')
        print("Test model has been loaded:", args.model_dir + '/best.h5 ...')
    else:
        if args.resume is True:
            model = load_model(args.model_dir + '/best.h5')
            print("Last best model has been loaded:",
                  args.model_dir + '/best.h5 ...')
        else:
            model = get_model(args=args)
            print("Model has been initialized ...")

    optimizer = get_optimizer(args=args)
    model.compile(loss=args.use_loss, optimizer=optimizer, metrics=['acc'])
    print(model.summary())
    if args.only_test is not True:
        train_generator, test_generator = get_data_generator(args=args)
        callbacks = get_callbacks(args=args)
        print("Start training process..")
        model.fit_generator(
            train_generator,
            steps_per_epoch=train_generator.samples // args.batch_size,
            validation_data=test_generator,
            validation_steps=test_generator.samples // args.batch_size,
            workers=args.num_workers,
Ejemplo n.º 21
0
from tensorflow.python.keras.metrics import categorical_accuracy
from tensorflow.python.keras.optimizers import SGD
from tensorflow.python.keras.models import Model
import numpy as np
import h5py

model_weight_file = '/home/adnan/InnerEye-Machine-Learning/Model_V1-175-0.8070.h5'
loss = {
    'reconstruction': losses.mean_squared_error,
    'predictions': losses.categorical_crossentropy
}
optimizer = SGD(lr=0.005, momentum=0.9, decay=1e-6, nesterov=True)
metrics = {'predictions': [categorical_accuracy]}
class_weight = {'reconstruction': 0.05, 'predictions': 1.}

model = get_model()
model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
model.load_weights(model_weight_file)

hf = h5py.File(
    '/home/adnan/Datasets/New-InnerEye-dataset/new-innereye-dataset-multi-labeled-64x64.h5',
    'r')
X_test, y_test = hf['X_test'][:], hf['y_test'][:]

count = 0

for c, (img, y) in enumerate(zip(X_test, y_test)):
    img = img.astype('float32') / 255
    img = img.reshape((1, 64, 64, 3))
    prediction = np.argmax(model.predict([img])[1])
    if prediction == y:
Ejemplo n.º 22
0
        evalIoU.getScoreAverage(classScoreList, evalIoU.args),
        evalIoU.args) + "{avg:5.3f}".format(avg=evalIoU.getScoreAverage(
            classScoreList, evalIoU.args)) + evalIoU.args.nocol
    iou = float(evalIoU.getScoreAverage(classScoreList, evalIoU.args))
    return iouAvgStr, iou, classScoreList


if __name__ == '__main__':

    def get_loader(args):
        dataset_train = SRdata(
            args
        )  # (imagepath_train, labelpath_train, train_transform) #DataSet
        dataset_val = SRdata(
            args)  # NeoData(imagepath_val, labelpath_val, val_transform)
        loader = DataLoader(dataset_train,
                            num_workers=args.num_workers,
                            batch_size=args.batch_size,
                            shuffle=True)
        loader_val = DataLoader(dataset_val,
                                num_workers=args.num_workers,
                                batch_size=2,
                                shuffle=False)
        return loader, loader_val

    parser = TrainOptions().parse()
    loader, loader_val = get_loader(parser)
    model = get_model(parser)  # load model
    criterion = CrossEntropyLoss2d()

    eval(parser, model, loader, criterion, 1)
Ejemplo n.º 23
0
def train_and_eval(
        tag, dataroot, metric="last", save_path=None, only_eval=False, unsupervised=False, labeled_sample_num=4000,
):
    max_epoch = C.get()["epoch"]
    trainloader, unsuploader, testloader = get_dataloaders(
        C.get()["dataset"], C.get()["batch"], C.get()["batch_unsup"], dataroot, labeled_sample_num
    )

    # create a model & an optimizer
    model = get_model(
        C.get()["model"], num_class(C.get()["dataset"]), data_parallel=True
    )

    criterion = nn.CrossEntropyLoss()
    if C.get()["optimizer"]["type"] == "sgd":
        optimizer = optim.SGD(
            model.parameters(),
            lr=C.get()["lr"],
            momentum=C.get()["optimizer"].get("momentum", 0.9),
            weight_decay=C.get()["optimizer"]["decay"],
            nesterov=C.get()["optimizer"]["nesterov"],
        )
    else:
        raise ValueError("invalid optimizer type=%s" % C.get()["optimizer"]["type"])

    lr_scheduler_type = C.get()["lr_schedule"].get("type", "cosine")
    if lr_scheduler_type == "cosine":
        t_max = C.get()["epoch"]
        if C.get()["lr_schedule"].get("warmup", None):
            t_max -= C.get()["lr_schedule"]["warmup"]["epoch"]
        scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
            optimizer, T_max=t_max, eta_min=0.0
        )
    else:
        raise ValueError("invalid lr_schduler=%s" % lr_scheduler_type)

    if C.get()["lr_schedule"].get("warmup", None):
        scheduler = GradualWarmupScheduler(
            optimizer,
            multiplier=C.get()["lr_schedule"]["warmup"]["multiplier"],
            total_epoch=C.get()["lr_schedule"]["warmup"]["epoch"],
            after_scheduler=scheduler,
        )

    if not tag.strip():
        from metrics import SummaryWriterDummy as SummaryWriter

        logger.warning("tag not provided, no tensorboard log.")
    else:
        from tensorboardX import SummaryWriter
    writers = [
        SummaryWriter(logdir="./logs/%s/%s" % (tag, x)) for x in ["train", "test"]
    ]

    result = OrderedDict()
    epoch_start = 1
    if save_path and os.path.exists(save_path) and (args.resume == True):
        data = torch.load(save_path)
        model.load_state_dict(data["model"])
        optimizer.load_state_dict(data["optimizer"])
        epoch_start = data["epoch"]
        print("load sucessfully")

    if only_eval:
        logger.info("evaluation only+")
        model.eval()
        rs = dict()
        rs["test"] = run_epoch(
            model,
            testloader,
            unsuploader,
            criterion,
            None,
            desc_default="*test",
            epoch=epoch_start,
            writer=writers[1],
            method=args.method,
        )
        for key, setname in itertools.product(
                ["loss", "top1", "top5"], ["train", "test"]
        ):
            result["%s_%s" % (key, setname)] = rs[setname][key]
        result["epoch"] = 0
        return result

    # train loop
    global best_valid_top1
    best_valid_loss = 10e10
    for epoch in range(epoch_start, max_epoch + 1):
        model.train()
        rs = dict()
        rs["train"] = run_epoch(
            model,
            trainloader,
            unsuploader,
            criterion,
            optimizer,
            desc_default="train",
            epoch=epoch,
            writer=writers[0],
            verbose=True,
            unsupervised=unsupervised,
            scheduler=scheduler,
            method=args.method,
        )
        if math.isnan(rs["train"]["loss"]):
            raise Exception("train loss is NaN.")

        model.eval()
        if (
                epoch % (10 if "cifar" in C.get()["dataset"] else 30) == 0
                or epoch == max_epoch
        ):
            rs["test"] = run_epoch(
                model,
                testloader,
                unsuploader,
                criterion,
                None,
                desc_default="*test",
                epoch=epoch,
                writer=writers[1],
                verbose=True,
                method=args.method
            )

            if best_valid_top1 < rs["test"]["top1"]:
                best_valid_top1 = rs["test"]["top1"]

            if metric == "last" or rs[metric]["loss"] < best_valid_loss:  # TODO
                if metric != "last":
                    best_valid_loss = rs[metric]["loss"]
                for key, setname in itertools.product(
                        ["loss", "top1", "top5"], ["train", "test"]
                ):
                    result["%s_%s" % (key, setname)] = rs[setname][key]
                result["epoch"] = epoch

                writers[1].add_scalar("test_top1/best", rs["test"]["top1"], epoch)

            # save checkpoint
            if save_path:
                logger.info("save model@%d to %s" % (epoch, save_path))
                torch.save(
                    {
                        "epoch": epoch,
                        "log": {
                            "train": rs["train"].get_dict(),
                            "test": rs["test"].get_dict(),
                        },
                        "optimizer": optimizer.state_dict(),
                        "model": model.state_dict(),
                    },
                    save_path,
                )

    del model

    return result
Ejemplo n.º 24
0
def train_and_eval(config, tag, dataroot, test_ratio=0.0, cv_fold=0, reporter=None, metric='last', save_path=None, only_eval=False, local_rank=0, evaluation_interval=5):

    # ckpt = torch.load(save_path)

    total_batch = config["batch"]
    if local_rank >= 0:
        dist.init_process_group(backend='nccl', init_method='env://', world_size=int(os.environ['WORLD_SIZE']))
        device = torch.device('cuda', local_rank)
        torch.cuda.set_device(device)

        config()['lr'] *= dist.get_world_size()
        logger.info(f'local batch={config["batch"]} world_size={dist.get_world_size()} ----> total batch={config["batch"] * dist.get_world_size()}')
        total_batch = config["batch"] * dist.get_world_size()

    is_master = local_rank < 0 or dist.get_rank() == 0
    if is_master:
        add_filehandler(logger, 'master' + '.log')

    if not reporter:
        reporter = lambda **kwargs: 0

    max_epoch = config['epoch']
    trainsampler, trainloader, validloader, testloader_ = get_dataloaders(config['dataset'], config['batch'], dataroot, test_ratio, split_idx=cv_fold, multinode=(local_rank >= 0))

    # create a model & an optimizer
    model = get_model(config['model'], num_class(config['dataset']), local_rank=local_rank)
    model_ema = get_model(config['model'], num_class(config['dataset']), local_rank=-1)
    model_ema.eval()

    criterion_ce = criterion = CrossEntropyLabelSmooth(num_class(config['dataset']), config.get('lb_smooth', 0))
    if config.get('mixup', 0.0) > 0.0:
        criterion = CrossEntropyMixUpLabelSmooth(num_class(config['dataset']), config.get('lb_smooth', 0))
    if config['optimizer']['type'] == 'sgd':
        optimizer = optim.SGD(
            model.parameters(),
            lr=config['lr'],
            momentum=config['optimizer'].get('momentum', 0.9),
            weight_decay=0.0,
            nesterov=config['optimizer'].get('nesterov', True)
        )
    elif config['optimizer']['type'] == 'rmsprop':
        optimizer = RMSpropTF(
            model.parameters(),
            lr=config['lr'],
            weight_decay=0.0,
            alpha=0.9, momentum=0.9,
            eps=0.001
        )
    else:
        raise ValueError('invalid optimizer type=%s' % config['optimizer']['type'])

    lr_scheduler_type = config['lr_schedule'].get('type', 'cosine')
    if lr_scheduler_type == 'cosine':
        scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=config['epoch'], eta_min=0.)
    elif lr_scheduler_type == 'resnet':
        scheduler = adjust_learning_rate_resnet(optimizer)
    elif lr_scheduler_type == 'efficientnet':
        scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda x: 0.97 ** int((x + config['lr_schedule']['warmup']['epoch']) / 2.4))
    else:
        raise ValueError('invalid lr_schduler=%s' % lr_scheduler_type)

    if config['lr_schedule'].get('warmup', None) and config['lr_schedule']['warmup']['epoch'] > 0:
        scheduler = GradualWarmupScheduler(
            optimizer,
            multiplier=config['lr_schedule']['warmup']['multiplier'],
            total_epoch=config['lr_schedule']['warmup']['epoch'],
            after_scheduler=scheduler
        )

    if not tag or not is_master:
        from FastAutoAugment.metrics import SummaryWriterDummy as SummaryWriter
        logger.warning('tag not provided, no tensorboard log.')
    else:
        from tensorboardX import SummaryWriter
    writers = [SummaryWriter(log_dir='./logs/%s/%s' % (tag, x)) for x in ['train', 'valid', 'test']]

    if config['optimizer']['ema'] > 0.0 and is_master:
        # https://discuss.pytorch.org/t/how-to-apply-exponential-moving-average-decay-for-variables/10856/4?u=ildoonet
        ema = EMA(config['optimizer']['ema'])
    else:
        ema = None

    result = OrderedDict()
    epoch_start = 1
    #TODO: change only eval=False when without save_path ??
    if save_path != 'test.pth':     # and is_master: --> should load all data(not able to be broadcasted)
        if save_path and not os.path.exists(save_path):
            import torch.utils.model_zoo as model_zoo
            data = model_zoo.load_url('https://download.pytorch.org/models/resnet50-19c8e357.pth',
                               model_dir=os.path.join(os.getcwd(), 'FastAutoAugment/models'))
            if config['dataset'] == 'cifar10':
                data.pop('fc.weight')
                data.pop('fc.bias')
                model_dict = model.state_dict()
                model_dict.update(data)
                model.load_state_dict(model_dict)
                torch.save(model_dict, save_path)

        logger.info('%s file found. loading...' % save_path)
        data = torch.load(save_path)
        key = 'model' if 'model' in data else 'state_dict'

        if 'epoch' not in data:
            model.load_state_dict(data)
        else:
            logger.info('checkpoint epoch@%d' % data['epoch'])
            if not isinstance(model, (DataParallel, DistributedDataParallel)):
                model.load_state_dict({k.replace('module.', ''): v for k, v in data[key].items()})
            else:
                model.load_state_dict({k if 'module.' in k else 'module.'+k: v for k, v in data[key].items()})
            logger.info('optimizer.load_state_dict+')
            optimizer.load_state_dict(data['optimizer'])
            if data['epoch'] < config['epoch']:
                epoch_start = data['epoch']
            else:
                only_eval = True
            if ema is not None:
                ema.shadow = data.get('ema', {}) if isinstance(data.get('ema', {}), dict) else data['ema'].state_dict()
        del data

    if local_rank >= 0:
        for name, x in model.state_dict().items():
            dist.broadcast(x, 0)
        logger.info(f'multinode init. local_rank={dist.get_rank()} is_master={is_master}')
        torch.cuda.synchronize()

    tqdm_disabled = bool(os.environ.get('TASK_NAME', '')) and local_rank != 0  # KakaoBrain Environment

    if only_eval:
        logger.info('evaluation only+')
        model.eval()
        rs = dict()
        rs['train'] = run_epoch(config, model, trainloader, criterion, None, desc_default='train', epoch=0, writer=writers[0], is_master=is_master)

        with torch.no_grad():
            rs['valid'] = run_epoch(config, model, validloader, criterion, None, desc_default='valid', epoch=0, writer=writers[1], is_master=is_master)
            rs['test'] = run_epoch(config, model, testloader_, criterion, None, desc_default='*test', epoch=0, writer=writers[2], is_master=is_master)
            if ema is not None and len(ema) > 0:
                model_ema.load_state_dict({k.replace('module.', ''): v for k, v in ema.state_dict().items()})
                rs['valid'] = run_epoch(config, model_ema, validloader, criterion_ce, None, desc_default='valid(EMA)', epoch=0, writer=writers[1], verbose=is_master, tqdm_disabled=tqdm_disabled)
                rs['test'] = run_epoch(config, model_ema, testloader_, criterion_ce, None, desc_default='*test(EMA)', epoch=0, writer=writers[2], verbose=is_master, tqdm_disabled=tqdm_disabled)
        for key, setname in itertools.product(['loss', 'top1', 'top5'], ['train', 'valid', 'test']):
            if setname not in rs:
                continue
            result['%s_%s' % (key, setname)] = rs[setname][key]
        result['epoch'] = 0
        return result

    # train loop
    best_top1 = 0
    for epoch in range(epoch_start, max_epoch + 1):
        if local_rank >= 0:
            trainsampler.set_epoch(epoch)

        model.train()
        rs = dict()
        rs['train'] = run_epoch(config, model, trainloader, criterion, optimizer, desc_default='train', epoch=epoch, writer=writers[0], verbose=(is_master and local_rank <= 0), scheduler=scheduler, ema=ema, wd=config['optimizer']['decay'], tqdm_disabled=tqdm_disabled)
        model.eval()

        if math.isnan(rs['train']['loss']):
            raise Exception('train loss is NaN.')

        if ema is not None and config['optimizer']['ema_interval'] > 0 and epoch % config['optimizer']['ema_interval'] == 0:
            logger.info(f'ema synced+ rank={dist.get_rank()}')
            if ema is not None:
                model.load_state_dict(ema.state_dict())
            for name, x in model.state_dict().items():
                # print(name)
                dist.broadcast(x, 0)
            torch.cuda.synchronize()
            logger.info(f'ema synced- rank={dist.get_rank()}')

        if is_master and (epoch % evaluation_interval == 0 or epoch == max_epoch):
            with torch.no_grad():
                rs['valid'] = run_epoch(config, model, validloader, criterion_ce, None, desc_default='valid', epoch=epoch, writer=writers[1], verbose=is_master, tqdm_disabled=tqdm_disabled)
                rs['test'] = run_epoch(config, model, testloader_, criterion_ce, None, desc_default='*test', epoch=epoch, writer=writers[2], verbose=is_master, tqdm_disabled=tqdm_disabled)

                if ema is not None:
                    model_ema.load_state_dict({k.replace('module.', ''): v for k, v in ema.state_dict().items()})
                    rs['valid'] = run_epoch(config, model_ema, validloader, criterion_ce, None, desc_default='valid(EMA)', epoch=epoch, writer=writers[1], verbose=is_master, tqdm_disabled=tqdm_disabled)
                    rs['test'] = run_epoch(config, model_ema, testloader_, criterion_ce, None, desc_default='*test(EMA)', epoch=epoch, writer=writers[2], verbose=is_master, tqdm_disabled=tqdm_disabled)

            logger.info(
                f'epoch={epoch} '
                f'[train] loss={rs["train"]["loss"]:.4f} top1={rs["train"]["top1"]:.4f} '
                f'[valid] loss={rs["valid"]["loss"]:.4f} top1={rs["valid"]["top1"]:.4f} '
                f'[test] loss={rs["test"]["loss"]:.4f} top1={rs["test"]["top1"]:.4f} '
            )

            if metric == 'last' or rs[metric]['top1'] > best_top1:
                if metric != 'last':
                    best_top1 = rs[metric]['top1']
                for key, setname in itertools.product(['loss', 'top1', 'top5'], ['train', 'valid', 'test']):
                    result['%s_%s' % (key, setname)] = rs[setname][key]
                result['epoch'] = epoch

                writers[1].add_scalar('valid_top1/best', rs['valid']['top1'], epoch)
                writers[2].add_scalar('test_top1/best', rs['test']['top1'], epoch)

                reporter(
                    loss_valid=rs['valid']['loss'], top1_valid=rs['valid']['top1'],
                    loss_test=rs['test']['loss'], top1_test=rs['test']['top1']
                )

                # save checkpoint
                if is_master and save_path:
                    logger.info('save model@%d to %s, err=%.4f' % (epoch, save_path, 1 - best_top1))
                    torch.save({
                        'epoch': epoch,
                        'log': {
                            'train': rs['train'].get_dict(),
                            'valid': rs['valid'].get_dict(),
                            'test': rs['test'].get_dict(),
                        },
                        'optimizer': optimizer.state_dict(),
                        'model': model.state_dict(),
                        'ema': ema.state_dict() if ema is not None else None,
                    }, save_path)

    del model

    result['top1_test'] = best_top1
    return result
Ejemplo n.º 25
0
def eval_tta(config, augment, reporter):
    C.get()
    C.get().conf = config
    cv_ratio_test, cv_fold, save_path = augment['cv_ratio_test'], augment[
        'cv_fold'], augment['save_path']

    # setup - provided augmentation rules
    C.get()['aug'] = policy_decoder(augment, augment['num_policy'],
                                    augment['num_op'])

    # eval
    model = get_model(C.get()['model'], num_class(C.get()['dataset']))
    ckpt = torch.load(save_path + '.pth')
    if 'model' in ckpt:
        model.load_state_dict(ckpt['model'])
    else:
        model.load_state_dict(ckpt)
    model = nn.DataParallel(model).cuda()
    model.eval()

    src_loaders = []
    # for _ in range(augment['num_policy']):
    _, src_tl, src_validloader, src_ttl = get_dataloaders(
        C.get()['dataset'],
        C.get()['batch'],
        augment['dataroot'],
        cv_ratio_test,
        cv_num,
        split_idx=cv_fold,
        target=False,
        random_range=C.get()['args'].random_range)

    del src_tl, src_ttl

    start_t = time.time()
    metrics = Accumulator()
    loss_fn = torch.nn.CrossEntropyLoss(reduction='none')

    emd_loss = nn.DataParallel(emdModule()).cuda()

    losses = []
    corrects = []
    for data in src_validloader:
        with torch.no_grad():
            point_cloud = data['point_cloud'].cuda()
            label = torch.ones_like(data['label'], dtype=torch.int64).cuda()
            trans_pc = data['transformed']

            pred = model(trans_pc)

            if C.get()['args'].use_emd_false:
                loss_emd = (torch.mean(emd_loss(point_cloud.permute(0, 2, 1),
                                                trans_pc.permute(0, 2, 1), 0.05, 3000)[0])).unsqueeze(0) \
                           * C.get()['args'].emd_coeff
            else:
                loss_emd = torch.tensor([0.0])

            if C.get()['args'].no_dc:
                loss = loss_emd
            else:
                loss = loss_emd + loss_fn(pred, label)
            # print(loss)
            losses.append(loss.detach().cpu().numpy())

            pred = pred.max(dim=1)[1]
            pred = pred.t()
            correct = float(
                torch.sum(pred == label).item()) / pred.size(0) * 100
            corrects.append(correct)
            del loss, correct, pred, data, label, loss_emd

    losses = np.concatenate(losses)
    losses_min = np.min(losses, axis=0).squeeze()
    corrects_max = max(corrects)
    metrics.add_dict({
        'minus_loss': -1 * np.sum(losses_min),
        'correct': np.sum(corrects_max),
        # 'cnt': len(corrects_max)
    })
    del corrects, corrects_max

    del model
    # metrics = metrics / 'cnt'
    gpu_secs = (time.time() - start_t) * torch.cuda.device_count()
    # print(metrics)
    reporter(minus_loss=metrics['minus_loss'],
             top1_valid=metrics['correct'],
             elapsed_time=gpu_secs,
             done=True)
    return metrics['minus_loss']
Ejemplo n.º 26
0
from datetime import datetime
import argparse

from lib.utils import yaml2config
from networks import get_model

try:
    from torch.utils.tensorboard import SummaryWriter
except ModuleNotFoundError:
    from tensorboardX import SummaryWriter

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="config")
    parser.add_argument(
        "--config",
        nargs="?",
        type=str,
        default="configs/gan_iam.yml",
        help="Configuration file to use",
    )

    args = parser.parse_args()
    cfg = yaml2config(args.config)
    run_id = datetime.strftime(datetime.now(), '%m-%d-%H-%M')
    logdir = os.path.join(
        "runs",
        os.path.basename(args.config)[:-4] + '-' + str(run_id))

    model = get_model(cfg.model)(cfg, logdir)
    model.train()
Ejemplo n.º 27
0
def train_and_eval(tag,
                   dataroot,
                   metric='last',
                   save_path=None,
                   only_eval=False,
                   unsupervised=False,
                   mode=None):
    max_epoch = C.get()['epoch']
    trainloader, unsuploader, testloader = get_dataloaders(
        C.get()['dataset'],
        C.get()['batch'],
        C.get()['batch_unsup'],
        dataroot,
        mode=mode,
        n_labeled=args.n_labeled)

    # create a model & an optimizer
    model = get_model(C.get()['model'],
                      num_class(C.get()['dataset']),
                      data_parallel=True)

    criterion = nn.CrossEntropyLoss()
    if C.get()['optimizer']['type'] == 'sgd':
        optimizer = optim.SGD(model.parameters(),
                              lr=C.get()['lr'],
                              momentum=C.get()['optimizer'].get(
                                  'momentum', 0.9),
                              weight_decay=C.get()['optimizer']['decay'],
                              nesterov=C.get()['optimizer']['nesterov'])
    else:
        raise ValueError('invalid optimizer type=%s' %
                         C.get()['optimizer']['type'])

    lr_scheduler_type = C.get()['lr_schedule'].get('type', 'cosine')
    if lr_scheduler_type == 'cosine':
        t_max = C.get()['epoch']
        if C.get()['lr_schedule'].get('warmup', None):
            t_max -= C.get()['lr_schedule']['warmup']['epoch']
        scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer,
                                                               T_max=t_max,
                                                               eta_min=0.)
    else:
        raise ValueError('invalid lr_schduler=%s' % lr_scheduler_type)

    if C.get()['lr_schedule'].get('warmup', None):
        scheduler = GradualWarmupScheduler(
            optimizer,
            multiplier=C.get()['lr_schedule']['warmup']['multiplier'],
            total_epoch=C.get()['lr_schedule']['warmup']['epoch'],
            after_scheduler=scheduler)

    if not tag.strip():
        from metrics import SummaryWriterDummy as SummaryWriter
        logger.warning('tag not provided, no tensorboard log.')
    else:
        from tensorboardX import SummaryWriter
    writers = [
        SummaryWriter(logdir='./logs/%s/%s' % (tag, x))
        for x in ['train', 'test']
    ]

    result = OrderedDict()
    epoch_start = 1
    if save_path and os.path.exists(save_path):
        data = torch.load(save_path)
        model.load_state_dict(data['model'])
        optimizer.load_state_dict(data['optimizer'])
        epoch_start = data['epoch']

    if only_eval:
        logger.info('evaluation only+')
        model.eval()
        rs = dict()
        rs['test'] = run_epoch(model,
                               testloader,
                               unsuploader,
                               criterion,
                               None,
                               desc_default='*test',
                               epoch=epoch_start,
                               writer=writers[1])
        for key, setname in itertools.product(['loss', 'top1', 'top5'],
                                              ['train', 'test']):
            result['%s_%s' % (key, setname)] = rs[setname][key]
        result['epoch'] = 0
        return result

    # train loop
    global best_valid_top1
    best_valid_loss = 10e10
    for epoch in range(epoch_start, max_epoch + 1):
        model.train()
        rs = dict()
        if args.train_mode == 'small':
            print(f'only small')
            rs['train'] = run_epoch(model,
                                    trainloader,
                                    unsuploader,
                                    criterion,
                                    optimizer,
                                    desc_default='train',
                                    epoch=epoch,
                                    writer=writers[0],
                                    verbose=True,
                                    unsupervised=False,
                                    scheduler=scheduler)
        else:
            rs['train'] = run_epoch(model,
                                    trainloader,
                                    unsuploader,
                                    criterion,
                                    optimizer,
                                    desc_default='train',
                                    epoch=epoch,
                                    writer=writers[0],
                                    verbose=True,
                                    unsupervised=unsupervised,
                                    scheduler=scheduler)
        if math.isnan(rs['train']['loss']):
            raise Exception('train loss is NaN.')

        model.eval()
        if epoch % (10 if 'cifar' in C.get()['dataset'] else
                    30) == 0 or epoch == max_epoch:
            rs['test'] = run_epoch(model,
                                   testloader,
                                   unsuploader,
                                   criterion,
                                   None,
                                   desc_default='*test',
                                   epoch=epoch,
                                   writer=writers[1],
                                   verbose=True)

            if best_valid_top1 < rs['test']['top1']:
                best_valid_top1 = rs['test']['top1']

            if metric == 'last' or rs[metric]['loss'] < best_valid_loss:  # TODO
                if metric != 'last':
                    best_valid_loss = rs[metric]['loss']
                for key, setname in itertools.product(['loss', 'top1', 'top5'],
                                                      ['train', 'test']):
                    result['%s_%s' % (key, setname)] = rs[setname][key]
                result['epoch'] = epoch

                writers[1].add_scalar('test_top1/best', rs['test']['top1'],
                                      epoch)

            # save checkpoint
            if save_path:
                logger.info('save model@%d to %s' % (epoch, save_path))
                torch.save(
                    {
                        'epoch': epoch,
                        'log': {
                            'train': rs['train'].get_dict(),
                            'test': rs['test'].get_dict(),
                        },
                        'optimizer': optimizer.state_dict(),
                        'model': model.state_dict()
                    }, save_path)

    del model

    return result
Ejemplo n.º 28
0
 def build_model(self):
     self.G = get_model(self.arch, pretrained=False).cuda()
     if self.parallel:
         self.G = nn.DataParallel(self.G)
Ejemplo n.º 29
0
logger = get_logger('learn2test')
logger.setLevel(logging.DEBUG)


if __name__ == '__main__':
    parser = ConfigArgumentParser(conflict_handler='resolve')
    parser.add_argument('--test-batch', type=int, default=32)
    parser.add_argument('--tta', type=str, default='center')
    parser.add_argument('--deform', type=str, default='')
    parser.add_argument('--corrupt', type=str, default='')
    args = parser.parse_args()

    assert args.dataset == 'imagenet'

    model_target = get_model(args.target_network, gpus=[0], num_classes=args.num_classes, train_aug=args.target_aug).eval()
    profiler = Profiler(model_target)
    print('target network, FLOPs=', profiler.flops(torch.zeros((1, 3, C.get()['target_size'], C.get()['target_size'])).cuda(), ))

    scaled_size = int(math.floor(args.target_size / 0.875))

    if args.deform != '':
        deform_type, deform_level = args.deform.split(' ')
        if deform_type in ['rotate', 'rotation']:
            t = torchvision.transforms.Lambda(lambda img_orig: torchvision.transforms.functional.rotate(img_orig, int(deform_level), resample=PIL.Image.BICUBIC))
        elif deform_type == 'bright':
            t = torchvision.transforms.Lambda(lambda img_orig: torchvision.transforms.functional.adjust_brightness(img_orig, float(deform_level)))
        elif deform_type == 'zoom':
            resize = int(scaled_size * float(deform_level))
            t = torchvision.transforms.Lambda(lambda img_orig: torchvision.transforms.functional.resize(img_orig, resize, interpolation=PIL.Image.BICUBIC))
        elif deform_type:
Ejemplo n.º 30
0
    def __getitem__(self, idx):
        if self.target_network is None:
            self.target_network = get_model(self.target_network_name,
                                            gpus=[0],
                                            num_classes=self.dataset.num_class,
                                            train_aug=self.target_aug).eval()

        img_orig, lb = self.dataset[idx]
        n_img, n_lb, n_losses, n_corrects = [], [], [], []
        for _ in range(self.num_sample):
            tta_rotate_default = random.choice(
                self.transform_r) if random.random() < self.d_tta_prob else 0.0
            tta_zoom_default = random.choice(
                self.transform_zs
            ) if random.random() < self.d_tta_prob else 1.0
            tta_bright_default = random.choice(
                self.transform_b) if random.random() < self.d_tta_prob else 1.0

            for t_flip in self.transform_flip:
                img_new = img_orig.copy()

                corrupt_name = corrupt_op = corrupt_lv = None
                if self.do_random_corrupt and random.random(
                ) < self.d_tta_prob:  # TODO
                    corrupt_op = random.choice(corruption_tuple)
                    corrupt_lv = random.choice([1, 2, 3, 4, 5])
                elif isinstance(self.corrupt_list, list) and random.random(
                ) < self.d_tta_prob:  # TODO : Partial Corruptions
                    corrupt_name = random.choice(self.corrupt_list)
                    corrupt_op = corruption_dict[corrupt_name]
                    corrupt_lv = random.choice([1, 2, 3, 4, 5])
                    # corrupt_lv = random.choice([3, 4, 5])

                if corrupt_op is not None:
                    img_np = corrupt_op(img_new, severity=corrupt_lv)
                    if isinstance(img_np, np.ndarray):
                        img_np = img_np.astype(np.uint8)
                        img_new = Image.fromarray(img_np)
                    elif isinstance(img_np, PIL.Image.Image):
                        img_new = img_np
                    else:
                        raise Exception(type(img_np))

                if t_flip:
                    img_new = torchvision.transforms.functional.hflip(img_new)
                mirror_expansion_factor = 3
                try:
                    img_mirror = mirror_expansion(img_new)
                except Exception as e:
                    print(corrupt_op, corrupt_lv)
                    print(e)
                    print(type(img_new))
                    print(img_new.size)
                    raise e
                img_new = img_mirror.copy()

                if tta_rotate_default != 0:
                    img_new = torchvision.transforms.functional.rotate(
                        img_new,
                        tta_rotate_default,
                        expand=False,
                        resample=PIL.Image.BICUBIC)
                assert tta_bright_default > 0
                if tta_bright_default != 1.0:
                    img_new = torchvision.transforms.functional.adjust_brightness(
                        img_new, tta_bright_default)
                new_resize = int((self.target_size + self.padding) *
                                 mirror_expansion_factor * tta_zoom_default)
                assert 0.5 < tta_zoom_default < 1.5
                if tta_zoom_default != 1.0:
                    img_new = torchvision.transforms.functional.resize(
                        img_new, new_resize, interpolation=PIL.Image.BICUBIC)

                imgs_pil = []
                for tta_action in tta_actions:
                    tta_rotate, tta_brightness, tta_zoom, tta_contrast, tta_color, tta_blur, tta_att, _ = decode(
                        tta_action)
                    if tta_rotate != 0:
                        img_rotate = torchvision.transforms.functional.rotate(
                            img_new,
                            tta_rotate,
                            expand=False,
                            resample=PIL.Image.BICUBIC)
                    else:
                        img_rotate = img_new.copy()
                    if tta_brightness != 1.0:
                        img_bright = torchvision.transforms.functional.adjust_brightness(
                            img_rotate, tta_brightness)
                    else:
                        img_bright = img_rotate.copy()
                    if tta_zoom != 1.0:
                        resize = int(new_resize * tta_zoom)
                        img_zoom = torchvision.transforms.functional.resize(
                            img_bright,
                            resize,
                            interpolation=PIL.Image.BICUBIC)
                        assert img_zoom.width > 32, (img_zoom.size,
                                                     img_bright.size)
                    else:
                        img_zoom = img_bright.copy()

                    if tta_contrast > 0.0:
                        img_zoom = PIL.ImageOps.autocontrast(img_zoom)
                    assert img_zoom.width > 32, ('autocont', img_zoom.size,
                                                 img_bright.size, img_new.size)
                    if tta_color != 1.0:
                        img_zoom = PIL.ImageEnhance.Color(img_zoom).enhance(
                            tta_color)
                    assert img_zoom.width > 32, ('color', img_zoom.size,
                                                 img_bright.size, img_new.size)
                    if tta_blur != 1.0:
                        img_zoom = ImageEnhance.Sharpness(img_zoom).enhance(
                            tta_blur)
                    assert img_zoom.width > 32, ('blur', img_zoom.size,
                                                 img_bright.size, img_new.size)

                    w, h = img_zoom.size
                    att_padding = self.padding if self.padding else 0
                    pw, ph = max(0, self.target_size + att_padding - w), max(
                        0, self.target_size + att_padding - h)
                    pw1, ph1 = pw // 2, ph // 2
                    pw2, ph2 = pw - pw1, ph - ph1
                    if pw1 > 0 or ph1 > 0 or pw2 > 0 or ph2 > 0:
                        img_pad = torchvision.transforms.functional.pad(
                            img_zoom, (pw1, ph1, pw2, ph2),
                            random.randint(0, 255), 'reflect')
                    else:
                        img_pad = img_zoom
                    # img = torchvision.transforms.functional.center_crop(img_zoom, (self.target_size, self.target_size))

                    crop_width = crop_height = self.target_size
                    # print(tta_action, 'orig.size=', img_orig.size, 'zoom.size=', img_zoom.size, 'img_pad.size', img_pad.size, 'target_size=', self.target_size, 'padding=', self.padding)
                    if tta_att == 0:
                        img = pil_center_crop(img_pad, self.target_size)
                    elif tta_att == 1:
                        img_pad = pil_center_crop(
                            img_pad, int(self.target_size + att_padding))
                        img = img_pad.crop((0, 0, crop_width, crop_height))
                    elif tta_att == 2:
                        img_pad = pil_center_crop(
                            img_pad, int(self.target_size + att_padding))
                        image_width, image_height = img_pad.size
                        img = img_pad.crop((image_width - crop_width, 0,
                                            image_width, crop_height))
                    elif tta_att == 3:
                        img_pad = pil_center_crop(
                            img_pad, int(self.target_size + att_padding))
                        image_width, image_height = img_pad.size
                        img = img_pad.crop((0, image_height - crop_height,
                                            crop_width, image_height))
                    elif tta_att == 4:
                        img_pad = pil_center_crop(
                            img_pad, int(self.target_size + att_padding))
                        image_width, image_height = img_pad.size
                        img = img_pad.crop((image_width - crop_width,
                                            image_height - crop_height,
                                            image_width, image_height))
                    else:
                        raise Exception

                    imgs_pil.append(img)
                    self.img_pils = imgs_pil

                imgs = []
                for img in imgs_pil:
                    img = torchvision.transforms.functional.to_tensor(img)
                    img = torchvision.transforms.functional.normalize(
                        img,
                        mean=[0.485, 0.456, 0.406],
                        std=[0.229, 0.224, 0.225])
                    imgs.append(img)
                imgs = torch.stack(imgs).cuda()
                assert len(imgs) == tta_num

                with torch.no_grad():
                    preds = self.target_network(imgs)
                corrects = (torch.argmax(
                    preds,
                    dim=1).squeeze() == lb).detach().cpu().int().float()
                lbs = torch.tensor([lb] * tta_num).squeeze().cuda()
                # taus = torch.FloatTensor(tta_taus).detach()
                losses = torch.nn.functional.cross_entropy(
                    preds, lbs, reduction='none').detach().cpu()
                del preds
                if self.target_size > 32:  # TODO
                    torch.cuda.empty_cache()

                w, h = img_new.size
                pw, ph = max(0, self.target_size + self.padding - w), max(
                    0, self.target_size + self.padding - h)
                pw1, ph1 = pw // 2, ph // 2
                pw2, ph2 = pw - pw1, ph - ph1
                if pw1 > 0 or ph1 > 0 or pw2 > 0 or ph2 > 0:
                    img_new = torchvision.transforms.functional.pad(
                        img_new, (pw1, ph1, pw2, ph2), random.randint(0, 255),
                        'reflect')
                if img_new.size[0] >= self.target_size or img_new.size[
                        1] >= self.target_size:
                    # img_new = torchvision.transforms.functional.center_crop(img_new, self.target_size)
                    img_new = pil_center_crop(img_new, self.target_size)
                self.orig_img_pil = img_new

                img_new = cutout(img_new,
                                 cutsize=self.cutout * mirror_expansion_factor)

                if self.is_test:
                    return img_mirror, imgs_pil, img_new, losses, corrects

                img_new = torchvision.transforms.functional.resize(
                    img_new, self.l2t_size,
                    interpolation=PIL.Image.BICUBIC)  # TODO
                img_new = torchvision.transforms.functional.to_tensor(img_new)
                img_new = torchvision.transforms.functional.normalize(
                    img_new,
                    mean=[0.485, 0.456, 0.406],
                    std=[0.229, 0.224, 0.225]).cpu()

                n_img.append(img_new)
                n_lb.append(lb)
                n_losses.append(losses)
                n_corrects.append(corrects)
        return torch.stack(n_img), torch.Tensor(n_lb), torch.stack(
            n_losses), torch.stack(n_corrects)