# Import necessary packages.import torch
import torch.nn as nn
from torchvision import datasets, models, transforms
from torchsummary import summary
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import time
import os
# Load the Subset-datasets of Caltech 256.
dataset ='./Datasets/'# Set the dataset directory.
train_directory = os.path.join(dataset,'train')
valid_directory = os.path.join(dataset,'valid')
test_directory = os.path.join(dataset,'test')# Number of classes.# num_classes = len(os.listdir(valid_directory))# print(num_classes) # num_classes is 10.# Load Data from folders.
data ={
'train':datasets.ImageFolder(root=train_directory,
transform=image_transforms['train']),'valid':datasets.ImageFolder(root=valid_directory,
transform=image_transforms['valid']),'test': datasets.ImageFolder(root=test_directory,
transform=image_transforms['test'])}
/home/wsl_ubuntu/anaconda3/envs/xy_trans/lib/python3.8/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
warnings.warn(
/home/wsl_ubuntu/anaconda3/envs/xy_trans/lib/python3.8/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
warnings.warn(msg)
ResNet(
(conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
(layer1): Sequential(
(0): Bottleneck(
(conv1): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=Tru