#Why is this error occurred when inputting gray image ？
Reshape image in 3 dimensions (height = 28px, width = 28px, canal = 1)
train_x= train.values.reshape(-1, 28, 28, 1).astype(‘uint8’)
#train_x= np.stack((train_x,)*3, axis=-1)
val_x = val.values.reshape(-1, 28, 28, 1).astype(‘uint8’)
#val_x= np.stack((val_x,)*3, axis=-1)
test_x = test.values.reshape(-1, 28, 28, 1).astype(‘uint8’)
#test_x= np.stack((test_x,)*3, axis=-1)
training 時做 data augmentation
train_transform = transforms.Compose([transforms.ToPILImage(),#一个现在还理解不了的操作
transforms.RandomHorizontalFlip(),#随机将图片水平翻转
transforms.RandomRotation(15),#随机旋转图片，15
transforms.ToTensor(),#将图片转成tensor，并把数值normalize到[0,1](data normalization),这里最后为啥还要加个，
])
test_transform=transforms.Compose([transforms.ToPILImage(),
transforms.ToTensor(),
])
class ImgDataset(Dataset):
def init(self,x,y=None,transform=None):
self.x=x
#label is required to be a Longtensor
self.y=y
if y is not None:
self.y =torch.LongTensor(y)
self.transform =transform
def len(self):
return len(self.x)
def getitem(self,index):
X=self.x[index]
if self.transform is not None:
X=self.transform(X)
if self.y is not None:
Y =self.y[index]
return X,Y
else:
return X
batch_size = 128
train_set=ImgDataset(train_x,train_y,train_transform)
val_set = ImgDataset(val_x,val_y,test_transform)
train_loader=DataLoader(train_set,batch_size=batch_size,shuffle=True)
val_loader=DataLoader(val_set,batch_size=batch_size,shuffle=False)