Arhazf
(Sophia)
September 20, 2019, 5:32am
1
Hi,
Here I have a simple 2-layers fully connected neural network, but I am not sure how I can convert input images to binary form in pytorch? Thank you in advance.
Data_tr = datasets.MNIST('../data', train=True, download=True,
transform=transforms.Compose([
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))]))
train_set = torch.utils.data.DataLoader(Data_tr, batch_size=batch_size, shuffle=True)
Data_ts = datasets.MNIST('../data', train=False, transform=transforms.Compose([
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))]))
test_set = torch.utils.data.DataLoader(Data_ts, batch_size=batch_size, shuffle=True)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(28 * 28, 64)
self.fc2 = nn.Linear(64, 10)
#W1 = list(self.fc1.parameters())
#print("w1:", len(W1[1]))
nn.init.xavier_normal_(self.fc1.weight)
nn.init.xavier_normal_(self.fc2.weight)
def forward(self, x):
x = F.relu(self.fc1(x))
x = self.fc2(x)
return F.log_softmax(x, dim=1)
net = Net()
print(net)
mailcorahul
(Raghul Asokan)
September 20, 2019, 8:30am
2
By binary form, do you mean thresholding images(only 0 and 1) rather than using them as grayscale images?
If thatās the case, I donāt think transforms has a function to threshold an image. You can write a custom dataset class which converts the image to binary form
class BinaryMNIST(Dataset):
def __init__(self):
self.images , self.labels = ##using datasets.MNIST
def __getitem__(self, idx):
image, label = self.images[idx], self.labels[idx]
image[image < 127] = 0
image[image >= 127] = 255
# do the necessary transforms ...
return image, label
Arhazf
(Sophia)
September 20, 2019, 10:33pm
3
Thank you for your response.
Sorry I am a beginer and I have some additional questions:
What is Dataset in the first line? Should I write the train_set instead? or should I define a new dataset?
What do you mean āusing datasets.MNISTā in the def__init__ function? According to my code, I wrote self.images , self.labels = Data_tr but I got an error.
mailcorahul
(Raghul Asokan)
September 21, 2019, 6:31am
4
āDatasetā here is an abstract class defined in torch.utils.data which should be subclassed when creating a custom dataset class - https://pytorch.org/docs/stable/data.html?highlight=dataset#torch.utils.data.Dataset .
binary_mnist = BinaryMNIST()
train_loader = torch.utils.data.DataLoader(binary_mnist, batch_size=batch_size, shuffle=True)
You can do dir(Data_tr) to check for the attributes. It has two variables - train_data and train_labels.
Assign them accordingly inside init method.
self.images, self.labels = Data_tr.train_data, Data_tr.train_labels
Check out this link for dataset/dataloader tutorial:
https://pytorch.org/tutorials/beginner/data_loading_tutorial.html
Arhazf
(Sophia)
September 21, 2019, 10:44pm
5
Thank you very much. It is solved.
1 Like
anu
(Nihat)
April 14, 2021, 9:56am
6
mailcorahul:
class BinaryMNIST(Dataset):
def __init__(self):
self.images , self.labels = ##using datasets.MNIST
def __getitem__(self, idx):
image, label = self.images[idx], self.labels[idx]
image[image < 127] = 0
image[image >= 127] = 255
# do the necessary transforms ...
return image, label
hello @mailcorahul
do you think normalizing would make sense if I binarize the MNIST set?
thanks in advance!