예제 #1
0
def build_model(opt, vocab, checkpoint, gpu_id=None):
    print('Building model...')
    use_gpu = len(opt.gpu_ranks) > 0
    if use_gpu and gpu_id is not None:
        device = torch.device("cuda", gpu_id)
    elif use_gpu and not gpu_id:
        device = torch.device("cuda")
    elif not use_gpu:
        device = torch.device("cpu")

    #  model = MLP([1000, 500, 500], len(vocab))
    model = CNN(len(vocab), opt.dropout)

    if checkpoint is not None:
        # end of patch for backward compatibility
        if opt.train_from:
            print("Loading model parameters from checkpoint...")
            model.load_state_dict(checkpoint['model'], strict=False)
        else:
            print("Loading model parameters from checkpoint...")
            cur_state_dict = model.state_dict()
            for key in cur_state_dict.keys():
                if key in checkpoint['model'] and cur_state_dict[key].size(
                ) == checkpoint['model'][key].size():
                    cur_state_dict[key] = checkpoint['model'][key]
                #  elif key in checkpoint['model'] and cur_state_dict[key].size() != checkpoint['model'][key].size():
                #  print("***" , key)
            model.load_state_dict(cur_state_dict, strict=False)
    if len(opt.gpu_ranks) > 1:
        model = nn.DataParallel(model, opt.gpu_ranks)
    model.to(device)
    return model, device
예제 #2
0
def image_cnn(image):
    train_x = tf.placeholder(tf.float32, [None, Model.image_h * Model.image_w * 3])
    keep_prob = tf.placeholder(tf.float32)
    model = CNN()
    output = model.cnn_model()
    saver = tf.train.Saver()
    ckpt = tf.train.get_checkpoint_state(logDIR)
    with tf.Session() as sess:
        saver.restore(sess, ckpt.model_checkpoint_path)
        predict = tf.argmax(tf.reshape(output, [-1, Model.max_length, Model.len_char]), 2)
        text_list = sess.run(predict, feed_dict={train_x: [image], keep_prob: 1})
        text = text_list[0].tolist()
        ans = [Model.char_set[char] for i, char in enumerate(text)]
    return ''.join(ans)
예제 #3
0
def train():
    # Dataset & Dataloader
    train_transform = transforms.Compose([
        transforms.RandomRotation(15),
        transforms.RandomResizedCrop(cfg.CROP_SIZE, scale=(0.8, 1.0)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor()
    ])

    test_transform = transforms.Compose([
        transforms.Resize(70),
        transforms.CenterCrop(cfg.CROP_SIZE),
        transforms.ToTensor()
    ])

    train_set = datasets.ImageFolder(cfg.TRAIN_DIR, transform=train_transform)
    trainloader = DataLoader(train_set,
                             batch_size=cfg.BATCH_SIZE,
                             shuffle=True,
                             num_workers=4)

    test_set = datasets.ImageFolder(cfg.TEST_DIR, transform=test_transform)
    testloader = DataLoader(test_set, batch_size=cfg.BATCH_SIZE, shuffle=True)

    # Config
    config = set_config({
        "batch_size": cfg.BATCH_SIZE,
        "crop_size": cfg.CROP_SIZE
    })

    # Training Preparation
    model = CNN().to(device)
    criterion = nn.NLLLoss()
    optimizer = optim.AdamW(model.parameters(), lr=0.001)
    callback = Callback(model, config, outdir=cfg.OUTDIR)

    # Training
    while True:
        train_cost, train_score = loop_fn("train", train_set, trainloader,
                                          model, criterion, optimizer, device)
        with torch.no_grad():
            test_cost, test_score = loop_fn("test", test_set, testloader,
                                            model, criterion, optimizer,
                                            device)

        # Callbacks
        callback.log(train_cost, test_cost, train_score, test_score)
        callback.save_checkpoint()
        if callback.early_stopping(model, monitor="test_score"):
            break
예제 #4
0
def main():
    parser = ArgumentParser("Load Data")
    parser.add_argument("data_path", type=Path, help="path to dataset folder")
    parser.add_argument("--limit",
                        "--l",
                        type=int,
                        help="limit of the dataset")
    args = parser.parse_args()

    transform = Compose([Resize(256, 256), ToTensor()])

    train_dataset = Dataset(args.data_path / "train", transform, args.limit)
    val_dataset = Dataset(args.data_path / "test", transform, args.limit)

    train_dataloader = torch.utils.data.DataLoader(train_dataset,
                                                   batch_size=8,
                                                   shuffle=True,
                                                   num_workers=8)
    val_dataloader = torch.utils.data.DataLoader(val_dataset,
                                                 batch_size=8,
                                                 shuffle=False,
                                                 num_workers=8)

    model = CNN((256, 256), 2)

    loss_fn = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=0.005,
                                 weight_decay=0.005)
    scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.998)
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    max_step = (len(train_dataloader.dataset) + (8 - 1)) // 8

    for epoch in range(1000):
        epoch_loss = 0
        for step, batch in enumerate(train_dataloader):
            start_time = time.perf_counter()
            img_batch, label_batch = batch["img"].to(
                device).float(), batch["label"].to(device).long()
            optimizer.zero_grad()
            x = model(img_batch)
            loss = loss_fn(x, label_batch)
            loss.backward()
            optimizer.step()
            epoch_loss += loss.item()
            print_step(step, max_step, loss, time.perf_counter() - start_time)
        print("")
        print(epoch_loss / max_step, flush=True)
예제 #5
0
def train_cnn(image_txt, label_txt, image_file):
    train_x = tf.placeholder(tf.float32,
                             [None, Model.image_h * Model.image_w * 3])
    train_y = tf.placeholder(tf.float32,
                             [None, Model.max_length * Model.len_char])
    image, label = get_data(image_txt, label_txt, image_file)
    keep_prob = tf.placeholder(tf.float32)
    model = CNN()
    output = model.cnn_model()
    optimizer, loss = model.model_loss(output)
    accuracy = model.model_acc(output)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        step = 0
        count = 0
        while True:
            batch_x, batch_y = get_next_batch(32, image, label)
            _, loss_ = sess.run([optimizer, loss],
                                feed_dict={
                                    train_x: batch_x,
                                    train_y: batch_y,
                                    keep_prob: 0.75
                                })
            print(step, loss_)
            # 每100 step计算一次准确率
            if step % 100 == 0:
                batch_x_test, batch_y_test = get_next_batch(64, image, label)
                acc = sess.run(accuracy,
                               feed_dict={
                                   train_x: batch_x_test,
                                   train_y: batch_y_test,
                                   keep_prob: 1.
                               })
                print(step, acc)
                if step % 1000 == 0:
                    checkpoint_path = os.path.join(logDIR, 'model.ckpt')
                    saver.save(sess, checkpoint_path, global_step=step)
                if acc == 1:
                    count = count + 1
                if count == 10:
                    checkpoint_path = os.path.join(logDIR, 'model.ckpt')
                    saver.save(sess, checkpoint_path, global_step=step)
                    break
            step += 1
def main():
    logger.info("Start Training Pipeline")
    augmented = True
    if augmented:
        if not os.path.exists(
                os.path.join(METADATA_DIR_AUGMENTED_PROCESSED, 'data.pkl')):
            augment.main()
            # Read Data
        dataset = pd.read_pickle(
            os.path.join(METADATA_DIR_AUGMENTED_PROCESSED, 'data.pkl'))

    else:
        # Generate MetaData if not generated yet
        if not os.path.exists(os.path.join(METADATA_DIR_PROCESSED,
                                           'data.pkl')):
            generate.run()
        dataset = pd.read_pickle(
            os.path.join(METADATA_DIR_PROCESSED, 'data.pkl'))

    logger.info(f"Number of samples: {len(dataset)}")
    most_shape = get_most_shape(dataset)
    train_data, test_data = train_test_split(dataset,
                                             augmented=augmented,
                                             split_ratio=0.65)

    X_train, y_train = features_target_split(train_data)
    X_test, y_test = features_target_split(test_data)

    # Reshape for CNN input
    X_train, X_test = reshape_feature_CNN(X_train), reshape_feature_CNN(X_test)

    # Preserve y_test values
    y_test_values = y_test.copy()

    # One-Hot encoding for classes
    y_train, y_test = one_hot_encode(y_train), one_hot_encode(y_test)

    # Instance of CNN model
    cnn = CNN(most_shape)
    logger.info(str(cnn))

    cnn.train(X_train, y_train, X_test, y_test)
    cnn.evaluate(X_train, y_train, X_test, y_test)

    predictions = cnn.model.predict_classes(X_test)
    conf_matrix = confusion_matrix(y_test_values,
                                   predictions,
                                   labels=range(10))
    logger.info('Confusion Matrix for classes {}:\n{}'.format(
        CLASSES, conf_matrix))
    cnn.save_model()
def build_model(opt, vocab, checkpoint, gpu_id=None):
    print('Building model...')
    if opt.gpu == -1:
        device = torch.device("cpu")
    else:
        device = torch.device("cuda", gpu_id)
    #  model = MLP([1000, 500, 500], len(vocab))
    model = CNN(len(vocab))

    if checkpoint is not None:
        # end of patch for backward compatibility
        print("Loading model parameters from checkpoint...")
        model.load_state_dict(checkpoint['model'], strict=False)
    model.to(device)
    return model, device
def load_model(opt):
    global model
    global device
    global idx2label

    print('Loading checkpoint from %s' % opt.model)
    checkpoint = torch.load(opt.model,
                            map_location=lambda storage, loc: storage)
    print('Loading vocab from checkpoint at %s' % opt.model)
    vocab = checkpoint['vocab']
    idx2label = {v: k for k, v in vocab.items()}

    print('Building model...')
    if opt.gpu == -1:
        device = torch.device("cpu")
    else:
        device = torch.device("cuda", opt.gpu)
    model = CNN(len(vocab))

    # end of patch for backward compatibility
    print("Loading model parameters from checkpoint...")
    model.load_state_dict(checkpoint['model'], strict=False)
    model.to(device)
    model.eval()
예제 #9
0
def init_model():
    cnn = CNN((128, 87))
    cnn.load_model()
    return cnn
예제 #10
0
        if not os.path.exists(os.path.join(PROCESSED_METADATA_DIR,
                                           'data.pkl')):
            run()
        dataset = pd.read_pickle(
            os.path.join(PROCESSED_METADATA_DIR, 'data.pkl'))

    print(f"Number of samples: {len(dataset)}")
    most_shape = get_most_shape(dataset)
    train_data, test_data = train_test_split(dataset,
                                             augmented=False,
                                             split_ratio=0.65)
    X_train, y_train = features_target_split(train_data)
    X_test, y_test = features_target_split(test_data)
    # Reshape for CNN input
    X_train, X_test = reshape_feature_CNN(X_train), reshape_feature_CNN(X_test)
    # Preserve y_test values
    y_test_values = y_test.copy()
    # One-Hot encoding for classes
    y_train, y_test = one_hot_encode(y_train), one_hot_encode(y_test)
    # Instance of CNN model
    cnn = CNN(most_shape)
    print(str(cnn))
    cnn.train(X_train, y_train, X_test, y_test)
    cnn.evaluate(X_train, y_train, X_test, y_test)
    predictions = cnn.model.predict_classes(X_test)
    conf_matrix = confusion_matrix(y_test_values,
                                   predictions,
                                   labels=range(10))
    print('Confusion Matrix for classes {}:\n{}'.format(CLASSES, conf_matrix))
    cnn.save_model()
예제 #11
0
        "conv4": cfg.conv4,
        "out_channel": cfg.out_channel,
        "kernel": cfg.kernel,
        "pad": cfg.pad,
        "in_size": cfg.in_size,
        "n1": cfg.n1,
        "n2": cfg.n2,
        "dropout": cfg.dropout,
        "out_size": cfg.out_size,
        "batch_norm": cfg.batch_norm,
        "author": cfg.author

})

# Training Preparation
model = CNN().to(device)
criterion = nn.NLLLoss()
optimizer = optim.AdamW(model.parameters(), lr=0.001)
callback = Callback(model, config, outdir=cfg.OUTDIR)

 # Training
 while True:
      train_cost, train_score = loop_fn(
           "train", train_set, trainloader, model, criterion, optimizer, device)
       with torch.no_grad():
            test_cost, test_score = loop_fn(
                "test", test_set, testloader, model, criterion, optimizer, device)

        # Callbacks
        callback.log(train_cost, test_cost, train_score, test_score)
        callback.save_checkpoint()
예제 #12
0
transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, ), (0.5, ))])
trainset = torchvision.datasets.MNIST(root='./data',
                                      train=True,
                                      download=True,
                                      transform=transform)
trainloader = torch.utils.data.DataLoader(trainset,
                                          batch_size=96,
                                          shuffle=True,
                                          num_workers=2)

testset = torchvision.datasets.MNIST(root='./data',
                                     train=False,
                                     download=True,
                                     transform=transform)
testloader = torch.utils.data.DataLoader(testset,
                                         batch_size=1,
                                         shuffle=False,
                                         num_workers=2)

trainer = Trainer(
    model=CNN(),
    trainloader=trainloader,
    testloader=testloader,
    n_epoch=5,
    lr=0.001,
)

trainer.train()
예제 #13
0
class ErrorFeedback(AbstractViewModel):
    def __init__(self) -> None:
        super(ErrorFeedback, self).__init__()
        self.errors: List[Error] = []

    def parse(self, html: bytes) -> List[Error]:
        page = self._parser(html)
        items = page.find_all(**ERROR_FEEDBACK)
        for it in items:
            self.errors.append(Error(it.text))

        return self.errors


if __name__ == "__main__":
    model = CNN()
    model.load("checkpoints/0228_ori/model.pth")
    model.cuda()
    model.eval()

    target_path = "dataset/crawled_data"
    if not os.path.exists(target_path):
        os.makedirs(target_path)
        os.makedirs(os.path.join(target_path, "ori"))

    crawled_data = []
    image_cnt = 1
    error_cnt = 1
    num_runs = 10000

    for _ in tqdm(range(num_runs)):
예제 #14
0
# Normalize the data the training and val sets
train_df, val_df = AvgNormalization(df_train, df_val)

# Create windowed data sets and labels. Display summary.
w1 = WindowGenerator(input_width=24,
                     label_width=1,
                     shift=1,
                     train_df=train_df,
                     val_df=val_df,
                     label_columns=['Pressure'])
print(w1)

# Import models from source
MAX_EPOCHS = 20
val_performance = {}

# Testing CNN Model
history = compile_and_fit(CNN, w1, MAX_EPOCHS)
val_performance['CNN'] = CNN.evaluate(w1.val)
plot_loss(history, 'CNN_Model_Loss')

# Testing RNN Model
history = compile_and_fit(RNN, w1, MAX_EPOCHS)
val_performance['RNN'] = RNN.evaluate(w1.val)
plot_loss(history, 'RNN_Model_Loss')

# Performance Summary
for name, value in val_performance.items():
    print(f'{name:12s}: {value[1]:0.4f}')