Exemple #1
0
def saveImg(img_folder):
    test_img_file = 'data/t10k-images.idx3-ubyte'
    test_label_file = 'data/t10k-labels.idx1-ubyte'
    test_img = loadImg(test_img_file)
    test_label = loadLabel(test_label_file)
    label = []
    for i in range(10):
        img = test_img[i][0] * 255
        mpimg.imsave('%s/%d.png' % (img_folder, i), img, format='png')
        label.append(test_label[i])
    return label
Exemple #2
0
    right = 0
    total_loss = 0
    for i in range(len(y)):
        network.img_placeholder.feed(X[i])
        network.label_placeholder.feed(y[i])
        loss, result = network.forword()
        total_loss += loss
        if y[i][result] == 1: right += 1
    print 'Accuracy: %.2f' % (right * 1.0 / len(y))
    print 'Avarage loss: %f' % (total_loss / len(y))


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print 'model folder is needed'
        exit()
    else:
        model_folder = sys.argv[1]

    test_img_file = 'data/t10k-images.idx3-ubyte'
    test_label_file = 'data/t10k-labels.idx1-ubyte'
    config_file = '%s/config.txt' % model_folder

    test_img = loadImg(test_img_file)
    test_label = loadLabel(test_label_file)

    network = NetworkConstructor(test_img[0].shape, test_label[0].shape)
    network.initNetwork(config_file, model_folder)

    test(network, test_img, test_label)
Exemple #3
0
def main(datasetName, n_sample_per_class, run, encoderPath=None):
    # 加载数据和标签
    info = DatasetInfo.info[datasetName]
    data_path = "./data/{}/{}.mat".format(datasetName, datasetName)
    label_path = './trainTestSplit/{}/sample{}_run{}.mat'.format(
        datasetName, n_sample_per_class, run)
    isExists(data_path)
    data = loadmat(data_path)[info['data_key']]
    bands = data.shape[2]
    isExists(label_path)
    trainLabel, testLabel = loadLabel(label_path)
    res = torch.zeros((3, EPOCHS))
    # 数据转换
    data, trainLabel, testLabel = data.astype(np.float32), trainLabel.astype(
        np.int), testLabel.astype(np.int)
    nc = int(np.max(trainLabel))
    trainDataset = HSIDatasetV1(data, trainLabel, patchsz=42)
    testDataset = HSIDatasetV1(data, testLabel, patchsz=42)
    trainLoader = DataLoader(trainDataset,
                             batch_size=BATCHSZ,
                             shuffle=True,
                             num_workers=NUM_WORKERS)
    testLoader = DataLoader(testDataset,
                            batch_size=128,
                            shuffle=True,
                            num_workers=NUM_WORKERS)
    UNITS[0] = bands
    model = SSDL(UNITS, nc)
    model.apply(weight_init)
    # 加载编码器的预训练参数
    if encoderPath is not None:
        isExists(encoderPath)
        raw_dict = torch.load(encoderPath, map_location=torch.device('cpu'))
        target_dict = model.encoder.state_dict()
        keys = list(zip(raw_dict.keys(), target_dict.keys()))
        for raw_key, target_key in keys:
            target_dict[target_key] = raw_dict[raw_key]
        model.encoder.load_state_dict(target_dict)
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=LR)
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.8)

    for epoch in range(EPOCHS):
        print('*' * 5 + 'Epoch:{}'.format(epoch) + '*' * 5)
        model, trainLoss = train(model,
                                 criterion=criterion,
                                 optimizer=optimizer,
                                 dataLoader=trainLoader)
        acc, evalLoss = test(model, criterion=criterion, dataLoader=testLoader)
        print('epoch:{} trainLoss:{:.8f} evalLoss:{:.8f} acc:{:.4f}'.format(
            epoch, trainLoss, evalLoss, acc))
        print('*' * 18)
        res[0][epoch], res[1][epoch], res[2][epoch] = trainLoss, evalLoss, acc
        if epoch % 5 == 0:
            torch.save(
                model.state_dict(),
                os.path.join(
                    ROOT, 'ssdl_sample{}_run{}_epoch{}.pkl'.format(
                        n_sample_per_class, run, epoch)))
        scheduler.step()
    tmp = res.numpy()
    savemat(os.path.join(ROOT, 'res.mat'), {
        'trainLoss': tmp[0],
        'evalLoss': tmp[1],
        'acc': tmp[2]
    })
    return res
Exemple #4
0
        network.label_placeholder.feed(y[i])
        loss, result = network.forword()
        if result == True: right += 1
    print 'Accuracy: %.2f' % (right * 1.0 / len(y))


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print 'model folder is needed'
        exit()
    else:
        model_folder = sys.argv[1]
        if len(sys.argv) > 2:
            batch_size = int(sys.argv[2])
            init_learning_rate = float(sys.argv[3])
            max_epoch = int(sys.argv[4])

    train_img_file = 'data/train-images.idx3-ubyte'
    train_label_file = 'data/train-labels.idx1-ubyte'
    test_img_file = 'data/t10k-images.idx3-ubyte'
    test_label_file = 'data/t10k-labels.idx1-ubyte'
    config_file = '%s/config.txt' % model_folder

    train_img = loadImg(train_img_file)
    train_label = loadLabel(train_label_file)

    network = NetworkConstructor(train_img[0].shape, train_label[0].shape)
    network.initNetwork(config_file, model_folder)

    train(network, train_img, train_label, model_folder)
Exemple #5
0
def main(datasetName, n_sample_per_class, run, preTrainedModel=None):
    # 加载数据和标签
    info = DatasetInfo.info[datasetName]
    data_path = "./data/{}/{}.mat".format(datasetName, datasetName)
    label_path = './trainTestSplit/{}/sample{}_run{}.mat'.format(
        datasetName, n_sample_per_class, run)
    isExists(data_path)
    data = loadmat(data_path)[info['data_key']]
    bands = data.shape[2]
    isExists(label_path)
    trainLabel, testLabel = loadLabel(label_path)
    res = torch.zeros((3, EPOCHS))
    # 数据转换
    data, trainLabel, testLabel = data.astype(np.float32), trainLabel.astype(
        np.int), testLabel.astype(np.int)
    nc = int(np.max(trainLabel))
    trainDataset = HSIDatasetV1(data, trainLabel, patchsz=5, n_components=4)
    testDataset = HSIDatasetV1(data, testLabel, patchsz=5, n_components=4)
    trainLoader = DataLoader(trainDataset,
                             batch_size=BATCHSZ,
                             shuffle=True,
                             num_workers=NUM_WORKERS)
    testLoader = DataLoader(testDataset,
                            batch_size=256,
                            shuffle=True,
                            num_workers=NUM_WORKERS)
    # 定义编码模型
    encoder = Encoder(info['band'] + 5 * 5 * 4, info['hidden_size'])
    # 读取预训练模型参数
    assert isExists(preTrainedModel), '预训练模型路径不存在'
    preTrainedModelDict = torch.load(preTrainedModel,
                                     map_location=torch.device('cpu'))
    # 移除b_decoder预训练参数
    preTrainedModelDict.pop('b_decoder')
    # 加载预训练参数
    encoder.load_state_dict(preTrainedModelDict)
    # 定义逻辑回归模型
    classifier = nn.Linear(info['hidden_size'], nc)
    # 整体
    model = nn.Sequential(encoder, classifier)
    # 损失函数,优化器,学习率下架管理器
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=LR)
    scheduler = optim.lr_scheduler.StepLR(optimizer,
                                          step_size=EPOCHS // 2,
                                          gamma=0.1)

    for epoch in range(EPOCHS):
        print('*' * 5 + 'Epoch:{}'.format(epoch) + '*' * 5)
        model, trainLoss = train(model,
                                 criterion=criterion,
                                 optimizer=optimizer,
                                 dataLoader=trainLoader,
                                 device=DEVICE)
        acc, evalLoss = test(model,
                             criterion=criterion,
                             dataLoader=testLoader,
                             device=DEVICE)
        print('epoch:{} trainLoss:{:.8f} evalLoss:{:.8f} acc:{:.4f}'.format(
            epoch, trainLoss, evalLoss, acc))
        print('*' * 18)
        res[0][epoch], res[1][epoch], res[2][epoch] = trainLoss, evalLoss, acc
        if epoch % 5 == 0:
            torch.save(
                model.state_dict(),
                os.path.join(
                    ROOT, '{}_sample{}_run{}_epoch{}.pkl'.format(
                        MODEL_NAME, n_sample_per_class, run, epoch)))
        scheduler.step()
    tmp = res.numpy()
    savemat(os.path.join(ROOT, 'res.mat'), {
        'trainLoss': tmp[0],
        'evalLoss': tmp[1],
        'acc': tmp[2]
    })
    return res
Exemple #6
0
def main(datasetName, n_sample_per_class, run, encoderPath=None):
    # 加载数据和标签
    info = DatasetInfo.info[datasetName]
    data_path = "./data/{}/{}.mat".format(datasetName, datasetName)
    label_path = './trainTestSplit/{}/sample{}_run{}.mat'.format(
        datasetName, n_sample_per_class, run)
    isExists(data_path)
    data = loadmat(data_path)[info['data_key']]
    bands = data.shape[2]
    isExists(label_path)
    trainLabel, testLabel = loadLabel(label_path)
    # 数据转换
    data, trainLabel, testLabel = data.astype(np.float32), trainLabel.astype(
        np.int), testLabel.astype(np.int)
    nc = int(np.max(trainLabel))
    trainDataset = HSIDatasetV1(data, trainLabel, patchsz=42)
    testDataset = HSIDatasetV1(data, testLabel, patchsz=42)
    trainLoader = DataLoader(trainDataset,
                             batch_size=BATCHSZ,
                             shuffle=True,
                             num_workers=NUM_WORKERS)
    testLoader = DataLoader(testDataset,
                            batch_size=BATCHSZ,
                            shuffle=True,
                            num_workers=NUM_WORKERS)
    UNITS[0] = bands
    model = SSDL(UNITS, nc)
    model.apply(weight_init)
    # 加载编码器的预训练参数
    if encoderPath is not None:
        isExists(encoderPath)
        raw_dict = torch.load(encoderPath, map_location=torch.device('cpu'))
        target_dict = model.encoder.state_dict()
        keys = list(zip(raw_dict.keys(), target_dict.keys()))
        for raw_key, target_key in keys:
            target_dict[target_key] = raw_dict[raw_key]
        model.encoder.load_state_dict(target_dict)
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=LR, weight_decay=1e-2)
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.8)

    for epoch in range(EPOCHS):
        print('*' * 5 + 'Epoch:{}'.format(epoch) + '*' * 5)
        model, trainLoss = train(model,
                                 criterion=criterion,
                                 optimizer=optimizer,
                                 dataLoader=trainLoader)
        acc, evalLoss = test(model, criterion=criterion, dataLoader=testLoader)
        viz.line([[trainLoss, evalLoss]], [epoch],
                 win='train&test loss',
                 update='append')
        viz.line([acc], [epoch], win='accuracy', update='append')
        print('epoch:{} trainLoss:{:.8f} evalLoss:{:.8f} acc:{:.4f}'.format(
            epoch, trainLoss, evalLoss, acc))
        print('*' * 18)
        # if os.path.isdir('./ssdl/{}/ssdl_sample{}_run{}_epoch{}'.format(datasetName, n_sample_per_class, run, epoch))
        if not os.path.isdir('./ssdl/{}'.format(datasetName)):
            os.makedirs('./ssdl/{}'.format(datasetName))
        torch.save(
            model.state_dict(),
            './ssdl/{}/ssdl_sample{}_run{}_epoch{}.pkl'.format(
                datasetName, n_sample_per_class, run, epoch))
        scheduler.step()