datadir= ‘./archive/chest_xray/chest_xray’
traindir = datadir + ‘/train/’
validdir = datadir + ‘/val/’
testdir = datadir + ‘/test/’
​
categories = []
img_categories = []
n_train = []
n_valid = []
n_test = []
hs = []
ws = []
​
for d in os.listdir(traindir):
if not d.startswith(’.’):
categories.append(d)
​
train_imgs = os.listdir(traindir + d)
valid_imgs = os.listdir(validdir + d)
test_imgs = os.listdir(testdir + d)
n_train.append(len(train_imgs))
n_valid.append(len(valid_imgs))
n_test.append(len(test_imgs))
​
    for i in train_imgs:
        if not i.startswith('.'):
            img_categories.append(d)
            img = Image.open(traindir + d + '/' + i)
            img_array = np.array(img)
            hs.append(img_array.shape[0])
            ws.append(img_array.shape[1])

​
cat_df = pd.DataFrame({‘category’: categories,
‘n_train’: n_train,
‘n_valid’: n_valid, ‘n_test’: n_test}).
sort_values(‘category’)
​
image_df = pd.DataFrame({
‘category’: img_categories,
‘height’: hs,
‘width’: ws
})
data = {
‘train’:
datasets.ImageFolder(root=traindir, transform=image_transforms[‘train’]),
‘val’:
datasets.ImageFolder(root=validdir, transform=image_transforms[‘val’]),
‘test’:
datasets.ImageFolder(root=testdir, transform=image_transforms[‘test’])
}
​
dataloaders = {
‘train’: DataLoader(data[‘train’], batch_size=batch_size, shuffle=True),
‘val’: DataLoader(data[‘val’], batch_size=batch_size, shuffle=True),
‘test’: DataLoader(data[‘test’], batch_size=batch_size, shuffle=True)
}
trainiter = iter(dataloaders[‘train’])
features, labels = next(trainiter)
features.shape, labels.shape
(torch.Size([20, 3, 224, 224]), torch.Size([20]))
n_classes = len(cat_df)
I am using Kaggle’s Chest XRay dataset. It looks like this in folder. chest_xray((train(NORMAL,PNEUMONIA)),(val(NORMAL,PNEUMONIA)),(test(NORMAL,PNEUMONIA)))