goheung/app/AI_modules/gpu_utils/gpu_config_example.py
2026-02-02 19:07:53 +09:00

144 lines
3.8 KiB
Python

"""
-------------------------------------------------------------------------
File: gpu_config.py
Description:
외부 GPU 연결 예제
사용법 : python -m app.AI_modules.gpu_utils.example
Author: 소지안 프로
Created: 2026-02-02
Last Modified: 2026-02-02
-------------------------------------------------------------------------
"""
def example_local_gpu():
"""로컬 GPU 사용 예제"""
from .gpu_config import check_gpu_status, setup_tensorflow_gpu
print("\n" + "=" * 50)
print("Example 1: Local GPU Setup")
print("=" * 50)
# GPU 상태 확인
check_gpu_status()
# TensorFlow GPU 설정
gpus = setup_tensorflow_gpu(memory_limit=4096) # 4GB 제한
if gpus:
print("\n[Ready] GPU is ready for training")
else:
print("\n[Warning] No GPU available, will use CPU")
def example_remote_gpu():
"""원격 GPU 서버 연결 예제"""
from .remote_gpu import RemoteGPUClient
print("\n" + "=" * 50)
print("Example 2: Remote GPU Connection")
print("=" * 50)
# 원격 서버 설정 (실제 값으로 변경 필요)
client = RemoteGPUClient(
host='YOUR_GPU_SERVER_IP', # 예: '192.168.1.100'
username='YOUR_USERNAME', # 예: 'ubuntu'
port=22,
key_path='~/.ssh/id_rsa' # SSH 키 경로
)
# 연결 테스트
if client.connect():
# GPU 상태 확인
client.check_gpu()
# 원격 명령어 실행
client.run_command('python --version')
else:
print("[Failed] Could not connect to remote server")
def example_remote_training():
"""원격 GPU 학습 예제"""
from .remote_gpu import SSHGPURunner
print("\n" + "=" * 50)
print("Example 3: Remote Training")
print("=" * 50)
# 설정 (실제 값으로 변경 필요)
runner = SSHGPURunner(
host='YOUR_GPU_SERVER_IP',
username='YOUR_USERNAME',
remote_project_dir='/home/user/projects/goheung',
key_path='~/.ssh/id_rsa'
)
if runner.connect():
# 1. 프로젝트 동기화
# runner.sync_project('./app')
# 2. 학습 실행
# runner.run('python -m app.AI_modules.DeepLabV3.train --epochs 50')
# 3. 백그라운드 학습 (연결 끊어도 계속 실행)
# runner.run_background('python -m app.AI_modules.DeepLabV3.train --epochs 50')
# 4. 로그 확인
# runner.check_training_log()
# 5. 결과 다운로드
# runner.download_model('DeepLabV3-Plus.h5', './models/')
print("[Ready] Remote training setup complete")
else:
print("[Failed] Could not connect to remote server")
def example_colab_config():
"""Google Colab GPU 설정 예제 (Colab에서 실행)"""
config_code = '''
# Google Colab에서 실행할 코드
# 1. GPU 런타임 확인
!nvidia-smi
# 2. Google Drive 마운트
from google.colab import drive
drive.mount('/content/drive')
# 3. 프로젝트 클론 또는 업로드
# !git clone https://github.com/your/repo.git
# 또는 드라이브에서 복사
# !cp -r /content/drive/MyDrive/goheung /content/
# 4. 의존성 설치
# !pip install tensorflow keras numpy matplotlib
# 5. 학습 실행
# %cd /content/goheung
# !python -m app.AI_modules.DeepLabV3.train --image_path /content/data/Images/
# 6. 결과 저장
# !cp DeepLabV3-Plus.h5 /content/drive/MyDrive/models/
'''
print("\n" + "=" * 50)
print("Example 4: Google Colab Configuration")
print("=" * 50)
print(config_code)
if __name__ == '__main__':
print("GPU Connection Examples")
print("=" * 50)
# 로컬 GPU 확인
try:
example_local_gpu()
except Exception as e:
print(f"[Error] {e}")
# 다른 예제는 설정 후 주석 해제하여 사용
# example_remote_gpu()
# example_remote_training()
# example_colab_config()