Newer
Older
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
def autoencoder_old(path_train, path_valid, batch_size, epochs, path_save):
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import ctypes
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
os.environ['TF_ENABLE_XLA'] = '1'
os.environ['TF_ENABLE_AUTO_MIXED_PRECISION'] = '1'
os.environ['TF_ENABLE_CUDNN_RNN_TENSOR_OP_MATH_FP32'] = '1'
os.environ['TF_DISABLE_CUDNN_TENSOR_OP_MATH'] = '1'
os.environ['TF_ENABLE_CUBLAS_TENSOR_OP_MATH_FP32'] = '1'
import numpy as np
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
tf.config.experimental.set_virtual_device_configuration(gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=4096)])
except RuntimeError as e:
print(e)
from keras.models import Sequential
from keras.layers import InputLayer, Dense, Flatten, Dropout, Conv2D, MaxPooling2D, Conv2DTranspose, LeakyReLU, Reshape, Activation, BatchNormalization, UpSampling2D
from keras.activations import selu
from keras.constraints import max_norm
from keras.optimizers import Adam, SGD
from keras.utils import to_categorical, normalize
from keras.callbacks import ModelCheckpoint
from keras.datasets import mnist
from keras import losses
from keras import backend as K
import matplotlib.pyplot as plt
import numpy as np
import random
# GET DATA
# def getData():
# Datasets
# path_train = '/media/aneumann/Harddisk/Datenbanken/PythonTest/test/Preprocessing/Train'
# path_valid = '/media/aneumann/Harddisk/Datenbanken/PythonTest/test/Preprocessing/Validation'
train = os.listdir(path_train)
train = sorted(train)
valid = os.listdir(path_valid)
valid = sorted(valid)
# partition = {'train': train,
# 'validation': valid}
# labels = []
# for file in train:
# np.load(path_train + '/' + file)
# print(partition)
# train_file = np.load(path_train + '/' + train[0])
# ids_train = np.arange(np.size(train_file['x'],0))
# print(ids_train)
# batch_size = 64
def batch_generator(files, path, batch_size):
i = 0
while True:
X = list()
y = list()
# for b in range(batch_size):
if i == len(files):
i = 0
random.shuffle(files)
file = files[i]
print("loading file " + str(i) + " : " + str(file))
try:
loadfile = np.load(path + '/' + file)
except Exception as e:
print("Couldn't load file: " + str(file) + "\nError: " + str(e))
ids = np.arange(np.size(loadfile['x'],0))
batch = []
batch_counter = 0
np.random.shuffle(ids)
print(ids.shape)
print(ids)
# while batch_counter < np.floor(len(ids)/batch_size):
for id in ids:
batch.append(id)
xl = np.nan_to_num(loadfile['x'][id,:,:,:], nan=1, posinf=500, neginf=-500)
yl = np.nan_to_num(loadfile['y'][id,:,:,:], nan=1, posinf=500, neginf=-500)
xl = (xl-np.mean(xl))/np.std(xl)
yl = (yl-np.mean(xl))/np.std(xl)
X.append(xl)
y.append(yl)
if len(batch) == batch_size:
print(batch)
yield np.array(X), np.array(y)
print(np.array(X).shape, np.array(y).shape)
batch = []
X = list()
y = list()
# break
#batch = []
# batch_counter += batch_size
i += 1
# print(np.array(X).shape,np.array(y).shape)
# yield np.array(X), np.array(y)
# def load_data(loadfile,ids):
# X = []
# Y = []
#
# for i in ids:
#
# x = loadfile['x'][i,:,:,:]
# y = loadfile['y'][i,:,:,:]
#
# X.append(x)
# Y.append(y)
#
# print("X: " + str(np.array(X).shape))
# print("Y: " + str(np.array(Y).shape))
# return np.array(X), np.array(Y)
# train_generator = batch_generator(ids, batch_size = 10)
train_generator = batch_generator(train, path_train, batch_size)
# valid_generator = batch_valid_generator(ids_valid, batch_size)
valid_generator = batch_generator(valid, path_valid, batch_size)
# for X, Y in train_generator:
# print(X.shape, Y.shape)
# return train_generator, valid_generator
# BUILD MODEL
max_norm_value = 100.0
model = Sequential([
# InputLayer(input_shape=(256,128,1)),
# BatchNormalization(axis=1),
Conv2D(filters=32, kernel_size=(3,3), strides=1, padding="same", kernel_constraint=max_norm(max_norm_value), kernel_initializer="he_normal", input_shape=(256, 128, 1), name="enc_conv1"),
LeakyReLU(),
# BatchNormalization(),
Dropout(0.3, name="enc_drop1"),
MaxPooling2D(pool_size=(2,2), name="enc_pool1"),
Conv2D(filters=32, kernel_size=(3,3), strides=1, padding="same", kernel_constraint=max_norm(max_norm_value), kernel_initializer="he_normal", name="enc_conv2"),
LeakyReLU(),
MaxPooling2D(pool_size=(2,2), name="enc_pool2"),
Conv2D(filters=32, kernel_size=(3,3), strides=1, padding="same", kernel_constraint=max_norm(max_norm_value), kernel_initializer="he_normal", name="enc_conv3"),
LeakyReLU(),
MaxPooling2D(pool_size=(2,2), name="enc_pool3"),
Conv2D(filters=32, kernel_size=(3,3), strides=1, padding="same", kernel_constraint=max_norm(max_norm_value), kernel_initializer="he_normal", name="enc_conv4"),
LeakyReLU(),
MaxPooling2D(pool_size=(2,2), name="enc_pool4"),
Flatten(),
Dense(units = 8192),
Reshape((32,16,16)),
UpSampling2D(size=(2,2), name="dec_up1"),
Conv2D(filters=32, kernel_size=(3,3), strides=1, padding="same", kernel_constraint=max_norm(max_norm_value), kernel_initializer="he_normal", name="dec_conv1"),
LeakyReLU(),
UpSampling2D(size=(2,2), name="dec_up2"),
Conv2D(filters=32, kernel_size=(3,3), strides=1, padding="same", kernel_constraint=max_norm(max_norm_value), kernel_initializer="he_normal", name="dec_conv2"),
LeakyReLU(),
Dropout(0.3, name="dec_drop1"),
UpSampling2D(size=(2,2), name="dec_up3"),
Conv2D(filters=1, kernel_size=(3,3), strides=1, padding="same", activation='linear', kernel_constraint=max_norm(max_norm_value), kernel_initializer="he_normal", name="dec_conv3")
])
# opt = Adam(lr=0.000001)
model.compile(loss='mape', optimizer='adam', metrics=['mse', 'accuracy'])
# MODEL Train
if not os.path.exists("weights_cnn"):
try:
os.mkdir("weights_cnn")
except Exception as e:
print("Konnte Ordner für Gewichte nicht erstellen" + str(e))
filepath = "weights_cnn/weights-{epoch:02d}-{loss:.4f}.hdf5"
checkpoint = ModelCheckpoint(
filepath,
monitor='loss',
verbose=0,
save_best_only=True,
mode='min'
)
model.summary()
history = model.fit(train_generator, steps_per_epoch = len(train), epochs = epochs, validation_data=valid_generator, validation_steps = len(valid), use_multiprocessing=False)
model.summary()
model.save(path_save + '/cnn_autoencoder_model.h5')
plt.style.use("ggplot")
plt.figure()
plt.plot(history.history["loss"], label="train_loss")
plt.plot(history.history["val_loss"], label="val_loss")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epochs")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")
plt.savefig(path_save + '/testrun.png', dpi=400)
plt.show()