aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNiklas Halle <niklas@niklashalle.net>2021-06-11 13:52:56 +0200
committerNiklas Halle <niklas@niklashalle.net>2021-06-11 13:52:56 +0200
commit1d096999007e300fd0e926abd049efbba54ea9fe (patch)
tree398348280b326314b1461a87333b19b067c9e510
parent4f48784d6deec36f8e180d2d7e35799891f4d2db (diff)
downloadbachelor_thesis-1d096999007e300fd0e926abd049efbba54ea9fe.tar.gz
bachelor_thesis-1d096999007e300fd0e926abd049efbba54ea9fe.zip
start refactir
-rw-r--r--code/python/calculate_exact_solutions.py0
-rwxr-xr-xcode/python/convolution.py157
-rw-r--r--code/python/convolution_example.py57
-rwxr-xr-x[-rw-r--r--]code/python/create_target_data.py0
-rw-r--r--code/python/generate_graphs.py52
l---------code/python/input/nk1
-rw-r--r--code/python/input/test0.graph13
-rwxr-xr-xcode/python/lfr_generate_graphs.py64
-rw-r--r--code/python/model.py7
-rw-r--r--code/python/pred.py12
-rw-r--r--code/python/rumprobieren.py135
-rw-r--r--code/python/terraform_example_code.py98
-rw-r--r--code/python/test.pngbin2396 -> 0 bytes
-rw-r--r--code/python/train.py100
-rw-r--r--code/python/train_network.py45
15 files changed, 112 insertions, 629 deletions
diff --git a/code/python/calculate_exact_solutions.py b/code/python/calculate_exact_solutions.py
deleted file mode 100644
index e69de29..0000000
--- a/code/python/calculate_exact_solutions.py
+++ /dev/null
diff --git a/code/python/convolution.py b/code/python/convolution.py
index 9a36f46..a459c4e 100755
--- a/code/python/convolution.py
+++ b/code/python/convolution.py
@@ -18,85 +18,92 @@ tf.debugging.set_log_device_placement(False)
model_type = "betweenness"
max_number_of_nodes = 500
-
-embeddings = utils.load_embeddings()
-
-# betweenness = load_betweenness()
-# closeness = load_closeness()
-# electrical_closeness = load_electrical_closeness()
-# communities = load_communities()
-
-# print(embeddings.shape)
-# print(betweenness.shape)
-# print(closeness.shape)
-# print(electrical_closeness)
-# print(electrical_closeness.shape)
-# print(communities.shape)
-# print(communities)
-
-if model_type == "betweenness":
- target_data = utils.load_betweenness(max_number_of_nodes)
-else:
- print(f"unknown type: {model_type}")
- exit(1)
-
-X_train, X_test, y_train, y_test = \
- train_test_split(embeddings, target_data, test_size=0.33, random_state=42)
-
-mirrored_strategy = tf.distribute.MirroredStrategy()
-
-with mirrored_strategy.scope():
- model = models.Sequential()
- model.add(layers.Flatten(input_shape=(max_number_of_nodes, 128, 1)))
- model.add(layers.Dropout(rate=0.6))
- model.add(layers.Dense(max_number_of_nodes * 128 / 32, activation='relu'))
- model.add(layers.Dropout(rate=0.6))
- model.add(layers.Dense(max_number_of_nodes * 128 / 64, activation='relu'))
- model.add(layers.Dropout(rate=0.6))
- model.add(layers.Dense(max_number_of_nodes, activation='linear'))
-
-model.summary()
-model.compile(optimizer='adam', loss=utils.loss, metrics=['accuracy'])
-
-X_train = X_train.reshape(*X_train.shape, 1)
-y_train = y_train.reshape(*y_train.shape, 1)
-X_test = X_test.reshape(*X_test.shape, 1)
-y_test = y_test.reshape(*y_test.shape, 1)
-
# Include the epoch in the file name (uses `str.format`)
checkpoint_path = "training/cp-{epoch:04d}.ckpt"
#checkpoint_dir = os.path.dirname(checkpoint_path)
batch_size = 32
-# Create a callback that saves the model's weights every 5 epochs
-cp_callback = tf.keras.callbacks.ModelCheckpoint(
- filepath=checkpoint_path,
- verbose=1,
- save_weights_only=True,
- save_freq=5*batch_size)
-
-# Save the weights using the `checkpoint_path` format
-model.save_weights(checkpoint_path.format(epoch=0))
-
-with utils.Timer() as t:
- history = model.fit(X_train, y_train, epochs=50, batch_size=batch_size,
- validation_data=(X_test, y_test),
- callbacks=[cp_callback])
-
-# to load a checkpoint: model.load_weights(checkpoint_path)
-model.save('saved_models/' + model_type + '_model')
-
-test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)
-
-print(f'Training took %.03fs' % t.interval)
-print(f'Test Accuracy: {test_acc}')
-print(f'Test Loss: {test_loss}')
-
-#plt.plot(history.history['accuracy'], label='accuracy')
-#plt.plot(history.history['val_accuracy'], label='val_accuracy')
-#plt.xlabel('Epoch')
-#plt.ylabel('Accuracy')
-#plt.ylim([0.5, 1])
-#plt.legend(loc='lower right')
+def main():
+ embeddings = utils.load_embeddings()
+
+ # betweenness = load_betweenness()
+ # closeness = load_closeness()
+ # electrical_closeness = load_electrical_closeness()
+ # communities = load_communities()
+
+ # print(embeddings.shape)
+ # print(betweenness.shape)
+ # print(closeness.shape)
+ # print(electrical_closeness)
+ # print(electrical_closeness.shape)
+ # print(communities.shape)
+ # print(communities)
+
+ if model_type == "betweenness":
+ target_data = utils.load_betweenness(max_number_of_nodes)
+ # TODO: remaining cases
+ else:
+ print(f"unknown type: {model_type}")
+ exit(1)
+
+ X_train, X_test, y_train, y_test = \
+ train_test_split(embeddings, target_data, test_size=0.33, random_state=42)
+
+ mirrored_strategy = tf.distribute.MirroredStrategy()
+
+ with mirrored_strategy.scope():
+ model = models.Sequential()
+ model.add(layers.Flatten(input_shape=(max_number_of_nodes, 128, 1)))
+ model.add(layers.Dropout(rate=0.6))
+ model.add(layers.Dense(max_number_of_nodes * 128 / 32, activation='relu'))
+ model.add(layers.Dropout(rate=0.6))
+ model.add(layers.Dense(max_number_of_nodes * 128 / 64, activation='relu'))
+ model.add(layers.Dropout(rate=0.6))
+ model.add(layers.Dense(max_number_of_nodes, activation='linear'))
+
+ model.summary()
+ model.compile(optimizer='adam', loss=utils.loss, metrics=['accuracy'])
+
+ X_train = X_train.reshape(*X_train.shape, 1)
+ y_train = y_train.reshape(*y_train.shape, 1)
+ X_test = X_test.reshape(*X_test.shape, 1)
+ y_test = y_test.reshape(*y_test.shape, 1)
+
+ # Create a callback that saves the model's weights every 5 epochs
+ cp_callback = tf.keras.callbacks.ModelCheckpoint(
+ filepath=checkpoint_path,
+ verbose=1,
+ save_weights_only=True,
+ save_freq=5*batch_size,
+ monitor='loss',
+ save_best_only=True)
+
+ # Save the weights using the `checkpoint_path` format
+ model.save_weights(checkpoint_path.format(epoch=0))
+
+ with utils.Timer() as t:
+ history = model.fit(X_train, y_train, epochs=50, batch_size=batch_size,
+ validation_data=(X_test, y_test),
+ callbacks=[cp_callback])
+
+ # to load a checkpoint: model.load_weights(checkpoint_path)
+ model.save('saved_models/' + model_type + '_model')
+
+ test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)
+
+ print(f'Training took %.03fs' % t.interval)
+ print(f'Test Accuracy: {test_acc}')
+ print(f'Test Loss: {test_loss}')
+
+ #plt.plot(history.history['accuracy'], label='accuracy')
+ #plt.plot(history.history['val_accuracy'], label='val_accuracy')
+ #plt.xlabel('Epoch')
+ #plt.ylabel('Accuracy')
+ #plt.ylim([0.5, 1])
+ #plt.legend(loc='lower right')
+
+
+if __name__ == "__main__":
+ main()
diff --git a/code/python/convolution_example.py b/code/python/convolution_example.py
deleted file mode 100644
index 88716fc..0000000
--- a/code/python/convolution_example.py
+++ /dev/null
@@ -1,57 +0,0 @@
-import tensorflow as tf
-import matplotlib.pyplot as plt
-
-from tensorflow.keras import datasets, layers, models
-
-tf.debugging.set_log_device_placement(True)
-
-(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
-
-# Normalize pixel values to be between 0 and 1
-train_images, test_images = train_images / 255.0, test_images / 255.0
-
-class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
- 'dog', 'frog', 'horse', 'ship', 'truck']
-
-plt.figure(figsize=(10, 10))
-for i in range(25):
- plt.subplot(5, 5, i + 1)
- plt.xticks([])
- plt.yticks([])
- plt.grid(False)
- plt.imshow(train_images[i], cmap=plt.cm.binary)
- # The CIFAR labels happen to be arrays,
- # which is why you need the extra index
- plt.xlabel(class_names[train_labels[i][0]])
-plt.show()
-
-model = models.Sequential()
-model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
-model.add(layers.MaxPooling2D((2, 2)))
-model.add(layers.Conv2D(64, (3, 3), activation='relu'))
-model.add(layers.MaxPooling2D((2, 2)))
-model.add(layers.Conv2D(64, (3, 3), activation='relu'))
-model.add(layers.Flatten())
-model.add(layers.Dense(64, activation='relu'))
-model.add(layers.Dense(10))
-
-model.summary()
-
-model.compile(optimizer='adam',
- loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
- metrics=['accuracy'])
-
-history = model.fit(train_images, train_labels, epochs=10,
- validation_data=(test_images, test_labels))
-
-plt.plot(history.history['accuracy'], label='accuracy')
-plt.plot(history.history['val_accuracy'], label='val_accuracy')
-plt.xlabel('Epoch')
-plt.ylabel('Accuracy')
-plt.ylim([0.5, 1])
-plt.legend(loc='lower right')
-plt.show()
-
-test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
-
-print(test_acc)
diff --git a/code/python/create_target_data.py b/code/python/create_target_data.py
index 44cbcb7..44cbcb7 100644..100755
--- a/code/python/create_target_data.py
+++ b/code/python/create_target_data.py
diff --git a/code/python/generate_graphs.py b/code/python/generate_graphs.py
deleted file mode 100644
index f175518..0000000
--- a/code/python/generate_graphs.py
+++ /dev/null
@@ -1,52 +0,0 @@
-# data storage
-import json
-# python -m pip install -e /home/niklas/thesis/networkit-embedding-branch
-import networkit as nk
-
-# general settings
-number_of_graphs = 1000
-target_dir = "./generated_graphs/"
-
-# graph settings
-#node_number = 20000
-node_number = 200
-cluster_number = 5
-intra_cluster_edge_probability = 0.73
-inter_cluster_edge_probability = 0.17
-
-prefix = f"{node_number}n_{cluster_number}c_{inter_cluster_edge_probability}e_{intra_cluster_edge_probability}i"
-
-# initialize graph generator algorithm
-crg = nk.generators.ClusteredRandomGraphGenerator(
- node_number,
- cluster_number,
- intra_cluster_edge_probability,
- inter_cluster_edge_probability
-)
-
-# initialize writer
-graph_writer = nk.graphio.EdgeListWriter(separator=",", firstNode=0)
-
-# generate graphs
-for i in range(0, number_of_graphs):
- print(f"Run {i:04} of {number_of_graphs}")
-
- # generate graph
- synthetic_g = nk.Graph(crg.generate())
-
- graph_file = f"/graph_{prefix}_{i:04}.nkg" # nkg --> NetworKitGraph
- features_file = f"/features_{prefix}_{i:04}.fff" # fff --> FlattenedFeatureFile
-
- # store graph
- graph_writer.write(synthetic_g, target_dir + graph_file)
-
- # run embedding, flatten features to simple list
- embedding = nk.embedding.Node2Vec(synthetic_g) # <-- missing parameters? ask Ahrens
- embedding.run()
- features = embedding.getFeatures()
- flat_features = sum(features, [])
- # --> len(flat_features) = 25600
-
- # store features
- with open(target_dir + features_file, "w", encoding='utf-8') as fp:
- json.dump(flat_features, fp)
diff --git a/code/python/input/nk b/code/python/input/nk
deleted file mode 120000
index a8288f9..0000000
--- a/code/python/input/nk
+++ /dev/null
@@ -1 +0,0 @@
-/home/niklas/thesis/networkit-embedding-branch/input \ No newline at end of file
diff --git a/code/python/input/test0.graph b/code/python/input/test0.graph
deleted file mode 100644
index a6b43da..0000000
--- a/code/python/input/test0.graph
+++ /dev/null
@@ -1,13 +0,0 @@
-12 10
-2 3 4 5 6
-1
-1
-1
-1
-1
-8 9 10 11 12
-7
-7
-7
-7
-7
diff --git a/code/python/lfr_generate_graphs.py b/code/python/lfr_generate_graphs.py
index 2434986..f1c7cdf 100755
--- a/code/python/lfr_generate_graphs.py
+++ b/code/python/lfr_generate_graphs.py
@@ -35,50 +35,46 @@ meta_file = target_dir + '/meta.json'
name = f"{node_count}n_{avg_node_degree}and_{max_node_degree}mnd_{degree_distribution_exponent}dde_" \
f"{min_community_size}mics_{max_community_size}macs_{community_distribution_exponent}cde_{mixing_coefficient}mu"
-
-#############
-# functions #
-#############
-
-
-########
-# code #
-########
# preparations
if not os.path.isdir(target_dir):
os.makedirs(target_dir)
if not os.path.exists(meta_file):
os.mknod(meta_file)
-# Initialize algorithm
-lfr = nk.generators.LFRGenerator(node_count)
+def main():
+ # Initialize algorithm
+ lfr = nk.generators.LFRGenerator(node_count)
+
+ # Generate sequences
+ lfr.generatePowerlawDegreeSequence(avg_node_degree, max_node_degree, degree_distribution_exponent)
+ lfr.generatePowerlawCommunitySizeSequence(min_community_size, max_community_size, community_distribution_exponent)
+ lfr.setMu(mixing_coefficient)
+
+ # generate graphs
+ for i in range(1, graph_count + 1):
+ print(f"Run {i:04} of {graph_count}")
-# Generate sequences
-lfr.generatePowerlawDegreeSequence(avg_node_degree, max_node_degree, degree_distribution_exponent)
-lfr.generatePowerlawCommunitySizeSequence(min_community_size, max_community_size, community_distribution_exponent)
-lfr.setMu(mixing_coefficient)
+ # Run algorithm
+ lfrG = lfr.generate()
-# generate graphs
-for i in range(1, graph_count + 1):
- print(f"Run {i:04} of {graph_count}")
+ # create names
+ graph_file = f"{target_dir}/{name}_{i:04}.graph"
+ features_file = f"{target_dir}/{name}_{i:04}.emb"
- # Run algorithm
- lfrG = lfr.generate()
+ # store graph
+ nk.writeGraph(lfrG, graph_file, nk.Format.LFR)
- # create names
- graph_file = f"{target_dir}/{name}_{i:04}.graph"
- features_file = f"{target_dir}/{name}_{i:04}.emb"
+ # run embedding
+ with utils.Timer() as t:
+ embedding = nk.embedding.Node2Vec(lfrG) # TODO: additional parameters?
+ embedding.run()
+ utils.edit_meta(meta_file, features_file, '%.03f' % t.interval)
+ features = embedding.getFeatures()
- # store graph
- nk.writeGraph(lfrG, graph_file, nk.Format.LFR)
+ # store features
+ with open(features_file, "w", encoding='utf-8') as fp:
+ json.dump(features, fp)
- # run embedding
- with utils.Timer() as t:
- embedding = nk.embedding.Node2Vec(lfrG) # TODO: additional parameters?
- embedding.run()
- utils.edit_meta(meta_file, features_file, '%.03f' % t.interval)
- features = embedding.getFeatures()
- # store features
- with open(features_file, "w", encoding='utf-8') as fp:
- json.dump(features, fp)
+if __name__ == "__main__":
+ main()
diff --git a/code/python/model.py b/code/python/model.py
deleted file mode 100644
index 43398b9..0000000
--- a/code/python/model.py
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
-embedding_matrix = np.zeros((len(model.wv.vocab), vector_dim))
-for i in range(len(model.wv.vocab)):
- embedding_vector = model.wv[model.wv.index2word[i]]
- if embedding_vector is not None:
- embedding_matrix[i] = embedding_vector \ No newline at end of file
diff --git a/code/python/pred.py b/code/python/pred.py
deleted file mode 100644
index 98d924d..0000000
--- a/code/python/pred.py
+++ /dev/null
@@ -1,12 +0,0 @@
-import json
-import numpy as np
-import tensorflow as tf
-
-model = tf.keras.models.load_model('saved_models/betweenness_model')
-
-with open('./generated_graphs/500n_20and_50mnd_-2dde_10mics_50macs_-1cde_0.5mu_0165.emb') as f:
- data = json.load(f)
-
-prediction = model.predict([data])
-
-print(prediction)
diff --git a/code/python/rumprobieren.py b/code/python/rumprobieren.py
deleted file mode 100644
index 10e6d7d..0000000
--- a/code/python/rumprobieren.py
+++ /dev/null
@@ -1,135 +0,0 @@
-import time
-# data storage
-import json
-
-# python -m pip install -e /home/niklas/thesis/networkit-embedding-branch
-import networkit as nk
-from networkit.community import PLM
-
-from sklearn.cluster import KMeans
-
-
-def calculate_cluster_coefficient_scores(graph):
- res = dict()
-
- for node in graph.iterNodes():
- # let p = number of possible edges in N = |N| times (|N| - 1)
- p = graph.degree(node) * 2 # times two, for the directed case - caught later on
- p *= (p - 1)
-
- if p == 0:
- res[node] = 1
- continue
-
- # let e = number of edges in N (undirected: times 2)
- e = graph.degree(node) # as it obviously has at least as many edges as neighbors
- for u in graph.iterNeighbors(node):
- for v in graph.iterNeighbors(node):
- if graph.hasEdge(u, v):
- e += 1
-
- if not graph.isDirected():
- e *= 2
-
- res[node] = e / p
-
- return res
-
-
-def rate_cluster(graph, cluster_label, coefficient_scores):
- rating = 0
-
- for node in graph.iterNodes():
- for u in graph.iterNeighbors(node):
- # vastly different scores
- if (coefficient_scores[u] < 0.3 and coefficient_scores[node] > 0.6) or \
- (coefficient_scores[u] > 0.6 and coefficient_scores[node] < 0.3):
- # same label is good (?)
- rating += 1 if cluster_label[u] == cluster_label[node] else -1
- # one low score, the other middle
- elif ((0.3 < coefficient_scores[node] < 0.6) and coefficient_scores[u] < 0.3) or \
- ((0.3 < coefficient_scores[u] < 0.6) and coefficient_scores[node] < 0.3):
- # same label is good (?)
- rating += 1 if cluster_label[u] == cluster_label[node] else -1
- return rating
-
-
-graphs = ["input/test0.graph", "input/nk/jazz.graph", "input/nk/karate.graph"]
-
-# Read the graph
-G = nk.graph.Graph(nk.readGraph(graphs[0], nk.Format.METIS))
-
-# synthetic tests
-node_number = 50
-cluster_number = 3
-intra_cluster_edge_probability = 1
-inter_cluster_edge_probability = 0
-# Initialize algorithm
-crg = nk.generators.ClusteredRandomGraphGenerator(
- node_number,
- cluster_number,
- intra_cluster_edge_probability,
- inter_cluster_edge_probability
-)
-# Run algorithm
-synthetic_g = crg.generate()
-# Overwrite G with synthetic
-G = synthetic_g
-
-# Print some info about the graph
-print("The graph has {:d} vertices and {:d} edges.".format(
- G.numberOfNodes(), G.numberOfEdges()))
-
-# Create a streaming client for Gephi
-#client = nk.gephi.streaming.GephiStreamingClient("http://localhost:8080/workspace1")
-
-# Export the graph
-#client.exportGraph(G)
-
-# score the nodes
-#cluster_coefficient_scores = calculate_cluster_coefficient_scores(G)
-
-# label the nodes with the score
-#client.exportNodeValues(G, list(cluster_coefficient_scores.values()), "cluster_coefficient")
-
-# get communities using PLM
-communities = nk.community.detectCommunities(G, algo=PLM(G, refine=False), inspect=False)
-#client.exportNodeValues(G, communities, 'PLM communities')
-
-# get number of cluster the "traditional" algo produced
-unique = set()
-for i in G.iterNodes():
- unique.add(communities[i])
-cluster_count = len(unique)
-
-# try to get communities via embedding --> K-Means
-embedding = nk.embedding.Node2Vec(G)
-
-# begin embedding algo
-tic = time.perf_counter()
-embedding.run()
-features = embedding.getFeatures()
-flat_features = sum(features, [])
-with open("test.json", "w", encoding='utf-8') as fp:
- json.dump(flat_features, fp, indent=2)
-
-with open("test.json", 'r') as f:
- score = json.load(f)
-
-# train k-means
-k_means = KMeans(n_clusters=cluster_count)
-k_means.fit(features)
-
-# get results
-labels = k_means.predict(features)
-# end embedding algo
-toc = time.perf_counter()
-labels = labels.tolist()
-
-print(f"time: {toc - tic:f} [s]")
-
-# make available in gephi
-#client.exportNodeValues(G, labels, 'Learned communities')
-
-print(f"rating of \"traditional\": {rate_cluster(G, communities, cluster_coefficient_scores)}\n"
- f"rating of embedding+ml: {rate_cluster(G, labels, cluster_coefficient_scores)}")
diff --git a/code/python/terraform_example_code.py b/code/python/terraform_example_code.py
deleted file mode 100644
index 192ff04..0000000
--- a/code/python/terraform_example_code.py
+++ /dev/null
@@ -1,98 +0,0 @@
-# -------------------------
-# ----- Toy Context -----
-# -------------------------
-import tensorflow as tf
-
-
-class Net(tf.keras.Model):
- """A simple linear model."""
-
- def get_config(self):
- pass
-
- def __init__(self):
- super(Net, self).__init__()
- self.l1 = tf.keras.layers.Dense(5)
-
- def call(self, x, **kwargs):
- return self.l1(x)
-
-
-def toy_dataset():
- inputs = tf.range(10.0)[:, None]
- labels = inputs * 5.0 + tf.range(5.0)[None, :]
- return (
- tf.data.Dataset.from_tensor_slices(dict(x=inputs, y=labels)).repeat().batch(2)
- )
-
-
-def train_step(net, example, optimizer):
- """Trains `net` on `example` using `optimizer`."""
- with tf.GradientTape() as tape:
- output = net(example["x"])
- loss = tf.reduce_mean(tf.abs(output - example["y"]))
- variables = net.trainable_variables
- gradients = tape.gradient(loss, variables)
- optimizer.apply_gradients(zip(gradients, variables))
- return loss
-
-
-# ----------------------------
-# ----- Create Objects -----
-# ----------------------------
-
-net = Net()
-opt = tf.keras.optimizers.Adam(0.1)
-dataset = toy_dataset()
-iterator = iter(dataset)
-ckpt = tf.train.Checkpoint(
- step=tf.Variable(1), optimizer=opt, net=net, iterator=iterator
-)
-manager = tf.train.CheckpointManager(ckpt, "./tf_ckpts", max_to_keep=3)
-
-# ----------------------------
-# ----- Train and Save -----
-# ----------------------------
-
-ckpt.restore(manager.latest_checkpoint)
-if manager.latest_checkpoint:
- print("Restored from {}".format(manager.latest_checkpoint))
-else:
- print("Initializing from scratch.")
-
-for _ in range(50):
- example = next(iterator)
- loss = train_step(net, example, opt)
- ckpt.step.assign_add(1)
- if int(ckpt.step) % 10 == 0:
- save_path = manager.save()
- print("Saved checkpoint for step {}: {}".format(int(ckpt.step), save_path))
- print("loss {:1.2f}".format(loss.numpy()))
-
-
-# ---------------------
-# ----- Restore -----
-# ---------------------
-
-# In another script, re-initialize objects
-opt = tf.keras.optimizers.Adam(0.1)
-net = Net()
-dataset = toy_dataset()
-iterator = iter(dataset)
-ckpt = tf.train.Checkpoint(
- step=tf.Variable(1), optimizer=opt, net=net, iterator=iterator
-)
-manager = tf.train.CheckpointManager(ckpt, "./tf_ckpts", max_to_keep=3)
-
-# Re-use the manager code above ^
-
-ckpt.restore(manager.latest_checkpoint)
-if manager.latest_checkpoint:
- print("Restored from {}".format(manager.latest_checkpoint))
-else:
- print("Initializing from scratch.")
-
-for _ in range(50):
- example = next(iterator)
- # Continue training or evaluate etc.
-
diff --git a/code/python/test.png b/code/python/test.png
deleted file mode 100644
index b95fd57..0000000
--- a/code/python/test.png
+++ /dev/null
Binary files differ
diff --git a/code/python/train.py b/code/python/train.py
deleted file mode 100644
index 223ad9b..0000000
--- a/code/python/train.py
+++ /dev/null
@@ -1,100 +0,0 @@
-#!/usr/bin/env python3
-import argparse
-import pickle
-import sys
-from datetime import datetime
-from pathlib import Path
-from sys import exit
-import numpy as np
-import tensorflow as tf
-from tensorflow import keras as keras
-import train_network as networks
-
-
-DATA_DIR = Path(Path(__file__).parent.absolute() / "data").resolve()
-
-
-def str2bool(v):
- if v.lower() in ('yes', 'true', 't', 'y', '1'):
- return True
- elif v.lower() in ('no', 'false', 'f', 'n', '0'):
- return False
- else:
- raise argparse.ArgumentTypeError('Boolean value expected.')
-
-
-def main(raw_args=None, model=None):
- parser = argparse.ArgumentParser(description='Train the network given')
-
- parser.add_argument('-b', '--database-path', dest='imgdb_path', default=str(DATA_DIR / 'imgdb.pkl'),
- help='Path to the image database to use for training. Default is imgdb.pkl in the data folder.')
- parser.add_argument("--output", dest="output", default="./", help="Folder where the trained models are saved")
-
- # for continuing training
- parser.add_argument('-m', '--model-path', dest='model_path', default="model.h5",
- help='Store the trained model using this path. Default is model.h5.')
- parser.add_argument("--proceed", type=str2bool, nargs='?', dest="proceed",
- const=True, help="Use the stored and pre-trained model base.")
-
- # hyperparameters
- parser.add_argument("--batch-size", dest="batch_size", default=256,
- help="Batch size. Default is 256")
- parser.add_argument("--epochs", dest="epochs", default=200, type=int,
- help="Number of epochs")
- args = parser.parse_args(raw_args)
-
- if model is not None:
- model.summary()
-
- with open(args.imgdb_path, "rb") as f:
- pickle.load(f)
- # skip mean
- x = pickle.load(f) # x are all input images
- y = pickle.load(f) # y are the trainings target: [x,y,r,1]
-
- # define the Keras network
- if args.proceed is not None and args.proceed is True:
- print("Loading model " + args.model_path)
- model = tf.keras.models.load_model(args.model_path)
- elif model is not None:
- print("Creating new model")
- else:
- print("ERROR: No model specified")
- exit(1)
-
- # For using custom loss import your loss function and use the name of the function as loss argument.
- model.compile(loss='mean_squared_error',
- optimizer='adam',
- metrics=['accuracy'])
-
- """
- The save callback will overwrite the previous models if the new model is better then the last. Restarting the
- training will always overwrite the models.
- """
- filepath = Path(args.output) / (model.name + ".h5")
- save_callback = tf.keras.callbacks.ModelCheckpoint(filepath=str(filepath), monitor='loss', verbose=1,
- save_best_only=True)
-
- log_path = Path(args.output) / "logs" / (model.name + "_" + str(datetime.now()).replace(" ", "_").replace(":", "-"))
- log_callback = keras.callbacks.TensorBoard(log_dir=log_path, profile_batch=0)
-
- callbacks = [save_callback, log_callback]
-
- # TODO prepare an extra validation set, that is consistent over multiple runs
- # history = model.fit(x, y, batch_size=args.batch_size, epochs=args.epochs, verbose=1,
- # validation_data=(X_test, Y_test),callbacks=callbacks)
- history = model.fit(x, y, batch_size=args.batch_size, epochs=args.epochs, verbose=1, validation_split=0.1,
- callbacks=callbacks)
- return history
-
-
-if __name__ == '__main__':
- test_model = networks.network_new()
- output_dir = "models"
- # forward commandline arguments to the arg-parser in the main function
- train_history = main(sys.argv[1:] + ['--output', output_dir], model=test_model)
-
- # save history in same folder as model
- history_filepath = Path(output_dir) / ("history_" + test_model.name + ".pkl")
- with open(str(history_filepath), "wb") as hist_file:
- pickle.dump(train_history.history, hist_file)
diff --git a/code/python/train_network.py b/code/python/train_network.py
deleted file mode 100644
index 6ea6fec..0000000
--- a/code/python/train_network.py
+++ /dev/null
@@ -1,45 +0,0 @@
-"""
- use activation argument in layers instead of defining activation functions as extra layers. The result is the same
- (https://stackoverflow.com/questions/59476564/relu-as-a-parameter-in-dense-or-any-other-layer-vs-relu-as-a-layer-in-keras)
- but the code generators support it better
-"""
-
-from tensorflow.keras.models import Sequential
-from tensorflow.keras.layers import Convolution2D, LeakyReLU, MaxPooling2D, Flatten, Dense, ReLU, Input, \
- PReLU, Softmax
-from tensorflow.keras import Model
-
-def network_new():
- input_shape = (16, 16, 1)
-
- model = Sequential()
- model._name = "test_model_1"
-
- model.add(Convolution2D(4, (3, 3), input_shape=input_shape, padding='same', name="Conv2D_1"))
- model.add(ReLU(name="activation_1"))
-
- model.add(Convolution2D(5, (3, 3), padding='same', name="Conv2D_2"))
- model.add(PReLU(name="activation_2"))
-
- model.add(MaxPooling2D(pool_size=(2, 2), name="pooling_1"))
- model.add(Convolution2D(6, (3, 3), padding='same', name="Conv2D_3"))
- model.add(PReLU(name="activation_3"))
-
- model.add(MaxPooling2D(pool_size=(2, 2), name="pooling_2"))
- model.add(Convolution2D(7, (3, 3), padding='same', name="Conv2D_4"))
- model.add(ReLU(name="activation_4"))
-
- model.add(Convolution2D(7, (1, 1), padding='same', name="Conv2D_5"))
-
- # classifier
- model.add(Flatten(name="flatten_1"))
-
- # output is radius, x, y, confidence
- model.add(Dense(4, activation="relu", name="dense_1"))
-
- return model
-
-# build model
-# load flattened features
-# load exact solutions
-#