83 lines
2.6 KiB
Python
83 lines
2.6 KiB
Python
import matplotlib.pyplot as plt
|
|
import os
|
|
|
|
import os
|
|
|
|
def find_newest_file(directory):
|
|
# دریافت لیست فایلها در دایرکتوری
|
|
files = os.listdir(directory)
|
|
# بررسی اینکه آیا دایرکتوری خالی است یا خیر
|
|
if not files:
|
|
return None # اگر دایرکتوری خالی باشد، مقدار None بازگردانده میشود
|
|
|
|
# ایجاد مسیر کامل برای فایلها و پیدا کردن فایل جدیدتر با استفاده از max
|
|
full_paths = [os.path.join(directory, file) for file in files]
|
|
newest_file = max(full_paths, key=os.path.getctime) # بر اساس زمان ایجاد (creation time)
|
|
return newest_file
|
|
|
|
def save_diagram(progress_data, file_path):
|
|
file_path_parts = file_path.split('/')
|
|
file_name = file_path_parts[len(file_path_parts)-1].replace('log','png')
|
|
# آرایه دادهها
|
|
data = progress_data
|
|
|
|
# استخراج مقادیر x و y از آرایه
|
|
x = [point[0] for point in data]
|
|
y = [point[1] for point in data]
|
|
|
|
# ترسیم نمودار
|
|
plt.figure(figsize=(8, 6)) # تنظیم اندازه نمودار
|
|
plt.plot(x, y, marker='', linestyle='-', color='b', label='Data Line') # ترسیم خط همراه با نقاط
|
|
|
|
# تنظیم عنوان و برچسبهای محور
|
|
plt.title("نمودار دوبعدی دادهها", fontsize=14)
|
|
plt.xlabel("EPOCHS", fontsize=12)
|
|
plt.ylabel("LOSS", fontsize=12)
|
|
|
|
# نمایش خطوط شبکه
|
|
plt.grid(True, linestyle='--', alpha=0.7)
|
|
|
|
# نمایش legend
|
|
plt.legend()
|
|
|
|
plt.savefig(f"./log/{file_name}", dpi=300, bbox_inches='tight')
|
|
|
|
plt.close()
|
|
|
|
|
|
|
|
def read_log(file_path):
|
|
with open(file_path, 'r') as f:
|
|
data = f.readlines()
|
|
|
|
lines = []
|
|
for item in data:
|
|
if item.__contains__(' - iter '):
|
|
lines.append(item)
|
|
|
|
progress = []
|
|
for item in lines:
|
|
epoch_part, loss_part = item.split(' iter ')
|
|
try:
|
|
epoch = int(epoch_part.split(' epoch ')[1].replace(' -', ''))
|
|
loss = float(loss_part.split(' - ')[1].replace('loss ', ''))
|
|
progress.append((epoch, loss))
|
|
except:
|
|
continue
|
|
|
|
return progress
|
|
|
|
|
|
def plot_diagram():
|
|
latest_log = find_newest_file("./log").replace('png', 'log')
|
|
if latest_log == '':
|
|
print('no file detected')
|
|
return
|
|
|
|
progress = read_log(latest_log)
|
|
save_diagram(progress, latest_log)
|
|
print('log diagram saved!')
|
|
|
|
if __name__ == "__main__":
|
|
plot_diagram()
|
|
pass |