コード例 #1
0
def main():
    args = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    args.add_argument("--model", help="The model to train on")
    args = args.parse_args()

    if args.model == 'alexnet':
        model = models.AlexNet(1)
        train(model, args.model)
    elif args.model == 'resnet18':
        model = models.ResNet(18, 1)
        train(model, args.model)
    elif args.model == 'resnet34':
        model = models.ResNet(34, 1)
        train(model, args.model)
    elif args.model == 'resnet':
        model = models.ResNet(101, 1)
        train(model, args.model)
    else:
        if args.model == 'all':
            # model = models.AlexNet()
            # train(model, 'AlexNet')
            # del model
            model = models.create_pretrained_alexnet(1)
            train(model,'AlexNetFinetuned')
            del model
            # model = models.ResNet(34,1)
            # train(model, 'ResNet34')
            # del model
            model = models.ResNet(50,1)
            train(model, 'ResNet50')
            del model
            model = models.ResNet(101,1)
            train(model, 'ResNet101')
            del model
        raise ValueError("Did not provide a valid model")
コード例 #2
0
    def testResNet(self):
        resolutions = [
            [3, 32, 32],
        ]

        blocks = [
            [3],
            [3, 3],
            [3, 3, 3],
            [3, 3, 3, 3],
        ]
        normalizations = [True, False]

        classes = 10
        batch_size = 100
        for resolution in resolutions:
            for block in blocks:
                for normalization in normalizations:
                    model = models.ResNet(classes,
                                          resolution,
                                          clamp=True,
                                          blocks=block,
                                          normalization=normalization)
                    output = model(
                        torch.autograd.Variable(
                            torch.zeros([batch_size] + resolution)))
                    self.assertEqual(output.size()[0], batch_size)
                    self.assertEqual(output.size()[1], classes)
コード例 #3
0
    def setUp(self):
        self.trainset = torch.utils.data.DataLoader(
            common.datasets.SVHNTrainSet(indices=range(10000)),
            batch_size=100,
            shuffle=True,
            num_workers=4)
        self.testset = torch.utils.data.DataLoader(
            common.datasets.SVHNTestSet(),
            batch_size=100,
            shuffle=False,
            num_workers=4)
        self.adversarialset = torch.utils.data.DataLoader(
            common.datasets.SVHNTestSet(indices=range(1000)),
            batch_size=100,
            shuffle=False,
            num_workers=4)
        self.randomset = torch.utils.data.DataLoader(
            common.datasets.RandomTestSet(1000, size=(28, 28, 1)),
            batch_size=100,
            shuffle=False,
            num_workers=4)

        self.cuda = True
        self.model = models.ResNet(10, [3, 32, 32],
                                   channels=12,
                                   blocks=[3, 3, 3])
        if self.cuda:
            self.model = self.model.cuda()
コード例 #4
0
    def _set_model(self):
        print("netType:", self.settings.netType)
        if self.settings.dataset in ["cifar10", "cifar100"]:
            if self.settings.netType == "DARTSNet":
                genotype = md.genotypes.DARTS
                self.model = md.DARTSNet(self.settings.init_channels,
                                         self.settings.nClasses,
                                         self.settings.layers,
                                         self.settings.auxiliary, genotype)
            elif self.settings.netType == "PreResNet":
                self.model = md.PreResNet(depth=self.settings.depth,
                                          num_classes=self.settings.nClasses,
                                          wide_factor=self.settings.wideFactor)
            elif self.settings.netType == "CifarResNeXt":
                self.model = md.CifarResNeXt(self.settings.cardinality,
                                             self.settings.depth,
                                             self.settings.nClasses,
                                             self.settings.base_width,
                                             self.settings.widen_factor)

            elif self.settings.netType == "ResNet":
                self.model = md.ResNet(self.settings.depth,
                                       self.settings.nClasses)
            else:
                assert False, "use %s data while network is %s" % (
                    self.settings.dataset, self.settings.netType)
        else:
            assert False, "unsupported data set: " + self.settings.dataset
コード例 #5
0
def get_model(dataset, net_type, depth, n_classes):
    """
    Available model
    cifar:
        preresnet
        vgg
    imagenet:
        resnet
    """

    if dataset in ["cifar10", "cifar100"]:
        test_input = torch.randn(1, 3, 32, 32).cuda()
        if net_type == "preresnet":
            model = md.PreResNet(depth=depth, num_classes=n_classes)
        else:
            assert False, "use {} data while network is {}".format(dataset, net_type)

    elif dataset in ["imagenet", "sub_imagenet"]:
        test_input = torch.randn(1, 3, 224, 224).cuda()
        if net_type == "resnet":
            model = md.ResNet(depth=depth, num_classes=n_classes)
        else:
            assert False, "use {} data while network is {}".format(dataset, net_type)

    else:
        assert False, "unsupported data set: {}".format(dataset)
    return model, test_input
コード例 #6
0
def get_confusion_matrix(weight_path, data):
    (x_test, y_test) = data
    x_test = x_test[:500]
    y_test = y_test[:500]

    model = models.ResNet(depth=8)
    _ = model(x_test[0:2])
    model.load_weights(weight_path)

    predictions = model(x_test, training=False).numpy()
    predictions = tf.argmax(predictions, axis=1)
    labels = tf.argmax(y_test, axis=1)
    confusion_matrix = tf.math.confusion_matrix(labels=labels, predictions=predictions, num_classes=10, dtype=tf.float32)
    print(confusion_matrix)
    confusion_matrix = tf.transpose(tf.divide(tf.transpose(confusion_matrix), tf.reduce_sum(y_test, axis=0)))
    print(tf.reduce_sum(y_test, axis=0))
    return confusion_matrix.numpy()
    def setUp(self):
        self.trainset = torch.utils.data.DataLoader(
            common.datasets.Cifar10TrainSet(),
            batch_size=100,
            shuffle=True,
            num_workers=4)
        self.testset = torch.utils.data.DataLoader(
            common.datasets.Cifar10TestSet(),
            batch_size=100,
            shuffle=False,
            num_workers=4)

        self.cuda = True
        self.model = models.ResNet(10, [3, 32, 32],
                                   channels=12,
                                   blocks=[3, 3, 3])
        if self.cuda:
            self.model = self.model.cuda()
コード例 #8
0
    def testResNet(self):
        resolutions = [
            [3, 32, 32],
        ]

        blocks = [
            [3],
            [3, 3, 3, 3],
        ]
        normalizations = [
            None,
            torch.nn.BatchNorm1d
        ]

        clamps = [
            True,
            False
        ]

        scales_and_whitens = [
            (False, False),
            (True, False),
            (False, True),
        ]

        classes = 10
        for resolution in resolutions:
            for block in blocks:
                for normalization in normalizations:
                    for clamp in clamps:
                        for scale_and_whiten in scales_and_whitens:
                            original_model = models.ResNet(classes, resolution, clamp=clamp, scale=scale_and_whiten[0], whiten=scale_and_whiten[1], blocks=block, normalization=normalization)
                            for parameters in original_model.parameters():
                                parameters.data.zero_()

                            common.state.State.checkpoint(self.filepath, original_model)
                            state = common.state.State.load(self.filepath)
                            loaded_model = state.model

                            for parameters in loaded_model.parameters():
                                self.assertEqual(torch.sum(parameters).item(), 0)
コード例 #9
0
def main():
    config = get_args()
    config.device = torch.device(
        "cuda" if torch.cuda.is_available() else "cpu")

    # preprocess
    if config.preprocess_frames:
        preprocess.get_frames(config.train_vid, config.train_frames)
        preprocess.get_frames(config.test_vid, config.test_frames)

    if config.create_csv:
        train_speeds = preprocess.read_speed(config.train_speeds)
        preprocess.create_csv(config.train_frames, train_speeds,
                              config.csv_path)

    # dataset creation
    dataset = FrameDataset(config.csv_path, config.train_frames)
    train_set, val_set = random_split(dataset, [16320, 4080])

    # test set creation
    transform = transforms.Compose([
        transforms.Resize((66, 220)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225]),
    ])
    test_set = datasets.ImageFolder(config.test_frames, transform=transform)

    # model selection
    if config.model == 'simpleCNN':
        model = models.simpleCNN().to(config.device)
    elif config.model == 'ResNet':
        model = models.ResNet().to(config.device)

    # train/val/test
    if config.train:
        runner.train(config, model, train_set)
    elif config.val:
        runner.validate(config, model, val_set)
    elif config.test:
        runner.test(config, model, test_set)
コード例 #10
0
def test_task1(root_path):
    '''
    :param root_path: root path of test data, e.g. ./dataset/task1/test/
    :return results: a dict of classification results
    results = {'audio_0000.pkl': 1, ‘audio_0001’: 3, ...}
    class number:
        ‘061_foam_brick’: 0
        'green_basketball': 1
        'salt_cylinder': 2
        'shiny_toy_gun': 3
        'stanley_screwdriver': 4
        'strawberry': 5
        'toothpaste_box': 6
        'toy_elephant': 7
        'whiteboard_spray': 8
        'yellow_block': 9
    '''
    preds = []
    names = []
    ds = my_dataset("test", transform, root_path)
    loader = torch.utils.data.DataLoader(ds, 64, False, num_workers=20)
    model = models.ResNet(block=models.BasicBlock, num_blocks=[3, 3, 3])
    model = nn.DataParallel(model).cuda()
    model.load_state_dict(torch.load("task1resnet18.pkl"))
    model.eval()
    for data in loader:
        image, name = data['image'], data['name']
        image = image.cuda()
        image = torch.autograd.Variable(image)
        output = model(image)
        pred = torch.argmax(output, 1)
        names.extend(name)
        preds.extend(pred)
    preds = [int(i) for i in preds]
    names = [i[-14:] for i in names]
    # dirs = os.listdir(os.path.join(root_path))
    results = dict(zip(names, preds))
    np.save("task1result.npy", results)
    return results
コード例 #11
0
def run(args):

    # Get the data
    train_data = CleanDataset(paths.train_images_file(
        args.dataset), paths.train_labels_file(args.dataset))
    test_data = CleanDataset(paths.test_images_file(
        args.dataset), paths.test_labels_file(args.dataset))

    trainset = DataLoader(train_data, batch_size=args.batch_size, shuffle=True)
    testset = DataLoader(test_data, batch_size=args.batch_size, shuffle=False)

    # Create or load saved model
    if args.saved_model_file:
        state = State.load(paths.experiment_file(
            args.models_dir, args.saved_model_file))
        model = state.model
        if args.cuda:
            model.cuda()
        optimizer = torch.optim.SGD(model.parameters(), lr=args.lr,
                                    momentum=args.momentum, weight_decay=args.weight_decay)
        scheduler = common.train.get_exponential_scheduler(
            optimizer, batches_per_epoch=len(trainset), gamma=args.lr_decay)
        optimizer.load_state_dict(state.optimizer)
        for st in optimizer.state.values():
            for k, v in st.items():
                if torch.is_tensor(v):
                    st[k] = v.cuda()
        scheduler.load_state_dict(state.scheduler)
        initial_epoch = state.epoch
    else:
        model = models.ResNet(args.n_classes, [3, 32, 32], channels=12, blocks=[
                              3, 3, 3], clamp=True)
        if args.cuda:
            model.cuda()
        optimizer = SGD(model.parameters(), lr=args.lr,
                        momentum=args.momentum, weight_decay=args.weight_decay)
        scheduler = common.train.get_exponential_scheduler(
            optimizer, batches_per_epoch=len(trainset), gamma=args.lr_decay)
        initial_epoch = -1

    # Logging
    if args.use_tensorboard:
        from torch.utils.tensorboard import SummaryWriter
    else:
        from common.summary import SummaryWriter
    writer = SummaryWriter(paths.log_dir(args.log_dir), max_queue=100)

    # Augmentation parameters
    augmentation_crop = True
    augmentation_contrast = True
    augmentation_add = False
    augmentation_saturation = False
    augmentation_value = False
    augmentation_flip = args.use_flip
    augmentation = common.imgaug.get_augmentation(noise=False, crop=augmentation_crop, flip=augmentation_flip, contrast=augmentation_contrast,
                                                  add=augmentation_add, saturation=augmentation_saturation, value=augmentation_value)

    # Create attack objects
    img_dims = (32, 32)
    if args.location == 'random':
        mask_gen = MaskGenerator(img_dims, tuple(args.mask_dims),
                                 exclude_list=np.array([args.exclude_box]))
    else:
        mask_gen = MaskGenerator(img_dims, tuple(args.mask_dims),
                                 include_list=np.array([args.mask_pos + args.mask_dims]))
    attack = AdversarialPatch(mask_gen, args.epsilon, args.iterations,
                              args.optimize_location, args.opt_type, args.stride, args.signed_grad)
    attack.norm = LInfNorm()
    objective = UntargetedF0Objective()

    if args.mode == 'adversarial':
        trainer = common.train.AdversarialTraining(model, trainset, testset, optimizer, scheduler,
                                                   attack, objective, fraction=args.adv_frac, augmentation=augmentation, writer=writer, cuda=args.cuda)
    elif args.mode == 'normal':
        trainer = common.train.NormalTraining(
            model, trainset, testset, optimizer, scheduler, augmentation=augmentation, writer=writer, cuda=args.cuda)

    trainer.summary_gradients = False

    # Train model
    for e in range(initial_epoch + 1, args.epochs):
        trainer.step(e)
        writer.flush()

        # Save model snapshot
        if (e + 1) % args.snapshot_frequency == 0:
            State.checkpoint(paths.experiment_file(
                args.models_dir, args.model_prefix + '_' + str(e + 1)), model, optimizer, scheduler, e)

    # Save final model
    State.checkpoint(paths.experiment_file(
        args.models_dir, args.model_prefix + '_complete_' + str(e + 1)), model, optimizer, scheduler, args.epochs)
コード例 #12
0
ファイル: resnet_pre_flip.py プロジェクト: kulugu2/DL_2019
        self.fc = nn.Linear(2048 ,5)

    def forward(self, x):
        out = self.conv1(x)
        #out = self.maxpool1(out)
        out = self.layer1(out)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.layer4(out)
        out = self.avgpool(out)
        out = out.view(out.size(0), -1)
        out = self.fc(out)

        return out

resnet_model = models.ResNet().to(device)
        
def adjust_lr(optimizer, epoch):
    if epoch < 10:
        lr = 0.01
    elif epoch < 500:
        lr = 0.003
    elif epoch < 2000:
        lr = 0.001
    elif epoch < 2500:
        lr = 0.0005
    else:
        lr = 0.0001

    for param_group in optimizer.param_groups:
        param_group['lr'] = lr
コード例 #13
0
def main():
    summery_writer = tf.summary.create_file_writer(LOG_DIR)

    print_every_n = 50

    (x_train, y_train), (x_test, y_test) = load_dataset()

    n_train = x_train.shape[0]
    l2_regularizer = tf.keras.regularizers.l2(LAMBDA)

    model = models.ResNet(depth=NUM_LAYERS,
                          se_block=SE_BLOCKS,
                          ratios=RATIOS,
                          regularizer=l2_regularizer)
    crossentropy = tf.keras.losses.CategoricalCrossentropy()
    adam = tf.keras.optimizers.Adam(learning_rate=lr_scheduler)

    global epoch
    for epoch in range(EPOCHS):
        epoch_loss = 0
        loss = 0
        acc = 0
        print("Epoch learning rate: ", adam.get_config()['learning_rate'])

        for i in range(int(np.ceil(n_train // BATCH_SIZE))):
            x_batch = x_train[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]
            x_batch = preprocess_batch(x_batch)
            y_batch = y_train[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]
            # (x_batch, y_batch) = shuffle(x_batch, y_batch)

            batch_loss, batch_acc = update_step(model,
                                                x_batch,
                                                y_batch,
                                                loss_func=crossentropy,
                                                optimizer=adam)
            loss += batch_loss
            acc += batch_acc
            epoch_loss += batch_loss
            if i % print_every_n == 0:
                if i != 0:
                    loss /= print_every_n + 1
                    acc /= print_every_n + 1
                print("Epoch {} Iteration {}/{} Loss {} Acc {}".format(
                    epoch, i, n_train // BATCH_SIZE, loss, acc))
                loss = 0
                acc = 0
        epoch_loss /= int(np.ceil(n_train // BATCH_SIZE))
        y_pred_test = model(x_test, training=False)
        test_acc = accuracy(y_test, y_pred_test)
        test_loss = crossentropy(
            y_test, y_pred_test)  #TODO: räknar den loss per batch size?

        print("Epoch {}, accuracy: {} loss {}".format(epoch, test_acc,
                                                      test_loss))

        save_directory = LOG_DIR + "/{}ResNet_{}_weights/".format(
            "SE_" if SE_BLOCKS else "", NUM_LAYERS)
        try:
            os.mkdir(save_directory)
        except FileExistsError:
            pass
        model.save_weights(filepath=save_directory + "checkpoint",
                           save_format='h5')
        with summery_writer.as_default():
            tf.summary.scalar('Test Accuracy', test_acc, step=epoch)
            tf.summary.scalar('Test Loss', test_loss, step=epoch)
            tf.summary.scalar('Training Loss', epoch_loss, step=epoch)
コード例 #14
0
    args = parser.parse_args()

    np.random.seed(args.seed)

    strategy = tf.distribute.MirroredStrategy()
    print(f'Number of devices for multigpu strategy: {strategy.num_replicas_in_sync}')

    with strategy.scope():
        if args.arch == "cnn":
            model, input_shape, channels = models.CNN()
        elif args.arch == "resnet-head":
            model, input_shape, channels = models.ResNetHead()
        elif args.arch == "1d-conv":
            model, input_shape, channels = models.Conv1D()
        elif args.arch == "resnet-full":
            model, input_shape, channels = models.ResNet()
        else:
            raise NotImplementedError("Model type not supported")

        # print("trainable count:", len(model.trainable_variables))
        optimizer = keras.optimizers.Adam(
            learning_rate=args.lr,
            # decay=1e-2,
        )

        model.compile(loss="binary_crossentropy", optimizer=optimizer,
                      metrics=[keras.metrics.CategoricalAccuracy(), utils.f1_m, utils.precision_m, utils.recall_m])


    # Data
    data_generator = dataloader.DataGenerator("spectrograms", batch_size=args.batch_size, dim=input_shape,
コード例 #15
0
    ax = sns.heatmap(cm, cmap="binary", annot=True, fmt="d")
    print(cm)
    figure = ax.get_figure()
    figure.savefig('{}/result-heatmap.png'.format(output_path), dpi=600)


if __name__ == "__main__":

    protocol(
        _custom_output_dir='V19_DA1000_LIMIT',
        _use_data_augmentation=True,
        _limit_dataset=True,
        # _network_class=models.EfficientNet(net='b7',
        # _network_class=models.MobileNet(net='v1',
        # _network_class=models.VGGNet(net='19',
        _network_class=models.ResNet(net='50',
                                     use_weights=True,
                                     use_regularization=False,
                                     regularizer=keras.regularizers.l2(0.01)),
        _load_data_function=utils.load_covid_dataset,
        _number_of_classes=3,
        _batch_size=10,
        _epochs=20,
        _shuffle_in_training=True,
        _plot_loss_epochs=2,
        _lr=0.001,
        _train_portion=0.9,
        _model_epochs_checkpoint=200,
        _save_intermediate_models=False,
        _save_model=True)
コード例 #16
0
alb = denorm(alb)
shade = applyMask(rImg, real_image_mask_test)
shade = shade / alb
utils.save_image(torchvision.utils.make_grid(shade.data, padding=1),
                 output_path + 'val/test_real_shading.png')

## TRUE SHADING
'''
tmp = next(iter(sirfs_shading_val))
tmp = utils.denorm(tmp)
tmp = applyMask(var(tmp).type(torch.DoubleTensor), real_image_mask_test) 
tmp = tmp.data
utils.save_image(torchvision.utils.make_grid(tmp, padding=1), output_path+'images/Validation_SIRFS_SHADING.png')
'''

featureNet = models.ResNet(models.BasicBlock, [2, 2, 2, 2], 27)
#featureNet = models.BaseSimpleFeatureNet()
lightingNet = models.LightingNet()
D = models.Discriminator()
R = models.ResNet(models.BasicBlock, [2, 2, 2, 2], 27)  #
#R = models.BaseSimpleFeatureNet()

print(featureNet)
print(lightingNet)
featureNet = featureNet.cuda()
lightingNet = lightingNet.cuda()
D = D.cuda()
R = R.cuda()

dtype = torch.FloatTensor
dtype = torch.cuda.FloatTensor  ## UNCOMMENT THIS LINE IF YOU'RE ON A GPU!
コード例 #17
0
def main(net_opt=None):
    """requirements:
    apt-get install graphviz
    pip install pydot termcolor"""

    start_time = time.time()
    opt = net_opt or NetOption()

    # set torch seed
    # init random seed
    torch.manual_seed(opt.manualSeed)
    torch.cuda.manual_seed(opt.manualSeed)
    cudnn.benchmark = True
    if opt.nGPU == 1 and torch.cuda.device_count() >= 1:
        assert opt.GPU <= torch.cuda.device_count() - 1, "Invalid GPU ID"
        torch.cuda.set_device(opt.GPU)
    else:
        torch.cuda.set_device(opt.GPU)

    # create data loader
    data_loader = DataLoader(dataset=opt.data_set,
                             train_batch_size=opt.trainBatchSize,
                             test_batch_size=opt.testBatchSize,
                             n_threads=opt.nThreads,
                             ten_crop=opt.tenCrop)
    train_loader, test_loader = data_loader.getloader()

    # define check point
    check_point = CheckPoint(opt=opt)
    # create residual network model
    if opt.retrain:
        check_point_params = check_point.retrainmodel()
    elif opt.resume:
        check_point_params = check_point.resumemodel()
    else:
        check_point_params = check_point.check_point_params

    optimizer = check_point_params['opts']
    start_epoch = check_point_params['resume_epoch'] or 0
    if check_point_params['resume_epoch'] is not None:
        start_epoch += 1
    if start_epoch >= opt.nEpochs:
        start_epoch = 0
    if opt.netType == "ResNet":
        model = check_point_params['model'] or MD.ResNet(
            depth=opt.depth,
            num_classes=opt.nClasses,
            wide_factor=opt.wideFactor)
        model = dataparallel(model, opt.nGPU, opt.GPU)
    elif opt.netType == "PreResNet":
        model = check_point_params['model'] or MD.PreResNet(
            depth=opt.depth,
            num_classes=opt.nClasses,
            wide_factor=opt.wideFactor)
        model = dataparallel(model, opt.nGPU, opt.GPU)
    elif opt.netType == "LeNet5":
        model = check_point_params['model'] or MD.LeNet5()
        model = dataparallel(model, opt.nGPU, opt.GPU)

    else:
        assert False, "invalid net type"

    # create online board
    if opt.onlineBoard:
        try:
            online_board = BoardManager("main")
        except:
            online_board = None
            print "|===> Failed to create online board! Check whether you have ran <python -m visdom.server>"
    else:
        online_board = None

    trainer = Trainer(model=model,
                      opt=opt,
                      optimizer=optimizer,
                      online_board=online_board)
    print "|===>Create trainer"

    # define visualizer
    visualize = Visualization(opt=opt)
    visualize.writeopt(opt=opt)
    # visualize model
    if opt.drawNetwork:
        if opt.data_set == "cifar10" or opt.data_set == "cifar100":
            rand_input = torch.randn(1, 3, 32, 32)
        elif opt.data_set == "mnist":
            rand_input = torch.randn(1, 1, 28, 28)
        else:
            assert False, "invalid data set"
        rand_input = Variable(rand_input.cuda())
        rand_output = trainer.forward(rand_input)
        visualize.gennetwork(rand_output)
        visualize.savenetwork()

    # test model
    if opt.testOnly:
        trainer.test(epoch=0, test_loader=test_loader)
        return

    best_top1 = 100
    best_top5 = 100
    for epoch in range(start_epoch, opt.nEpochs):
        start_epoch = 0
        # training and testing
        train_error, train_loss, train5_error = trainer.train(
            epoch=epoch, train_loader=train_loader)
        test_error, test_loss, test5_error = trainer.test(
            epoch=epoch, test_loader=test_loader)

        # show training information on online board
        if online_board is not None:
            online_board.updateplot(train_error,
                                    train5_error,
                                    train_loss,
                                    mode="Train")
            online_board.updateplot(test_error,
                                    test5_error,
                                    test_loss,
                                    mode="Test")

        # write and print result
        log_str = "%d\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t" % (
            epoch, train_error, train_loss, test_error, test_loss,
            train5_error, test5_error)
        visualize.writelog(log_str)
        best_flag = False
        if best_top1 >= test_error:
            best_top1 = test_error
            best_top5 = test5_error
            best_flag = True
            if online_board is not None:
                online_board.updateresult([best_top1, best_top5, test_loss])
            print colored(
                "==>Best Result is: Top1 Error: %f, Top5 Error: %f\n" %
                (best_top1, best_top5), "red")
        else:
            print colored(
                "==>Best Result is: Top1 Error: %f, Top5 Error: %f\n" %
                (best_top1, best_top5), "blue")

        # save check_point
        # save best result and recent state
        check_point.savemodel(epoch=epoch,
                              model=trainer.model,
                              opts=trainer.optimzer,
                              best_flag=best_flag)

        if (epoch + 1) % opt.drawInterval == 0:
            visualize.drawcurves()

    end_time = time.time()
    time_interval = end_time - start_time

    t_string = "Running Time is: " + str(
        datetime.timedelta(seconds=time_interval)) + "\n"
    print(t_string)

    # save experimental results
    visualize.writereadme(
        "Best Result of all is: Top1 Error: %f, Top5 Error: %f\n" %
        (best_top1, best_top5))
    visualize.writereadme(t_string)
    visualize.drawcurves()
コード例 #18
0
 def __init__(self, n_layer):
     super(Feature_ResNet, self).__init__()
     all_model = models.ResNet(1, n_layer, pretrained=False)
     for name, modules in all_model._modules.items():
         if name != 'fc':
             self.add_module(name, modules)
コード例 #19
0
ファイル: test.py プロジェクト: Didi-26/DeepLearningProjects
        plot_file_path='./plots/pairSetup_VGGNetLike.svg',
        lr=0.0001,
        epochs=300,
        use_crossentropy=True,
        verbose=True,
        rounds=10)
    print('mean minimum test error : {0:.{1}f} %'.format(test_err_mean, 1))
    print('std minimum test error : {0:.{1}f} %'.format(test_err_std, 1))
    print('number of trainable parameters : {}'.format(
        count_parameters(model)))

if run_PairSetup_ResNet:
    torch.manual_seed(random_seed)
    print('************ Running ResNet model (for PairSetup) ****************')
    in_depth, out_dim = 2, 2
    model = models.ResNet(in_depth, out_dim, 8)
    test_err_mean, test_err_std, _, _ = training_functions.rounds_train(
        model,
        'PairSetup',
        plot_title='Pair Setup ResNet error history',
        plot_file_path='./plots/pairSetup_ResNet.svg',
        lr=0.0002,
        epochs=300,
        use_crossentropy=True,
        verbose=True,
        rounds=10)
    print('mean minimum test error : {0:.{1}f} %'.format(test_err_mean, 1))
    print('std minimum test error : {0:.{1}f} %'.format(test_err_std, 1))
    print('number of trainable parameters : {}'.format(
        count_parameters(model)))
コード例 #20
0
def test_task2(root_path):
    '''
    :param root_path: root path of test data, e.g. ./dataset/task2/test/0/
    :return results: a dict of classification results
    results = {'audio_0000.pkl': 23, ‘audio_0001’: 11, ...}
    This means audio 'audio_0000.pkl' is matched to video 'video_0023' and ‘audio_0001’ is matched to 'video_0011'.
    '''
    results = {}

    audio_preds = []
    audio_names = []
    audio_ds = my_dataset("test", transform, root_path)
    audio_loader = torch.utils.data.DataLoader(audio_ds,
                                               4,
                                               False,
                                               num_workers=0)
    audio_model = models.ResNet(block=models.BasicBlock, num_blocks=[3, 3, 3])
    audio_model = nn.DataParallel(audio_model).cuda()
    audio_model.load_state_dict(torch.load("task1resnet18.pkl"))
    audio_model.eval()
    for data in audio_loader:
        image, name = data['image'], data['name']
        image = image.cuda()
        image = torch.autograd.Variable(image)
        output = audio_model(image)
        pred = torch.argmax(output, 1)
        audio_names.extend(name)
        audio_preds.extend(pred)
    audio_preds = [int(i) for i in audio_preds]
    audio_names = [i.split('/')[-1][:-4] for i in audio_names]
    audio_results = dict(zip(audio_names, audio_preds))

    video_preds = []
    video_names = []
    video_ds = video_dataset("test", transform, root_path)
    video_loader = torch.utils.data.DataLoader(video_ds,
                                               4,
                                               False,
                                               num_workers=0)
    video_model = models.ResNet(in_ch=1,
                                in_stride=(1, 1),
                                fc_size=64,
                                block=models.BasicBlock,
                                num_blocks=[3, 3, 3])
    video_model = nn.DataParallel(video_model).cuda()
    video_model.load_state_dict(torch.load("new_video_resnet.pkl"))
    video_model.eval()
    for data in video_loader:
        image, name = data['image'], data['name']
        image = image.cuda()
        image = torch.autograd.Variable(image)
        output = video_model(image)
        pred = torch.argmax(output, 1)
        video_names.extend(name)
        video_preds.extend(pred)
    video_preds = [int(i) for i in video_preds]
    video_names_unique = list(set(video_names))
    video_preds_max = []
    for name in video_names_unique:
        indices = [i for i, x in enumerate(video_names) if x == name]
        pred = [video_preds[i] for i in indices]
        pred = max(pred, key=pred.count)
        video_preds_max.append(pred)
    video_results = dict(zip(video_names_unique, video_preds_max))

    audio_num = len(audio_names)
    for i in range(10):
        class_name = classes[i]
        matching_resnet_model = models.ResNet(block=models.BasicBlock,
                                              num_blocks=[3, 3, 3])
        matching_resnet_model = nn.DataParallel(matching_resnet_model).cuda()
        matching_resnet_model.load_state_dict(
            torch.load(class_name + "_resnet.pkl"))
        matching_resnet_model.eval()
        matching_mlp_model = models.MLP()
        matching_mlp_model = nn.DataParallel(matching_mlp_model).cuda()
        matching_mlp_model.load_state_dict(torch.load(class_name + "_mlp.pkl"))
        matching_mlp_model.eval()

        audio_i = [k for k, v in audio_results.items() if v == i]
        video_i = [k for k, v in video_results.items() if v == i]
        print(audio_i)
        print(video_i)

        matching_ds = matching_dataset(mode="test",
                                       transforms=transform,
                                       test_path=root_path,
                                       test_audio=audio_i,
                                       test_video=video_i)
        matching_loader = torch.utils.data.DataLoader(matching_ds,
                                                      4,
                                                      False,
                                                      num_workers=0)

        distance_matrix = np.zeros((len(audio_i), len(video_i)))

        for data in matching_loader:
            raw, name = data['raw'], data['name']
            image = raw[0]
            video = raw[1]
            image = torch.autograd.Variable(image.cuda())
            video = torch.autograd.Variable(video.cuda())
            video_output = matching_mlp_model(video)
            image_output = matching_resnet_model(image)
            dist = F.pairwise_distance(video_output, image_output)
            for j in range(len(dist)):
                audio_num = audio_i.index(name[0][j])
                video_num = video_i.index(name[1][j])
                distance_matrix[audio_num][video_num] = dist[j]

        print(distance_matrix)
        row_ind, col_ind = linear_sum_assignment(distance_matrix)
        print(row_ind)
        print(col_ind)
        for j in range(len(row_ind)):
            audio_name = audio_i[row_ind[j]]
            video_name = video_i[col_ind[j]]
            results[audio_name] = video_name

    audio_set = list(set(audio_names) - set([k for k, v in results.items()]))
    video_set = list(set(video_names) - set([v for k, v in results.items()]))
    perm = np.random.permutation(len(audio_set))
    for j in perm:
        audio_name = audio_set[j]
        video_name = video_set[j]
        results[audio_name] = video_name
    for k, v in results.items():
        results[k] = int(v[-4:])
    print(results)
    return results
コード例 #21
0
    def train(self):
        """
        Training configuration.
        """

        def get_augmentation(crop=True, flip=True):
            augmenters = []
            if crop:
                augmenters.append(iaa.CropAndPad(
                    px=((0, 4), (0, 4), (0, 4), (0, 4)),
                    pad_mode='constant',
                    pad_cval=(0, 0),
                ))
            if flip:
                augmenters.append(iaa.Fliplr(0.5))

            return iaa.Sequential(augmenters)

        writer = common.summary.SummaryPickleWriter('%s/logs/' % self.args.directory, max_queue=100)
        if self.args.tensorboard:
            writer = torch.utils.tensorboard.SummaryWriter('%s/logs/' % self.args.directory, max_queue=100)

        crop = False
        flip = False
        if self.args.dataset == 'svhn':
            crop = True
        elif self.args.dataset == 'cifar10':
            crop = True
            flip = True

        epochs = 200
        snapshot = 10

        model_file = '%s/classifier.pth.tar' % self.args.directory
        incomplete_model_file = find_incomplete_state_file(model_file)
        load_file = model_file
        if incomplete_model_file is not None:
            load_file = incomplete_model_file

        start_epoch = 0
        if os.path.exists(load_file):
            state = common.state.State.load(load_file)
            self.model = state.model
            start_epoch = state.epoch + 1
            log('loaded %s' % load_file)
        else:
            self.model = models.ResNet(10, [self.trainset.images.shape[3], self.trainset.images.shape[1], self.trainset.images.shape[2]],
                                       blocks=[3, 3, 3])
        if self.args.cuda:
            self.model = self.model.cuda()

        augmentation = get_augmentation(crop=crop, flip=flip)
        optimizer = torch.optim.SGD(self.model.parameters(), lr=0.075, momentum=0.9)
        scheduler = common.train.get_exponential_scheduler(optimizer, batches_per_epoch=len(self.trainloader),
                                                           gamma=0.97)
        trainer = common.train.NormalTraining(self.model, self.trainloader, self.testloader, optimizer, scheduler,
                                              augmentation=augmentation, writer=writer, cuda=self.args.cuda)

        self.model.train()
        for epoch in range(start_epoch, epochs):
            trainer.step(epoch)
            writer.flush()

            snapshot_model_file = '%s/classifier.pth.tar.%d' % (self.args.directory, epoch)
            common.state.State.checkpoint(snapshot_model_file, self.model, optimizer, scheduler, epoch)

            previous_model_file = '%s/classifier.pth.tar.%d' % (self.args.directory, epoch - 1)
            if os.path.exists(previous_model_file) and (epoch - 1) % snapshot > 0:
                os.unlink(previous_model_file)

        previous_model_file = '%s/classifier.pth.tar.%d' % (self.args.directory, epoch - 1)
        if os.path.exists(previous_model_file) and (epoch - 1) % snapshot > 0:
            os.unlink(previous_model_file)

        common.state.State.checkpoint(model_file, self.model, optimizer, scheduler, epoch)
コード例 #22
0
 def getModel(cls):
     return models.ResNet(10, [1, 28, 28], blocks=[1, 1, 1])
コード例 #23
0
            video = torch.autograd.Variable(video.cuda())
            label = torch.autograd.Variable(label.cuda())
        video_output = video_model(video)
        image_output = img_model(image)
        dist = F.pairwise_distance(video_output, image_output)
        print(dist)
        pred = (dist < 10.0)
        print(label)
        correct += (pred == label).sum().float()
        total += len(label)
    acc = (100 * correct * 1.0 / total)

    return acc


resnet = models.ResNet(block=models.BasicBlock, num_blocks=[3, 3, 3])
cnn3d = models.CNN3D()
cnn = models.CNN()
mlp = models.MLP()
criterion = nn.MSELoss().cuda()
optimizer = torch.optim.Adam(list(mlp.parameters()) +
                             list(resnet.parameters()),
                             lr=lr)
# optimizer = torch.optim.SGD(list(cnn3d.parameters()) + list(resnet.parameters()), lr=lr, momentum=0.9, weight_decay=1e-4)

task2_ds = matching_dataset(cat="whiteboard_spray", transforms=transform_train)
val_len = len(task2_ds) // 10
train_len = len(task2_ds) - val_len
train_ds, val_ds = torch.utils.data.random_split(task2_ds,
                                                 [train_len, val_len])
train_loader = torch.utils.data.DataLoader(train_ds, 10, False, num_workers=10)
コード例 #24
0
        data, holdout_split=0.2)
    input_size = col - 1
    # * shshshshshs

    # print('\nNet: ')
    # tt.train_test(models.Net(input_size), X_train_tor,
    #               X_test_tor, y_train_tor, y_test_tor)
    # print('\nResNet: ')
    # tt.train_test(models.ResNet(input_size), X_train_tor,
    #               X_test_tor, y_train_tor, y_test_tor)
    # print('\nTDRNN: ')
    # tt.train_test(models.TDRNN(input_size), X_train_tor,
    #               X_test_tor, y_train_tor, y_test_tor)
    # print('\nODRNN: ')
    # tt.train_test(models.ODRNN(input_size), X_train_tor,
    #               X_test_tor, y_train_tor, y_test_tor)
    print('\nSORNN: ')
    # tt.train_test(models.SORNN(input_size), X_train_tor,
    #               X_test_tor, y_train_tor, y_test_tor)

    # tt.train_test(models.SORNN(input_size), cv_data, X_test_tor, y_test_tor)

    # ep.error_plot(models.SORNN(input_size), cv_data,
    #               X_test_tor, y_test_tor, iterations=10)
    #

    network_list = [models.ResNet(input_size), models.TDRNN(input_size), models.ODRNN(input_size), models.SORNN(input_size)]
    name_network = ['ResNet', 'TDRNN', 'ODRNN', 'SORNN']
    print(len(network_list))
    mc.models_comp(network_list, cv_data, X_test_tor, y_test_tor, name_network, iterations=2)
コード例 #25
0
ファイル: train_full.py プロジェクト: xiaoyu258/Inbetweening
    help='checkpoint saving frequency. N: after every N epochs.')
parser.add_argument("--frm_num", type=int, default=7)
args = parser.parse_args()

log_name = './log/full_model'
cpt_name = '/full_model_'

writer = SummaryWriter(log_name)

print("torch.cuda.is_available: ", torch.cuda.is_available())
print("torch.cuda.device_count: ", torch.cuda.device_count())

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
multiGPUs = [0, 1, 2, 3]

netT = models.ResNet()
sketExt = models.PWCExtractor()
imagExt = models.PWCExtractor()
flowEst = models.Network()
blenEst = models.blendNet()
flowRef = models.UNet(14, 8)
ImagRef = model_deform.DeformUNet(21, 15)

W = 576
H = 384
flowBackWarp = models.backWarp(W, H)
occlusiCheck = models.occlusionCheck(W, H)

if torch.cuda.device_count() >= 1:
    netT = nn.DataParallel(netT, device_ids=multiGPUs)
    sketExt = nn.DataParallel(sketExt, device_ids=multiGPUs)
コード例 #26
0
        elif args.target == 'ip':
            if args.arch == 'LeNet_300_100':
                cfg = [300, 100]
                for i in range(len(cfg)):
                    cfg[i] = round(cfg[i] * (1 - args.pruning_ratio))
                temp_cfg = cfg
            pass

    # generate the model
    if args.arch == 'VGG':
        model = models.VGG(num_classes, cfg=cfg)
    elif args.arch == 'LeNet_300_100':
        model = models.LeNet_300_100(bias_flag=True, cfg=cfg)
    elif args.arch == 'ResNet':
        model = models.ResNet(int(args.depth_wide), num_classes, cfg=cfg)
    elif args.arch == 'WideResNet':
        model = models.WideResNet(args.depth_wide[0],
                                  num_classes,
                                  widen_factor=args.depth_wide[1],
                                  cfg=cfg)
    else:
        pass

    if args.cuda:
        model.cuda()

    # pretrain
    best_acc = 0.0
    best_epoch = 0
    if args.pretrained:
コード例 #27
0
def main():
    torch.manual_seed(0)
    torch.random.manual_seed(0)

    # create results folder, if not already exists
    output_directory = misc.get_output_directory(args)
    if not os.path.exists(output_directory):
        os.makedirs(output_directory)
    train_csv = os.path.join(output_directory, 'train.csv')
    test_csv = os.path.join(output_directory, 'test.csv')
    best_txt = os.path.join(output_directory, 'best.txt')
    
    print("=> creating data loaders ...")
    if args.data == 'MNIST':
        datadir = './data/'
        all_dataset = loader.MNIST(datadir)
        train_size = len(all_dataset) // 5 * 4
        test_size = len(all_dataset) // 10
        val_size = len(all_dataset) - (train_size + test_size)
        train_dataset, test_dataset, val_dataset = torch.utils.data.random_split(all_dataset, [train_size, test_size, val_size])
    else:
        raise RuntimeError('Dataset not found.' +
                           'The dataset must be either of nyudepthv2 or kitti.')

    train_loader = torch.utils.data.DataLoader(
        train_dataset, batch_size=args.batch_size, shuffle=True,
        num_workers=args.workers, pin_memory=True, sampler=None)
    # set batch size to be 1 for validation
    test_loader = torch.utils.data.DataLoader(test_dataset,
                                             batch_size=1, shuffle=False, num_workers=args.workers, pin_memory=True)

    print("=> data loaders created.")

    # optionally resume from a checkpoint
    if args.start_epoch != 0:
        assert os.path.isfile(args.resume), \
            "=> no checkpoint found at '{}'".format(args.resume)
        print("=> loading checkpoint '{}'".format(args.resume))
        checkpoint = torch.load(args.resume)
        args.start_epoch = checkpoint['epoch'] + 1
        best_result = checkpoint['best_result']
        model = checkpoint['model']
        optimizer = checkpoint['optimizer']
        print("=> loaded checkpoint (epoch {})".format(checkpoint['epoch']))

    # create new model
    else:
        # define model
        print("=> creating Model ({}) ...".format(args.arch))

        if args.arch == 'resnet50':
            model = models.ResNet(50)
        else:
            raise RuntimeError("model not found")

        print("=> model created.")

        
    # define loss function (criterion) and optimizer
    if args.criterion == 'cce':
        criterion = criteria.CrossEntropyLoss().cuda()
    else:
        raise RuntimeError("criterion not found")

    if args.optimizer == 'Adam':
        optimizer = torch.optim.Adam(model.parameters(), lr = args.lr, weight_decay = args.weight_decay)
    elif  args.optimizer == 'SGD':
        optimizer = torch.optim.SGD(model.parameters(), args.lr,
                                    momentum=args.momentum,
                                    weight_decay=args.weight_decay)
    else:
        raise RuntimeError("optimizer not defined")

    optimizer_scheduler = lr_scheduler.StepLR(optimizer, args.epochs//3)

    model = model.cuda()
    print(model)
    print("=> model transferred to GPU.")

    train_logger, test_logger = None, None

    for epoch in range(args.start_epoch, args.epochs):
        train_result = train.train(train_loader, model, criterion, optimizer)

        if epoch == 0:
            train_logger = logger.Logger(train_result, output_directory, train_csv)
        else:
            train_logger.append(train_result)

        optimizer_scheduler.step()

        # evaluate on validation set
        test_result = test.validate(test_loader, model, criterion, optimizer)

        if epoch == 0:
            test_logger = logger.Logger(test_result, output_directory, test_csv)
        else:
            test_logger.append(test_result)

        misc.save_checkpoint({
            'args': args,
            'epoch': epoch,
            'arch': args.arch,
            'model': model,
            'best_result': best_result,
            'optimizer': optimizer,
        }, is_best, epoch, output_directory)

    train_logger.write_into_file('train')
    test_logger.write_into_file('test')
コード例 #28
0
        image, label = data['image'], data['label']
        image, label = image.cuda(), label.cuda()
        image, label = torch.autograd.Variable(image), torch.autograd.Variable(
            label)
        output = model(image)
        pred = torch.argmax(output, 1)
        correct += (pred == label).sum().float()
        total += len(label)
    acc = (100 * correct * 1.0 / total)
    print("accuracy : %.03f" % (acc))
    return acc


resnet = models.ResNet(in_ch=1,
                       in_stride=(1, 1),
                       fc_size=64,
                       block=models.BasicBlock,
                       num_blocks=[3, 3, 3])
mlp = models.MLP()
criterion = nn.CrossEntropyLoss().cuda()
# optimizer = torch.optim.Adam(resnet.parameters(), lr = lr)
optimizer = torch.optim.SGD(resnet.parameters(),
                            lr=lr,
                            momentum=0.9,
                            weight_decay=1e-4)
ds = video_dataset("train", transform_train)
# print(ds.__len__())
train_ds, val_ds = torch.utils.data.random_split(ds, [50000, 6998])
train_loader = torch.utils.data.DataLoader(train_ds,
                                           128,
                                           False,
コード例 #29
0
    resnet = [50, 18]
    pretrain = [True, False]
    hi_name = ['', '', '', '']
    highest_accur = [[0, 0], [0, 0], [0, 0], [0, 0]]
    j = 0
    for layer in resnet:
        plot.show_result_start(title + str(layer) + ')', x1, x2, xd, y1, y2,
                               yd)
        for p_i in pretrain:
            print('~~~Resnet%d:' % layer, p_i, '~~~')
            if (layer == 18):
                TOTAL_EPOCH = EPOCH[0]
            else:
                TOTAL_EPOCH = EPOCH[1]

            net = models.ResNet(layer, p_i).cuda() if (
                p_i == True) else models.ResNet_no(layer).cuda()

            optimizer = t.optim.SGD(net.parameters(),
                                    lr=LR,
                                    momentum=MOMENTUM,
                                    weight_decay=WEIGHT_DECAY)

            # train
            train_accuracy = []
            test_accuracy = []
            for epoch_i in range(1, TOTAL_EPOCH + 1):
                print('epoch', epoch_i)
                print('START:',
                      time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
                right = 0
コード例 #30
0
def create_model(observations,
                 model_name,
                 step_size=0.1,
                 horizon=10,
                 dim_obs=784,
                 dim_latent=10,
                 inf_horizon=1,
                 infer_qdot=True,
                 **model_param):

    if model_name == 'ResNet':
        dynamics = models.ResNet(step_size,
                                 horizon,
                                 name=model_name,
                                 **model_param)
    elif model_name == 'VIN_VV':
        dynamics = models.VIN_VV(step_size,
                                 horizon,
                                 name=model_name,
                                 **model_param)
    elif model_name == 'VIN_SV':
        dynamics = models.VIN_SV(step_size,
                                 horizon,
                                 name=model_name,
                                 **model_param)
    elif model_name == 'VIN_SO2':
        dynamics = models.VIN_SO2(step_size,
                                  horizon,
                                  name=model_name,
                                  **model_param)
    elif model_name == 'FeedForward':
        dynamics = models.FeedForward(step_size,
                                      1,
                                      name=model_name,
                                      **model_param)
    elif model_name in ['VAE', 'LG_VAE']:
        dynamics = None
    else:
        raise NotImplementedError()

    if model_name in ['VIN_SV', 'VIN_SO2']:
        infer_qdot = False

    if observations == 'observed':
        model = dynamics
    elif observations == 'noisy':
        dim_dec_in = dynamics.dim_state
        dim_obs = dynamics.dim_state
        model = models.NoisyLDDN(dim_obs,
                                 dim_dec_in,
                                 dynamics,
                                 inf_horizon,
                                 name="NoisyLDDN",
                                 infer_qdot=infer_qdot,
                                 **model_param)
    elif observations == 'pixels':
        if model_name in ['ResNet', 'FeedForward']:
            dim_dec_in = dynamics.dim_state
        elif 'VIN' in model_name:
            dim_dec_in = dynamics.dim_Q

        if model_name in ['VIN_SO2', 'LG_VAE']:
            dec_inp_fn = lambda x: tf.concat([tf.sin(x), tf.cos(x)], 1)
        else:
            dec_inp_fn = None
        if dynamics is not None:
            model = models.PixelLDDN(dim_obs,
                                     dim_dec_in,
                                     dynamics,
                                     inf_horizon,
                                     name="PixelLDDN",
                                     infer_qdot=infer_qdot,
                                     dec_inp_fn=dec_inp_fn,
                                     **model_param)
        else:
            raise NotImplementedError()
    else:
        raise NotImplementedError()

    return compile_model(model, observations)