How to run perticular code in gpu using PyTorch?
I am using an image processing code in python opencv. Since that process is taking a lot of time to process say 30 images. I tried to process these image parallel using Multiprocessing. The multiprocessing part is working good in CPU but I want to use that multiprocessing thing in GPU(cuda). 

I use torch.multiprocessing for running task in parallel. So I am using torch.device('cuda') for our class to run whole thing in to this perticular device. When I run the code it's showing device using "cuda" but not using any GPU processing.   

import cv2
import numpy as np
import torch
import torch.nn as nn
from torch.multiprocessing import Process, Pool, Manager, set_start_method
import sys
import os

class RoadShoulderWidth(nn.Module):
    def __init__(self):  
        super(RoadShoulderWidth, self).__init__()
        pass

    // Want to run below method in parallel for 30 images.
    @staticmethod   
    def get_dim(image, road_shoulder_width_list):
        ..... code

    def get_road_shoulder_width(self, _root_dir, _img_path_list):

    manager = Manager()
    road_shoulder_width_list = manager.list()
    processes = []
    for img_path in img_path_list[:30]:
        img = cv2.imread(_root_dir + '/' + img_path)
        img = img[72 * 5:72 * 6, 0:1280]
        # Do work
        p = Process(target=self.get_dim,args=(img,road_shoulder_width_list))
        p.start()
        processes.append(p)

    for p in processes:
        p.join()
    return road_shoulder_width_list 


Use below set of code to run your class

if __name__ == '__main__':
    root_dir = '/home/nikhil_m/r'
    img_path_list = os.listdir(root_dir)

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    print('Using device:', device)
    dataloader_kwargs = {'pin_memory': True} 
    set_start_method('fork')   
    obj = RoadShoulderWidth().to(device)
    val = obj.get_road_shoulder_width(str(root_dir), img_path_list)
    print(val)
    print(torch.cuda.is_available())


Can anybody suggest me how to fix this?

Your class RoadShoulderWidth is a nn.Module subclass which lets you use .to(device). This only means that all other nn.Module objects or nn.Parameters that are members of your RoadShoulderWidth object are moved to the device. As from your example, there are none, so nothing happens.

In general PyTorch does not move code to GPU but data. If all data of a pytorch operation are on the GPU (e.g. a + b, a and b are on GPU) then the operation is executed on the GPU. You can move the data with a.to(device), given a is a torch.Tensor object.

PyTorch can only execute its own operations on GPU. It's not able to execute OpenCV code on GPU.
