image_output_1.png
from torch.utils.data import Dataset,DataLoader

from torchvision import transforms,datasets

import matplotlib.pyplot as plt

import numpy as np

import torch

torch.manual_seed(42)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
train_transforms = transforms.Compose([transforms.Resize((224, 224)), transforms.Grayscale(), transforms.RandomHorizontalFlip(p=0.5), transforms.ToTensor()])
test_transforms = transforms.Compose([transforms.Resize((224, 224)), transforms.Grayscale(), transforms.ToTensor()])
dataset = datasets.ImageFolder('../input/brain-mri-images-for-brain-tumor-detection/brain_tumor_dataset', train_transforms)
dataloader = DataLoader(dataset, batch_size=64, shuffle=True)

dataiter = iter(dataloader)
X, Y = dataiter.next()
classes = {0: 'Negative', 1: 'Positive'}
fig, axes = plt.subplots(3, 3, figsize=(14, 14))
for i in range(3):
    for j in range(3):
        plt.sca(axes[i, j])
        idx = np.random.randint(0, 31)
        image = np.moveaxis(X[idx].numpy(), 0, 2)
        plt.title(classes[Y[idx].item()])
        plt.imshow(image, cmap='gray')
        plt.axis('off')