Idk
This commit is contained in:
0
rpi/training/__init__.py
Normal file
0
rpi/training/__init__.py
Normal file
6
rpi/training/chess.yaml
Normal file
6
rpi/training/chess.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
train: C:/Users/Laurent/Desktop/board-mate/rpi/training/datasets/roboflow/labels-bck-bck-bck
|
||||
val: C:/Users/Laurent/Desktop/board-mate/rpi/training/datasets/roboflow/labels-bck-bck-bck
|
||||
|
||||
nc: 12
|
||||
names: ['w_pawn','w_knight','w_bishop','w_rook','w_queen','w_king',
|
||||
'b_pawn','b_knight','b_bishop','b_rook','b_queen','b_king']
|
||||
7
rpi/training/datasets/chesscog/data.yaml
Normal file
7
rpi/training/datasets/chesscog/data.yaml
Normal file
@@ -0,0 +1,7 @@
|
||||
train: ../train/
|
||||
val: ../valid/
|
||||
test: ../test/
|
||||
|
||||
nc: 12
|
||||
names: ['w_pawn','w_knight','w_bishop','w_rook','w_queen','w_king',
|
||||
'b_pawn','b_knight','b_bishop','b_rook','b_queen','b_king']
|
||||
7
rpi/training/datasets/unified/data.yaml
Normal file
7
rpi/training/datasets/unified/data.yaml
Normal file
@@ -0,0 +1,7 @@
|
||||
train: ../train/
|
||||
val: ../valid/
|
||||
test: ../test/
|
||||
|
||||
nc: 12
|
||||
names: ['w_pawn','w_knight','w_bishop','w_rook','w_queen','w_king',
|
||||
'b_pawn','b_knight','b_bishop','b_rook','b_queen','b_king']
|
||||
36
rpi/training/decrease_labels.py
Normal file
36
rpi/training/decrease_labels.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import os
|
||||
|
||||
# --------------------------
|
||||
# Configuration
|
||||
# --------------------------
|
||||
labels_dir = "datasets/visiope/test/labels"
|
||||
|
||||
# --------------------------
|
||||
# Process each label file
|
||||
# --------------------------
|
||||
for filename in os.listdir(labels_dir):
|
||||
if not filename.endswith(".txt"):
|
||||
continue
|
||||
|
||||
txt_path = os.path.join(labels_dir, filename)
|
||||
new_lines = []
|
||||
|
||||
with open(txt_path, "r") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
for line in lines:
|
||||
parts = line.strip().split()
|
||||
if len(parts) < 5:
|
||||
continue # skip invalid lines
|
||||
cls = int(parts[0])
|
||||
cls -= 1 # subtract 1 from class index
|
||||
cls = max(cls, 0) # ensure no negative indices
|
||||
new_lines.append(" ".join([str(cls)] + parts[1:]))
|
||||
|
||||
# Overwrite file with updated indices
|
||||
with open(txt_path, "w") as f:
|
||||
f.write("\n".join(new_lines))
|
||||
|
||||
print(f"Updated {filename}")
|
||||
|
||||
print("All label files have been adjusted!")
|
||||
70
rpi/training/labelizer.py
Normal file
70
rpi/training/labelizer.py
Normal file
@@ -0,0 +1,70 @@
|
||||
import os
|
||||
import cv2
|
||||
from ultralytics import YOLO
|
||||
|
||||
# --------------------------
|
||||
# Configuration
|
||||
# --------------------------
|
||||
model_path = "models/bck/best-3.pt" # your trained YOLO model
|
||||
images_dir = "C:/Users/Laurent/Desktop/board-mate/rpi/training/datasets/universe/train/images"
|
||||
labels_dir = "C:/Users/Laurent/Desktop/board-mate/rpi/training/datasets/universe/train/labels"
|
||||
img_width = 640
|
||||
img_height = 640
|
||||
|
||||
os.makedirs(labels_dir, exist_ok=True)
|
||||
|
||||
# --------------------------
|
||||
# Load model
|
||||
# --------------------------
|
||||
model = YOLO(model_path)
|
||||
|
||||
# --------------------------
|
||||
# Mapping YOLO class index -> piece name (optional)
|
||||
# --------------------------
|
||||
names = ['w_pawn','w_knight','w_bishop','w_rook','w_queen','w_king',
|
||||
'b_pawn','b_knight','b_bishop','b_rook','b_queen','b_king']
|
||||
|
||||
# --------------------------
|
||||
# Process images
|
||||
# --------------------------
|
||||
for img_file in os.listdir(images_dir):
|
||||
if not img_file.lower().endswith((".png", ".jpg", ".jpeg")):
|
||||
continue
|
||||
|
||||
img_path = os.path.join(images_dir, img_file)
|
||||
img = cv2.imread(img_path)
|
||||
if img is None:
|
||||
print(f"Failed to read {img_file}")
|
||||
continue
|
||||
|
||||
height, width = img.shape[:2]
|
||||
|
||||
# Run YOLO detection
|
||||
results = model(img)
|
||||
res = results[0]
|
||||
|
||||
lines = []
|
||||
boxes = res.boxes.xyxy.cpu().numpy() # [x1, y1, x2, y2]
|
||||
classes = res.boxes.cls.cpu().numpy()
|
||||
confs = res.boxes.conf.cpu().numpy()
|
||||
|
||||
for box, cls, conf in zip(boxes, classes, confs):
|
||||
if conf < 0.5: # skip low-confidence predictions
|
||||
continue
|
||||
|
||||
x1, y1, x2, y2 = box
|
||||
x_center = (x1 + x2) / 2 / width
|
||||
y_center = (y1 + y2) / 2 / height
|
||||
w_norm = (x2 - x1) / width
|
||||
h_norm = (y2 - y1) / height
|
||||
|
||||
lines.append(f"{int(cls)} {x_center:.6f} {y_center:.6f} {w_norm:.6f} {h_norm:.6f}")
|
||||
|
||||
# Save YOLO .txt file with same basename as image
|
||||
txt_path = os.path.join(labels_dir, os.path.splitext(img_file)[0] + ".txt")
|
||||
with open(txt_path, "w") as f:
|
||||
f.write("\n".join(lines))
|
||||
|
||||
print(f"Pre-labeled {img_file} -> {txt_path}")
|
||||
|
||||
print("All images have been pre-labeled!")
|
||||
25
rpi/training/move_image.py
Normal file
25
rpi/training/move_image.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import os
|
||||
import shutil
|
||||
|
||||
# ----------------------------
|
||||
# Configuration
|
||||
# ----------------------------
|
||||
|
||||
source_folder = "datasets/twhpv/valid/images"
|
||||
destination_folder = "datasets/_unified/valid/images"
|
||||
|
||||
os.makedirs(destination_folder, exist_ok=True)
|
||||
|
||||
# Supported image extensions
|
||||
image_extensions = [".jpg", ".jpeg", ".png", ".bmp", ".gif"]
|
||||
|
||||
# ----------------------------
|
||||
# Copy images
|
||||
# ----------------------------
|
||||
for filename in os.listdir(source_folder):
|
||||
if any(filename.lower().endswith(ext) for ext in image_extensions):
|
||||
src_path = os.path.join(source_folder, filename)
|
||||
dst_path = os.path.join(destination_folder, filename)
|
||||
shutil.copy2(src_path, dst_path) # copy2 preserves metadata
|
||||
|
||||
print(f"All images copied to '{destination_folder}'")
|
||||
59
rpi/training/sort_labels.py
Normal file
59
rpi/training/sort_labels.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
|
||||
# ----------------------------
|
||||
# Configuration
|
||||
# ----------------------------
|
||||
|
||||
src_dir = "datasets/visiope/test/labels"
|
||||
dest_dir = "datasets/_unified/test/labels"
|
||||
|
||||
os.makedirs(dest_dir, exist_ok=True)
|
||||
|
||||
# Reference class order you want to follow
|
||||
"""[
|
||||
'w_pawn','w_knight','w_bishop','w_rook','w_queen','w_king',
|
||||
'b_pawn','b_knight','b_bishop','b_rook','b_queen','b_king'
|
||||
]"""
|
||||
|
||||
reference_classes = [
|
||||
'w_pawn','w_knight','w_bishop','w_rook','w_queen','w_king',
|
||||
'b_pawn','b_knight','b_bishop','b_rook','b_queen','b_king'
|
||||
]
|
||||
|
||||
# Current class order in your dataset (change this to match your dataset!)
|
||||
current_classes = ['bishop', 'black-bishop', 'black-king', 'black-knight', 'black-pawn', 'black-queen', 'black-rook',
|
||||
'white-bishop', 'white-king', 'white-knight', 'white-pawn', 'white-queen', 'white-rook']
|
||||
|
||||
# ----------------------------
|
||||
# Build index mapping
|
||||
# ----------------------------
|
||||
index_map = {current_classes.index(cls): reference_classes.index(cls) for cls in current_classes}
|
||||
|
||||
# ----------------------------
|
||||
# Process each label file
|
||||
# ----------------------------
|
||||
|
||||
count = 0
|
||||
for filename in os.listdir(src_dir):
|
||||
if filename.endswith(".txt"):
|
||||
input_path = os.path.join(src_dir, filename)
|
||||
output_path = os.path.join(dest_dir, filename)
|
||||
|
||||
with open(input_path, "r") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
new_lines = []
|
||||
for line in lines:
|
||||
parts = line.strip().split()
|
||||
old_idx = int(parts[0])
|
||||
new_idx = index_map[old_idx]
|
||||
new_lines.append(" ".join([str(new_idx)] + parts[1:]))
|
||||
|
||||
with open(output_path, "w") as f:
|
||||
f.write("\n".join(new_lines))
|
||||
|
||||
if count%100 == 0:
|
||||
print(count)
|
||||
count += 1
|
||||
|
||||
print(f"All labels remapped and saved to '{dest_dir}'")
|
||||
18
rpi/training/training.py
Normal file
18
rpi/training/training.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from ultralytics import YOLO
|
||||
|
||||
def main():
|
||||
model = YOLO("models/yolo11n.pt")
|
||||
model.train(
|
||||
data="./datasets/unified/data.yaml",
|
||||
epochs=200,
|
||||
patience=30,
|
||||
imgsz=640,
|
||||
batch=12,
|
||||
device=0,
|
||||
project="result",
|
||||
name="unified-training",
|
||||
exist_ok=True
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user