从github中下载源码(https://github.com/amdegroot/ssd.pytorch),按照readme文件中的步鄹进行安装,运行。
训练
1.下载数据集VOC2007(训练,测试),VOC2012(训练),可以自己创建新的文件夹,也可以在源代码的data文件下创建小的文件夹。预训练文件(vgg16_reducedfc.pth),放置在weights文件中。
2.数据集加载路径更改:voc0712.py文件中VOC_ROOT = osp.join(HOME, "更改为自己数据集所在的路径")
使用下载的vgg16模型,需将train.py文件中更改为parser.add_argument('--basenet', default='vgg16_reducedfc.pth',help='Pretrained base model')
3.将train.py文件中第175行的images, targets = next(batch_iterator)更改为
try:
images,targets=next(batch_iterator)
except StopIteration:
batch_iterator=iter(data_loader)
images,targets=next(batch_iterator)
这样就解决的自动跳出迭代的问题
4.FileNotFoundError: [Errno 2] No such file or directory: ‘coco_labels.txt'
针对这个问题,将coco.py文件中35行的labels = open(label_file, 'r')的label_file更改为coco_labels.txt文件所在的绝对路径
5.UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.targets = [Variable(ann.cuda(), volatile=True) for ann in targets]
针对这个问题,将ssd.py文件中第34行
self.priors = Variable(self.priorbox.forward(), volatile=True)
更改为
with torch.no_grad():
self.priors = Variable(self.priorbox.forward())
6.RuntimeError: CUDA out of memory.将batch_size改小
损失函数出现nan的情况,将学习率改小
7.IndexError: The shape of the mask [2, 8732] at index 0 does not match the shape of the indexed tensor [17464, 1] at index 0
在multibox_loss.py文件第97行loss_c[pos] = 0添加一行代码
loss_c=loss_c.view(pos.size()[0],pos.size()[1])
8.IndexError: invalid index of a 0-dim tensor. Use `tensor.item()` in Python or `tensor.item<T>()` in C++ to convert a 0-dim tensor to a number
将train.py文件中的data[0]改为item()
至此,train.py文件可以跑通,迭代次数以及保存文件等操作可以自己在train.py文件中更改。
验证
1.RuntimeError: Legacy autograd function with non-static forward method is deprecated. Please use new-style autograd function with static forward method.
将ssd.py文件第99-104行
if self.phase == "test":
output = self.detect(
loc.view(loc.size(0), -1, 4),
self.softmax(conf.view(conf.size(0), -1,
self.num_classes)),
self.priors.type(type(x.data))
更改为
if self.phase == "test":
output = self.detect。forward(
loc.view(loc.size(0), -1, 4),
self.softmax(conf.view(conf.size(0), -1,
self.num_classes)),
self.priors.type(type(x.data))
2.RuntimeError: index_select(): functions with out=... arguments don't support automatic differentiation, but one of the arguments requires grad.
box_utils.py214行idx = idx[:-1] 后面添加
idx = torch.autograd.Variable(idx, requires_grad=False)
idx = idx.data
x1 = torch.autograd.Variable(x1, requires_grad=False)
x1 = x1.data
y1 = torch.autograd.Variable(y1, requires_grad=False)
y1 = y1.data
x2 = torch.autograd.Variable(x2, requires_grad=False)
x2 = x2.data
y2 = torch.autograd.Variable(y2, requires_grad=False)
y2 = y2.data
244行inter = w*h添加
area = torch.autograd.Variable(area, requires_grad=False)
area = area.data
idx = torch.autograd.Variable(idx, requires_grad=False)
idx = idx.data
训练得到MAP.