-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTesting_loading_model.py
More file actions
107 lines (94 loc) · 3.43 KB
/
Copy pathTesting_loading_model.py
File metadata and controls
107 lines (94 loc) · 3.43 KB
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
# net import
import numpy as np
import keras.backend as K
import tensorflow as tf
import keras
from keras.regularizers import *
from keras.constraints import *
from keras.models import Sequential
from keras.layers import *
from keras.utils import np_utils
from keras.datasets import mnist
from matplotlib import pyplot as plt
np.random.seed(123)
config = tf.ConfigProto(
device_count = {'GPU': 0}
)
sess = tf.Session(config=config)
K.set_session(sess)
ROWS = 64
COLS = 64
def show_images(images, cols=1, titles=None):
"""Display a list of images in a single figure with matplotlib.
Parameters
—-------
images: List of np.arrays compatible with plt.imshow.
cols (Default = 1): Number of columns in figure (number of rows is
set to np.ceil(n_images/float(cols))).
titles: List of titles corresponding to each image. Must have
the same length as titles.
"""
assert ((titles is None) or (len(images) == len(titles)))
n_images = len(images)
if titles is None: titles = ['Image (%d)' % i for i in range(1, n_images + 1)]
fig = plt.figure()
for n, (image, title) in enumerate(zip(images, titles)):
a = fig.add_subplot(cols, np.ceil(n_images / float(cols)), n + 1)
if image.ndim == 2:
plt.gray()
plt.imshow(image)
a.set_title(title)
fig.set_size_inches(np.array(fig.get_size_inches()) * n_images)
plt.show()
path = 'L:\\Documents\\PyCharmProjects\\HelloDrone\\data10'
size = 560
def generator():
i = np.random.randint(1, size)
while True:
x = np.loadtxt(path + "\\pic_from" + str(i) + ".txt")
y = np.loadtxt(path + "\\pic_to" + str(i) + ".txt")
x = np.expand_dims(np.expand_dims(np.expand_dims(x, 0),-1),0)
y = np.expand_dims(np.expand_dims(y, 0), -1)
if i == size: i = 1
i += 1
yield x,y
model = keras.models.load_model('model5.h5')
#a = generator()
#for i in range(5):
# x_data, y_data = next(a)
# res = model.predict(x_data)
# np.savetxt("L:\\Documents\\PyCharmProjects\\HelloDrone\\to_check_data\\pic" + str(i) + ".txt", res)
print(model.summary())
epochs = 1
ep = 0
a = generator()
while ep < 10000:
try:
print(ep)
history = model.fit_generator(a, epochs=epochs, steps_per_epoch=5, verbose=1, workers=1)
x_data, y_data = next(a)
res = model.predict(x_data)
# show_images([np.reshape(x_data, (ROWS, COLS)), np.reshape(y_data, (ROWS, COLS)), np.reshape(res,(ROWS, COLS)),
# ], 1, ["from", "want", "predict"])
#airsimdata.resetImageConn()
if history.history['loss'] == np.nan:
break
model.reset_states()
#if ep % 5 == 0:
#show_images([np.reshape(x_data, (ROWS, COLS)), np.reshape(y_data, (ROWS, COLS)), np.reshape(res, (ROWS, COLS)),
# ], 1, ["from", "want", "predict"])
#model.save('model5' + str(ep % 5) +'.h5')
if ep % 2000 == 0:
model.save('model5'+str(ep)+'.h5')
except airsimdata.ExeptInGenData as ex:
model.reset_states()
finally:
ep += 1
print(history.history['loss'])
for i in range(10):
for j in range(10):
x_data, y_data = next(generator())
res = model.predict(x_data)
# show_images([np.reshape(x_data, (ROWS, COLS)), np.reshape(y_data, (ROWS, COLS)), np.reshape(res,(ROWS, COLS)),
# ], 1, ["from", "want", "predict"])
print("<3")