23.06.20 - 23.07.03
YOLO, Retinanet, Faster R-CNN์ ์ด์ฉํ์ฌ ์๋ฌผ ๋ณ์ถฉํด๋ฅผ ํ์งํ๋ DETECTOR๋ฅผ ์ ์ํ๊ณ ์ธ ๋ชจ๋ธ ๊ฐ์ ์ฑ๋ฅ๋น๊ต์ one stage detector์ two stage detector ๊ฐ์ ์ฑ๋ฅ ๋น๊ต๋ฅผ ์งํํ๋ค.
Ai-hub์์ ์ ๊ณตํ๋ dataset ์ค ํ ๋งํ ์ data๋ฅผ ์ถ์ถํ์ฌ ์ฌ์ฉํ๋ค. ๋งํฌ : Ai-hub "์์ค ์๋ฌผ ์ง๋ณ ์ง๋จ ์ด๋ฏธ์ง"
๋จผ์ , ์ ์ ์๋ฌผ์ ๋ํด ์ธ ๊ฐ์ง ๋ชจ๋ธ๋ก ํ์งํ ๊ฒฐ๊ณผ์ ๋๋ค. Faster R-CNN๋ง์ด ์ ์ ์๋ฌผ๋ก ํ์งํ๊ณ YOLOv8๊ณผ RetinaNet์ ํฉํ์๋ง์ด๋ฐ์ด๋ฌ์ค๋ก ํ์งํ์ต๋๋ค. ์๋์ ์ผ๋ก ์ ์ ์ด๋ฏธ์ง ๊ฐ์๊ฐ ์ ์๊ณ ๋ง๋ ค์๋ฏํ ์ ๋๋ฌธ์ ์ง๋ณ2๋ก ํ์งํด๋์์ ์ ์ ์์์ต๋๋ค.
Faster R-CNN | YOLOv8 | RetinaNet |
---|---|---|
๋ค์์ ์ฒซ ๋ฒ์งธ ์ง๋ณ์ธ ํ ๋งํ ์ ๊ณฐํก์ด๋ณ์ ๋๋ค. ์ ๋ท๋ฉด์ ๋ณ๋ฐ์ด ์๊ธฐ๊ณ ๊ฐ์ ๊ณฐํก์ด๊ฐ ์๊ธฐ๋ฉด์ ํฉ์์ผ๋ก ๋ณํ๋ ํน์ง์ด ์์ต๋๋ค. ์ธ ํ์ง ๋ชจ๋ธ ๋ชจ๋ ์ ํ์งํด๋์ต๋๋ค.
Faster R-CNN | YOLOv8 | RetinaNet |
---|---|---|
๊ทธ๋ฆฌ๊ณ ๋ ๋ฒ์งธ ์ง๋ณ์ธ ํฉํ์๋ง์ด๋ฐ์ด๋ฌ์ค์ ๋๋ค. ํฉํ์๋ง์ด๋ฐ์ด๋ฌ์ค๋ ์ด๋ฆฐ์์ด ์ชผ๊ทธ๋ผ๋ค๊ณ ๋ ธ๋๊ฒ ๋ณํ๋ค๋ ํน์ง์ด ์์ต๋๋ค. ์ธ ํ์ง ๋ชจ๋ธ ๋ชจ๋ ์ ํ์งํด๋์ต๋๋ค.
Faster R-CNN | YOLOv8 | RetinaNet |
---|---|---|
!pip install ultralytics
import ultralytics
ultralytics.checks()
from ultralytics import YOLO
model = YOLO('yolov8s.pt')
model.train(data = '/content/drive/MyDrive/dataset_root/data.yaml', epochs = 10, batch= 16)
import cv2
from google.colab.patches import cv2_imshow
%cd '/content/drive/MyDrive/ultralytics'
original_image = cv2.imread('/content/drive/MyDrive/KakaoTalk_20230707_143733806.jpg')
model = YOLO('/content/drive/MyDrive/BEST.pt (class7 & class 3)/runs/detect/train3_แแ
ฅแผแแ
ฉแผ class 3แแ
ข/weights/best.pt')
result = model.predict(source='/content/drive/MyDrive/KakaoTalk_20230707_143733806.jpg', save=True, imgsz=640)
# ์ด๋ฏธ์ง ์ถ๋ ฅ
cv2_imshow(original_image)
# ์ด๋ฏธ์ง ๋ก๋
image = cv2.imread('/content/drive/MyDrive/ultralytics/runs/detect/predict24/KakaoTalk_20230707_143733806.jpg')
# ์ด๋ฏธ์ง ์ถ๋ ฅ
cv2_imshow(image)
#20 epoch, 4 batch ํ์ต
#class ๋ถ๋ฅ
#0 : background
#1 : ์ ์
#2 : 18๋ฒ ์ง๋ณ - ํ ๋งํ ์๊ณฐํก์ด๋ณ
#3 : 19๋ฒ ์ง๋ณ - ํ ๋งํ ํฉํ์๋ง์ด๋ฐ์ด๋ฌ์ค
detection_threshold = 0.4
for i in range(len(test_images)):
image_name = test_images[i].split('\\')[-1].split('.')[0]
image = cv2.imread(test_images[i])
orig_image = image.copy()
image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB).astype(float)
image /= 255.0
image = np.transpose(image, (2, 0, 1)).astype(float)
image = torch.tensor(image, dtype=torch.float).cuda()
image = torch.unsqueeze(image, 0)
with torch.no_grad():
outputs = model(image)
outputs = [{k: v.to('cpu') for k, v in t.items()} for t in outputs]
if len(outputs[0]['boxes']) != 0:
boxes = outputs[0]['boxes'].data.numpy()
scores = outputs[0]['scores'].data.numpy()
boxes = boxes[scores >= detection_threshold].astype(np.int32)
draw_boxes = boxes.copy()
pred_classes = [CLASSES[i] for i in outputs[0]['labels'].cpu().numpy()]
for j, box in enumerate(draw_boxes):
cv2.rectangle(orig_image,
(int(box[0]), int(box[1])),
(int(box[2]), int(box[3])),
(0, 0, 255), 2)
cv2.putText(orig_image, str(pred_classes[j]),
(int(box[0]), int(box[1] - 5)),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0),
5, lineType=cv2.LINE_AA)
cv2.imshow('Prediction', orig_image)
cv2.waitKey(1)
cv2.imwrite(f"../test_predictions/{image_name}.jpg", orig_image, )
print(f"Image {i + 1} done...")
print('-' * 50)
print('TEST PREDICTIONS COMPLETE')
cv2.destroyAllWindows()
# load retinanet model
model = models.load_model('/content/drive/MyDrive/tomato/resnet50_csv_13_infer.h5', backbone_name='resnet50')
# load label to names mapping for visualization purposes
labels_to_names = {0:'normal',1:'disease1',2:'disease2'}
# load image
image = read_image_bgr('/content/test.JPG')
# copy to draw on
draw = image.copy()
draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
# preprocess image for network
image = preprocess_image(image)
image, scale = resize_image(image)
# process image
boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
# correct for image scale
boxes /= scale
for box, score, label in zip(boxes[0], scores[0], labels[0]):
# scores are sorted so we can break
if score < 0.45:
break
color = label_color(label)
b = box.astype(int)
draw_box(draw, b, color=color)
caption = "{}".format(labels_to_names[label])
cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 5, (0, 0, 0), 3)
cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 5, (255, 255, 255), 2)
plt.figure(figsize=(15, 15))
plt.axis('off')
plt.imshow(draw)
plt.show()
์์ธํ ๋ด์ฉ์ ๋ค์ ppt ์๋ฃ๋ฅผ ์ฐธ๊ณ ํด์ฃผ์ธ์. 23 ๋ด ๊ธฐ์ ์คํ ์ธ๋ฏธ๋ ๋ฐํ ์๋ฃ_์ฝค๋น๋ค์ด์ .pdf