参考教程:https://blog.csdn.net/Layumi1993/article/details/84849506
https://zhuanlan.zhihu.com/p/50387521
一、需要安装的软件包
1.Python 3.6
2.Numpy
3.Pytorch 0.3+ (http://pytorch.org/)
方法一:
conda install torchvision -c pytorch
pip install torchvision
方法二:
Torchvision from the source
git clone https://github.com/pytorch/vision
cd vision
python setup.py install
额外补充包:
matplotlib
pretrainedmodels
ps:安装之前修复时间问题:
https://blog.csdn.net/jiuliang1916/article/details/80046977
二、训练
1.准备数据集 (python prepare.py)
那么现在打开刚刚下载的代码prepare.py。 将第五行的地址改为你本地的地址,比如 \home\zzd\Download\Market,然后在终端中跑一下。
python prepare.py
2. Build Neural Network (model.py)
我们可以利用预训练的模型。普遍来说,利用ImageNet预训练的网络能达到更好的结果,因为它保留了一些好的特征。
在pytorch里,我们可以通过两行代码来引入他们。
from torchvision import models
model = models.resnet50(pretrained=True)
你可以使用下面这行代码来简单检查网络结构
print(model)
但在实际使用中,我们需要修改网络。因为Market1501中有751个种类(不同的人)。 而不是像ImageNet一样有1000类。所以我们需要改变我们的模型来训练我们的分类器。
3: 训练 (python train.py)
好的。现在我们准备好了训练数据 和定义好的网络结构。
我们可以输入如下命令开始训练:
python train.py --gpu_ids 0 --name ft_ResNet50 --train_all --batchsize 32 --data_dir your_data_path
–gpu_ids which gpu to run.
–name the name of the model.
–data_dir the path of the training data.
–train_all using all images to train.
–batchsize batch size.
–erasing_p random erasing probability.
4.测试
(1) 特征提取 (python test.py)
这一部分, 我们载入我们刚刚训练的模型 来抽取每张图片的视觉特征
python test.py --gpu_ids 0 --name ft_ResNet50 --test_dir your_data_path --batchsize 32 --which_epoch 59
–gpu_ids which gpu to run.
–name the dir name of the trained model.
–batchsize batch size.
–which_epoch select the i-th model.
–data_dir the path of the testing data.
(2)评测
是的,现在我们有了每张图片的特征。 我们需要做的事情只有用特征去匹配图像。
python evaluate_gpu.py
5.一个简单的可视化程序 (python demo.py)
可视化结果,
python demo.py --query_index 777
–query_index which query you want to test. You may select a number in the range of 0 ~ 3367.
代码类似 evaluate.py. 我们加入了可视化的部分。