Refer to:
https://blog.csdn.net/Lizongming_/article/details/135652383
1. Run python file to train model
from ultralytics import YOLO
# Create a new YOLO model from scratch
#model = YOLO('yolov8n.yaml')
model = YOLO('yolov8x.yaml')
# Load a pretrained YOLO model (recommended for training)
model = YOLO('yolov8n.pt')
#model = YOLO('yolov8n.yaml').load('yolov8n.pt')
model = YOLO('yolov8x.yaml').load('yolov8n.pt')
# Train the model using the 'tree.yaml' dataset for 500 epochs
results = model.train(data='./Datasets/tree.yaml ',
epochs=500,
iou=0.5,
imgsz=64,
batch=2,
lr0=0.001,
lrf=0.001,
hsv_h=0, #.015 # (float) image HSV-Hue augmentation (fraction),
hsv_s=0, #.7 # (float) image HSV-Saturation augmentation (fraction),
hsv_v=0, #.4 # (float) image HSV-Value augmentation (fraction)
workers=1,
project='./word_dir/std_7bands_yolov8x',
device=0)
# Evaluate the model's performance on the validation set
#results = model.val()
# Perform object detection on an image using the model
#results = model('https://ultralytics.com/images/bus.jpg')
# Export the model to ONNX format
#success = model.export(format='onnx')
把hsv_h,hsv_s和hsv_v三个参数设置为0,不做HSV数据增
2. .yaml to load data
path: /home/ultralytics/Datasets/7bands/ # dataset root dir
train: images/train/
val: images/val/
#test: ./Datasets/Tree/images/val/
# number of classes
nc: 1
#chanel number of the data
ch: 7
# class names
#names: [["pine", "spruce", "birch", "populus"]]
names: ["tree"]
3. /ultralytics/data/utils.py
def verify_image_label(args):
4. /ultralytics/data/base.py
def load_image(self, i, rect_mode=True):
def affine_transform(self, img, border):
5. /ultralytics/data/augment.py
class LetterBox: def __call__(self, labels=None, image=None):
6. /ultralytics/utils/plotting.py
def plot_images
class Annotator:
7. /ultralytics/cfg/default.yaml
channel: 7
8.
(1) in ./ultralytics/engine/predictor.py
def stream_inference(self, source=None, model=None, *args, **kwargs):
# Warmup model
# Warmup model
if not self.done_warmup:
img_ch = 5
self.model.warmup(imgsz=(1 if self.model.pt or self.model.triton else self.dataset.bs, img_ch, *self.imgsz)) #self.args.channel 3
self.done_warmup = True
(2) './ultralytics/data/build.py'
load_inference_source
(3) Predict: in Line 490, './ultralytics/data/loaders.py'
class LoadImagesAndVideos, def __next__(self):
else:
self.mode = "image"
if path.endswith('.tif') or path.endswith('.TIF'):
im0 = tifffile.imread(path)
else:
im0 = cv2.imread(path) # BGR
#im0 = cv2.imread(path) # BGR
if im0 is None:
raise FileNotFoundError(f"Image Not Found {path}")
save_pred
# Save imgs
if self.dataset.mode == "image":
if im0.shape[2] >3:
im0 = im0[:, :, :3]
cv2.imwrite(save_path, im0)