From 8bdeaf681b146bfb8102002c9e4293f27548755e Mon Sep 17 00:00:00 2001
From: Sun Jin Kim <sk2521@ic.ac.uk>
Date: Sun, 3 Apr 2022 13:10:40 +0900
Subject: [PATCH] edit aa_learner

---
 MetaAugment/CP2_Max.py                        |   4 +-
 MetaAugment/__pycache__/main.cpython-38.pyc   | Bin 2681 -> 2859 bytes
 .../autoaugment_learners/aa_learner.py        | 179 +++++++++---------
 .../randomsearch_learner.py                   | 178 ++++++++---------
 4 files changed, 186 insertions(+), 175 deletions(-)

diff --git a/MetaAugment/CP2_Max.py b/MetaAugment/CP2_Max.py
index 97a72e50..aacec6a8 100644
--- a/MetaAugment/CP2_Max.py
+++ b/MetaAugment/CP2_Max.py
@@ -37,8 +37,10 @@ class Learner(nn.Module):
         self.relu3 = nn.ReLU()
         self.fc2 = nn.Linear(120, 84)
         self.relu4 = nn.ReLU()
-        self.fc3 = nn.Linear(84, 13)
+        self.fc3 = nn.Linear(84, num_transforms + 21)
         # self.sig = nn.Sigmoid()
+# Currently using discrete outputs for the probabilities 
+
 
     def forward(self, x):
         y = self.conv1(x)
diff --git a/MetaAugment/__pycache__/main.cpython-38.pyc b/MetaAugment/__pycache__/main.cpython-38.pyc
index 1e1bcf0fd748c5095a38d23ff67c3d3eee9818b7..5dcce355ebb82a6ff165d8bf473700ee3f54eae9 100644
GIT binary patch
delta 1128
zcmZ8fON$&;6h8OXqpPc5)vxNuJTgv9Vj~1ZB^nY4WXzz1h|c(+%sABay-vDky1V9f
z&A`Ad8f;feW26?MtE!C~mu_6T5L^i^rB>q3g+IVe$f@dHS#a(-=ljmN=W*-5#*cRS
zgHp)^{C0D@N59p-DStJynZndBh*M=PmS-kfHC|_h6Z({hHUmaZ8>6OU6JSNB#Y#?_
zmxdiyW(Ldg!mtu`V_=nq>a1nB#d80{*Ljt%@pax})w%kZIvdU=x1%dDI9uEtzVI5~
zinj5J8mlj;bCqk+i%4B#4d*(q@h!f=H~Ce5jZxNI=*|sB_Mq4P=^^=>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~*f{vWUK3?JbYUSw8R-6Hm=J;n>cc!=x?ZqzT$@rhs(Qe;^6l-N^T
z3ogUfG|SmDJj*BeC?Dffe3}uKKh^Ck3~!)n@yi<iMLJO<E4wsm=VFw>M|26Sy@wH;
z-lJggSI9G5B(*MHMrU~Yu9uJ~z#+oOB@#z;dwq*|NwAb>x=Nr*v#v2*MvUx{5PNB<
z1bU>GNTz~Vh`PArSyGjjGy@7D@iH<K#QPE@Fe2Z&2BouLIVhIs7fS}I5PfL$%aVTC
z7Z+vo-VmhD4^jtah&;<x>S<|3&vQz=f*cAgplJ!K1L75BCkuj%EJk=Bnnzw%=45ej
za)@%R;2FA&o}hK~97)(4L0(CgBt1~!b!TL0a61hu<4gm6T*jq-puyfEhI?vlx>=@6
z+9lAp5#(ek#L!k=#z3)CmQ)&hWCKYGZxVYX;J%R<fyuwG-@|!7)Q7V=ggxyI7tMB0
zYwy<_?tj&9Xd)5G{^-LMt#N$hob;6T@lj86#OsFJ(`$~y8?0LPtFedvyVz=+9@cjF
z;md~O|A@8LR^VSmRIDnhVk#8p;iEW-O_&V@c;taueL`^o4=a=yz;!|u17-s!i4H2U
z=zpTKiOs6oQ$@4siV6QGeLOw~`Kwrv$SHl-Z)>G`Ra^(=Ka79Ji+<5?7j8n>Q#MwY
zdxldJwIl9w(H6HNYAr#}?FS84G@X+*Sc=&QYust>?u&W<n=ykI{NF}J`D@(qQ;D&E
DUwha@

diff --git a/MetaAugment/autoaugment_learners/aa_learner.py b/MetaAugment/autoaugment_learners/aa_learner.py
index e9c3d1ea..8d3a1d3f 100644
--- a/MetaAugment/autoaugment_learners/aa_learner.py
+++ b/MetaAugment/autoaugment_learners/aa_learner.py
@@ -53,109 +53,114 @@ class aa_learner:
         # TODO: We should probably use a different way to store results than self.history
         self.history = []
 
-    def generate_new_policy(self):
+    def generate_new_discrete_operation(self, fun_num=14, p_bins=10, m_bins=10):
         '''
-        Generate a new random policy in the form of
-            [
-            (("Invert", 0.8, None), ("Contrast", 0.2, 6)),
-            (("Rotate", 0.7, 2), ("Invert", 0.8, None)),
-            (("Sharpness", 0.8, 1), ("Sharpness", 0.9, 3)),
-            (("ShearY", 0.5, 8), ("Invert", 0.7, None)),
-            ]
+        generate a new random operation in the form of a tensor of dimension:
+            (fun_num + 11 + 10)
+
+        The first fun_num dimensions is a 1-hot encoding to specify which function to use.
+        The next 11 dimensions specify which 'probability' to choose.
+            (0.0, 0.1, ..., 1.0)
+        The next 10 dimensions specify which 'magnitude' to choose.
+            (0, 1, ..., 9)
         '''
+        fun = np.random.randint(0, fun_num)
+        prob = np.random.randint(p_bins+1, fun_num)
+        mag = np.random.randint(m_bins, fun_num)
+        
+        fun_t= torch.zeros(fun_num)
+        fun_t[fun] = 1
+        prob_t = torch.zeros(p_bins+1)
+        prob_t[prob] = 1
+        mag_t = torch.zeros(m_bins)
+        mag_t[mag] = 1
 
-        def generate_new_discrete_operation(fun_num=14, p_bins=10, m_bins=10):
-            '''
-            generate a new random operation in the form of a tensor of dimension:
-                (fun_num + 11 + 10)
-
-            The first fun_num dimensions is a 1-hot encoding to specify which function to use.
-            The next 11 dimensions specify which 'probability' to choose.
-                (0.0, 0.1, ..., 1.0)
-            The next 10 dimensions specify which 'magnitude' to choose.
-                (0, 1, ..., 9)
-            '''
-            fun = np.random.randint(0, fun_num)
-            prob = np.random.randint(p_bins+1, fun_num)
-            mag = np.random.randint(m_bins, fun_num)
-            
-            fun_t= torch.zeros(fun_num)
-            fun_t[fun] = 1
-            prob_t = torch.zeros(p_bins+1)
-            prob_t[prob] = 1
-            mag_t = torch.zeros(m_bins)
-            mag_t[mag] = 1
-
-            return torch.cat([fun_t, prob_t, mag_t])
+        return torch.cat([fun_t, prob_t, mag_t])
+
+
+    def generate_new_continuous_operation(self, fun_num=14, p_bins=10, m_bins=10):
+        '''
+        Returns operation_tensor, which is a tensor representation of a random operation with
+        dimension:
+            (fun_num + 1 + 1)
+
+        The first fun_num dimensions is a 1-hot encoding to specify which function to use.
+        The next 1 dimensions specify which 'probability' to choose.
+            0 < x < 1
+        The next 1 dimensions specify which 'magnitude' to choose.
+            0 < x < 9
+        '''
+        fun = np.random.randint(0, fun_num)
+        
+        fun_p_m = torch.zeros(fun_num + 2)
+        fun_p_m[fun] = 1
+        fun_p_m[-2] = np.random.uniform() # 0<prob<1
+        fun_p_m[-1] = np.random.uniform() * (m_bins-1) # 0<mag<9
         
+        return fun_p_m
 
-        def generate_new_continuous_operation(fun_num=14, p_bins=10, m_bins=10):
-            '''
-            Returns operation_tensor, which is a tensor representation of a random operation with
-            dimension:
-                (fun_num + 1 + 1)
-
-            The first fun_num dimensions is a 1-hot encoding to specify which function to use.
-            The next 1 dimensions specify which 'probability' to choose.
-                0 < x < 1
-            The next 1 dimensions specify which 'magnitude' to choose.
-                0 < x < 9
-            '''
-            fun = np.random.randint(0, fun_num)
-            
-            fun_p_m = torch.zeros(fun_num + 2)
-            fun_p_m[fun] = 1
-            fun_p_m[-2] = np.random.uniform() # 0<prob<1
-            fun_p_m[-1] = np.random.uniform() * (m_bins-1) # 0<mag<9
-            
-            return fun_p_m
-
-
-        def translate_operation_tensor(operation_tensor, fun_num=14,
+
+    def translate_operation_tensor(self, operation_tensor, fun_num=14,
                                         p_bins=10, m_bins=10,
                                         discrete_p_m=False):
-            '''
-            takes in a tensor representing a operation and returns an actual operation which
-            is in the form of:
-                ("Invert", 0.8, None)
-                or
-                ("Contrast", 0.2, 6)
-
-            Args:
-                operation_tensor
-                continuous_p_m (boolean): whether the operation_tensor has continuous representations
-                                        of probability and magnitude
-            '''
-            # if input operation_tensor is discrete
-            if discrete_p_m:
-                fun_t = operation_tensor[:fun_num]
-                prob_t = operation_tensor[fun_num:fun_num+p_bins+1]
-                mag_t = operation_tensor[-m_bins:]
-
-                fun = torch.argmax(fun_t)
-                prob = torch.argmax(prob_t)
-                mag = torch.argmax(mag_t)
-                raise NotImplementedError("U can implement this if u want")
-            
-            # process continuous operation_tensor
+        '''
+        takes in a tensor representing a operation and returns an actual operation which
+        is in the form of:
+            ("Invert", 0.8, None)
+            or
+            ("Contrast", 0.2, 6)
+
+        Args:
+            operation_tensor
+            continuous_p_m (boolean): whether the operation_tensor has continuous representations
+                                    of probability and magnitude
+        '''
+        # if input operation_tensor is discrete
+        if discrete_p_m:
             fun_t = operation_tensor[:fun_num]
-            p = operation_tensor[-2].item() # 0 < p < 1
-            m = operation_tensor[-1].item() # 0 < m < 9
+            prob_t = operation_tensor[fun_num:fun_num+p_bins+1]
+            mag_t = operation_tensor[-m_bins:]
+
+            fun = torch.argmax(fun_t)
+            prob = torch.argmax(prob_t) # 0 <= p <= 10
+            mag = torch.argmax(mag_t) # 0 <= m <= 9
+
+            fun = augmentation_space[fun][0]
+            prob = prob/10
 
-            fun_num = torch.argmax(fun_t)
-            function = augmentation_space[fun_num][0]
-            p = round(p, 1) # round to nearest first decimal digit
-            m = round(m) # round to nearest integer
-            return (function, p, m)
+            return (fun, prob, mag)
+
+        
+        # process continuous operation_tensor
+        fun_t = operation_tensor[:fun_num]
+        p = operation_tensor[-2].item() # 0 < p < 1
+        m = operation_tensor[-1].item() # 0 < m < 9
 
+        fun_num = torch.argmax(fun_t)
+        function = augmentation_space[fun_num][0]
+        p = round(p, 1) # round to nearest first decimal digit
+        m = round(m) # round to nearest integer
+        return (function, p, m)
+
+
+    def generate_new_policy(self):
+        '''
+        Generate a new random policy in the form of
+            [
+            (("Invert", 0.8, None), ("Contrast", 0.2, 6)),
+            (("Rotate", 0.7, 2), ("Invert", 0.8, None)),
+            (("Sharpness", 0.8, 1), ("Sharpness", 0.9, 3)),
+            (("ShearY", 0.5, 8), ("Invert", 0.7, None)),
+            ]
+        '''
 
         new_policy = []
         for _ in range(self.sp_num):
             # generate 2 operations for each subpolicy
             ops = []
             for i in range(2):
-                new_op = generate_new_continuous_operation(self.fun_num)
-                new_op = translate_operation_tensor(new_op)
+                new_op = self.generate_new_continuous_operation(self.fun_num)
+                new_op = self.translate_operation_tensor(new_op)
                 ops.append(new_op)
 
             new_subpolicy = tuple(ops)
diff --git a/MetaAugment/autoaugment_learners/randomsearch_learner.py b/MetaAugment/autoaugment_learners/randomsearch_learner.py
index 3ecaa2a7..f5fb54fe 100644
--- a/MetaAugment/autoaugment_learners/randomsearch_learner.py
+++ b/MetaAugment/autoaugment_learners/randomsearch_learner.py
@@ -57,109 +57,113 @@ class randomsearch_learner:
         # TODO: We should probably use a different way to store results than self.history
         self.history = []
 
-    def generate_new_policy(self):
+    def generate_new_discrete_operation(self, fun_num=14, p_bins=10, m_bins=10):
         '''
-        Generate a new random policy in the form of
-            [
-            (("Invert", 0.8, None), ("Contrast", 0.2, 6)),
-            (("Rotate", 0.7, 2), ("Invert", 0.8, None)),
-            (("Sharpness", 0.8, 1), ("Sharpness", 0.9, 3)),
-            (("ShearY", 0.5, 8), ("Invert", 0.7, None)),
-            ]
+        generate a new random operation in the form of a tensor of dimension:
+            (fun_num + 11 + 10)
+
+        The first fun_num dimensions is a 1-hot encoding to specify which function to use.
+        The next 11 dimensions specify which 'probability' to choose.
+            (0.0, 0.1, ..., 1.0)
+        The next 10 dimensions specify which 'magnitude' to choose.
+            (0, 1, ..., 9)
         '''
+        fun = np.random.randint(0, fun_num)
+        prob = np.random.randint(p_bins+1, fun_num)
+        mag = np.random.randint(m_bins, fun_num)
+        
+        fun_t= torch.zeros(fun_num)
+        fun_t[fun] = 1
+        prob_t = torch.zeros(p_bins+1)
+        prob_t[prob] = 1
+        mag_t = torch.zeros(m_bins)
+        mag_t[mag] = 1
 
-        def generate_new_discrete_operation(fun_num=14, p_bins=10, m_bins=10):
-            '''
-            generate a new random operation in the form of a tensor of dimension:
-                (fun_num + 11 + 10)
-
-            The first fun_num dimensions is a 1-hot encoding to specify which function to use.
-            The next 11 dimensions specify which 'probability' to choose.
-                (0.0, 0.1, ..., 1.0)
-            The next 10 dimensions specify which 'magnitude' to choose.
-                (0, 1, ..., 9)
-            '''
-            fun = np.random.randint(0, fun_num)
-            prob = np.random.randint(p_bins+1, fun_num)
-            mag = np.random.randint(m_bins, fun_num)
-            
-            fun_t= torch.zeros(fun_num)
-            fun_t[fun] = 1
-            prob_t = torch.zeros(p_bins+1)
-            prob_t[prob] = 1
-            mag_t = torch.zeros(m_bins)
-            mag_t[mag] = 1
-
-            return torch.cat([fun_t, prob_t, mag_t])
+        return torch.cat([fun_t, prob_t, mag_t])
+
+
+    def generate_new_continuous_operation(self, fun_num=14, p_bins=10, m_bins=10):
+        '''
+        Returns operation_tensor, which is a tensor representation of a random operation with
+        dimension:
+            (fun_num + 1 + 1)
+
+        The first fun_num dimensions is a 1-hot encoding to specify which function to use.
+        The next 1 dimensions specify which 'probability' to choose.
+            0 < x < 1
+        The next 1 dimensions specify which 'magnitude' to choose.
+            0 < x < 9
+        '''
+        fun = np.random.randint(0, fun_num)
         
+        fun_p_m = torch.zeros(fun_num + 2)
+        fun_p_m[fun] = 1
+        fun_p_m[-2] = np.random.uniform() # 0<prob<1
+        fun_p_m[-1] = np.random.uniform() * (m_bins-1) # 0<mag<9
+        
+        return fun_p_m
+
 
-        def generate_new_continuous_operation(fun_num=14, p_bins=10, m_bins=10):
-            '''
-            Returns operation_tensor, which is a tensor representation of a random operation with
-            dimension:
-                (fun_num + 1 + 1)
-
-            The first fun_num dimensions is a 1-hot encoding to specify which function to use.
-            The next 1 dimensions specify which 'probability' to choose.
-                0 < x < 1
-            The next 1 dimensions specify which 'magnitude' to choose.
-                0 < x < 9
-            '''
-            fun = np.random.randint(0, fun_num)
-            
-            fun_p_m = torch.zeros(fun_num + 2)
-            fun_p_m[fun] = 1
-            fun_p_m[-2] = np.random.uniform() # 0<prob<1
-            fun_p_m[-1] = np.random.uniform() * (m_bins-1) # 0<mag<9
-            
-            return fun_p_m
-
-
-        def translate_operation_tensor(operation_tensor, fun_num=14,
+    def translate_operation_tensor(self, operation_tensor, fun_num=14,
                                         p_bins=10, m_bins=10,
                                         discrete_p_m=False):
-            '''
-            takes in a tensor representing a operation and returns an actual operation which
-            is in the form of:
-                ("Invert", 0.8, None)
-                or
-                ("Contrast", 0.2, 6)
-
-            Args:
-                operation_tensor
-                continuous_p_m (boolean): whether the operation_tensor has continuous representations
-                                        of probability and magnitude
-            '''
-            # if input operation_tensor is discrete
-            if discrete_p_m:
-                fun_t = operation_tensor[:fun_num]
-                prob_t = operation_tensor[fun_num:fun_num+p_bins+1]
-                mag_t = operation_tensor[-m_bins:]
-
-                fun = torch.argmax(fun_t)
-                prob = torch.argmax(prob_t)
-                mag = torch.argmax(mag_t)
-                raise NotImplementedError("U can implement this if u want")
-            
-            # process continuous operation_tensor
+        '''
+        takes in a tensor representing a operation and returns an actual operation which
+        is in the form of:
+            ("Invert", 0.8, None)
+            or
+            ("Contrast", 0.2, 6)
+
+        Args:
+            operation_tensor
+            continuous_p_m (boolean): whether the operation_tensor has continuous representations
+                                    of probability and magnitude
+        '''
+        # if input operation_tensor is discrete
+        if discrete_p_m:
             fun_t = operation_tensor[:fun_num]
-            p = operation_tensor[-2].item() # 0 < p < 1
-            m = operation_tensor[-1].item() # 0 < m < 9
+            prob_t = operation_tensor[fun_num:fun_num+p_bins+1]
+            mag_t = operation_tensor[-m_bins:]
+
+            fun = torch.argmax(fun_t)
+            prob = torch.argmax(prob_t) # 0 <= p <= 10
+            mag = torch.argmax(mag_t) # 0 <= m <= 9
 
-            fun_num = torch.argmax(fun_t)
-            function = augmentation_space[fun_num][0]
-            p = round(p, 1) # round to nearest first decimal digit
-            m = round(m) # round to nearest integer
-            return (function, p, m)
+            fun = augmentation_space[fun][0]
+            prob = prob/10
 
+            return (fun, prob, mag)
+
+        
+        # process continuous operation_tensor
+        fun_t = operation_tensor[:fun_num]
+        p = operation_tensor[-2].item() # 0 < p < 1
+        m = operation_tensor[-1].item() # 0 < m < 9
 
+        fun_num = torch.argmax(fun_t)
+        function = augmentation_space[fun_num][0]
+        p = round(p, 1) # round to nearest first decimal digit
+        m = round(m) # round to nearest integer
+        return (function, p, m)
+
+
+    def generate_new_policy(self):
+        '''
+        Generate a new random policy in the form of
+            [
+            (("Invert", 0.8, None), ("Contrast", 0.2, 6)),
+            (("Rotate", 0.7, 2), ("Invert", 0.8, None)),
+            (("Sharpness", 0.8, 1), ("Sharpness", 0.9, 3)),
+            (("ShearY", 0.5, 8), ("Invert", 0.7, None)),
+            ]
+        '''
         new_policy = []
         for _ in range(self.sp_num):
             # generate 2 operations for each subpolicy
             ops = []
             for i in range(2):
-                new_op = generate_new_continuous_operation(self.fun_num)
-                new_op = translate_operation_tensor(new_op)
+                new_op = self.generate_new_continuous_operation(self.fun_num)
+                new_op = self.translate_operation_tensor(new_op)
                 ops.append(new_op)
 
             new_subpolicy = tuple(ops)
-- 
GitLab