From a366d28adeaf38aede24c657c9ef4becaa10d952 Mon Sep 17 00:00:00 2001 From: jh Date: Tue, 16 Mar 2021 16:36:52 -0700 Subject: [PATCH 01/17] debug shape --- models/Model_SAEHD/Model.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 25d6228..b47e339 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -495,6 +495,9 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... gpu_pred_src_src_d, \ gpu_pred_src_src_d2 = self.D_src(gpu_pred_src_src_masked_opt) + print("Shape (gpu_pred_src_src_d): ", gpu_pred_src_src_d.shape) + print("Shape (gpu_pred_src_src_d2): ", gpu_pred_src_src_d2.shape) + gpu_pred_src_src_d_ones = tf.ones_like (gpu_pred_src_src_d) gpu_pred_src_src_d_zeros = tf.zeros_like(gpu_pred_src_src_d) From 22cb3103d88e443e00ad37b099611941029e617a Mon Sep 17 00:00:00 2001 From: jh Date: Tue, 16 Mar 2021 16:43:03 -0700 Subject: [PATCH 02/17] more debugging --- models/Model_SAEHD/Model.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index b47e339..2bfed55 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -498,6 +498,19 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... print("Shape (gpu_pred_src_src_d): ", gpu_pred_src_src_d.shape) print("Shape (gpu_pred_src_src_d2): ", gpu_pred_src_src_d2.shape) + print("Flattened Shape (gpu_pred_src_src_d): ", gpu_pred_src_src_d.flatten().shape()) + + # def get_smooth_noisy_labels_like(label, tensor, smoothing=0.2, noise=0.05): + # new_labels = [] + # for label in labels: + # if np.random.random() < noise: + # label = 0 if label == 1 else 1 + # if label == 0: + # new_labels.append(np.random.uniform(0, 0+smoothing/2)) + # else: + # new_labels.append(np.random.uniform(1-smoothing/2, 1.0)) + # return new_labels + gpu_pred_src_src_d_ones = tf.ones_like (gpu_pred_src_src_d) gpu_pred_src_src_d_zeros = tf.zeros_like(gpu_pred_src_src_d) From 6092a77924f34762b8c4769b753d5a4136862664 Mon Sep 17 00:00:00 2001 From: jh Date: Tue, 16 Mar 2021 16:57:53 -0700 Subject: [PATCH 03/17] more debugging --- models/Model_SAEHD/Model.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 2bfed55..5d48762 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -497,8 +497,9 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... print("Shape (gpu_pred_src_src_d): ", gpu_pred_src_src_d.shape) print("Shape (gpu_pred_src_src_d2): ", gpu_pred_src_src_d2.shape) + for i in gpu_pred_src_src_d2.shape: + print("Shape (gpu_pred_src_src_d2) i: ", i) - print("Flattened Shape (gpu_pred_src_src_d): ", gpu_pred_src_src_d.flatten().shape()) # def get_smooth_noisy_labels_like(label, tensor, smoothing=0.2, noise=0.05): # new_labels = [] From 0d5b67a35518c9f61cdd4fccd19089c0fc54642f Mon Sep 17 00:00:00 2001 From: jh Date: Tue, 16 Mar 2021 17:11:31 -0700 Subject: [PATCH 04/17] test --- models/Model_SAEHD/Model.py | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 5d48762..216f861 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -497,32 +497,30 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... print("Shape (gpu_pred_src_src_d): ", gpu_pred_src_src_d.shape) print("Shape (gpu_pred_src_src_d2): ", gpu_pred_src_src_d2.shape) - for i in gpu_pred_src_src_d2.shape: - print("Shape (gpu_pred_src_src_d2) i: ", i) - # def get_smooth_noisy_labels_like(label, tensor, smoothing=0.2, noise=0.05): - # new_labels = [] - # for label in labels: - # if np.random.random() < noise: - # label = 0 if label == 1 else 1 - # if label == 0: - # new_labels.append(np.random.uniform(0, 0+smoothing/2)) - # else: - # new_labels.append(np.random.uniform(1-smoothing/2, 1.0)) - # return new_labels + def get_smooth_noisy_label(label, smoothing=0.2, noise=0.05): + if np.random.random() < noise: + label = 0 if label == 1 else 1 + if label == 0: + return lambda x: np.random.uniform(0, 0+smoothing/2) + else: + return lambda x: np.random.uniform(1-smoothing/2, 1.0) - gpu_pred_src_src_d_ones = tf.ones_like (gpu_pred_src_src_d) - gpu_pred_src_src_d_zeros = tf.zeros_like(gpu_pred_src_src_d) + gpu_pred_src_src_d_ones = tf.map_fn (get_smooth_noisy_label(1, smoothing=0.2, noise=0.05), gpu_pred_src_src_d) + gpu_pred_src_src_d_zeros = tf.map_fn (get_smooth_noisy_label(0, smoothing=0.2, noise=0.05), gpu_pred_src_src_d) - gpu_pred_src_src_d2_ones = tf.ones_like (gpu_pred_src_src_d2) - gpu_pred_src_src_d2_zeros = tf.zeros_like(gpu_pred_src_src_d2) + print("Shape (gpu_pred_src_src_d_ones): ", gpu_pred_src_src_d.shape) + print("Shape (gpu_pred_src_src_d_zeros): ", gpu_pred_src_src_d2.shape) + + gpu_pred_src_src_d2_ones = tf.map_fn (get_smooth_noisy_label(1, smoothing=0.2, noise=0.05), gpu_pred_src_src_d2) + gpu_pred_src_src_d2_zeros = tf.map_fn (get_smooth_noisy_label(0, smoothing=0.2, noise=0.05), gpu_pred_src_src_d2) gpu_target_src_d, \ gpu_target_src_d2 = self.D_src(gpu_target_src_masked_opt) - gpu_target_src_d_ones = tf.ones_like(gpu_target_src_d) - gpu_target_src_d2_ones = tf.ones_like(gpu_target_src_d2) + gpu_target_src_d_ones = tf.map_fn (get_smooth_noisy_label(1, smoothing=0.2, noise=0.05), gpu_target_src_d) + gpu_target_src_d2_ones = tf.map_fn (get_smooth_noisy_label(1, smoothing=0.2, noise=0.05), gpu_target_src_d2) gpu_D_src_dst_loss = (DLoss(gpu_target_src_d_ones , gpu_target_src_d) + \ DLoss(gpu_pred_src_src_d_zeros , gpu_pred_src_src_d) ) * 0.5 + \ From cca2d86c3f2098afadc373d98b16a3d0805c2bde Mon Sep 17 00:00:00 2001 From: jh Date: Tue, 16 Mar 2021 17:17:38 -0700 Subject: [PATCH 05/17] test --- models/Model_SAEHD/Model.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 216f861..4fa6b72 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -495,8 +495,10 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... gpu_pred_src_src_d, \ gpu_pred_src_src_d2 = self.D_src(gpu_pred_src_src_masked_opt) - print("Shape (gpu_pred_src_src_d): ", gpu_pred_src_src_d.shape) - print("Shape (gpu_pred_src_src_d2): ", gpu_pred_src_src_d2.shape) + print("gpu_pred_src_src_d.shape: ", gpu_pred_src_src_d.shape) + print("gpu_pred_src_src_d2.shape: ", gpu_pred_src_src_d2.shape) + + print("gpu_pred_src_src_d.reshape([-1]).shape: ", gpu_pred_src_src_d.reshape([-1]).shape) def get_smooth_noisy_label(label, smoothing=0.2, noise=0.05): @@ -507,7 +509,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... else: return lambda x: np.random.uniform(1-smoothing/2, 1.0) - gpu_pred_src_src_d_ones = tf.map_fn (get_smooth_noisy_label(1, smoothing=0.2, noise=0.05), gpu_pred_src_src_d) + gpu_pred_src_src_d_ones = tf.map_fn (get_smooth_noisy_label(1, smoothing=0.2, noise=0.05), gpu_pred_src_src_d.reshape([-1])).reshape(gpu_pred_src_src_d.shape) gpu_pred_src_src_d_zeros = tf.map_fn (get_smooth_noisy_label(0, smoothing=0.2, noise=0.05), gpu_pred_src_src_d) print("Shape (gpu_pred_src_src_d_ones): ", gpu_pred_src_src_d.shape) From 08495a199e2821e4641cef3d3a71846d383e9c48 Mon Sep 17 00:00:00 2001 From: jh Date: Tue, 16 Mar 2021 17:19:26 -0700 Subject: [PATCH 06/17] test --- models/Model_SAEHD/Model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 4fa6b72..f879310 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -498,7 +498,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... print("gpu_pred_src_src_d.shape: ", gpu_pred_src_src_d.shape) print("gpu_pred_src_src_d2.shape: ", gpu_pred_src_src_d2.shape) - print("gpu_pred_src_src_d.reshape([-1]).shape: ", gpu_pred_src_src_d.reshape([-1]).shape) + print("tf.reshape(gpu_pred_src_src_d, [-1]).shape: ", tf.reshape(gpu_pred_src_src_d, [-1]).shape) def get_smooth_noisy_label(label, smoothing=0.2, noise=0.05): @@ -509,7 +509,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... else: return lambda x: np.random.uniform(1-smoothing/2, 1.0) - gpu_pred_src_src_d_ones = tf.map_fn (get_smooth_noisy_label(1, smoothing=0.2, noise=0.05), gpu_pred_src_src_d.reshape([-1])).reshape(gpu_pred_src_src_d.shape) + gpu_pred_src_src_d_ones = tf.reshape(tf.map_fn (get_smooth_noisy_label(1, smoothing=0.2, noise=0.05), tf.reshape(gpu_pred_src_src_d, [-1])), gpu_pred_src_src_d.shape) gpu_pred_src_src_d_zeros = tf.map_fn (get_smooth_noisy_label(0, smoothing=0.2, noise=0.05), gpu_pred_src_src_d) print("Shape (gpu_pred_src_src_d_ones): ", gpu_pred_src_src_d.shape) From 91301d0516ac43816a386b542d0b58d68999b90e Mon Sep 17 00:00:00 2001 From: jh Date: Tue, 16 Mar 2021 19:16:48 -0700 Subject: [PATCH 07/17] update labels --- models/Model_SAEHD/Model.py | 39 ++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index f879310..991e805 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -498,31 +498,38 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... print("gpu_pred_src_src_d.shape: ", gpu_pred_src_src_d.shape) print("gpu_pred_src_src_d2.shape: ", gpu_pred_src_src_d2.shape) - print("tf.reshape(gpu_pred_src_src_d, [-1]).shape: ", tf.reshape(gpu_pred_src_src_d, [-1]).shape) + # print("tf.reshape(gpu_pred_src_src_d, [-1]).shape: ", tf.reshape(gpu_pred_src_src_d, [-1]).shape) - def get_smooth_noisy_label(label, smoothing=0.2, noise=0.05): - if np.random.random() < noise: - label = 0 if label == 1 else 1 - if label == 0: - return lambda x: np.random.uniform(0, 0+smoothing/2) - else: - return lambda x: np.random.uniform(1-smoothing/2, 1.0) + def get_smooth_noisy_labels(label, tensor, smoothing=0.2, noise=0.05): + labels = [] + num_labels = self.batch_size + for d in tensor.shape[1:]: + num_labels *= d + for _ in num_labels: + if np.random.random() < noise: + label = 0 if label == 1 else 1 + if label == 0: + label = np.random.uniform(0, 0+smoothing/2) + else: + label = np.random.uniform(1-smoothing/2, 1.0) + labels.append(label) + return tf.reshape(labels, tensor.shape) - gpu_pred_src_src_d_ones = tf.reshape(tf.map_fn (get_smooth_noisy_label(1, smoothing=0.2, noise=0.05), tf.reshape(gpu_pred_src_src_d, [-1])), gpu_pred_src_src_d.shape) - gpu_pred_src_src_d_zeros = tf.map_fn (get_smooth_noisy_label(0, smoothing=0.2, noise=0.05), gpu_pred_src_src_d) + gpu_pred_src_src_d_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d, smoothing=0.2, noise=0.05) + gpu_pred_src_src_d_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d, smoothing=0.2, noise=0.05) - print("Shape (gpu_pred_src_src_d_ones): ", gpu_pred_src_src_d.shape) - print("Shape (gpu_pred_src_src_d_zeros): ", gpu_pred_src_src_d2.shape) + print("gpu_pred_src_src_d_ones.shape: ", gpu_pred_src_src_d_ones.shape) + print("gpu_pred_src_src_d_zeros.shape: ", gpu_pred_src_src_d_zeros.shape) - gpu_pred_src_src_d2_ones = tf.map_fn (get_smooth_noisy_label(1, smoothing=0.2, noise=0.05), gpu_pred_src_src_d2) - gpu_pred_src_src_d2_zeros = tf.map_fn (get_smooth_noisy_label(0, smoothing=0.2, noise=0.05), gpu_pred_src_src_d2) + gpu_pred_src_src_d2_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d2, smoothing=0.2, noise=0.05) + gpu_pred_src_src_d2_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d2, smoothing=0.2, noise=0.05) gpu_target_src_d, \ gpu_target_src_d2 = self.D_src(gpu_target_src_masked_opt) - gpu_target_src_d_ones = tf.map_fn (get_smooth_noisy_label(1, smoothing=0.2, noise=0.05), gpu_target_src_d) - gpu_target_src_d2_ones = tf.map_fn (get_smooth_noisy_label(1, smoothing=0.2, noise=0.05), gpu_target_src_d2) + gpu_target_src_d_ones = get_smooth_noisy_labels(1, gpu_target_src_d, smoothing=0.2, noise=0.05) + gpu_target_src_d2_ones = get_smooth_noisy_labels(1, gpu_target_src_d2, smoothing=0.2, noise=0.05) gpu_D_src_dst_loss = (DLoss(gpu_target_src_d_ones , gpu_target_src_d) + \ DLoss(gpu_pred_src_src_d_zeros , gpu_pred_src_src_d) ) * 0.5 + \ From 4223ec79864767b129246be3072b439d1282bc17 Mon Sep 17 00:00:00 2001 From: jh Date: Tue, 16 Mar 2021 19:18:22 -0700 Subject: [PATCH 08/17] fix range --- models/Model_SAEHD/Model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 991e805..553e6e8 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -506,7 +506,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... num_labels = self.batch_size for d in tensor.shape[1:]: num_labels *= d - for _ in num_labels: + for _ in range(num_labels): if np.random.random() < noise: label = 0 if label == 1 else 1 if label == 0: From a89cf63e9f7adc1c380fc473ebaa6d9ced5a6a9c Mon Sep 17 00:00:00 2001 From: jh Date: Tue, 16 Mar 2021 19:20:24 -0700 Subject: [PATCH 09/17] fix reshape --- models/Model_SAEHD/Model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 553e6e8..7cd65a6 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -514,7 +514,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... else: label = np.random.uniform(1-smoothing/2, 1.0) labels.append(label) - return tf.reshape(labels, tensor.shape) + return tf.reshape(labels, (self.batch_size,) + tensor.shape[1:]) gpu_pred_src_src_d_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d, smoothing=0.2, noise=0.05) gpu_pred_src_src_d_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d, smoothing=0.2, noise=0.05) From 3e7fb39cada1fce5a96a7743ebc4f5022959cc22 Mon Sep 17 00:00:00 2001 From: jh Date: Wed, 17 Mar 2021 08:26:00 -0700 Subject: [PATCH 10/17] Vectorize --- models/Model_SAEHD/Model.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 7cd65a6..95445ac 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -502,19 +502,15 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... def get_smooth_noisy_labels(label, tensor, smoothing=0.2, noise=0.05): - labels = [] num_labels = self.batch_size for d in tensor.shape[1:]: num_labels *= d - for _ in range(num_labels): - if np.random.random() < noise: - label = 0 if label == 1 else 1 - if label == 0: - label = np.random.uniform(0, 0+smoothing/2) - else: - label = np.random.uniform(1-smoothing/2, 1.0) - labels.append(label) - return tf.reshape(labels, (self.batch_size,) + tensor.shape[1:]) + probs = tf.math.log([[noise, 1-noise]]) if label == 1 else tf.math.log([[1-noise, noise]]) + x = tf.random.categorical(probs, num_labels) + x = tf.cast(x, tf.float32) + x = x * (1-smoothing) + (smoothing/x.shape[1]) + x = tf.reshape(x, (self.batch_size,) + tensor.shape[1:]) + return x gpu_pred_src_src_d_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d, smoothing=0.2, noise=0.05) gpu_pred_src_src_d_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d, smoothing=0.2, noise=0.05) From 3edcd9a173fc5bda2c62077a4246829551c402bb Mon Sep 17 00:00:00 2001 From: jh Date: Wed, 17 Mar 2021 08:30:39 -0700 Subject: [PATCH 11/17] fix error --- models/Model_SAEHD/Model.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 95445ac..f8dfbe9 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -508,7 +508,8 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... probs = tf.math.log([[noise, 1-noise]]) if label == 1 else tf.math.log([[1-noise, noise]]) x = tf.random.categorical(probs, num_labels) x = tf.cast(x, tf.float32) - x = x * (1-smoothing) + (smoothing/x.shape[1]) + x = x * (1-smoothing) + (smoothing/num_labels) + # x = tf.math.scalar_mul(1-smoothing, x) + (smoothing/x.shape[1]) x = tf.reshape(x, (self.batch_size,) + tensor.shape[1:]) return x From faf2a4a88544f79f5949d1c1c3cade6f81e1dfdd Mon Sep 17 00:00:00 2001 From: jh Date: Wed, 17 Mar 2021 08:34:00 -0700 Subject: [PATCH 12/17] fix error --- models/Model_SAEHD/Model.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index f8dfbe9..3dfef64 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -508,8 +508,9 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... probs = tf.math.log([[noise, 1-noise]]) if label == 1 else tf.math.log([[1-noise, noise]]) x = tf.random.categorical(probs, num_labels) x = tf.cast(x, tf.float32) - x = x * (1-smoothing) + (smoothing/num_labels) - # x = tf.math.scalar_mul(1-smoothing, x) + (smoothing/x.shape[1]) + # x = x * (1-smoothing) + (smoothing/num_labels) + x = tf.math.scalar_mul(1-smoothing, x) + x = x + (smoothing/num_labels) x = tf.reshape(x, (self.batch_size,) + tensor.shape[1:]) return x From 86c7618c071bfd3d0e70ef1c1f1a245f5ed5b75f Mon Sep 17 00:00:00 2001 From: jh Date: Wed, 17 Mar 2021 08:37:33 -0700 Subject: [PATCH 13/17] fix num_labels type --- models/Model_SAEHD/Model.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 3dfef64..ff5584a 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -503,8 +503,9 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... def get_smooth_noisy_labels(label, tensor, smoothing=0.2, noise=0.05): num_labels = self.batch_size - for d in tensor.shape[1:]: + for d in tensor.get_shape().as_list()[1:]: num_labels *= d + # num_labels = int(num_labels) probs = tf.math.log([[noise, 1-noise]]) if label == 1 else tf.math.log([[1-noise, noise]]) x = tf.random.categorical(probs, num_labels) x = tf.cast(x, tf.float32) From 7696a30d84133951614cde87f9abe30e631d12c4 Mon Sep 17 00:00:00 2001 From: jh Date: Wed, 17 Mar 2021 08:44:45 -0700 Subject: [PATCH 14/17] clean up --- models/Model_SAEHD/Model.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index ff5584a..81368b1 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -495,13 +495,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... gpu_pred_src_src_d, \ gpu_pred_src_src_d2 = self.D_src(gpu_pred_src_src_masked_opt) - print("gpu_pred_src_src_d.shape: ", gpu_pred_src_src_d.shape) - print("gpu_pred_src_src_d2.shape: ", gpu_pred_src_src_d2.shape) - - # print("tf.reshape(gpu_pred_src_src_d, [-1]).shape: ", tf.reshape(gpu_pred_src_src_d, [-1]).shape) - - - def get_smooth_noisy_labels(label, tensor, smoothing=0.2, noise=0.05): + def get_smooth_noisy_labels(label, tensor, smoothing=0.1, noise=0.05): num_labels = self.batch_size for d in tensor.get_shape().as_list()[1:]: num_labels *= d @@ -515,20 +509,17 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... x = tf.reshape(x, (self.batch_size,) + tensor.shape[1:]) return x - gpu_pred_src_src_d_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d, smoothing=0.2, noise=0.05) - gpu_pred_src_src_d_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d, smoothing=0.2, noise=0.05) + gpu_pred_src_src_d_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d) + gpu_pred_src_src_d_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d) - print("gpu_pred_src_src_d_ones.shape: ", gpu_pred_src_src_d_ones.shape) - print("gpu_pred_src_src_d_zeros.shape: ", gpu_pred_src_src_d_zeros.shape) - - gpu_pred_src_src_d2_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d2, smoothing=0.2, noise=0.05) - gpu_pred_src_src_d2_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d2, smoothing=0.2, noise=0.05) + gpu_pred_src_src_d2_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d2) + gpu_pred_src_src_d2_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d2) gpu_target_src_d, \ gpu_target_src_d2 = self.D_src(gpu_target_src_masked_opt) - gpu_target_src_d_ones = get_smooth_noisy_labels(1, gpu_target_src_d, smoothing=0.2, noise=0.05) - gpu_target_src_d2_ones = get_smooth_noisy_labels(1, gpu_target_src_d2, smoothing=0.2, noise=0.05) + gpu_target_src_d_ones = get_smooth_noisy_labels(1, gpu_target_src_d) + gpu_target_src_d2_ones = get_smooth_noisy_labels(1, gpu_target_src_d2) gpu_D_src_dst_loss = (DLoss(gpu_target_src_d_ones , gpu_target_src_d) + \ DLoss(gpu_pred_src_src_d_zeros , gpu_pred_src_src_d) ) * 0.5 + \ From d2b44fc2ca9a94deaab9e084c5b1d300b19a7c0a Mon Sep 17 00:00:00 2001 From: jh Date: Wed, 17 Mar 2021 09:06:49 -0700 Subject: [PATCH 15/17] Add GAN options --- models/Model_SAEHD/Model.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 81368b1..28e14f1 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -139,6 +139,8 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... default_gan_power = self.options['gan_power'] = self.load_or_def_option('gan_power', 0.0) default_gan_patch_size = self.options['gan_patch_size'] = self.load_or_def_option('gan_patch_size', self.options['resolution'] // 8) default_gan_dims = self.options['gan_dims'] = self.load_or_def_option('gan_dims', 16) + default_gan_smoothing = self.options['gan_smoothing'] = self.load_or_def_option('gan_smoothing', 0.1) + default_gan_noise = self.options['gan_noise'] = self.load_or_def_option('gan_noise', 0.05) if self.is_first_run() or ask_override: self.options['models_opt_on_gpu'] = io.input_bool ("Place models and optimizer on GPU", default_models_opt_on_gpu, help_message="When you train on one GPU, by default model and optimizer weights are placed on GPU to accelerate the process. You can place they on CPU to free up extra VRAM, thus set bigger dimensions.") @@ -158,6 +160,9 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... gan_dims = np.clip ( io.input_int("GAN dimensions", default_gan_dims, add_info="4-64", help_message="The dimensions of the GAN network. The higher dimensions, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is 16." ), 4, 64 ) self.options['gan_dims'] = gan_dims + self.options['gan_smoothing'] = np.clip ( io.input_number("GAN label smoothing", default_gan_smoothing, add_info="0 - 0.5", help_message="Uses soft labels with values slightly off from 0/1 for GAN, has a regularizing effect"), 0, 0.5) + self.options['gan_noise'] = np.clip ( io.input_number("GAN noisy labels", default_gan_noise, add_info="0 - 0.5", help_message="Marks some images with the wrong label, helps prevent collapse"), 0, 0.5) + if 'df' in self.options['archi']: self.options['true_face_power'] = np.clip ( io.input_number ("'True face' power.", default_true_face_power, add_info="0.0000 .. 1.0", help_message="Experimental option. Discriminates result face to be more like src face. Higher value - stronger discrimination. Typical value is 0.01 . Comparison - https://i.imgur.com/czScS9q.png"), 0.0, 1.0 ) else: @@ -499,27 +504,29 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... num_labels = self.batch_size for d in tensor.get_shape().as_list()[1:]: num_labels *= d - # num_labels = int(num_labels) + probs = tf.math.log([[noise, 1-noise]]) if label == 1 else tf.math.log([[1-noise, noise]]) x = tf.random.categorical(probs, num_labels) x = tf.cast(x, tf.float32) - # x = x * (1-smoothing) + (smoothing/num_labels) x = tf.math.scalar_mul(1-smoothing, x) x = x + (smoothing/num_labels) x = tf.reshape(x, (self.batch_size,) + tensor.shape[1:]) return x - gpu_pred_src_src_d_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d) - gpu_pred_src_src_d_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d) + smoothing = self.options['gan_smoothing'] + noise = self.options['gan_noise'] - gpu_pred_src_src_d2_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d2) - gpu_pred_src_src_d2_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d2) + gpu_pred_src_src_d_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d, smoothing=smoothing, noise=noise) + gpu_pred_src_src_d_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d, smoothing=smoothing, noise=noise) + + gpu_pred_src_src_d2_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d2, smoothing=smoothing, noise=noise) + gpu_pred_src_src_d2_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d2, smoothing=smoothing, noise=noise) gpu_target_src_d, \ gpu_target_src_d2 = self.D_src(gpu_target_src_masked_opt) - gpu_target_src_d_ones = get_smooth_noisy_labels(1, gpu_target_src_d) - gpu_target_src_d2_ones = get_smooth_noisy_labels(1, gpu_target_src_d2) + gpu_target_src_d_ones = get_smooth_noisy_labels(1, gpu_target_src_d, smoothing=smoothing, noise=noise) + gpu_target_src_d2_ones = get_smooth_noisy_labels(1, gpu_target_src_d2, smoothing=smoothing, noise=noise) gpu_D_src_dst_loss = (DLoss(gpu_target_src_d_ones , gpu_target_src_d) + \ DLoss(gpu_pred_src_src_d_zeros , gpu_pred_src_src_d) ) * 0.5 + \ From f3f28079728b56f788f81fee9ecab2bba6df9f39 Mon Sep 17 00:00:00 2001 From: jh Date: Mon, 22 Mar 2021 12:00:44 -0700 Subject: [PATCH 16/17] Fix gan options --- models/Model_SAEHD/Model.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 791e051..f19cbfe 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -161,13 +161,14 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... else: self.options['gan_power'] = np.clip ( io.input_number ("GAN power", default_gan_power, add_info="0.0 .. 1.0", help_message="Forces the neural network to learn small details of the face. Enable it only when the face is trained enough with lr_dropout(on) and random_warp(off), and don't disable. The higher the value, the higher the chances of artifacts. Typical fine value is 0.1"), 0.0, 1.0 ) - if self.options['gan_power'] != 0.0 and self.options['gan_version'] == 3: - gan_patch_size = np.clip ( io.input_int("GAN patch size", default_gan_patch_size, add_info="3-640", help_message="The higher patch size, the higher the quality, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is resolution / 8." ), 3, 640 ) - self.options['gan_patch_size'] = gan_patch_size - - gan_dims = np.clip ( io.input_int("GAN dimensions", default_gan_dims, add_info="4-64", help_message="The dimensions of the GAN network. The higher dimensions, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is 16." ), 4, 64 ) - self.options['gan_dims'] = gan_dims + if self.options['gan_power'] != 0.0: + if self.options['gan_version'] == 3: + gan_patch_size = np.clip ( io.input_int("GAN patch size", default_gan_patch_size, add_info="3-640", help_message="The higher patch size, the higher the quality, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is resolution / 8." ), 3, 640 ) + self.options['gan_patch_size'] = gan_patch_size + gan_dims = np.clip ( io.input_int("GAN dimensions", default_gan_dims, add_info="4-64", help_message="The dimensions of the GAN network. The higher dimensions, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is 16." ), 4, 64 ) + self.options['gan_dims'] = gan_dims + self.options['gan_smoothing'] = np.clip ( io.input_number("GAN label smoothing", default_gan_smoothing, add_info="0 - 0.5", help_message="Uses soft labels with values slightly off from 0/1 for GAN, has a regularizing effect"), 0, 0.5) self.options['gan_noise'] = np.clip ( io.input_number("GAN noisy labels", default_gan_noise, add_info="0 - 0.5", help_message="Marks some images with the wrong label, helps prevent collapse"), 0, 0.5) @@ -545,7 +546,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... num_labels = self.batch_size for d in tensor.get_shape().as_list()[1:]: num_labels *= d - + probs = tf.math.log([[noise, 1-noise]]) if label == 1 else tf.math.log([[1-noise, noise]]) x = tf.random.categorical(probs, num_labels) x = tf.cast(x, tf.float32) From c36eca36105623f16906739e5700bc1b05eae587 Mon Sep 17 00:00:00 2001 From: jh Date: Mon, 22 Mar 2021 15:22:28 -0700 Subject: [PATCH 17/17] feat: use one-sided smoothing (eg: positive/real labels 1.0 -> 0.9), and only apply to discriminator --- models/Model_SAEHD/Model.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index f19cbfe..5522891 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -168,7 +168,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... gan_dims = np.clip ( io.input_int("GAN dimensions", default_gan_dims, add_info="4-64", help_message="The dimensions of the GAN network. The higher dimensions, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is 16." ), 4, 64 ) self.options['gan_dims'] = gan_dims - + self.options['gan_smoothing'] = np.clip ( io.input_number("GAN label smoothing", default_gan_smoothing, add_info="0 - 0.5", help_message="Uses soft labels with values slightly off from 0/1 for GAN, has a regularizing effect"), 0, 0.5) self.options['gan_noise'] = np.clip ( io.input_number("GAN noisy labels", default_gan_noise, add_info="0 - 0.5", help_message="Marks some images with the wrong label, helps prevent collapse"), 0, 0.5) @@ -551,29 +551,28 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... x = tf.random.categorical(probs, num_labels) x = tf.cast(x, tf.float32) x = tf.math.scalar_mul(1-smoothing, x) - x = x + (smoothing/num_labels) + # x = x + (smoothing/num_labels) x = tf.reshape(x, (self.batch_size,) + tensor.shape[1:]) return x smoothing = self.options['gan_smoothing'] noise = self.options['gan_noise'] - gpu_pred_src_src_d_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d, smoothing=smoothing, noise=noise) - gpu_pred_src_src_d_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d, smoothing=smoothing, noise=noise) + gpu_pred_src_src_d_ones = tf.ones_like(gpu_pred_src_src_d) + gpu_pred_src_src_d2_ones = tf.ones_like(gpu_pred_src_src_d2) - gpu_pred_src_src_d2_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d2, smoothing=smoothing, noise=noise) - gpu_pred_src_src_d2_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d2, smoothing=smoothing, noise=noise) + gpu_pred_src_src_d_smooth_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d, smoothing=smoothing, noise=noise) + gpu_pred_src_src_d2_smooth_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d2, smoothing=smoothing, noise=noise) - gpu_target_src_d, \ - gpu_target_src_d2 = self.D_src(gpu_target_src_masked_opt) + gpu_target_src_d, gpu_target_src_d2 = self.D_src(gpu_target_src_masked_opt) - gpu_target_src_d_ones = get_smooth_noisy_labels(1, gpu_target_src_d, smoothing=smoothing, noise=noise) - gpu_target_src_d2_ones = get_smooth_noisy_labels(1, gpu_target_src_d2, smoothing=smoothing, noise=noise) + gpu_target_src_d_smooth_ones = get_smooth_noisy_labels(1, gpu_target_src_d, smoothing=smoothing, noise=noise) + gpu_target_src_d2_smooth_ones = get_smooth_noisy_labels(1, gpu_target_src_d2, smoothing=smoothing, noise=noise) - gpu_D_src_dst_loss = (DLoss(gpu_target_src_d_ones , gpu_target_src_d) + \ - DLoss(gpu_pred_src_src_d_zeros , gpu_pred_src_src_d) ) * 0.5 + \ - (DLoss(gpu_target_src_d2_ones , gpu_target_src_d2) + \ - DLoss(gpu_pred_src_src_d2_zeros , gpu_pred_src_src_d2) ) * 0.5 + gpu_D_src_dst_loss = DLoss(gpu_target_src_d_smooth_ones, gpu_target_src_d) \ + + DLoss(gpu_pred_src_src_d_smooth_zeros, gpu_pred_src_src_d) \ + + DLoss(gpu_target_src_d2_smooth_ones, gpu_target_src_d2) \ + + DLoss(gpu_pred_src_src_d2_smooth_zeros, gpu_pred_src_src_d2) gpu_D_src_dst_loss_gvs += [ nn.gradients (gpu_D_src_dst_loss, self.D_src.get_weights() ) ]#+self.D_src_x2.get_weights()