-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit_results.py
86 lines (70 loc) · 3.19 KB
/
submit_results.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import progressbar
import keras
from keras_retinanet import models
from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image
from keras_retinanet.utils.colors import label_color
import pandas as pd
import datetime
import numpy as np
# set tf backend to allow memory to grow, instead of claiming everything
import tensorflow as tf
def get_session():
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
return tf.Session(config=config)
keras.backend.tensorflow_backend.set_session(get_session())
base_path = "/work/BigDataDecomp/lale/road_damage_dataset"
test_csv_path = base_path + "/testset.csv"
# model_path = "trained_models/resnet101_rdd_20_best8103_infer.h5"
# model_path = "trained_models/resnet50_rdd_82_infer.h5"
# model_path = "trained_models/vgg19_rdd_10_best8279_infer.h5"
model_path = "trained_models/resnet152_rdd_19_best8140_infer.h5"
model = models.load_model(model_path, backbone_name='resnet152')
labels_to_names = {0: 'D00',
1: 'D01',
2: 'D10',
3: 'D11',
4: 'D20',
5: 'D40',
6: 'D43',
7: 'D44',
8: 'D30'}
test_df = pd.read_csv(test_csv_path)
start = datetime.datetime.now()
predict_df = pd.DataFrame(columns=["ImageId", "PredictionString"])
for img in progressbar.progressbar(list(test_df.file), prefix='Parsing annotations: '):
img_path = base_path + '/ImageSets/{}.jpg'.format(img)
image = read_image_bgr(img_path)
image = preprocess_image(image)
image, scale = resize_image(image)
boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
boxes /= scale
PredictionString = ''
i = 0
for box, score, label in zip(boxes[0], scores[0], labels[0]):
# scores are sorted so we can break
if i < 6 and score > 0.55 and label < 8:
i = i + 1
color = label_color(label)
b = box.astype(int)
PredictionString += (str(label + 1) + ' ' + str(b).replace("]", "").replace("[", "").strip() + " ")
# get the top one if there is not boxes confident over 0.5
if PredictionString == '':
for box, score, label in zip(boxes[0], scores[0], labels[0]):
# scores are sorted so we can break
if (label < 8) and (label >= 0):
b = box.astype(int)
PredictionString += (str(label + 1) + ' ' + str(b).replace("]", "").replace("[", "").strip() + " ")
break
if PredictionString == '':
PredictionString += '0 -1 -1 -1 -1 '
end = datetime.datetime.now()
elapsed = end - start
print(elapsed.seconds)
predict_df = predict_df.append({'ImageId': img + ".jpg",
'PredictionString': PredictionString.strip().replace(" ", " ").replace(" ",
" ").replace(
" ", " ")},
ignore_index=True)
predict_df = predict_df.sort_values("ImageId")
predict_df.to_csv("submit_res152_55.csv", header=False, index=False)