manbehindthemadness / blood-magic Goto Github PK
View Code? Open in Web Editor NEWLets see how many core dumps we can squeeze into a single experiment
License: GNU General Public License v3.0
Lets see how many core dumps we can squeeze into a single experiment
License: GNU General Public License v3.0
When I defined cupy2cudaGpuMat in 2 dimensions, memory copy was only partially done.
Defining it as 1D and reshape it after memory copying worked fine.
I would appreciate a check on my changes.
*** blood_magic.py 2023-03-23 17:10:14.566403963 +0900
--- CUDA_MatConversion.py 2023-03-23 17:10:26.322404181 +0900
***************
*** 1,19 ****
! # blood_magic.py
"""
This is a buncha stuff that is on the edge of possibility, totally unsafe, and completely unadvised to use in production...sounds fun yeah?
"""
import os
import cv2 # noqa
import pycuda # noqa
- import pycuda.gpuarray as gpuarray
import pycuda.autoinit # noqa
! import torch # noqa
! import numpy as np
! import cupy as cp
import pytorch_pfn_extras as ppe
! import PIL
from PIL import Image
! from torchvision.transforms import ToTensor, Resize # noqa
ppe.cuda.use_torch_mempool_in_cupy()
--- 1,24 ----
! # Original Code: https://github.com/manbehindthemadness/blood-magic
! # Concerned Topic: https://github.com/rapidsai/cucim/issues/329
! # Modified: 2023.03.23 Hitoshi Uchida
! #
"""
This is a buncha stuff that is on the edge of possibility, totally unsafe, and completely unadvised to use in production...sounds fun yeah?
"""
import os
+ from typing import Union, NamedTuple
+
+ import cupy as cp
import cv2 # noqa
+ import numpy as np
+ import PIL
import pycuda # noqa
import pycuda.autoinit # noqa
! import pycuda.gpuarray as gpuarray
import pytorch_pfn_extras as ppe
! import torch # noqa
from PIL import Image
! from torchvision.transforms import Resize, ToTensor # noqa
ppe.cuda.use_torch_mempool_in_cupy()
***************
*** 34,39 ****
--- 39,65 ----
##################################################################################
+ class cudaMemcpyKind(NamedTuple):
+ '''CUDA memory copy types
+
+ Values
+ cudaMemcpyHostToHost = 0
+ Host -> Host
+ cudaMemcpyHostToDevice = 1
+ Host -> Device
+ cudaMemcpyDeviceToHost = 2
+ Device -> Host
+ cudaMemcpyDeviceToDevice = 3
+ Device -> Device
+ cudaMemcpyDefault = 4
+ Direction of the transfer is inferred from the pointer values. Requires unified virtual addressing
+ '''
+ cudaMemcpyHostToHost = 0
+ cudaMemcpyHostToDevice = 1
+ cudaMemcpyDeviceToHost = 2
+ cudaMemcpyDeviceToDevice = 3
+ cudaMemcpyDefault = 4
+
def pinned_array(array):
"""
***************
*** 232,237 ****
--- 258,266 ----
"""
i_face = CV2CPArrayInterface
interface = cp.asarray(i_face(mat))
+ if interface.ndim == 3 and interface.shape[-1] == 1:
+ interface = interface.reshape(interface.shape[:2])
+
return interface
***************
*** 296,303 ****
print(f'unable to convert {channel} number of channels to Cuda_GpuMat')
raise IndexError
! gpuMat = cv2.cuda_GpuMat((width, height), cv_dtype) # noqa
! pycuda.driver.memcpy_dtod(gpuMat.cudaPtr(), mat.data.ptr, mat.size) # noqa
return gpuMat
--- 325,335 ----
print(f'unable to convert {channel} number of channels to Cuda_GpuMat')
raise IndexError
! gpuMat = cv2.cuda_GpuMat((width * height, 1), cv_dtype) # noqa
! cp.cuda.runtime.memcpy(gpuMat.cudaPtr(), mat.data.ptr,
! mat.size, cudaMemcpyKind.cudaMemcpyDeviceToDevice)
! gpuMat = gpuMat.reshape(channel, height)
!
return gpuMat
***************
*** 320,327 ****
def convert_mat(
! mat: [np.ndarray, cv2.cuda_GpuMat, cp.ndarray, torch.tensor, list, PIL.Image.Image], # noqa
! output_type: type) -> [np.ndarray, cv2.cuda_GpuMat, cp.ndarray, torch.tensor, list, PIL.Image.Image]: # noqa
"""
This will unify type conversions into a single statement, so we can get past all the weird conversion problems.
--- 352,359 ----
def convert_mat(
! mat: Union[np.ndarray, cv2.cuda_GpuMat, cp.ndarray, torch.tensor, list, PIL.Image.Image], # noqa
! output_type: type) -> Union[np.ndarray, cv2.cuda_GpuMat, cp.ndarray, torch.tensor, list, PIL.Image.Image]: # noqa
"""
This will unify type conversions into a single statement, so we can get past all the weird conversion problems.
***************
*** 367,372 ****
--- 399,407 ----
elif isinstance(mat, cv2.cuda_GpuMat): # noqa
if output_type in [list, np.ndarray, PIL.Image]: # Opencv 2 numpy.
mat = mat.download() # noqa
+ if mat.ndim == 3 and mat.shape[-1] == 1:
+ mat = mat.reshape(mat.shape[:2])
+
if output_type == list: # Opencv 2 list.
mat = mat.tolist()
elif output_type == PIL.Image: # Opencv 2 PIL.
***************
*** 396,402 ****
return mat
! def convert_many(mats: list, output_types: [type, list[type]]):
"""
Converts arrays of stuff
"""
--- 431,437 ----
return mat
! def convert_many(mats: list, output_types: Union[type, list[type]]):
"""
Converts arrays of stuff
"""
***************
*** 443,449 ****
if __name__ == '__main__':
! print('single')
! convert_mat_test(single=True)
! print('multi')
! convert_mat_test(single=False)
--- 478,514 ----
if __name__ == '__main__':
! # i = 3000
! # j = 5800
! i = 3
! j = 6
!
! # arr0 = np.arange(i*j*3, dtype=np.uint8).reshape((i,j,3))
! arr0 = np.arange(i*j, dtype=np.float32).reshape((i, j))
!
! cparr = convert_mat(arr0, cp.ndarray)
! cvarr = convert_mat(cparr, cv2.cuda_GpuMat)
! arr2 = convert_mat(cvarr, np.ndarray)
!
! diff = arr0 - arr2
! print(cparr)
! print(arr2)
! print('#1', diff.max(), diff.min())
!
! cvarr = cv2.cuda_GpuMat()
! cvarr.upload(arr0)
! arr2 = cvarr.download()
! diff = arr0 - arr2
! print('#2', diff.max(), diff.min())
!
! cvarr = cv2.cuda_GpuMat()
! cvarr.upload(arr0)
! cparr = convert_mat(cvarr, cp.ndarray)
!
! diff = arr0 - cp.asnumpy(cparr)
! print('#3', diff.max(), diff.min())
!
! cvarr = convert_mat(arr0, cv2.cuda_GpuMat)
! cparr = convert_mat(cvarr, cp.ndarray)
! diff = arr0 - cp.asnumpy(cparr)
! print('#4', diff.max(), diff.min())
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.