Esempio n. 1
0
def main():
    # Training settings
    from config import args
    from config import train_dataset
    from config import test_dataset
    from config import fun_params

    args = args
    use_cuda = not args.no_cuda and torch.cuda.is_available()
    torch.manual_seed(args.seed)

    device = torch.device("cuda" if use_cuda else "cpu")

    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

    train_dataset = train_dataset
    test_dataset = test_dataset

    train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, **kwargs)
    test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=args.test_batch_size, shuffle=True, **kwargs)

    target_model = CNN(input_channel=1)
    target_model = torch.nn.DataParallel(target_model).cuda()
    target_optimizer = optim.SGD(target_model.parameters(), lr=args.lr, momentum=args.momentum)

    for epoch in range(args.target_epoch):
        train(args, target_model, device, train_loader, target_optimizer, epoch)
    target_model_path = os.path.join(fun_params['logdir'], 'target_model.pth.tar')
    torch.save({'state_dict': target_model.state_dict()}, target_model_path)
    test_loss, correct = test(target_model, test_loader)
    print('Target model completed. Test loss %f, test accuracy %f' % (test_loss, correct))
    trainer = OuterTrainer(target_model, device, train_dataset, test_dataset, fun_params)
    trainer.train(fun_params['n_process'])
class Classifier:
    def __init__(
        self,
        model_path='/Users/archiegertsman/Desktop/CS126/Final Project/finalproject-ArchieGertsman/src/classification/res/model.pt'
    ):
        from network import CNN

        # load a model
        self.model = CNN()
        self.model.load_state_dict(torch.load(model_path))
        self.model.eval()

    def classify(self, img_path):
        from character_dataset import CharacterDataset
        import image_utils as iu

        # load image
        img = iu.read_image_as_tensor(img_path)

        # make prediction using model
        output = self.model(img)
        prediction_idx = torch.argmax(output).item()
        confidence = torch.max(output).item()

        # returns ascii value of predicted character
        return (ord(CharacterDataset.CHARSET[prediction_idx]), confidence)
    def __init__(
        self,
        model_path='/Users/archiegertsman/Desktop/CS126/Final Project/finalproject-ArchieGertsman/src/classification/res/model.pt'
    ):
        from network import CNN

        # load a model
        self.model = CNN()
        self.model.load_state_dict(torch.load(model_path))
        self.model.eval()
Esempio n. 4
0
    def __init__(self, num_epochs=5, batch_size=4, lr=0.001):
        self.num_epochs = num_epochs

        trainset = CharacterDataset()
        self.trainloader = torch.utils.data.DataLoader(trainset,
                                                       batch_size=batch_size,
                                                       shuffle=True,
                                                       num_workers=2)

        self.net = CNN()
        self.criterion = nn.CrossEntropyLoss()
        self.optimizer = optim.SGD(self.net.parameters(), lr=lr, momentum=0.9)
Esempio n. 5
0
def main(model_name, model_version, moves_path, data_path):

    # Grab the testing data as a DataLoader
    test_set = PeakDataset(moves_path + '/test_moves.pt',
                           data_path + '/test/%s/test_%d.pt')

    # Load the saved network
    hparams = torch.load('../networks/' + str(model_version) + '/hparams')
    model = torch.load('../networks/' + str(model_version) + '/model_9')
    net = LinearSkip(num_layers=hparams['layers'], num_units=hparams['units'])
    net.load_state_dict(model['model_state_dict'])

    if model_name == 'linear':
        net = Linear(num_layers=hparams['layers'], num_units=hparams['units'])
        net.load_state_dict(model['model_state_dict'])
    elif model_name == 'linearskip':
        net = LinearSkip(num_layers=hparams['layers'],
                         num_units=hparams['units'],
                         bottleneck=hparams['bottleneck'])
        net.load_state_dict(model['model_state_dict'])
    elif model_name == 'conv':
        net = CNN(num_layers=hparams['layers'],
                  num_filters=hparams['filters'],
                  filter_size=hparams['size'],
                  stride=hparams['stride'],
                  pad=hparams['padding'])
        net.load_state_dict(model['model_state_dict'])

    # Test the network
    test_performance(net, test_set, model_version)
Esempio n. 6
0
def main():
    model = L.Classifier(CNN())

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

    train = load_data('./nene_data/train')
    test = load_data('./nene_data/test')
    train_iter = chainer.iterators.SerialIterator(train, batch_size=100)
    test_iter = chainer.iterators.SerialIterator(test,
                                                 batch_size=100,
                                                 repeat=False,
                                                 shuffle=False)

    updater = training.StandardUpdater(train_iter, optimizer, device=None)
    trainer = training.Trainer(updater, (100, 'epoch'), out='result')
    trainer.extend(extensions.Evaluator(test_iter, model, device=None))
    trainer.extend(extensions.LogReport())
    trainer.extend(
        extensions.PrintReport([
            'epoch', 'main/loss', 'validation/main/loss', 'main/accuracy',
            'validation/main/accuracy'
        ]))
    trainer.extend(extensions.ProgressBar())

    trainer.run()

    modeldir = './model'
    if not os.path.isdir(modeldir):
        os.mkdir(modeldir)
    serializers.save_npz(os.path.join(modeldir, 'model.npz'), model)
    serializers.save_npz(os.path.join(modeldir, 'optimizer.npz'), optimizer)
Esempio n. 7
0
class Trainer:
    def __init__(self, num_epochs=5, batch_size=4, lr=0.001):
        self.num_epochs = num_epochs

        trainset = CharacterDataset()
        self.trainloader = torch.utils.data.DataLoader(trainset,
                                                       batch_size=batch_size,
                                                       shuffle=True,
                                                       num_workers=2)

        self.net = CNN()
        self.criterion = nn.CrossEntropyLoss()
        self.optimizer = optim.SGD(self.net.parameters(), lr=lr, momentum=0.9)

    def train(self):
        for epoch in range(self.num_epochs):
            for i, data in enumerate(self.trainloader, 0):
                # get the inputs
                inputs, labels = data

                # zero the parameter gradients
                self.optimizer.zero_grad()

                # forward + backward + optimize
                outputs = self.net(inputs)
                loss = self.criterion(outputs, labels)
                loss.backward()
                self.optimizer.step()

    def test_model_against_trainset(self):
        correct = 0
        total = 0

        with torch.no_grad():
            for data in self.trainloader:
                images, labels = data

                outputs = self.net(images)
                _, predicted = torch.max(outputs.data, 1)

                total += labels.size(0)
                correct += (predicted == labels).sum().item()

        return correct / total

    def save_model(self, filepath='res/model.pt'):
        torch.save(self.net.state_dict(), filepath)
Esempio n. 8
0
 def __init__(self):
     # 制作模型,采用adam优化器、交叉熵作为损失函数,并以准确度作为模型训练结果
     self.network = CNN()
     self.network.model.compile(optimizer='adam',
                                loss='sparse_categorical_crossentropy',
                                metrics=['accuracy'])
     # 读入数据
     self.data = DataSource()
     print('【网络建立及数据读入已完成】')
Esempio n. 9
0
def entropy_single_input(im_path,
                         norm_size,
                         model_path,
                         n_bins,
                         ignore_lowest,
                         reduction,
                         device='cpu'):
    """
    Calculate entropy of a single image and its prediction
    :param im_path: path to an image file
    :param norm_size: image normalization size, (width, height)
    :param model_path: path of the saved model
    :param n_bins: the bins to be divided
    :param ignore_lowest: whether to ignore the lowest value when calculating the entropy
    :param reduction: 'mean' or 'sum', the way to reduce results of c channels
    :param device: 'cpu' or 'cuda'
    :return: image entropy and predicted probability entropy
    """

    # read image and calculate image entropy
    im = cv2.imread(im_path)
    im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    im = cv2.resize(im, norm_size)
    ent_im = im_entropy(im)
    # preprocess
    im = (torch.from_numpy(im).float() - 127.5) / 127.5
    im = im.view(1, 1, norm_size[1], norm_size[0])
    im = im.to(device)

    # initialize the model
    print('[Info] loading checkpoints from %s ...' % model_path)
    checkpoint = torch.load(model_path)
    configs = checkpoint['configs']
    model = CNN(configs['in_channels'],
                configs['num_class'],
                batch_norm=configs['batch_norm'],
                p=configs['dropout'])
    # load model parameters (checkpoint['model_state']) we saved in model_path using model.load_state_dict()
    model.load_state_dict(checkpoint['model_state'])
    model = model.to(device)

    # calculate prediction entropy
    model.eval()
    with torch.no_grad():
        f1, f2, f3, f4, f5, out = model(im, return_features=True)
    ent_f1 = feature_entropy(f1[0], n_bins, ignore_lowest, reduction)
    ent_f2 = feature_entropy(f2[0], n_bins, ignore_lowest, reduction)
    ent_f3 = feature_entropy(f3[0], n_bins, ignore_lowest, reduction)
    ent_f4 = feature_entropy(f4[0], n_bins, ignore_lowest, reduction)
    ent_f5 = feature_entropy(f5[0], n_bins, ignore_lowest, reduction)
    pred = out[0].argmax().item()
    pred = chr(pred + ord('A'))
    prob = out[0].softmax(0).cpu().numpy()
    confidence = prob.max()
    prob = prob[prob > 0]
    ent_pred = np.sum(-prob * np.log(prob))

    return pred, confidence, ent_im, ent_f1, ent_f2, ent_f3, ent_f4, ent_f5, ent_pred, f1[
        0], f2[0], f3[0], f4[0], f5[0]
Esempio n. 10
0
def main(permute):
    batch_size = 100
    train_data, val_data, test_data = create_train_val_test_split(batch_size)
    val_data = make_batch(len(val_data), 0, val_data, use_cuda = True, volatile = True)
    test_data = make_batch(len(test_data), 0, test_data, use_cuda = True, volatile = True)

    cnn = CNN().cuda()
    fcc = FCC().cuda()
    
    cnn_test_loss = Logger("cnn_test_losses.txt")
    cnn_test_acc = Logger("cnn_test_acc.txt")
    fcc_test_loss = Logger("fcc_test_losses.txt")
    fcc_test_acc = Logger("fcc_test_acc.txt")
    for i in range(0, 100001, 1000):
        print(i)
        cnn.load_state_dict(torch.load("savedir/cnn_it"+str(i//1000)+"k.pth"))
        evaluate_acc(batch_size, cnn, test_data, i, cnn_test_loss, cnn_test_acc, permute)
        fcc.load_state_dict(torch.load("savedir/fcc_it"+str(i//1000)+"k.pth"))
        evaluate_acc(batch_size, fcc, test_data, i, fcc_test_loss, fcc_test_acc, permute)
Esempio n. 11
0
def evaluate(model_path, root, test_list, batch_size):
    channel = 1

    device = 'cuda'

    model = CNN(channel)
    model = nn.DataParallel(model)

    model.module.load_state_dict(torch.load(model_path))

    model.eval()  # 推論モードへ切り替え(Dropoutなどの挙動に影響)

    df_test = pd.read_csv(test_list)
    test_loader = LoadDataset(df_test, root)
    test_dataset = torch.utils.data.DataLoader(test_loader,
                                               batch_size=batch_size,
                                               shuffle=True,
                                               drop_last=True)

    correct = 0

    for img, label in test_dataset:
        img = img.float()
        img = img / 255
        # 3次元テンソルを4次元テンソルに変換(1チャネルの情報を追加)
        batch, height, width = img.shape
        img = torch.reshape(img, (batch_size, 1, height, width))

        img = img.to(device)

        output = model(img)
        pred = output.data.max(1, keepdim=False)[1]
        for i, l in enumerate(label):
            if l == pred[i]:
                correct += 1

    data_num = len(test_dataset.dataset)  # データの総数
    acc = correct / data_num * 100  # 精度

    print('Accuracy for test dataset: {}/{} ({:.1f}%)'.format(
        correct, data_num, acc))
Esempio n. 12
0
    def test(self, model_path):
        model = CNN(input_channel=1)
        model = torch.nn.DataParallel(model).cuda()
        optimizer = optim.SGD(model.parameters(),
                              lr=self.inner_args['inner_lr'],
                              momentum=self.inner_args['inner_momentum'])
        cross_entropy = SoftCrossEntropy()
        model.train()

        self.target_model.eval()

        eloss = loss_net(self.inner_args['nclass'])
        eloss = torch.nn.DataParallel(eloss).cuda()

        assert os.path.exists(model_path), 'model path is not exist.'
        check_point = torch.load(model_path)
        eloss.load_state_dict(check_point['state_dict'])

        eloss.eval()
        minloss = 10000
        # print('start train model')
        train_loader = torch.utils.data.DataLoader(
            self.train_dataset,
            batch_size=self.inner_args['inner_batch_size'],
            shuffle=True)
        test_loader = torch.utils.data.DataLoader(
            self.test_dataset,
            batch_size=self.inner_args['test_batch_size'],
            shuffle=True)

        for batch_idx, (data, target) in enumerate(train_loader):
            data = data.cuda()
            optimizer.zero_grad()

            output = model(data)
            target_output = self.target_model(data)
            target = target.view(-1, 1)

            target = torch.zeros(data.shape[0],
                                 10).scatter_(1, target.long(), 1.0)
            varInput = torch.cat((target.cuda(), target_output), 1)
            # print(varInput.shape, target_output.shape)
            soft_target = eloss.forward(varInput)
            # print(soft_target)
            loss = cross_entropy(output, soft_target)
            if self.save_model:
                self.log.log_tabular('epoch', batch_idx)
                self.log.log_tabular('loss', loss.item())
                self.log.dump_tabular()
                if loss < minloss:
                    torch.save(
                        {
                            'epoch': batch_idx + 1,
                            'state_dict': model.state_dict(),
                            'best_loss': minloss,
                            'optimizer': optimizer.state_dict()
                        }, self.log.path_model)
            loss.backward()
            optimizer.step()
Esempio n. 13
0
def feature_entropy_dataset(n_bins,
                            ignore_lowest,
                            reduction,
                            file_path,
                            norm_size,
                            batch_size,
                            model_path,
                            device='cpu'):
    """
    Calculate entropy of features extracted by our model.
    :param n_bins: the bins to be divided
    :param ignore_lowest: whether to ignore the lowest value when calculating the entropy
    :param reduction: 'mean' or 'sum', the way to reduce results of c channels
    :param file_path: path to directory with images
    :param norm_size: image normalization size, (width, height)
    :param batch_size: batch size
    :param model_path: path of the saved model
    :param device: 'cpu' or 'cuda'
    :return: the entropy of features
    """

    # initialize dataloader and model
    dataloader = dataLoader(file_path, norm_size, batch_size)
    print('[Info] loading checkpoints from %s ...' % model_path)
    checkpoint = torch.load(model_path)
    configs = checkpoint['configs']
    model = CNN(configs['in_channels'],
                configs['num_class'],
                batch_norm=configs['batch_norm'],
                p=configs['dropout'])
    # load model parameters (checkpoint['model_state']) we saved in model_path using model.load_state_dict()
    model.load_state_dict(checkpoint['model_state'])
    model = model.to(device)

    # extract features and calculate entropy
    ent1, ent2, ent3, ent4, ent5 = 0., 0., 0., 0., 0.
    n_ims = 0
    model.eval()
    with torch.no_grad():
        for ims, _ in dataloader:
            ims = ims.to(device)
            feats1, feats2, feats3, feats4, feats5, _ = model(
                ims, return_features=True)
            n_ims += ims.size(0)
            for f1, f2, f3, f4, f5 in zip(feats1, feats2, feats3, feats4,
                                          feats5):
                ent1 += feature_entropy(f1, n_bins, ignore_lowest, reduction)
                ent2 += feature_entropy(f2, n_bins, ignore_lowest, reduction)
                ent3 += feature_entropy(f3, n_bins, ignore_lowest, reduction)
                ent4 += feature_entropy(f4, n_bins, ignore_lowest, reduction)
                ent5 += feature_entropy(f5, n_bins, ignore_lowest, reduction)

    return ent1 / n_ims, ent2 / n_ims, ent3 / n_ims, ent4 / n_ims, ent5 / n_ims
Esempio n. 14
0
def main():
    batch_size = 100
    mnist_train = datasets.MNIST("/hdd/Data/MNIST/",
                                 train=True,
                                 transform=transforms.ToTensor(),
                                 download=True)
    mnist_test = datasets.MNIST("/hdd/Data/MNIST/",
                                train=False,
                                transform=transforms.ToTensor(),
                                download=True)
    data_feeder = DataFeeder(mnist_train,
                             preprocess_workers=1,
                             cuda_workers=1,
                             cpu_size=10,
                             cuda_size=10,
                             batch_size=batch_size,
                             use_cuda=True,
                             volatile=False)
    data_feeder.start_queue_threads()
    cnn = ModelStruct(CNN().cuda(), 0.001)
    fcc = ModelStruct(FCC().cuda(), 0.001)

    test_data = make_batch(len(mnist_test), 0, mnist_test, use_cuda=True)

    for i in range(100001):
        images, labels = data_feeder.get_batch()
        train(cnn, images, labels, i)
        train(fcc, images, labels, i)
        if i % 100 == 0:
            evaluate_acc(cnn, test_data, i)
            evaluate_acc(fcc, test_data, i)
        if i in [33333, 66666]:
            decrease_lr(cnn)
            decrease_lr(fcc)
        if i % 20000 == 0:
            torch.save(cnn.model, "savedir/cnn_it" + str(i // 1000) + "k.pt")
            torch.save(fcc.model, "savedir/fcc_it" + str(i // 1000) + "k.pt")

    print(max(cnn.acc))
    print(max(fcc.acc))
    graph(fcc, cnn)
    cnn.losses = losses_to_ewma(cnn.losses)
    cnn.val_losses = losses_to_ewma(cnn.val_losses, alpha=0.3)
    cnn.acc = losses_to_ewma(cnn.acc)
    fcc.losses = losses_to_ewma(fcc.losses)
    fcc.val_losses = losses_to_ewma(fcc.val_losses, alpha=0.3)
    fcc.acc = losses_to_ewma(fcc.acc)
    graph(fcc, cnn)

    data_feeder.kill_queue_threads()
Esempio n. 15
0
def inference(img):
    model = L.Classifier(CNN())
    serializers.load_npz('./model/model.npz', model)

    if img.shape[2] == 4:
        img = skimage.color.rgba2rgb(img)
    height, width = img.shape[:2]
    img = rescale(img, (IMAGE_SIZE / height, IMAGE_SIZE / width),
                  mode='constant')
    im = img.astype(np.float32).reshape(1, IMAGE_SIZE, IMAGE_SIZE, 3)
    im = im.transpose(0, 3, 1, 2)
    x = Variable(im)
    y = model.predictor(x)
    [pred] = y.data
    recog = np.argmax(pred)
    return recog, im.reshape(3, IMAGE_SIZE, IMAGE_SIZE).transpose(1, 2, 0)
Esempio n. 16
0
    def train(self, epoch, pool_rank, phi):
        model = CNN(input_channel=1)
        model = torch.nn.DataParallel(model).cuda()
        optimizer = optim.SGD(model.parameters(),
                              lr=self.inner_lr,
                              momentum=self.inner_momentum)
        cross_entropy = SoftCrossEntropy()
        model.train()

        self.target_model.eval()

        eloss = loss_net(self.nclass)
        eloss = torch.nn.DataParallel(eloss).cuda()
        for key in eloss.state_dict().keys():
            eloss.state_dict()[key] = eloss.state_dict()[key] + phi[key]
        eloss.eval()
        minloss = 10000
        # print('start train model')
        for e in range(self.inner_epoch_freq):
            for batch_idx, (data, target) in enumerate(self.train_loader):
                data = data.cuda()
                optimizer.zero_grad()

                output = model(data)
                target_output = self.target_model(data)
                target = target.view(-1, 1)

                target = torch.zeros(data.shape[0],
                                     10).scatter_(1, target.long(), 1.0)
                varInput = torch.cat((target.cuda(), target_output), 1)
                # print(varInput.shape, target_output.shape)
                soft_target = eloss.forward(varInput)
                # print(soft_target)
                loss = cross_entropy(output, soft_target)
                if self.save_model:
                    if epoch % 20 == 0:
                        self.log.log_tabular('epoch', batch_idx)
                        self.log.log_tabular('loss', loss.item())
                    if loss < minloss:
                        torch.save(
                            {
                                'epoch': batch_idx + 1,
                                'state_dict': model.state_dict(),
                                'best_loss': minloss,
                                'optimizer': optimizer.state_dict()
                            }, self.log.path_model)
                loss.backward()
                optimizer.step()
                # print('[pool: %d] epoch %d, loss: %f' % (pool_rank, epoch, loss.item()))

            if epoch % 20 == 0 and self.save_model:
                self.log.dump_tabular()

        accuracy = self.test(model)
        return accuracy
Esempio n. 17
0
def label_entropy_model(file_path,
                        norm_size,
                        batch_size,
                        model_path,
                        device='cpu'):
    """
    We use the trained model for prediction.
    :param file_path: path to directory with images
    :param norm_size: image normalization size, (width, height)
    :param batch_size: batch size
    :param model_path: path of the saved model
    :param device: 'cpu' or 'cuda'
    :return: the entropy
    """

    # initialize dataloader and model
    dataloader = dataLoader(file_path, norm_size, batch_size)
    print('[Info] loading checkpoints from %s ...' % model_path)
    checkpoint = torch.load(model_path)
    configs = checkpoint['configs']
    model = CNN(configs['in_channels'],
                configs['num_class'],
                batch_norm=configs['batch_norm'],
                p=configs['dropout'])
    # load model parameters (checkpoint['model_state']) we saved in model_path using model.load_state_dict()
    model.load_state_dict(checkpoint['model_state'])
    model = model.to(device)

    # extract features
    outs = []
    model.eval()
    with torch.no_grad():
        for ims, _ in dataloader:
            ims = ims.to(device)
            out = model(ims)
            outs.append(out)

    # calculate entropy
    probs = torch.cat(outs, 0).softmax(
        1)  # [n_ims, 26], probabilities of predicted characters
    probs = probs.cpu().numpy()
    ent = 0.
    for prob in probs:
        prob = prob[prob > 0]
        ent -= np.sum(prob * np.log(prob))
    ent /= len(probs)

    return ent
Esempio n. 18
0
                                                       transform=transform),
                                        batch_size=32,
                                        shuffle=False,
                                        num_workers=4)

    print("Train : ", len(trains.dataset))
    print("Test : ", len(tests.dataset))

    dropmax_cnn = DropMaxCNN(n_classes=n_classes).to(device)
    dropmax_loss = DropMax(device)
    adam = optim.Adam(dropmax_cnn.parameters(),
                      lr=0.0005,
                      betas=(0.5, 0.999),
                      weight_decay=1e-4)

    cnn = CNN(n_classes=n_classes).to(device)
    ce_loss = nn.CrossEntropyLoss()
    adam2 = optim.Adam(cnn.parameters(),
                       lr=0.0005,
                       betas=(0.5, 0.999),
                       weight_decay=1e-4)

    for epoch in range(30):
        dropmax_cnn.train()
        print("Epoch : ", epoch)
        for i, (img, target) in enumerate(trains):
            img = img.to(device)
            one_hot = torch.zeros(img.shape[0], n_classes)
            one_hot.scatter_(1, target.unsqueeze(dim=1), 1)
            one_hot = one_hot.to(device)
Esempio n. 19
0
if __name__ == "__main__":
    train_start = '2015-01-01 01:30:00'
    train_end = '2019-03-01 15:00:00'

    cv_start = '2019-03-02 01:30:00'
    cv_end = '2019-12-01 15:00:00'

    test_start = '2019-12-02 01:30:00'
    test_end = '2021-04-20 15:00:00'

    data_processor = DataProcessor()
    train_loader = data_processor.get_data(train_start, train_end)
    cv_loader = data_processor.get_data(cv_start, cv_end)
    test_loader = data_processor.get_data(test_start, test_end)

    model = CNN(transform='GAF')
    model = model.double()
    loss_fn = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)

    max_epochs = 100
    accuracy_list = []
    for t in range(max_epochs):
        print(f"Epoch {t+1}\n-------------------------------")
        train_loop(train_loader, model, loss_fn, optimizer)
        acc = test_loop(cv_loader, model, loss_fn)
        accuracy_list.append(acc)

        if len(accuracy_list) > 8:
            print(accuracy_list[-8:])
Esempio n. 20
0
def main():
    batch_size = 100
    train_data, val_data, test_data = create_train_val_test_split(batch_size)
    data_feeder = DataFeeder(train_data,
                             preprocess_workers=1,
                             cuda_workers=1,
                             cpu_size=10,
                             cuda_size=10,
                             batch_size=batch_size,
                             use_cuda=True,
                             volatile=False)
    data_feeder.start_queue_threads()
    val_data = make_batch(len(val_data),
                          0,
                          val_data,
                          use_cuda=True,
                          volatile=True)
    test_data = make_batch(len(test_data),
                           0,
                           test_data,
                           use_cuda=True,
                           volatile=True)

    cnn = CNN().cuda()
    fcc = FCC().cuda()

    optimizer_cnn = optim.SGD(cnn.parameters(),
                              lr=0.001,
                              momentum=0.9,
                              weight_decay=0.00001)
    optimizer_fcc = optim.SGD(fcc.parameters(),
                              lr=0.001,
                              momentum=0.9,
                              weight_decay=0.00001)

    cnn_train_loss = Logger("cnn_train_losses.txt")
    cnn_val_loss = Logger("cnn_val_losses.txt")
    cnn_val_acc = Logger("cnn_val_acc.txt")
    fcc_train_loss = Logger("fcc_train_losses.txt")
    fcc_val_loss = Logger("fcc_val_losses.txt")
    fcc_val_acc = Logger("fcc_val_acc.txt")

    #permute = Variable(torch.from_numpy(np.random.permutation(28*28)).long().cuda(), requires_grad=False)
    permute = None

    for i in range(100001):
        images, labels = data_feeder.get_batch()
        train(cnn, optimizer_cnn, images, labels, i, cnn_train_loss, permute)
        train(fcc, optimizer_fcc, images, labels, i, fcc_train_loss, permute)
        if i % 100 == 0:
            print(i)
            evaluate_acc(batch_size, cnn, val_data, i, cnn_val_loss,
                         cnn_val_acc, permute)
            evaluate_acc(batch_size, fcc, val_data, i, fcc_val_loss,
                         fcc_val_acc, permute)
        if i in [70000, 90000]:
            decrease_lr(optimizer_cnn)
            decrease_lr(optimizer_fcc)
        if i % 1000 == 0:
            torch.save(cnn.state_dict(),
                       "savedir/cnn_it" + str(i // 1000) + "k.pth")
            torch.save(fcc.state_dict(),
                       "savedir/fcc_it" + str(i // 1000) + "k.pth")

    data_feeder.kill_queue_threads()

    import evaluate
    evaluate.main(permute)
Esempio n. 21
0
(training_data, training_labels), (validation_data, validation_labels), (_, _) = \
  pickle.load(gzip.open('mnist.pkl.gz', 'rb'))

training_data, training_labels = th.from_numpy(training_data), th.from_numpy(
    training_labels)
training_data = training_data.view(-1, 1, 28, 28)
training_set = TensorDataset(training_data, training_labels)
training_loader = DataLoader(training_set, args.batch_size)

validation_data, validation_labels = th.from_numpy(
    validation_data), th.from_numpy(validation_labels)
validation_data = validation_data.view(-1, 1, 28, 28)
validation_set = TensorDataset(validation_data, validation_labels)
validation_loader = DataLoader(validation_set, args.batch_size)

model = CNN()
model.cuda()
optimizer = SGD(model.parameters(), lr=1e-2, momentum=0.9)

for epoch in range(args.n_epochs):
    for iteration, batch in enumerate(training_loader):
        data, labels = batch
        data, labels = data.cuda(), labels.cuda()
        n_noises = int(args.noise * labels.size()[0])
        noise = np.random.choice(np.arange(10), n_noises)
        labels[:n_noises] = th.from_numpy(noise)
        data, labels = Variable(data), Variable(labels)
        data = model(data)
        loss = F.nll_loss(F.log_softmax(data), labels)

        optimizer.zero_grad()
Esempio n. 22
0
from imageLoader import imageload
from network import CNN
from time import sleep
import os
import dlib
import json
import sys

# Load the Classifier Network
network = CNN.EmotionClassifier()
network.load_network_state("0.15_lr.npz")
# get our face detector
face_detector = dlib.get_frontal_face_detector()
emotions = [
    "Contentness", "Happiness", "Sadness", "Surprise", "Fear", "Anger",
    "Disgust", "Contempt"
]


def search_in_files(dir_="/in/"):
    for root, name, files in os.walk(os.getcwd() + dir_):
        files = sorted([
            file
            for file in files if file.endswith(".png") or file.endswith(".jpg")
        ],
                       key=lambda x: x[:-4])
        fnames = len(files) * [os.getcwd() + dir_]
        for j, f in enumerate(files):
            fnames[j] += f
        return fnames
    return []
Esempio n. 23
0
def get_network(lr=theano.shared(np.cast['float32'](0.1)), load=False):
    E = CNN.EmotionClassifier(epochs=500, learning_rate=lr)
    if load:
        E.load_network_state()
    return E
Esempio n. 24
0
def create_agent_and_opponent(board_size, win_length, replay_maxlen):
    #network and exp replay
    if not os.path.exists(model_path):
        torch.save(CNN(board_size).to(device).state_dict(), model_path)
    if os.path.exists(experience_path):
        with open(experience_path, "rb") as f:
            exp_replay = pickle.load(f)
    else:
        exp_replay = ExperienceReplay(replay_maxlen)

    #agent
    agent_network = CNN(board_size).to(device)
    agent_network.load_state_dict(torch.load(model_path))
    agent_network.eval()
    agent_mcts = MCTS(board_size, win_length, agent_network)

    #opponent
    opponent_network = CNN(board_size).to(device)
    opponent_network.load_state_dict(torch.load(model_path))
    opponent_network.eval()
    opponent_mcts = MCTS(board_size, win_length, opponent_network)

    return agent_mcts, opponent_mcts, exp_replay
Esempio n. 25
0
def trainModel():
    # Parse args
    parser = argparse.ArgumentParser(description='Train the CNN')
    parser.add_argument('--expt_dir',
                        default='./logs',
                        help='save dir for experiment logs')
    parser.add_argument('--train',
                        default='./data',
                        help='path to training set')
    parser.add_argument('--val',
                        default='./data',
                        help='path to validation set')
    parser.add_argument('--test', default='./data', help='path to test set')
    parser.add_argument('--save_dir',
                        default='./models',
                        help='path to save model')
    parser.add_argument('--arch',
                        default='models/cnn.json',
                        help='path to model architecture')
    parser.add_argument('--model_name',
                        default='model',
                        help='name of the model to save logs, weights')
    parser.add_argument('--lr', default=0.001, help='learning rate')
    parser.add_argument('--init', default='1', help='initialization')
    parser.add_argument('--batch_size', default=20, help='batch_size')
    args = parser.parse_args()

    # Load data
    train_path, valid_path, test_path = args.train, args.val, args.test
    logs_path = args.expt_dir
    model_path, model_arch, model_name = args.save_dir, args.arch, args.model_name
    model_path = os.path.join(model_path, model_name)
    if not os.path.isdir(model_path):
        os.mkdir(model_path)
    lr, batch_size, init = float(args.lr), int(args.batch_size), int(args.init)

    data = loadData(train_path, valid_path, test_path)
    train_X, train_Y, valid_X, valid_Y, test_X, test_Y = data['train']['X'], data['train']['Y'],\
                                                         data['valid']['X'], data['valid']['Y'],\
                                                         data['test']['X'], data['test']['Y'],

    # Load architecture
    arch = loadArch(model_arch)

    # Logging
    train_log_name = '{}.train.log'.format(model_name)
    valid_log_name = '{}.valid.log'.format(model_name)
    train_log = setup_logger('train-log',
                             os.path.join(logs_path, train_log_name))
    valid_log = setup_logger('valid-log',
                             os.path.join(logs_path, valid_log_name))

    # GPU config
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.0)

    # Train
    num_epochs = 100
    num_batches = int(float(train_X.shape[0]) / batch_size)
    steps = 0
    patience = 50
    early_stop = 0

    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as session:
        model = CNN(arch, session, logs_path, init, lr)
        loss_history = [np.inf]
        for epoch in range(num_epochs):
            print 'Epoch {}'.format(epoch)
            steps = 0
            indices = np.arange(train_X.shape[0])
            np.random.shuffle(indices)
            train_X, train_Y = train_X[indices], train_Y[indices]
            for batch in range(num_batches):
                start, end = batch * batch_size, (batch + 1) * batch_size
                x, y = Augment(train_X[range(start,
                                             end)]).batch, train_Y[range(
                                                 start, end)]
                try:
                    model.step(x, y)
                except MemoryError:
                    print 'Memory error in step'
                    exit()
                steps += batch_size
                if steps % train_X.shape[0] == 0 and steps != 0:
                    try:
                        train_loss, train_acc = testModel(
                            model, train_X, train_Y, batch_size)
                    except MemoryError:
                        print 'Memory error in test for train'
                        exit()
                    train_log.info(
                        'Epoch {}, Step {}, Loss: {}, Accuracy: {}, lr: {}'.
                        format(epoch, steps, train_loss, train_acc, model.lr))
                    try:
                        valid_loss, valid_acc = testModel(
                            model, valid_X, valid_Y, batch_size)
                    except MemoryError:
                        print 'Memory error in test for valid'
                        exit()
                    valid_log.info(
                        'Epoch {}, Step {}, Loss: {}, Accuracy: {}, lr: {}'.
                        format(epoch, steps, valid_loss, valid_acc, model.lr))
                    if valid_loss < min(loss_history):
                        save_path = os.path.join(model_path, 'model')
                        model.save(save_path)
                        early_stop = 0
                    early_stop += 1
                    if (early_stop >= patience):
                        print "No improvement in validation loss for " + str(
                            patience) + " steps - stopping training!"
                        print("Optimization Finished!")
                        return 1
                    loss_history.append(valid_loss)
        print("Optimization Finished!")
Esempio n. 26
0
training_data, training_labels = th.from_numpy(training_data), th.from_numpy(
    training_labels)
training_data = training_data.view(-1, 1, 28, 28)
training_labels = th.unsqueeze(training_labels, 1)
training_set = TensorDataset(training_data, training_labels)
training_loader = DataLoader(training_set, args.batch_size)

validation_data, validation_labels = th.from_numpy(
    validation_data), th.from_numpy(validation_labels)
validation_data = validation_data.view(-1, 1, 28, 28)
validation_labels = th.unsqueeze(validation_labels, 1)
validation_set = TensorDataset(validation_data, validation_labels)
validation_loader = DataLoader(validation_set, args.batch_size)

model = CNN()
if cuda:
    model.cuda()
criterion = nn.L1Loss()
optimizer = Adam(model.parameters(), lr=1e-3)

for epoch in range(args.n_epochs):
    for iteration, batch in enumerate(training_loader):
        data, labels = batch
        if cuda:
            data, labels = data.cuda(), labels.cuda()

        noisy_labels = th.zeros(labels.size())
        noisy_labels.copy_(labels)
        n_noises = int(args.noise * labels.size()[0])
        noise = th.from_numpy(np.random.choice(np.arange(1, 10), n_noises))
Esempio n. 27
0
    def evaluation(self):
        '''Evaluates current state (some is already done in __init__)
           Returns evaluated state value'''
        if self.terminate:
            self.v = -self.prev_r
        return self.v


    def backup(self, v):
        '''Backs up tree statistics up to root
           Currently uses mean Q'''
        if self.parent != None:
            n_a = self.parent.children_n_visited[self.prev_a]
            self.parent.children_n_visited[self.prev_a] = n_a + 1
            n = torch.sum(self.parent.children_n_visited) - 1

            old_Q = self.parent.Q[self.prev_a]
            new_Q = (-v+old_Q*n)/(n+1)
            self.parent.Q[self.prev_a] = new_Q
            self.parent.backup(-v)


if __name__=="__main__":
    from network import CNN
    network = CNN().to(device)
    network.eval()
    mcts = MCTS(3, 3, network)
    a = mcts.monte_carlo_tree_search(100, 0.05)
    print(a)
Esempio n. 28
0
def main(model_name, model_version, num_layers, num_units, bottleneck,
         num_filters, filter_size, stride, padding, batch_size, n_epochs,
         learning_rate, moves_path, data_path, continue_train):

    # Grab the training and validation data as a DataLoader
    train_set = PeakDataset(moves_path + '/train_moves.pt',
                            data_path + '/train/%s/train_%d.pt')
    val_set = PeakDataset(moves_path + '/val_moves.pt',
                          data_path + '/val/%s/val_%d.pt')

    # Check if a folder exists for this network version number, if not create it
    folder_path = '../networks/' + str(model_version)
    if not os.path.isdir(folder_path):
        os.makedirs(folder_path)
    elif continue_train:
        pass
    else:
        raise Exception("Model version already exists")

    # Initialize the network
    if model_name == 'linear':
        net = Linear(num_layers=num_layers, num_units=num_units)
        # Save out the hyperparameters
        torch.save(
            {
                'model': model_name,
                'version': model_version,
                'layers': num_layers,
                'units': num_units,
                'batch': batch_size,
                'epoch': n_epochs,
                'lr': learning_rate,
            }, '../networks/' + str(model_version) + '/hparams')

    elif model_name == 'linearskip':
        net = LinearSkip(num_layers=num_layers,
                         num_units=num_units,
                         bottleneck=bottleneck)
        # Save out the hyperparameters
        torch.save(
            {
                'model': model_name,
                'version': model_version,
                'layers': num_layers,
                'units': num_units,
                'batch': batch_size,
                'epoch': n_epochs,
                'lr': learning_rate,
                'bottleneck': bottleneck,
            }, '../networks/' + str(model_version) + '/hparams')

    elif model_name == 'conv':
        net = CNN(num_layers=num_layers,
                  num_filters=num_filters,
                  filter_size=filter_size,
                  stride=stride,
                  pad=padding)
        # Save out the hyperparameters
        torch.save(
            {
                'model': model_name,
                'version': model_version,
                'layers': num_layers,
                'filters': num_filters,
                'size': filter_size,
                'stride': stride,
                'padding': padding,
                'batch': batch_size,
                'epoch': n_epochs,
                'lr': learning_rate,
            }, '../networks/' + str(model_version) + '/hparams')

    # Continue training for a network
    last_epoch = -1
    if continue_train:
        files = os.listdir(folder_path)
        for file in files:
            if file[0:5] == 'model':
                curr_epoch = int(file.split('_')[1])
                if curr_epoch > last_epoch:
                    last_epoch = curr_epoch
        if last_epoch >= 0:
            checkpoint = folder_path + '/model_%d' % last_epoch
            net.load_state_dict(torch.load(checkpoint)['model_state_dict'])

    # Train the network
    train(net,
          batch_size=batch_size,
          n_epochs=n_epochs,
          start_epoch=last_epoch,
          learning_rate=learning_rate,
          train_set=train_set,
          val_set=val_set,
          L2=0,
          model_name=model_name,
          model_version=model_version)
Esempio n. 29
0
    valnum = get_indexNum(config, index, "val")
    testnum = get_indexNum(config, index, "test")

    train_dataset = IQADataset(dataset, config, index, "train")
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               pin_memory=True,
                                               num_workers=0)
    val_dataset = IQADataset(dataset, config, index, "val")
    val_loader = torch.utils.data.DataLoader(val_dataset)

    test_dataset = IQADataset(dataset, config, index, "test")
    test_loader = torch.utils.data.DataLoader(test_dataset)

    model = CNN().to(device)

    criterion = nn.L1Loss()
    optimizer = torch.optim.SGD(model.parameters(),
                                lr=args.lr,
                                weight_decay=args.weight_decay)

    best_SROCC = -1

    for epoch in range(args.epochs):
        #train
        model.train()
        LOSS = 0
        for i, (patches, label) in enumerate(train_loader):
            patches = patches.to(device)
            label = label.to(device)
Esempio n. 30
0
                        help='index of images for visualizing feature')
    parser.add_argument('--save_dir',
                        type=str,
                        default='visual/',
                        help='directory to save visualization results')

    opt = parser.parse_args()
    if not os.path.exists(opt.save_dir):
        os.mkdir(opt.save_dir)
    print('[Info] loading checkpoint from %s ...' %
          os.path.join(opt.ckpt_path, 'ckpt_epoch_%d.pth' % opt.epoch))
    checkpoint = torch.load(
        os.path.join(opt.ckpt_path, 'ckpt_epoch_%d.pth' % opt.epoch))
    configs = checkpoint['configs']
    model = CNN(configs['in_channels'],
                configs['num_class'],
                batch_norm=configs['batch_norm'],
                p=configs['dropout'])
    model.load_state_dict(checkpoint['model_state'])
    model.eval()
    conv_net = torch.nn.ModuleList()
    for name, m in model.named_children():
        if name != 'fc_net':
            conv_net.append(m)
    conv_net = torch.nn.Sequential(*conv_net)
    fc_net = model.fc_net

    if opt.type == 'filter':
        filter_dir = os.path.join(opt.save_dir, 'filter')
        if not os.path.exists(filter_dir):
            os.mkdir(filter_dir)