We often use lightweight network models for development at work, so we often pay attention to their use and records. There are a lot of related practical work in the previous blog posts. If you are interested, you can read it by yourself.
First look at the example effect:
This article uses the GhostNet model. GhostNet is a lightweight convolutional neural network specially designed for applications on mobile devices. Its main building block is the Ghost module, a novel plug-and-play module. The original intention of the Ghost module is to generate more features by using fewer parameters.
The official paper address is here, as shown below:
The official has also open sourced the project, the address ishere, as shown below:
You can read the official code examples in detail, and then you can develop and build models based on your own data sets.
Here is the core implementation part of GhostNet, as shown below:
class GhostNet(nn.Module):
def __init__(self, cfgs, num_classes=1000, width_mult=1.0):
super(GhostNet, self).__init__()
self.cfgs = cfgs
output_channel = _make_divisible(16 * width_mult, 4)
layers = [
nn.Sequential(
nn.Conv2d(3, output_channel, 3, 2, 1, bias=False),
nn.BatchNorm2d(output_channel),
nn.ReLU(inplace=True),
)
]
input_channel = output_channel
block = GhostBottleneck
for k, exp_size, c, use_se, s in self.cfgs:
output_channel = _make_divisible(c * width_mult, 4)
hidden_channel = _make_divisible(exp_size * width_mult, 4)
layers.append(
block(input_channel, hidden_channel, output_channel, k, s, use_se)
)
input_channel = output_channel
self.features = nn.Sequential(*layers)
output_channel = _make_divisible(exp_size * width_mult, 4)
self.squeeze = nn.Sequential(
nn.Conv2d(input_channel, output_channel, 1, 1, 0, bias=False),
nn.BatchNorm2d(output_channel),
nn.ReLU(inplace=True),
nn.AdaptiveAvgPool2d((1, 1)),
)
input_channel = output_channel
output_channel = 1280
self.classifier = nn.Sequential(
nn.Linear(input_channel, output_channel, bias=False),
nn.BatchNorm1d(output_channel),
nn.ReLU(inplace=True),
nn.Dropout(0.2),
nn.Linear(output_channel, num_classes),
)
self._initialize_weights()
def forward(self, x, need_fea=False):
if need_fea:
features, features_fc = self.forward_features(x, need_fea)
x = self.classifier(features_fc)
return features, features_fc, x
else:
x = self.forward_features(x)
x = self.classifier(x)
return x
def forward_features(self, x, need_fea=False):
if need_fea:
input_size = x.size(2)
scale = [4, 8, 16, 32]
features = [None, None, None, None]
for idx, layer in enumerate(self.features):
x = layer(x)
if input_size // x.size(2) in scale:
features[scale.index(input_size // x.size(2))] = x
x = self.squeeze(x)
return features, x.view(x.size(0), -1)
else:
x = self.features(x)
x = self.squeeze(x)
return x.view(x.size(0), -1)
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def cam_layer(self):
return self.features[-1]
Let’s take a brief look at the data set:
The official description states that the data set is extracted from original high-resolution photovoltaic images. The data set contains 2624 8-bit grayscale image samples of 300x300 pixels. These images were extracted from 44 different solar modules with different degradations. degree of functionality and defective solar cells. Defects in the annotation image are either intrinsic or extrinsic and are known to reduce the power efficiency of solar modules. All images are normalized for size and viewing angle. Additionally, any distortion caused by the camera lens used to capture the EL images is eliminated before the solar cells are extracted. Each image is annotated with a defect probability (a floating point value between 0 and 1) and the solar module type (monocrystalline or polycrystalline) from which the solar cell image was originally extracted.
The data distribution visualization looks like this:
After the training is completed, look at the result details:
[Confusion Matrix]
【loss curve】
【acc curve】
Batch instance visualization is as follows:
An example of visual reasoning is as follows:
If you are interested, you can try it yourself!