def __init__(self, nfm, first=False, strides=1, batch_norm=False): self.trunk = None self.side_path = None main_path = [ Convolution( **conv_params(1, nfm, strides=strides, batch_norm=batch_norm)), Convolution(**conv_params(3, nfm, batch_norm=batch_norm)), Convolution(**conv_params(1, nfm * 4, relu=False, batch_norm=False)) ] if first or strides == 2: self.side_path = Convolution(**conv_params( 1, nfm * 4, strides=strides, relu=False, batch_norm=False)) else: if batch_norm: main_path = [BatchNorm(), Activation(Rectlin())] + main_path else: main_path = [Activation(Rectlin())] + main_path if strides == 2: if batch_norm: self.trunk = Sequential([BatchNorm(), Activation(Rectlin())]) else: self.trunk = Sequential([Activation(Rectlin())]) self.main_path = Sequential(main_path)
def __init__(self, inputs, dataset, stage_depth, batch_norm=False, activation=False, preprocess=False): nfms = [ 2**(stage + 4) for stage in sorted(list(range(3)) * stage_depth) ] strides = [ 1 if cur == prev else 2 for cur, prev in zip(nfms[1:], nfms[:-1]) ] layers = [] if preprocess and dataset == 'cifar10': layers = Preprocess(functor=cifar_mean_subtract) layers.append(Convolution(**conv_params(3, 16, batch_norm=batch_norm))) layers.append(f_module(nfms[0], first=True, batch_norm=batch_norm)) for nfm, stride in zip(nfms[1:], strides): layers.append(f_module(nfm, strides=stride, batch_norm=batch_norm)) if batch_norm: layers.append(BatchNorm()) if activation: layers.append(Activation(Rectlin())) layers.append(Pool2D(8, strides=2, op='avg')) if dataset == 'cifar10': ax.Y.length = 10 layers.append( Affine(axes=ax.Y, weight_init=KaimingInit(), batch_norm=batch_norm, activation=Softmax())) elif dataset == 'i1k': ax.Y.length = 1000 layers.append( Affine(axes=ax.Y, weight_init=KaimingInit(), batch_norm=batch_norm, activation=Softmax())) else: raise ValueError("Incorrect dataset provided") super(mini_residual_network, self).__init__(layers=layers)