-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
62 lines (52 loc) · 1.75 KB
/
utils.py
File metadata and controls
62 lines (52 loc) · 1.75 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import io
import base64
import random
import torch
import numpy as np
import torchvision.transforms as transforms
"""
This file contains utility functions used across experiments.
"""
def set_seed(seed: int) -> None:
"""
Set random seed for reproducibility across random, numpy, and torch.
Args:
seed (int): The seed value to be used.
"""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# Ensure deterministic behavior in CUDA operations
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def load_user_program(code):
"""
Loads a user program from a string of code, and returns the program as a string
along with the function `execute_command` extracted from the code.
Args:
code (str): String of code that defines the user program.
Returns:
tuple: A tuple containing:
- program_str (str): The original code.
- execute_command (Callable): The function `execute_command` from the code.
"""
program_str = code
# Execute the program string to create a function
exec_globals = {}
exec(program_str, exec_globals)
return program_str, exec_globals['execute_command']
def encode_base64_content(image) -> str:
"""
Encode an image tensor to base64 format for API transmission.
Args:
image: A tensor representation of an image.
Returns:
str: Base64 encoded string of the image in JPEG format.
"""
transform = transforms.ToPILImage()
pil_image = transform(image)
buffered = io.BytesIO()
pil_image.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return img_str