-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpu_acceleration.py
33 lines (26 loc) · 1.06 KB
/
gpu_acceleration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import cv2
import tensorflow as tf
# Set the visible devices to the GPU
tf.config.experimental.set_visible_devices([tf.config.experimental.list_physical_devices("GPU")[0]], "GPU")
# Load a deep learning model
model = tf.saved_model.load("model")
# Classify an image using the model on the GPU
def classify_image(image, patch_size=None):
# Prepare the input tensor
input_data = tf.constant(image, dtype=tf.float32)
# Split the input image into patches, if patch_size is specified
if patch_size is not None:
patches = cv2.split(image)
else:
patches = [image]
# Classify each patch individually on the GPU
results = []
with tf.device("GPU:0"):
for patch in patches:
patch_data = tf.constant(patch, dtype=tf.float32)
patch_result = model(patch_data, training=False).numpy()
results.append(patch_result.tolist()[0])
# Merge the classification results for each patch into a single result
result = sum(results) / len(results)
# Return the final classification result
return result