From 0e6eafd0afd8167c3fa101fb755f872071da041d Mon Sep 17 00:00:00 2001 From: Matthew Burke Date: Tue, 19 Nov 2019 14:33:06 -0800 Subject: [PATCH 1/2] tensorflow 2.0 compatibility --- README.md | 6 +++- numpy-simplex-matrix.py | 7 ++-- numpy-simplex-naive.py | 7 ++-- tensor-flow-simplex-matrix.py | 63 ++++++++++++++++------------------- tf_get_simplex_vertices.py | 25 ++++++++------ tf_input.py | 13 ++++---- tf_map_gradient.py | 15 +++++---- 7 files changed, 68 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index f99be9c..2795320 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # simplexnoise -python implementation of 3d simplex noise + +Python implementation of 3d simplex noise + +Forked and modified from the original to be compatible with Tensorflow 2.0 with minimal changes + diff --git a/numpy-simplex-matrix.py b/numpy-simplex-matrix.py index 6f16f80..81e6f85 100644 --- a/numpy-simplex-matrix.py +++ b/numpy-simplex-matrix.py @@ -81,14 +81,11 @@ def matrix_noise3d(input_vectors, perm, grad3): if __name__ == "__main__": - shape = (512, 512) + shape = (3840, 2160) phases = 5 scaling = 200.0 input_vectors = get_input_vectors(shape, phases, scaling) - num_steps_burn_in = 10 - num_steps_benchmark = 20 - for i in range(num_steps_burn_in): - raw_noise = matrix_noise3d(input_vectors, np_perm, np_grad3) + num_steps_benchmark = 1 start_time = time() for i in range(num_steps_benchmark): raw_noise = matrix_noise3d(input_vectors, np_perm, np_grad3) diff --git a/numpy-simplex-naive.py b/numpy-simplex-naive.py index 4b90f2a..3932239 100644 --- a/numpy-simplex-naive.py +++ b/numpy-simplex-naive.py @@ -78,14 +78,15 @@ def np_noise3d(v): if __name__ == "__main__": - shape = (512, 512) + shape = (1000, 512) phases = 5 scaling = 200.0 input_vectors = get_input_vectors(shape, phases, scaling) raw_noise = np.empty(input_vectors.shape[0], dtype=np.float32) start_time = time() - for i in range(0, input_vectors.shape[0]): + for i in range(input_vectors.shape[0]): raw_noise[i] = np_noise3d(input_vectors[i]) - print("The calculation took " + str(time() - start_time) + " seconds.") + end_time = time() + print("The calculation took {:.4} seconds.".format((end_time - start_time) / input_vectors.shape[0])) image_data = sum_phases(raw_noise, phases, shape) show(image_data) diff --git a/tensor-flow-simplex-matrix.py b/tensor-flow-simplex-matrix.py index 5d10ec3..22302e3 100644 --- a/tensor-flow-simplex-matrix.py +++ b/tensor-flow-simplex-matrix.py @@ -46,10 +46,10 @@ def calculate_gradient_contribution(offsets, gis, gradient_map, length): - t = 0.5 - offsets[:, 0] ** 2 - offsets[:, 1] ** 2 - offsets[:, 2] ** 2 + t = 0.5 - offsets[:, 0] ** 2. - offsets[:, 1] ** 2. - offsets[:, 2] ** 2. mapped_gis = map_gradients(gradient_map, gis, length) dot_products = tf.reduce_sum(mapped_gis * offsets, 1) - return tf.to_float(tf.greater_equal(t, 0)) * t ** 4 * dot_products + return tf.cast(tf.math.greater_equal(t, 0.), tf.float32) * t ** 4. * dot_products def noise3d(input_vectors, perm, grad3, vertex_table, length): @@ -57,11 +57,11 @@ def noise3d(input_vectors, perm, grad3, vertex_table, length): skewed_vectors = tf.floor(input_vectors + tf.expand_dims(skew_factors, 1)) unskew_factors = (skewed_vectors[:, 0] + skewed_vectors[:, 1] + skewed_vectors[:, 2]) * 1.0 / 6.0 offsets_0 = input_vectors - (skewed_vectors - tf.expand_dims(unskew_factors, 1)) - simplex_vertices = get_simplex_vertices(offsets_0, vertex_table, length) + simplex_vertices = get_simplex_vertices(offsets_0, vertex_table, length) # divided it by 2, doesn't error now offsets_1 = offsets_0 - simplex_vertices[:, 0, :] + 1.0 / 6.0 offsets_2 = offsets_0 - simplex_vertices[:, 1, :] + 1.0 / 3.0 offsets_3 = offsets_0 - 0.5 - masked_skewed_vectors = tf.to_int32(skewed_vectors) % 256 + masked_skewed_vectors = tf.cast(skewed_vectors, tf.int32) % 256 gi0s = tf.gather_nd( perm, tf.expand_dims(masked_skewed_vectors[:, 0], 1) + @@ -75,28 +75,28 @@ def noise3d(input_vectors, perm, grad3, vertex_table, length): gi1s = tf.gather_nd( perm, tf.expand_dims(masked_skewed_vectors[:, 0], 1) + - tf.expand_dims(tf.to_int32(simplex_vertices[:, 0, 0]), 1) + + tf.expand_dims(tf.cast(simplex_vertices[:, 0, 0], tf.int32), 1) + tf.expand_dims(tf.gather_nd( perm, tf.expand_dims(masked_skewed_vectors[:, 1], 1) + - tf.expand_dims(tf.to_int32(simplex_vertices[:, 0, 1]), 1) + + tf.expand_dims(tf.cast(simplex_vertices[:, 0, 1], tf.int32), 1) + tf.expand_dims(tf.gather_nd( perm, tf.expand_dims(masked_skewed_vectors[:, 2], 1) + - tf.expand_dims(tf.to_int32(simplex_vertices[:, 0, 2]), 1)), 1)), 1) + tf.expand_dims(tf.cast(simplex_vertices[:, 0, 2], tf.int32), 1)), 1)), 1) ) % 12 gi2s = tf.gather_nd( perm, tf.expand_dims(masked_skewed_vectors[:, 0], 1) + - tf.expand_dims(tf.to_int32(simplex_vertices[:, 1, 0]), 1) + + tf.expand_dims(tf.cast(simplex_vertices[:, 1, 0], tf.int32), 1) + tf.expand_dims(tf.gather_nd( perm, tf.expand_dims(masked_skewed_vectors[:, 1], 1) + - tf.expand_dims(tf.to_int32(simplex_vertices[:, 1, 1]), 1) + + tf.expand_dims(tf.cast(simplex_vertices[:, 1, 1], tf.int32), 1) + tf.expand_dims(tf.gather_nd( perm, tf.expand_dims(masked_skewed_vectors[:, 2], 1) + - tf.expand_dims(tf.to_int32(simplex_vertices[:, 1, 2]), 1)), 1)), 1) + tf.expand_dims(tf.cast(simplex_vertices[:, 1, 2], tf.int32), 1)), 1)), 1) ) % 12 gi3s = tf.gather_nd( perm, @@ -121,43 +121,36 @@ def noise3d(input_vectors, perm, grad3, vertex_table, length): def calculate_image(noise_values, phases, shape): val = tf.floor((tf.add_n(tf.split( - 2, - phases, tf.reshape(noise_values, [shape[0], shape[1], phases]) / tf.pow( 2.0, - tf.linspace(0.0, tf.to_float(phases - 1), phases)) - )) + 1.0) * 128) - return tf.concat(2, [val, val, val]) + tf.linspace(0.0, phases - 1., phases)), phases, 2)) + 1.0) * 128.) + return tf.concat([val, val, val], 2) if __name__ == "__main__": - shape = (512, 512) - phases = 10 + + print('is executing eagerly:', tf.executing_eagerly()) + + shape = [3840, 2160] + phases = 5 scaling = 200.0 offset = (0.0, 0.0, 1.7) - v_shape = tf.Variable([512, 512], name='shape') - v_phases = tf.Variable(5, name='phases') - v_scaling = tf.Variable(200.0, name='scaling') - v_offset = tf.Variable([0.0, 0.0, 1.7], name='offset') + v_shape = tf.constant(shape, name='shape') + v_phases = tf.constant(5, name='phases') + v_scaling = tf.constant(200.0, name='scaling') + v_offset = tf.constant([0.0, 0.0, 1.7], name='offset') v_input_vectors = get_input_vectors(v_shape, v_phases, v_scaling, v_offset) - perm = tf.Variable(np_perm, name='perm') - grad3 = tf.Variable(np_grad3, name='grad3') + perm = tf.constant(np_perm, name='perm') + grad3 = tf.constant(np_grad3, name='grad3') num_steps_burn_in = 10 num_steps_benchmark = 20 - vertex_table = tf.Variable(np_vertex_table, name='vertex_table') + vertex_table = tf.constant(np_vertex_table, name='vertex_table') + start_time = time() raw_noise = noise3d(v_input_vectors, perm, grad3, vertex_table, shape[0] * shape[1] * phases) + end_time = time() + print('Time to calculate one iteration: {:.4f}'.format(end_time - start_time)) raw_image_data = calculate_image(raw_noise, phases, v_shape) - init = tf.initialize_all_variables() input_vectors = get_input_vectors(shape, phases, scaling, offset) noise = noise3d(input_vectors, np_perm, np_grad3, np_vertex_table, shape[0] * shape[1] * phases) image_data = calculate_image(noise, phases, shape) - sess = tf.Session() - sess.run(init) - for i in range(num_steps_burn_in): - raw_img = sess.run(image_data) - start_time = time() - for i in range(num_steps_benchmark): - raw_img = sess.run(image_data) - print("The calculation took %.4f seconds." % ((time() - start_time) / num_steps_benchmark)) - # writer = tf.train.SummaryWriter("tf-logs/", sess.graph) # write logs for TensorBoard - show(raw_img.astype(np.uint8)) + show(image_data.numpy().astype(np.uint8)) diff --git a/tf_get_simplex_vertices.py b/tf_get_simplex_vertices.py index 303fe1b..1568424 100644 --- a/tf_get_simplex_vertices.py +++ b/tf_get_simplex_vertices.py @@ -14,32 +14,35 @@ ], dtype=np.float32) # Dimesions are: x0 >= y0, y0 >= z0, x0 >= z0 -np_vertex_table = np.array([ +# np_vertex_table = np.array([ +tf_vertex_table = tf.convert_to_tensor([ [[vertex_options[3], vertex_options[3]], [vertex_options[4], vertex_options[5]]], [[vertex_options[2], vertex_options[1]], [vertex_options[2], vertex_options[0]]] -], dtype=np.float32) +], dtype=tf.float32) def get_simplex_vertices(offsets, vertex_table, length): - vertex_table_x_index = tf.to_int32(offsets[:, 0] >= offsets[:, 1]) - vertex_table_y_index = tf.to_int32(offsets[:, 1] >= offsets[:, 2]) - vertex_table_z_index = tf.to_int32(offsets[:, 0] >= offsets[:, 2]) - index_list = tf.concat(1, [ - tf.reshape(tf.tile(tf.concat(1, [ + vertex_table_x_index = tf.cast((offsets[:, 0] >= offsets[:, 1]), tf.int32) + vertex_table_y_index = tf.cast((offsets[:, 1] >= offsets[:, 2]), tf.int32) + vertex_table_z_index = tf.cast((offsets[:, 0] >= offsets[:, 2]), tf.int32) + + index_list = tf.concat([ + tf.reshape(tf.tile(tf.concat([ tf.expand_dims(vertex_table_x_index, 1), tf.expand_dims(vertex_table_y_index, 1), tf.expand_dims(vertex_table_z_index, 1), - ]), [1, 6]), [6 * length, 3]), - tf.expand_dims(tf.tile(tf.range(0, limit=6), [length]), 1)]) + ], 1), [1, 6]), [6 * length, 3]), + tf.expand_dims(tf.tile(tf.range(0, limit=6), [length]), 1)], 1) vertices = tf.reshape(tf.gather_nd(vertex_table, index_list), [-1, 2, 3]) return vertices if __name__ == "__main__": - vertex_table = tf.Variable(np_vertex_table, name='vertex_table') - test_offsets = tf.Variable([[0.2, 0.1, 0.3], [0.03, 0.15, 0.12], + + vertex_table = tf.constant(tf_vertex_table, name='vertex_table') + test_offsets = tf.constant([[0.2, 0.1, 0.3], [0.03, 0.15, 0.12], [0.34, 0.21, 0.31], [0.49, 0.0012, 0.237]], name="offsets") verts = get_simplex_vertices(test_offsets, vertex_table, 4) init = tf.initialize_all_variables() diff --git a/tf_input.py b/tf_input.py index abb739b..63c8e94 100644 --- a/tf_input.py +++ b/tf_input.py @@ -11,16 +11,17 @@ def tf_repeat(x, num_repeats): def get_input_vectors(shape, phases, scaling, offset): - x = tf.reshape(tf_repeat(offset[0] + tf.linspace(0.0, tf.to_float(shape[0] - 1), shape[0]) / scaling, + x = tf.reshape(tf_repeat(offset[0] + tf.linspace(0.0, float(shape[0]) - 1., shape[0]) / float(scaling), shape[1] * phases), - [shape[0], shape[1], phases]) * tf.pow(2.0, tf.linspace(0.0, tf.to_float(phases - 1), phases)) + [shape[0], shape[1], phases]) * tf.pow(2.0, tf.linspace(0.0, float(phases) - 1., phases)) y = tf.reshape(tf_repeat(tf.tile( - offset[1] + tf.linspace(0.0, tf.to_float(shape[1] - 1), shape[1]) / scaling, + offset[1] + tf.linspace(0.0, float(shape[1]) - 1., shape[1]) / scaling, [shape[0]] - ), phases), [shape[0], shape[1], phases]) * tf.pow(2.0, tf.linspace(0.0, tf.to_float(phases - 1), phases)) + ), phases), [shape[0], shape[1], phases]) * tf.pow(2.0, tf.linspace(0.0, float(phases) - 1., phases)) z = tf.reshape( - tf.tile(offset[2] + 10 * tf.linspace(0.0, tf.to_float(phases - 1), phases), [shape[0] * shape[1]]), + tf.tile(offset[2] + 10. * tf.linspace(0.0, float(phases) - 1., phases), [shape[0] * shape[1]]), [shape[0], shape[1], phases, 1]) x = tf.reshape(x, [shape[0], shape[1], phases, 1]) y = tf.reshape(y, [shape[0], shape[1], phases, 1]) - return tf.reshape(tf.concat(3, [x, y, z]), [shape[0] * shape[1] * phases, 3]) + + return tf.reshape(tf.concat([x, y, z], 3), [shape[0] * shape[1] * phases, 3]) diff --git a/tf_map_gradient.py b/tf_map_gradient.py index d0cb1f7..e1fb9db 100644 --- a/tf_map_gradient.py +++ b/tf_map_gradient.py @@ -11,18 +11,19 @@ def map_gradients(gradient_map, gis, length): - index_tensor = tf.reshape(tf.concat(1, [ + index_tensor = tf.reshape(tf.concat([ tf.reshape(tf.tile(tf.expand_dims(gis, 1), [1, 3]), [length * 3, 1]), tf.expand_dims(tf.tile(tf.range(0, limit=3), [length]), 1) - ]), [length, 3, 2]) + ], 1), [length, 3, 2]) return tf.gather_nd(gradient_map, index_tensor) if __name__ == "__main__": - gradient_map = tf.Variable(np_grad3, name='vertex_table') + gradient_map = tf.constant(np_grad3, name='vertex_table') gis = tf.Variable([0, 3, 7, 2, 9, 11, 7, 4], name="gis") gradients = map_gradients(gradient_map, gis, 8) - init = tf.initialize_all_variables() - sess = tf.Session() - sess.run(init) - print(sess.run(gradients)) + # init = tf.initialize_all_variables() + # sess = tf.Session() + # sess.run(init) + # print(sess.run(gradients)) + print(gradients) From 2b84292df73b4eb3f932c281da6352892e79dd4a Mon Sep 17 00:00:00 2001 From: Matthew Burke Date: Thu, 19 Dec 2019 14:48:45 -0800 Subject: [PATCH 2/2] fixed some example run code --- README.md | 3 +-- numpy-simplex-matrix.py | 7 +++++-- numpy-simplex-naive.py | 5 ++--- tensor-flow-simplex-matrix.py | 23 +++++++++++++---------- tf_get_simplex_vertices.py | 5 +---- tf_map_gradient.py | 4 ---- 6 files changed, 22 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 2795320..21e75dd 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,4 @@ Python implementation of 3d simplex noise -Forked and modified from the original to be compatible with Tensorflow 2.0 with minimal changes - +Note: Tensorflow implementation now compatible with verseion 2.x and may not work with 1.x versions. diff --git a/numpy-simplex-matrix.py b/numpy-simplex-matrix.py index 81e6f85..6f16f80 100644 --- a/numpy-simplex-matrix.py +++ b/numpy-simplex-matrix.py @@ -81,11 +81,14 @@ def matrix_noise3d(input_vectors, perm, grad3): if __name__ == "__main__": - shape = (3840, 2160) + shape = (512, 512) phases = 5 scaling = 200.0 input_vectors = get_input_vectors(shape, phases, scaling) - num_steps_benchmark = 1 + num_steps_burn_in = 10 + num_steps_benchmark = 20 + for i in range(num_steps_burn_in): + raw_noise = matrix_noise3d(input_vectors, np_perm, np_grad3) start_time = time() for i in range(num_steps_benchmark): raw_noise = matrix_noise3d(input_vectors, np_perm, np_grad3) diff --git a/numpy-simplex-naive.py b/numpy-simplex-naive.py index 3932239..2735ea0 100644 --- a/numpy-simplex-naive.py +++ b/numpy-simplex-naive.py @@ -78,7 +78,7 @@ def np_noise3d(v): if __name__ == "__main__": - shape = (1000, 512) + shape = (512, 512) phases = 5 scaling = 200.0 input_vectors = get_input_vectors(shape, phases, scaling) @@ -86,7 +86,6 @@ def np_noise3d(v): start_time = time() for i in range(input_vectors.shape[0]): raw_noise[i] = np_noise3d(input_vectors[i]) - end_time = time() - print("The calculation took {:.4} seconds.".format((end_time - start_time) / input_vectors.shape[0])) + print("The calculation took " + str(time() - start_time) + " seconds.") image_data = sum_phases(raw_noise, phases, shape) show(image_data) diff --git a/tensor-flow-simplex-matrix.py b/tensor-flow-simplex-matrix.py index 22302e3..bbdaeeb 100644 --- a/tensor-flow-simplex-matrix.py +++ b/tensor-flow-simplex-matrix.py @@ -128,22 +128,17 @@ def calculate_image(noise_values, phases, shape): if __name__ == "__main__": - - print('is executing eagerly:', tf.executing_eagerly()) - - shape = [3840, 2160] - phases = 5 + shape = (512, 512) + phases = 10 scaling = 200.0 offset = (0.0, 0.0, 1.7) v_shape = tf.constant(shape, name='shape') - v_phases = tf.constant(5, name='phases') - v_scaling = tf.constant(200.0, name='scaling') - v_offset = tf.constant([0.0, 0.0, 1.7], name='offset') + v_phases = tf.constant(phases, name='phases') + v_scaling = tf.constant(scaling, name='scaling') + v_offset = tf.constant(offset, name='offset') v_input_vectors = get_input_vectors(v_shape, v_phases, v_scaling, v_offset) perm = tf.constant(np_perm, name='perm') grad3 = tf.constant(np_grad3, name='grad3') - num_steps_burn_in = 10 - num_steps_benchmark = 20 vertex_table = tf.constant(np_vertex_table, name='vertex_table') start_time = time() raw_noise = noise3d(v_input_vectors, perm, grad3, vertex_table, shape[0] * shape[1] * phases) @@ -154,3 +149,11 @@ def calculate_image(noise_values, phases, shape): noise = noise3d(input_vectors, np_perm, np_grad3, np_vertex_table, shape[0] * shape[1] * phases) image_data = calculate_image(noise, phases, shape) show(image_data.numpy().astype(np.uint8)) + + # Custom vector repeating the top quarter of the image 4 times + base_values = input_vectors[0:int(input_vectors.shape[0] / 4)] + input_vectors = tf.tile(base_values, [4, 1]) + noise = noise3d(input_vectors, np_perm, np_grad3, np_vertex_table, shape[0] * shape[1] * phases) + image_data = calculate_image(noise, phases, shape) + + show(image_data.numpy().astype(np.uint8)) diff --git a/tf_get_simplex_vertices.py b/tf_get_simplex_vertices.py index 1568424..251da0d 100644 --- a/tf_get_simplex_vertices.py +++ b/tf_get_simplex_vertices.py @@ -45,7 +45,4 @@ def get_simplex_vertices(offsets, vertex_table, length): test_offsets = tf.constant([[0.2, 0.1, 0.3], [0.03, 0.15, 0.12], [0.34, 0.21, 0.31], [0.49, 0.0012, 0.237]], name="offsets") verts = get_simplex_vertices(test_offsets, vertex_table, 4) - init = tf.initialize_all_variables() - sess = tf.Session() - sess.run(init) - print(sess.run(tf.to_int32(verts[:, 0, 1]))) + print(verts) diff --git a/tf_map_gradient.py b/tf_map_gradient.py index e1fb9db..536ba4d 100644 --- a/tf_map_gradient.py +++ b/tf_map_gradient.py @@ -22,8 +22,4 @@ def map_gradients(gradient_map, gis, length): gradient_map = tf.constant(np_grad3, name='vertex_table') gis = tf.Variable([0, 3, 7, 2, 9, 11, 7, 4], name="gis") gradients = map_gradients(gradient_map, gis, 8) - # init = tf.initialize_all_variables() - # sess = tf.Session() - # sess.run(init) - # print(sess.run(gradients)) print(gradients)