图像分类CIFAR-10【可直接运行的代码】

图像分类CIFAR-10学习

# -*- coding: utf-8 -*-
"""CIFAR10 官网案例.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1-0eKHTgI1VMPU4CfDQGwqq1HSG4KKj1j
"""

import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dense

import matplotlib.pyplot as plt
from math import sqrt

# 1 加载数据集

(x_train, y_train), (x_test, y_test) = cifar10.load_data()

x_train.shape

y_train.shape

x_test.shape

y_test.shape

# 类别名称

labels = ['airplane', 'automobile', 'bird', 'cat', 'deer',
               'dog', 'frog', 'horse', 'ship', 'truck']

# 2 显示部分数据集(36张图片)

def show_images(dataset, size=0):
    plt.figure(figsize=(10,8)) # 画布大小
    plt.suptitle("Image Samples")
    for i in range(size):
        plt.subplot(int(sqrt(size)), int(sqrt(size)), i+1) # 图片显示位置
        plt.imshow(dataset[i]) # 显示图片
        plt.xticks([]) # 关闭横纵坐标轴值显示
        plt.yticks([])
        plt.xlabel(labels[y_train[i][0]]) # 横轴标签
    plt.show()

show_images(x_train, size=25)

# 3 数据集归一化

x_train = x_train / 255.0

x_test = x_test / 255.0

# 4 搭建模型

model = Sequential() # 创建序列模型
model.add(Conv2D(32, (3,3), activation='relu', input_shape=(32, 32, 3))) # 设置32个神经元,卷积核大小3x3,input_shape
#32个神经元 卷积核的大小(3,3) 激活函数
model.add(MaxPool2D((2,2))) # 池化层
#32/2 将featureMap缩小
model.add(Conv2D(64, (3,3), activation='relu'))
model.add(MaxPool2D((2,2))) # 池化层
model.add(Conv2D(64, (3,3), activation='relu'))
model.add(Flatten()) # 3D变成1D
model.add(Dense(128, activation='relu'))
#全连接层
model.add(Dense(10))
#分类有10个类型

# 5 模型编译

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
#优化器"adam""std",损失函数,评估函数
#Sparse会把标签集转换成向量
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"

"""categorical_crossentropy 与 SparseCategoricalCrossentropy 区别:

两者都是多分类交叉熵损失函数,区别在于sparse(稀疏),在于对target编码的要求。  
1.categorical_crossentropy要求target为onehot编码。  
2.sparse_categorical_crossentropy要求target为非onehot编码,函数内部进行onehot编码实现。
"""

# 6 模型训练

history = model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))

# 7 显示训练accuracy和验证accuracy

plt.figure(figsize=(10,8))
plt.plot(history.history['accuracy'], label='Train acc')
plt.plot(history.history['val_accuracy'], label='Val acc')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.title('Train and Val Acc')
plt.show()

# 8 模型验证

result = model.evaluate(x_test, y_test)

print("test loss : ", result[0])

print("test acc : ", result[1])
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用GoogleNet进行CIFAR-10图像分类代码(使用PyTorch实现): ```python import torch import torch.nn as nn import torch.optim as optim import torchvision import torchvision.transforms as transforms # 定义GoogleNet模型 class GoogLeNet(nn.Module): def __init__(self): super(GoogLeNet, self).__init__() self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1) self.conv2 = nn.Conv2d(64, 192, kernel_size=3, stride=1, padding=1) self.inception3a = Inception(192, 64, 96, 128, 16, 32, 32) self.inception3b = Inception(256, 128, 128, 192, 32, 96, 64) self.inception4a = Inception(480, 192, 96, 208, 16, 48, 64) self.inception4b = Inception(512, 160, 112, 224, 24, 64, 64) self.inception4c = Inception(512, 128, 128, 256, 24, 64, 64) self.inception4d = Inception(512, 112, 144, 288, 32, 64, 64) self.inception4e = Inception(528, 256, 160, 320, 32, 128, 128) self.inception5a = Inception(832, 256, 160, 320, 32, 128, 128) self.inception5b = Inception(832, 384, 192, 384, 48, 128, 128) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.dropout = nn.Dropout(p=0.4) self.fc1 = nn.Linear(1024, 10) def forward(self, x): x = nn.functional.relu(self.conv1(x)) x = nn.functional.max_pool2d(x, kernel_size=3, stride=2, padding=1) x = nn.functional.relu(self.conv2(x)) x = nn.functional.max_pool2d(x, kernel_size=3, stride=2, padding=1) x = self.inception3a(x) x = self.inception3b(x) x = nn.functional.max_pool2d(x, kernel_size=3, stride=2, padding=1) x = self.inception4a(x) x = self.inception4b(x) x = self.inception4c(x) x = self.inception4d(x) x = self.inception4e(x) x = nn.functional.max_pool2d(x, kernel_size=3, stride=2, padding=1) x = self.inception5a(x) x = self.inception5b(x) x = self.avgpool(x) x = torch.flatten(x, 1) x = self.dropout(x) x = self.fc1(x) return x # 定义Inception模块 class Inception(nn.Module): def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, pool_proj): super(Inception, self).__init__() # 1x1卷积层 self.branch1 = nn.Sequential( nn.Conv2d(in_channels, ch1x1, kernel_size=1), nn.BatchNorm2d(ch1x1), nn.ReLU(inplace=True) ) # 1x1卷积层 + 3x3卷积层 self.branch2 = nn.Sequential( nn.Conv2d(in_channels, ch3x3red, kernel_size=1), nn.BatchNorm2d(ch3x3red), nn.ReLU(inplace=True), nn.Conv2d(ch3x3red, ch3x3, kernel_size=3, padding=1), nn.BatchNorm2d(ch3x3), nn.ReLU(inplace=True) ) # 1x1卷积层 + 5x5卷积层 self.branch3 = nn.Sequential( nn.Conv2d(in_channels, ch5x5red, kernel_size=1), nn.BatchNorm2d(ch5x5red), nn.ReLU(inplace=True), nn.Conv2d(ch5x5red, ch5x5, kernel_size=5, padding=2), nn.BatchNorm2d(ch5x5), nn.ReLU(inplace=True) ) # 3x3最大池化层 + 1x1卷积层 self.branch4 = nn.Sequential( nn.MaxPool2d(kernel_size=3, stride=1, padding=1), nn.Conv2d(in_channels, pool_proj, kernel_size=1), nn.BatchNorm2d(pool_proj), nn.ReLU(inplace=True) ) def forward(self, x): branch1 = self.branch1(x) branch2 = self.branch2(x) branch3 = self.branch3(x) branch4 = self.branch4(x) outputs = [branch1, branch2, branch3, branch4] return torch.cat(outputs, 1) # 导入数据集并进行预处理 transform_train = transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) transform_test = 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_train) trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=2) testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test) testloader = torch.utils.data.DataLoader(testset, batch_size=128, shuffle=False, num_workers=2) # 定义损失函数和优化器 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") net = GoogLeNet().to(device) criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.1, momentum=0.9, weight_decay=5e-4) # 训练模型 for epoch in range(200): running_loss = 0.0 for i, data in enumerate(trainloader, 0): inputs, labels = data[0].to(device), data[1].to(device) optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() if i % 100 == 99: print('[%d, %5d] loss: %.3f' % (epoch+1, i+1, running_loss/100)) running_loss = 0.0 # 在测试集上测试模型 correct, total = 0, 0 with torch.no_grad(): for data in testloader: images, labels = data[0].to(device), data[1].to(device) outputs = net(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Accuracy of the network on the 10000 test images: %d %%' % (100*correct/total)) print('Finished Training') ``` 注意:该代码可能需要在GPU上运行,因为GoogleNet比较深,计算量较大。如果没有GPU,可能需要更长的训练时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值