Need help to run a python function with GPU from pycharm or jupyter notebook

34 views Asked by At

i have created a python function that basically generates images with random pixels and saves them to a folder. Heres the code:

from PIL import Image
from pathlib import Path  # to create the folder to store the images
import numpy as np
import matplotlib.pyplot as plt
import time
import random
from random import randint

#creates backround images with random pixel values
#if i run it again the previous images remain because in their names i add current time

def create_random_bg(N):
    Path("bg_images").mkdir(parents=True, exist_ok=True)  # creates the folder
    folder = "bg_images/"  # keep folder name here and use it to save the image
    for i in range(N):
        pixel_data = np.random.randint(
            low=0,
            high=256,
            size=(1024, 1024, 3),
            dtype=np.uint8
        )

        img = Image.fromarray(pixel_data, "RGB")  # turn the array into an image
        img_name = str("bg_") + str(i) + str(time.time()) + str(".png")  # give a unique name using current time
        img = img.save(folder + img_name)

create_random_bg(100)

I want to run this code with my GPU in either pycharm or my jupyter notebook. I have NVIDIA Geforce RTX 3050 laptop. Can anyone help?

I attempted to do something like this:

from PIL import Image
from pathlib import Path  # to create the folder to store the images
import numpy as np
import matplotlib.pyplot as plt
import time
import random
from random import randint
**from numba import jit, cuda**

#creates backround images with random pixel values
#if i run it again the previous images remain because in their names i add current time

@jit(target_backend='cuda') #code i addedd, here i made it bold for people to see easier
def create_random_bg(N):
    Path("bg_images").mkdir(parents=True, exist_ok=True)  # creates the folder
    folder = "bg_images/"  # keep folder name here and use it to save the image
    for i in range(N):
        pixel_data = np.random.randint(
            low=0,
            high=256,
            size=(1024, 1024, 3),
            dtype=np.uint8
        )

        img = Image.fromarray(pixel_data, "RGB")  # turn the array into an image
        img_name = str("bg_") + str(i) + str(time.time()) + str(".png")  # give a unique name using current time
        img = img.save(folder + img_name)

if __name__=="__main__":
  create_random_bg(100)

and it gives errors. Heres some of them:

@jit(target_backend='cuda')
C:\Users\wwwsh\anaconda3\lib\site-packages\numba\core\object_mode_passes.py:151: NumbaWarning: Function "create_random_bg" was compiled in object mode without forceobj=True.

File "diffraction_imgs_cuda_gpu.py", line 17:
def create_random_bg(N):
    <source elided>
    folder = "bg_images/"  # keep folder name here and use it to save the image
    for i in range(N):
    ^

  warnings.warn(errors.NumbaWarning(warn_msg,
C:\Users\wwwsh\anaconda3\lib\site-packages\numba\core\object_mode_passes.py:161: NumbaDeprecationWarning: 
Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour.
0

There are 0 answers