本文整理汇总了Python中reid.models.create方法的典型用法代码示例。如果您正苦于以下问题:Python models.create方法的具体用法?Python models.create怎么用?Python models.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reid.models
的用法示例。
在下文中一共展示了models.create方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def train(self, train_data, step, loss, dropout=0.5):
# adjust training epochs and learning rate
epochs = self.initial_steps if step==0 else self.later_steps
init_lr = 0.1 if step==0 else 0.01
step_size = self.step_size if step==0 else sys.maxsize
""" create model and dataloader """
dataloader = self.get_dataloader(train_data, training=True)
# the base parameters for the backbone (e.g. ResNet50)
base_param_ids = set(map(id, self.model.module.CNN.base.parameters()))
# we fixed the first three blocks to save GPU memory
base_params_need_for_grad = filter(lambda p: p.requires_grad, self.model.module.CNN.base.parameters())
# params of the new layers
new_params = [p for p in self.model.parameters() if id(p) not in base_param_ids]
# set the learning rate for backbone to be 0.1 times
param_groups = [
{'params': base_params_need_for_grad, 'lr_mult': 0.1},
{'params': new_params, 'lr_mult': 1.0}]
optimizer = torch.optim.SGD(param_groups, lr=init_lr, momentum=0.9, weight_decay=5e-4, nesterov=True)
# change the learning rate by step
def adjust_lr(epoch, step_size):
lr = init_lr / (10 ** (epoch // step_size))
for g in optimizer.param_groups:
g['lr'] = lr * g.get('lr_mult', 1)
""" main training process """
trainer = Trainer(self.model, self.criterion, fixed_layer=self.fixed_layer)
for epoch in range(epochs):
adjust_lr(epoch, step_size)
trainer.train(epoch, dataloader, optimizer, print_freq=max(5, len(dataloader) // 30 * 10))
示例2: train
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def train(self, train_data, step, loss, dropout=0.5):
# adjust training epochs and learning rate
epochs = self.initial_steps if step==0 else self.later_steps
init_lr = 0.1 if step==0 else 0.01
step_size = self.step_size if step==0 else sys.maxsize
""" create model and dataloader """
dataloader = self.get_dataloader(train_data, training=True)
# the base parameters for the backbone (e.g. ResNet50)
base_param_ids = set(map(id, self.model.module.CNN.base.parameters()))
# we fixed the first three blocks to save GPU memory
base_params_need_for_grad = filter(lambda p: p.requires_grad, self.model.module.CNN.base.parameters())
# params of the new layers
new_params = [p for p in self.model.parameters() if id(p) not in base_param_ids]
# set the learning rate for backbone to be 0.1 times
param_groups = [
{'params': base_params_need_for_grad, 'lr_mult': 0.1},
{'params': new_params, 'lr_mult': 1.0}]
optimizer = torch.optim.SGD(param_groups, lr=init_lr, momentum=0.9, weight_decay=5e-4, nesterov=True)
def adjust_lr(epoch, step_size):
lr = init_lr / (10 ** (epoch // step_size))
for g in optimizer.param_groups:
g['lr'] = lr * g.get('lr_mult', 1)
""" main training process """
trainer = Trainer(self.model, self.criterion, fixed_layer=self.fixed_layer)
for epoch in range(epochs):
adjust_lr(epoch, step_size)
trainer.train(epoch, dataloader, optimizer, print_freq=max(5, len(dataloader) // 30 * 10))
示例3: get_dataloader
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def get_dataloader(self, dataset, training=False) :
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
if training:
transformer = T.Compose([
T.RandomSizedRectCrop(self.data_height, self.data_width),
T.RandomHorizontalFlip(),
T.ToTensor(),
normalizer,
])
batch_size = self.batch_size
else:
transformer = T.Compose([
T.RectScale(self.data_height, self.data_width),
T.ToTensor(),
normalizer,
])
batch_size = self.eval_bs
data_loader = DataLoader(
Preprocessor(dataset, root=self.data_dir,
transform=transformer, is_training=training, max_frames=self.max_frames),
batch_size=batch_size, num_workers=self.data_workers,
shuffle=training, pin_memory=True, drop_last=training)
current_status = "Training" if training else "Test"
print("create dataloader for {} with batch_size {}".format(current_status, batch_size))
return data_loader
示例4: resume
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def resume(self, ckpt_file, step):
print("continued from step", step)
model = models.create(self.model_name, dropout=self.dropout, num_classes=self.num_classes, mode=self.mode)
self.model = nn.DataParallel(model).cuda()
self.model.load_state_dict(load_checkpoint(ckpt_file))
示例5: __init__
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def __init__(self, model_name, batch_size, num_classes, dataset, u_data, save_path, embeding_fea_size=1024,
dropout=0.5, max_frames=900, initial_steps=20, step_size=16):
self.model_name = model_name
self.num_classes = num_classes
self.data_dir = dataset.images_dir
self.is_video = dataset.is_video
self.save_path = save_path
self.dataset = dataset
self.u_data = u_data
self.u_label = np.array([label for _, label, _, _ in u_data])
self.label_to_images = {}
self.sort_image_by_label=[]
self.dataloader_params = {}
self.dataloader_params['height'] = 256
self.dataloader_params['width'] = 128
self.dataloader_params['batch_size'] = batch_size
self.dataloader_params['workers'] = 6
self.batch_size = batch_size
self.data_height = 256
self.data_width = 128
self.data_workers = 6
self.initial_steps = initial_steps
self.step_size = step_size
# batch size for eval mode. Default is 1.
self.dropout = dropout
self.max_frames = max_frames
self.embeding_fea_size = embeding_fea_size
if self.is_video:
self.eval_bs = 1
self.fixed_layer = True # layer fixed when training with video clips
self.frames_per_video = 16
self.later_steps = 5
else:
self.eval_bs = 64
self.fixed_layer = False # layer not fixed when training with image datasets
self.frames_per_video = 1
self.later_steps = 2
model = models.create(self.model_name, dropout=self.dropout,
embeding_fea_size=self.embeding_fea_size, fixed_layer=self.fixed_layer)
self.model = nn.DataParallel(model).cuda()
self.criterion = ExLoss(self.embeding_fea_size, self.num_classes, t=10).cuda()
示例6: __init__
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def __init__(self, model_name, batch_size, num_classes, dataset, u_data, save_path, embeding_fea_size=1024,
dropout=0.5, max_frames=900, initial_steps=20, step_size=16):
self.model_name = model_name
self.num_classes = num_classes
self.data_dir = dataset.images_dir
self.is_video = dataset.is_video
self.save_path = save_path
self.dataset = dataset
self.u_data = u_data
self.u_label = np.array([label for _, label, _, _ in u_data])
self.label_to_images = {}
self.sort_image_by_label=[]
self.dataloader_params = {}
self.dataloader_params['height'] = 256
self.dataloader_params['width'] = 128
self.dataloader_params['batch_size'] = batch_size
self.dataloader_params['workers'] = 6
self.batch_size = batch_size
self.data_height = 256
self.data_width = 128
self.data_workers = 6
self.initial_steps = initial_steps
self.step_size = step_size
self.dropout = dropout
self.max_frames = max_frames
self.embeding_fea_size = embeding_fea_size
if self.is_video:
self.eval_bs = 1
self.fixed_layer = True
self.frames_per_video = 16
self.later_steps = 5
else:
self.eval_bs = 64
self.fixed_layer = False
self.frames_per_video = 1
self.later_steps = 2
model = models.create(self.model_name, dropout=self.dropout,
embeding_fea_size=self.embeding_fea_size, fixed_layer=self.fixed_layer)
self.model = nn.DataParallel(model).cuda()
self.criterion = ExLoss(self.embeding_fea_size, self.num_classes, t=10).cuda()
示例7: get_data
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def get_data(name, split_id, data_dir, height, width, batch_size, workers,
combine_trainval):
root = osp.join(data_dir, name)
dataset = datasets.create(name, root, split_id=split_id)
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
train_set = dataset.trainval if combine_trainval else dataset.train
num_classes = (dataset.num_trainval_ids if combine_trainval
else dataset.num_train_ids)
train_transformer = T.Compose([
T.RandomSizedRectCrop(height, width),
T.RandomHorizontalFlip(),
T.ToTensor(),
normalizer,
])
test_transformer = T.Compose([
T.RectScale(height, width),
T.ToTensor(),
normalizer,
])
train_loader = DataLoader(
Preprocessor(train_set, root=dataset.images_dir,
transform=train_transformer),
batch_size=batch_size, num_workers=workers,
shuffle=True, pin_memory=True, drop_last=True)
val_loader = DataLoader(
Preprocessor(dataset.val, root=dataset.images_dir,
transform=test_transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
test_loader = DataLoader(
Preprocessor(list(set(dataset.query) | set(dataset.gallery)),
root=dataset.images_dir, transform=test_transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
return dataset, num_classes, train_loader, val_loader, test_loader
示例8: get_data
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def get_data(name, split_id, data_dir, height, width, batch_size, num_instances,
workers, combine_trainval):
root = osp.join(data_dir, name)
dataset = datasets.create(name, root, split_id=split_id)
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
train_set = dataset.trainval if combine_trainval else dataset.train
num_classes = (dataset.num_trainval_ids if combine_trainval
else dataset.num_train_ids)
train_transformer = T.Compose([
T.RandomSizedRectCrop(height, width),
T.RandomHorizontalFlip(),
T.ToTensor(),
normalizer,
])
test_transformer = T.Compose([
T.RectScale(height, width),
T.ToTensor(),
normalizer,
])
train_loader = DataLoader(
Preprocessor(train_set, root=dataset.images_dir,
transform=train_transformer),
batch_size=batch_size, num_workers=workers,
sampler=RandomIdentitySampler(train_set, num_instances),
pin_memory=True, drop_last=True)
val_loader = DataLoader(
Preprocessor(dataset.val, root=dataset.images_dir,
transform=test_transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
test_loader = DataLoader(
Preprocessor(list(set(dataset.query) | set(dataset.gallery)),
root=dataset.images_dir, transform=test_transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
return dataset, num_classes, train_loader, val_loader, test_loader
示例9: get_data
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def get_data(name, split_id, data_dir, height, width, batch_size, num_instances,
workers, combine_trainval):
root = osp.join(data_dir, name)
dataset = datasets.create(name, root, split_id=split_id)
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
train_set = dataset.trainval if combine_trainval else dataset.train
num_classes = (dataset.num_trainval_ids if combine_trainval
else dataset.num_train_ids)
train_transformer = T.Compose([
T.RectScale(height, width),
T.RandomSizedEarser(),
T.RandomHorizontalFlip(),
T.ToTensor(),
normalizer,
])
test_transformer = T.Compose([
T.RectScale(height, width),
T.ToTensor(),
normalizer,
])
train_loader = DataLoader(
Preprocessor(train_set, root=dataset.images_dir,
transform=train_transformer),
batch_size=batch_size, num_workers=workers,
sampler=RandomMultipleGallerySampler(train_set, num_instances),
pin_memory=True, drop_last=True)
val_loader = DataLoader(
Preprocessor(dataset.val, root=dataset.images_dir,
transform=test_transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
test_loader = DataLoader(
Preprocessor(list(set(dataset.query) | set(dataset.gallery)),
root=dataset.images_dir, transform=test_transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
return dataset, num_classes, train_loader, val_loader, test_loader
示例10: get_data
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def get_data(name, split_id, data_dir, batch_size, num_instances, workers):
root = osp.join(data_dir, name)
dataset = datasets.create(name, root, split_id=split_id)
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
train_set = dataset.trainval
num_classes = dataset.num_trainval_ids
train_transformer = T.Compose([
T.RandomSizedRectCrop(256, 128),
T.RandomHorizontalFlip(),
T.ToTensor(),
normalizer,
])
test_transformer = T.Compose([
T.RectScale(256, 128),
T.ToTensor(),
normalizer,
])
train_loader = DataLoader(
Preprocessor(train_set, root=dataset.images_dir,
transform=train_transformer),
batch_size=batch_size, num_workers=workers,
sampler=RandomIdentitySampler(train_set, num_instances),
pin_memory=True, drop_last=True)
val_loader = DataLoader(
Preprocessor(dataset.val, root=dataset.images_dir,
transform=test_transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
test_loader = DataLoader(
Preprocessor(list(set(dataset.query) | set(dataset.gallery)),
root=dataset.images_dir, transform=test_transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
return dataset, num_classes, train_loader, val_loader, test_loader
示例11: get_data
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def get_data(name, data_dir, height, width, batch_size, workers):
root = osp.join(data_dir, name)
root = data_dir
dataset = datasets.create(name, root)
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
num_classes = dataset.num_train_ids
train_transformer = T.Compose([
T.RectScale(height, width),
T.RandomHorizontalFlip(),
T.ToTensor(),
normalizer,
])
test_transformer = T.Compose([
T.RectScale(height, width),
T.ToTensor(),
normalizer,
])
train_loader = DataLoader(
Preprocessor(dataset.train, root=osp.join(dataset.images_dir,dataset.train_path),
transform=train_transformer,random_mask=False),
batch_size=batch_size, num_workers=workers,
shuffle=True, pin_memory=True, drop_last=True)
query_loader = DataLoader(
Preprocessor(dataset.query, root=osp.join(dataset.images_dir,dataset.query_path),
transform=test_transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
gallery_loader = DataLoader(
Preprocessor(dataset.gallery, root=osp.join(dataset.images_dir,dataset.gallery_path),
transform=test_transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
return dataset, num_classes, train_loader, query_loader, gallery_loader
示例12: train
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def train(self, train_data, step, epochs=70, step_size=55, init_lr=0.1, dropout=0.5):
""" create model and dataloader """
model = models.create(self.model_name, dropout=self.dropout, num_classes=self.num_classes, mode=self.mode)
model = nn.DataParallel(model).cuda()
dataloader = self.get_dataloader(train_data, training=True)
# the base parameters for the backbone (e.g. ResNet50)
base_param_ids = set(map(id, model.module.CNN.base.parameters()))
# we fixed the first three blocks to save GPU memory
base_params_need_for_grad = filter(lambda p: p.requires_grad, model.module.CNN.parameters())
# params of the new layers
new_params = [p for p in model.parameters() if id(p) not in base_param_ids]
# set the learning rate for backbone to be 0.1 times
param_groups = [
{'params': base_params_need_for_grad, 'lr_mult': 0.1},
{'params': new_params, 'lr_mult': 1.0}]
criterion = nn.CrossEntropyLoss().cuda()
optimizer = torch.optim.SGD(param_groups, lr=init_lr, momentum=0.5, weight_decay = 5e-4, nesterov=True)
# change the learning rate by step
def adjust_lr(epoch, step_size):
lr = init_lr / (10 ** (epoch // step_size))
for g in optimizer.param_groups:
g['lr'] = lr * g.get('lr_mult', 1)
if epoch % step_size == 0:
print("Epoch {}, current lr {}".format(epoch, lr))
""" main training process """
trainer = Trainer(model, criterion)
for epoch in range(epochs):
adjust_lr(epoch, step_size)
trainer.train(epoch, dataloader, optimizer, print_freq=len(dataloader)//30 * 10)
torch.save(model.state_dict(), osp.join(self.save_path, "{}_step_{}.ckpt".format(self.mode, step)))
self.model = model
示例13: get_one_shot_in_cam1
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def get_one_shot_in_cam1(dataset, load_path, seed=0):
np.random.seed(seed)
random.seed(seed)
# if previous split exists, load it and return
if osp.exists(load_path):
with open(load_path, "rb") as fp:
dataset = pickle.load(fp)
label_dataset = dataset["label set"]
unlabel_dataset = dataset["unlabel set"]
print(" labeled | N/A | {:8d}".format(len(label_dataset)))
print(" unlabel | N/A | {:8d}".format(len(unlabel_dataset)))
print("\nLoad one-shot split from", load_path)
return label_dataset, unlabel_dataset
#print("random create new one-shot split and save it to", load_path)
label_dataset = []
unlabel_dataset = []
# dataset indexed by [pid, cam]
dataset_in_pid_cam = [[[] for _ in range(dataset.num_cams)] for _ in range(dataset.num_train_ids) ]
for index, (images, pid, camid, videoid) in enumerate(dataset.train):
dataset_in_pid_cam[pid][camid].append([images, pid, camid, videoid])
# generate the labeled dataset by randomly selecting a tracklet from the first camera for each identity
for pid, cams_data in enumerate(dataset_in_pid_cam):
for camid, videos in enumerate(cams_data):
if len(videos) != 0:
selected_video = random.choice(videos)
break
label_dataset.append(selected_video)
assert len(label_dataset) == dataset.num_train_ids
labeled_videoIDs =[vid for _, (_,_,_, vid) in enumerate(label_dataset)]
# generate unlabeled set
for (imgs, pid, camid, videoid) in dataset.train:
if videoid not in labeled_videoIDs:
unlabel_dataset.append([imgs, pid, camid, videoid])
with open(load_path, "wb") as fp:
pickle.dump({"label set":label_dataset, "unlabel set":unlabel_dataset}, fp)
print(" labeled | N/A | {:8d}".format(len(label_dataset)))
print(" unlabeled | N/A | {:8d}".format(len(unlabel_dataset)))
print("\nCreate new one-shot split, and save it to", load_path)
return label_dataset, unlabel_dataset
示例14: __init__
# 需要导入模块: from reid import models [as 别名]
# 或者: from reid.models import create [as 别名]
def __init__(self, model_name, batch_size, num_classes, dataset, u_data, save_path, embeding_fea_size=1024,
dropout=0.5, max_frames=900, initial_steps=20, step_size=16):
self.model_name = model_name
self.num_classes = num_classes
self.data_dir = dataset.images_dir
self.is_video = dataset.is_video
self.save_path = save_path
self.dataset = dataset
self.u_data = u_data
self.u_label = np.array([label for _, label, _, _ in u_data])
self.dataloader_params = {}
self.dataloader_params['height'] = 256
self.dataloader_params['width'] = 128
self.dataloader_params['batch_size'] = batch_size
self.dataloader_params['workers'] = 6
self.batch_size = batch_size
self.data_height = 256
self.data_width = 128
self.data_workers = 6
self.initial_steps = initial_steps
self.step_size = step_size
# batch size for eval mode. Default is 1.
self.dropout = dropout
self.max_frames = max_frames
self.embeding_fea_size = embeding_fea_size
if self.is_video:
self.eval_bs = 1
self.fixed_layer = True
self.frames_per_video = 16
self.later_steps = 5
else:
self.eval_bs = 64
self.fixed_layer = False
self.frames_per_video = 1
self.later_steps = 2
model = models.create(self.model_name, dropout=self.dropout,
embeding_fea_size=self.embeding_fea_size, fixed_layer=self.fixed_layer)
self.model = nn.DataParallel(model).cuda()
self.criterion = ExLoss(self.embeding_fea_size, self.num_classes, t=10).cuda()