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 javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

pytorch model.cuda() runtime error

Runtime error loading certificate in Azure Functions

in these functions error message is not showing in runtime modal and router

Why am I getting a Pytorch Runtime Error on Test Set

How to resolve runtime error due to size mismatch in PyTorch?

Taking the derivative of the zero function in pytorch giving Runtime error

Pytorch tensor, how to switch channel position - Runtime error

Use a type generated at runtime

Referencing runtime generated pickers

Pytorch Autograd: what does runtime error "grad can be implicitly created only for scalar outputs" mean

PyTorch runtime error : invalid argument 0: Sizes of tensors must match except in dimension 1

Pytorch custom activation functions?

Plotting contour of pytorch functions

Generated arbitrary functions for wj

Protocol buffer objects generated at runtime

Get bytes for a class that was generated at runtime

Runtime Generated EventHandler of Unknown Type

Catch runtime errors in functions

Azure Functions Runtime

Azure functions runtime configuration

Javascript: making functions at runtime

runtime creation of functions in phpunit

AWS Step Functions and Fargate task: container runtime error does not cause pipeline to fail

Using #ifdef preprocessor around virtual functions causes runtime error in program linked against libraries

Grouping Functions In JSDoc Generated Documentation

JS wait/pause in generated functions

How to use functions in generated columns?

ARM GCC generated functions prolog

Generated columns using aggregate functions

TOP Ranking

HotTag

Archive