Ejemplo n.º 1
0
    def translate(self, source):
        assert len(source.size()) == 2

        device = get_default_device()

        # Toggle eval mode to disable dropout
        self._encoder.eval()
        self._decoder.eval()

        with torch.no_grad():
            encoder_output, hidden = self._encoder(source, None)

            translation = torch.zeros(len(source),
                                      self._max_length + 1,
                                      1,
                                      dtype=torch.long,
                                      device=device)
            translation[:, 0, 0] = START_OF_SENTENCE_INDEX

            for i in range(self._max_length):
                decoder_output, hidden = self._decoder(translation[:, i],
                                                       hidden, encoder_output)

                _, token_indices = decoder_output.data.topk(1)
                translation[:, i + 1, 0] = token_indices.squeeze()

            # Slice off <SOS> token
            return translation[:, 1:, 0]
Ejemplo n.º 2
0
    def load_from_args(cls, args):
        vocab = Bivocabulary.create(args.source_language, args.target_language,
                                    args.reverse)

        train_sentences = args.train_file

        train_pairs = (vocab.add_sentence_pair(pair)
                       for pair in train_sentences)
        dev_pairs = (vocab.add_sentence_pair(pair) for pair in args.dev_file)

        index = 1 if args.reverse else 0

        device = get_default_device()

        test_sources = (vocab.source.add_sentence(
            sentence.split('|||', 1)[index]) for sentence in args.test_file)
        test_tensors = tuple(
            torch.tensor(s, dtype=torch.long, device=device).unsqueeze(0)
            for s in test_sources)

        train_batches = _batch_by_source(train_pairs,
                                         batch_size=args.train_batch_size)
        dev_batches = _batch_by_source(dev_pairs)

        return cls(vocab, train_batches, dev_batches, test_tensors)
Ejemplo n.º 3
0
    def __init__(self,
                 model: torch.nn.Module,
                 optimizer,
                 criterion,
                 name: str,
                 bag_of_ops,
                 rl_agent=None,
                 M=8,
                 rl_n_steps=16,
                 normalize=None,
                 device=None):
        if device is None:
            device = get_default_device()
        self.device = device

        # main model
        self.model = model.to(self.device)
        self.optimizer = optimizer
        self.criterion = criterion
        self.name = name
        self.best_acc = 0

        self.rl_agent = rl_agent
        self.rl_n_steps = rl_n_steps

        self.bag_of_ops = bag_of_ops
        self.M = M
        self.random_erasing = torchvision.transforms.RandomErasing(
            p=1, scale=(0.25, 0.25), ratio=(1., 1.))

        self.normalize = normalize
        self.writer = SummaryWriter(f'runs/{name}')
Ejemplo n.º 4
0
    def __init__(self,
                 net: nn.Module,
                 name: str,
                 lr=0.00035,
                 grad_norm=0.5,
                 batch_size=1,
                 epsilon=0.2,
                 ent_coef=1e-5,
                 online=True,
                 augmentation=None,
                 device=None):
        if device:
            self.device = device
        else:
            self.device = get_default_device()
        self.net = net.to(self.device)
        self.name = name
        self.optimizer = optim.Adam(self.net.parameters(), lr=lr)
        self.grad_norm = grad_norm

        self.memory = deque(maxlen=256)
        self.batch_size = batch_size
        self.epsilon = epsilon
        self.ent_coef = ent_coef
        self.online = online
        self.augmentation = augmentation
Ejemplo n.º 5
0
    def create_from_args(cls, args, vocab, max_length):
        device = get_default_device()

        encoder = build_encoder(args, vocab).to(device)

        decoder = build_decoder(args, vocab).to(device)

        return cls(encoder, decoder, max_length)
Ejemplo n.º 6
0
def predict_image(img, model):
    #get default device
    device = get_default_device()
    # Convert to a batch of 1
    xb = to_device(img.unsqueeze(0), device)
    # Get predictions from model
    yb = model(xb)
    # Pick index with highest probability
    _, preds = torch.max(yb, dim=1)
    # Retrieve the class label
    return classes[preds[0].item()]
Ejemplo n.º 7
0
    def train(self,
              source,
              target,
              target_lens,
              optimizer,
              criterion,
              teacher_force_percent=0.0):
        # tests
        assert len(source.size()) == 2
        assert len(target.size()) == 2
        assert len(target_lens.size()) == 1
        assert source.size(0) == target.size(0) == target_lens.size(0)

        device = get_default_device()
        optimizer.zero_grad()

        # Toggle training mode so dropout is enabled
        self._encoder.train()
        self._decoder.train()

        # Run input_tensor word-by-word through encoder
        encoder_output, hidden = self._encoder(source, None)

        # Run encoder output through decoder to build up target_tensor
        last_translated_tokens = torch.full((len(source), 1),
                                            START_OF_SENTENCE_INDEX,
                                            dtype=torch.long,
                                            device=device)

        loss = 0.0

        for i in range(target.size(1)):
            decoder_output, hidden = self._decoder(last_translated_tokens,
                                                   hidden, encoder_output)

            # Next input fed through is the most likely token from this
            # iteration
            _, most_likely_token_indices = decoder_output.topk(1)
            last_translated_tokens = \
                most_likely_token_indices.squeeze(1).detach()

            # Apply teacher forcing probabilistically per token (across batch)
            if random.random() < teacher_force_percent:
                last_translated_tokens = target[:, i:i + 1]

            loss += criterion(decoder_output.squeeze(1), target[:, i],
                              i < target_lens)

        # Backprop
        loss.backward()
        optimizer.step()

        return loss.item()
Ejemplo n.º 8
0
def predict_fn(input_object, model):

    print('Inferring class of input data.')

    tfms = get_transform()

    input_data = tfms(input_object)

    device = get_default_device()

    output = predict_image(input_data, model)

    return output
Ejemplo n.º 9
0
def model_fn(model_dir):
    """Load the PyTorch model from the `model_dir` directory."""
    print("Loading model.")

    # Determine the device and construct the model.
    device = get_default_device()
    model = Cifar10CnnModel()

    # Load the store model parameters.
    model_path = os.path.join(model_dir, 'model.pth')
    with open(model_path, 'rb') as f:
        model.load_state_dict(torch.load(f, map_location=device))

    model.to(device).eval()

    print("Done loading model.")
    return model
Ejemplo n.º 10
0
def _make_minibatch_pair(pairs):
    device = get_default_device()

    sources, targets = map(tuple, zip(*pairs))

    target_lens = torch.tensor([len(target) for target in targets],
                               device=device)
    pad_len = target_lens.max().item()
    targets_padded = tuple(
        (target + (0, ) * pad_len)[:pad_len] for target in targets)

    source_minibatch = torch.tensor(sources, dtype=torch.long, device=device)
    target_minibatch = torch.tensor(targets_padded,
                                    dtype=torch.long,
                                    device=device)

    return source_minibatch, target_minibatch, target_lens
Ejemplo n.º 11
0
    def train(self, criterion, epoch_count=200, log_interval=50):
        """Train on MQ2007 dataset with ListNet algorithm"""
        k_vals = [1, 3, 5, 10, 20, 50]
        ndcg_ks_list = []
        err_ks_list = []
        device = utils.get_default_device()
        for fold in range(1, 6):
            print(f"Fold {fold}")

            full_model = None
            if self.model == "simple_one_layer":
                full_model = utils.to_device(
                    models.SimpleOneLayerLinear(self.feature_count), device)
            else:
                full_model = utils.to_device(
                    models.SimpleThreeLayerLinear(self.feature_count), device)
            optimizer = optim.Adam(full_model.parameters(),
                                   lr=self.lrate,
                                   weight_decay=self.weight_decay)

            path = 'resources/' + ('MQ2007' if self.use_mq2007 else 'MQ2008') + \
                  '/Fold' +str(fold) + '/'
            train_iter = utils.DeviceDataLoader(
                data_loader.get_ms_dataset(path + 'train.txt'), device)
            valid_iter = utils.DeviceDataLoader(
                data_loader.get_ms_dataset(path + 'vali.txt', shuffle=False),
                device)
            test_iter = utils.DeviceDataLoader(
                data_loader.get_ms_dataset(path + 'test.txt', shuffle=False),
                device)
            super(MQ200XTrainer, self).__init__(name="ListNet",
                                                train_iter=train_iter,
                                                valid_iter=valid_iter,
                                                test_iter=test_iter,
                                                full_model=full_model,
                                                criterion=criterion,
                                                optimizer=optimizer)
            ndcg_ks, err_ks = super(MQ200XTrainer, self).run_training(
                epoch_count, k_vals=k_vals, log_interval=log_interval)
            ndcg_ks_list.append(ndcg_ks)
            err_ks_list.append(err_ks)
        return dump_metrics(k_vals, ndcg_ks_list, err_ks_list)
Ejemplo n.º 12
0
def eval():
    model_path = os.path.join(constant.MODEL_DIR, constant.TRAINED_MODEL)
    device = get_default_device()
    c3d = model.C3D(constant.NUM_CLASSES)
    c3d.load_state_dict(torch.load(model_path))
    c3d.to(device, non_blocking=True, dtype=torch.float)
    c3d.eval()
    print(model_path)

    testset = UCF101DataSet(framelist_file=constant.TEST_LIST,
                            clip_len=constant.CLIP_LENGTH,
                            crop_size=constant.CROP_SIZE,
                            split="testing")
    testloader = torch.utils.data.DataLoader(
        testset,
        batch_size=constant.TEST_BATCH_SIZE,
        shuffle=False,
        num_workers=8)

    total_predict_label = []
    total_accuracy = []

    for (i, data) in enumerate(testloader, 0):
        inputs, labels = data['clip'].to(
            device, dtype=torch.float), data['label'].to(device)
        _, outputs = c3d(inputs).max(1)

        total = labels.size(0)
        correct = (outputs == labels).sum().item()
        accuracy = float(correct) / float(total)
        print("iteration %d, accuracy = %g" % (i, accuracy))

        total_predict_label.append(outputs)
        total_accuracy.append(accuracy)

    total_accuracy = np.array(total_accuracy)
    total_predict_label = np.array(total_predict_label)

    print(model_path)
    print("Final accuracy", np.mean(total_accuracy))
Ejemplo n.º 13
0
def RUN(batchsize, lr):
    #batchsize=config.batchsize
    #lr=config.learning_rate
    #num_epochs=config.num_epochs
    num_epochs = 50
    device = config.device
    if device == None:
        device = utils.get_default_device()
    label_dict = utils.create_label_dict(config.symbols)
    revdict = {}
    for i, sym in enumerate(config.symbols):
        revdict[i] = sym
    model = InceptFC.FC_Model()
    #model=Resnet.ResNet50(3,97)
    model.to(device)
    print(config.checkpath)
    checkpoint = torch.load(config.checkpath, map_location=device)
    model.load_state_dict(checkpoint['model_state_dict'])
    print("MODEL LOADED")
    model.train()
    for name, child in model.named_children():
        if name in ['conv_block1', "conv_block2", "conv1"]:
            print(name + ' is frozen')
            for param in child.parameters():
                param.requires_grad = False
        else:
            print(name + ' is unfrozen')
            for param in child.parameters():
                param.requires_grad = True

    optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad,
                                        model.parameters()),
                                 lr=lr,
                                 weight_decay=lr / 10.)
    finepath = config.data_dir_path
    myvalpath = "/home/ubuntu/data/ocr/kdeval/good/images/"
    valid_paths = [
        join(myvalpath, f) for f in listdir(myvalpath)
        if isfile(join(myvalpath, f))
    ]
    refinement_ratio = [0.5]
    checkpath = os.path.dirname(config.checkpath)
    checkpath = join(checkpath, "FineTune2")
    os.system('mkdir -p ' + checkpath)
    p = 'runs/Inceptfinalrun/hypergridfine_tune/LR' + str(int(
        1000000 * lr)) + 'BS' + str(batchsize)
    writer = SummaryWriter(p)
    fineds = [f for f in listdir(finepath) if isfile(join(finepath, f))]
    for epoch_fine in range(num_epochs):
        random.shuffle(fineds)
        ds_train = DataUtils.FINEIMGDS(label_dict, finepath, fineds)
        train_gen = torch.utils.data.DataLoader(ds_train,
                                                batch_size=batchsize,
                                                shuffle=True,
                                                num_workers=6,
                                                pin_memory=True)
        train_gen = DataUtils.DeviceDataLoader(train_gen, device)
        result = ModelUtils.fit_fine(model, train_gen, optimizer)
        loss_epoch = result.item()
        print("MEAN LOSS ON EPOCH {} is : {}".format(epoch_fine, loss_epoch))
        ## SAVE WEIGHT AFTER FINETUNE PER EPOCH
        '''
        torch.save({
                    'epoch': epoch_fine,
                    'model_state_dict': model.state_dict(),
                    'optimizer_state_dict': optimizer.state_dict(),
                    'loss': loss_epoch,
                    }, os.path.join(checkpath, 'fine-epoch-{}.pt'.format(epoch_fine)))
        '''
        ## WRITER TENSORBOARD
        writer.add_scalar('Training loss per epoch', loss_epoch, epoch_fine)

        ###############################################################
        ####### CHECK FOR VALIDATION+
        pdf_acc = []
        weight = []
        for imgpath in tqdm(valid_paths, desc="TEST"):
            with io.open(imgpath, 'rb') as image_file:
                content = image_file.read()
            jsonpath = "/home/ubuntu/data/ocr/kdeval/good/json/" + os.path.splitext(
                os.path.basename(imgpath))[0] + ".json"
            with open(jsonpath) as f:
                bounds = json.load(f)
            bounds = bounds_refine(bounds, imgpath, 0.48)
            #print("Characters in Image=",len(bounds))
            ds = get_ds(imgpath, bounds)
            ds_train = DataUtils.EVALIMGDS(label_dict, ds)
            train_gen = torch.utils.data.DataLoader(ds_train,
                                                    batch_size=64,
                                                    shuffle=False,
                                                    num_workers=6,
                                                    pin_memory=True)
            train_gen = DataUtils.DeviceDataLoader(train_gen, device)
            result = ModelUtils.evaluate(model, train_gen)
            pdf_acc.append(len(bounds) * result['val_acc'])
            weight.append(len(bounds))
        print("EPOCHFINE={} Validation Accuracy Mean on GOOD pdf is {}".format(
            epoch_fine,
            sum(pdf_acc) / sum(weight)))
        writer.add_scalar('validation acc per epoch',
                          sum(pdf_acc) / sum(weight), epoch_fine)
Ejemplo n.º 14
0
        ds.append((im1,label))
        labels.append(label)
        coord.append((bound["vertices"][0]['x'],
                       bound["vertices"][0]['y'],
                       bound["vertices"][2]['x'],
                       bound["vertices"][2]['y']))
    
    #image.save(str(uuid.uuid1()) + '_handwritten.png')
    return ds,coord,labels,wordid,seq



if __name__ =='__main__':
    device=config.device
    if device==None:
        device = utils.get_default_device()
    label_dict=utils.create_label_dict(config.symbols)
    revdict={}
    for i,sym in enumerate(config.symbols):
        revdict[i]=sym
    model=InceptFC.FC_Model()    
    #model=Resnet.ResNet50(3,97)
    model.to(device)
    print(config.checkpath)
    checkpoint=torch.load(config.checkpath, map_location=device)
    model.load_state_dict(checkpoint['model_state_dict'])
    print("MODEL LOADED")
    model.train()
    pdf_acc=[]
    weight=[]
    mypath=join(config.pdfdata,"images")
Ejemplo n.º 15
0
import torch

import models.concrete.single
from dataloader import VisionDataset
from opts import parse_args
from training import Trainer
from utils import AverageMeter, get_logger, save_pretrained_model, load_pretrained_model, \
    get_default_device

device = get_default_device()


def exp1(opt):
    model = getattr(models.concrete.single, opt.model)(opt).to(device)
    opt.exp_name += opt.model
    vd = VisionDataset(opt, class_order=list(range(10)))

    optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

    logger = get_logger(folder=opt.log_dir + '/' + opt.exp_name + '/')
    logger.info(f'Running with device {device}')
    logger.info("==> Opts for this training: " + str(opt))

    trainer = Trainer(opt, logger, device=device)

    # pretraining
    if opt.num_pretrain_classes > 0:
        try:
            logger.info('Trying to load pretrained model...')
            model = load_pretrained_model(opt, model, logger)
            pretrain = False
Ejemplo n.º 16
0
def train():
    # initialize the model
    model_path = os.path.join(constant.MODEL_DIR, constant.PRETRAINED_MODEL)
    c3d = model.C3D(constant.NUM_CLASSES)

    print(model_path)

    device = get_default_device()

    if device == torch.device('cpu'):
        pretrained_param = torch.load(model_path, map_location='cpu')
    else:
        pretrained_param = torch.load(model_path)

    to_load = {}

    for key in pretrained_param.keys():
        if 'conv' in key:
            to_load[key] = pretrained_param[key]
        else:
            to_load[key] = c3d.state_dict()[key]

    c3d.load_state_dict(to_load)

    print(c3d.state_dict())

    train_params = [{'params': c3d.get_conv_1x_lr_param(), 'weight_decay': constant.WEIGHT_DECAY},
                    {'params': c3d.get_conv_2x_lr_param(), 'lr': constant.BASE_LR * 2},
                    {'params': c3d.get_fc_1x_lr_param(), 'weight_decay': constant.WEIGHT_DECAY},
                    {'params': c3d.get_fc_2x_lr_param(), 'lr': constant.BASE_LR * 2}]

    # import input data
    trainset = UCF101DataSet(framelist_file=constant.TRAIN_LIST, clip_len=constant.CLIP_LENGTH,
                             crop_size=constant.CROP_SIZE, split="training")
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=constant.TRAIN_BATCH_SIZE,
                                              shuffle=True, num_workers=10)

    c3d.to(device, non_blocking=True, dtype=torch.float)
    c3d.train()

    # define loss function (Cross Entropy loss)
    criterion = nn.CrossEntropyLoss()
    criterion.to(device)

    # define optimizer
    optimizer = optim.SGD(train_params, lr=constant.BASE_LR,
                          momentum=constant.MOMENTUM, weight_decay=0)

    print(optimizer.state_dict())
    # define lr schedule

    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=constant.LR_DECAY_STEP_SIZE,
                                          gamma=constant.LR_DECAY_GAMMA)
    writer = SummaryWriter()

    for epoch in range(constant.NUM_EPOCHES):

        running_loss = 0.0
        running_accuracy = 0.0
        scheduler.step()

        for i, data in enumerate(trainloader, 0):
            step = epoch * len(trainloader) + i
            inputs, labels = data['clip'].to(device, dtype=torch.float), data['label'].to(
                device=device, dtype=torch.int64)
            optimizer.zero_grad()

            outputs = c3d(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            running_loss += loss.item()
            print('Step %d, loss: %.3f' % (i, loss.item()))
            writer.add_scalar('Train/Loss', loss.item(), step)

            outputs = nn.Softmax(dim=1)(outputs)
            _, predict_label = outputs.max(1)
            correct = (predict_label == labels).sum().item()
            accuracy = float(correct) / float(constant.TRAIN_BATCH_SIZE)
            running_accuracy += accuracy
            writer.add_scalar('Train/Accuracy', accuracy, step)

            print("iteration %d, accuracy = %.3f" % (i, accuracy))

            if i % 100 == 99:
                print('[%d, %5d] loss: %.3f' %
                      (epoch + 1, i + 1, running_loss / 100))
                print('[%d, %5d] accuracy: %.3f' %
                      (epoch + 1, i + 1, running_accuracy / 100))
                running_loss = 0.0
                running_accuracy = 0.0
            if step % 10000 == 9999:
                torch.save(c3d.state_dict(), os.path.join(
                    constant.MODEL_DIR, '%s-%s-%d' % (constant.TRAIN_MODEL_NAME, datetime.date.today(), step + 1)))

    print('Finished Training')
    writer.close()