-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFCN_setup.py
296 lines (237 loc) · 12.1 KB
/
FCN_setup.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from __future__ import print_function
import sys
import os
import site
from shutil import copyfile
if len(sys.argv)<2:
print("Usage:")
print(" 'python", sys.argv[0], "install' to install FCN extensions to Keras")
print(" or ")
print(" 'python", sys.argv[0], "uninstall' to remove FCN extensions from Keras")
sys.exit()
if sys.argv[1]=='install':
mode = 1
elif sys.argv[1]=='uninstall':
mode = 2
else:
print("Usage:")
print(" 'python", sys.argv[0], "install' to install FCN extensions to Keras")
print(" or ")
print(" 'python", sys.argv[0], "uninstall' to remove FCN extensions from Keras")
sys.exit()
# site-packages path
site_path = ''
for p in site.getsitepackages():
if "packages" in p:
site_path = p
break
# Keras paths
keras_path = os.path.join(site_path, 'keras')
keras_pre_path = os.path.join(site_path, 'keras', 'preprocessing')
keras_app_path = os.path.join(site_path, 'keras', 'applications')
keras_pre_pkg_path = os.path.join(site_path, 'keras_preprocessing')
keras_app_pkg_path = os.path.join(site_path, 'keras_applications')
# Check if keras' paths exist
if not os.path.isdir(keras_path):
print('path', keras_path, 'not found.')
print('Cannot continue...')
sys.exit()
if not os.path.isdir(keras_pre_path):
print('path', keras_pre_path, 'not found.')
print('Cannot continue...')
sys.exit()
if not os.path.isdir(keras_app_path):
print('path', keras_app_path, 'not found.')
print('Cannot continue...')
sys.exit()
if not os.path.isdir(keras_pre_pkg_path):
print('path', keras_pre_pkg_path, 'not found.')
print('Cannot continue...')
sys.exit()
if not os.path.isdir(keras_app_pkg_path):
print('path', keras_app_pkg_path, 'not found.')
print('Cannot continue...')
sys.exit()
print('~~~ Found Keras in:')
print(' ', keras_path)
print(' ', keras_pre_path)
print(' ', keras_app_path)
print(' ', keras_pre_pkg_path)
print(' ', keras_app_pkg_path)
print(' ')
if mode==1:
# check if Keras is installed
print('~~~ Starting Keras')
import keras
print('Keras version:', keras.__version__)
print(' ')
print('Installing:')
# write new content in keras/applications/__init__.py
print('~~~ Checking keras/applications/__init__.py')
if not os.path.isfile(os.path.join(keras_app_path, '__init__-backup')):
print(' Creating backup of:', os.path.join(keras_app_path, '__init__.py'))
copyfile(os.path.join(keras_app_path, '__init__.py'), os.path.join(keras_app_path, '__init__-backup'))
print(' Writing new lines in', os.path.join(keras_app_path, '__init__.py'))
with open(os.path.join(keras_app_path, '__init__.py'), 'a') as f:
f.write('\n')
f.write('from .vgg16_fcn import VGG16_fcn\n')
f.write('from .vgg19_fcn import VGG19_fcn\n')
f.write('from .inception_v3_fcn import InceptionV3_fcn\n')
f.write('from .xception_fcn import Xception_fcn\n')
else:
print(' Skipped,', os.path.join(keras_app_path, '__init__.py'), 'is updated.)')
print(' ')
# write new content in keras_preprocessing/image.py
print('~~~ Checking keras_preprocessing/image.py')
if not os.path.isfile(os.path.join(keras_pre_pkg_path, 'image-backup')):
print(' Creating backup of:', os.path.join(keras_pre_pkg_path, 'image.py'))
copyfile(os.path.join(keras_pre_pkg_path, 'image.py'), os.path.join(keras_pre_pkg_path, 'image-backup'))
print(' Writing new lines in', os.path.join(keras_pre_pkg_path, 'image.py'))
with open(os.path.join(keras_pre_pkg_path, 'image.py'), 'r') as f:
data = f.readlines()
for i,line in enumerate(data):
if 'load_img' in line:
adjusted_line = line.replace('load_img', 'load_img_old')
print(' (original line: "', line.strip(), '")')
print(' (adjusted line: "', adjusted_line.strip(), '")')
data[i] = adjusted_line
break
with open(os.path.join(keras_pre_pkg_path, 'image.py'), 'w') as f:
for item in data:
f.write("%s" % item)
print(' ')
print("~~~ Adding new content:")
with open(os.path.join(keras_pre_pkg_path, 'image.py'), 'a') as f_dst:
print(" adding method for 'load_img_keras'")
with open(os.path.join('extensions', 'load_img', 'load_img_keras.py'), 'r') as f_src:
lines = f_src.readlines()
for line in lines:
f_dst.write(line)
print(" adding method for 'load_img_pad'")
with open(os.path.join('extensions', 'load_img', 'load_img_pad.py'), 'r') as f_src:
lines = f_src.readlines()
for line in lines:
f_dst.write(line)
print(" adding method for 'load_img_crop'")
with open(os.path.join('extensions', 'load_img', 'load_img_crop.py'), 'r') as f_src:
lines = f_src.readlines()
for line in lines:
f_dst.write(line)
print(" adding method for 'load_img_multicrop'")
with open(os.path.join('extensions', 'load_img', 'load_img_multicrop.py'), 'r') as f_src:
lines = f_src.readlines()
for line in lines:
f_dst.write(line)
print(" adding method for top-level 'load_img'")
with open(os.path.join('extensions', 'load_img', 'load_img.py'), 'r') as f_src:
lines = f_src.readlines()
for line in lines:
f_dst.write(line)
else:
print(' Skipped,', os.path.join(keras_pre_path, 'image.py'), 'is updated.)')
print(' ')
# write new content in keras/preprocessing/image.py
print('~~~ Checking keras/preprocessing/image.py')
if not os.path.isfile(os.path.join(keras_pre_path, 'image-backup')):
print(' Creating backup of:', os.path.join(keras_pre_path, 'image.py'))
copyfile(os.path.join(keras_pre_path, 'image.py'), os.path.join(keras_pre_path, 'image-backup'))
print(' Writing new lines in', os.path.join(keras_pre_path, 'image.py'))
with open(os.path.join(keras_pre_path, 'image.py'), 'a') as f:
f.write('set_load_img_type = image.set_load_img_type\n')
else:
print(' Skipped,', os.path.join(keras_pre_path, 'image.py'), 'is updated.)')
print(' ')
# copy FCNs
print('~~~ Copying FCNs to keras_applications:')
if not os.path.isfile(os.path.join(keras_app_pkg_path, 'vgg16_fcn.py')):
print(' copying', os.path.join(keras_app_pkg_path, 'vgg16_fcn.py'))
copyfile(os.path.join('extensions', 'vgg16_fcn.py'), os.path.join(keras_app_pkg_path, 'vgg16_fcn.py'))
else:
print(' Skipped,', os.path.join(keras_app_pkg_path, 'vgg16_fcn.py') , 'exists')
if not os.path.isfile(os.path.join(keras_app_pkg_path, 'vgg19_fcn.py')):
print(' copying', os.path.join(keras_app_pkg_path, 'vgg19_fcn.py'))
copyfile(os.path.join('extensions', 'vgg19_fcn.py'), os.path.join(keras_app_pkg_path, 'vgg19_fcn.py'))
else:
print(' Skipped,', os.path.join(keras_app_pkg_path, 'vgg19_fcn.py') , 'exists')
if not os.path.isfile(os.path.join(keras_app_pkg_path, 'inception_v3_fcn.py')):
print(' copying', os.path.join(keras_app_pkg_path, 'inception_v3_fcn.py'))
copyfile(os.path.join('extensions', 'inception_v3_fcn.py'), os.path.join(keras_app_pkg_path, 'inception_v3_fcn.py'))
else:
print(' Skipped,', os.path.join(keras_app_pkg_path, 'inception_v3_fcn.py') , 'exists')
if not os.path.isfile(os.path.join(keras_app_pkg_path, 'xception_fcn.py')):
print(' copying', os.path.join(keras_app_pkg_path, 'xception_fcn.py'))
copyfile(os.path.join('extensions', 'xception_fcn.py'), os.path.join(keras_app_pkg_path, 'xception_fcn.py'))
else:
print(' Skipped,', os.path.join(keras_app_pkg_path, 'xception_fcn.py') , 'exists')
print(' ')
print('~~~ Copying FCNs to keras.applications:')
if not os.path.isfile(os.path.join(keras_app_path, 'vgg16_fcn.py')):
print(' copying', os.path.join(keras_app_path, 'vgg16_fcn.py'))
copyfile(os.path.join('extensions', 'keras.applications', 'vgg16_fcn.py'), os.path.join(keras_app_path, 'vgg16_fcn.py'))
else:
print(' Skipped,', os.path.join(keras_app_pkg_path, 'vgg16_fcn.py') , 'exists')
if not os.path.isfile(os.path.join(keras_app_path, 'vgg19_fcn.py')):
print(' copying', os.path.join(keras_app_path, 'vgg19_fcn.py'))
copyfile(os.path.join('extensions', 'keras.applications', 'vgg19_fcn.py'), os.path.join(keras_app_path, 'vgg19_fcn.py'))
else:
print(' Skipped,', os.path.join(keras_app_pkg_path, 'vgg19_fcn.py') , 'exists')
if not os.path.isfile(os.path.join(keras_app_path, 'inception_v3_fcn.py')):
print(' copying', os.path.join(keras_app_path, 'inception_v3_fcn.py'))
copyfile(os.path.join('extensions', 'keras.applications', 'inception_v3_fcn.py'), os.path.join(keras_app_path, 'inception_v3_fcn.py'))
else:
print(' Skipped,', os.path.join(keras_app_pkg_path, 'inception_v3_fcn.py') , 'exists')
if not os.path.isfile(os.path.join(keras_app_path, 'xception_fcn.py')):
print(' copying', os.path.join(keras_app_path, 'xception_fcn.py'))
copyfile(os.path.join('extensions', 'keras.applications', 'xception_fcn.py'), os.path.join(keras_app_path, 'xception_fcn.py'))
else:
print(' Skipped,', os.path.join(keras_app_pkg_path, 'xception_fcn.py') , 'exists')
print('\nSuccessfully installed FCN extensions to Keras.')
if mode==2:
print('Uninstalling:')
# Restoring keras/applications/__init__.py
print('~~~ Restoring keras/applications/__init__.py...')
if os.path.isfile(os.path.join(keras_app_path, '__init__-backup')):
os.remove(os.path.join(keras_app_path, '__init__.py'))
copyfile(os.path.join(keras_app_path, '__init__-backup'), os.path.join(keras_app_path, '__init__.py'))
os.remove(os.path.join(keras_app_path, '__init__-backup'))
# Restoring keras_preprocessing/image.py
print('~~~ Restoring keras_preprocessing/image.py...')
if os.path.isfile(os.path.join(keras_pre_pkg_path, 'image-backup')):
os.remove(os.path.join(keras_pre_pkg_path, 'image.py'))
copyfile(os.path.join(keras_pre_pkg_path, 'image-backup'), os.path.join(keras_pre_pkg_path, 'image.py'))
os.remove(os.path.join(keras_pre_pkg_path, 'image-backup'))
# write new content in keras/preprocessing/image.py
print('~~~ Restoring keras/preprocessing/image.py...')
if os.path.isfile(os.path.join(keras_pre_path, 'image-backup')):
os.remove(os.path.join(keras_pre_path, 'image.py'))
copyfile(os.path.join(keras_pre_path, 'image-backup'), os.path.join(keras_pre_path, 'image.py'))
os.remove(os.path.join(keras_pre_path, 'image-backup'))
# Delete FCNs from keras_applications
print('~~~ Removing FCNs from keras_applications...')
if os.path.isfile(os.path.join(keras_app_pkg_path, 'vgg16_fcn.py')):
print(' deleting', os.path.join(keras_app_pkg_path, 'vgg16_fcn.py'))
os.remove(os.path.join(keras_app_pkg_path, 'vgg16_fcn.py'))
if os.path.isfile(os.path.join(keras_app_pkg_path, 'vgg19_fcn.py')):
print(' deleting', os.path.join(keras_app_pkg_path, 'vgg19_fcn.py'))
os.remove(os.path.join(keras_app_pkg_path, 'vgg19_fcn.py'))
if os.path.isfile(os.path.join(keras_app_pkg_path, 'inception_v3_fcn.py')):
print(' deleting', os.path.join(keras_app_pkg_path, 'inception_v3_fcn.py'))
os.remove(os.path.join(keras_app_pkg_path, 'inception_v3_fcn.py'))
if os.path.isfile(os.path.join(keras_app_pkg_path, 'xception_fcn.py')):
print(' deleting', os.path.join(keras_app_pkg_path, 'xception_fcn.py'))
os.remove(os.path.join(keras_app_pkg_path, 'xception_fcn.py'))
# Delete FCNs from keras.applications
print('~~~ Removing FCNs from keras.applications...')
if os.path.isfile(os.path.join(keras_app_path, 'vgg16_fcn.py')):
print(' deleting', os.path.join(keras_app_path, 'vgg16_fcn.py'))
os.remove(os.path.join(keras_app_path, 'vgg16_fcn.py'))
if os.path.isfile(os.path.join(keras_app_path, 'vgg19_fcn.py')):
print(' deleting', os.path.join(keras_app_path, 'vgg19_fcn.py'))
os.remove(os.path.join(keras_app_path, 'vgg19_fcn.py'))
if os.path.isfile(os.path.join(keras_app_path, 'inception_v3_fcn.py')):
print(' deleting', os.path.join(keras_app_path, 'inception_v3_fcn.py'))
os.remove(os.path.join(keras_app_path, 'inception_v3_fcn.py'))
if os.path.isfile(os.path.join(keras_app_path, 'xception_fcn.py')):
print(' deleting', os.path.join(keras_app_path, 'xception_fcn.py'))
os.remove(os.path.join(keras_app_path, 'xception_fcn.py'))
print('\nSuccessfully uninstall FCN extensions from Keras.')