Пример #1
0
def save_all_feature(fileName, targetName, *suffix):
    # 文件存在
    if os.path.exists(fileName):

        # 加载vgg模型
        vgg_net = torch.load(VGG_FACE_LOCATION)
        vgg_net.modules[31] = nn.View(1, 25088)

        # 读取一级目录
        f = os.listdir(fileName)
        for name in f:
            # 读取二级目录
            images = os.listdir(fileName + os.sep + name)
            for image in images:
                if endwith(image, suffix):  # 后缀名判断
                    # 文件读取,获取特征
                    img = cv.imread(fileName + os.sep + name + os.sep + image)
                    feature = list(get_feature(img, vgg_net))
                    # print(feature)

                    # 创建文件
                    if not os.path.exists(targetName):
                        os.makedirs(targetName)

                    # 保存标签文件
                    with open(targetName + os.sep + 'id.dat', 'a') as f:
                        f.write(name)
                        f.write('\n')

                    # 保存特征文件
                    with open(targetName + os.sep + 'feature.dat', 'a') as f:
                        feature = map(str, feature)
                        f.writelines(s + ' ' for s in feature)
                        f.write('\n')
                    print(name, " successful")
Пример #2
0
def compare_two_img(img1, img2):
    vgg_net = torch.load(VGG_FACE_LOCATION)
    vgg_net.modules[31] = nn.View(1, 25088)
    feature1 = get_feature(img1, vgg_net)
    feature2 = get_feature(img2, vgg_net)
    result = compare(feature1, feature2)
    return result
Пример #3
0
    def test_Parallel(self):
        input = torch.randn(3, 4, 5)
        m = nn.Parallel(0, 2)
        m.add(nn.View(4, 5, 1))
        m.add(nn.View(4, 5, 1))
        m.add(nn.View(4, 5, 1))

        # Check that these don't raise errors
        m.__repr__()
        str(m)

        output = m.forward(input)
        output2 = input.transpose(0, 2).transpose(0, 1)
        self.assertEqual(output2, output)

        gradInput = m.backward(input, output2)
        self.assertEqual(gradInput, input)
Пример #4
0
def findFeatureZeroCount(fileName):
    img = pretreat_img_chinese(fileName)
    vgg_net = torch.load(VGG_FACE_LOCATION)
    vgg_net.modules[31] = nn.View(1, 25088)
    feature = get_feature(img, vgg_net)
    count = 0
    for i in feature:
        if i == 0:
            count += 1
    print(count)
Пример #5
0
def simbreak_threshold_test(filename):
    if os.path.exists(filename):
        images = os.listdir(filename)
        sum_count = len(images)
        test_point = np.arange(0.3, 0.8, 0.1).astype('float32')
        accuracy = np.arange(0.3, 0.8, 0.1).astype('float32')
        vgg_net = torch.load(VGG_FACE_LOCATION)
        print('模型加载成功')
        vgg_net.modules[31] = nn.View(1, 25088)
        feature = loadtxt(FEATURE_LOCATION)
        print('feature加载成功')
        for w in range(test_point.shape[0]):
            right_count = 0
            for image in images:
                num = -1
                sim = -1
                y = os.path.splitext(image)[0]
                img = cv.imread(filename + os.path.sep + image)
                image_feature = get_feature(img, vgg_net)
                # 找到对应的id在第几行
                for i in range(feature.shape[0]):
                    comp = compare(feature[i].reshape(1, -1),
                                   image_feature.reshape(1, -1))
                    if comp > sim:
                        num = i
                        sim = comp
                    if sim >= test_point[w]:
                        break

                # 判断找到的id和正确id是否相同
                if not num == -1:
                    count = 0
                    with open(ID_LOCATION) as f:
                        for line in f.readlines():
                            if num == count:
                                if line[:-1] == y:
                                    right_count += 1
                                    print(test_point[w], ' | ', line[:-1],
                                          ' | ', y, ' right ', sim)
                                    break
                                else:
                                    print(test_point[w], ' | ', line[:-1],
                                          ' | ', y, ' wrong ', sim)
                                    break
                            count += 1
                else:
                    print(test_point[w], ' | ', image, ' | ', y, ' not found')
            print(right_count, ' ', sum_count)
            accuracy[w] = right_count / sum_count
            print(test_point[w], ' | ', accuracy[w])

        for i in range(accuracy.shape[0]):
            print('threshold: ', i + 1, ' | accuracy:', accuracy[i])
Пример #6
0
    def test_ParallelTable(self):
        input = torch.randn(3, 4, 5)
        p = nn.ParallelTable()
        p.add(nn.View(4, 5, 1))
        p.add(nn.View(4, 5, 1))
        p.add(nn.View(4, 5, 1))
        m = nn.Sequential()
        m.add(nn.SplitTable(0))
        m.add(p)
        m.add(nn.JoinTable(2))

        # Check that these don't raise errors
        p.__repr__()
        str(p)

        output = m.forward(input)
        output2 = input.transpose(0, 2).transpose(0, 1)
        self.assertEqual(output2, output)

        gradInput = m.backward(input, output2)
        self.assertEqual(gradInput, input)
Пример #7
0
def save_feature(fileName, targetName, *suffix):
    if os.path.exists(fileName):
        name = os.path.basename(fileName)
        vgg_net = torch.load(VGG_FACE_LOCATION)
        vgg_net.modules[31] = nn.View(1, 25088)
        images = os.listdir(fileName)
        for image in images:
            if endwith(image, suffix):
                img = cv.imread(fileName + os.sep + image)
                feature = list(get_feature(img, vgg_net))
                # print(feature)
                if not os.path.exists(targetName):
                    os.makedirs(targetName)
                with open(targetName + os.sep + 'id.dat', 'a') as f:
                    f.write(name)
                    f.write('\n')
                with open(targetName + os.sep + 'feature.dat', 'a') as f:
                    feature = map(str, feature)
                    f.writelines(s + ' ' for s in feature)
                    f.write('\n')
                print(name, " successfule")
Пример #8
0
) else 'torch.FloatTensor'

# two-dimensional SparseConvNet
model = nn.Sequential()
sparseModel = scn.Sequential()
denseModel = nn.Sequential()
model.add(sparseModel).add(denseModel)
sparseModel.add(scn.ValidConvolution(2, 3, 16, 3, False))
sparseModel.add(scn.MaxPooling(2, 3, 2))
sparseModel.add(
    scn.SparseResNet(
        2, 16,
        [['b', 16, 2, 1], ['b', 32, 2, 2], ['b', 48, 2, 2], ['b', 96, 2, 2]]))
sparseModel.add(scn.Convolution(2, 96, 128, 4, 1, False))
sparseModel.add(scn.BatchNormReLU(128))
sparseModel.add(scn.SparseToDense(2))
denseModel.add(nn.View(-1, 128))
denseModel.add(nn.Linear(128, 3755))
model.type(dtype)
print(model)

spatial_size = sparseModel.suggestInputSize(torch.LongTensor([1, 1]))
print('input spatial size', spatial_size)
dataset = getIterators(spatial_size, 63, 3)
scn.ClassificationTrainValidate(model, dataset, {
    'nEpochs': 100,
    'initial_LR': 0.1,
    'LR_decay': 0.05,
    'weightDecay': 1e-4
})
Пример #9
0
import socket
from numpy import loadtxt
from face_recognition import compare_feature
import torch
from torch.legacy import nn
from flask import Flask, request

VGG_FACE_LOCATION = 'D://vgg.pkl'
FEATURE_LOCATION = 'D:\\feature\\feature.dat'

vgg_net = torch.load(VGG_FACE_LOCATION)
vgg_net.modules[31] = nn.View(1, 25088)
feature = loadtxt(FEATURE_LOCATION)
print('文件加载成功!')

app = Flask(__name__)
app.debug = True

@app.route('/findsim', methods=['GET', 'POST'])
def findsim():
    if request.method == 'POST':
        name = request.form['name']
        name, sim = compare_feature(name, vgg_net, feature)
        result = name + ',' + str(sim[0][0])
        print(result)
        return result
    else:
        return 'method is not right'

@app.route('/hello')
def hello():
Пример #10
0
denseModel = nn.Sequential()
model.add(sparseModel).add(denseModel)
sparseModel.add(scn.ValidConvolution(2, 3, 16, 3, False))\
    .add(scn.SparseDenseNet(2, 16, [
        {'pool': 'MP', 'compression': 0},
        {'nExtraLayers': 2, 'growthRate': 16},
        {'pool': 'BN-R-C-AP', 'compression': 0},
        {'nExtraLayers': 2, 'growthRate': 16},
        {'pool': 'BN-R-C-AP', 'compression': 0},
        {'nExtraLayers': 2, 'growthRate': 16},
        {'pool': 'BN-R-C-AP', 'compression': 0},
        {'nExtraLayers': 2, 'growthRate': 16}]))
n_out = sparseModel.modules[-1].nOutputPlanes
sparseModel.add(scn.Convolution(2, n_out, 256, 4, 1, False))
sparseModel.add(scn.BatchNormReLU(256))
sparseModel.add(scn.SparseToDense(2))
denseModel.add(nn.View(-1, 256))
denseModel.add(nn.Linear(256, 3755))
model.type(dtype)
print(model)

spatial_size = sparseModel.suggestInputSize(torch.LongTensor([1, 1]))
print('input spatial size', spatial_size)
dataset = getIterators(spatial_size, 64, 2)
scn.ClassificationTrainValidate(model, dataset, {
    'nEpochs': 100,
    'initial_LR': 0.1,
    'LR_decay': 0.05,
    'weightDecay': 1e-4
})
Пример #11
0
dtype = 'torch.cuda.FloatTensor'  # Uncomment this to run on GPU

# two-dimensional SparseConvNet
model = nn.Sequential()
sparseModel = scn.Sequential()
denseModel = nn.Sequential()
model.add(sparseModel).add(denseModel)
sparseModel.add(
    scn.SparseVggNet(2, 3, [[
        'C',
        8,
    ], ['C', 8], 'MP', ['C', 16], ['C', 16], 'MP', ['C', 16, 8], ['C', 16, 8],
                            'MP', ['C', 24, 8], ['C', 24, 8], 'MP']))
sparseModel.add(scn.Convolution(2, 32, 64, 5, 1, False))
sparseModel.add(scn.BatchNormReLU(64))
sparseModel.add(scn.SparseToDense(2))
denseModel.add(nn.View(-1, 64))
denseModel.add(nn.Linear(64, 183))
model.type(dtype)
print(model)

spatial_size = sparseModel.suggestInputSize(torch.LongTensor([1, 1]))
print('input spatial size', spatial_size)
dataset = getIterators(spatial_size, 63, 3)
scn.ClassificationTrainValidate(model, dataset, {
    'nEpochs': 100,
    'initial_LR': 0.1,
    'LR_decay': 0.05,
    'weightDecay': 1e-4
})