-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
195 lines (158 loc) · 6.21 KB
/
cli.py
File metadata and controls
195 lines (158 loc) · 6.21 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
"""
Command-line interface for Custom Image Classifier
Quick tool for managing projects without using the web interface
"""
import argparse
import json
import os
import sys
from pathlib import Path
def list_projects():
"""List all projects"""
projects_dir = Path('projects')
if not projects_dir.exists():
print("No projects found. Create one first!")
return
projects = []
for project_dir in projects_dir.iterdir():
if project_dir.is_dir():
config_file = project_dir / 'config.json'
if config_file.exists():
with open(config_file, 'r') as f:
config = json.load(f)
projects.append(config)
if not projects:
print("No projects found. Create one first!")
return
print(f"\n{'='*70}")
print(f"{'PROJECT NAME':<25} {'CLASSES':<10} {'STATUS':<15} {'CREATED'}")
print(f"{'='*70}")
for project in projects:
name = project['name']
num_classes = project.get('num_classes', 0)
status = '✓ Trained' if project.get('trained') else '⏳ Untrained'
created = project.get('created_at', 'N/A')[:10]
print(f"{name:<25} {num_classes:<10} {status:<15} {created}")
print(f"{'='*70}\n")
def show_project_info(project_name):
"""Show detailed information about a project"""
config_file = Path('projects') / project_name / 'config.json'
if not config_file.exists():
print(f"Error: Project '{project_name}' not found!")
return
with open(config_file, 'r') as f:
config = json.load(f)
print(f"\n{'='*70}")
print(f"Project: {config['name']}")
print(f"{'='*70}")
print(f"Description: {config.get('description', 'N/A')}")
print(f"Created: {config.get('created_at', 'N/A')}")
print(f"Status: {'✓ Trained' if config.get('trained') else '⏳ Not trained'}")
print(f"Number of classes: {config.get('num_classes', 0)}")
if config.get('classes'):
print(f"\nClasses:")
for i, class_name in enumerate(config['classes'], 1):
count = config.get('class_counts', {}).get(class_name, 0)
print(f" {i}. {class_name} ({count} images)")
if config.get('training_history'):
print(f"\nTraining History:")
history = config['training_history']
last_epoch = history[-1]
print(f" Epochs trained: {len(history)}")
print(f" Final loss: {last_epoch['loss']:.4f}")
print(f" Final accuracy: {last_epoch['accuracy']:.2f}%")
print(f"{'='*70}\n")
def create_project(name, description=""):
"""Create a new project"""
projects_dir = Path('projects')
projects_dir.mkdir(exist_ok=True)
project_dir = projects_dir / name
if project_dir.exists():
print(f"Error: Project '{name}' already exists!")
return
# Create project structure
project_dir.mkdir()
(project_dir / 'dataset').mkdir()
(project_dir / 'models').mkdir()
# Create config
config = {
"name": name,
"description": description,
"created_at": str(Path(__file__).stat().st_mtime),
"num_classes": 0,
"classes": [],
"trained": False,
"model_path": None,
"training_history": []
}
config_file = project_dir / 'config.json'
with open(config_file, 'w') as f:
json.dump(config, f, indent=2)
print(f"✓ Project '{name}' created successfully!")
print(f" Location: {project_dir}")
print(f"\nNext steps:")
print(f" 1. Add your dataset to: {project_dir / 'dataset'}")
print(f" 2. Organize images into class folders")
print(f" 3. Run: python scripts/train_model.py --project {name}")
def train_project(project_name, epochs=10):
"""Train a project model"""
config_file = Path('projects') / project_name / 'config.json'
if not config_file.exists():
print(f"Error: Project '{project_name}' not found!")
return
print(f"Starting training for project: {project_name}")
print(f"Epochs: {epochs}")
print(f"\nTraining output:\n")
import subprocess
cmd = [
sys.executable,
'scripts/train_model.py',
'--project', project_name,
'--epochs', str(epochs)
]
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
print(f"\nError: Training failed with exit code {e.returncode}")
sys.exit(1)
def main():
parser = argparse.ArgumentParser(
description='Custom Image Classifier CLI',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s list # List all projects
%(prog)s info my_project # Show project details
%(prog)s create animal_classifier # Create new project
%(prog)s train my_project --epochs 20 # Train model
"""
)
subparsers = parser.add_subparsers(dest='command', help='Command to execute')
# List command
subparsers.add_parser('list', help='List all projects')
# Info command
info_parser = subparsers.add_parser('info', help='Show project information')
info_parser.add_argument('project', help='Project name')
# Create command
create_parser = subparsers.add_parser('create', help='Create a new project')
create_parser.add_argument('name', help='Project name')
create_parser.add_argument('--description', '-d', default='', help='Project description')
# Train command
train_parser = subparsers.add_parser('train', help='Train a project model')
train_parser.add_argument('project', help='Project name')
train_parser.add_argument('--epochs', '-e', type=int, default=10, help='Number of epochs')
args = parser.parse_args()
if not args.command:
parser.print_help()
return
if args.command == 'list':
list_projects()
elif args.command == 'info':
show_project_info(args.project)
elif args.command == 'create':
create_project(args.name, args.description)
elif args.command == 'train':
train_project(args.project, args.epochs)
if __name__ == '__main__':
main()