Example #1
0
def main(argv=None):
    model = CNN(
        train_images_dir='data/train/',
        val_images_dir='data/val/',
        test_images_dir='data/test/',
        num_epochs=40,
        train_batch_size=1000,
        val_batch_size=1000,
        test_batch_size=10000,
        height_of_image=28,
        width_of_image=28,
        num_channels=1,
        num_classes=10,
        learning_rate=0.001,
        base_dir='results',
        max_to_keep=2,
        model_name="CNN",
        model='CNN'
    )

    model.create_network()
    model.initialize_network()

    if True:
        model.train_model(1, 1, 1, 4)
    else:
        model.test_model()
Example #2
0
def main(argv=None):
    model = CNN(train_images_dir=FLAGS.train_images_dir,
                val_images_dir=FLAGS.val_images_dir,
                test_images_dir=FLAGS.test_images_dir,
                inference_dir=FLAGS.inference_dir,
                num_epochs=FLAGS.num_epochs,
                train_batch_size=FLAGS.train_batch_size,
                val_batch_size=FLAGS.val_batch_size,
                height_of_image=FLAGS.height_of_image,
                width_of_image=FLAGS.width_of_image,
                num_channels=FLAGS.num_channels,
                num_classes=FLAGS.num_classes,
                learning_rate=FLAGS.learning_rate,
                base_dir=FLAGS.base_dir,
                max_to_keep=FLAGS.max_to_keep,
                model_name=FLAGS.model_name,
                keep_prob=FLAGS.keep_prob)

    model.create_network()
    model.initialize_network()

    if FLAGS.train:
        model.train_model(FLAGS.display_step, FLAGS.validation_step,
                          FLAGS.checkpoint_step, FLAGS.summary_step)
    else:
        model.test_model()
Example #3
0
def main(epochs=5, learning_rate=1e-3):
    # use GPU
    device = torch.device('cuda')

    # get data loaders
    training = get_dataloader(train=True)
    testing = get_dataloader(train=False)

    # model
    model = CNN().to(device)
    info('Model')
    print(model)

    # cost function
    cost = torch.nn.BCELoss()

    # optimizers
    optimizer = optim.Adam(model.parameters(), lr=learning_rate)

    for epoch in range(1, epochs + 1):
        info('Epoch {}'.format(epoch))
        train(model, device, training, cost, optimizer, epoch)
        test(model, device, testing, cost)

    # save model
    info('Saving Model')
    save_model(model, device, 'model.onnx')
    print('Saving PyTorch Model as model.pth')
    torch.save(model.state_dict(), 'model.pth')
Example #4
0
 def __init__(self, _hparams):
     self.test_loader = get_test_loader(_hparams)
     self.encoder = CNN().to(DEVICE)
     self.decoder = RNN(fea_dim=_hparams.fea_dim,
                        embed_dim=_hparams.embed_dim,
                        hid_dim=_hparams.hid_dim,
                        max_sen_len=_hparams.max_sen_len,
                        vocab_pkl=_hparams.vocab_pkl).to(DEVICE)
     self.test_cap = _hparams.test_cap
Example #5
0
    def bilstm_train(self, numEpochs, batch_size, save_file, lr):
        print('training .....')

        # set up loss function -- 'SVM Loss' a.k.a ''Cross-Entropy Loss
        loss_func = nn.CrossEntropyLoss()
        net = CNN(embed_dim=100)
        # net.load_state_dict(torch.load('model_50.pth'))
        # SGD used for optimization, momentum update used as parameter update
        optimization = torch.optim.SGD(net.parameters(), lr=lr, momentum=0.9)
        net.cuda()
        loss_func.cuda()
        train_losses = []
        test_losses = []
        for epoch in range(0,numEpochs):

            # training set -- perform model training
            epoch_training_loss = 0.0
            num_batches = 0
            pbar = tqdm(range(0, len(self.train_seqs), batch_size))
            for batch_num in pbar:  # 'enumerate' is a super helpful function
                # split training data into inputs and labels
                if batch_num+batch_size>len(self.train_seqs):
                    end = len(self.action_seqs)
                else:
                    end = batch_num+batch_size
                raw_inputs, labels_ = self.train_seqs[batch_num:end], self.train_labels[batch_num:end]  # 'training_batch' is a list
                inputs_ = self.get_embedding(raw_inputs)
                inputs = torch.from_numpy(inputs_).float().cuda()
                labels = torch.from_numpy(labels_).cuda()
                # wrap data in 'Variable'
                inputs, labels = torch.autograd.Variable(inputs), torch.autograd.Variable(labels)
                # Make gradients zero for parameters 'W', 'b'
                optimization.zero_grad()
                # forward, backward pass with parameter update
                forward_output = net(inputs)
                loss = loss_func(forward_output, labels)
                loss.backward()
                optimization.step()
                # calculating loss
                epoch_training_loss += loss.data.item()
                num_batches += 1
                # print(loss.data.item())
                pbar.set_description("processing batch %s" % str(batch_num))
            print("epoch: ", epoch, ", loss: ", epoch_training_loss / num_batches)
            # train_loss = self.test(net, batch_size=256, test_data=self.train_seqs, test_label=self.train_labels)
            test_loss = self.test(net, batch_size=256, test_data=self.test_seqs, test_label=self.test_labels)
            # train_losses.append(train_loss)
            test_losses.append(test_loss)
            # if epoch%10 == 0:
            #     save_path = save_file+'model3_' +str(epoch)+'.pth'
            #     torch.save(net.state_dict(), save_path)
        # with open('train_loss_1.p','wb') as fin:
        #     pickle.dump(train_losses,fin)
        #     fin.close()
        with open('test_loss_1.p','wb') as fin:
            pickle.dump(test_losses,fin)
            fin.close()
    def build(self, is_train):
        self.model = CNN(self.config)
        self.loss_fn = self.config.loss_fn()

        if is_train:
            self.model.train()
            self.optimizer = self.config.optimizer(self.model.parameters(), lr=self.config.lr)
        else:
            self.model.eval()
    def check_model(self, X_train, X_val, y_train, y_val, X_test, y_test,
                    raw_seq):
        """ Funtion used to navigate to the specific model. The is defined when initialising the class.
            Reads the self.model_type 
            Each statement does the following:
                - Calls function to format data for the model
                - Calls funtion to train the model
                - Calls funtion to plot the MSE graph
                - Calls funtion to test the model
                - Returns the accuarcy as R2 score"""

        if self.model_type == 'CNN':

            X_train, X_val, y_train, n_input, n_output, ytrain1, ytrain2, ytrain3, ytrain4 = CNN.data_format(
                X_train, X_val, y_train)
            history = CNN.CNN_train_model(self, X_train, X_val, y_train, y_val,
                                          self.verbose, n_input, n_output,
                                          ytrain1, ytrain2, ytrain3, ytrain4)
            Models.plotting(history)
            yhat = CNN.CNN_test_model(self, X_test, self.verbose, y_test)
            Models.accuracy(self, yhat, y_test, X_test, self.model_type)

        if self.model_type == 'MLP':

            X_train, X_val, y_train, n_input, n_output, ytrain1, ytrain2, ytrain3, ytrain4 = MLP.data_format(
                X_train, X_val, y_train)
            history = MLP.MLP_train_model(self, X_train, X_val, y_train, y_val,
                                          self.verbose, n_input, n_output,
                                          ytrain1, ytrain2, ytrain3, ytrain4)
            # Models.plotting(history)
            yhat, final_cols = MLP.MLP_test_model(X_test, self.verbose, y_test)
            Models.accuracy(self, yhat, y_test, final_cols, self.model_type)

        if self.model_type == 'KNN':

            X_train, X_val, y_train, X_test = KNN.data_format(
                X_train, X_val, y_train, X_test)
            yhat, final_cols = KNN.KNN_train_model(self, X_train, X_val,
                                                   y_train, y_val, X_test,
                                                   y_test, raw_seq)
            Models.accuracy(self, yhat, y_test, final_cols, self.model_type)

        if self.model_type == 'LSTM':

            history, model = LSTMs.LSTM_train_model(self, X_train, X_val,
                                                    y_train, y_val,
                                                    self.verbose)
            Models.plotting(history)
            yhat = LSTMs.LSTM_test_model(X_test, model, self.verbose, y_test)
            Models.accuracy(self, yhat, y_test, X_test, self.model_type)

        if self.model_type == 'BASELINE':
            n_input, X_train, n_output = BaseLine.data_format(X_train, y_train)
            model = BaseLine.baseline_train(self, X_train, y_train, n_input,
                                            n_output)
            yhat, final_cols = BaseLine.baseline_test(X_test, n_input, model)
            Models.accuracy(self, yhat, y_test, final_cols, self.model_type)
Example #8
0
def test_pl_model():
    mnist = mnist_data()
    cnn = CNN(num_channels=mnist.dims[0],
              num_classes=mnist.num_classes)  # Architecture
    BaseLitModel(datamodule=mnist, backbone=cnn, lr=1e-3,
                 flood_height=0)  # Lightning model
    cnn = CNN(num_channels=mnist.dims[0],
              num_classes=mnist.num_classes,
              maxpool=False)
    BaseLitModel(datamodule=mnist, backbone=cnn, lr=1e-3, flood_height=0)
Example #9
0
def train():
    fluid.enable_dygraph(device)
    processor = SentaProcessor(data_dir=args.data_dir,
                               vocab_path=args.vocab_path,
                               random_seed=args.random_seed)
    num_labels = len(processor.get_labels())

    num_train_examples = processor.get_num_examples(phase="train")

    max_train_steps = args.epoch * num_train_examples // args.batch_size // dev_count

    train_data_generator = processor.data_generator(
        batch_size=args.batch_size,
        padding_size=args.padding_size,
        places=device,
        phase='train',
        epoch=args.epoch,
        shuffle=False)

    eval_data_generator = processor.data_generator(
        batch_size=args.batch_size,
        padding_size=args.padding_size,
        places=device,
        phase='dev',
        epoch=args.epoch,
        shuffle=False)
    if args.model_type == 'cnn_net':
        model = CNN(args.vocab_size, args.batch_size, args.padding_size)
    elif args.model_type == 'bow_net':
        model = BOW(args.vocab_size, args.batch_size, args.padding_size)
    elif args.model_type == 'gru_net':
        model = GRU(args.vocab_size, args.batch_size, args.padding_size)
    elif args.model_type == 'bigru_net':
        model = BiGRU(args.vocab_size, args.batch_size, args.padding_size)

    optimizer = fluid.optimizer.Adagrad(learning_rate=args.lr,
                                        parameter_list=model.parameters())

    inputs = [Input([None, None], 'int64', name='doc')]
    labels = [Input([None, 1], 'int64', name='label')]

    model.prepare(optimizer,
                  CrossEntropy(),
                  Accuracy(topk=(1, )),
                  inputs,
                  labels,
                  device=device)

    model.fit(train_data=train_data_generator,
              eval_data=eval_data_generator,
              batch_size=args.batch_size,
              epochs=args.epoch,
              save_dir=args.checkpoints,
              eval_freq=args.eval_freq,
              save_freq=args.save_freq)
def run(args):
    if not os.path.exists(args.logdir):
        os.makedirs(args.logdir)
    logger = get_logger(os.path.join(args.logdir, 'main.log'))
    logger.info(args)

    # data
    source_transform = transforms.Compose([
        # transforms.Grayscale(),
        transforms.ToTensor()]
    )
    target_transform = transforms.Compose([
        transforms.Resize(32),
        transforms.ToTensor(),
        transforms.Lambda(lambda x: x.repeat(3, 1, 1))
    ])
    source_dataset_train = SVHN(
        './input', 'train', transform=source_transform, download=True)
    target_dataset_train = MNIST(
        './input', train=True, transform=target_transform, download=True)
    target_dataset_test = MNIST(
        './input', train=False, transform=target_transform, download=True)
    source_train_loader = DataLoader(
        source_dataset_train, args.batch_size, shuffle=True,
        drop_last=True,
        num_workers=args.n_workers)
    target_train_loader = DataLoader(
        target_dataset_train, args.batch_size, shuffle=True,
        drop_last=True,
        num_workers=args.n_workers)
    target_test_loader = DataLoader(
        target_dataset_test, args.batch_size, shuffle=False,
        num_workers=args.n_workers)

    # train source CNN
    source_cnn = CNN(in_channels=args.in_channels).to(args.device)
    if os.path.isfile(args.trained):
        print("load model")
        c = torch.load(args.trained)
        source_cnn.load_state_dict(c['model'])
        logger.info('Loaded `{}`'.format(args.trained))
    else:
        print("not load model")

    # train target CNN
    target_cnn = CNN(in_channels=args.in_channels, target=True).to(args.device)
    target_cnn.load_state_dict(source_cnn.state_dict())
    discriminator = Discriminator(args=args).to(args.device)
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(
        target_cnn.encoder.parameters(),
        lr=args.lr, betas=args.betas, weight_decay=args.weight_decay)
    d_optimizer = optim.Adam(
        discriminator.parameters(),
        lr=args.lr, betas=args.betas, weight_decay=args.weight_decay)
    train_target_cnn(
        source_cnn, target_cnn, discriminator,
        criterion, optimizer, d_optimizer,
        source_train_loader, target_train_loader, target_test_loader,
        args=args)
def character_classification():
    print('Loading data...')
    x, y = load_data_chars()
    print('Processing data..')
    print('Training data shape: ', x.shape)
    print('Test data shape: ', y.shape)
    plots.plot_filters(x[0])
    SVM.svm(x, y)
    Naive_Bayes.naive_bayes(x, y)
    KNN.knn(x, y)
    CNN.fit_cnn(x, y, trials=1, network_type='simple')
Example #12
0
class Tester:
    """
    测试
    """
    def __init__(self, _hparams):
        self.test_loader = get_test_loader(_hparams)
        self.encoder = CNN().to(DEVICE)
        self.decoder = RNN(fea_dim=_hparams.fea_dim,
                           embed_dim=_hparams.embed_dim,
                           hid_dim=_hparams.hid_dim,
                           max_sen_len=_hparams.max_sen_len,
                           vocab_pkl=_hparams.vocab_pkl).to(DEVICE)
        self.test_cap = _hparams.test_cap

    def testing(self, save_path, test_path):
        """
        测试

        :param save_path: 模型的保存地址
        :param test_path: 保存测试过程生成句子的路径
        :return:
        """
        print('*' * 20, 'test', '*' * 20)
        self.load_models(save_path)
        self.set_eval()

        sen_json = []
        with torch.no_grad():
            for val_step, (img, img_id) in tqdm(enumerate(self.test_loader)):
                img = img.to(DEVICE)
                features = self.encoder.forward(img)
                sens, _ = self.decoder.sample(features)
                sen_json.append({'image_id': int(img_id), 'caption': sens[0]})

        with open(test_path, 'w') as f:
            json.dump(sen_json, f)

        result = coco_eval(self.test_cap, test_path)
        for metric, score in result:
            print(metric, score)

    def load_models(self, save_path):
        ckpt = torch.load(save_path,
                          map_location={'cuda:2': 'cuda:0'
                                        })  # 映射是因为解决保存模型的卡与加载模型的卡不一致的问题
        encoder_state_dict = ckpt['encoder_state_dict']
        self.encoder.load_state_dict(encoder_state_dict)
        decoder_state_dict = ckpt['decoder_state_dict']
        self.decoder.load_state_dict(decoder_state_dict)

    def set_eval(self):
        self.encoder.eval()
        self.decoder.eval()
def main():
    parser = argparse.ArgumentParser(description="FGSM")
    parser.add_argument("--device", type=str, default="cuda")
    parser.add_argument("--data_root", type=str, default="./data")
    parser.add_argument("--data_name", type=str, default="mnist")
    parser.add_argument("--image_size", type=int, default=32)
    parser.add_argument("--image_channels", type=int, default=1)
    parser.add_argument("--epsilon", type=int, default=0.1)
    opt = parser.parse_args()

    model = CNN(opt.image_size, opt.image_channels).to(opt.device)
    model.load_state_dict(torch.load("./weights/cnn.pth"))

    test(model, opt)
Example #14
0
    def build(self, is_train):
        if torch.cuda.is_available():
            self.model = nn.DataParallel(CNN(self.config)).cuda()
        else:
            self.model = CNN(self.config)

        self.loss_fn = self.config.loss_fn()

        if is_train:
            self.model.train()
            self.optimizer = self.config.optimizer(self.model.parameters(), lr=self.config.lr)
        else:
            if torch.cuda.is_available():
                self.model = self.model.module
            self.model.eval()
Example #15
0
 def __init__(self, ckpt_path):
     super().__init__()
     self.transform = transforms.Compose([
         transforms.ToPILImage(),
         transforms.Resize(256),
         transforms.ToTensor()
     ])
     self.checkpoint = torch.load(ckpt_path)
     self.model = CNN()
     self.model.load_state_dict(state_dict=self.checkpoint['state_dict'])
     self.use_cuda = False
     self.model.eval()
     if torch.cuda.is_available():
         self.model.cuda()
         self.use_cuda = True
Example #16
0
def main(args):
    train, test = datasets.get_mnist(withlabel=True, ndim=3)
    train_iter = iterators.SerialIterator(train, args.batchsize)
    test_iter = iterators.SerialIterator(test,
                                         args.batchsize,
                                         repeat=False,
                                         shuffle=False)

    model = L.Classifier(CNN())

    if args.gpu >= 0:
        cuda.check_cuda_available()
        cuda.get_device(args.gpu).use()
        model.to_gpu()

    optimizer = optimizers.Adam()
    optimizer.setup(model)

    updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu)

    trainer = training.Trainer(updater, (args.epochs, 'epoch'))
    trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu))
    trainer.extend(extensions.LogReport())  # Default log report
    trainer.extend(
        extensions.PrintReport([
            'epoch', 'main/loss', 'main/accuracy', 'validation/main/loss',
            'validation/main/accuracy'
        ]))
    trainer.extend(extensions.ProgressBar())
    trainer.run()
Example #17
0
    def _train(self, train_images, train_labels, test_images, test_labels):
        # PARAMETERS
        epoch = 1000
        batch_size = 512
        early = EarlyStopping(monitor='loss',
                              min_delta=0,
                              patience=300,
                              verbose=1,
                              mode='auto')

        # BUILD MODEL
        self.model = CNN.get_model()

        #%%time
        hist = self.model.fit(train_images,
                              train_labels,
                              epochs=epoch,
                              batch_size=batch_size,
                              validation_data=(test_images[:-2000],
                                               test_labels[:-2000]),
                              verbose=1,
                              callbacks=[early])
        # plot loss and test scatter
        self._plot(hist, test_images[-2000:], test_labels[-2000:])

        # save model
        h5_file = os.path.join(self.temp_path, 'model.h5')
        self.model.save(h5_file)
def slicing_window(img, model_weights):
    stride_width = 5
    stride_height = 5
    width = 20
    height = 20
    network = CNN.CNN(num_classes=27,
                      sample=img[0, 0:0 + height, 0:0 + height],
                      model_weights=model_weights,
                      network_type='simple')

    predictions = []
    for i in range(0, img.shape[1] - height, stride_height):
        for j in range(0, img.shape[2] - width, stride_width):
            predicted_prob = network.predict_character(img[:, i:i + height,
                                                           j:j + width])
            if np.amax(predicted_prob) > 0.7 and np.argmax(
                    predicted_prob) != 0 and check_if_all_zero(
                        img[:, i:i + height, j:j + width]):
                predictions.append([np.argmax(predicted_prob), j, i])
                print(str(chr(96 + np.argmax(predicted_prob))), j, i)
    fig, ax = plt.subplots(1)
    ax.imshow(img[0, :, :, 0], cmap='gray')
    for i in range(len(predictions)):
        rect = patches.Rectangle((predictions[i][1], predictions[i][2]),
                                 20,
                                 20,
                                 linewidth=1,
                                 edgecolor='r',
                                 facecolor='none')
        # Add the patch to the Axes
        ax.add_patch(rect)
    plt.show()
Example #19
0
def infer():
    fluid.enable_dygraph(device)
    processor = SentaProcessor(data_dir=args.data_dir,
                               vocab_path=args.vocab_path,
                               random_seed=args.random_seed)

    infer_data_generator = processor.data_generator(
        batch_size=args.batch_size,
        padding_size=args.padding_size,
        places=device,
        phase='infer',
        epoch=1,
        shuffle=False)
    if args.model_type == 'cnn_net':
        model_infer = CNN(args.vocab_size, args.batch_size, args.padding_size)
    elif args.model_type == 'bow_net':
        model_infer = BOW(args.vocab_size, args.batch_size, args.padding_size)
    elif args.model_type == 'gru_net':
        model_infer = GRU(args.vocab_size, args.batch_size, args.padding_size)
    elif args.model_type == 'bigru_net':
        model_infer = BiGRU(args.vocab_size, args.batch_size,
                            args.padding_size)

    print('Do inferring ...... ')
    inputs = [Input([None, None], 'int64', name='doc')]
    model_infer.prepare(None,
                        CrossEntropy(),
                        Accuracy(topk=(1, )),
                        inputs,
                        device=device)
    model_infer.load(args.checkpoints, reset_optimizer=True)
    preds = model_infer.predict(test_data=infer_data_generator)
    preds = np.array(preds[0]).reshape((-1, 2))

    if args.output_dir:
        with open(os.path.join(args.output_dir, 'predictions.json'), 'w') as w:

            for p in range(len(preds)):
                label = np.argmax(preds[p])
                result = json.dumps({
                    'index': p,
                    'label': label,
                    'probs': preds[p].tolist()
                })
                w.write(result + '\n')
        print('Predictions saved at ' +
              os.path.join(args.output_dir, 'predictions.json'))
Example #20
0
def main():
    parser = argparse.ArgumentParser(description="CNN")
    parser.add_argument("--num_epoch", type=int, default=30)
    parser.add_argument("--batch_size", type=int, default=64)
    parser.add_argument("--device", type=str, default="cuda")
    parser.add_argument("--data_root", type=str, default="./data")
    parser.add_argument("--data_name", type=str, default="mnist")
    parser.add_argument("--image_size", type=int, default=32)
    parser.add_argument("--image_channels", type=int, default=1)
    opt = parser.parse_args()

    model = CNN(opt.image_size, opt.image_channels).to(opt.device)
    for epoch in range(opt.num_epoch):
        loss = train(model, opt)
        print("loss: {:.6f}".format(loss))

    torch.save(model.state_dict(), "./weights/cnn.pth")
Example #21
0
def test_SimSiam():
    cnn = CNN(num_channels=1,
              num_classes=10,
              maxpool=False,
              wpool=5,
              p_dropout=0.0)
    simsiam = SimSiam(backbone=cnn, p_dropout=0.0)
    return simsiam
Example #22
0
def main():
    dataset = 'new'  # Which dataset to use
    batch_size = 12
    epochs = 50

    model = net.create_model()

    current_dir = os.path.dirname(__file__)
    training_dir = os.path.join(current_dir, 'datasets/', dataset, 'training/')
    validation_dir = os.path.join(current_dir, 'datasets/', dataset,
                                  'validation/')

    # Calculate training & validation steps
    num_images = 0
    for folder in ['true/', 'false/']:
        dir = os.path.join(training_dir, folder)
        num_images += len(os.listdir(dir))
    train_steps = num_images / batch_size
    num_images = 0
    for folder in ['true/', 'false/']:
        dir = os.path.join(validation_dir, folder)
        num_images += len(os.listdir(dir))
    val_steps = num_images / batch_size

    print('Dataset: ' + dataset)
    print('batch_size: ' + str(batch_size))
    print('train_steps: ' + str(train_steps))
    print('val_steps: ' + str(val_steps))
    print('epochs: ' + str(epochs))

    # All images will be rescaled by 1./255
    train_datagen = ImageDataGenerator(rescale=1. / 255)
    valid_datagen = ImageDataGenerator(rescale=1. / 255)

    # Flow training images in batches of 20 using train_datagen generator
    train_generator = train_datagen.flow_from_directory(
        training_dir,  # This is the source directory for training images
        target_size=(150, 150),  # All images will be resized to 150x150
        batch_size=batch_size,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

    # Flow validation images in batches of 20 using test_datagen generator
    validation_generator = valid_datagen.flow_from_directory(
        validation_dir,
        target_size=(150, 150),
        batch_size=batch_size,
        class_mode='binary')

    # Train model
    history = model.fit_generator(train_generator,
                                  steps_per_epoch=train_steps,
                                  epochs=epochs,
                                  validation_data=validation_generator,
                                  validation_steps=val_steps,
                                  verbose=2)

    evaluate(history)
Example #23
0
def predict(img_dir='./data/test'):
    transforms = Compose([Resize(height, weight), ToTensor()])
    dataset = CaptchaData(img_dir, transform=transforms)
    cnn = CNN()
    if torch.cuda.is_available():
        cnn = cnn.cuda()
    cnn.eval()
    cnn.load_state_dict(torch.load(model_path))

    for k, (img, target) in enumerate(dataset):
        img = img.view(1, 3, height, weight).cuda()
        target = target.view(1, 4 * 36).cuda()
        output = cnn(img)

        output = output.view(-1, 36)
        target = target.view(-1, 36)
        output = nn.functional.softmax(output, dim=1)
        output = torch.argmax(output, dim=1)
        target = torch.argmax(target, dim=1)
        output = output.view(-1, 4)[0]
        target = target.view(-1, 4)[0]

        print('pred: ' + ''.join([alphabet[i] for i in output.cpu().numpy()]))
        print('true: ' + ''.join([alphabet[i] for i in target.cpu().numpy()]))

        plot.imshow(img.permute((0, 2, 3, 1))[0].cpu().numpy())
        plot.show()

        if k >= 10: break
Example #24
0
def post_predict():
    text = "failure"
    if request.method == 'POST':
        file = request.get_data()
        img = Image.open(BytesIO(file)).convert('RGB')
        print('宽:%d,高:%d' % (img.size[0], img.size[1]))
        width = img.size[0]
        height = img.size[1]
        transform = Compose([Resize(height, width), ToTensor()])
        img = transform(img)
        cnn = CNN()
        if torch.cuda.is_available():
            cnn = cnn.cuda()
        cnn.eval()
        cnn.load_state_dict(torch.load(model_path, map_location='cpu'))
        img = img.view(1, 3, height, width).cuda()
        output = cnn(img)
        output = output.view(-1, 36)
        output = nn.functional.softmax(output, dim=1)
        output = torch.argmax(output, dim=1)
        output = output.view(-1, 4)[0]
        text = ''.join([alphabet[i] for i in output.cpu().numpy()])
        # print('pred: '+text)
        # plot.imshow(img.permute((0, 2, 3, 1))[0].cpu().numpy())
        # plot.show()

    return text
Example #25
0
def predict(img_dir=Path('./captcha')):
    transforms = Compose([ToTensor()])
    for i in img_dir.glob("*png"):
        print(i.name)
        img = img_loader('./captcha/%s' % i.name)
        img = transforms(img)
        cnn = CNN()
        cnn.load_state_dict(torch.load(model_path))

        img = img.view(1, 3, 36, 120)
        output = cnn(img)

        output = output.view(-1, 36)
        output = nn.functional.softmax(output, dim=1)
        output = torch.argmax(output, dim=1)
        output = output.view(-1, num_class)[0]

        pred = ''.join([alphabet[i] for i in output.cpu().numpy()])
        print(pred)
Example #26
0
def train(model_type, epochs, batch_size, logdir):
    ds_train, ds_test = tfds.load('cifar10',
                                  split=['train', 'test'],
                                  as_supervised=True)
    num_samples = 50000
    num_samples_test = 10000
    ds_train = ds_train.shuffle(num_samples)\
      .batch(batch_size)\
      .map(preprocess, num_parallel_calls=tf.data.experimental.AUTOTUNE)\
      .repeat(epochs)\
      .prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

    ds_test = ds_test.batch(batch_size)\
      .map(preprocess, num_parallel_calls=tf.data.experimental.AUTOTUNE)\
      .repeat(epochs)\
      .prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

    kl_div_fn = (lambda q, p, _: tfd.kl_divergence(q, p) / num_samples)

    if model_type == 'normal':
        model = CNN(num_classes=10)
    elif model_type == 'reparam':
        model = ReparamCNN(num_classes=10, kernel_divergence_fn=kl_div_fn)
    else:
        model = FlipOutCNN(num_classes=10, kernel_divergence_fn=kl_div_fn)
    # Set input_shape explicitly (before compile) to instantiate model.losses
    model.build(input_shape=[None, 32, 32, 3])

    optimizer = optimizers.Adam()
    loss_fn = losses.SparseCategoricalCrossentropy(from_logits=True)
    metrics = [tf.keras.metrics.SparseCategoricalAccuracy(name='accuracy')]
    model.compile(optimizer, loss=loss_fn, metrics=metrics)

    callbacks = [tf.keras.callbacks.TensorBoard(log_dir=logdir)]
    model.fit(ds_train,
              epochs=epochs,
              callbacks=callbacks,
              validation_data=ds_test,
              steps_per_epoch=num_samples // batch_size,
              validation_steps=num_samples_test // batch_size)

    return None
Example #27
0
    def __init__(self,
                 paths,
                 batch_size=6,
                 iterations=50,
                 initial_lr=0.003,
                 hidden_size=256,
                 dropout=0.2,
                 kernel_sz=3):

        self.use_cuda = torch.cuda.is_available()
        self.device = torch.device('cuda:0' if self.use_cuda else 'cpu')

        self.data = DataReader(paths)
        self.data.set_training_data(batch_size,
                                    ('cuda:0' if self.use_cuda else 'cpu'))
        self.train_batch_loader = BatchGenerator(self.data.train_data,
                                                 'Sentence', 'Label')
        self.val_batch_loader = BatchGenerator(self.data.val_data, 'Sentence',
                                               'Label')
        self.test_batch_loader = BatchGenerator(self.data.test_data,
                                                'Sentence', 'Label')

        # Store hyperparameters
        self.batch_size = batch_size
        self.iterations = iterations
        self.initial_lr = initial_lr
        self.kernel_sz = kernel_sz

        # Create Model
        emb_size, emb_dim = self.data.TEXT.vocab.vectors.size()
        self.cnn_model = CNN(emb_size=emb_size,
                             emb_dimension=emb_dim,
                             n_out=len(self.data.LABEL.vocab),
                             dropout=dropout,
                             kernel_sz=kernel_sz,
                             stride=1,
                             padding=0,
                             out_filters=hidden_size,
                             pretrained_emb=self.data.TEXT.vocab.vectors)

        if self.use_cuda:
            self.cnn_model.cuda()
Example #28
0
def main(args):
    print('[*] Arguments: %s' % args)
    print('[*] Read MNIST...')
    num_test_images = args.num_test_images
    images, labels = load_mnist('test', path='./data', max_ind=num_test_images)
    images, labels = images[:num_test_images, :, :], labels[:num_test_images]
    images = images.astype(np.float32)
    images = images / 255.
    print('[*] The shape of image: %s' % str(images.shape))

    print('[*] Load the network...')
    if args.network == 'mlp':  # Lab 2
        if args.quantized:
            print('[!] MLP does not support quantization')
            return
        model_path = os.path.join('./pretrained_weights',
                                  'mlp_iter_10000.caffemodel')
        net = MLP(model_path, args)
    elif args.network == 'cnn':
        if args.quantized:  # Lab 14
            model_path = os.path.join('./pretrained_weights',
                                      'quantized_cnn_weights.txt')
        else:  # Lab 11
            model_path = os.path.join('./pretrained_weights',
                                      'cnn_weights.txt')
        net = CNN(model_path, args)
    else:
        raise

    print('[*] Run tests...')
    test_images = [images[i, :, :].copy() for i in xrange(num_test_images)]
    n_correct = 0
    start_time = time.time()

    for i in xrange(num_test_images):
        X = test_images[i]
        X = X.reshape((28 * 28))  # 28x28->784

        logit = net.inference(X)
        prediction = np.argmax(logit)
        label = labels[i, ]

        n_correct += (label == prediction)

    print('[*] Statistics...')
    model_stats = {
        'total_time': time.time() - start_time,
        'total_image': num_test_images,
        'accuracy': float(n_correct) / num_test_images,
        'avg_num_call': net.total_num_call[0] / num_test_images,
        'm_size': net.m_size,
        'v_size': net.v_size,
    }
    pp.pprint(model_stats)
Example #29
0
    def __init__(self, _hparams):
        utils.set_seed(_hparams.fixed_seed)

        self.train_loader = get_train_loader(_hparams)
        self.val_loader = get_val_loader(_hparams)
        self.encoder = CNN().to(DEVICE)
        self.decoder = RNN(fea_dim=_hparams.fea_dim,
                           embed_dim=_hparams.embed_dim,
                           hid_dim=_hparams.hid_dim,
                           max_sen_len=_hparams.max_sen_len,
                           vocab_pkl=_hparams.vocab_pkl).to(DEVICE)
        self.loss_fn = nn.CrossEntropyLoss()
        self.optimizer = torch.optim.Adam(self.get_params(), lr=_hparams.lr)
        self.writer = SummaryWriter()

        self.max_sen_len = _hparams.max_sen_len
        self.val_cap = _hparams.val_cap
        self.ft_encoder_lr = _hparams.ft_encoder_lr
        self.ft_decoder_lr = _hparams.ft_decoder_lr
        self.best_CIDEr = 0
Example #30
0
def run_forrest_run(dataset_list, activation_list, modelname):
    for dataset_name in dataset_list:
        for name in activation_list:
            for model in modelname:
                if model == "DNN":
                    dataset = Datasets()
                    if (dataset_name == 'MNIST'):
                        x_train, x_test, y_train, y_test = dataset.get_mnist(
                            "DNN")
                        num_classes = dataset.num_classes
                        input_shape = dataset.input_shape
                    elif (dataset_name == 'Fashion-MNIST'):
                        x_train, x_test, y_train, y_test = dataset.get_fashion_mnist(
                            "DNN")
                        num_classes = dataset.num_classes
                        input_shape = dataset.input_shape
                    dnn = DNN(name)
                    score, history = dnn.run_model(input_shape, x_train,
                                                   x_test, y_train, y_test, 1)

                else:
                    dataset = Datasets()
                    if (dataset_name == 'MNIST'):
                        x_train, x_test, y_train, y_test = dataset.get_mnist(
                            "CNN")
                    elif (dataset_name == 'Fashion-MNIST'):
                        x_train, x_test, y_train, y_test = dataset.get_fashion_mnist(
                            "CNN")
                    num_classes = dataset.num_classes
                    input_shape = dataset.input_shape
                    if model == "CNN":
                        cnn = CNN(name)
                        score, history = cnn.run_model(input_shape, x_train,
                                                       x_test, y_train, y_test)
                    elif model == "CAE":
                        cae = CAE(name)
                        score, history = cae.run_model(input_shape, x_train,
                                                       x_test, y_train, y_test)
                    score, history = cnn.run_model(input_shape, x_train,
                                                   x_test, y_train, y_test)
                plot_model(history, name, model, dataset_name)