예제 #1
0
    def forward(self, x):
        x = quantize_activations_gemm(x)

        x1 = self.conv1(x)
        conv1_weight, conv1_scale = quantize_weight_gemm(self.conv1.weight)
        # conv1_weight = quantize_weights_bias_gemm(self.conv1.weight)
        # conv1_scale = 1
        conv1_bias = quantize_bias_gemm(self.conv1.bias) / conv1_scale
        x = F.conv2d(x, conv1_weight, conv1_bias, stride=1,
                     padding=1) * conv1_scale
        x = self.relu(x)
        x = quantize_activations_gemm(x)
        # x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = F.avg_pool2d(x, 4)
        x = x.view(x.size(0), -1)
        x = self.linear(x)
        # x = quantize_activations_gemm(x)
        # x = self.scalar(x)  # 修改

        return x
예제 #2
0
    def forward(self, x):
        x = quantize_activations_gemm(x)
        residual = x

        out1 = self.conv1(x)
        conv1_weight, conv1_scale = quantize_weight_gemm(self.conv1.weight)
        # conv1_weight = quantize_weights_bias_gemm(self.conv1.weight)
        # conv1_scale = 1
        conv1_bias = quantize_bias_gemm(self.conv1.bias) / conv1_scale
        out = F.conv2d(x, conv1_weight, conv1_bias) * conv1_scale
        out = self.relu(out)
        out = quantize_activations_gemm(out)

        out1 = self.conv2(out)
        conv2_weight, conv2_scale = quantize_weight_gemm(self.conv2.weight)
        # conv2_weight = quantize_weights_bias_gemm(self.conv2.weight)
        # conv2_scale = 1
        conv2_bias = quantize_bias_gemm(self.conv2.bias) / conv2_scale
        out = F.conv2d(
            out, conv2_weight, conv2_bias, stride=self.stride,
            padding=1) * conv2_scale
        out = self.relu(out)
        out = quantize_activations_gemm(out)

        out1 = self.conv3(out)
        conv3_weight, conv3_scale = quantize_weight_gemm(self.conv3.weight)
        # conv3_weight = quantize_weights_bias_gemm(self.conv3.weight)
        # conv3_scale = 1
        conv3_bias = quantize_bias_gemm(self.conv3.bias) / conv3_scale
        out = F.conv2d(out, conv3_weight, conv3_bias, padding=1) * conv3_scale
        out = quantize_activations_gemm(out)

        out1 += self.shortcut(residual)
        if self.downsample:
            short_weight, short_scale = quantize_weight_gemm(
                self.shortcut[0].weight)
            short_bias = quantize_bias_gemm(
                self.shortcut[0].bias) / short_scale
            residual = F.conv2d(
                residual, short_weight, short_bias,
                stride=self.stride) * short_scale

        out += residual
        out = self.relu(out)

        return out
예제 #3
0
    def forward(self, x):
        out1 = self.conv(x)
        conv_weight, conv_scale = quantize_weight_gemm(self.conv.weight)
        conv_bias = quantize_bias_gemm(self.conv.bias / conv_scale)
        out = F.conv2d(x, conv_weight, conv_bias, stride=1,
                       padding=1) * conv_scale
        # s = nn.Parameter(torch.tensor(conv_scale))
        out = self.relu(out)
        out = quantize_activations_gemm(out)

        return out
예제 #4
0
# params = params.reshape((-1, 6))
l_vgg = [
    '.0.', '.2.', '.5.', '.7.', '.10.', '.12.', '.14.', '.17.', '.19.', '.21.',
    '.24.', '.26.', '.28.'
]
scale = []
for index in range(params.shape[0]):
    weight = state_dict[params[index][0]]
    gamma = state_dict[params[index][1]]
    beta = state_dict[params[index][2]]
    running_mean = state_dict[params[index][3]]
    running_var = state_dict[params[index][4]]
    delta = gamma / (torch.sqrt(running_var + epsilon))
    weight = weight * delta.view(-1, 1, 1, 1)
    bias = (0 - running_mean) * delta + beta
    qweight, s = quantize_weight_gemm(weight)
    qbias = quantize_bias_gemm(bias)
    scale.append(s)
    merge_state_dict.update({
        params[index][0]: qweight,
        params[index][0][:-6] + "bias": qbias
    })
#    merge_state_dict.update({params[index][0][0:15] + l_vgg[index] + "weight": weight,
#                             params[index][0][0:15] + l_vgg[index] + "bias":  bias})

merge_model.load_state_dict(merge_state_dict)

print(scale)
summary_writer = SummaryWriter(save_dir)
x = state_dict["module.conv1.weight"]
y = merge_state_dict["module.conv1.weight"]