示例#1
0
        if opt.PCB:
            fnorm = torch.norm(ff, p=2, dim=1, keepdim=True) * np.sqrt(6)
            ff = ff.div(fnorm.expand_as(ff))
            ff = ff.view(ff.size(0), -1)
        else:
            fnorm = torch.norm(ff, p=2, dim=1, keepdim=True)
            ff = ff.div(fnorm.expand_as(ff))

        features = torch.cat((features, ff), 0)
    return features


if opt.use_dense:
    model_structure = ft_net_dense(751)
else:
    model_structure = ft_net(751)

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)

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

file_path = '../friends2/'

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 = 100
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)

# Change to test mode
model = model.eval()
示例#3
0
def main(mode):
    ImageFile.LOAD_TRUNCATED_IMAGES = True
    ######################################################################
    # Options
    # --------
    gpu_ids = '0'
    which_epoch = 'last'
    name = 'ft_ResNet50'
    batchsize = 32
    ms = '1'

    if mode == 'all':  # 전체 공고 대상
        test_dir = 'C:/Users/kdan/BigJob12/main_project/_db/data'
        mode_path = 'preprocessed_data'
    else:  # 특정 공고 대상
        test_dir = 'C:/Users/kdan/BigJob12/main_project/_db/data/model_data'
        mode_path = 'gallery'

    # load the training config
    config_path = os.path.join(
        'C:/Users/kdan/BigJob12/main_project/_src/data_analysis/re_id/code/model',
        name, 'opts.yaml')
    with open(config_path, 'r') as stream:
        config = yaml.load(stream)
    stride = config['stride']

    if 'nclasses' in config:  # tp compatible with old config files
        nclasses = config['nclasses']

    ######################################################################
    # GPU Options
    # --------
    str_ids = gpu_ids.split(',')
    gpu_ids = []
    for str_id in str_ids:
        id = int(str_id)
        if id >= 0:
            gpu_ids.append(id)

    print('We use the scale: %s' % ms)
    str_ms = ms.split(',')
    ms = []
    for s in str_ms:
        s_f = float(s)
        ms.append(math.sqrt(s_f))

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

    ######################################################################
    # Data transform
    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])
    ])

    ######################################################################
    # Load data
    data_dir = test_dir
    image_datasets = {
        x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms)
        for x in [mode_path]
    }
    dataloaders = {
        x: torch.utils.data.DataLoader(image_datasets[x],
                                       batch_size=batchsize,
                                       shuffle=False,
                                       num_workers=0)
        for x in [mode_path]
    }
    use_gpu = torch.cuda.is_available()

    # Load Collected data Trained model
    print('-------test-----------')
    model_structure = ft_net(nclasses, stride=stride)
    model = load_network(model_structure, name, which_epoch)

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

    # 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[mode_path], ms)

    # Save to Matlab for check
    gallery_result = {'gallery_f': gallery_feature.numpy()}
    if mode == 'all':
        scipy.io.savemat(
            'C:/Users/kdan/BigJob12/main_project/_src/data_analysis/re_id/code/gallery_all_result.mat',
            gallery_result)
        pd.DataFrame(gallery_result['gallery_f']).to_csv(
            ('gallery_all_result.csv'))
    else:
        scipy.io.savemat(
            'C:/Users/kdan/BigJob12/main_project/_src/data_analysis/re_id/code/gallery_result.mat',
            gallery_result)
    print(name)
示例#4
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)

if not opt.PCB:
    ignored_params = 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,
示例#5
0
        network.cuda(gpu_ids[0])
        #nn.DataParallel(network, device_ids=[2,3]).cuda()


######################################################################
# Finetuning the convnet
# ----------------------
#
# Load a pretrainied model and reset final fully connected layer.
#
def load_network_path(network, save_path):
    network.load_state_dict(torch.load(save_path))
    return network


model_structure = ft_net(len(class_names))
target = 'experiment_Result/p1'
path = target + '/ft_ResNet50/net_39.pth'
print('origin_with: ' + target)
model = load_network_path(model_structure, path)

model_from = ft_net(len(class_names))

nw.dup_net6(model_from, model, 'model.conv1', 'model.bn1', 'xx', 'xx',
            'model.fc', 'classifier')  #C1+FC remain

if use_gpu:
    model = model.cuda()
    #nn.DataParallel(model, device_ids=[2,3]).cuda()
criterion = nn.CrossEntropyLoss()
示例#6
0
        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
class dcganDataset(Dataset):
    def __init__(self, root, transform=None, targte_transform=None):
        super(dcganDataset, self).__init__()
        self.image_dir = os.path.join(opt.data_dir, root)
示例#7
0
    opt.num_gpus = num_gpus

    num_classes, data_loader = get_data_loader(opt.data_path, opt.batch_size)
    if opt.num_classes < num_classes:
        opt.num_classes = num_classes

    class_split = None
    if opt.model_parallel:
        # If using model parallel, split the number of classes
        # accroding to the number of GPUs
        class_split = get_class_split(opt.num_classes, num_gpus)

    model = ft_net(
            feature_dim=256,
            num_classes=opt.num_classes,
            num_gpus=num_gpus,
            am=opt.am,
            model_parallel=opt.model_parallel,
            class_split=class_split
        )
    optimizer_ft = optim.SGD(
            model.parameters(),
            lr=opt.lr,
            weight_decay=5e-4,
            momentum=0.9,
            nesterov=True
        )

    if opt.model_parallel:
        # When using model parallel, we wrap all the model except classifier in DataParallel
        model.backbone = nn.DataParallel(model.backbone).cuda()
        model.features = nn.DataParallel(model.features).cuda()
示例#8
0
search = target + '/ft_ResNet50'

file_list = os.listdir(search)

for file_name in file_list:
    #if file_name[-5:] == 't.pth':
    if file_name[-6:] == '39.pth':
        #if file_name[-6:-5] == '4':
        #if file_name[-3:] == 'pth':
        path = search + '/' + file_name
    else:
        continue

    print(path)
    #model_structure = ft_net(702) #duke
    model_structure = ft_net(num_class)  #market
    model = load_network_path(model_structure, path)
    # 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'])
    query_feature = extract_feature(model, dataloaders['query'])

    # Save to Matlab for check
示例#9
0
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
# ------------------

y_loss = {}  # loss history
y_loss['train'] = []
y_loss['val'] = []
y_err = {}
y_err['train'] = []
示例#10
0
    if opt.re_rank:
        newD = re_ranking(q_g_dist, q_q_dist, g_g_dist)
        newD = np.transpose(newD)
        res += _cmc_core(newD, gallery_labels_set, query_label, k)
    res /= Times
    return res


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

    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, gallery_label = extract_feature(model,
                                                     dataloaders['gallery'])
    query_feature, query_label = extract_feature(model, dataloaders['query'])
示例#11
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)
示例#12
0
    opt.nclasses = len(class_names)
    if opt.use_dense:
        model = ft_net_dense(len(class_names), opt.droprate, opt.stride, None,
                             opt.pool)
    elif opt.use_NAS:
        model = ft_net_NAS(len(class_names), opt.droprate, opt.stride)
    elif opt.use_SE:
        model = ft_net_SE(len(class_names), opt.droprate, opt.stride, opt.pool,
                          None)
    elif opt.use_DSE:
        model = ft_net_DSE(len(class_names), opt.droprate, opt.stride,
                           opt.pool)
    elif opt.use_IR:
        model = ft_net_IR(len(class_names), opt.droprate, opt.stride)
    else:
        model = ft_net(len(class_names), opt.droprate, opt.stride, None,
                       opt.pool)

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

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

    if opt.angle:
        model = ft_net_angle(len(class_names), opt.droprate, opt.stride)
    elif opt.arc:
        model = ft_net_arc(len(class_names), opt.droprate, opt.stride)

if opt.init_name != 'imagenet':
    old_opt = parser.parse_args()
    init_model, old_opt, _ = load_network(opt.init_name, old_opt)
示例#13
0
    print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
    model.load_state_dict(last_model_wts)
    save_network(model)
    return model


# 保存pth文件
def save_network(network):
    save_path = './net.pth' #生成pth文件
    torch.save(network.cpu().state_dict(), save_path)
    if torch.cuda.is_available():
        network.cuda(0)


#加载网络,丢弃全连接层
model = ft_net(len(class_names), droprate, stride)
ignored_params = 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*lr},
         {'params': model.classifier.parameters(), 'lr': lr}
     ], weight_decay=5e-4, momentum=0.9, nesterov=True)

#学习率参数
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=40, gamma=0.1)
if use_gpu:
    model = model.cuda()
criterion = nn.CrossEntropyLoss()
model = train_model(model, criterion, optimizer_ft, exp_lr_scheduler,num_epochs=60)#训练的epoch数量设置

示例#14
0
query_path = image_datasets['query'].imgs

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

if multi_query:
    mquery_path = image_datasets['multi-query'].imgs
    mquery_cam,mquery_label = get_id(mquery_path)
    
#%%
train_all = '_all'
image_datasets_train = {}
image_datasets_train['train'] = datasets.ImageFolder(os.path.join(data_dir, 'train' + train_all), data_transforms)    
class_names_train = image_datasets_train['train'].classes
   
model = ft_net(len(class_names_train), 0.2, 2)
model.load_state_dict(torch.load('net_last.pth'))    
  
temp = nn.Sequential(*list(model.children())[:-1])
res50_conv = nn.Sequential(*list(temp[0].children())[:-1])    
#for param in res50_conv.parameters():
#    param.requires_grad = False    
res50_conv.eval()
model = res50_conv

if use_gpu:
    model = model.cuda()

# Extract feature
with torch.no_grad():
    gallery_feature = extract_feature(model,dataloaders['gallery'])
示例#15
0
######################################################################
# Load Collected data Trained model
print('-------test-----------')

search = target + '/ft_ResNet50'

file_list = os.listdir(search)

print(search)
for tepoch in range(euntil + 1):
    if tepoch < efrom:
        continue

    file_name = 'net_%d.pth' % tepoch
    path = search + '/' + file_name
    model = ft_net(num_class)  #market
    model = load_network_path(model, path)

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

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

    # Save to Matlab for check

    query_label = np.asarray(query_label)
    gallery_label = np.asarray(gallery_label)
示例#16
0
    transforms.Resize((256, 128), interpolation=3),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

# load training dataDatasetFolder
print('Starting loading training data: ', args.dataset)
train_dataset = loader.DatasetFolder(os.path.join(data_dir, 'train'),
                                     transform=train_transform)
class_names = train_dataset.classes
dataset_sizes_train = len(train_dataset)

use_gpu = torch.cuda.is_available()

# Define a model
model1 = ft_net(len(class_names))
model2 = ft_net(len(class_names))
if args.pattern:
    model_tmp = ft_net(len(class_names))
if use_gpu:
    model1 = model1.cuda()
    model2 = model2.cuda()
    if args.pattern:
        model_tmp = model_tmp.cuda()

# Load a pretrainied model
if args.resume:
    # two pretrained SELF models (ignore when you start)
    model_name1 = 'market_noise_0.2_batch_32_refine_lr_1.0e-02_model1.t'
    print('Initilizaing weights with ', args.resume)
    model_path1 = checkpoint_path + model_name1
    transforms.Resize((256, 128), interpolation=3),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])

data_transforms_visualize = transforms.Compose(
    [transforms.Resize((256, 128), interpolation=3),
     transforms.ToTensor()])

image_datasets = {
    x: datasets.ImageFolder(os.path.join(opt.data_dir, x), data_transforms)
    for x in ['gallery', 'query']
}

query_path, _ = image_datasets['query'].imgs[qi]

query_img = Image.open(query_path)
query = data_transforms(query_img)
query_vis = data_transforms_visualize(query_img)
query.unsqueeze_(0)

model = ft_net(class_num=575)
model.load_state_dict(torch.load('./model/ft_ResNet/net_99.pth'))

model.classifier.classifier = nn.Sequential()
model = model.eval().cuda()

heatmap = generate_heatmap(model, query)

heatmap2d(query_vis, heatmap)
示例#18
0
        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

dir_name = os.path.join('./model', name)
if not os.path.isdir(dir_name):
    os.mkdir(dir_name)

# save opts
示例#19
0
    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)

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

print(model)

if use_gpu:
    model = model.cuda()

criterion = nn.CrossEntropyLoss()

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([
示例#20
0
    save_network(model, 'last')
    return model


# 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])


# Finetuning the convnet
# Load a pretrainied model and reset final fully connected layer.
model = ft_net(len(image_datasets.classes))
print(model)

if torch.cuda.is_available():
    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())
optimizer_ft = optim.SGD([{
    'params': base_params,
    'lr': 0.01
}, {
    'params': model.model.fc.parameters(),
    'lr': 0.1
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
nnn = opt.training_set_classes
# duke-market 702
print('-------test-----------')

if opt.use_two_stream_resnet:
    model_structure = two_stream_resnet(nnn, True)
else:
    model_structure = ft_net(nnn)
model = load_network(model_structure)

#model.model.avgpool = nn.AdaptiveMaxPool2d((7,1))

# Remove the final fc layer and classifier layer
if not opt.use_two_stream_resnet:
    model.classifier.fc = nn.Sequential()
    model.classifier = nn.Sequential()

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

# Extract feature
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)

# Change to test mode
示例#23
0
    return model


######################################################################
# Train and evaluate
# ^^^^^^^^^^^^^^^^^^
#
# It should take around 1-2 hours on GPU.
#
dir_name = os.path.join('./model', name)
if not os.path.exists('model'):
    os.mkdir('model')

# class_num *= 3
print('class_num = %d' % (class_num))
model = ft_net(class_num)
if use_gpu:
    model.cuda()

if False:
    model = load_whole_network(model, name, 'pretrain')
    opt.lr *= 0.5

# print('model structure')
# print(model)

criterion = nn.CrossEntropyLoss()
criterion_soft = SoftLabelLoss()

classifier_id = list(map(id, model.classifier.parameters())) \
                + list(map(id, model.model.fc.parameters()))
示例#24
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])
            ])
示例#25
0
    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_three_stream_resnet:
    model = three_stream_resnet(len(class_names))
else:
    model = ft_net(len(class_names))
print(model)

if use_gpu:
    model = model.cuda()

criterion = nn.CrossEntropyLoss()

if opt.use_three_stream_resnet:
    ignored_params = list(map(id, model.model.fc.parameters()))
    ignored_params += (list(map(id, model.classifier_original.parameters())) +
                       list(map(id, model.classifier_upper.parameters())) +
                       list(map(id, model.classifier_lower.parameters())))
    base_params = filter(lambda p: id(p) not in ignored_params,
                         model.parameters())
    optimizer_ft = optim.SGD([
示例#26
0
import shutil
import re
from PIL import Image
from torchvision import datasets, transforms, models
from model import ft_net
data_dir='/home/luke/'
target=4
ip='127.0.0.2' #ip address of the server 
port_num=1234  #port to connect
sc=True				#True: panda acts as server; False: panda acts as client;


test_transforms = transforms.Compose([transforms.Resize(size=(256,128),interpolation=3),transforms.ToTensor(),transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
device=torch.device('cpu')
imsize = 256
model1=ft_net(6)
#model.load_state_dict(torch.load(file_path))
model1.load_state_dict(torch.load('net_9.pth'))
model1.eval()
def predict_image(image):
	#image=Image.open(image_p)
	image_tensor = test_transforms(image).float()
	image_tensor = image_tensor.unsqueeze_(0)
	input = Variable(image_tensor)
	input = input.to(device)
	print (input.shape)
	output = model1(input)
	index = output.data.cpu().numpy().argmax()
	return index

        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(767)
else:
    model_structure = ft_net(767)
if opt.use_resnext:
    model_structure = resnext50(num_classes=767)
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'])
示例#28
0
query_path = image_datasets['query'].imgs

gallery_cam, gallery_label = get_id(gallery_path)
query_cam, query_label = get_id(query_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)
else:
    model_structure = ft_net(opt.nclasses)

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)
示例#29
0
    save_path = os.path.join('./model2/', 'net_%s.pth' % opt.which_epoch)
    # os.path.join('./model',name,'net_%s.pth'%opt.which_epoch)
    network.load_state_dict(torch.load(save_path))
    return network


use_gpu = torch.cuda.is_available()

# 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)

model = load_network(model_structure)
model.classifier.classifier = nn.Sequential()
model = model.eval()
if use_gpu:
    model = model.cuda()

# encoder = create_box_encoder('./mars-small128.pb', batch_size = 32)

######################################################################
# Base Functions
######################################################################


class DetectorAPI:
示例#30
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)
    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},
             {'params': model.model.fc.parameters(), 'lr': 0.1},
             {'params': model.classifier.parameters(), 'lr': 0.1}
示例#32
0
def main(first=True):
    ImageFile.LOAD_TRUNCATED_IMAGES = True
    ######################################################################
    # Options
    # --------
    # gpu_ids='0'  # use gpu
    which_epoch = 'last'
    test_dir = 'C:/Users/kdan/BigJob12/main_project/_db/data/model_data'
    name = 'ft_ResNet50'
    batchsize = 32
    ms = '1'

    # load the training config
    config_path = os.path.join(
        'C:/Users/kdan/BigJob12/main_project/_src/data_analysis/re_id/code/model',
        name, 'opts.yaml')
    with open(config_path, 'r') as stream:
        config = yaml.load(stream)
    stride = config['stride']

    if 'nclasses' in config:  # tp compatible with old config files
        nclasses = config['nclasses']

    ######################################################################
    # gpu Options
    # --------
    # str_ids = gpu_ids.split(',')
    # gpu_ids = []
    # for str_id in str_ids:
    #     id = int(str_id)
    #     if id >= 0:
    #         gpu_ids.append(id)

    print('We use the scale: %s' % ms)
    str_ms = ms.split(',')
    ms = []
    for s in str_ms:
        s_f = float(s)
        ms.append(math.sqrt(s_f))

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

    ######################################################################
    # Data transform
    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])
    ])

    ######################################################################
    # Load data
    data_dir = test_dir
    image_datasets = {
        x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms)
        for x in ['query']
    }
    dataloaders = {
        x: torch.utils.data.DataLoader(image_datasets[x],
                                       batch_size=batchsize,
                                       shuffle=False,
                                       num_workers=0)
        for x in ['query']
    }
    class_names = image_datasets['query'].classes
    use_gpu = torch.cuda.is_available()

    # Load Collected data Trained model
    print('-------test-----------')
    model_structure = ft_net(nclasses, stride=stride)
    model = load_network(model_structure, name, which_epoch)

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

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

    # Extract feature
    with torch.no_grad():
        query_feature = extract_feature(model, dataloaders['query'], ms)
    print(time.time() - start_load)

    # Save to Matlab for check
    query_result = {'query_f': query_feature.numpy()}
    reid_sort.main(query_result, image_datasets, 'all', first)
    scipy.io.savemat(
        'C:/Users/kdan/BigJob12/main_project/_src/data_analysis/re_id/code/query_result.mat',
        query_result)
    print(name)