Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import re
|
| 3 |
|
| 4 |
def generate_script_v8(dataset_code, task, model_size, epochs, batch_size):
|
|
@@ -16,6 +15,9 @@ def generate_script_v8(dataset_code, task, model_size, epochs, batch_size):
|
|
| 16 |
project_name = project_name_match.group(1)
|
| 17 |
version_number = int(version_number_match.group(1))
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
# Generate the script
|
| 20 |
script = f"""
|
| 21 |
import yaml
|
|
@@ -53,16 +55,13 @@ def auto_train():
|
|
| 53 |
yaml_file_path = f'{{dataset.location}}/data.yaml'
|
| 54 |
with open(yaml_file_path, 'r') as file:
|
| 55 |
data = yaml.safe_load(file)
|
| 56 |
-
|
| 57 |
data['val'] = '../valid/images'
|
| 58 |
data['test'] = '../test/images'
|
| 59 |
data['train'] = '../train/images'
|
| 60 |
-
|
| 61 |
with open(yaml_file_path, 'w') as file:
|
| 62 |
yaml.safe_dump(data, file)
|
| 63 |
|
| 64 |
# Determine the model name based on the selected size and task
|
| 65 |
-
model_type = "seg" if task == "Segmentation" else "cls"
|
| 66 |
model_name = f"yolov8{model_size}-{model_type}.pt"
|
| 67 |
|
| 68 |
# Load and train the model
|
|
@@ -79,7 +78,6 @@ def auto_train():
|
|
| 79 |
# Start a thread to update logs in real-time
|
| 80 |
log_thread = threading.Thread(target=update_logs)
|
| 81 |
log_thread.start()
|
| 82 |
-
|
| 83 |
results = model.train(data=yaml_file_path, epochs={epochs}, imgsz=640, batch={batch_size})
|
| 84 |
|
| 85 |
# Stop the log update thread
|
|
@@ -92,14 +90,12 @@ def auto_train():
|
|
| 92 |
log_output = log_stream.read()
|
| 93 |
print("Results Directory:", results.results_dir)
|
| 94 |
print("Final Training Logs:", log_output)
|
| 95 |
-
|
| 96 |
except Exception as e:
|
| 97 |
logger.error(f"An error occurred: {{e}}")
|
| 98 |
log_stream.seek(0)
|
| 99 |
log_output = log_stream.read()
|
| 100 |
print(f"Error: {{e}}")
|
| 101 |
print(log_output)
|
| 102 |
-
|
| 103 |
finally:
|
| 104 |
logger.removeHandler(log_handler)
|
| 105 |
|
|
@@ -115,7 +111,7 @@ def generate_script_v9(dataset_code, task, model_size, epochs, batch_size):
|
|
| 115 |
project_name_match = re.search(r'project\("([^"]+)"\)', dataset_code)
|
| 116 |
version_number_match = re.search(r'version\((\d+)\)', dataset_code)
|
| 117 |
|
| 118 |
-
if not (api_key_match and workspace_match and project_name_match and version_number_match):
|
| 119 |
return "Error: Could not extract necessary information from the dataset code."
|
| 120 |
|
| 121 |
api_key = api_key_match.group(1)
|
|
@@ -132,13 +128,11 @@ def generate_script_v9(dataset_code, task, model_size, epochs, batch_size):
|
|
| 132 |
# Generate the script
|
| 133 |
script = f"""
|
| 134 |
!pip install roboflow
|
| 135 |
-
|
| 136 |
from roboflow import Roboflow
|
| 137 |
rf = Roboflow(api_key="{api_key}")
|
| 138 |
project = rf.workspace("{workspace}").project("{project_name}")
|
| 139 |
version = project.version({version_number})
|
| 140 |
dataset = version.download("yolov9")
|
| 141 |
-
|
| 142 |
!python train.py \\
|
| 143 |
--batch {batch_size} --epochs {epochs} --img 640 --device 0 --min-items 0 --close-mosaic 15 \\
|
| 144 |
--data {{dataset.location}}/data.yaml \\
|
|
@@ -148,7 +142,8 @@ dataset = version.download("yolov9")
|
|
| 148 |
"""
|
| 149 |
return script
|
| 150 |
|
| 151 |
-
|
|
|
|
| 152 |
st.title("Auto Train Script Generator")
|
| 153 |
st.write("Generate a YOLO training script using a Roboflow dataset")
|
| 154 |
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
|
| 3 |
def generate_script_v8(dataset_code, task, model_size, epochs, batch_size):
|
|
|
|
| 15 |
project_name = project_name_match.group(1)
|
| 16 |
version_number = int(version_number_match.group(1))
|
| 17 |
|
| 18 |
+
# Determine the model type based on the selected task
|
| 19 |
+
model_type = "seg" if task == "Segmentation" else "cls"
|
| 20 |
+
|
| 21 |
# Generate the script
|
| 22 |
script = f"""
|
| 23 |
import yaml
|
|
|
|
| 55 |
yaml_file_path = f'{{dataset.location}}/data.yaml'
|
| 56 |
with open(yaml_file_path, 'r') as file:
|
| 57 |
data = yaml.safe_load(file)
|
|
|
|
| 58 |
data['val'] = '../valid/images'
|
| 59 |
data['test'] = '../test/images'
|
| 60 |
data['train'] = '../train/images'
|
|
|
|
| 61 |
with open(yaml_file_path, 'w') as file:
|
| 62 |
yaml.safe_dump(data, file)
|
| 63 |
|
| 64 |
# Determine the model name based on the selected size and task
|
|
|
|
| 65 |
model_name = f"yolov8{model_size}-{model_type}.pt"
|
| 66 |
|
| 67 |
# Load and train the model
|
|
|
|
| 78 |
# Start a thread to update logs in real-time
|
| 79 |
log_thread = threading.Thread(target=update_logs)
|
| 80 |
log_thread.start()
|
|
|
|
| 81 |
results = model.train(data=yaml_file_path, epochs={epochs}, imgsz=640, batch={batch_size})
|
| 82 |
|
| 83 |
# Stop the log update thread
|
|
|
|
| 90 |
log_output = log_stream.read()
|
| 91 |
print("Results Directory:", results.results_dir)
|
| 92 |
print("Final Training Logs:", log_output)
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
logger.error(f"An error occurred: {{e}}")
|
| 95 |
log_stream.seek(0)
|
| 96 |
log_output = log_stream.read()
|
| 97 |
print(f"Error: {{e}}")
|
| 98 |
print(log_output)
|
|
|
|
| 99 |
finally:
|
| 100 |
logger.removeHandler(log_handler)
|
| 101 |
|
|
|
|
| 111 |
project_name_match = re.search(r'project\("([^"]+)"\)', dataset_code)
|
| 112 |
version_number_match = re.search(r'version\((\d+)\)', dataset_code)
|
| 113 |
|
| 114 |
+
if not (api_key_match and workspace_match and project_name_match and version_number_match)):
|
| 115 |
return "Error: Could not extract necessary information from the dataset code."
|
| 116 |
|
| 117 |
api_key = api_key_match.group(1)
|
|
|
|
| 128 |
# Generate the script
|
| 129 |
script = f"""
|
| 130 |
!pip install roboflow
|
|
|
|
| 131 |
from roboflow import Roboflow
|
| 132 |
rf = Roboflow(api_key="{api_key}")
|
| 133 |
project = rf.workspace("{workspace}").project("{project_name}")
|
| 134 |
version = project.version({version_number})
|
| 135 |
dataset = version.download("yolov9")
|
|
|
|
| 136 |
!python train.py \\
|
| 137 |
--batch {batch_size} --epochs {epochs} --img 640 --device 0 --min-items 0 --close-mosaic 15 \\
|
| 138 |
--data {{dataset.location}}/data.yaml \\
|
|
|
|
| 142 |
"""
|
| 143 |
return script
|
| 144 |
|
| 145 |
+
import streamlit as st
|
| 146 |
+
|
| 147 |
st.title("Auto Train Script Generator")
|
| 148 |
st.write("Generate a YOLO training script using a Roboflow dataset")
|
| 149 |
|