-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
231 lines (192 loc) · 7.58 KB
/
train.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import cv2 as cv
import tensorflow as tf
import numpy as np
import os
import Inception as inc
import Mobilenet as mn
import Densenet as dn
import pickle
import sys
# load the pretrained models.
mobilenetModel = mn.Mobilenet()
inceptionModel = inc.Inception()
densenetModel = dn.Densenet()
def calculateTransferValues(listsDirectory):
"""
Calculate the transfer values of each image in the MURA dataset using
all three pretrained models and then concatenate these values together
and finally writes them to cache files.
:param listsDirectory:
Directory for the lists that contain the paths of image files.
:return:
Nothing.
"""
imagePaths = []
# Get files that hold the image paths (ex. elbowList.txt).
listNames = os.listdir(listsDirectory)
for listName in listNames:
# Get the path of the file and set the cache path with the same name.
listPath = listsDirectory + "/" + listName
listName = listName[:-4]
cachePath = "cache/" + listName + ".pkl"
print(listPath)
# Read the paths in the file and remove the \n character.
with open(listPath) as file:
imagePaths = file.readlines()
imagePaths = [x.strip() for x in imagePaths]
# Create an empty list to hold the transfer values.
imageCount = len(imagePaths)
transferValues = [None] * imageCount
# Loop over all images.
for i in range(imageCount):
msg = "\r- Processing image: {0:>6} / {1}".format(i+1, imageCount)
sys.stdout.write(msg)
sys.stdout.flush()
imagePath = imagePaths[i]
# Get the transfer values of all three models.
mobilenetTransferValue = mobilenetModel.getTransferValue(imagePath)
inceptionTransferValue = inceptionModel.getTransferValue(imagePath)
densenetTransferValue = densenetModel.getTransferValue(imagePath)
# Concatenate the transfer values.
result = np.concatenate((inceptionTransferValue, mobilenetTransferValue))
transferValues[i] = np.concatenate((result, densenetTransferValue))
print()
# Clear the list after each group of images and cache the transfer values.
imagePaths.clear()
cacheTransferValues(transferValues, cachePath)
# Close the models to clear any resorces they hold.
mobilenetModel.close()
inceptionModel.close()
densenetModel.close()
def calculateInceptionTransferValues(listsDirectory):
"""
Calculate the transfer values of each image in the MURA dataset using
the inception model only and then writes them to cache files.
:param listsDirectory:
Directory for the lists that contain the paths of image files.
:return:
Nothing.
"""
imagePaths = []
# Get files that hold the image paths (ex. elbowList.txt).
listNames = os.listdir(listsDirectory)
for listName in listNames:
# Get the path of the file and set the cache path with the same name.
listPath = listsDirectory + "/" + listName
listName = listName[:-4]
cachePath = "inceptionCache/" + listName + ".pkl"
print(listPath)
# Read the paths in the file and remove the \n character.
with open(listPath) as file:
imagePaths = file.readlines()
imagePaths = [x.strip() for x in imagePaths]
# Get the transfer values and cache them.
getInceptionTransferValues(imagePaths, cachePath)
imagePaths.clear()
inceptionModel.close()
def calculateMobilenetTransferValues(listsDirectory):
"""
Calculate the transfer values of each image in the MURA dataset using
the mobilenet model only and then writes them to cache files.
:param listsDirectory:
Directory for the lists that contain the paths of image files.
:return:
Nothing.
"""
imagePaths = []
# Get files that hold the image paths (ex. elbowList.txt).
listNames = os.listdir(listsDirectory)
for listName in listNames:
# Get the path of the file and set the cache path with the same name.
listPath = listsDirectory + "/" + listName
listName = listName[:-4]
cachePath = "mobilenetCache/" + listName + ".pkl"
print(listPath)
# Read the paths in the file and remove the \n character.
with open(listPath) as file:
imagePaths = file.readlines()
imagePaths = [x.strip() for x in imagePaths]
# Get the transfer values and cache them.
getMobileNetTransferValues(imagePaths, cachePath)
imagePaths.clear()
mobilenetModel.close()
def calculateDensenetTransferValues(listsDirectory):
"""
Calculate the transfer values of each image in the MURA dataset using
the densenet model only and then writes them to cache files.
:param listsDirectory:
Directory for the lists that contain the paths of image files.
:return:
Nothing.
"""
imagePaths = []
# Get files that hold the image paths (ex. elbowList.txt).
listNames = os.listdir(listsDirectory)
for listName in listNames:
# Get the path of the file and set the cache path with the same name.
listPath = listsDirectory + "/" + listName
listName = listName[:-4]
cachePath = "densenetCache/" + listName + ".pkl"
print(listPath)
# Read the paths in the file and remove the \n character.
with open(listPath) as file:
imagePaths = file.readlines()
imagePaths = [x.strip() for x in imagePaths]
# Get the transfer values and cache them.
getDensenetTransferValues(imagePaths, cachePath)
imagePaths.clear()
densenetModel.close()
def getInceptionTransferValues(imagePaths, cachePath):
"""
Calculate the transfer values of a group of images using
the inception model only and then writes them to cache files.
:param imagePaths:
List of image paths.
:param cachePath:
The file to cache the transfer values in.
:return:
Nothing.
"""
values = inceptionModel.getTransferValues(imagePaths)
cacheTransferValues(values, cachePath)
def getMobileNetTransferValues(imagePaths, cachePath):
"""
Calculate the transfer values of a group of images using
the mobilenet model only and then writes them to cache files.
:param imagePaths:
List of image paths.
:param cachePath:
The file to cache the transfer values in.
:return:
Nothing.
"""
values = mobilenetModel.getTransferValues(imagePaths)
cacheTransferValues(values, cachePath)
def getDensenetTransferValues(imagePaths, cachePath):
"""
Calculate the transfer values of a group of images using
the densenet model only and then writes them to cache files.
:param imagePaths:
List of image paths.
:param cachePath:
The file to cache the transfer values in.
:return:
Nothing.
"""
values = densenetModel.getTransferValues(imagePaths)
cacheTransferValues(values, cachePath)
def cacheTransferValues(transferValues, cachePath):
"""
Caches the transfer values in the given file using pickle.
:param transferValues:
The transfer values to be cached
:param cachePath:
The file to cache the transfer values in.
:return:
Nothing.
"""
print("caching transfer values in: " + cachePath)
if not os.path.exists(cachePath):
with open(cachePath, mode='wb') as file:
pickle.dump(transferValues, file)
calculateTransferValues("E:/MURA-v1.1/lists")