【发布时间】:2021-12-16 04:25:36
【问题描述】:
我正在使用 pytorch 开发一个 CNN。我的模型在没有增强的情况下在训练集和测试集上都提供了很好的准确性,但我想学习增强,所以我使用了 torchvision 变换进行增强,并且在应用增强模型后开始表现最差,损失并没有减少。所以我尝试调试并观察到增强图像看起来失真/出乎意料有人可以帮我解决这个问题。
自定义数据集
class traindataset(Dataset):
def __init__(self,data,train_end_idx,augmentation = None):
'''
data: data is a pandas dataframe generated from csv file where it has columns-> [name,labels,col 1,col2,...,col784]. shape of data->(10000, 786)
'''
self.data=data
self.augmentation=augmentation
self.train_end=train_end_idx
self.target=self.data.iloc[:self.train_end,1].values
self.image=self.data.iloc[:self.train_end,2:].values#contains full data
def __len__(self):
return len(self.target);
def __getitem__(self,idx):
self.target=self.target
self.ima=self.image[idx].reshape(1,784) #only takes the selected index
if self.augmentation is not None:
self.ima = self.augmentation(self.ima)
return torch.tensor(self.target[idx]),self.ima
使用增强
torchvision_transform = transforms.Compose([
np.uint8,
transforms.ToPILImage(),
transforms.Resize((28,28)),
transforms.RandomRotation([45,135]),
transforms.ToTensor()
])
增强图像(图片的 PFA)
transformed=torchvision_transform(x)
plt.imshow(transformed.squeeze().numpy(), interpolation='nearest')
plt.show()
普通图片
x=data.iloc[:1,2:].values
plt.imshow(x.reshape(28,28), interpolation='nearest')
plt.show()
第一张图片有增强,第二张图片没有增强。 如果你愿意,你可以使用代码here 玩,而无需下载任何东西。
【问题讨论】:
-
你确定是同一张原图吗?
-
是的,我有和它相同的原始图像@Ivan
标签: machine-learning deep-learning pytorch computer-vision torchvision