Learn practical skills, build real-world projects, and advance your career
import os
import torch
import jovian
import torchvision
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from torch.utils.data import random_split
from torchvision.transforms import ToTensor
from torchvision.datasets import ImageFolder
from torchvision.utils import make_grid
from torch.utils.data import DataLoader

data_dir = './dataset/' # Path to dataset

print(os.listdir(data_dir)) # Print folders in dataset
classes = os.listdir(data_dir + '/train') # Image Classes
print(len(classes))
['test', 'train'] 131
count = []
train_count = test_count = 0
for folder in classes:
    num_images = len(os.listdir(data_dir + '/train/' + folder))
    num_images1 = len(os.listdir(data_dir + '/test/' + folder))
    train_count+=num_images
    test_count+=num_images1
    count.append(num_images)
    print(f'There are {num_images} training images and {num_images1} test images in the {folder} category.')
print(f'\nThere are {train_count} and {test_count} number of training and test images respectively.')
There are 492 training images and 164 test images in the Apple Braeburn category. There are 444 training images and 148 test images in the Apple Crimson Snow category. There are 480 training images and 160 test images in the Apple Golden 1 category. There are 492 training images and 164 test images in the Apple Golden 2 category. There are 481 training images and 161 test images in the Apple Golden 3 category. There are 492 training images and 164 test images in the Apple Granny Smith category. There are 456 training images and 152 test images in the Apple Pink Lady category. There are 492 training images and 164 test images in the Apple Red 1 category. There are 492 training images and 164 test images in the Apple Red 2 category. There are 429 training images and 144 test images in the Apple Red 3 category. There are 490 training images and 166 test images in the Apple Red Delicious category. There are 492 training images and 164 test images in the Apple Red Yellow 1 category. There are 672 training images and 219 test images in the Apple Red Yellow 2 category. There are 492 training images and 164 test images in the Apricot category. There are 427 training images and 143 test images in the Avocado category. There are 491 training images and 166 test images in the Avocado ripe category. There are 490 training images and 166 test images in the Banana category. There are 450 training images and 152 test images in the Banana Lady Finger category. There are 490 training images and 166 test images in the Banana Red category. There are 450 training images and 150 test images in the Beetroot category. There are 462 training images and 154 test images in the Blueberry category. There are 490 training images and 166 test images in the Cactus fruit category. There are 492 training images and 164 test images in the Cantaloupe 1 category. There are 492 training images and 164 test images in the Cantaloupe 2 category. There are 490 training images and 166 test images in the Carambula category. There are 702 training images and 234 test images in the Cauliflower category. There are 492 training images and 164 test images in the Cherry 1 category. There are 738 training images and 246 test images in the Cherry 2 category. There are 738 training images and 246 test images in the Cherry Rainier category. There are 492 training images and 164 test images in the Cherry Wax Black category. There are 492 training images and 164 test images in the Cherry Wax Red category. There are 492 training images and 164 test images in the Cherry Wax Yellow category. There are 450 training images and 153 test images in the Chestnut category. There are 490 training images and 166 test images in the Clementine category. There are 490 training images and 166 test images in the Cocos category. There are 450 training images and 150 test images in the Corn category. There are 462 training images and 154 test images in the Corn Husk category. There are 392 training images and 130 test images in the Cucumber Ripe category. There are 468 training images and 156 test images in the Cucumber Ripe 2 category. There are 490 training images and 166 test images in the Dates category. There are 468 training images and 156 test images in the Eggplant category. There are 702 training images and 234 test images in the Fig category. There are 297 training images and 99 test images in the Ginger Root category. There are 490 training images and 166 test images in the Granadilla category. There are 984 training images and 328 test images in the Grape Blue category. There are 492 training images and 164 test images in the Grape Pink category. There are 490 training images and 166 test images in the Grape White category. There are 490 training images and 166 test images in the Grape White 2 category. There are 492 training images and 164 test images in the Grape White 3 category. There are 471 training images and 158 test images in the Grape White 4 category. There are 490 training images and 166 test images in the Grapefruit Pink category. There are 492 training images and 164 test images in the Grapefruit White category. There are 490 training images and 166 test images in the Guava category. There are 464 training images and 157 test images in the Hazelnut category. There are 490 training images and 166 test images in the Huckleberry category. There are 490 training images and 166 test images in the Kaki category. There are 466 training images and 156 test images in the Kiwi category. There are 471 training images and 157 test images in the Kohlrabi category. There are 490 training images and 166 test images in the Kumquats category. There are 492 training images and 164 test images in the Lemon category. There are 490 training images and 166 test images in the Lemon Meyer category. There are 490 training images and 166 test images in the Limes category. There are 490 training images and 166 test images in the Lychee category. There are 490 training images and 166 test images in the Mandarine category. There are 490 training images and 166 test images in the Mango category. There are 426 training images and 142 test images in the Mango Red category. There are 300 training images and 102 test images in the Mangostan category. There are 490 training images and 166 test images in the Maracuja category. There are 738 training images and 246 test images in the Melon Piel de Sapo category. There are 492 training images and 164 test images in the Mulberry category. There are 492 training images and 164 test images in the Nectarine category. There are 480 training images and 160 test images in the Nectarine Flat category. There are 654 training images and 218 test images in the Nut Forest category. There are 534 training images and 178 test images in the Nut Pecan category. There are 450 training images and 150 test images in the Onion Red category. There are 445 training images and 155 test images in the Onion Red Peeled category. There are 438 training images and 146 test images in the Onion White category. There are 479 training images and 160 test images in the Orange category. There are 492 training images and 164 test images in the Papaya category. There are 490 training images and 166 test images in the Passion Fruit category. There are 492 training images and 164 test images in the Peach category. There are 738 training images and 246 test images in the Peach 2 category. There are 492 training images and 164 test images in the Peach Flat category. There are 492 training images and 164 test images in the Pear category. There are 696 training images and 232 test images in the Pear 2 category. There are 490 training images and 166 test images in the Pear Abate category. There are 702 training images and 234 test images in the Pear Forelle category. There are 300 training images and 102 test images in the Pear Kaiser category. There are 490 training images and 166 test images in the Pear Monster category. There are 666 training images and 222 test images in the Pear Red category. There are 711 training images and 237 test images in the Pear Stone category. There are 490 training images and 166 test images in the Pear Williams category. There are 490 training images and 166 test images in the Pepino category. There are 444 training images and 148 test images in the Pepper Green category. There are 702 training images and 234 test images in the Pepper Orange category. There are 666 training images and 222 test images in the Pepper Red category. There are 666 training images and 222 test images in the Pepper Yellow category. There are 492 training images and 164 test images in the Physalis category. There are 492 training images and 164 test images in the Physalis with Husk category. There are 490 training images and 166 test images in the Pineapple category. There are 493 training images and 163 test images in the Pineapple Mini category. There are 490 training images and 166 test images in the Pitahaya Red category. There are 447 training images and 151 test images in the Plum category. There are 420 training images and 142 test images in the Plum 2 category. There are 900 training images and 304 test images in the Plum 3 category. There are 492 training images and 164 test images in the Pomegranate category. There are 450 training images and 153 test images in the Pomelo Sweetie category. There are 450 training images and 150 test images in the Potato Red category. There are 453 training images and 151 test images in the Potato Red Washed category. There are 450 training images and 150 test images in the Potato Sweet category. There are 450 training images and 150 test images in the Potato White category. There are 490 training images and 166 test images in the Quince category. There are 492 training images and 164 test images in the Rambutan category. There are 490 training images and 166 test images in the Raspberry category.
There are 492 training images and 164 test images in the Redcurrant category. There are 490 training images and 162 test images in the Salak category. There are 492 training images and 164 test images in the Strawberry category. There are 738 training images and 246 test images in the Strawberry Wedge category. There are 490 training images and 166 test images in the Tamarillo category. There are 490 training images and 166 test images in the Tangelo category. There are 738 training images and 246 test images in the Tomato 1 category. There are 672 training images and 225 test images in the Tomato 2 category. There are 738 training images and 246 test images in the Tomato 3 category. There are 479 training images and 160 test images in the Tomato 4 category. There are 492 training images and 164 test images in the Tomato Cherry Red category. There are 684 training images and 228 test images in the Tomato Heart category. There are 367 training images and 127 test images in the Tomato Maroon category. There are 474 training images and 158 test images in the Tomato not Ripened category. There are 459 training images and 153 test images in the Tomato Yellow category. There are 735 training images and 249 test images in the Walnut category. There are 475 training images and 157 test images in the Watermelon category. There are 67692 and 22688 number of training and test images respectively.

1. Data Preparation


Preprocess the input images
import torchvision.transforms as tt

# stats = ((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
stats = ((0.6840, 0.5786, 0.5037), (0.2586, 0.3219, 0.3662))

train_tt = tt.Compose([
    tt.Resize((32, 32)),
    tt.RandomHorizontalFlip(),
    tt.ToTensor(),
    tt.Normalize(*stats, inplace = True)
])

val_tt = tt.Compose([
    tt.Resize((32, 32)),
    tt.ToTensor(),
    tt.Normalize(*stats)
])