2025 USA-NA-AIO Round 2, Problem 3, Part 4

Part 4 (5 points, coding task)

In this part, we preprocess image data.

  1. Your job is to create a tensor images_pt from image_list that has shape (1000,3,224,224) and datatype float64.

    • The data range is from -1 to 1.

    • Hint: If a is a PIL object, then you can use a.resize to resize it.

  2. Print images_pt.shape.

  3. Print images_pt.dtype.

  4. Print images_pt[5].

### WRITE YOUR SOLUTION HERE ###

images_pt_list = []

for image in image_list:
    image = image.resize((224,224)) # Resize the image
    image_np = np.array(image) # Convert to numpy array
    image_pt = torch.from_numpy(image_np) # Convert to pytorch tensor
    image_pt = image_pt.permute(2,0,1) # Permute the dimension
    image_pt = image_pt / 255 # Normalize value between 0 and 1
    image_pt = image_pt * 2 - 1 # Normalize value between -1 and 1
    images_pt_list.append(image_pt)

images_pt = torch.stack(images_pt_list)

print(images_pt.shape)
print(images_pt.dtype)
print(images_pt[5])

""" END OF THIS PART """