def deeper_conv_block(conv_layer, kernel_size, weighted=True): filter_shape = (kernel_size,) * 2 n_filters = conv_layer.filters weight = np.zeros((n_filters, n_filters) + filter_shape) center = tuple(map(lambda x: int((x - 1) / 2), filter_shape)) for i in range(n_filters): filter_weight = np.zeros((n_filters,) + filter_shape) index = (i,) + center filter_weight[index] = 1 weight[i, ...] = filter_weight bias = np.zeros(n_filters) new_conv_layer = StubConv(conv_layer.filters, n_filters, kernel_size=kernel_size) bn = StubBatchNormalization(n_filters) if weighted: new_conv_layer.set_weights((add_noise(weight, np.array([0, 1])), add_noise(bias, np.array([0, 1])))) new_weights = [add_noise(np.ones(n_filters, dtype=np.float32), np.array([0, 1])), add_noise(np.zeros(n_filters, dtype=np.float32), np.array([0, 1])), add_noise(np.zeros(n_filters, dtype=np.float32), np.array([0, 1])), add_noise(np.ones(n_filters, dtype=np.float32), np.array([0, 1]))] bn.set_weights(new_weights) return [StubReLU(), new_conv_layer, bn]
def wider_bn(layer, start_dim, total_dim, n_add, weighted=True): if not weighted: return StubBatchNormalization(layer.num_features + n_add) weights = layer.get_weights() new_weights = [add_noise(np.ones(n_add, dtype=np.float32), np.array([0, 1])), add_noise(np.zeros(n_add, dtype=np.float32), np.array([0, 1])), add_noise(np.zeros(n_add, dtype=np.float32), np.array([0, 1])), add_noise(np.ones(n_add, dtype=np.float32), np.array([0, 1]))] student_w = tuple() for weight, new_weight in zip(weights, new_weights): temp_w = weight.copy() temp_w = np.concatenate((temp_w[:start_dim], new_weight, temp_w[start_dim:total_dim])) student_w += (temp_w,) new_layer = StubBatchNormalization(layer.num_features + n_add) new_layer.set_weights(student_w) return new_layer