-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFeaturizeImages.py
63 lines (55 loc) · 2.22 KB
/
FeaturizeImages.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
import os
import numpy as np
#import imageio
import pandas as pd
from pathlib import Path
import cv2
###
from keras.applications.densenet import DenseNet121
from keras.preprocessing import image
from keras.applications.densenet import preprocess_input
from keras.models import Model
def feat_data():
data_dir = Path('./www')
labeled_dir = data_dir / 'LABELED'
unlabeled_dir = data_dir / 'UNLABELED'
normal_cases_dir = labeled_dir / 'NORMAL'
abnormal_cases_dir = labeled_dir / 'ABNORMAL'
normal_cases = normal_cases_dir.glob('*.jpeg')
abnormal_cases = abnormal_cases_dir.glob('*.jpeg')
unlabeled_cases = unlabeled_dir.glob('*.jpeg')
data = []
# Go through all the normal cases. The label for these cases will be 0
for img in normal_cases:
imgx = cv2.imread(str(img))
if imgx.shape[2] ==3:
data.append((img,0))
# Go through all the abnormal cases. The label for these cases will be 1
for img in abnormal_cases:
imgx = cv2.imread(str(img))
if imgx.shape[2] ==3:
data.append((img, 1))
# Go through all the abnormal cases. The label for these cases will be 'NA'
for img in unlabeled_cases:
imgx = cv2.imread(str(img))
if imgx.shape[2] ==3:
data.append((img, np.nan))
# Get a pandas dataframe from the data we have in our list
data = pd.DataFrame(data, columns=['image', 'label'],index=None)
# Shuffle the data
data = data.sample(frac=1).reset_index(drop=True)
base_model = DenseNet121(weights='imagenet')
model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1000').output)
fc_features =np.zeros((data.shape[0],1000))
for i in range(data.shape[0]):
img_path = str(data['image'][i])
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
fc_features[i]=model.predict(x)
df = pd.DataFrame(fc_features, columns=['X'+str(i) for i in range(fc_features.shape[1])],index=None)
final_data = pd.concat([df, data], axis= 1)
final_data['image'] = final_data['image'].astype(str)
final_data.to_csv('final_data_test.csv')
return final_data