def load_model(self):
        if self.use_dense:
            model_structure = ft_net_dense(self.nclasses)
        elif self.use_NAS:
            model_structure = ft_net_NAS(self.nclasses)
        else:
            model_structure = ft_net(self.nclasses, stride=self.stride)

        if self.PCB:
            model_structure = PCB(self.nclasses)

        #if self.fp16:
        #    model_structure = network_to_half(model_structure)

        self.model = self.load_network(model_structure)

        # Remove the final fc layer and classifier layer
        if self.PCB:
            #if self.fp16:
            #    model = PCB_test(model[1])
            #else:
            self.model = PCB_test(self.model)
        else:
            #if self.fp16:
            #model[1].model.fc = nn.Sequential()
            #model[1].classifier = nn.Sequential()
            #else:
            self.model.classifier.classifier = nn.Sequential()

        # Change to test mode
        self.model = self.model.eval()
        if self.use_gpu:
            self.model = self.model.cuda()
예제 #2
0
def load_network(name, opt):
    # Load config
    dirname = os.path.join('./model', name)
    last_model_name = os.path.basename(get_model_list(dirname, 'net'))

    epoch = last_model_name.split('_')[1]
    epoch = epoch.split('.')[0]
    if not epoch == 'last':
        epoch = int(epoch)
    config_path = os.path.join(dirname, 'opts.yaml')
    with open(config_path, 'r') as stream:
        config = yaml.load(stream)

    opt.name = config['name']
    opt.data_dir = config['data_dir']
    opt.train_all = config['train_all']
    opt.droprate = config['droprate']
    opt.color_jitter = config['color_jitter']
    opt.batchsize = config['batchsize']
    opt.h = config['h']
    opt.w = config['w']
    opt.stride = config['stride']
    if 'pool' in config:
        opt.pool = config['pool']
    if 'h' in config:
        opt.h = config['h']
        opt.w = config['w']
    if 'gpu_ids' in config:
        opt.gpu_ids = config['gpu_ids']
    opt.erasing_p = config['erasing_p']
    opt.lr = config['lr']
    opt.nclasses = config['nclasses']
    opt.erasing_p = config['erasing_p']
    opt.use_dense = config['use_dense']
    opt.PCB = config['PCB']
    opt.fp16 = config['fp16']

    if opt.use_dense:
        model = ft_net_dense(opt.nclasses, opt.droprate, opt.stride, None,
                             opt.pool)
    else:
        model = ft_net(opt.nclasses, opt.droprate, opt.stride, None, opt.pool)
    if opt.PCB:
        model = PCB(opt.nclasses)

    # load model
    if isinstance(epoch, int):
        save_filename = 'net_%03d.pth' % epoch
    else:
        save_filename = 'net_%s.pth' % epoch

    save_path = os.path.join('./model', name, save_filename)
    print('Load the model from %s' % save_path)
    network = model
    network.load_state_dict(torch.load(save_path))
    return network, opt, epoch
예제 #3
0
def get_model(class_names_size):
    if opt.use_dense:
        model = ft_net_dense(class_names_size, opt.droprate)
    elif opt.use_NAS:
        model = ft_net_NAS(class_names_size, opt.droprate)
    else:
        model = ft_net(class_names_size, opt.droprate, opt.stride)
    if opt.PCB:
        model = PCB(class_names_size)
    return model
예제 #4
0
def cal_camfeatures(src_path='data/market/pytorch/train_all',
                    dst_path='data/market/pytorch'):
    print('-------cal cam features-----------')
    if opt.use_dense:
        model_structure = ft_net_dense(751, istrain=False)
    else:
        model_structure = ft_net(751)
    model = load_network_easy(model_structure)

    # Remove the final fc layer and classifier layer
    model.model.fc = nn.Sequential()
    model.classifier = nn.Sequential()
    model.model2.fc = nn.Sequential()
    model.classifier2 = nn.Sequential()
    model.fc = nn.Sequential()
    model.classifier3 = nn.Sequential()
    model.fc3 = nn.Sequential()
    model.classifier4 = nn.Sequential()

    # Change to test mode
    model = model.eval()
    if use_gpu:
        model = model.cuda()

    files = glob.glob(src_path + '/*/*.jpg')
    print(len(files))
    cam_feature = np.zeros((6, 1024))
    cam_cnt = np.zeros((6, ))
    cnt = 0
    for file in files:
        feature, label = extract_one_feature(file, model)
        cam_feature[label] += feature
        cam_cnt[label] += 1
        if (cnt + 1) % 1000 == 0:
            print('cnt = %d' % (cnt + 1))
            # break
        cnt += 1

    for i in range(6):
        cam_feature[i] /= cam_cnt[i]
        print('cam_cnt_%d = %4d  cam_feature_%d = %s' %
              (i, cam_cnt[i], i, cam_feature[i]))
        print('sum = %.3f' % (np.sum(cam_feature[i])))
        print('max = %.5f  index = %4d' %
              (np.max(cam_feature[i]), np.argmax(cam_feature[i])))
        print('min = %.5f  index = %4d' %
              (np.min(cam_feature[i]), np.argmin(cam_feature[i])))

    np.save(os.path.join(dst_path, 'cam_features_no_norm1.npy'), cam_feature)
    f = np.load(os.path.join(dst_path, 'cam_features_no_norm1.npy'))
    print('cam features:')
    print(f)
예제 #5
0
def loadModel(modelName, config, stateDictPath):
    nclasses = config["nclasses"]
    if modelName == "densenet121":
        model_structure = ft_net_dense(nclasses)
    elif modelName == "NAS":
        model_structure = ft_net_NAS(nclasses)
    elif modelName == "ResNet50":
        model_structure = ft_net(nclasses, stride=config["stride"])
    elif modelName == "PCB":
        model_structure = PCB(nclasses)
    else:
        raise KeyError("Undefined modelName, %s" % modelName)

    return loadNetwork(model_structure, stateDictPath)
예제 #6
0
    def __init__(self, model_name='fp16'):
        '''
        model_name in ['fp16', 'ft_net_dense', 'ft_ResNet50']
        '''
        if model_name not in ['fp16', 'ft_net_dense', 'ft_ResNet50']:
            raise ValueError('ReID model name not correct')

        model_path = os.path.join('ReID/models', model_name)
        sys.path.append(model_path)
        import model

        if model_name == 'fp16':
            self.net = model.ft_net(751, stride=1)
        elif model_name == 'ft_net_dense':
            self.net = model.ft_net_dense(751, stride=1)
        elif model_name == 'ft_ResNet50':
            self.net = model.ft_net_middle(751, stride=1)

        pretrain_path = os.path.join('ReID/models', model_name, 'net_last.pth')
        self.net.load_state_dict(torch.load(pretrain_path))
        self.net.eval()

        self.net.classifier = nn.Sequential(
        )  # Remove the classifier layer in reference situation
예제 #7
0
파일: train_test.py 프로젝트: amena6490/PCB
def main():
    print("==========\nArgs:{}\n==========".format(opt))
    train_data_dir = opt.train_data_dir
    test_data_dir = opt.test_data_dir
    name = opt.name
    os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu_ids
    use_gpu = torch.cuda.is_available()
    if opt.use_cpu: use_gpu = False

    if not opt.evaluate:
        sys.stdout = Logger(osp.join(opt.save_dir, opt.train_log))
    else:
        sys.stdout = Logger(osp.join(opt.save_dir, opt.test_log))

    if use_gpu:
        print("Currently using GPU {}".format(opt.gpu_ids))
        cudnn.benchmark = True
    else:
        print("Currently using CPU (GPU is highly recommended)")
    #str_ids = opt.gpu_ids.split(',')
    #gpu_ids = []
    #for str_id in str_ids:
    #    gid = int(str_id)
    #    if gid >=0:
    #        gpu_ids.append(gid)
    #
    ## set gpu ids
    #if len(gpu_ids)>0:
    #    torch.cuda.set_device(gpu_ids[0])
    #print(gpu_ids[0])

    # Load train Data
    # ---------
    print("==========Preparing trian dataset========")
    transform_train_list = [
        # transforms.RandomResizedCrop(size=128, scale=(0.75,1.0), ratio=(0.75,1.3333), interpolation=3), #Image.BICUBIC)
        transforms.Resize((288, 144), interpolation=3),
        transforms.RandomCrop((256, 128)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]
    if opt.PCB:
        transform_train_list = [
            transforms.Resize((384, 192), interpolation=3),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]
    if opt.erasing_p > 0:
        transform_train_list = transform_train_list + [
            RandomErasing(probability=opt.erasing_p, mean=[0.0, 0.0, 0.0])
        ]

    if opt.color_jitter:
        transform_train_list = [
            transforms.ColorJitter(
                brightness=0.1, contrast=0.1, saturation=0.1, hue=0)
        ] + transform_train_list

    train_data_transforms = transforms.Compose(transform_train_list)
    train_all = ''
    if opt.train_all:
        if opt.use_clean_imgs:
            train_all = '_all_clean'
        else:
            train_all = '_all'
            print("Using all the train images")

    train_image_datasets = {}
    train_image_datasets['train'] = datasets.ImageFolder(
        os.path.join(train_data_dir, 'train' + train_all),
        train_data_transforms)
    train_dataloaders = {
        x: torch.utils.data.DataLoader(train_image_datasets[x],
                                       batch_size=opt.train_batch,
                                       shuffle=True,
                                       num_workers=4)
        for x in ['train']
    }
    dataset_sizes = {x: len(train_image_datasets[x]) for x in ['train']}
    class_names = train_image_datasets['train'].classes
    inputs, classes = next(iter(train_dataloaders['train']))

    ######################################################################
    # Prepare test data
    if not opt.train_only:
        print("========Preparing test dataset========")
        transform_test_list = transforms.Compose([
            transforms.Resize((384, 192), interpolation=3),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ])
        test_image_datasets = {
            x: datasets.ImageFolder(os.path.join(test_data_dir, x),
                                    transform_test_list)
            for x in ['gallery', 'query']
        }
        test_dataloaders = {
            x: torch.utils.data.DataLoader(test_image_datasets[x],
                                           batch_size=opt.test_batch,
                                           shuffle=False,
                                           num_workers=4)
            for x in ['gallery', 'query']
        }

    print("Initializing model...")
    if opt.use_dense:
        model = ft_net_dense(len(class_names))
    else:
        model = ft_net(len(class_names))
    if opt.PCB:
        model = PCB(len(class_names))
    print("Model size: {:.5f}M".format(
        sum(p.numel() for p in model.parameters()) / 1000000.0))
    start_epoch = opt.start_epoch

    if opt.resume:
        print("Loading checkpoint from '{}'".format(opt.resume))
        checkpoint = torch.load(opt.resume)
        model.load_state_dict(checkpoint['state_dict'])
        # model.load_state_dict(checkpoint)
        start_epoch = checkpoint['epoch']
    if use_gpu:
        model = nn.DataParallel(model).cuda()
    if opt.evaluate:
        print("Evaluate only")
        test(model, test_image_datasets, test_dataloaders, use_gpu)
        return
    criterion = nn.CrossEntropyLoss().cuda()
    if opt.PCB:
        ignored_params = list(map(id, model.module.resnet50.fc.parameters()))
        ignored_params += (
            list(map(id, model.module.classifier0.parameters())) +
            list(map(id, model.module.classifier1.parameters())) +
            list(map(id, model.module.classifier2.parameters())) +
            list(map(id, model.module.classifier3.parameters())) +
            list(map(id, model.module.classifier4.parameters())) +
            list(map(id, model.module.classifier5.parameters()))
            #+list(map(id, model.classifier7.parameters() ))
        )
        base_params = filter(lambda p: id(p) not in ignored_params,
                             model.parameters())
        optimizer = optim.SGD(
            [
                {
                    'params': base_params,
                    'lr': 0.01
                },
                {
                    'params': model.module.resnet50.fc.parameters(),
                    'lr': 0.1
                },
                {
                    'params': model.module.classifier0.parameters(),
                    'lr': 0.1
                },
                {
                    'params': model.module.classifier1.parameters(),
                    'lr': 0.1
                },
                {
                    'params': model.module.classifier2.parameters(),
                    'lr': 0.1
                },
                {
                    'params': model.module.classifier3.parameters(),
                    'lr': 0.1
                },
                {
                    'params': model.module.classifier4.parameters(),
                    'lr': 0.1
                },
                {
                    'params': model.module.classifier5.parameters(),
                    'lr': 0.1
                },
                #{'params': model.classifier7.parameters(), 'lr': 0.01}
            ],
            weight_decay=5e-4,
            momentum=0.9,
            nesterov=True)
    else:
        ignored_params = list(map(
            id, model.module.model.fc.parameters())) + list(
                map(id, model.module.classifier.parameters()))
        base_params = filter(lambda p: id(p) not in ignored_params,
                             model.parameters())
        optimizer = optim.SGD([{
            'params': base_params,
            'lr': 0.01
        }, {
            'params': model.module.model.fc.parameters(),
            'lr': 0.1
        }, {
            'params': model.module.classifier.parameters(),
            'lr': 0.1
        }],
                              weight_decay=5e-4,
                              momentum=0.9,
                              nesterov=True)
    # Decay LR by a factor of 0.1 every 40 epochs
    if opt.stepsize > 0:
        scheduler = lr_scheduler.StepLR(optimizer,
                                        step_size=opt.stepsize,
                                        gamma=0.1)

    start_time = time.time()
    train_time = 0
    best_rank1 = -np.inf
    best_epoch = 0
    print("==> Start training")
    ######################################################################
    # Training the model
    # ------------------
    #
    # Now, let's write a general function to train a model. Here, we will
    # illustrate:
    #
    # -  Scheduling the learning rate
    # -  Saving the best model
    #
    # In the following, parameter ``scheduler`` is an LR scheduler object from
    # ``torch.optim.lr_scheduler``.

    for epoch in range(start_epoch, opt.max_epoch):
        start_train_time = time.time()
        if opt.train_only:
            print("==> Training only")
            train(epoch, model, criterion, optimizer, train_dataloaders,
                  use_gpu)
            train_time += round(time.time() - start_train_time)
            if epoch % opt.eval_step == 0:
                if use_gpu:
                    state_dict = model.module.state_dict()
                else:
                    state_dict = model.state_dict()
                save_checkpoint(
                    {
                        'state_dict': state_dict,
                        'epoch': epoch,
                    }, 0,
                    osp.join(opt.save_dir,
                             'checkpoint_ep' + str(epoch + 1) + '.pth.tar'))

            if opt.stepsize > 0: scheduler.step()
        else:
            train(epoch, model, criterion, optimizer, train_dataloaders,
                  use_gpu)
            train_time += round(time.time() - start_train_time)

            if opt.stepsize > 0: scheduler.step()
            if (epoch + 1) > opt.start_eval and opt.eval_step > 0 and (
                    epoch + 1) % opt.eval_step == 0 or (epoch +
                                                        1) == opt.max_epoch:
                print("==> Test")
                rank1 = test(model, test_image_datasets, test_dataloaders,
                             use_gpu)
                is_best = rank1 > best_rank1
                if is_best:
                    best_rank1 = rank1
                    best_epoch = epoch + 1
                if use_gpu:
                    state_dict = model.module.state_dict()
                else:
                    state_dict = model.state_dict()
                save_checkpoint(
                    {
                        'state_dict': state_dict,
                        'rank1': rank1,
                        'epoch': epoch,
                    }, is_best,
                    osp.join(opt.save_dir,
                             'checkpoint_ep' + str(epoch + 1) + '.pth.tar'))
    if not opt.train_only:
        print("==> Best Rank-1 {:.1%}, achieved at epoch {}".format(
            best_rank1, best_epoch))

    elapsed = round(time.time() - start_time)
    elapsed = str(datetime.timedelta(seconds=elapsed))
    train_time = str(datetime.timedelta(seconds=train_time))
    print(
        "Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.".
        format(elapsed, train_time))
    save_filename = 'net_%s.pth'% epoch_label
    save_path = os.path.join('./model',name,save_filename)
    torch.save(network.cpu().state_dict(), save_path)
    if torch.cuda.is_available:
        network.cuda(gpu_ids[0])


######################################################################
# Finetuning the convnet
# ----------------------
#
# Load a pretrainied model and reset final fully connected layer.
#

if opt.use_dense:
    model = ft_net_dense(len(class_names))
else:
    model = ft_net(len(class_names))
print(model)

if use_gpu:
    model = model.cuda()

criterion = nn.CrossEntropyLoss()

ignored_params = list(map(id, model.model.fc.parameters() )) + list(map(id, model.classifier.parameters() ))
base_params = filter(lambda p: id(p) not in ignored_params, model.parameters())

# Observe that all parameters are being optimized
optimizer_ft = optim.SGD([
             {'params': base_params, 'lr': 0.01},
예제 #9
0
######################################################################
# Save model
# ---------------------------
def save_network(network, epoch_label):
    save_filename = 'net_%s.pth' % epoch_label
    save_path = os.path.join('./model', name, save_filename)
    torch.save(network.cpu().state_dict(), save_path)
    if torch.cuda.is_available:
        network.cuda(gpu_ids[0])


# print('------------'+str(len(clas_names))+'--------------')
if opt.use_dense:
    # print(len(class_names['train']))
    model = ft_net_dense(
        n_classes)  # 751 class for training data in market 1501 in total
else:
    model = ft_net(n_classes)

if use_gpu:
    model = model.cuda()
criterion = SLSloss()

ignored_params = list(map(id, model.model.fc.parameters())) + list(
    map(id, model.classifier.parameters()))
base_params = filter(lambda p: id(p) not in ignored_params, model.parameters())

# Observe that all parameters are being optimized
optimizer_ft = optim.SGD([{
    'params': base_params,
    'lr': 0.01
예제 #10
0
    # load best model weights
    model.load_state_dict(best_model_wts)
    save_network(model, 'best')
    return model


def save_network(network, epoch_label):
    save_filename = 'net_%s.pth' % epoch_label
    save_path = os.path.join('./model', name, save_filename)
    torch.save(network.cpu().state_dict(), save_path)
    if torch.cuda.is_available:
        network.cuda(gpu_ids[0])


if opt.use_dense:
    model = ft_net_dense(n_classes)
else:
    model = ft_net(n_classes)

if use_gpu:
    model = model.cuda()
criterion = SLSloss()

ignored_params = list(map(id, model.model.fc.parameters())) + list(
    map(id, model.classifier.parameters()))
base_params = filter(lambda p: id(p) not in ignored_params, model.parameters())

optimizer_ft = optim.SGD([{
    'params': base_params,
    'lr': 0.01
}, {
예제 #11
0
    save_filename = 'net_%s.pth' % epoch_label
    save_path = os.path.join('./model', name, save_filename)
    torch.save(network.cpu().state_dict(), save_path)
    if torch.cuda.is_available():
        network.cuda(gpu_ids[0])


######################################################################
# Finetuning the convnet
# ----------------------
#
# Load a pretrainied model and reset final fully connected layer.
#

if opt.use_dense:
    model = ft_net_dense((id_class_number), opt.droprate)
else:
    model = ft_net((id_class_number), attr_class_number, opt.droprate,
                   opt.stride)

if opt.PCB:
    model = PCB((id_class_number))

opt.nclasses = (id_class_number)

print(model)

if not opt.PCB:
    ignored_params = list(map(id, model.model.fc.parameters()))
    for i in range(attr_class_number + 1):
        list(map(id, model.__getattr__('class_' + str(i)).parameters()))
예제 #12
0
            labels.append(int(label))
        camera_id.append(int(camera[0]))
        files.append(filename)
    return camera_id, labels, files

gallery_path = image_datasets['gallery'].imgs
query_path = image_datasets['query'].imgs

gallery_cam,gallery_label,gallery_files= get_id(gallery_path)
query_cam,query_label,query_files = get_id(query_path)
######################################################################
# Load Collected data Trained model
print('-------test-----------')
if opt.use_dense:
    if 'market' in test_dir:
        model_structure = ft_net_dense(1072, istrain=False)
    else:
        model_structure = ft_net_dense(702, istrain=False)
else:
    model_structure = ft_net(751)
# model = load_network(model_structure)
model = load_network_easy(model_structure)


model.org_fc = nn.Sequential()
model.org_classifier = nn.Sequential()
model.org_mid_fc = nn.Sequential()
model.org_mid_classifier = nn.Sequential()
model.cam_fc = nn.Sequential()
model.cam_classifier = nn.Sequential()
model.wo_fc = nn.Sequential()
예제 #13
0
    save_network(model, 'best')
    save_network(model, 'best' + '_stage_' + str(stage))
    save_network(model, opt.modelname)
    torch.save(model, os.path.join('./model', name, 'whole_net_best.pth'))
    torch.save(
        model,
        os.path.join('./model', name,
                     'whole_net_best' + '_stage_' + str(stage)) + '.pth')

    return model


# print('------------'+str(len(clas_names))+'--------------')
if True:  # opt.use_dense:
    model = ft_net_dense(
        id_num, cam_num,
        istrain=True)  # 751 class for training data in all dataset in total
    # model = ft_net_dense(751, 6, istrain=True)  # 751 class for training data in market 1501 in total
    # model = ft_net_dense(702, 8, istrain=True)  # 751 class for training data in duke in total
else:
    model = ft_net(751)

# print(model)

if use_gpu:
    model = model.cuda()

criterion = nn.CrossEntropyLoss()

# Decay LR by a factor of 0.1 every 40 epochs
예제 #14
0
"""
for key, value in query_dict.items():
    print(key)

for key, value in gallery_dict.items():
    print(key)
"""

print('-------test-----------')
if opt.loss_type == 'soft':
    if opt.name == 'resnet_50':
        model_structure = ft_net(751)
    elif opt.name == 'resnext_50':
        model_structure = resnext50(num_classes=751)
    elif opt.name == 'densenet_121':
        model_structure = ft_net_dense(751)
model = load_network(model_structure)

# Change to test mode
model = model.eval()
if use_gpu:
    model = model.cuda()

for p in model.parameters():
    p.requires_grad = False
    # print(p.requires_grad)

gallery_feature_2048_adv = torch.FloatTensor()
gallery_feature_2048 = torch.FloatTensor()
query_feature_2048 = torch.FloatTensor()
toImage = transforms.ToPILImage()
예제 #15
0
            labels.append(int(label))
        camera_id.append(int(camera[0]))
        files.append(filename)
    return camera_id, labels, files


gallery_path = image_datasets['gallery'].imgs
query_path = image_datasets['query'].imgs

gallery_cam, gallery_label, gallery_files = get_id(gallery_path)
query_cam, query_label, query_files = get_id(query_path)
######################################################################
# Load Collected data Trained model
print('-------test-----------')
if opt.use_dense:
    embedding_net = ft_net_dense()
    model_structure = SiameseNet(embedding_net)
else:
    model_structure = ft_net(751)
# model = load_network(model_structure)
model = load_network_easy(model_structure)

model.bn = nn.Sequential()
model.fc = nn.Sequential()
model.classifier = nn.Sequential()

# Change to test mode
model = model.eval()
if use_gpu:
    model = model.cuda()
예제 #16
0
def train(opt):
    version = torch.__version__

    fp16 = opt.fp16
    data_dir = opt.data_dir
    name = opt.name
    str_ids = opt.gpu_ids.split(',')
    gpu_ids = []
    for str_id in str_ids:
        gid = int(str_id)
        if gid >= 0:
            gpu_ids.append(gid)

    # set gpu ids
    if len(gpu_ids) > 0:
        torch.cuda.set_device(gpu_ids[0])
        cudnn.benchmark = True
    ######################################################################
    # Load Data
    # ---------
    #

    transform_train_list = [
        # transforms.RandomResizedCrop(size=128, scale=(0.75,1.0), ratio=(0.75,1.3333), interpolation=3), #Image.BICUBIC)
        transforms.Resize((256, 128), interpolation=3),
        transforms.Pad(10),
        transforms.RandomCrop((256, 128)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]

    transform_val_list = [
        transforms.Resize(size=(256, 128), interpolation=3),  # Image.BICUBIC
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]

    if opt.PCB:
        transform_train_list = [
            transforms.Resize((384, 192), interpolation=3),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]
        transform_val_list = [
            transforms.Resize(size=(384, 192),
                              interpolation=3),  # Image.BICUBIC
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]

    if opt.erasing_p > 0:
        transform_train_list = transform_train_list + [
            RandomErasing(probability=opt.erasing_p, mean=[0.0, 0.0, 0.0])
        ]

    if opt.color_jitter:
        transform_train_list = [
            transforms.ColorJitter(
                brightness=0.1, contrast=0.1, saturation=0.1, hue=0)
        ] + transform_train_list

    # print(transform_train_list)
    data_transforms = {
        'train': transforms.Compose(transform_train_list),
        'val': transforms.Compose(transform_val_list),
    }

    train_all = ''
    if opt.train_all:
        train_all = '_all'

    image_datasets = {}
    image_datasets['train'] = datasets.ImageFolder(
        os.path.join(data_dir, 'train' + train_all), data_transforms['train'])
    image_datasets['val'] = datasets.ImageFolder(os.path.join(data_dir, 'val'),
                                                 data_transforms['val'])

    dataloaders = {
        x: torch.utils.data.DataLoader(image_datasets[x],
                                       batch_size=opt.batchsize,
                                       shuffle=True,
                                       num_workers=8,
                                       pin_memory=True)
        # 8 workers may work faster
        for x in ['train', 'val']
    }
    dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
    class_names = image_datasets['train'].classes

    use_gpu = torch.cuda.is_available()

    #since = time.time()
    #inputs, classes = next(iter(dataloaders['train']))
    #print('time used for loading data: %ds' %(time.time() - since))

    ######################################################################
    # Training the model
    # ------------------
    #
    # Now, let's write a general function to train a model. Here, we will
    # illustrate:
    #
    # -  Scheduling the learning rate
    # -  Saving the best model
    #
    # In the following, parameter ``scheduler`` is an LR scheduler object from
    # ``torch.optim.lr_scheduler``.

    y_loss = {}  # loss history
    y_loss['train'] = []
    y_loss['val'] = []
    y_err = {}
    y_err['train'] = []
    y_err['val'] = []

    def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
        since = time.time()

        results = []
        for epoch in range(num_epochs):
            print('Epoch {}/{}'.format(epoch, num_epochs - 1))

            # Each epoch has a training and validation phase
            for phase in ['train', 'val']:
                if phase == 'train':
                    scheduler.step()
                    model.train(True)  # Set model to training mode
                else:
                    model.train(False)  # Set model to evaluate mode

                running_loss = 0.0
                running_corrects = 0.0

                # Iterate over data.
                pbar = tqdm(dataloaders[phase])
                for inputs, labels in pbar:
                    # get the inputs
                    now_batch_size, c, h, w = inputs.shape
                    if now_batch_size < opt.batchsize:  # skip the last batch
                        continue
                    # print(inputs.shape)
                    # wrap them in Variable
                    if use_gpu:
                        inputs = Variable(inputs.cuda().detach())
                        labels = Variable(labels.cuda().detach())
                    else:
                        inputs, labels = Variable(inputs), Variable(labels)
                    # if we use low precision, input also need to be fp16
                    # if fp16:
                    #    inputs = inputs.half()

                    # zero the parameter gradients
                    optimizer.zero_grad()

                    # forward
                    if phase == 'val':
                        with torch.no_grad():
                            outputs = model(inputs)
                    else:
                        outputs = model(inputs)

                    if not opt.PCB:
                        _, preds = torch.max(outputs.data, 1)
                        loss = criterion(outputs, labels)
                    else:
                        part = {}
                        sm = nn.Softmax(dim=1)
                        num_part = 6
                        for i in range(num_part):
                            part[i] = outputs[i]

                        score = sm(part[0]) + sm(part[1]) + sm(part[2]) + sm(
                            part[3]) + sm(part[4]) + sm(part[5])
                        _, preds = torch.max(score.data, 1)

                        loss = criterion(part[0], labels)
                        for i in range(num_part - 1):
                            loss += criterion(part[i + 1], labels)

                    # backward + optimize only if in training phase
                    if phase == 'train':
                        if fp16:  # we use optimier to backward loss
                            with amp.scale_loss(loss,
                                                optimizer) as scaled_loss:
                                scaled_loss.backward()
                        else:
                            loss.backward()
                        optimizer.step()

                    # statistics
                    if int(version[0]) > 0 or int(
                            version[2]
                    ) > 3:  # for the new version like 0.4.0, 0.5.0 and 1.0.0
                        running_loss += loss.item() * now_batch_size
                    else:  # for the old version like 0.3.0 and 0.3.1
                        running_loss += loss.data[0] * now_batch_size
                    running_corrects += float(torch.sum(preds == labels.data))
                    pbar.set_description(
                        desc='loss: {:.4f}'.format(loss.item()))

                epoch_loss = running_loss / dataset_sizes[phase]
                epoch_acc = running_corrects / dataset_sizes[phase]

                print('\r\n{} Loss: {:.4f} Acc: {:.4f}'.format(
                    phase, epoch_loss, epoch_acc))
                logging.info('epoch: {}, {} Loss: {:.4f} Acc: {:.4f}'.format(
                    epoch, phase, epoch_loss, epoch_acc))

                y_loss[phase].append(epoch_loss)
                y_err[phase].append(1.0 - epoch_acc)
                # deep copy the model
                if phase == 'val':
                    results.append({
                        'epoch': epoch,
                        'trainLoss': y_loss['train'][-1],
                        'trainError': y_err['train'][-1],
                        'valLoss': y_loss['val'][-1],
                        'valError': y_err['val'][-1]
                    })

                    last_model_wts = model.state_dict()
                    if epoch % 10 == 9:
                        save_network(model, epoch)
                    draw_curve(epoch)
                    write_to_csv(results)

            time_elapsed = time.time() - since
            print('\r\nTraining complete in {:.0f}m {:.0f}s'.format(
                time_elapsed // 60, time_elapsed % 60))
            print()

        time_elapsed = time.time() - since
        print('\r\nTraining complete in {:.0f}m {:.0f}s'.format(
            time_elapsed // 60, time_elapsed % 60))
        # print('Best val Acc: {:4f}'.format(best_acc))

        # load best model weights
        model.load_state_dict(last_model_wts)
        save_network(model, 'last')
        return model

    ######################################################################
    # Draw Curve
    # ---------------------------
    x_epoch = []
    fig = plt.figure()
    ax0 = fig.add_subplot(121, title="loss")
    ax1 = fig.add_subplot(122, title="top1err")

    def draw_curve(current_epoch):
        x_epoch.append(current_epoch)
        ax0.plot(x_epoch, y_loss['train'], 'bo-', label='train')
        ax0.plot(x_epoch, y_loss['val'], 'ro-', label='val')
        ax1.plot(x_epoch, y_err['train'], 'bo-', label='train')
        ax1.plot(x_epoch, y_err['val'], 'ro-', label='val')
        if current_epoch == 0:
            ax0.legend()
            ax1.legend()
        fig.savefig(os.path.join('./model', name, 'train.jpg'))

    def write_to_csv(results):
        path = os.path.join('./model', name, 'result.csv')

        with open(path, 'w', newline='') as csvfile:
            writer = csv.DictWriter(csvfile,
                                    fieldnames=list(results[0].keys()))
            writer.writeheader()
            writer.writerows(results)

    ######################################################################
    # Save model
    # ---------------------------
    def save_network(network, epoch_label):
        save_filename = 'net_%s.pth' % epoch_label
        rpth = os.path.join('./model', name, 'Model Files')
        if not os.path.exists(rpth):
            os.makedirs(rpth)
        save_path = os.path.join(rpth, save_filename)
        torch.save(network.cpu().state_dict(), save_path)
        if torch.cuda.is_available():
            network.cuda(gpu_ids[0])

    ######################################################################
    # Finetuning the convnet
    # ----------------------
    #
    # Load a pretrainied model and reset final fully connected layer.
    #

    if opt.use_dense:
        model = ft_net_dense(len(class_names), opt.droprate)
    else:
        model = ft_net(len(class_names), opt.droprate, opt.stride)

    if opt.PCB:
        model = PCB(len(class_names))

    opt.nclasses = len(class_names)

    print(model)
    print('model loaded')

    if not opt.PCB:
        ignored_params = list(map(id, model.model.fc.parameters())) + list(
            map(id, model.classifier.parameters()))
        base_params = filter(lambda p: id(p) not in ignored_params,
                             model.parameters())
        optimizer_ft = optim.SGD([{
            'params': base_params,
            'lr': 0.1 * opt.lr
        }, {
            'params': model.model.fc.parameters(),
            'lr': opt.lr
        }, {
            'params': model.classifier.parameters(),
            'lr': opt.lr
        }],
                                 weight_decay=5e-4,
                                 momentum=0.9,
                                 nesterov=True)
    else:
        ignored_params = list(map(id, model.model.fc.parameters()))
        ignored_params += (
            list(map(id, model.classifier0.parameters())) +
            list(map(id, model.classifier1.parameters())) +
            list(map(id, model.classifier2.parameters())) +
            list(map(id, model.classifier3.parameters())) +
            list(map(id, model.classifier4.parameters())) +
            list(map(id, model.classifier5.parameters()))
            # +list(map(id, model.classifier6.parameters() ))
            # +list(map(id, model.classifier7.parameters() ))
        )
        base_params = filter(lambda p: id(p) not in ignored_params,
                             model.parameters())
        optimizer_ft = optim.SGD(
            [
                {
                    'params': base_params,
                    'lr': 0.1 * opt.lr
                },
                {
                    'params': model.model.fc.parameters(),
                    'lr': opt.lr
                },
                {
                    'params': model.classifier0.parameters(),
                    'lr': opt.lr
                },
                {
                    'params': model.classifier1.parameters(),
                    'lr': opt.lr
                },
                {
                    'params': model.classifier2.parameters(),
                    'lr': opt.lr
                },
                {
                    'params': model.classifier3.parameters(),
                    'lr': opt.lr
                },
                {
                    'params': model.classifier4.parameters(),
                    'lr': opt.lr
                },
                {
                    'params': model.classifier5.parameters(),
                    'lr': opt.lr
                },
                # {'params': model.classifier6.parameters(), 'lr': 0.01},
                # {'params': model.classifier7.parameters(), 'lr': 0.01}
            ],
            weight_decay=5e-4,
            momentum=0.9,
            nesterov=True)

    # Decay LR by a factor of 0.1 every 40 epochs
    exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft,
                                           step_size=40,
                                           gamma=0.1)

    ######################################################################
    # Train and evaluate
    # ^^^^^^^^^^^^^^^^^^
    #
    # It should take around 1-2 hours on GPU.
    #
    dir_name = os.path.join('./model', name)
    if not os.path.isdir(dir_name):
        os.mkdir(dir_name)
    # record every run
    copyfile('./train.py', dir_name + '/train.py')
    copyfile('./model.py', dir_name + '/model.py')

    # save opts
    with open('%s/opts.yaml' % dir_name, 'w') as fp:
        yaml.dump(vars(opt), fp, default_flow_style=False)

    # model to gpu
    model = model.cuda()
    if fp16:
        # model = network_to_half(model)
        # optimizer_ft = FP16_Optimizer(optimizer_ft, static_loss_scale = 128.0)
        model, optimizer_ft = amp.initialize(model,
                                             optimizer_ft,
                                             opt_level="O1")

    criterion = losses.DualLoss()

    model = train_model(model,
                        criterion,
                        optimizer_ft,
                        exp_lr_scheduler,
                        num_epochs=60)


#
# if __name__ == "__main__":
#     train(opt)
예제 #17
0
# ----------single gpu training-----------------
def load_network(network, model_name=None):
    print('load pretraind model')
    if model_name == None:
        # save_path = os.path.join('./model', name, 'baseline_best_without_gan.pth')
        save_path = os.path.join('./model', name, 'net_best.pth')
    else:
        save_path = model_name
    network.load_state_dict(torch.load(save_path))
    return network


# print('------------'+str(len(clas_names))+'--------------')
if opt.use_dense:
    # print(len(class_names['train']))
    model = ft_net_dense(
        751)  # 751 class for training data in market 1501 in total
    model_pred = ft_net_dense(
        751)  # 751 class for training data in market 1501 in total
else:
    model = ft_net(751)
    model_pred = ft_net(751)

if use_gpu:
    model = model.cuda()
    model_pred = model_pred.cuda()

# load_network(model_pred)
# model_pred.eval()


# read dcgan data
예제 #18
0
    def __init__(self, gpuID, model_path):
        ####################################################
        # Options
        # ------------------
        self.gpu_ids = 1
        self.which_epoch = '59'
        self.batch_size = 1
        self.use_dense = False
        self.use_PCB = False
        self.model_path = model_path
        self.class_num = 751
        self.score_threshold = 0.9
        self.confidence_threshold = 0.6
        ####################################################
        # Set gpu
        # ------------------
        use_gpu = torch.cuda.is_available()
        if not use_gpu:
            print('can not user gpu')
            exit()
        torch.cuda.set_device(self.gpu_ids)

        ####################################################
        # Load model
        # ------------------
        print('load model...')
        if self.use_dense:
            model_structure = ft_net_dense(self.class_num)
        else:
            model_structure = ft_net(self.class_num)
        if self.use_PCB:
            model_structure = PCB(self.class_num)

        model = utils.load_network(model_structure, self.model_path,
                                   self.which_epoch)
        # Remove the final fc layer and classifier layer
        if not self.use_PCB:
            model.model.fc = nn.Sequential()
            model.classifier = nn.Sequential()
        else:
            model = PCB_test(model)
        model = model.eval()
        #print(model)
        if use_gpu:
            model = model.cuda()
        self.model = model
        ####################################################
        # Set Transform
        # ------------------
        print('set transform...')
        self.data_transforms = transforms.Compose([
            transforms.Resize((256, 128), interpolation=3),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ])

        if self.use_PCB:
            self.data_transforms = transforms.Compose([
                transforms.Resize((384, 192), interpolation=3),
                transforms.ToTensor(),
                transforms.Normalize([0.485, 0.456, 0.406],
                                     [0.229, 0.224, 0.225])
            ])
예제 #19
0
                                shuffle=True,
                                num_workers=8)

print('dataset_sizes[train] = %s' % dataset_sizes['train'])
print('dataset_sizes[val] = %s' % dataset_sizes['val'])

class_num = {}
class_num['train'] = len(os.listdir(dataset_train_dir))
class_num['val'] = len(os.listdir(dataset_val_dir))
print('class_num  train = %d   test = %d' %
      (class_num['train'], class_num['val']))

if opt.use_dense:
    # print(len(class_names['train']))
    model = ft_net_dense(
        class_num['train']
    )  # 751 class for training data in market 1501 in total
    model_pred = ft_net_dense(
        class_num['train']
    )  # 751 class for training data in market 1501 in total
else:
    model = ft_net(class_num['train'])
    model_pred = ft_net(class_num['train'])

if use_gpu:
    model = model.cuda()
    model_pred = model_pred.cuda()

######################################################################
# Training the model
# ------------------
예제 #20
0
        camera_id.append(int(camera[0]))
    return camera_id, labels

gallery_path = image_datasets['gallery'].imgs
query_path = image_datasets['query'].imgs
mquery_path = image_datasets['multi-query'].imgs

gallery_cam,gallery_label = get_id(gallery_path)
query_cam,query_label = get_id(query_path)
mquery_cam,mquery_label = get_id(mquery_path)

######################################################################
# Load Collected data Trained model
print('-------test-----------')
if opt.use_dense:
    model_structure = ft_net_dense(751)
else:
    model_structure = ft_net(751)

if opt.PCB:
    model_structure = PCB(751)

model = load_network(model_structure)
#model = model_structure

# Remove the final fc layer and classifier layer
#if not opt.PCB:
#    model.model.fc = nn.Sequential()
#    model.classifier = nn.Sequential()
#else:
#    model = PCB_test(model)
        frames.append(int(frame))
    return camera_id, labels, frames


gallery_path = image_datasets['gallery'].imgs
query_path = image_datasets['query'].imgs

gallery_cam, gallery_label, gallery_frames = get_id(gallery_path)
query_cam, query_label, query_frames = get_id(query_path)

######################################################################
# Load Collected data Trained model
class_num = 751
print('-------test-----------')
if opt.use_dense:
    model_structure = ft_net_dense(class_num)
else:
    model_structure = ft_net(class_num)

if opt.PCB:
    model_structure = PCB(class_num)

model = load_network(model_structure)

# Remove the final fc layer and classifier layer
if not opt.PCB:
    model.model.fc = nn.Sequential()
    model.classifier = nn.Sequential()
else:
    model = PCB_test(model)
예제 #22
0
    if current_epoch == 0:
        ax0.legend()
        ax1.legend()
    fig.savefig(os.path.join('./model', name, 'train.jpg'))



######################################################################
# Finetuning the convnet
# ----------------------
#
# Load a pretrainied model and reset final fully connected layer.
#

if opt.use_dense:
    model = ft_net_dense(len(class_names))
else:
    model = ft_net(len(class_names))

if opt.PCB:
    model = PCB(len(class_names))

model_verif = verif_net()
# print(model)
# print(model_verif)

if use_gpu:
    model = model.cuda()
    model_verif = model_verif.cuda()

criterion = nn.CrossEntropyLoss()
예제 #23
0
if __name__ == "__main__":

    if len(sys.argv) != 2:
        print('Usage python dist_test.py model_name')
        exit(0)

    model_name = sys.argv[1]
    model_path = os.path.join('models', model_name)
    sys.path.append(model_path)
    import model

    if model_name == 'fp16':
        net = model.ft_net(751, stride=1)
    elif model_name == 'ft_net_dense':
        net = model.ft_net_dense(751, stride=1)
    elif model_name == 'ft_ResNet50':
        net = model.ft_net_middle(751, stride=1)

    pretrain_path = os.path.join('models', model_name, 'net_last.pth')
    net.load_state_dict(torch.load(pretrain_path))
    net.eval()

    net.classifier = nn.Sequential()

    data_transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])
    my_dataset = Market1501_Dataset(transform=data_transform)
예제 #24
0
def test():
    print('-------test-----------')
    time_start = time.time()
    if opt.use_dense:
        model_structure = ft_net_dense(opt.nclasses)
    #elif opt.use_NAS:
    #    model_structure = ft_net_NAS(opt.nclasses)
    else:
        model_structure = ft_net(opt.nclasses, stride=opt.stride)

    if opt.PCB:
        model_structure = PCB(opt.nclasses)

    #if opt.fp16:
    #    model_structure = network_to_half(model_structure)

    model = load_network(model_structure)

    # Remove the final fc layer and classifier layer
    if opt.PCB:
        #if opt.fp16:
        #    model = PCB_test(model[1])
        #else:
        model = PCB_test(model)
    else:
        #if opt.fp16:
        #model[1].model.fc = nn.Sequential()
        #model[1].classifier = nn.Sequential()
        #else:
        model.classifier.classifier = nn.Sequential()

    # Change to test mode
    model = model.eval()
    if use_gpu:
        model = model.cuda()

    # Extract feature
    with torch.no_grad():
        gallery_feature = extract_feature(model, dataloaders['gallery'])
        query_feature = extract_feature(model, dataloaders['query'])
        if opt.multi:
            mquery_feature = extract_feature(model, dataloaders['multi-query'])

    # Save to Matlab for check
    result = {
        'gallery_f': gallery_feature.numpy(),
        'gallery_label': gallery_label,
        'gallery_cam': gallery_cam,
        'query_f': query_feature.numpy(),
        'query_label': query_label,
        'query_cam': query_cam
    }
    scipy.io.savemat('pytorch_result.mat', result)

    print(opt.name)

    os.system('python evaluate_gpu.py')

    if opt.multi:
        result = {
            'mquery_f': mquery_feature.numpy(),
            'mquery_label': mquery_label,
            'mquery_cam': mquery_cam
        }
        scipy.io.savemat('multi_query.mat', result)
    time_end = time.time()
    print('totally cost', time_end - time_start)
예제 #25
0
    save_filename = 'net_%s.pth' % epoch_label
    save_path = os.path.join('./model', name, save_filename)
    torch.save(network.cpu().state_dict(), save_path)
    if torch.cuda.is_available():
        network.cuda(gpu_ids[0])


######################################################################
# Finetuning the convnet
# ----------------------
#
# Load a pretrainied model and reset final fully connected layer.
#

if opt.use_dense:
    model = ft_net_dense(len(class_names), opt.droprate, circle=opt.circle)
elif opt.use_NAS:
    model = ft_net_NAS(len(class_names), opt.droprate)
else:
    model = ft_net(len(class_names),
                   opt.droprate,
                   opt.stride,
                   circle=opt.circle)

if opt.PCB:
    model = PCB(len(class_names))

opt.nclasses = len(class_names)

print(model)
예제 #26
0
    dataset_path.append(image_datasets[dataset_list[i]].imgs)

dataset_cam = []
dataset_label = []
for i in range(len(dataset_list)):
    cam, label = get_id(dataset_path[i])
    dataset_cam.append(cam)
    dataset_label.append(label)

######################################################################
# Load Collected data Trained model
print('-------test-----------')
class_num = len(os.listdir(os.path.join(data_dir, 'train_all_new')))
# class_num = 751
class_num *= opt.domain_num
encoder_a = ft_net_dense(class_num)
encoder_s = ft_net_dense(class_num)
decoder_a = decoder(2048)
model = Auto_Encoder(encoder_a, encoder_s, decoder_a)
if use_gpu:
    model.cuda()

if 'best' in opt.which_epoch or 'last' in opt.which_epoch:
    model = load_network(model, name, opt.which_epoch + '_' + str(opt.net_loss_model))
else:
    model = load_network(model, name, opt.which_epoch)
model = model.eval()
if use_gpu:
    model = model.cuda()

# Extract feature
예제 #27
0
    save_filename = 'net_%s.pth' % epoch_label
    save_path = os.path.join('./model', name, save_filename)
    torch.save(network.cpu().state_dict(), save_path)
    if torch.cuda.is_available:
        network.cuda(gpu_ids[0])


######################################################################
# Finetuning the convnet
# ----------------------
#
# Load a pretrainied model and reset final fully connected layer.
#

if opt.use_dense:
    model = ft_net_dense(len(class_names))
else:
    model = ft_net(len(class_names))

if opt.PCB:
    model = PCB(len(class_names))

# print(model)
# not showing the PCB model

if use_gpu:
    model = model.cuda()

criterion = nn.CrossEntropyLoss()

if not opt.PCB:
        [transforms.ToTensor()(crop) for crop in crops])),
    transforms.Lambda(lambda crops: torch.stack([
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        (crop) for crop in crops
    ]))
])


def load_network(network):
    save_path = os.path.join('./model', opt.model_name,
                             'net_%s.pth' % opt.which_epoch)
    network.load_state_dict(torch.load(save_path))
    return network


model_structure = ft_net_dense(1503)
model = load_network(model_structure)
model = model.eval()
use_gpu = torch.cuda.is_available()
if use_gpu:
    model = model.cuda()


class Dataset(Dataset):
    def __init__(self, path, transform):
        self.dir = path
        self.image = [f for f in os.listdir(self.dir) if f.endswith('png')]
        self.transform = transform

    def __len__(self):
        return len(self.image)
예제 #29
0
파일: train.py 프로젝트: lsh110600/Project
    save_filename = 'net_%s.pth' % epoch_label
    save_path = os.path.join('./model', name, save_filename)
    torch.save(network.cpu().state_dict(), save_path)
    if torch.cuda.is_available():
        network.cuda(gpu_ids[0])


######################################################################
# Finetuning the convnet
# ----------------------
#
# Load a pretrainied model and reset final fully connected layer.
#

if opt.use_dense:
    model = ft_net_dense(len(class_names), opt.droprate)
elif opt.use_NAS:
    model = ft_net_NAS(len(class_names), opt.droprate)
else:
    model = ft_net(len(class_names), opt.droprate, opt.stride)

if opt.PCB:
    model = PCB(len(class_names))

opt.nclasses = len(class_names)

print(model)

if not opt.PCB:
    ignored_params = list(map(id, model.classifier.parameters()))
    base_params = filter(lambda p: id(p) not in ignored_params,
예제 #30
0
            labels.append(int(label))
        camera_id.append(int(camera[0]))
    return camera_id, labels


gallery_path = image_datasets['gallery'].imgs
query_path = image_datasets['query'].imgs

gallery_cam, gallery_label = get_id(gallery_path)
query_cam, query_label = get_id(query_path)

######################################################################
# Load Collected data Trained model
print('-------test-----------')
if opt.use_dense:
    model_structure = ft_net_dense(28310)
else:
    model_structure = ft_net(28310)

if opt.PCB:
    model_structure = PCB(28310)

model_structure = nn.DataParallel(model_structure)
model = load_network(model_structure)

# Remove the final fc layer and classifier layer
#print(model)
if not opt.PCB:
    model.module.model.fc = nn.Sequential()
    # model.classifier = nn.Sequential()
else:
query_path = image_datasets['query'].imgs
train_all_path = image_datasets['train_all'].imgs

gallery_cam, gallery_label = get_id(gallery_path)
query_cam, query_label = get_id(query_path)
train_all_cam, train_all_label = get_id(train_all_path)

if opt.multi:
    mquery_path = image_datasets['multi-query'].imgs
    mquery_cam, mquery_label = get_id(mquery_path)

######################################################################
# Load Collected data Trained model
print('-------test-----------')
if opt.use_dense:
    model_structure = ft_net_dense(opt.nclasses)
elif opt.use_NAS:
    model_structure = ft_net_NAS(opt.nclasses)
else:
    model_structure = ft_net(opt.nclasses, stride=opt.stride)

if opt.PCB:
    model_structure = PCB(opt.nclasses)

#if opt.fp16:
#    model_structure = network_to_half(model_structure)

model = load_network(model_structure)

# Remove the final fc layer and classifier layer
if opt.PCB:
예제 #32
0
gallery_cam, gallery_label = get_id(gallery_path)
query_cam, query_label = get_id(query_path)

######################################################################
# Load Collected data Trained model
print('-------test-----------')
dataset_train_dir = os.path.join(data_dir, 'train_new')
dataset_val_dir = os.path.join(data_dir, 'val_new')
class_num = {}
class_num['train'] = len(os.listdir(dataset_train_dir))
class_num['val'] = len(os.listdir(dataset_val_dir))
print('class_num  train = %d   val = %d' %
      (class_num['train'], class_num['val']))
if opt.use_dense:
    model_structure = ft_net_dense(class_num['train'])
else:
    model_structure = ft_net(class_num['train'])
model = load_network(model_structure)

# Remove the final fc layer and classifier layer
model.model.fc = nn.Sequential()
model.classifier = nn.Sequential()

# Change to test mode
model = model.eval()
if use_gpu:
    model = model.cuda()

# Extract feature
gallery_feature = extract_feature(model, dataloaders['gallery'])