예제 #1
0
    def genotype(self):
        PRIMITIVES = get_primitives()

        def get_op(w):
            if 'none' in PRIMITIVES:
                i = max([k for k in range(len(PRIMITIVES)) if k != PRIMITIVES.index('none')], key=lambda k: w[k])
            else:
                i = max(range(len(PRIMITIVES)), key=lambda k: w[k])
            return w[i], PRIMITIVES[i]

        def _parse(weights):
            gene = []
            start = 0
            for i in range(self._steps):
                end = start + i + 2
                W = weights[start:end]
                edges = sorted(range(i + 2), key=lambda x: -get_op(W[x])[0])[:2]
                for j in edges:
                    gene.append((get_op(W[j])[1], j))
                start = end
            return gene

        gene_normal = _parse(tf.nn.softmax(self.alphas_normal, axis=-1).numpy())

        concat = range(2 + self._steps - self._multiplier, self._steps + 2)
        genotype = Genotype(
            normal=gene_normal, normal_concat=concat,
        )
        return genotype
예제 #2
0
파일: gdas.py 프로젝트: sbl1996/tfnas
    def __init__(self,
                 C,
                 layers,
                 steps=4,
                 multiplier=4,
                 stem_multiplier=3,
                 drop_path=0,
                 num_classes=10):
        super().__init__()
        self._C = C
        self._steps = steps
        self._multiplier = multiplier
        self._drop_path = drop_path

        C_curr = stem_multiplier * C
        self.stem = Sequential([
            Conv2d(3, C_curr, 3, bias=False),
            Norm(C_curr, 'def', affine=True),
        ])

        C_prev_prev, C_prev, C_curr = C_curr, C_curr, C
        self.cells = []
        reduction_prev = False
        for i in range(layers):
            if i in [layers // 3, 2 * layers // 3]:
                C_curr *= 2
                reduction = True
            else:
                reduction = False
            cell = Cell(steps, multiplier, C_prev_prev, C_prev, C_curr,
                        reduction, reduction_prev, drop_path)
            reduction_prev = reduction
            self.cells.append(cell)
            C_prev_prev, C_prev = C_prev, multiplier * C_curr

        self.avg_pool = GlobalAvgPool()
        self.classifier = Linear(C_prev, num_classes)

        k = sum(2 + i for i in range(self._steps))
        num_ops = len(get_primitives())
        self.alphas_normal = self.add_weight(
            'alphas_normal', (k, num_ops),
            initializer=RandomNormal(stddev=1e-2),
            trainable=True,
            experimental_autocast=False)
        self.alphas_reduce = self.add_weight(
            'alphas_reduce', (k, num_ops),
            initializer=RandomNormal(stddev=1e-2),
            trainable=True,
            experimental_autocast=False)

        self.tau = self.add_weight('tau', (),
                                   initializer=Constant(1.0),
                                   trainable=False,
                                   experimental_autocast=False)
예제 #3
0
파일: pc_darts.py 프로젝트: sbl1996/tfnas
 def __init__(self, C, stride, k):
     super().__init__()
     self.stride = stride
     self.mp = Pool2d(2, 2, type='max')
     self.k = k
     self._ops = []
     self._channels = C // k
     for i, primitive in enumerate(get_primitives()):
         op = OPS[primitive](self._channels, stride)
         if 'pool' in primitive:
             op = Sequential([op, Norm(self._channels)])
         self._ops.append(op)
예제 #4
0
 def __init__(self, C, stride, drop_path):
     super().__init__()
     self.stride = stride
     self._ops = []
     for i, primitive in enumerate(get_primitives()):
         if drop_path:
             op = Sequential([
                 OPS[primitive](C, stride),
             ])
             if 'pool' in primitive:
                 op.add(Norm(C))
             op.add(DropPath(drop_path))
         else:
             op = OPS[primitive](C, stride)
             if 'pool' in primitive:
                 op = Sequential([op, Norm(C)])
         self._ops.append(op)