Closed TaihuLight closed 6 years ago
Hi @TaihuLight, you will get a syntax error if you unpack dictionaries in this manner with any python version older than 3.5 (it’s mentioned in the readme but I will update it to make this clearer).
The pre-trained pedestrian alignment model has been ported to pytorch and uploaded here: http://www.robots.ox.ac.uk/~albanie/pytorch-models.html#pedestrian-alginment.
One thing to be aware of is that the authors of the pedestrian alignment network apply local response normalization to the network features before performing the re-identification task. There doesn’t yet seem to be an official implementation of this in pytorch, although it looks like it is in progress here. It should be fairly simple to write your own or find one online, but you will need this function to reproduce their experiments.
Thank you! BTW, how can I solve the error,SyntaxError: invalid syntax in line 277?
If you just wanted to fix this line, you could switch the order of the arguments to be compatible with Python 2.7:
arch = sg.build_header_str(self.name, debug_mode=self.debug_mode, **self.meta)
However, I would recommend running the tool with Python 3.5, since there are likely to be other parts of the program that use similar syntax.
Sorry, I forgot your tips in the readme.
I try to separately load the net structure and parameters of the converted model from matconvnet as follow,but I get the errors, how to correctly do it ?And i think the error arises from the wrong defined model variable? But how to correct it?
import torchvision.models as models import torch import torch.nn as nn
pretrained=False model = models.resnet50(pretrained) modelpath="/home/chengzi/Downloads/netvlad_v103/pre-trained-models/pedestrian_alignment.pth" checkpoint = torch.load(modelpath) net=model.load_state_dict(checkpoint) print(net)`
Traceback (most recent call last):
File "/home/chengzi/workspace/demo/model_s.py", line 13, in
The code you have given above will try to load the pretrained weights directly into the resnet50
architecture. Since the pedestrian alignment network has some modifications, unfortunately this won't work. There is a function provided for loading the model (below the network definition in ped_align.py
which looks like this:
def ped_align(weights_path=None, **kwargs):
"""
load imported model instance
Args:
weights_path (str): If set, loads model weights from the given path
"""
model = Ped_align()
if weights_path:
state_dict = torch.load(weights_path)
model.load_state_dict(state_dict)
return model
So to load the model, you just need to supply the weights path and call this function. E.g.
from ped_align import ped_align
weights_path = '../ped_align.pth'
net = ped_align(weights_path)
Thank you!
The .pth file very probably only contains the trained parameter values. That is the recommended way of saving a model. So you need to create the network structure in your code (or borrow their code) and then load the weights. Here is how their code to load their saved model http://www.robots.ox.ac.uk/~albanie/models/pytorch-mcn/ped_align.py
After create the network structure with the above code ped_align.py, the .pth pretrained model can be load, but get the follow warning. I fix it according to fix unbroadcastable UserWarning in inception.py, but it is failed. /usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py:482: UserWarning: src is not broadcastable to dst, but they have the same number of elements. Falling back to deprecated pointwise behavior. ownstate[name].copy(param)
Answer: https://discuss.pytorch.org/t/how-to-load-net-structure-of-the-model-and-its-parameters-from-the-pretrained-pth-with-pytrch/12364/6?u=taihulight I think you can ignore the warning. The model was probably saved in a previous version of pytorch and there has probably been of a slight change in behaviour in some part of pytorch. The warning occurs for res2a_branch2a.weight, which is of shape (64, 64, 1, 1), but got saved shape (64, 64). It looks to me like they are compatible and that a pointwise copy would work equivalently to the suggested fix. I wondered why only one instance of a Conv2d caused such a warning when the model contains many, and interestingly enough, there is only one Conv2d in the entire model with in_channels==out_channels and kernel_size=[1, 1] and stride=(1, 1). Maybe the shape of the weight array in this specific case has been changed in a recent update to pytorch.
I'm sorry, I don't quite understand this comment. Could you explain it again?
I means that you should upload the corresponding model definition files *.py (eg. http://www.robots.ox.ac.uk/~albanie/models/pytorch-mcn/ped_align.py )for the published Pytorch models on the page http://www.robots.ox.ac.uk/~albanie/pytorch-models.html .Thus, the users will not need to rewrite the network models with Pytorch code!
Are any of them missing? Each of the uploaded models is supposed to have a model definition script (.py
) and a set of weights (.pth
).
I am sorry, I get it now!
models[f'model_{fold}{epoch}'] = model.state_dict() ^ SyntaxError: invalid syntax
i am getting error and using python 3.5
with timer('train'): splits = list( StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=seed).split(train_x, y_binary)) splits_test = list(KFold(n_splits=n_splits, shuffle=True, random_state=seed).split(test_x))
for fold, ((train_idx, valid_idx), (train_idx_test, _)) in enumerate(zip(splits, splits_test)):
x_train_fold = np.concatenate((train_x[train_idx], test_x[train_idx_test]), axis=0)
y_train_fold = np.concatenate((train_y[train_idx], test_y[train_idx_test]), axis=0)
x_valid_fold = train_x[valid_idx]
y_valid_fold = train_y[valid_idx]
valid_nan_mask = train_nan_mask[valid_idx]
y_valid_fold_binary = y_binary[valid_idx]
y_valid_fold_identity_binary = y_identity_binary[valid_idx]
evaluator = JigsawEvaluator(y_valid_fold_binary, y_valid_fold_identity_binary)
train_dataset = TextDataset(x_train_fold, targets=y_train_fold, maxlen=max_len)
valid_dataset = TextDataset(x_valid_fold, targets=y_valid_fold, maxlen=max_len)
train_sampler = BucketSampler(train_dataset, train_dataset.get_keys(),
bucket_size=batch_size * 20, batch_size=batch_size)
valid_sampler = BucketSampler(valid_dataset, valid_dataset.get_keys(),
batch_size=batch_size, shuffle_data=False)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=False,
sampler=train_sampler, num_workers=0, collate_fn=collate_fn)
valid_loader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=False,
sampler=valid_sampler, collate_fn=collate_fn)
model = NeuralNet(embedding_matrix).to(device)
ema_model = copy.deepcopy(model)
ema_model.eval()
ema_n = int(len(train_loader.dataset) / (updates_per_epoch * batch_size))
ema = EMA(model, mu, n=ema_n)
scale_fn = combine_scale_functions(
[partial(scale_cos, 1e-4, 5e-3), partial(scale_cos, 5e-3, 1e-3)], [0.2, 0.8])
optimizer = torch.optim.Adam(model.parameters(), lr=0.005)
scheduler = ParamScheduler(optimizer, scale_fn, train_epochs * len(train_loader))
all_valid_preds = []
all_test_preds = []
for epoch in range(train_epochs):
start_time = time.time()
model.train()
for _, x_batch, y_batch in train_loader:
x_batch = x_batch.to(device)
y_batch = y_batch.to(device)
scheduler.batch_step()
y_pred = model(x_batch)
loss = nn.BCEWithLogitsLoss(weight=y_batch[:, 1])(y_pred[:, 0], y_batch[:, 0])
optimizer.zero_grad()
loss.backward()
optimizer.step()
ema.on_batch_end(model)
valid_preds = eval_model(model, valid_loader)
valid_preds[valid_nan_mask] = nan_pred
all_valid_preds.append(valid_preds)
auc_score = evaluator.get_final_metric(valid_preds)
elapsed_time = time.time() - start_time
print('Epoch {}/{} \t auc={:.5f} \t time={:.2f}s'.format(
epoch + 1, train_epochs, auc_score, elapsed_time))
test_preds = eval_model(model, test_loader)
all_test_preds.append(test_preds)
models[f'model_{fold}{epoch}'] = model.state_dict()
ema.on_epoch_end(model)
PLease suggest on the above and also getting the error in below function def clean_text(x): x = str(x) for punct in puncts + list(string.punctuation): if punct in x: x = x.replace(punct, f' {punct} ') return x
please advice TaggingAnalysis.zip
When I imports the trained MatConvNet model https://pan.baidu.com/s/1kWpxAtT into Pytorch, which is trained with code https://github.com/layumi/Pedestrian_Alignment , it get the error as follow: can you import it, verify and public it on your web? importing net-epoch-23 flattening at Exporting MatConvNet model to PyTorch (may take some time)... File "/home/chengzi/pytorch-mcn/python/importer.py", line 277 arch = sg.build_header_str(self.name, **self.meta, debug_mode=self.debug_mode) ^ SyntaxError: invalid syntax