Runtime error generated by pytorch functions

dato datuashvili

Let us consider following code:

from multiprocessing import freeze_support
import torch
import torch.nn as nn
import torchvision
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np
from torchvision import datasets,models, transforms
import time
import os
import copy
import matplotlib.pyplot as plt
#import torch.backends.cudnn as cudnn
#cudnn.benchmark = True
plt.ion()   # interactive mode
path ='C:/Users/User/PycharmProjects/AI_Project/hymenoptera_data'
data_transforms ={
    'train':transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    'val':transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
}
image_datasets ={x:datasets.ImageFolder(os.path.join(path,x),data_transforms[x])
                    for x in ['train','val']}
dataloaders ={x: torch.utils.data.DataLoader(image_datasets[x],batch_size=4,shuffle=True,num_workers=4)
                        for x in ['train','val']}
class_names = image_datasets['train'].classes
device =torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def imshow(inp,title=None):
    inp =inp.numpy().transpose((1,2,0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    inp = std * inp + mean
    inp = np.clip(inp, 0, 1)
    plt.imshow(inp)
    if title is not None:
        plt.title(title)
    plt.pause(0.001)  # pause a b
inputs,classes =next((iter(dataloaders['train'])))
out =torchvision.utils.make_grid(inputs)
imshow(out,title=[class_names[x]for x in classes])

When I run following code, I got this error

raise RuntimeError('''
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

I did not get point from where this error is coming, maybe DataLoader causes this error, but how to fix?

erip

You need to run code with multiple workers (like your DataLoader) in the main function. This is so child processes know whether to join or not.

import torch
import torch.nn as nn
import torchvision
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np
from torchvision import datasets,models, transforms
import time
import os
import copy
import matplotlib.pyplot as plt
#import torch.backends.cudnn as cudnn
#cudnn.benchmark = True
plt.ion()   # interactive mode
path ='C:/Users/User/PycharmProjects/AI_Project/hymenoptera_data'

if __name__ == "__main__":
    data_transforms ={
        'train':transforms.Compose([
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'val':transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
    }
    image_datasets ={x:datasets.ImageFolder(os.path.join(path,x),data_transforms[x])
                     for x in ['train','val']}
    dataloaders ={x: torch.utils.data.DataLoader(image_datasets[x],batch_size=4,shuffle=True,num_workers=4)
                         for x in ['train','val']}
    class_names = image_datasets['train'].classes
    device =torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    def imshow(inp,title=None):
        inp =inp.numpy().transpose((1,2,0))
        mean = np.array([0.485, 0.456, 0.406])
        std = np.array([0.229, 0.224, 0.225])
        inp = std * inp + mean
        inp = np.clip(inp, 0, 1)
        plt.imshow(inp)
        if title is not None:
            plt.title(title)
        plt.pause(0.001)  # pause a b
    inputs,classes =next((iter(dataloaders['train'])))
    out =torchvision.utils.make_grid(inputs)
    imshow(out,title=[class_names[x]for x in classes])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related