title: Error:RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase
body: <p>I got error after running the following script:</p>
  <h1>-<em>- coding: utf-8 -</em>-</h1>
  <h1>Import the Stuff</h1>
  <pre><code>import torch
  import torch.nn as nn
  import torch.optim as optim
  from torch.utils import data
  from torch.utils.data import DataLoader
  import torchvision.transforms as transforms
  
  import cv2
  
  import numpy as np
  
  import csv
  </code></pre>
  <h1>Step1: Read from the log file</h1>
  <pre><code>samples = []
  with open('data/driving_log.csv') as csvfile:
      reader = csv.reader(csvfile)
      next(reader, None)
      for line in reader:
          samples.append(line)
      
  </code></pre>
  <h1>Step2: Divide the data into training set and validation set</h1>
  <pre><code>train_len = int(0.8*len(samples))
  valid_len = len(samples) - train_len
  train_samples, validation_samples = data.random_split(samples, lengths=[train_len, valid_len])
  </code></pre>
  <h1>Step3a: Define the augmentation, transformation processes, parameters and dataset for dataloader</h1>
  <pre><code>def augment(imgName, angle):
    name = 'data/IMG/' + imgName.split('/')[-1]
    current_image = cv2.imread(name)
    current_image = current_image[65:-25, :, :]
    if np.random.rand() &lt; 0.5:
      current_image = cv2.flip(current_image, 1)
      angle = angle * -1.0  
    return current_image, angle
  
  class Dataset(data.Dataset):
  
      def __init__(self, samples, transform=None):
  
          self.samples = samples
          self.transform = transform
  
      def __getitem__(self, index):
        
          batch_samples = self.samples[index]
          
          steering_angle = float(batch_samples[3])
          
          center_img, steering_angle_center = augment(batch_samples[0], steering_angle)
          left_img, steering_angle_left = augment(batch_samples[1], steering_angle + 0.4)
          right_img, steering_angle_right = augment(batch_samples[2], steering_angle - 0.4)
  
          center_img = self.transform(center_img)
          left_img = self.transform(left_img)
          right_img = self.transform(right_img)
  
          return (center_img, steering_angle_center), (left_img, steering_angle_left), (right_img, steering_angle_right)
        
      def __len__(self):
          return len(self.samples)
  </code></pre>
  <h1>Step3b: Creating generator using the dataloader to parallasize the process</h1>
  <pre><code>def _my_normalization(x):
      return x/255.0 - 0.5
  transformations = transforms.Compose([transforms.Lambda(_my_normalization)])
  
  params = {'batch_size': 32,
            'shuffle': True,
            'num_workers': 4}
  
  training_set = Dataset(train_samples, transformations)
  training_generator = data.DataLoader(training_set, **params)
  
  validation_set = Dataset(validation_samples, transformations)
  validation_generator = data.DataLoader(validation_set, **params)
  </code></pre>
  <h1>Step4: Define the network</h1>
  <pre><code>class NetworkDense(nn.Module):
  
      def __init__(self):
          super(NetworkDense, self).__init__()
          self.conv_layers = nn.Sequential(
              nn.Conv2d(3, 24, 5, stride=2),
              nn.ELU(),
              nn.Conv2d(24, 36, 5, stride=2),
              nn.ELU(),
              nn.Conv2d(36, 48, 5, stride=2),
              nn.ELU(),
              nn.Conv2d(48, 64, 3),
              nn.ELU(),
              nn.Conv2d(64, 64, 3),
              nn.Dropout(0.25)
          )
          self.linear_layers = nn.Sequential(
              nn.Linear(in_features=64 * 2 * 33, out_features=100),
              nn.ELU(),
              nn.Linear(in_features=100, out_features=50),
              nn.ELU(),
              nn.Linear(in_features=50, out_features=10),
              nn.Linear(in_features=10, out_features=1)
          )
          
      def forward(self, input):  
          input = input.view(input.size(0), 3, 70, 320)
          output = self.conv_layers(input)
          output = output.view(output.size(0), -1)
          output = self.linear_layers(output)
          return output
  
  
  class NetworkLight(nn.Module):
  
      def __init__(self):
          super(NetworkLight, self).__init__()
          self.conv_layers = nn.Sequential(
              nn.Conv2d(3, 24, 3, stride=2),
              nn.ELU(),
              nn.Conv2d(24, 48, 3, stride=2),
              nn.MaxPool2d(4, stride=4),
              nn.Dropout(p=0.25)
          )
          self.linear_layers = nn.Sequential(
              nn.Linear(in_features=48*4*19, out_features=50),
              nn.ELU(),
              nn.Linear(in_features=50, out_features=10),
              nn.Linear(in_features=10, out_features=1)
          )
          
  
      def forward(self, input):
          input = input.view(input.size(0), 3, 70, 320)
          output = self.conv_layers(input)
          output = output.view(output.size(0), -1)
          output = self.linear_layers(output)
          return output
  </code></pre>
  <h1>Step5: Define optimizer</h1>
  <pre><code>model = NetworkLight()
  optimizer = optim.Adam(model.parameters(), lr=0.0001)
  
  criterion = nn.MSELoss()
  </code></pre>
  <h1>Step6: Check the device and define function to move tensors to that device</h1>
  <pre><code>device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  print('device is: ', device)
  
  def toDevice(datas, device):
    
    imgs, angles = datas
    return imgs.float().to(device), angles.float().to(device)
  </code></pre>
  <h1>Step7: Train and validate network based on maximum epochs defined</h1>
  <pre><code>max_epochs = 22
  
  for epoch in range(max_epochs):
      
      model.to(device)
      
      # Training
      train_loss = 0
      model.train()
      for local_batch, (centers, lefts, rights) in enumerate(training_generator):
          # Transfer to GPU
          centers, lefts, rights = toDevice(centers, device), toDevice(lefts, device), toDevice(rights, device)
          
          # Model computations
          optimizer.zero_grad()
          datas = [centers, lefts, rights]        
          for data in datas:
              imgs, angles = data
  #             print(&quot;training image: &quot;, imgs.shape)
              outputs = model(imgs)
              loss = criterion(outputs, angles.unsqueeze(1))
              loss.backward()
              optimizer.step()
  
              train_loss += loss.data[0].item()
              
          if local_batch % 100 == 0:
              print('Loss: %.3f '
                   % (train_loss/(local_batch+1)))
  
      
      # Validation
      model.eval()
      valid_loss = 0
      with torch.set_grad_enabled(False):
          for local_batch, (centers, lefts, rights) in enumerate(validation_generator):
              # Transfer to GPU
              centers, lefts, rights = toDevice(centers, device), toDevice(lefts, device), toDevice(rights, device)
          
              # Model computations
              optimizer.zero_grad()
              datas = [centers, lefts, rights]        
              for data in datas:
                  imgs, angles = data
  #                 print(&quot;Validation image: &quot;, imgs.shape)
                  outputs = model(imgs)
                  loss = criterion(outputs, angles.unsqueeze(1))
                  
                  valid_loss += loss.data[0].item()
  
              if local_batch % 100 == 0:
                  print('Valid Loss: %.3f '
                       % (valid_loss/(local_batch+1)))
  </code></pre>
  <h1>Step8: Define state and save the model wrt to state</h1>
  <pre><code>state = {
          'model': model.module if device == 'cuda' else model,
          }
  
  torch.save(state, 'model.h5')
  </code></pre>
  <p>This the error message:</p>
  <p>&quot;D:\VICO\Back up\venv\Scripts\python.exe&quot; &quot;D:/VICO/Back up/venv/Scripts/self_driving_car.py&quot;
  device is:  cpu
  device is:  cpu
  Traceback (most recent call last):
  File &quot;&quot;, line 1, in 
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py&quot;, line 105, in spawn_main
  exitcode = _main(fd)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py&quot;, line 114, in _main
  prepare(preparation_data)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py&quot;, line 225, in prepare
  _fixup_main_from_path(data['init_main_from_path'])
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py&quot;, line 277, in _fixup_main_from_path
  run_name=&quot;<strong>mp_main</strong>&quot;)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\runpy.py&quot;, line 263, in run_path
  Traceback (most recent call last):
  File &quot;D:/VICO/Back up/venv/Scripts/self_driving_car.py&quot;, line 165, in 
  pkg_name=pkg_name, script_name=fname)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\runpy.py&quot;, line 96, in _run_module_code
  for local_batch, (centers, lefts, rights) in enumerate(training_generator):
  File &quot;D:\VICO\Back up\venv\lib\site-packages\torch\utils\data\dataloader.py&quot;, line 291, in <strong>iter</strong>
  mod_name, mod_spec, pkg_name, script_name)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\runpy.py&quot;, line 85, in _run_code
  exec(code, run_globals)
  File &quot;D:\VICO\Back up\venv\Scripts\self_driving_car.py&quot;, line 165, in 
  return _MultiProcessingDataLoaderIter(self)
  File &quot;D:\VICO\Back up\venv\lib\site-packages\torch\utils\data\dataloader.py&quot;, line 737, in <strong>init</strong>
  for local_batch, (centers, lefts, rights) in enumerate(training_generator):
  File &quot;D:\VICO\Back up\venv\lib\site-packages\torch\utils\data\dataloader.py&quot;, line 291, in <strong>iter</strong>
  return _MultiProcessingDataLoaderIter(self)
  File &quot;D:\VICO\Back up\venv\lib\site-packages\torch\utils\data\dataloader.py&quot;, line 737, in <strong>init</strong>
  w.start()
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py&quot;, line 112, in start
  self._popen = self._Popen(self)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py&quot;, line 223, in _Popen
  w.start()
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py&quot;, line 112, in start
  return _default_context.get_context().Process._Popen(process_obj)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py&quot;, line 322, in _Popen
  self._popen = self._Popen(self)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py&quot;, line 223, in _Popen
  return Popen(process_obj)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\popen_spawn_win32.py&quot;, line 89, in <strong>init</strong>
  return _default_context.get_context().Process._Popen(process_obj)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py&quot;, line 322, in _Popen
  reduction.dump(process_obj, to_child)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py&quot;, line 60, in dump
  return Popen(process_obj)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\popen_spawn_win32.py&quot;, line 46, in <strong>init</strong>
  ForkingPickler(file, protocol).dump(obj)
  BrokenPipeError: [Errno 32] Broken pipe
  prep_data = spawn.get_preparation_data(process_obj._name)
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py&quot;, line 143, in get_preparation_data
  _check_not_importing_main()
  File &quot;C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py&quot;, line 136, in _check_not_importing_main
  is not going to be frozen to produce an executable.''')
  RuntimeError:
  An attempt has been made to start a new process before the
  current process has finished its bootstrapping phase.</p>
  <pre><code>    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 &quot;freeze_support()&quot; line can be omitted if the program
      is not going to be frozen to produce an executable.
  </code></pre>
  <p>Process finished with exit code 1</p>
  <p>I am not sure the next step for solving the problem</p>