1659654660
Một trong những công việc phổ biến nhất trong thị giác máy tính là phát hiện đối tượng. Nó là nền tảng để hiểu và tương tác với cảnh.
Tính năng phát hiện đối tượng được sử dụng trong mọi thứ, từ các ứng dụng đơn giản như phát hiện vật phẩm đến các công việc phức tạp như ô tô tự lái để hiểu các tình huống đa dạng và đưa ra phán đoán dựa trên chúng. Camera an ninh và thậm chí cả điện thoại di động hiện nay đều có các tính năng tương tự được tích hợp sẵn cho nhiều chức năng khác nhau.
Ngày nay, YOLO (You Only Look Once) là khung mô hình phát hiện đối tượng tốt hơn và mô hình này là sự bổ sung mới nhất cho dòng mô hình YOLO. YOLO là mô hình phát hiện đối tượng đầu tiên kết hợp dự đoán hộp giới hạn và phân loại đối tượng vào một mạng có thể phân biệt từ đầu đến cuối duy nhất. Nó được tạo ra và được duy trì trong khuôn khổ Darknet. YOLOv5 là mô hình YOLO đầu tiên được viết trên khung công tác PyTorch, nó nhẹ hơn và dễ sử dụng hơn nhiều. Tuy nhiên, YOLOv7 không vượt trội hơn YOLOv6 trên điểm chuẩn tiêu chuẩn, tập dữ liệu COCO, vì nó không thực hiện các cải tiến kiến trúc cơ bản cho mạng trong YOLOv6.
Yolo v6 có một vài sai sót, chẳng hạn như hiệu suất kém trên các mục nhỏ và khả năng tổng quát kém khi kích thước của các đối tượng không bằng nhau.
Đây là hình ảnh từ giấy YOLO gốc thể hiện hoạt động của YOLO. Nó đã trải qua một chặng đường dài kể từ đó, và chúng tôi hiện đang ở phiên bản 5. Mặc dù thực tế là nó không được viết bởi bất kỳ tác giả hoặc cộng tác viên ban đầu nào, nhưng nó vẫn tuân theo cùng một chiến lược cơ bản. Nó được viết bằng PyTorch, đó là một điểm cộng. Trong phiên bản này của Yolo mosaic, tăng cường được sử dụng, và các phương pháp tiếp cận mở rộng quy mô khác nhau cung cấp nhiều cải tiến.
Để thiết bị phát hiện đối tượng của bạn hoạt động, trước tiên bạn phải thu thập các bức ảnh huấn luyện. Bạn nên suy nghĩ cẩn thận về hoạt động bạn đang cố gắng hoàn thành và lên kế hoạch trước cho các thành phần của nhiệm vụ mà mô hình của bạn có thể cảm thấy khó khăn. Để cải thiện độ chính xác của mô hình cuối cùng của bạn, tôi khuyên bạn nên giảm miền mà mô hình của bạn phải xử lý càng nhiều càng tốt.
Đối với đào tạo tùy chỉnh YOLOv7, chúng tôi cần phát triển tập dữ liệu. Nếu bạn không có bất kỳ dữ liệu nào, bạn có thể sử dụng cơ sở dữ liệu openimages .
Sử dụng LabelImg hoặc bất kỳ công cụ chú thích nào để chú thích tập dữ liệu. Tạo một tệp có cùng tên với hình ảnh và văn bản chú thích.
Chuẩn bị một bộ, ví dụ, tương ứng với
YOLOv7 chấp nhận dữ liệu nhãn trong tệp văn bản (.txt) ở định dạng sau:
Sau khi bạn đã gắn thẻ dữ liệu của mình, chúng tôi sẽ chia dữ liệu đó thành các thư mục đào tạo và kiểm tra. Tỷ lệ phân chia sẽ do người dùng xác định, tuy nhiên tỷ lệ phân chia phổ biến nhất là (80-20) phần trăm, có nghĩa là 80 phần trăm dữ liệu được sử dụng để đào tạo và 20 phần trăm để thử nghiệm. * Hình ảnh và nhãn được lưu trữ trong cấu trúc thư mục đã nêu.
* Để phân chia dữ liệu, hãy xem thư viện python - Thư mục phân tách, sẽ phân chia ngẫu nhiên dữ liệu của bạn thành huấn luyện, kiểm tra và xác thực.
Lệnh pip sau để cài đặt thư viện tách dữ liệu
pip cài đặt các thư mục chia nhỏ
Thư mục đầu vào phải được định dạng như sau:
Để cung cấp cho bạn điều này:
Tách các tệp thành một tập huấn luyện và một tập xác thực (và tùy chọn là một tập thử nghiệm). Thư mục tập dữ liệu cuối cùng trông giống như bên dưới trước khi tham gia khóa đào tạo YOLOv7,
├── yolov7
## └── đào tạo
#### └── hình ảnh (thư mục bao gồm tất cả hình ảnh đào tạo)
#### └── nhãn (thư mục bao gồm tất cả nhãn đào tạo)
## └── kiểm tra
#### └── hình ảnh (thư mục bao gồm tất cả các hình ảnh thử nghiệm)
#### └── nhãn (thư mục bao gồm tất cả các nhãn thử nghiệm)
## └── hợp lệ
#### └── hình ảnh (thư mục bao gồm tất cả các hình ảnh hợp lệ)
### # └── nhãn (thư mục bao gồm tất cả các nhãn hợp lệ)
Bây giờ chúng ta phải phát triển một tệp cấu hình tùy chỉnh. (Đảm bảo chỉ định thư mục thích hợp), vì quá trình đào tạo sẽ hoàn toàn phụ thuộc vào tệp đó.
Tạo một tệp với tên “custom.yaml” trong thư mục (yolov7 / data). Trong tệp đó, hãy dán mã bên dưới. Đặt đường dẫn chính xác đến thư mục tập dữ liệu, thay đổi số lượng lớp và tên của chúng, sau đó lưu nó.
Tạo một tệp chỉ định cấu hình đào tạo. Trong tệp custom.yaml , hãy viết như sau:
train: (Complete path to dataset train folder)
test: (Complete path to dataset test folder)
valid: (Complete path to dataset valid folder)
#Classes
nc: 1 # replace classes count
#classes names
#replace all class names list with your custom classes
namesnames: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear']
Sau khi tất cả các thủ tục tiền xử lý đã được hoàn thành, nó đã sẵn sàng để bắt đầu đào tạo. Khởi chạy thiết bị đầu cuối trong “ yolov7 ” chính, kích hoạt môi trường ảo và thực hiện các lệnh được liệt kê bên dưới.
git clone https://github.com/WongKinYiu/yolov7.git # clone
cd yolov7
pip install -r requirements.txt # install modules
wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt # download pretrained weight
Kho lưu trữ chính thức chứa các trọng số được đào tạo trước cho mô hình của bạn.
Lưu ý :
Tùy thuộc vào ngữ cảnh sử dụng, chẳng hạn như Google Colab, bộ nhớ GPU có thể không đủ. Bạn có thể tìm hiểu bằng cách giảm kích thước lô trong tình huống đó.
python train.py --weights yolov7.pt --data "data/custom.yaml" --workers 4 --batch-size 4 --img 416 --cfg cfg/training/yolov7.yaml --name yolov7 --hyp data/hyp.scratch.p5.yaml
- img = kích thước của hình ảnh mà mô hình sẽ đào tạo, giá trị mặc định là 640.
- batch-size = kích thước lô được sử dụng để đào tạo tập dữ liệu tùy chỉnh.
- epochs = số kỷ nguyên đào tạo để có được mô hình tốt nhất
- data = đường dẫn tệp cấu hình tùy chỉnh
- weights = weights yolov7 được xử lý trước ( yolov7.pt )
Lưu ý : Nếu bất kỳ hình ảnh nào bị hỏng, quá trình đào tạo sẽ không bắt đầu. Nếu bất kỳ tệp nhãn nào bị hỏng, quá trình đào tạo sẽ không bắt đầu vì yolov7 sẽ bỏ qua tệp hình ảnh và nhãn đó.
Chờ cho quá trình tập luyện kết thúc trước khi thực hiện suy luận với các quả tạ mới hình thành. Các quả cân được đào tạo tùy chỉnh sẽ được lưu trong đường dẫn thư mục sau.
[yolov7/runs/train/yolov7/weights/best.pt]
Khi quá trình đào tạo kết thúc, hãy chuyển đến thiết bị đầu cuối và thực hiện lệnh được liệt kê bên dưới để phát hiện về trọng lượng tùy chỉnh.
python detect.py --weights runs/train/yolov7/weights/best.pt --source "path to your testing image"
Bạn có thể sử dụng YOLO để thiết kế mô hình phát hiện tùy chỉnh của riêng mình cho bất kỳ thứ gì bạn muốn.
Yolo v7 là một bước tiến đáng kể về tốc độ và độ chính xác, đồng thời nó phù hợp hoặc thậm chí vượt trội so với các mô hình dựa trên RPN. Mô hình này nhanh chóng và đáng tin cậy, và bây giờ nó có thể được sử dụng cho mọi thứ.
Đó là tất cả những gì cần có để “Đào tạo YOLOv7 về Dữ liệu tùy chỉnh”. Bạn có thể thử nghiệm với dữ liệu của riêng mình. YOLOv7 nhẹ và dễ sử dụng. YOLO v7 đào tạo nhanh chóng, đưa ra kết luận tốt và hoạt động tốt.
Các bài học chính từ YOLOV7 nói trên được tóm tắt như sau:
1659654660
Một trong những công việc phổ biến nhất trong thị giác máy tính là phát hiện đối tượng. Nó là nền tảng để hiểu và tương tác với cảnh.
Tính năng phát hiện đối tượng được sử dụng trong mọi thứ, từ các ứng dụng đơn giản như phát hiện vật phẩm đến các công việc phức tạp như ô tô tự lái để hiểu các tình huống đa dạng và đưa ra phán đoán dựa trên chúng. Camera an ninh và thậm chí cả điện thoại di động hiện nay đều có các tính năng tương tự được tích hợp sẵn cho nhiều chức năng khác nhau.
Ngày nay, YOLO (You Only Look Once) là khung mô hình phát hiện đối tượng tốt hơn và mô hình này là sự bổ sung mới nhất cho dòng mô hình YOLO. YOLO là mô hình phát hiện đối tượng đầu tiên kết hợp dự đoán hộp giới hạn và phân loại đối tượng vào một mạng có thể phân biệt từ đầu đến cuối duy nhất. Nó được tạo ra và được duy trì trong khuôn khổ Darknet. YOLOv5 là mô hình YOLO đầu tiên được viết trên khung công tác PyTorch, nó nhẹ hơn và dễ sử dụng hơn nhiều. Tuy nhiên, YOLOv7 không vượt trội hơn YOLOv6 trên điểm chuẩn tiêu chuẩn, tập dữ liệu COCO, vì nó không thực hiện các cải tiến kiến trúc cơ bản cho mạng trong YOLOv6.
Yolo v6 có một vài sai sót, chẳng hạn như hiệu suất kém trên các mục nhỏ và khả năng tổng quát kém khi kích thước của các đối tượng không bằng nhau.
Đây là hình ảnh từ giấy YOLO gốc thể hiện hoạt động của YOLO. Nó đã trải qua một chặng đường dài kể từ đó, và chúng tôi hiện đang ở phiên bản 5. Mặc dù thực tế là nó không được viết bởi bất kỳ tác giả hoặc cộng tác viên ban đầu nào, nhưng nó vẫn tuân theo cùng một chiến lược cơ bản. Nó được viết bằng PyTorch, đó là một điểm cộng. Trong phiên bản này của Yolo mosaic, tăng cường được sử dụng, và các phương pháp tiếp cận mở rộng quy mô khác nhau cung cấp nhiều cải tiến.
Để thiết bị phát hiện đối tượng của bạn hoạt động, trước tiên bạn phải thu thập các bức ảnh huấn luyện. Bạn nên suy nghĩ cẩn thận về hoạt động bạn đang cố gắng hoàn thành và lên kế hoạch trước cho các thành phần của nhiệm vụ mà mô hình của bạn có thể cảm thấy khó khăn. Để cải thiện độ chính xác của mô hình cuối cùng của bạn, tôi khuyên bạn nên giảm miền mà mô hình của bạn phải xử lý càng nhiều càng tốt.
Đối với đào tạo tùy chỉnh YOLOv7, chúng tôi cần phát triển tập dữ liệu. Nếu bạn không có bất kỳ dữ liệu nào, bạn có thể sử dụng cơ sở dữ liệu openimages .
Sử dụng LabelImg hoặc bất kỳ công cụ chú thích nào để chú thích tập dữ liệu. Tạo một tệp có cùng tên với hình ảnh và văn bản chú thích.
Chuẩn bị một bộ, ví dụ, tương ứng với
YOLOv7 chấp nhận dữ liệu nhãn trong tệp văn bản (.txt) ở định dạng sau:
Sau khi bạn đã gắn thẻ dữ liệu của mình, chúng tôi sẽ chia dữ liệu đó thành các thư mục đào tạo và kiểm tra. Tỷ lệ phân chia sẽ do người dùng xác định, tuy nhiên tỷ lệ phân chia phổ biến nhất là (80-20) phần trăm, có nghĩa là 80 phần trăm dữ liệu được sử dụng để đào tạo và 20 phần trăm để thử nghiệm. * Hình ảnh và nhãn được lưu trữ trong cấu trúc thư mục đã nêu.
* Để phân chia dữ liệu, hãy xem thư viện python - Thư mục phân tách, sẽ phân chia ngẫu nhiên dữ liệu của bạn thành huấn luyện, kiểm tra và xác thực.
Lệnh pip sau để cài đặt thư viện tách dữ liệu
pip cài đặt các thư mục chia nhỏ
Thư mục đầu vào phải được định dạng như sau:
Để cung cấp cho bạn điều này:
Tách các tệp thành một tập huấn luyện và một tập xác thực (và tùy chọn là một tập thử nghiệm). Thư mục tập dữ liệu cuối cùng trông giống như bên dưới trước khi tham gia khóa đào tạo YOLOv7,
├── yolov7
## └── đào tạo
#### └── hình ảnh (thư mục bao gồm tất cả hình ảnh đào tạo)
#### └── nhãn (thư mục bao gồm tất cả nhãn đào tạo)
## └── kiểm tra
#### └── hình ảnh (thư mục bao gồm tất cả các hình ảnh thử nghiệm)
#### └── nhãn (thư mục bao gồm tất cả các nhãn thử nghiệm)
## └── hợp lệ
#### └── hình ảnh (thư mục bao gồm tất cả các hình ảnh hợp lệ)
### # └── nhãn (thư mục bao gồm tất cả các nhãn hợp lệ)
Bây giờ chúng ta phải phát triển một tệp cấu hình tùy chỉnh. (Đảm bảo chỉ định thư mục thích hợp), vì quá trình đào tạo sẽ hoàn toàn phụ thuộc vào tệp đó.
Tạo một tệp với tên “custom.yaml” trong thư mục (yolov7 / data). Trong tệp đó, hãy dán mã bên dưới. Đặt đường dẫn chính xác đến thư mục tập dữ liệu, thay đổi số lượng lớp và tên của chúng, sau đó lưu nó.
Tạo một tệp chỉ định cấu hình đào tạo. Trong tệp custom.yaml , hãy viết như sau:
train: (Complete path to dataset train folder)
test: (Complete path to dataset test folder)
valid: (Complete path to dataset valid folder)
#Classes
nc: 1 # replace classes count
#classes names
#replace all class names list with your custom classes
namesnames: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear']
Sau khi tất cả các thủ tục tiền xử lý đã được hoàn thành, nó đã sẵn sàng để bắt đầu đào tạo. Khởi chạy thiết bị đầu cuối trong “ yolov7 ” chính, kích hoạt môi trường ảo và thực hiện các lệnh được liệt kê bên dưới.
git clone https://github.com/WongKinYiu/yolov7.git # clone
cd yolov7
pip install -r requirements.txt # install modules
wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt # download pretrained weight
Kho lưu trữ chính thức chứa các trọng số được đào tạo trước cho mô hình của bạn.
Lưu ý :
Tùy thuộc vào ngữ cảnh sử dụng, chẳng hạn như Google Colab, bộ nhớ GPU có thể không đủ. Bạn có thể tìm hiểu bằng cách giảm kích thước lô trong tình huống đó.
python train.py --weights yolov7.pt --data "data/custom.yaml" --workers 4 --batch-size 4 --img 416 --cfg cfg/training/yolov7.yaml --name yolov7 --hyp data/hyp.scratch.p5.yaml
- img = kích thước của hình ảnh mà mô hình sẽ đào tạo, giá trị mặc định là 640.
- batch-size = kích thước lô được sử dụng để đào tạo tập dữ liệu tùy chỉnh.
- epochs = số kỷ nguyên đào tạo để có được mô hình tốt nhất
- data = đường dẫn tệp cấu hình tùy chỉnh
- weights = weights yolov7 được xử lý trước ( yolov7.pt )
Lưu ý : Nếu bất kỳ hình ảnh nào bị hỏng, quá trình đào tạo sẽ không bắt đầu. Nếu bất kỳ tệp nhãn nào bị hỏng, quá trình đào tạo sẽ không bắt đầu vì yolov7 sẽ bỏ qua tệp hình ảnh và nhãn đó.
Chờ cho quá trình tập luyện kết thúc trước khi thực hiện suy luận với các quả tạ mới hình thành. Các quả cân được đào tạo tùy chỉnh sẽ được lưu trong đường dẫn thư mục sau.
[yolov7/runs/train/yolov7/weights/best.pt]
Khi quá trình đào tạo kết thúc, hãy chuyển đến thiết bị đầu cuối và thực hiện lệnh được liệt kê bên dưới để phát hiện về trọng lượng tùy chỉnh.
python detect.py --weights runs/train/yolov7/weights/best.pt --source "path to your testing image"
Bạn có thể sử dụng YOLO để thiết kế mô hình phát hiện tùy chỉnh của riêng mình cho bất kỳ thứ gì bạn muốn.
Yolo v7 là một bước tiến đáng kể về tốc độ và độ chính xác, đồng thời nó phù hợp hoặc thậm chí vượt trội so với các mô hình dựa trên RPN. Mô hình này nhanh chóng và đáng tin cậy, và bây giờ nó có thể được sử dụng cho mọi thứ.
Đó là tất cả những gì cần có để “Đào tạo YOLOv7 về Dữ liệu tùy chỉnh”. Bạn có thể thử nghiệm với dữ liệu của riêng mình. YOLOv7 nhẹ và dễ sử dụng. YOLO v7 đào tạo nhanh chóng, đưa ra kết luận tốt và hoạt động tốt.
Các bài học chính từ YOLOV7 nói trên được tóm tắt như sau:
1640341828
Tìm hiểu cách đào tạo YOLOv5 trên tập dữ liệu tùy chỉnh của bạn
YOLO (Bạn Chỉ Nhìn Một Lần) là một trong những thuật toán phát hiện đối tượng dựa trên học sâu phổ biến nhất. Hôm nay chúng ta sẽ xem cách chúng ta có thể đào tạo máy dò đối tượng của riêng mình, sử dụng biến thể YOLO mới nhất, YOLOv5.
Chúng tôi sẽ sử dụng tập dữ liệu về biển báo.
Điều đầu tiên cần làm là sao chép kho lưu trữ của YOLOv5 GitHub và tải xuống các yêu cầu của nó (bạn có thể cần chạy sudo cho một số phụ thuộc).
Trong thiết bị đầu cuối, nhập:
git clone https://github.com/ultralytics/yolov5
Nếu bạn muốn chạy YOLOv5 trong một môi trường mới, đây là thời điểm thích hợp để tạo một môi trường mới.
Sau khi được kích hoạt, bước tiếp theo là cài đặt các phụ thuộc cần thiết.
pip install -r yolov5/requirements.txt
Để kết thúc quá trình thiết lập, sau khi các yêu cầu được cài đặt, hãy nhập các mô-đun mà chúng ta sẽ sử dụng.
import torch
from IPython.display import Image # for displaying images
import os
import random
import shutil
from sklearn.model_selection import train_test_split
import xml.etree.ElementTree as ET
from xml.dom import minidom
from tqdm import tqdm
from PIL import Image, ImageDraw
import numpy as np
import matplotlib.pyplot as plt
random.seed(42)
Chúng tôi sẽ sử dụng tập dữ liệu phát hiện biển báo đường bộ từ MakeML, bạn có thể tìm thấy nó tại đây .
Chúng tôi chủ yếu có bốn lớp: đèn giao thông, dừng, giới hạn tốc độ và băng qua đường.
Chúng tôi đang sử dụng một tập dữ liệu tương đối nhỏ (lý tưởng là bạn muốn một tập dữ liệu lớn hơn để khai thác đầy đủ các khả năng của YOLO), đây là để tạo mẫu mô hình nhanh, vì vậy quá trình đào tạo sẽ không mất nhiều thời gian và bạn có thể dễ dàng thử nghiệm với các siêu tham số khác nhau và cấu hình.
Bước tiếp theo là tạo một thư mục cho tập dữ liệu của chúng tôi:
mkdir road_sign_dataset
cd road_sign_dataset
Tiếp theo, tải xuống tập dữ liệu:
wget -O RoadSignDetectionDataset.zip https://arcraftimages.s3-accelerate.amazonaws.com/Datasets/RoadSigns/RoadSignsPascalVOC.zip?region=us-east-2
Và giải nén nó.
unzip RoadSignDetectionDataset.zip
Bây giờ chúng tôi đã tải xuống tập dữ liệu, chúng tôi cần chuyển đổi các chú thích sang định dạng được YOLO chấp nhận.
Đối với tập dữ liệu của chúng tôi, định dạng chú thích là PASCAL VOC XML, là định dạng rất phổ biến. Điều đó có nghĩa là bạn có thể dễ dàng tìm thấy các công cụ trực tuyến để chuyển đổi định dạng chú thích.
YOLO v5 mong đợi các chú thích cho mỗi hình ảnh ở dạng .txt
tệp trong đó mỗi dòng của tệp văn bản mô tả một hộp giới hạn, giống như trong hình ảnh:
Đó là một hàng cho mỗi đối tượng, với các thông số kỹ thuật sau:
class
x_center
y_center
width
height
định dạng.Bây giờ chúng ta sẽ viết một hàm trích xuất thông tin từ tệp VOC và lưu trữ nó vào từ điển.
# Function to get the data from XML Annotation
def extract_info_from_xml(xml_file):
root = ET.parse(xml_file).getroot()
# Initialise the info dict
info_dict = {}
info_dict['bboxes'] = []
# Parse the XML
for elem in root:
# Get the file name
if elem.tag == "filename":
info_dict['filename'] = elem.text
# Get the image size
elif elem.tag == "size":
image_size = []
for subelem in elem:
image_size.append(int(subelem.text))
info_dict['image_size'] = tuple(image_size)
# Get details of the bounding box
elif elem.tag == "object":
bbox = {}
for subelem in elem:
if subelem.tag == "name":
bbox["class"] = subelem.text
elif subelem.tag == "bndbox":
for subsubelem in subelem:
bbox[subsubelem.tag] = int(subsubelem.text)
info_dict['bboxes'].append(bbox)
return info_dict
Vui lòng kiểm tra nó trên một tệp chú thích.
Bây giờ chúng ta cần chuyển đổi thông tin được lưu trữ trong từ điển này thành chú thích kiểu YOLO v5 và ghi chúng vào một txt
tệp. Nếu bạn đã chọn một tập dữ liệu khác và bạn có một định dạng chú thích khác, bạn sẽ cần phải điều chỉnh hàm theo định dạng của riêng mình.
# Dictionary that maps class names to IDs
class_name_to_id_mapping = {"trafficlight": 0,
"stop": 1,
"speedlimit": 2,
"crosswalk": 3}
# Convert the info dict to the required yolo format and write it to disk
def convert_to_yolov5(info_dict):
print_buffer = []
# For each bounding box
for b in info_dict["bboxes"]:
try:
class_id = class_name_to_id_mapping[b["class"]]
except KeyError:
print("Invalid Class. Must be one from ", class_name_to_id_mapping.keys())
# Transform the bbox co-ordinates as per the format required by YOLO v5
b_center_x = (b["xmin"] + b["xmax"]) / 2
b_center_y = (b["ymin"] + b["ymax"]) / 2
b_width = (b["xmax"] - b["xmin"])
b_height = (b["ymax"] - b["ymin"])
# Normalise the co-ordinates by the dimensions of the image
image_w, image_h, image_c = info_dict["image_size"]
b_center_x /= image_w
b_center_y /= image_h
b_width /= image_w
b_height /= image_h
#Write the bbox details to the file
print_buffer.append("{} {:.3f} {:.3f} {:.3f} {:.3f}".format(class_id, b_center_x, b_center_y, b_width, b_height))
# Name of the file which we have to save
save_file_name = os.path.join("annotations", info_dict["filename"].replace("png", "txt"))
# Save the annotation to disk
print("\n".join(print_buffer), file= open(save_file_name, "w"))
Cuối cùng, chúng tôi chuyển đổi tất cả các xml
chú thích thành các chú thích kiểu YOLO txt
.
# Get the annotations
annotations = [os.path.join('annotations', x) for x in os.listdir('annotations') if x[-3:] == "xml"]
annotations.sort()
# Convert and save the annotations
for ann in tqdm(annotations):
info_dict = extract_info_from_xml(ann)
convert_to_yolov5(info_dict)
annotations = [os.path.join('annotations', x) for x in os.listdir('annotations') if x[-3:] == "txt"]
Hãy lưu ý, nếu tập dữ liệu bạn chọn không đi kèm với chú thích, bạn không thể thoát khỏi tập dữ liệu đó, bạn sẽ cần phải làm điều đó theo cách thủ công.
May mắn cho bạn, Internet cung cấp các giải pháp khác nhau để gắn nhãn tập dữ liệu của bạn theo cách thủ công:
Ngoài ra, nếu bạn bắt gặp tập dữ liệu có chú thích tương thích với YOLO, bạn không cần tất cả mớ hỗn độn này, về cơ bản bạn có thể bỏ qua chương đào tạo.
Như mọi khi, điều rất quan trọng là phải chia bộ dữ liệu của bạn thành các thư mục huấn luyện, xác thực và thử nghiệm. Chúng tôi sẽ thực hiện phương pháp tiếp cận 80%, 10%, 10%, nhưng hãy thoải mái thay đổi nó!
# Read images and annotations
images = [os.path.join('images', x) for x in os.listdir('images')]
annotations = [os.path.join('annotations', x) for x in os.listdir('annotations') if x[-3:] == "txt"]
images.sort()
annotations.sort()
# Split the dataset into train-valid-test splits
train_images, val_images, train_annotations, val_annotations = train_test_split(images, annotations, test_size = 0.2, random_state = 42)
val_images, test_images, val_annotations, test_annotations = train_test_split(val_images, val_annotations, test_size = 0.5,
Bây giờ chúng ta cần các thư mục để lưu trữ phiên bản mới của tập dữ liệu
!mkdir images/train images/val images/test annotations/train annotations/val annotations/test
Và bây giờ chúng ta hãy di chuyển các hình ảnh!
def move_files_to_folder(list_of_files, destination_folder):
for f in list_of_files:
try:
shutil.move(f, destination_folder)
except:
print(f)
assert False
# Move the splits into their folders
move_files_to_folder(train_images, 'images/train')
move_files_to_folder(val_images, 'images/val/')
move_files_to_folder(test_images, 'images/test/')
move_files_to_folder(train_annotations, 'annotations/train/')
move_files_to_folder(val_annotations, 'annotations/val/')
move_files_to_folder(test_annotations, 'annotations/test/')
Đổi tên annotations
thư mục thành labels
, vì đây là nơi YOLO v5 mong đợi các chú thích được đặt trong đó.
Bây giờ tập dữ liệu của chúng tôi đã sẵn sàng, chúng tôi cần đào tạo trình phát hiện đối tượng tùy chỉnh của mình!
Chúng tôi sẽ sử dụng chính kho lưu trữ yolov5, nhưng trước tiên hãy xem các cờ mà chúng tôi sẽ sử dụng.
img
: kích thước của hình ảnh, nó là một hình vuông.batch
: kích thước lôepochs
: số kỷ nguyêndata
: tệp dữ liệu YAML chứa thông tin về tập dữ liệu (đường dẫn của hình ảnh, nhãn)workers
: số lượng công nhân CPUcfg
: Tập tin mô hình kiến trúc cấu hình, có 4 lựa chọn có sẵn: yolo5s.yaml
, yolov5m.yaml
, yolov5l.yaml
, yolov5x.yaml
. Kích thước và độ phức tạp của các mô hình này tăng dần theo thứ tự tăng dần và bạn có thể chọn một mô hình phù hợp với mức độ phức tạp của nhiệm vụ phát hiện đối tượng của mình.weights
: Mức tạ đã luyện trước mà bạn muốn bắt đầu tập luyện. Nếu bạn muốn đào tạo từ đầu, hãy sử dụng--weights ' '
name
: nhiều thứ khác nhau về đào tạo, chẳng hạn như nhật ký tàu. Trọng lượng đào tạo sẽ được lưu trữ trong một thư mục có tênruns/train/name
hyp
: Tệp YAML mô tả các lựa chọn siêu tham số. Nếu không xác định, tệp data/hyp.scratch.yaml
được sử dụng.Tạo một tệp mới có tên road_sign_data.yaml
và đặt nó vào yolov5/data
thư mục. Sau đó, điền vào nó với những điều sau đây.
YOLO v5 hy vọng sẽ tìm thấy nhãn đào tạo cho hình ảnh trong thư mục có tên có thể được lấy bằng cách thay thế “hình ảnh” bằng “nhãn” trong đường dẫn đến hình ảnh tập dữ liệu. Ví dụ, trong ví dụ trên, YOLO v5 sẽ tìm kiếm các nhãn tàu trong ../road_sign_dataset/labels/train/
.
Để hướng dẫn dễ dàng và ngắn gọn, chúng ta sẽ không xem xét tệp cấu hình Hyperparameters hoặc tệp Kiến trúc. Nếu cần, bạn cũng có thể tinh chỉnh chúng.
Thật khôn ngoan khi bắt đầu với mô hình nhỏ nhất, vì vậy chúng tôi có thể thấy nó hoạt động như thế nào với tập dữ liệu của chúng tôi và chúng tôi có thể mở rộng quy mô từ đó.
Để bắt đầu quá trình đào tạo, hãy đảm bảo rằng bạn đang ở trong thư mục của repo yolov5 và chỉ cần từ terminal:
python train.py --img 640 --cfg yolov5s.yaml --hyp hyp.scratch.yaml --batch 32 --epochs 100 --data road_sign_data.yaml --weights yolov5s.pt --workers 24 --name yolo_road_det
Bạn có thể sửa đổi nó theo nhu cầu của bạn. Có thể mất đến 30 phút để hoàn thành, nhưng điều đó phụ thuộc vào phần cứng của bạn.
Chà, bây giờ bạn đã có máy dò đối tượng của riêng mình! Xin chúc mừng vì đã đi xa đến thế này!
Bây giờ, tôi tin rằng bạn muốn thử nghiệm nó và có nhiều cách khác nhau để làm như vậy, sử dụng detect.py
tệp (hình ảnh đơn, thư mục, video, webcam).
python detect.py --source ../road_sign_dataset/images/test/ --weights runs/train/yolo_road_det/weights/best.pt --conf 0.25 --name yolo_road_det
source
: nó xác định nguồn của bài kiểm tra suy luận, hơn thế nữa trong một phút.weights
: “Best.pt” chứa các trọng lượng hoạt động tốt nhất được lưu trong quá trình luyện tập.conf
: nó xác định ngưỡng tin cậy.name
: nơi lưu trữ các phát hiện.Ngoài thư mục tập dữ liệu, bạn có thể sử dụng các đầu vào khác cho cờ nguồn.
Bây giờ bạn đã hoàn thành! Bạn có Máy dò tìm đối tượng của riêng mình!
1659852060
Curly is a template language that completely separates structure and logic. Instead of interspersing your HTML with snippets of Ruby, all logic is moved to a presenter class.
Installing Curly is as simple as running gem install curly-templates
. If you're using Bundler to manage your dependencies, add this to your Gemfile
gem 'curly-templates'
Curly can also install an application layout file, replacing the .erb file commonly created by Rails. If you wish to use this, run the curly:install
generator.
$ rails generate curly:install
In order to use Curly for a view or partial, use the suffix .curly
instead of .erb
, e.g. app/views/posts/_comment.html.curly
. Curly will look for a corresponding presenter class named Posts::CommentPresenter
. By convention, these are placed in app/presenters/
, so in this case the presenter would reside in app/presenters/posts/comment_presenter.rb
. Note that presenters for partials are not prepended with an underscore.
Add some HTML to the partial template along with some Curly components:
<!-- app/views/posts/_comment.html.curly -->
<div class="comment">
<p>
{{author_link}} posted {{time_ago}} ago.
</p>
{{body}}
{{#author?}}
<p>{{deletion_link}}</p>
{{/author?}}
</div>
The presenter will be responsible for providing the data for the components. Add the necessary Ruby code to the presenter:
# app/presenters/posts/comment_presenter.rb
class Posts::CommentPresenter < Curly::Presenter
presents :comment
def body
SafeMarkdown.render(@comment.body)
end
def author_link
link_to @comment.author.name, @comment.author, rel: "author"
end
def deletion_link
link_to "Delete", @comment, method: :delete
end
def time_ago
time_ago_in_words(@comment.created_at)
end
def author?
@comment.author == current_user
end
end
The partial can now be rendered like any other, e.g. by calling
render 'comment', comment: comment
render comment
render collection: post.comments
Curly components are surrounded by curly brackets, e.g. {{hello}}
. They always map to a public method on the presenter class, in this case #hello
. Methods ending in a question mark can be used for conditional blocks, e.g. {{#admin?}} ... {{/admin?}}
.
Curly components can specify an identifier using the so-called dot notation: {{x.y.z}}
. This can be very useful if the data you're accessing is hierarchical in nature. One common example is I18n:
<h1>{{i18n.homepage.header}}</h1>
# In the presenter, the identifier is passed as an argument to the method. The
# argument will always be a String.
def i18n(key)
translate(key)
end
The identifier is separated from the component name with a dot. If the presenter method has a default value for the argument, the identifier is optional – otherwise it's mandatory.
In addition to an identifier, Curly components can be annotated with attributes. These are key-value pairs that affect how a component is rendered.
The syntax is reminiscent of HTML:
<div>{{sidebar rows=3 width=200px title="I'm the sidebar!"}}</div>
The presenter method that implements the component must have a matching keyword argument:
def sidebar(rows: "1", width: "100px", title:); end
All argument values will be strings. A compilation error will be raised if
You can define default values using Ruby's own syntax. Additionally, if the presenter method accepts arbitrary keyword arguments using the **doublesplat
syntax then all attributes will be valid for the component, e.g.
def greetings(**names)
names.map {|name, greeting| "#{name}: #{greeting}!" }.join("\n")
end
{{greetings alice=hello bob=hi}}
<!-- The above would be rendered as: -->
alice: hello!
bob: hi!
Note that since keyword arguments in Ruby are represented as Symbol objects, which are not garbage collected in Ruby versions less than 2.2, accepting arbitrary attributes represents a security vulnerability if your application allows untrusted Curly templates to be rendered. Only use this feature with trusted templates if you're not on Ruby 2.2 yet.
If there is some content you only want rendered under specific circumstances, you can use conditional blocks. The {{#admin?}}...{{/admin?}}
syntax will only render the content of the block if the admin?
method on the presenter returns true, while the {{^admin?}}...{{/admin?}}
syntax will only render the content if it returns false.
Both forms can have an identifier: {{#locale.en?}}...{{/locale.en?}}
will only render the block if the locale?
method on the presenter returns true given the argument "en"
. Here's how to implement that method in the presenter:
class SomePresenter < Curly::Presenter
# Allows rendering content only if the locale matches a specified identifier.
def locale?(identifier)
current_locale == identifier
end
end
Furthermore, attributes can be set on the block. These only need to be specified when opening the block, not when closing it:
{{#square? width=3 height=3}}
<p>It's square!</p>
{{/square?}}
Attributes work the same way as they do for normal components.
Sometimes you want to render one or more items within the current template, and splitting out a separate template and rendering that in the presenter is too much overhead. You can instead define the template that should be used to render the items inline in the current template using the collection block syntax.
Collection blocks are opened using an asterisk:
{{*comments}}
<li>{{body}} ({{author_name}})</li>
{{/comments}}
The presenter will need to expose the method #comments
, which should return a collection of objects:
class Posts::ShowPresenter < Curly::Presenter
presents :post
def comments
@post.comments
end
end
The template within the collection block will be used to render each item, and it will be backed by a presenter named after the component – in this case, comments
. The name will be singularized and Curly will try to find the presenter class in the following order:
Posts::ShowPresenter::CommentPresenter
Posts::CommentPresenter
CommentPresenter
This allows you some flexibility with regards to how you want to organize these nested templates and presenters.
Note that the nested template will only have access to the methods on the nested presenter, but all variables passed to the "parent" presenter will be forwarded to the nested presenter. In addition, the current item in the collection will be passed, as well as that item's index in the collection:
class Posts::CommentPresenter < Curly::Presenter
presents :post, :comment, :comment_counter
def number
# `comment_counter` is automatically set to the item's index in the collection,
# starting with 1.
@comment_counter
end
def body
@comment.body
end
def author_name
@comment.author.name
end
end
Collection blocks are an alternative to splitting out a separate template and rendering that from the presenter – which solution is best depends on your use case.
While collection blocks allow you to define the template that should be used to render items in a collection right within the parent template, context blocks allow you to define the template for an arbitrary context. This is very powerful, and can be used to define widget-style components and helpers, and provide an easy way to work with structured data. Let's say you have a comment form on your page, and you'd rather keep the template inline. A simple template could look like:
<!-- post.html.curly -->
<h1>{{title}}</h1>
{{body}}
{{@comment_form}}
<b>Name: </b> {{name_field}}<br>
<b>E-mail: </b> {{email_field}}<br>
{{comment_field}}
{{submit_button}}
{{/comment_form}}
Note that an @
character is used to denote a context block. Like with collection blocks, a separate presenter class is used within the block, and a simple convention is used to find it. The name of the context component (in this case, comment_form
) will be camel cased, and the current presenter's namespace will be searched:
class PostPresenter < Curly::Presenter
presents :post
def title; @post.title; end
def body; markdown(@post.body); end
# A context block method *must* take a block argument. The return value
# of the method will be used when rendering. Calling the block argument will
# render the nested template. If you pass a value when calling the block
# argument it will be passed to the presenter.
def comment_form(&block)
form_for(Comment.new, &block)
end
# The presenter name is automatically deduced.
class CommentFormPresenter < Curly::Presenter
# The value passed to the block argument will be passed in a parameter named
# after the component.
presents :comment_form
# Any parameters passed to the parent presenter will be forwarded to this
# presenter as well.
presents :post
def name_field
@comment_form.text_field :name
end
# ...
end
end
Context blocks were designed to work well with Rails' helper methods such as form_for
and content_tag
, but you can also work directly with the block. For instance, if you want to directly control the value that is passed to the nested presenter, you can call the call
method on the block yourself:
def author(&block)
content_tag :div, class: "author" do
# The return value of `call` will be the result of rendering the nested template
# with the argument. You can post-process the string if you want.
block.call(@post.author)
end
end
If you find yourself opening a context block just in order to use a single component, e.g. {{@author}}{{name}}{{/author}}
, you can use the shorthand syntax instead: {{author:name}}
. This works for all component types, e.g.
{{#author:admin?}}
<p>The author is an admin!</p>
{{/author:admin?}}
The syntax works for nested contexts as well, e.g. {{comment:author:name}}
. Any identifier and attributes are passed to the target component, which in this example would be {{name}}
.
Although most code in Curly presenters should be free of side effects, sometimes side effects are required. One common example is defining content for a content_for
block.
If a Curly presenter class defines a setup!
method, it will be called before the view is rendered:
class PostPresenter < Curly::Presenter
presents :post
def setup!
content_for :title, post.title
content_for :sidebar do
render 'post_sidebar', post: post
end
end
end
In order to have {{
appear verbatim in the rendered HTML, use the triple Curly escape syntax:
This is {{{escaped}}.
You don't need to escape the closing }}
.
If you want to add comments to your Curly templates that are not visible in the rendered HTML, use the following syntax:
{{! This is some interesting stuff }}
Presenters are classes that inherit from Curly::Presenter
– they're usually placed in app/presenters/
, but you can put them anywhere you'd like. The name of the presenter classes match the virtual path of the view they're part of, so if your controller is rendering posts/show
, the Posts::ShowPresenter
class will be used. Note that Curly is only used to render a view if a template can be found – in this case, at app/views/posts/show.html.curly
.
Presenters can declare a list of accepted variables using the presents
method:
class Posts::ShowPresenter < Curly::Presenter
presents :post
end
A variable can have a default value:
class Posts::ShowPresenter < Curly::Presenter
presents :post
presents :comment, default: nil
end
Any public method defined on the presenter is made available to the template as a component:
class Posts::ShowPresenter < Curly::Presenter
presents :post
def title
@post.title
end
def author_link
# You can call any Rails helper from within a presenter instance:
link_to author.name, profile_path(author), rel: "author"
end
private
# Private methods are not available to the template, so they're safe to
# use.
def author
@post.author
end
end
Presenter methods can even take an argument. Say your Curly template has the content {{t.welcome_message}}
, where welcome_message
is an I18n key. The following presenter method would make the lookup work:
def t(key)
translate(key)
end
That way, simple ``functions'' can be added to the Curly language. Make sure these do not have any side effects, though, as an important part of Curly is the idempotence of the templates.
Both layouts and content blocks (see content_for
) use yield
to signal that content can be inserted. Curly works just like ERB, so calling yield
with no arguments will make the view usable as a layout, while passing a Symbol will make it try to read a content block with the given name:
# Given you have the following Curly template in
# app/views/layouts/application.html.curly
#
# <html>
# <head>
# <title>{{title}}</title>
# </head>
# <body>
# <div id="sidebar">{{sidebar}}</div>
# {{body}}
# </body>
# </html>
#
class ApplicationLayout < Curly::Presenter
def title
"You can use methods just like in any other presenter!"
end
def sidebar
# A view can call `content_for(:sidebar) { "some HTML here" }`
yield :sidebar
end
def body
# The view will be rendered and inserted here:
yield
end
end
In order to make a Rails helper method available as a component in your template, use the exposes_helper
method:
class Layouts::ApplicationPresenter < Curly::Presenter
# The components {{sign_in_path}} and {{root_path}} are made available.
exposes_helper :sign_in_path, :root_path
end
Presenters can be tested directly, but sometimes it makes sense to integrate with Rails on some levels. Currently, only RSpec is directly supported, but you can easily instantiate a presenter:
SomePresenter.new(context, assigns)
context
is a view context, i.e. an object that responds to render
, has all the helper methods you expect, etc. You can pass in a test double and see what you need to stub out. assigns
is the hash containing the controller and local assigns. You need to pass in a key for each argument the presenter expects.
In order to test presenters with RSpec, make sure you have rspec-rails
in your Gemfile. Given the following presenter:
# app/presenters/posts/show_presenter.rb
class Posts::ShowPresenter < Curly::Presenter
presents :post
def body
Markdown.render(@post.body)
end
end
You can test the presenter methods like this:
# You can put this in your `spec_helper.rb`.
require 'curly/rspec'
# spec/presenters/posts/show_presenter_spec.rb
describe Posts::ShowPresenter, type: :presenter do
describe "#body" do
it "renders the post's body as Markdown" do
assign(:post, double(:post, body: "**hello!**"))
expect(presenter.body).to eq "<strong>hello!</strong>"
end
end
end
Note that your spec must be tagged with type: :presenter
.
Here is a simple Curly template – it will be looked up by Rails automatically.
<!-- app/views/posts/show.html.curly -->
<h1>{{title}}<h1>
<p class="author">{{author}}</p>
<p>{{description}}</p>
{{comment_form}}
<div class="comments">
{{comments}}
</div>
When rendering the template, a presenter is automatically instantiated with the variables assigned in the controller or the render
call. The presenter declares the variables it expects with presents
, which takes a list of variables names.
# app/presenters/posts/show_presenter.rb
class Posts::ShowPresenter < Curly::Presenter
presents :post
def title
@post.title
end
def author
link_to(@post.author.name, @post.author, rel: "author")
end
def description
Markdown.new(@post.description).to_html.html_safe
end
def comments
render 'comment', collection: @post.comments
end
def comment_form
if @post.comments_allowed?
render 'comment_form', post: @post
else
content_tag(:p, "Comments are disabled for this post")
end
end
end
Caching is handled at two levels in Curly – statically and dynamically. Static caching concerns changes to your code and templates introduced by deploys. If you do not wish to clear your entire cache every time you deploy, you need a way to indicate that some view, helper, or other piece of logic has changed.
Dynamic caching concerns changes that happen on the fly, usually made by your users in the running system. You wish to cache a view or a partial and have it expire whenever some data is updated – usually whenever a specific record is changed.
Because of the way logic is contained in presenters, caching entire views or partials by the data they present becomes exceedingly straightforward. Simply define a #cache_key
method that returns a non-nil object, and the return value will be used to cache the template.
Whereas in ERB you would include the cache
call in the template itself:
<% cache([@post, signed_in?]) do %>
...
<% end %>
In Curly you would instead declare it in the presenter:
class Posts::ShowPresenter < Curly::Presenter
presents :post
def cache_key
[@post, signed_in?]
end
end
Likewise, you can add a #cache_duration
method if you wish to automatically expire the fragment cache:
class Posts::ShowPresenter < Curly::Presenter
...
def cache_duration
30.minutes
end
end
In order to set any cache option, define a #cache_options
method that returns a Hash of options:
class Posts::ShowPresenter < Curly::Presenter
...
def cache_options
{ compress: true, namespace: "my-app" }
end
end
Static caching will only be enabled for presenters that define a non-nil #cache_key
method (see Dynamic Caching.)
In order to make a deploy expire the cache for a specific view, set the version
of the view to something new, usually by incrementing by one:
class Posts::ShowPresenter < Curly::Presenter
version 3
def cache_key
# Some objects
end
end
This will change the cache keys for all instances of that view, effectively expiring the old cache entries.
This works well for views, or for partials that are rendered in views that themselves are not cached. If the partial is nested within a view that is cached, however, the outer cache will not be expired. The solution is to register that the inner partial is a dependency of the outer one such that Curly can automatically deduce that the outer partial cache should be expired:
class Posts::ShowPresenter < Curly::Presenter
version 3
depends_on 'posts/comment'
def cache_key
# Some objects
end
end
class Posts::CommentPresenter < Curly::Presenter
version 4
def cache_key
# Some objects
end
end
Now, if the version
of Posts::CommentPresenter
is bumped, the cache keys for both presenters would change. You can register any number of view paths with depends_on
.
Curly integrates well with the caching mechanism in Rails 4 (or Cache Digests in Rails 3), so the dependencies defined with depends_on
will be tracked by Rails. This will allow you to deploy changes to your templates and have the relevant caches automatically expire.
Thanks to Zendesk for sponsoring the work on Curly.
Author: zendesk
Source code: https://github.com/zendesk/curly
1626331037
WordPress caching has nothing new to showcase in this context. WordPress websites also run on a specific server system and you have to make sure these servers work well for user engagement. So caching can help your website server work effectively to serve too many visitors collectively. The commonly requested items can be converted into varied copies that the website server doesn’t want to showcase every time to every website visitor. Classification of Caching is usually divided into two kinds. The Client-Side Caching & the Server Side Caching. Where client-side caching has nothing to do with your website, Server Side Caching is usually its opposite. Read more on https://bit.ly/3rbqvVh
#caching plugins #server side caching #client side caching #wordpress websites #wordpress caching
1685237760
Trong hướng dẫn Laravel này, chúng ta sẽ tìm hiểu về Cách tạo Mô hình lặp lại với Mô hình định kỳ Laravel
Đôi khi, bạn cần lặp lại dữ liệu theo khoảng thời gian ngày, chẳng hạn như cuộc hẹn trên lịch cố định, sự kiện hàng tuần hoặc lịch thanh toán. Gói Mô hình định kỳ cho Laravel là giải pháp tối ưu để thêm chức năng định kỳ vào Mô hình Laravel của bạn:
$model->repeat()->daily();
$model->repeat()->weekly();
$model->repeat()->weekly()
->on(['sunday', 'monday', 'tuesday']);
Khi bạn lặp lại một mô hình, ngày bắt đầu được tính bằng phương thức startedAt() trên mô hình. Hoặc bạn có thể chỉ định ngày bắt đầu như sau:
$model->repeat()->daily()->startsAt(Carbon::make());
Bạn cũng có thể đặt ngày kết thúc khi mô hình lặp lại sẽ dừng:
$model->repeat()->daily()->endsAt(
Carbon::make('2023-06-01')
);
Và khi bạn có dữ liệu định kỳ, bạn có thể truy xuất dữ liệu đó bằng phạm vi được cung cấp của gói:
$tasks = Task::whereOccurresOn(
Carbon::make('2023-05-01')
)->get();
// Between
$tasks = Task::whereOccurresBetween(
Carbon::make('2023-05-01'),
Carbon::make('2023-05-30')
)->get();
Gói mới này có thể đóng vai trò là nguồn cảm hứng tuyệt vời để đơn giản hóa các mô hình cần lặp lại ngày tháng. Để bắt đầu với gói này, hãy kiểm tra mã nguồn . trên GitHub.
Nguồn bài viết: https://laravel-news.com