def imagenet_model_resnet1(N=5, factor=10): def block(in_filters, out_filters, k, downsample): if not downsample: k_first = 3 skip_stride = 1 k_skip = 1 else: k_first = 4 skip_stride = 2 k_skip = 2 return [ Dense( nn.Conv2d(in_filters, out_filters, k_first, stride=skip_stride, padding=1)), nn.ReLU(), Dense( nn.Conv2d(in_filters, out_filters, k_skip, stride=skip_stride, padding=0), None, nn.Conv2d(out_filters, out_filters, k, stride=1, padding=1)), nn.ReLU() ] conv1 = [nn.Conv2d(3, 16 * factor, 7, stride=2, padding=3), nn.ReLU()] conv2 = block(16 * factor, 16 * factor, 3, True) for _ in range(N): conv2.extend(block(16 * factor, 16 * factor, 3, False)) conv3 = block(16 * factor, 32 * factor, 3, True) for _ in range(N - 1): conv3.extend(block(32 * factor, 32 * factor, 3, False)) conv4 = block(32 * factor, 64 * factor, 3, True) for _ in range(N - 1): conv4.extend(block(64 * factor, 64 * factor, 3, False)) layers = (conv1 + conv2 + conv3 + conv4 + [ Flatten(), nn.Linear(64 * factor * 4 * 4, 512), nn.ReLU(), nn.Linear(512, 200) ]) model = DenseSequential(*layers) for m in model.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) if m.bias is not None: m.bias.data.zero_() return model
def averaging_model(models, output_size=10): modules = [] modules.append(Dense(nn.Sequential())) model_layer_counts = [] for i, model in enumerate(models): model_modules = list(model.modules())[1:] model_layer_counts.append(len(model_modules)) modules.append( Dense(*([nn.Sequential()] + [None] * (len(modules) - 1)))) for model_module in model_modules: modules.append(model_module) combine_modules = [] combine_modules.append(get_copy_layer(output_size)) for model_layer_count in model_layer_counts[1:]: combine_modules += [None] * model_layer_count combine_modules.append(get_copy_layer(output_size)) modules.append(Dense(*combine_modules)) modules.append(get_average_layer(output_size, len(models))) avg_model = DenseSequential(*modules) return avg_model
def har_resnet_model(): model = DenseSequential(Dense(nn.Linear(561, 561)), nn.ReLU(), Dense(nn.Sequential(), None, nn.Linear(561, 561)), nn.ReLU(), nn.Linear(561, 6)) return model