pytorch部分问题

torchvision.models.inception_v3()加载问题

import torchvision.models as models
inception = models.inception_v3(pretrained=args.pt_imagenet, aux_logits=False)

原因根源在于scipy库。torchvision在加载完模型后需要对模型进行参数初始化,用到了scipy中的stats.truncnorm.rvs方法,而该方法在scipy1.4以上版本中速度变慢了100倍,导致torchvision运行时间变长。
将scipy版本从1.4.1降到1.2.1,问题解决

pip install scipy==1.2.1

预训练模型下载太慢问题

mkdir /home/用户名/.cache/torch
mkdir /home/用户名/.cache/torch/checkpoints

然后将下载好的模型文件放在该目录下

requires_grad

for i, child in enumerate(self.model.children()):
    if i < min(args.num_untrainable_blocks, num_untrainable_blocks_max):
        for param in child.parameters():
            param.requires_grad = False

requires_grad默认为True,需要更新梯度。

分类指标准确率(Precision)和正确率(Accuracy)的区别

  • 正确率(accuracy)
    正确率是我们最常见的评价指标,accuracy = (TP+TN)/(P+N),这个很容易理解,就是被分对的样本数除以所有的样本数,通常来说,正确率越高,分类器越好
  • 精度(precision)
    精度是精确性的度量,表示被分为正例的示例中实际为正例的比例,precision=TP/(TP+FP)
  • 召回率(recall)
    召回率是覆盖面的度量,度量有多个正例被分为正例,recall=TP/(TP+FN)=TP/P,可以看到召回率与灵敏度是一样的

为什么使用mean = [0.485, 0.456, 0.406] std = [0.229, 0.224, 0.225]

Using the mean and std of Imagenet is a common practice. They are calculated based on millions of images. If you want to train from scratch on your own dataset, you can calculate the new mean and std. Otherwise, using the Imagenet pretrianed model with its own mean and std is recommended.

如果使用的数据量比较小,可以将数据先加载到内存中

可以保存为.pkl文件,然后读取文件。

def load_img(in_path, size):
    img = cv2.imread(in_path)
    if img is None:
        img = cv2.VideoCapture(in_path)
        _, img = img.read()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.resize(img, (size, size))
    return img

dataset_root = '/data/Dataset/tags/form/pets/'
datasets = ['val', 'test']
img_size = {'val': 224, 'test':224}
# img_size = {'train': 333, 'val': 299, 'test':299}
# img_size = {'train': 378, 'val': 331, 'test':331}
for dataset in datasets:
    print('dataset {} in processing ...'.format(dataset))
    file = 'list_{}.txt'.format(dataset)
    dicts = dict()
    list_names = list()
    list_imgs = list()
    list_labels = list()
    list_lines = list()
    with open(file, 'r') as f:
        for i, line in enumerate(f):
            print('\rprocessing {} in {} ...'.format(i, dataset), end='')
            line = line.strip().split()
            list_names.append(line[0])
            list_labels.append(line[1])
            list_lines.append(i)
            in_path = os.path.join(dataset_root, line[0])
            img = load_img(in_path, img_size[dataset])
            list_imgs.append(img)
    dicts['names'] = list_names
    dicts['imgs'] = list_imgs
    dicts['labels'] = list_labels
    dicts['lines'] = list_lines
    out_path = 'data_{}_{}.pkl'.format(dataset, img_size['val'])
    pickle.dump(dicts, open(out_path, 'wb'))
    print('\r\ndataset {} done.\r\n'.format(dataset))

你可能感兴趣的:(Pytorch)