Load a saved NN model in different Python file
I am trying to implement the code from a Pytorch beginner's tutorial. But I have written the code for loading the saved model in another Python file.
The FashionClassify file contains the code exactly as its in the tutorial.
Below is the code:
from FashionClassify import NeuralNetwork
from FashionClassify import test_data
import torch

model = NeuralNetwork()
model.load_state_dict(torch.load(&quot;model.pth&quot;))
classes = [
    &quot;T-shirt/top&quot;, &quot;Trouser&quot;,&quot;Pullover&quot;,&quot;Dress&quot;,&quot;Coat&quot;,&quot;Sandal&quot;,&quot;Shirt&quot;,&quot;Sneaker&quot;,&quot;Bag&quot;,&quot;Ankle boot&quot;,
]

model.eval()
x, y = test_data[0][0], test_data[0][1]
with torch.no_grad():
    pred = model(x)
    predicted, actual = classes[pred[0].argmax(0)],classes[y]
    print(f'Predicted: &quot;{predicted}&quot;, Actual: &quot;{actual}&quot;')

However, when I run this, the entire training process starts again. Why is that so ?
OR
Is it an expected behavior ?
(I have gone through a couple of webpages and StackOverflow answers but couldn't find my problem)
FashionClassify file code:
import torch
from torch import nn
from torch.utils.data import DataLoader  # wraps an iterable around dataset
from torchvision import datasets  # stores samples and their label
from torchvision.transforms import ToTensor, Lambda, Compose
import matplotlib as plt

training_data = datasets.FashionMNIST(root='data', train=True, download=True, transform=ToTensor(), )
test_data = datasets.FashionMNIST(root='data', train=False, download=True, transform=ToTensor(), )

batch_size = 64
train_dataLoader = DataLoader(training_data, batch_size=batch_size)
test_dataLoader = DataLoader(test_data, batch_size=batch_size)

for X, y in test_dataLoader:
    print('Shape of X [N,C,H,W]:', X.size())
    print('Shape of y:', y.shape, y.dtype)
    break

device = 'cuda' if torch.cuda.is_available() else 'cpu'
print('Using {} device'.format(device))


# to define a NN, we inherit a class from nn.Module
class NeuralNetwork(nn.Module):
    def __init__(self):
        # will specify how data will proceed in the forward pass
        super(NeuralNetwork, self).__init__()
        self.flatten = nn.Flatten()
        self.linear_relu_stack = nn.Sequential(
            nn.Linear(28 * 28, 512),
            nn.ReLU(),
            nn.Linear(512, 512),
            nn.ReLU(),
            nn.Linear(512, 10),
            nn.ReLU()
        )

    def forward(self, x):
        x = self.flatten(x)
        logits = self.linear_relu_stack(x)
        return logits


model = NeuralNetwork().to(device)
print(model)

loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)


def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)
    for batch, (X,y) in enumerate(dataloader):
        X,y = X.to(device), y.to(device)

        #compute prediction error
        pred = model(X)
        loss = loss_fn(pred, y)

        #backprop
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if batch%100 ==0:
            loss,current = loss.item(), batch * len(X)
            print(f&quot;loss: {loss:&gt;7f} [{current:&gt;5d}/{size:&gt;5d}]&quot;)

def test(dataloader, model):
    size = len(dataloader.dataset)
    model.eval()
    test_loss, correct = 0,0
    with torch.no_grad():
        for X, y in dataloader:
            X,y = X.to(device), y.to(device)
            pred = model(X)
            test_loss += loss_fn(pred, y).item()
            correct +=  (pred.argmax(1) == y).type(torch.float).sum().item()
    test_loss /= size
    correct /= size
    print(f&quot;Test Error: \n Accuracy: {(100*correct):&gt;0.1f}%, Avg loss: {test_loss:&gt;8f} \n&quot;)

epochs = 5
for t in range(epochs):
    print(f&quot;Epoch {t+1}\n-------------------------------&quot;)
    train(train_dataLoader, model, loss_fn, optimizer)
    test(test_dataLoader, model)
print(&quot;Done!&quot;)

torch.save(model.state_dict(), &quot;model.pth&quot;)
print(&quot;Saved PyTorch Model State to model.pth&quot;)


That's what happens when you import another file. All the code gets rerun.
Instead, in your training file:
class FancyNetwork(nn.Module):
    [...]

def train():
    [train code]

if __name__ == &quot;__main__&quot;:
    train()

Now when you run this file train() will get called, but when you import this file in another one, train won't get called automatically.
