Pytorch实现男女证件照性别分类

今儿个用pytorch写了一个识别证件照性别的神经网络,一开始用sgd,死活收敛不到一半,还不如蒙呢,蒙还有50%的准确率,后来用adam,一下子就收敛到接近100%了,可以商用了。我发现别放男明星的,不太准啊,这个明星娘化看来被人工智能发现了。。。
来,先上图,看看成果。
Pytorch实现男女证件照性别分类_第1张图片
Pytorch实现男女证件照性别分类_第2张图片
该程序用了ImageLoader加载数据,省去了自己定义数据模型的功夫。
本地图片应该如下图一样放置
Pytorch实现男女证件照性别分类_第3张图片
Pytorch实现男女证件照性别分类_第4张图片
Pytorch实现男女证件照性别分类_第5张图片
test文件夹一样的结构,就不上图了。
训练代码我贴一下把,完整代码可以加我微信好友获取。我的微信是A1354164181。


```python
if __name__ == '__main__':
    # transform = transforms.Compose(
    #     [transforms.ToTensor(),
    #      transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
    #
    # trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
    # trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)
    #
    # testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
    # testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)
    data_transform = transforms.Compose([
        transforms.Resize(22),
        transforms.CenterCrop(20),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
    hymenoptera_dataset = datasets.ImageFolder(root='MaleAndFemale',
                                               transform=data_transform)
    test_dataset = datasets.ImageFolder(root='Test',
                                               transform=data_transform)
    trainloader = torch.utils.data.DataLoader(hymenoptera_dataset,
                                                 batch_size=4, shuffle=False, num_workers=2)
    testloader = torch.utils.data.DataLoader(test_dataset,
                                                 batch_size=4, shuffle=False, num_workers=2)
    classes = ('男人', '女人')
    net = Net()

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(net.parameters(), lr=0.001)
    for epoch in range(20):  # loop over the dataset multiple times

        running_loss = 0.0
        for i, data in enumerate(trainloader, 0):
            # get the inputs
            inputs, labels = data
            # zero the parameter gradients
            optimizer.zero_grad()

            # forward + backward + optimize
            outputs = net(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            # print statistics
            running_loss += loss.item()
            # if i % 4 == 19:  # print every 2000 mini-batches
            #     print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 20))
            #     running_loss = 0.0
            print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, loss.item()))
    print('Finished Training')
    PATH = './cifar_net.pth'
    # torch.save(net.state_dict(), PATH)
    dataiter = iter(testloader)
    images, labels = dataiter.next()
    print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
    # 输出图片
    imshow(torchvision.utils.make_grid(images))
    net.load_state_dict(torch.load(PATH))
    outputs = net(images)
    print(outputs)
    _, predicted = torch.max(outputs, 1)
    print('predicted',predicted)
    print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4)))

你可能感兴趣的:(pytorch,python,pytorch,神经网络,机器学习,深度学习)