/
Apollo.py
132 lines (94 loc) · 4.3 KB
/
Apollo.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import keras_segmentation
import numpy as np
import cv2
import os
import glob
from tqdm import tqdm
from keras.preprocessing.image import img_to_array, load_img
from keras_segmentation import metrics
# from keras_segmentation.data_utils.data_loader import get_segmentation_arr
model = keras_segmentation.models.unet.vgg_unet(n_classes=12, input_height=800, input_width=2400)
#Show the detail of model, and total parameters
model.summary()
model.train(
train_images = "./dataset3/image/train",
train_annotations = "./dataset3/label/train",
val_images=None,
val_annotations=None,
verify_dataset=False,
checkpoints_path = "./path_to_checkpoints/" ,
epochs = 1,
batch_size = 1,
validate=False ,
val_batch_size=None ,
auto_resume_checkpoint=False ,
load_weights=None ,
steps_per_epoch=2,
optimizer_name='adadelta'
)
all_prs = model.predict_multiple(
inp_dir = "./dataset3/image/test",
out_dir = "path_to_predictions/",
checkpoints_path = "path_to_checkpoints/"
)
def evaluate_camvid():
# get seg (ground truth) and compare with pr (prediction)
# Get seg
segs_path = "./dataset3/label/test/"
segs = glob.glob( os.path.join(segs_path,"*.png") )
#print(len(segs))
imglabels = np.ndarray((len(segs),2710, 3384), dtype=np.uint8)
i=0
for x in range(len(segs)):
imgpath = segs[x]
pic_name = imgpath.split('/')[-1]
labelpath = "dataset3/label/" + pic_name.split('.')[0] + '.png'
label = load_img(labelpath, grayscale=True, target_size=[2710, 3384]) # grayscale = False
label = img_to_array(label)
imglabels[i] = label[:,:,0]
if i % 100 == 0:
print('Creating testing images: {0}/{1} images'.format(i, len(segs)))
i += 1
#np.save('./segs_test.npy', imglabels)
i=0
orininal_w = 3384
orininal_h = 2710
n_classes = 12
ious=[]
precisions=[]
recalls=[]
for pr in tqdm(all_prs):
gt = imglabels[i]
pr = cv2.resize(pr, (orininal_w , orininal_h), interpolation=cv2.INTER_NEAREST)
#IoU
iou = metrics.get_iou( gt , pr , n_classes )
ious.append( iou )
# precision
precision = metrics.get_precision( gt , pr , n_classes )
precisions.append( precision )
# recall
recall = metrics.get_recall( gt , pr , n_classes )
recalls.append( recall )
i+=1
ious = np.array(ious)
precisions = np.array( precisions )
recalls = np.array(recalls)
#print("Class wise IoU Class:{:d} / IoU:{:.2f}\n".format(11, np.mean(ious , axis=0 )[11]))
#print("Class wise Precision Class:{:d} / Precision:{:.2f}\n".format(11, np.mean(precisions , axis=0 )[11]))
#print("Class wise Recall Class:{:d} / Recall:{:.2f}\n".format(11, np.mean(recalls , axis=0 )[11]))
print("Class wise IoU " , np.mean(ious , axis=0 ))
print("Total IoU " , np.mean(ious ))
# print("Class wise IoU:{:.2f}\n".format(np.mean(ious , axis=0 )))
# print("Class wise Precision:{:.2f}\n".format(np.mean(precisions , axis=0 )))
# print("Class wise Recall:{:.2f}\n".format(np.mean(recalls , axis=0 )))
#
'''
# dataset1 (prepared dataset, 12 calsses)
python -m keras_segmentation train --checkpoints_path="path_to_checkpoints" --train_images="dataset1/images_prepped_train/" --train_annotations="dataset1/annotations_prepped_train/" --val_images="dataset1/images_prepped_test/" --val_annotations="dataset1/annotations_prepped_test/" --n_classes=12 --input_height=320 --input_width=640 --model_name="vgg_unet"
python -m keras_segmentation predict --checkpoints_path="./path_to_checkpoints/" --input_path="dataset2/image/test/" --output_path="path_to_predictions"
'''
'''
# dataset2 (CamVid, 32 classes)
python -m keras_segmentation train --checkpoints_path="path_to_checkpoints/" --train_images="dataset2/image/train/" --train_annotations="dataset2/trainId_label/train/" --val_images="dataset2/image/test/" --val_annotations="dataset2/trainId_label/test/" --n_classes=32 --input_height=736 --input_width=960 --model_name="vgg_unet" --epochs=20
python -m keras_segmentation predict --checkpoints_path="./path_to_checkpoints/" --input_path="dataset2/image/test/" --output_path="path_to_predictions"
'''