Hi. I’m facing a problem transforming RGB image into greyscale image.
I want to make the channel of image from 3 to 1, and the image came out as 1 channel but green-ish color, not greyscale.
Lines of codes commented out in class MyNormalize is what I have tried.
Could anybody suggest a way to transform this RGB image into greyscale&1 channel image?
By the way, in test(test_loader) method, variable image includes 16 images, but does it process each of the 16 images?
I don’t quite get how this process is working
I appreciate your cooperation. Thank you!
imgDataset = MyDataset(csv_file_path, ROOT_DIR, transform=transforms.Compose([
transforms.Resize(50),
transforms.ToTensor(),
MyNormalize()
]))
class MyNormalize:
def __call__(self, image):
shape = image.shape
'''
trans1 = transforms.ToPILImage()
trans2 = transforms.ToTensor()
image = trans1(image)
image = image.convert('L')
image = trans2(image)
'''
#image = tv.transforms.functional.to_grayscale(trans1(image), num_output_channels=1)
#image = 0.299 * image[:, :, 0] + 0.587 * image[:, :, 1] + 0.114 * image[:, :, 2]
#image = (image - np.mean(image.numpy()))/np.std(image.numpy())*16+64
return image
def test(test_loader):
correct = 0
test_loss = 0
model.eval()
with torch.no_grad():
for (image, label) in test_loader:
image, label = Variable(image.float()), Variable(label)
print("image: ", type(image))
#image: <class 'torch.Tensor'>
print("image: ", image.shape)
#image: torch.Size([16, 1, 50, 50])
print("image[0]: ", type(image[0]))
#image: <class 'torch.Tensor'>
print("image[0]: ", image[0].shape)
#image: torch.Size([1, 50, 50])
print("image[0][0]: ", type(image[0][0]))
#image: <class 'torch.Tensor'>
print("image[0][0]: ", image[0][0].shape)
#image: torch.Size([50, 50])
output = model(image)
test_loss += criterion(output, label).data
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(label.data.view_as(pred)).long().cpu().sum()
test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))