Pytorch - handling picutres and .jpeg files (beginner's questions)
I am new at Pytorch, and have a couple of questions regarding the way pictures are being handled:

1) In the "training a classifier" tutorial, the pictures are PIL files, and are being handled via the following commands (where "transform" also turns the PIL format into a tensor format):

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                    download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=2)


It seems like trainset[1] (and also for the other indices) consists of a tensor, and a number.  I want to define a new variable "image" that will consist of the tensor part of trainset[ 1 ] and then print it - how can I do it?  

2) Assume that I have a different dataset that I want to classify. It consists of .jpeg images that are located in the folder "C:/temp/dataset". How can I define the variable "trainset" to consist of these images? 

Thanks a lot in advance! 

For your first question:

image = trainset[1][0]

print(image)


For your second question:

from PIL import Image
import numpy as np
import os

def load_image(infilename):
    """This function loads an image into memory when you give it
       the path of the image
    """
    img = Image.open(infilename)
    img.load()
    data = np.asarray(img, dtype="float32")
    return data


def create_npy_from_image(images_folder, output_name, num_images, image_dim):
    """Loops through the images in a folder and saves all of them
       as a numpy array in output_name
    """
    image_matrix = np.empty((num_images, image_dim, image_dim, 3), dtype=np.float32)
    for i, filename in enumerate(os.listdir(images_folder)):
        if filename.endswith(".jpg"):
            data = load_image(images_folder + filename)
            image_matrix[i] = data
        else:
            continue
    np.save(output_name, image_matrix)


So I would write something like this:

create_npy_from_image(path_to_images_folder, "trainset.npy", numer_of_images_in_your_folder, DIM)

DIM is 64 for example if your images are 64x64x3

You can then load the saved array with np.load and then convert it to a pytorch tensor using from_numpy function.

Let me know if this works. Good luck!
