-
Notifications
You must be signed in to change notification settings - Fork 1
/
categorize_eye.py
67 lines (56 loc) · 1.92 KB
/
categorize_eye.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
import tensorflow as tf
from glob import glob
import numpy as np
import os
import shutil
from patch_generator import square_frame
from tqdm import tqdm
cur_dir = os.path.dirname(os.path.realpath(__file__))
os.sys.path.insert(-1, cur_dir)
MODEL_WEIGHT_FILE = os.path.join(cur_dir, 'EyeClassification/new_model_2/weights-0644-0.0243-0.9909-0.3269-0.9604.hdf5')
model = tf.keras.models.load_model(MODEL_WEIGHT_FILE)
def classify(images):
resized_images = tf.image.resize(images, size=(256, 256)).numpy()
if np.max(resized_images) > 1:
resized_images = np.array(resized_images/255., dtype=np.float32)
# valid_index = []
# invalid_index = []
# for i, image in enumerate(resized_images):
# if is_fundus(image):
# valid_index.append(i)
# else:
# invalid_index.append(i)
# resized_images = resized_images[valid_index]
prediction = model.predict(resized_images, batch_size=16)
classes =[]
for p in prediction:
if p[0] < 0.5:
label = 'OD'
else:
label = 'OS'
classes.append(label)
return classes
def main():
files = glob('../neo/*.png')
res_dir = '../eye_classification_new_neo'
od_dir = os.path.join(res_dir, 'OD')
os_dir = os.path.join(res_dir, 'OS')
if not os.path.isdir(res_dir):
os.mkdir(res_dir)
if not os.path.isdir(os_dir):
os.mkdir(os_dir)
if not os.path.isdir(od_dir):
os.mkdir(od_dir)
images = []
for file in tqdm(files, desc='Reading Images'):
image = np.array(tf.keras.preprocessing.image.load_img(file))
image = square_frame(image)
images.append(image)
for file, image in tqdm(zip(files, images), total=len(files)):
label = classify(np.expand_dims(image, axis=0))
if label[0] == 'OD':
shutil.copy(file, od_dir)
else:
shutil.copy(file, os_dir)
if __name__ == '__main__':
main()