میخواهم نتیجه را بر روی داده های تستم امتحان کنم و همچنین یک آستانه در نظر بگیرید و confidence هایی که کمتر از آستانه هستند را در کلاس 5 قرار دهید و چنین تصویر تست که کلاسهای از 0 تا 5 را مشخص کرده اند نمایش دهید در نهایت کد من به صورت زیر است: import os
import zipfile
from google.colab import drive
drive.mount('/content/drive') local_zip="/content/drive/MyDrive/ZIP archive 6.zip"
zip_ref=zipfile.ZipFile(local_zip,'r')
zip_ref.extractall("/content/bone fracture") !python -m pip install --upgrade pip
!pip install tensorflow
!pip install tensorboard !pip install torch import torch
from IPython.display import Image # کلون کردن YOLOv5
!git clone https://github.com/ultralytics/yolov5.git
ورود به دایرکتوری
%cd yolov5
نصب وابستگیها
!pip install -r requirements.txt import os
from random import choice, sample
import shutil
imgs = []
xmls = []
Define paths for train, val, and test
train_path = '/content/yolov5/data/images/train'
val_path = '/content/yolov5/data/images/val'
test_path = '/content/yolov5/data/images/test'
trainlabelPath = '/content/yolov5/data/labels/train'
vallabelPath = '/content/yolov5/data/labels/val'
testlabelPath = '/content/yolov5/data/labels/test'
source_path = '/content/bone fracture/YOLODataSet1/bone_fracture'
Create necessary directories if not exist
for path in [train_path, val_path, test_path, trainlabelPath, vallabelPath, testlabelPath]:
if not os.path.exists(path):
os.mkdir(path)
train_ratio = 0.8
val_ratio = 0.2
test_ratio = 0.1
Total count of images (assuming each image has a corresponding .txt)
totalImgCount = len(os.listdir(source_path)) / 2
Sort files into corresponding arrays
for (dirname, dirs, files) in os.walk(source_path):
for filename in files:
if filename.endswith('.txt'):
xmls.append(filename)
elif filename.lower().endswith(('.jpg', '.jpeg', '.png')):
imgs.append(filename)
Randomly select 10% for test
test_count = int(len(imgs) * test_ratio)
test_imgs = sample(imgs, test_count)
test_xmls = [img[:-4] + '.txt' for img in test_imgs]
for img, xml in zip(test_imgs, test_xmls):
# Copy test files to test directory
shutil.copy(os.path.join(source_path, img), os.path.join(test_path, img))
shutil.copy(os.path.join(source_path, xml), os.path.join(testlabelPath, xml))
# Remove test files from original list
imgs.remove(img)
xmls.remove(xml)
Counting range for cycles
countForTrain = int(len(imgs) * train_ratio)
countForVal = int(len(imgs) * val_ratio)
Cycle for train
for x in range(countForTrain):
fileJpg = choice(imgs)
fileXml = fileJpg[:-4] + '.txt'
shutil.copy(os.path.join(source_path, fileJpg), os.path.join(train_path, fileJpg))
shutil.copy(os.path.join(source_path, fileXml), os.path.join(trainlabelPath, fileXml))
imgs.remove(fileJpg)
xmls.remove(fileXml)
Cycle for validation
for x in range(countForVal):
fileJpg = choice(imgs)
fileXml = fileJpg[:-4] + '.txt'
shutil.copy(os.path.join(source_path, fileJpg), os.path.join(val_path, fileJpg))
shutil.copy(os.path.join(source_path, fileXml), os.path.join(vallabelPath, fileXml))
imgs.remove(fileJpg)
xmls.remove(fileXml)
print("Data distribution completed.")
shutil.copy('/content/drive/MyDrive/xr_bones - 10.yaml', '/content/yolov5/data/dataset.yaml') !python train.py --img 416 --batch 8 --epochs 30 --data dataset.yaml --weights yolov5n.pt --cache