|
|
|
import math |
|
import sys |
|
import logging |
|
import os |
|
import subprocess |
|
|
|
def pause(): |
|
""" |
|
Pauses the execution until any key is pressed. |
|
""" |
|
if sys.platform.startswith('win'): |
|
import msvcrt |
|
print("Press any key to continue...") |
|
msvcrt.getch() |
|
else: |
|
import termios |
|
import tty |
|
print("Press any key to continue...") |
|
fd = sys.stdin.fileno() |
|
old_settings = termios.tcgetattr(fd) |
|
try: |
|
tty.setraw(fd) |
|
sys.stdin.read(1) |
|
finally: |
|
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) |
|
|
|
|
|
def install(package): |
|
import subprocess |
|
subprocess.check_call([sys.executable, "-m", "pip", "install", package]) |
|
|
|
def get_filename(file): |
|
filename = None |
|
if file is not None: |
|
filename = file.name |
|
return filename |
|
|
|
def get_extension(file): |
|
extension = None |
|
if file is not None: |
|
extension = file.name.split(".")[-1] |
|
return extension |
|
|
|
|
|
def convert_ratio_to_dimensions(ratio, height=512, rotate90=False): |
|
""" |
|
Calculate pixel dimensions based on a given aspect ratio and base height. |
|
|
|
This function computes the width and height in pixels for an image, ensuring that both dimensions are divisible by 16. The height is adjusted upwards to the nearest multiple of 16 if necessary, and the width is calculated based on the adjusted height and the provided aspect ratio. Additionally, it ensures that both width and height are at least 16 pixels to avoid extremely small dimensions. |
|
|
|
Parameters: |
|
ratio (float): The aspect ratio of the image (width divided by height). |
|
height (int, optional): The base height in pixels. Defaults to 512. |
|
|
|
Returns: |
|
tuple: A tuple containing the calculated (width, height) in pixels, both divisible by 16. |
|
""" |
|
base_height = 512 |
|
|
|
|
|
scaled_height = max(height, base_height) |
|
|
|
adjusted_height = math.ceil(scaled_height / 16) * 16 |
|
|
|
calculated_width = int(adjusted_height * ratio) |
|
|
|
adjusted_width = math.ceil(calculated_width / 16) * 16 |
|
if rotate90: |
|
adjusted_width, adjusted_height = adjusted_height, adjusted_width |
|
return adjusted_width, adjusted_height |
|
|
|
def install_torch(): |
|
print("\nInstalling PyTorch with CUDA support...") |
|
|
|
package = "torch==2.4.0" |
|
index_url = "https://download.pytorch.org/whl/cu124" |
|
|
|
command = [ |
|
"pip", "install", "--force-reinstall", |
|
f"{package}", "--index-url", f"{index_url}" |
|
] |
|
|
|
subprocess.run(command, check=True) |
|
print("\nPyTorch installation completed.") |
|
print("\nInstalling torchvision...") |
|
package = "torchvision==0.19.0" |
|
index_url = "https://download.pytorch.org/whl/cu124" |
|
|
|
command = [ |
|
"pip", "install", "--force-reinstall", |
|
f"{package}", "--index-url", f"{index_url}" |
|
] |
|
|
|
subprocess.run(command, check=True) |
|
print("\nPlease restart the kernel to use the newly installed PyTorch version.") |
|
|
|
def _get_output(cmd): |
|
try: |
|
return subprocess.check_output(cmd).decode("utf-8") |
|
except Exception as ex: |
|
logging.exception(ex) |
|
return None |
|
|
|
def install_cuda_toolkit(): |
|
|
|
|
|
CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda_12.4.1_550.54.15_linux.run" |
|
CUDA_TOOLKIT_FILE = "/tmp/%s" % os.path.basename(CUDA_TOOLKIT_URL) |
|
print("\nDownloading CUDA Toolkit from %s" % CUDA_TOOLKIT_URL) |
|
subprocess.call(["wget", "-q", CUDA_TOOLKIT_URL, "-O", CUDA_TOOLKIT_FILE]) |
|
subprocess.call(["chmod", "+x", CUDA_TOOLKIT_FILE]) |
|
subprocess.call([CUDA_TOOLKIT_FILE, "--silent", "--toolkit"]) |
|
os.environ["CUDA_HOME"] = "/usr/local/cuda" |
|
os.environ["PATH"] = "%s/bin:%s" % (os.environ["CUDA_HOME"], os.environ["PATH"]) |
|
os.environ["LD_LIBRARY_PATH"] = "%s/lib:%s" % ( |
|
os.environ["CUDA_HOME"], |
|
"" if "LD_LIBRARY_PATH" not in os.environ else os.environ["LD_LIBRARY_PATH"], |
|
) |
|
|
|
os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6" |
|
print("\nPlease restart the kernel to use the newly installed CUDA Toolkit.") |
|
|
|
def setup_runtime_env(): |
|
from torch import cuda |
|
logging.info("Python Version: %s" % _get_output(["python", "--version"])) |
|
logging.info("CUDA Version: %s" % _get_output(["nvcc", "--version"])) |
|
logging.info("GCC Version: %s" % _get_output(["gcc", "--version"])) |
|
logging.info("CUDA is available: %s" % cuda.is_available()) |
|
logging.info("CUDA Device Capability: %s" % (cuda.get_device_capability(),)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|