MCPcopy Create free account
hub / github.com/Sirwenhao/Deep-Learning-Notes / ShuffleNetV2

Class ShuffleNetV2

CV/Pytorch_classification/ShuffleNet/model.py:74–130  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

72 return out
73
74class ShuffleNetV2(nn.Module):
75 def __init__(self, stages_repeats, stages_out_channels, num_classes=1000, inverted_residual=InvertedResidual):
76 super(ShuffleNetV2, self).__init__()
77
78 if len(stages_repeats) != 3:
79 raise ValueError("expected stages_repeats as list of 3 positive ints")
80 if len(stages_out_channels) != 5:
81 raise ValueError("expected stages_out_channels as list of 5 positive ints")
82 self._stage_out_channels = stages_out_channels
83
84 # input RGB image
85 input_channels = 3
86 output_channels = self._stage_out_channels[0]
87
88 self.conv1 = nn.Sequential(
89 nn.Conv2d(input_channels, output_channels, kernel_size=3, stride=2, padding=1, bias=False),
90 nn.BatchNorm2d(output_channels),
91 nn.ReLU(inplace=True) # inplace=True表示直接进行覆盖
92 )
93 input_channels = output_channels
94
95 self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
96
97 self.stage2: nn.Sequential
98 self.stage3: nn.Sequential
99 self.stage4: nn.Sequential
100
101 stage_names = ["stage{}".format(i) for i in [2, 3, 4]]
102 for name, repeats, output_channels in zip(stage_names, stages_repeats, self._stage_out_channels[1:]):
103 seq = [inverted_residual(input_channels, output_channels, 2)]
104 for i in range(repeats - 1):
105 seq.append(inverted_residual(output_channels, output_channels, 1))
106 setattr(self, name, nn.Sequential(*seq))
107 input_channels = output_channels
108
109 output_channels = self._stage_out_channels[-1]
110 self.conv5 = nn.Sequential(
111 nn.Conv2d(input_channels, output_channels, kernel_size=1, stride=2, padding=0, bias=False),
112 nn.BatchNorm2d(output_channels),
113 nn.ReLU(inplace=True)
114 )
115
116 self.fc = nn.Linear(output_channels, num_classes)
117
118 def _forward_impl(self, x):
119 x = self.conv1(x)
120 x = self.maxpool(x)
121 x = self.stage2(x)
122 x = self.stage3(x)
123 x = self.stage4(x)
124 x = self.conv5(x)
125 x = x.mean([2, 3])
126 x = self.fc(x)
127 return x
128
129 def forward(self, x):
130 return self._forward_impl(x)
131

Callers 4

shufflenet_v2_x0_5Function · 0.85
shufflenet_v2_x1_0Function · 0.85
shufflenet_v2_x1_5Function · 0.85
shufflenet_v2_x2_0Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected