Jia Suan 01 Determinator Based on Convolutional Neural Network

Jia Dinner 01 classification model (based on convolutional neural network)

Abstract: This paper creatively applies convolutional neural network to the 01 problem of Jia Wanwan, hoping that the model trained by machine learning can provide us with an answer. In the experiment, a simple convolutional neural network was built, and the cross-entropy loss function was selected and the parameters were optimized with Adam, SGD, AdamGrad and other optimizers. The correct rate reached more than 80% on the self-built 01 data set. Put the pictures of Jiaran and Xiangwan into the model respectively, and output the probabilities of both being 1.
Keywords: sudden attack, late attack, convolutional neural network

Foreword

In recent years, as the fact that “Jia Supper is real” has become popular among the people, the team of Tangciwan is also growing, and the number of high-quality second creations is also increasing. However, it is worth noting that everyone has been arguing about the issue of “who is 1 and who is 0”, and even quarreled and fought. It can be said that the 01 problem of Jia Dinner has become a century-old problem that needs to be solved urgently. In order to enrich the theoretical system of Jia Dinner 01, the author tries to use the convolutional neural network to build a lesbian 01 judger to explore the truth of the strongest natural attack.

One. 01Data set construction

The training of neural networks requires the support of suitable data sets. For 01 judgment, appearance is an important indicator. In the field of animation or manga, there are many Lily works with clear 01, such as I will become you, Morning Glory and Kase-san and so on. Due to the limited image range and difficulty in collection, we only collected 100 such pictures, manually labeled them as 0 and 1, and built a simple data set. Part of it is shown below:

The images are all RGB three-channel images. Due to the different sizes of the images, a strategy of random cropping is adopted when reading in to ensure that they are all 256*256 pixels when reading in. 70% of the images are used as the training set and 0 as the test set.
In addition, 40 pictures of Jiaran and Xiangwan were collected respectively, which were used to judge the 01 content of Jiaran and Xiangwan after the model training was completed. Part of the schematic diagram is as follows:

two. Construction of Convolutional Neural Network

The experiment only involves a simple binary classification problem, so the convolutional neural network built is relatively simple. Set the convolution kernel to 33, the step size to 1, and the border padding value to 1 to ensure that the image features formed after the first convolution are still 256256, which is convenient for calculation. A pooling layer is added after two convolutions. The pooling layer adopts the method of maximum pooling, sets the convolution kernel to 22, and the step size is also 22, so that the original feature length and width are exactly half of the original, that is, 128*128. Finally A fully connected layer is added, and the larger-dimensional features after convolution are output through softmax as 2 features corresponding to the 2 classes we want to separate. And set dropout to 0.3 to make the model have better generalization and prevent overfitting.

class Net(nn.Module):#Convolutional neural network
    def __init__(self): #initialization
        super(Net, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(stride=2, kernel_size=2),
            nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(stride=2, kernel_size=2))
        self.dense = nn.Sequential(
            nn.Linear(64 * 64 * 64, 1024),
            nn.ReLU(),
            nn. Dropout(p=0.5),
            nn. Linear(1024, 2))
    def forward(self, x):
        x = self.conv(x)
        x = x. view(-1, 64 * 64 * 64)
        x = self. dense(x)
        return x

Three. Training results and analysis

On the training set, the cross-entropy loss function is used to calculate the loss value, and the Adam algorithm is used to optimize the loss. As shown in the figure, the loss changes with batch_idx. It can be seen that the loss value of the model is basically stable and close to 0 after several epochs , the convergence speed is faster. The model was validated on the test set with a final accuracy of 80%.
We input 40 pictures of Jia Ran and Xiang Wan respectively, let the model judge whether each picture is 1 or 0, and calculate the proportion of the number of pictures classified as 1 in the pictures to the total, as Jia Ran and Xiang Wan are 1 The probability. In the experiment, multiple tests were carried out, and the average value was taken. Finally, the probability of Xiangwan being 1 was 76.92%, and the probability of Jiaran being 1 was 36.84%:

Draw a pie chart visualization:


It can be seen that the probability of late being 1 is higher than that of Ranran.

Four. Summary

In this experiment, we built a 01 judgment model, and analyzed the 01 problem of Jiawanwan from the perspective of images. But it is worth noting that the construction of this model is entirely based on appearance, which will be significantly affected by factors such as hair color and face shape. The higher probability of outputting Xiangwan as 1 only means that in a certain statistical sense, Xiangwan has more appearance characteristics of 1 in the Lily relationship, and it does not mean that Xiangwan is 1 in Jia Suan. “I 1 you 0” is the unchanging truth in Jia Dinner’s relationship mode that is not affected by factors such as appearance and time.
Combined with our in-depth understanding of Jia Suan, it is obviously impossible for Xiang Wan to be 1 in this relationship, so we can say that the construction of the model this time was a failure, lack of consideration, irrational, and ridiculous. More factors can be considered when building a model, such as language habits, getting along with habits, etc. These need to collect more data and perform reasonable data processing. In the experiment, due to the lack of existing 01 data, it is difficult to organize, resulting in a small data set. The number of data sets can be increased by cutting, rotating, adding noise and other data enhancement methods.

Appendix

The complete code is as follows:

##
import torch
from torch import nn
from torch.nn import functional as F
from torch import optim
import torchvision
from matplotlib import pyplot as plt
from torch.autograd import Variable
from torchvision import datasets, transforms
import os

plt.rcParams['font.sans-serif'] = ['SimHei'] #Solve Chinese display problem
plt.rcParams['axes.unicode_minus'] = False # Solve the Chinese display problem
def plot_curve(data):
    fig = plt. figure()
    plt.plot(range(len(data)), data, color='red')
    plt.legend(['value'], loc='upper right')
    plt.xlabel('step')
    plt.ylabel('value')
    plt. show()

def Test_acc(test):
    total_correct = 0
    for x, y in test:
        x = x.to(device)
        y = y.to(device)
        out = net(x)
        pred = out.argmax(dim=1)
        correct = pred.eq(y).sum().float().item()
        total_correct += correct
    total_num = len(test.dataset)
    acc = total_correct / total_num
    return acc



class Net(nn.Module):#Convolutional neural network
    def __init__(self): #initialization
        super(Net, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(stride=2, kernel_size=2),
            nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(stride=2, kernel_size=2))
        self.dense = nn.Sequential(
            nn.Linear(64 * 64 * 64, 1024),
            nn.ReLU(),
            nn. Dropout(p=0.5),
            nn. Linear(1024, 2))
    def forward(self, x):
        x = self.conv(x)
        x = x. view(-1, 64 * 64 * 64)
        x = self. dense(x)
        return x
##
batch_size = 10

#Download training set and test set
path = r'D:\minist\dataset'
path0 = r'D:\minist\dataset\AvaDiana'

def dataLoad(train,test,path):
    transform = {<!-- -->x: transforms.Compose([transforms.ToTensor(),
                                        transforms.RandomResizedCrop(256)])
                 for x in [train, test]}
    dataset = {<!-- -->x: datasets.ImageFolder(root=os.path.join(path, x),
                                       transform=transform[x])
               for x in [train, test]}
    dataloader = {<!-- -->x: torch.utils.data.DataLoader(dataset=dataset[x],
                                                 batch_size=batch_size,
                                                 shuffle=True)
                  for x in [train, test]}
    train_loader, test_loader = dataloader[train], dataloader[test]
    return train_loader, test_loader

train_loader, test_loader = dataLoad("train","test",path)
Ava,Diana = dataLoad("Ava","Diana",path0)

##
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net = Net().to(device)

# Adam, Adagrad
optimizer = optim.Adam(net.parameters())
# optimizer = optim.Adagrad(net.parameters())
train_loss = []

for epoch in range(5):
    for batch_idx, (x, y) in enumerate(train_loader):
        x = x.to(device)
        out = net(x)
        y = y.to(device)
        loss = F.cross_entropy(out,y)
        optimizer. zero_grad()
        loss. backward()
        optimizer. step()
        train_loss.append(loss.item())
        print(epoch, batch_idx, loss. item())

plot_curve(train_loss)
total_correct = 0
for x,y in test_loader:
    x = x.to(device)
    out = net(x)
    pred = out.argmax(dim=1)
    correct = pred.eq(y).sum().float().item()
    total_correct += correct

total_num = len(test_loader.dataset)
acc = total_correct / total_num
print('test acc:', acc) # output the correct rate on the test set

## Ava & Diana Judgment
A = Test_acc(Ava)
D = Test_acc(Diana)
print("Ava is the probability of 1:",A*100,"%")
print("Diana is the probability of 1:",D*100,"%")
if A>D:
    print("Late attack on toilet products, your taste is here! Identified as wrong model.")
else:
    print("Xianpin Rangong, you have figured out Jia Suan! Identified as a model with taste.")
##
labels = '1', '0'
sizesA = [A, 1-A]
sizesD = [D, 1-D]
colors = ['yellowgreen', 'gold']
explode = (0.1, 0)
# plt.pie(sizesA, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90)
plt.pie(sizesD, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90)
plt.axis('equal')
plt.title("Of course 01 distribution map")
plt. show()