yolov5s pretrained

This commit is contained in:
Jakub Pokrywka 2022-08-02 10:09:06 +00:00
commit c16fff0818
8 changed files with 94 additions and 0 deletions

45
change-to-yolo-format.py Normal file
View File

@ -0,0 +1,45 @@
# start od 0
import cv2
from labels_list import labels_list
from tqdm import tqdm
import sys
dataset = sys.argv[1]
def to_yolo_format(entity, y_total, x_total):
label_str, ref = entity.split(':')
label_id = labels_list.index(label_str)
x0,y0,x1,y1 = [float(i) for i in ref.split(',')]
x_middle = (x0 + x1) / 2
y_middle = (y0 + y1) / 2
x_w = x1 - x0
y_w = y1 - y0
x_middle /= x_total
y_middle /= y_total
x_w /= x_total
y_w /= y_total
items_to_write = (label_id, x_middle, y_middle, x_w, y_w)
items_to_write = [str(i) for i in items_to_write]
str_to_write = ' '.join(items_to_write) + '\n'
return str_to_write
with open(f'{dataset}/expected.tsv','r') as f_exp, open(f'{dataset}/in.tsv') as f_in:
for line_in, line_exp in zip(f_in, f_exp):
img_name = line_in.rstrip()
img_references = line_exp.rstrip().split(' ')
img = cv2.imread('images/' + img_name)
img_dimensions = img.shape
y_total, x_total, _ = img_dimensions
with open(f'{dataset}-dataset/labels/' + img_name.replace('.jpg','.txt'), 'w') as f_out:
for entity in img_references:
str_to_write = to_yolo_format(entity, y_total, x_total)
f_out.write(str_to_write)

View File

@ -0,0 +1 @@
python ~/yolov5/train.py --img 640 --project merge --name run --batch 192 --epochs 300 --data cfg.yaml --weights 'yolov5s.pt' --cfg yolov5s.yaml --cache ram

View File

@ -0,0 +1,6 @@
python ~/yolov5/detect.py --source ../../dev-0-dataset/images --weights ./merge/run/weights/best.pt --name dev-0 --project predictions --save-txt --max-det 50 --save-conf \
--conf-thres 0.2
python ~/yolov5/detect.py --source ../../test-A-dataset/images --weights ./merge/run/weights/best.pt --name test-A --project predictions --save-txt --max-det 50 --save-conf \
--conf-thres 0.2

View File

@ -0,0 +1,2 @@
for i in ./predictions/dev-0/labels/*txt ; do python change_to_nonrelative.py $i $i-nonrelative ; done
for i in ./predictions/test-A/labels/*txt ; do python change_to_nonrelative.py $i $i-nonrelative ; done

View File

@ -0,0 +1,10 @@
# stworz format pliku wyjsciowego
for d in 'dev-0', 'test-A':
with open(f'../../{d}/in.tsv') as f_in, open(f'../../{d}/out.tsv','w') as f_out:
for line in f_in:
pred_path = 'predictions/dev-0/labels/' + line.rstrip().replace('jpg','txt') + '-nonrelative'
try:
s = open(pred_path).read()
f_out.write(s + '\n')
except FileNotFoundError:
f_out.write('\n')

View File

@ -0,0 +1,28 @@
import sys
import cv2
from labels_list import labels_list
im_name = sys.argv[1].split('/')[-1].split('.')[0] + '.jpg'
if 'dev-0' in sys.argv[1]:
im_path = '../../dev-0-dataset/images/' + im_name
elif 'test-A' in sys.argv[1]:
im_path = '../../test-A-dataset/images/' + im_name
with open(sys.argv[1],'r') as f_in, open(sys.argv[2],'w') as f_out:
refs = []
for line_in in f_in:
cls, xcenter, ycenter, xwidth, ywidth, confidence = line_in.rstrip().split(' ')
cls = labels_list[int(cls)]
dims = cv2.imread(im_path).shape
y_total, x_total, _ = dims
xcenter = float(xcenter) * x_total
ycenter = float(ycenter) * y_total
xwidth = float(xwidth) * x_total
ywidth = float(ywidth) * y_total
xmin = xcenter - xwidth/2
xmax = xcenter + xwidth/2
ymin = ycenter - ywidth/2
ymax = ycenter + ywidth/2
refs.append(cls + ':' + str(round(xmin)) + ',' + str(round(ymin)) + ',' + str(round(xmax)) + ',' + str(round(ymax)))
f_out.write(' '.join(refs))

View File

@ -0,0 +1 @@
labels_list = ['photograph', 'illustration', 'map', 'cartoon', 'editorial_cartoon', 'headline', 'advertisement', 'combined']

1
labels_list.py Normal file
View File

@ -0,0 +1 @@
labels_list = ['photograph', 'illustration', 'map', 'cartoon', 'editorial_cartoon', 'headline', 'advertisement', 'combined']