This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathUtils.py
75 lines (58 loc) · 2.5 KB
/
Utils.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
## Utils.py -- Some utility functions
##
## Copyright (C) 2018, IBM Corp
## Chun-Chen Tu <[email protected]>
## PaiShun Ting <[email protected]>
## Pin-Yu Chen <[email protected]>
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
from keras.models import Model, model_from_json, Sequential
from PIL import Image
import tensorflow as tf
import os
import numpy as np
def load_AE(codec_prefix, print_summary=False):
saveFilePrefix = "models/AE_codec/" + codec_prefix + "_"
decoder_model_filename = saveFilePrefix + "decoder.json"
decoder_weight_filename = saveFilePrefix + "decoder.h5"
if not os.path.isfile(decoder_model_filename):
raise Exception("The file for decoder model does not exist:{}".format(decoder_model_filename))
json_file = open(decoder_model_filename, 'r')
decoder = model_from_json(json_file.read(), custom_objects={"tf": tf})
json_file.close()
if not os.path.isfile(decoder_weight_filename):
raise Exception("The file for decoder weights does not exist:{}".format(decoder_weight_filename))
decoder.load_weights(decoder_weight_filename)
if print_summary:
print("Decoder summaries")
decoder.summary()
return decoder
def save_img(img, name = "output.png"):
np.save(name, img)
fig = np.around((img + 0.5)*255)
fig = fig.astype(np.uint8).squeeze()
pic = Image.fromarray(fig)
pic.save(name)
def generate_data(data, id, target_label):
inputs = []
target_vec = []
inputs.append(data.test_data[id])
target_vec.append(np.eye(data.test_labels.shape[1])[target_label])
inputs = np.array(inputs)
target_vec = np.array(target_vec)
return inputs, target_vec
def model_prediction(model, inputs):
prob = model.model.predict(inputs)
predicted_class = np.argmax(prob)
prob_str = np.array2string(prob).replace('\n','')
return prob, predicted_class, prob_str