In [166]:
import os
from google.colab import drive
drive.mount('/content/drive')
os.chdir("/content/drive/My Drive/")
In [0]:
from importlib.machinery import SourceFileLoader
methods = SourceFileLoader('methods', '/content/drive/My Drive/methods.py').load_module()
In [0]:
%tensorflow_version 1.x
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import random
import cv2
import pickle
import h5py
from methods import AverageSmooth
from methods import RMSE
from methods import CrossCorrelation
from methods import calcFurierTransform
In [0]:
'''
Parameters:
'''
wd = 200 # deepness of moving window in time
wh = 64 # wertical and horizontal width of images at downscaling
color_channel = 1
rate_epoch_num = 1000 # Number of epochs in training of rates
In [0]:
'''
Load in signal from file
'''
def readFromFile(path,filename,signal):
print("Load to from file:"+path+filename)
print("...")
print("loaded")
with open(path+filename,"r") as ins:
lines = ins.readlines()
for line in lines:
signal.append(float(line))
return np.asarray(signal)
In [0]:
'''
save signal to file
'''
def saveToFile(path,filename,signal):
print("Save to this file:"+path+filename)
print("...")
print("saved")
with open(path+filename,"w") as o:
for i in range(len(signal)):
o.write(str(signal[i]))
o.write("\n")
In [0]:
def loadSignalSet(path,set):
ujset = []
with open(path,'r') as ins:
lines = ins.readlines()
for line in lines:
values = line.split(',')
values = values[:-1] # At the end we have an extra ','
row = []
for val in values:
row.append(float(val))
ujset.append(row)
return np.asarray(ujset)
In [173]:
'''
Load the calculated signal
'''
signal=[]
signal = loadSignalSet("output/MixedDataset2",signal)
print(signal.shape)
In [174]:
'''
Load the calculated signal
'''
reference = []
reference = readFromFile("output/","MixedDataset_ref2",reference)
print(reference.shape)
Rate Head
In [0]:
tf.reset_default_graph()
In [0]:
'''
Initialization
'''
oneD_kernels = [1, 64, 64, 32, 16, 8]
oneD_win_sizes = [32, 32, 16, 8, 4]
batch_size = 1
leaning_rate = 1e-4
THRESHOLDING = False
AUGMENTATION = False
BATCH_NORM = False
DROPOUT = True
UNET = False
L2 = False
In [0]:
'''
Define placeholders
'''
input_data = tf.placeholder(tf.float32, [None, wd])
ref_rate = tf.placeholder(tf.float32, [None,1])
In [200]:
layer_num = 0
'''
1D convolutions. Shape it to the shape of expected output.
'''
current_input = tf.reshape(input_data,[batch_size,1,wd])
for i in range(len(oneD_kernels)-1):
with tf.variable_scope("1d_conv_"+str(layer_num)):
W = tf.get_variable('weight', [oneD_win_sizes[i],oneD_kernels[i],oneD_kernels[i+1]], trainable=True)
conv_result = tf.nn.conv1d(current_input, W, stride=[1], padding='SAME', data_format='NCW')
B = tf.get_variable('bias', oneD_kernels[i+1], trainable=True)
act = tf.nn.elu(conv_result)
current_input = act
pooled = tf.nn.avg_pool1d(current_input, ksize=[1,2,1], strides=[2], padding='SAME', data_format='NCW')
print(pooled)
current_input = pooled
layer_num = layer_num + 1
In [201]:
with tf.variable_scope('fully-' + str(layer_num)):
current_shape = current_input.get_shape()
feature_length = int(current_shape[1] * current_shape[2])
fully_connected = tf.reshape(current_input, [-1, feature_length])
w = tf.get_variable('weight', [feature_length, 14])
b = tf.get_variable('bias', [1])
fully_connected = tf.matmul(fully_connected, w)
fully_connected = tf.add(fully_connected,b, name = 'output')
fully_connected = tf.nn.relu(fully_connected)
if (DROPOUT == True):
fully_connected = tf.nn.dropout(fully_connected, rate = 0.3)
print(fully_connected)
layer_num += 1
with tf.variable_scope('fully-' + str(layer_num)):
w = tf.get_variable('weight', [14,7])
b = tf.get_variable('bias', [7])
fully_connected = tf.matmul(fully_connected,w)
fully_connected = tf.add(fully_connected,b)
fully_connected = tf.nn.relu(fully_connected)
if (DROPOUT == True):
fully_connected = tf.nn.dropout(fully_connected, rate = 0.3)
print(fully_connected)
layer_num += 1
with tf.variable_scope('fully-' + str(layer_num)):
w = tf.get_variable('weight', [7,1])
b = tf.get_variable('bias', [1])
fully_connected = tf.matmul(fully_connected,w)
fully_connected = tf.add(fully_connected,b)
#fully_connected = tf.nn.relu(fully_connected)
layer_num += 1
output = fully_connected
myrate = output
In [0]:
'''
Define loss function and optimizer
'''
with tf.variable_scope('loss'):
loss = tf.reduce_mean(tf.losses.mean_squared_error(ref_rate, myrate))
with tf.name_scope('optimizer'):
optimizer = tf.train.AdamOptimizer(leaning_rate).minimize(loss)
In [0]:
def mynorm(sig):
avg = np.mean(sig)
sig = sig - np.ones_like(sig)*avg
sig = sig / np.max(sig)
return sig
In [0]:
# 1204 + 1504 + 1604
In [0]:
train_epoch_num = 1504
batch_size = 1
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
saver = tf.train.Saver()
indices = list(range(0,len(reference)))
#saver.restore(sess, "checkpoint/TrainCNN-FlowOnMix-1204")
loss_plot = np.zeros(train_epoch_num*int(len(indices)/batch_size))
In [0]:
step = 0
for epoch in range(train_epoch_num):
print("new epoch " + str(epoch))
# create list of indices
indices = list(range(0,len(reference)))
length = len(indices)
avg_loss = 0
while(batch_size<len(indices)):
# Make batch of input data
used_in_batch = random.sample(indices,batch_size)
batch_x = []
for i in used_in_batch:
batch_x.append(mynorm(signal[i]))
batch_x = np.asarray(batch_x)
batch_x = np.reshape(batch_x, [batch_size, wd])
batch_y = []
for i in used_in_batch:
batch_y.append(reference[i])
batch_y = np.asarray(batch_y)
batch_y = np.reshape(batch_y,[batch_size,1])
_, l = sess.run([optimizer, loss], feed_dict={input_data: batch_x, ref_rate: batch_y})
loss_plot[step] = l
step = step + 1
avg_loss = avg_loss + l
for index in used_in_batch:
indices.remove(index)
print(avg_loss/length)
print('Saving model...')
print(saver.save(sess, "checkpoint/TrainCNN-FlowOnMix", train_epoch_num))
plt.plot(loss_plot[loss_plot!=0])
In [190]:
train_epoch_num = 1201
batch_size = 1
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
saver = tf.train.Saver()
indices = list(range(0,len(reference)))
saver.restore(sess, "checkpoint/TrainCNN-FlowOnMix-1503")
loss_plot = np.zeros(train_epoch_num*int(len(indices)/batch_size))
In [191]:
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
#saver.restore(sess, "checkpoint/FreqNet4-1005")
saver.restore(sess, "checkpoint/TrainCNN-FlowOnMix-1503")
In [0]:
output_rates = []
#refs = []
for i in range(0,len(signal)):
sig = np.asarray(mynorm(signal[i]))
sig = np.reshape(sig, [1,wd])
r = sess.run([myrate], feed_dict={input_data: sig})
output_rates.append(r)
#refs.append(np.mean(reference_train[i:i+wd]))
In [193]:
# absolute error
errors = []
for i in range(len(output_rates)):
errors.append(abs(output_rates[i]-reference[i]))
print(np.mean(errors))
In [194]:
output_rates = np.asarray(output_rates)
print(output_rates.shape)
print(reference[:300].shape)
In [0]:
output_rates = output_rates.flatten()
In [0]:
output_rates = np.reshape(output_rates, [len(output_rates)])
In [165]:
plt.step(output_rates,'r')
plt.step(reference,'b')
Out[165]:
In [0]: