Beispiel #1
0
def test_model_load(_model_path, _n_class):
    _model = net_sphere.sphere20a(feature=True)
    in_feature = _model.fc6.in_features
    _model.fc6 = net_sphere.AngleLinear(in_feature, _n_class)

    state_dict = torch.load(_model_path)
    _model.load_state_dict(state_dict)

    return _model
Beispiel #2
0
    def prepare_sphereface(self, pretrained_params_file, use_gpu=True):
        f_model = sphere20a(feature=True)
        f_model.load_state_dict(torch.load(pretrained_params_file))
        # don't want to update params in pretrained model
        for param in f_model.parameters():
            param.requires_grad = False

        self.model['F'] = f_model
        if use_gpu:
            self.model['F'] = self.model['F'].cuda()
Beispiel #3
0
def get_net(model_path, class_num):
    net = sphere20a()
    net.load_state_dict(torch.load(model_path))
    net.fc6 = AngleLinear(512, class_num)

    net.classnum = class_num

    for name, param in net.named_parameters():
        param.requires_grad = name.startswith('conv4') or name.startswith(
            'relu4') or name.startswith('fc')  # or name.startswith('stn')

    return net
Beispiel #4
0
def set_fine_tune_model(_model_path, _n_class):
    _model = net_sphere.sphere20a()
    state_dict = torch.load(_model_path)
    _model.load_state_dict(state_dict)
    in_feature = _model.fc6.in_features
    _model.fc6 = net_sphere.AngleLinear(in_feature, _n_class)

    for i, param in enumerate(_model.parameters()):
        if i < 6:
            param.requires_grad = False

    return _model
Beispiel #5
0
def get_predicts(dataset_path, model_path, class_num=227, folds_iter=unresticted_fold_iter, cached=False):
    if cached:  # false
        model_name = os.path.splitext(os.path.split(model_path)[1])[0]
        feats_path = os.path.join(dataset_path, model_name+'.pkl')
        if os.path.exists(feats_path):
            predicts, folds_length = pickle.load(open(feats_path, 'rb'))
            return predicts, folds_length
    
    predicts=[]
    net = sphere20a(classnum=class_num)
    net.load_state_dict(torch.load(model_path))
    net.cuda()
    net.eval()
    net.feature = True

    transform = transforms.Compose([
        transforms.Resize((112, 96)),
    ])

    folds_length = []
    for name1, name2, sameflag in folds_iter(dataset_path, folds_length):
        img1 = os.path.join(dataset_path, 'OriginalImages', name1+'.jpg')
        # landmark1 = load_landmark(os.path.join(dataset_path, 'FacialPoints', name1+'.txt'))
        # img1 = alignment(cv2.imread(img1, 1), landmark1)
        img1 = np.array(transform(Image.open(img1).convert('RGB')))

        img2 = os.path.join(dataset_path, 'OriginalImages', name2+'.jpg')
        # landmark2 = load_landmark(os.path.join(dataset_path, 'FacialPoints', name2+'.txt'))
        # img2 = alignment(cv2.imread(img2, 1), landmark2)  # 112*96*3
        img2 = np.array(transform(Image.open(img2).convert('RGB')))

        imglist = [img1, cv2.flip(img1, 1), img2, cv2.flip(img2, 1)]  # cv2.flip(img1, 1):112*96*3
        for i in range(len(imglist)):
            imglist[i] = imglist[i].transpose(2, 0, 1).reshape((1, 3, 112, 96))
            imglist[i] = (imglist[i] - 127.5) / 128.

        img = np.vstack(imglist)  # 4*3*112*96  (垂直将imglist中的四组数据合起来)
        with torch.no_grad():
            img = Variable(torch.from_numpy(img).float(), volatile=True).cuda()
            output = net(img)  # 4*512
        f = output.data
        f1, f2 = f[0], f[2]  # img1 and img2 output
        cosdistance = f1.dot(f2) / (f1.norm() * f2.norm() + 1e-5)
        predicts.append((cosdistance, sameflag))
    predicts = np.array(predicts)
    if cached:
        pickle.dump((predicts, folds_length), open(feats_path, 'wb'))
    return predicts, folds_length
Beispiel #6
0
    def __init__(self, gpuid):
        self.frnet = net_sphere.sphere20a()
        self.frnet.load_state_dict(torch.load('model/sphere20a_20171020.pth'))
        self.frnet.cuda()
        self.frnet.eval()
        self.frnet.feature = True
        self.fcp = feature_comparer(512, 0.8)

        os.environ["CUDA_VISIBLE_DEVICES"] = str(gpuid)

        self.net, self.names = libpysunergy.load(
            "data/face.data", "cfg/yolo_face_547.cfg",
            "weights/yolo_face_547.weights", gpuid)
        self.net2, self.names2 = libpysunergy.load("data/age1.1.data",
                                                   "cfg/age1.1.cfg",
                                                   "weights/age1.1.weights",
                                                   gpuid)
        self.net3, self.names3 = libpysunergy.load(
            "data/gender1.1.data", "cfg/gender1.1.cfg",
            "weights/gender1.1.weights", gpuid)

        self.top = 1
# In[3]:

from torch.autograd import Variable
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torchvision.utils as vutils
from Models import Generator, Discriminator
from VGG import VGG16Feature

# In[4]:

netG = Generator()
feature_net = net_sphere.sphere20a(feature=True)

criterion = nn.MSELoss()

input = torch.FloatTensor(batchSize, 3, imageSize, imageSize)
noise = torch.FloatTensor(batchSize, nz, 1, 1)
feature = torch.FloatTensor(batchSize, 512, 7, 7)
fixed_noise = torch.FloatTensor(batchSize, nz, 1, 1).normal_(0, 1)

label_real = torch.FloatTensor(batchSize)
label_real_smooth = torch.FloatTensor(batchSize)
label_fake = torch.FloatTensor(batchSize)

netG.cuda()
criterion.cuda()
feature_net.cuda()
Beispiel #8
0
def main():
    torch.manual_seed(args.seed)
    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
    use_gpu = torch.cuda.is_available()
    if args.use_cpu: use_gpu = False


    if use_gpu:
        print("Currently using GPU: {}".format(args.gpu))
        cudnn.benchmark = True
        torch.cuda.manual_seed_all(args.seed)
    else:
        print("Currently using CPU")

    print("Creating dataset: {}".format(args.dataset))


    trans = transforms.Compose([transforms.Resize((200, 200)), transforms.ToTensor(), transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])

    train_set = dataset.UTK_DS(TRAIN_LAND_PATH,TRAIN_ROOT_PATH, args.label, trans) 

    test_set = dataset.UTK_DS(TEST_LAND_PATH,TEST_ROOT_PATH, args.label, trans)

    trainloader = torch.utils.data.DataLoader(train_set, batch_size = args.bs, shuffle = True,  num_workers = args.workers, drop_last = True)

    testloader = torch.utils.data.DataLoader(test_set, batch_size = args.bs, shuffle = True,  num_workers = args.workers, drop_last = True)

    print("Creating model: {}".format(args.model))
    if args.model == 'sphere20a':
        model = net_sphere.sphere20a(classnum = num_class)   
    else:
        model = net_sphere.sphere64a(classnum = num_class)  

    if use_gpu:
        model = nn.DataParallel(model).cuda()

    if os.path.isfile(model_PATH):
        print("\nLoading the lastest model\n")
        model.load_state_dict(torch.load(model_PATH))
        model.eval()

    criterion = net_sphere.AngleLoss()

    lr = args.lr


    start_time = time.time()

    for epoch in range(args.max_epoch):
        if epoch > 0 and epoch % == 0:
            lr = lr * 0.1
        optimizer = torch.optim.SGD(model.parameters(), lr = lr, momentum = 0.5, weight_decay = 5e-4)


        print("==> Epoch {}/{}".format(epoch+1, args.max_epoch))
        train(model, criterion,
              optimizer, trainloader, use_gpu, num_class, epoch)

        
        print("==> Test")
        acc, err = test(model, testloader, use_gpu, num_class , epoch)
        print("Accuracy (%): {}\t Error rate (%): {}".format(acc, err))

    elapsed = round(time.time() - start_time)
    elapsed = str(datetime.timedelta(seconds=elapsed))
    print("Finished. Total elapsed time (h:m:s): {}".format(elapsed))
    torch.save(model.state_dict(), model_PATH)
from torch.autograd import Variable
torch.backends.cudnn.bencmark = True

import os,sys,cv2,random,datetime
import argparse
import numpy as np
import zipfile

from dataset import ImageDataset
from net_sphere import sphere20a #Uses the spherenet neural architecture
import imp
import torchvision
import foolbox

print(np.version.version)
net=sphere20a()
net.load_state_dict(torch.load('model/sphere20a_20171020.pth'))
#net.cuda()
net.eval()
net.feature = True

model=foolbox.models.PyTorchModel(net, (0,1), 512, preprocessing=(0, 1))

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
IMAGES="/home/konstantinoskk/Documents/Bench/test/"
img=mpimg.imread(IMAGES+'Alexa_Loren_0001.jpg')

import torchvision.transforms as transforms
resize1=transforms.Resize((112,96))
randcrop=transforms.RandomCrop((112,96))
Beispiel #10
0
    negScore = negScore["0"] <= threshold
    posScore = posScore["0"] > threshold

    acc = (negScore.sum()+posScore.sum())/(len(negScore)+len(posScore))

    print("lfw acc:", acc)


if __name__ == '__main__':

    posPairsTxT = "data/LFW/postive_pairs.txt"
    negPairsTxT = "data/LFW/negative_pairs.txt"
    filenameTxT = "data/LFW/Path_lfw2.txt"
    rootPath = "data/LFW/lfw-deepfunneled/lfw-deepfunneled"

    modelPath = "model_file/sphere20a_20171020.pth"
    modelName = modelPath.replace('.', '/').split('/')[1]
    print("model:", modelName)

    net = sphere20a(10574)
    net.load_state_dict(th.load(modelPath))
    net = net.cuda()
    net = net.eval()

    example(net, imgSize=(112, 96))
    runLFWPosPairs(net, modelName, imgSize=(112, 96), posPairsTxT=posPairsTxT, filenameTxT=filenameTxT, rootPath=rootPath)
    runLFWNegPairs(net, modelName, imgSize=(112, 96), negPairsTxT=negPairsTxT, filenameTxT=filenameTxT, rootPath=rootPath)
    plotSimliarityHist(modelName)

    threshold = 0.58
    LFWAccScore(modelName, threshold)
Beispiel #11
0
types = ['bb', 'ss', 'sibs', 'fd', 'fs', 'md', 'ms']

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='FIW Sphereface Baseline')
    parser.add_argument('--type', '-t', default='bb', type=str, help='relationship type (None processes entire directory)')
    parser.add_argument('--batch_size', default=32, type=int, help='training batch size')
    parser.add_argument('--modelpath', default='finetuned/checkpoint.pth.tar', type=str,
                        help='the pretrained model to point to')
    parser.add_argument('--label_dir', '-l', type=str, default=sys_home() + '/datasets/FIW/RFIW/val/pairs/',
                            help='Root directory of data (assumed to containing pairs list labels)')
    parser.add_argument('--data_dir', '-d', type=str, default=sys_home() + '/datasets/FIW/RFIW/val/',
                        help='Root directory of data (assumed to contain valdata)')

    args = parser.parse_args()

    net = net_sphere.sphere20a(classnum=300)

    if cuda:
        net.cuda()

    epoch, bess_acc = TorchTools.load_checkpoint(net, f_weights=args.modelpath)

    ncols = int(np.ceil(len(do_types) / 2))
    nrows = 2
    f, axes = plt.subplots(nrows, ncols, sharex='all', sharey='all')

    for i, id in enumerate(do_types):
        if i < ncols:
            ax = axes[0, i]
        else:
            ax = axes[1, i - ncols]