python训练问题

1.val的性能强于train

官网迁移学习教程的训练结果也出现了这个情况,不知道怎么回事

补一下,是训练轮数太少了,增多轮数,观察acc和loss曲线,逐渐平稳的情况下一定是train比较好

2.RuntimeError: output with shape [1, 256, 256] doesn‘t match the broadcast shape [3, 256, 256]【报错】

image = Image.open(image_name)改为image = Image.open(image_name).convert('RGB')

class MyDataset(Dataset):
    def __init__(self, dirname, transform=None):
        super(MyDataset, self).__init__()
        self.classes = os.listdir(dirname)
        self.images = []
        self.transform = transform
        for i, classes in enumerate(self.classes):
            classes_path = os.path.join(dirname, classes)
            for image_name in os.listdir(classes_path):
                self.images.append((os.path.join(classes_path, image_name), i))
    def __len__(self):
        return len(self.images)
    def __getitem__(self, idx):
        image_name, classes = self.images[idx]
        image = Image.open(image_name).convert('RGB')
        if self.transform:
            image = self.transform(image)
        return image, classes
    def get_claesses(self):
        return self.classes
# 分布实现训练和预测的transform
train_transform = transforms.Compose([
    #transforms.Grayscale(3),
    transforms.RandomResizedCrop(224), #随机裁剪一个area然后再resize
    transforms.RandomHorizontalFlip(), #随机水平翻转
    #transforms.Resize(size=(256, 256)),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
val_transform = transforms.Compose([
    #transforms.Grayscale(3),
    transforms.Resize(size=(256, 256)),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
# 分别实现loader
train_dataset = MyDataset('./hymenoptera_data/train/', train_transform)
train_loader = DataLoader(train_dataset, shuffle=True, batch_size=64 ,num_workers=3)
val_dataset = MyDataset('./hymenoptera_data/val/', val_transform)
val_loader = DataLoader(val_dataset, shuffle=True, batch_size=64,num_workers=3)

3. 当代码定义test,pycharm默认进入pytest模式,所以要尽量避免使用test定义模块

def test():
    print('\n Accuracy: {}/{} ({:.0f}%)\n'.format(correct, N, 100 * acc))

if __name__ == '__main__':
    test()

4.python中imshow()和show()的区别

plt.imshow(image)  #image表示待处理的图像
plt.show()
#plt.imshow()函数负责对图像进行处理,并显示其格式,而plt.show()则是将plt.imshow()处理后的函数显示出来。

4.list包含多个tensor,tensor长度不一致,整体转tensor

   stack只适用于每个tensor维度一致,但我最后一个tensor和前边不一样

[tensor([0, 0, 0, 0, 0, 0, 0, 0]), tensor([0, 0, 0, 0, 0, 0, 0, 0]), tensor([0, 0, 0, 0, 0, 0, 0, 0]), tensor([0, 0, 0, 0, 0, 0, 0, 0]), tensor([0, 0, 0, 0, 0, 0, 1, 1]), tensor([1, 1, 1, 1, 1, 1, 1, 1]), tensor([1, 1, 1, 1, 1, 1, 1, 1]), tensor([1, 1, 1, 1, 1, 1, 1, 1]), tensor([1, 1, 1, 1, 1, 1, 1, 1]), tensor([1, 1, 1, 1, 1, 1])]

tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1])

       
        labels = torch.cat(tuple(labels), 0)
        print(self.labels)
        print(type(self.labels))
 labels = np.asarray(labels,order='C')#从别的博客看到这个也可以,我没有验证

 self.labels.extend(label.tolist())#tensor变list,最终组成一维list

5.dropout

dropout在解决过拟合的问题中确实是一个利器,但是dropout取值过大一样会overfitting,一般在0.3-0.5

6. list形式不能直接传入GPU

    train_acc = torch.tensor(train_acc)
    train_loss = torch.tensor(train_loss)
    val_loss = torch.tensor(val_loss)
    val_acc = torch.tensor(val_acc)

7. AttributeError: ‘_IncompatibleKeys‘ object has no attribute ‘parameters‘

model = model.load_state_dict(torch.load(model_weight_path))#载入模型参数

out = model(input)



改一下

model.load_state_dict(torch.load(model_weight_path))

out = model(input)

8、 model 'pil.image' has no attribute 'Transpose'

pillow的版本问题,过高版本用第一个

    #ng_mirror90 = ng_rotate.transpose(Image.FLIP_LEFT_RIGHT)  # 左右对换。
    ng_mirror90 = ng_rotate.transpose(Image.Transpose.FLIP_LEFT_RIGHT)

9、当数据集过小,解冻训练的结果反而不如冻结分类前的所有层

10、RuntimeError: expected scalar type Long but found Int

loss的格式出现了问题,把下面的代码改成上面的就行

      #loss = loss_function(pred, labels.type(torch.LongTensor).to(device))
        loss = loss_function(pred, labels.to(device))

12、数据增强的常用方法

图像的翻转和旋转

图像的平移变换

图像的放缩变换

图像的随机剪裁

添加噪声干扰

图像的光照变换

13、优化对比实验

激活函数

梯度下降算法

学习率、Dropout、batch_size

14、类别激活热力图grad-cam(pytorch) 

AttributeError: 'GradCAM' object has no attribute 'activations_and_grads'

target_layer = model.layer4[-1] 改成下一行
#target_layer = [model.layer4]

 

你可能感兴趣的:(python,深度学习,机器学习)