欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

classify CIFAR Image With CNN

程序员文章站 2024-03-26 12:46:05
...

本教程演示如何训练一个简单的卷积神经网络(CNN)来对CIFAR图像进行分类。因为本教程使用Keras Sequential API,所以创建和训练我们的模型只需要几行代码

1.1、加载数据

def loadData():
    # 加载数据,cifar数据集 有10个分类,每个分类有6000张彩色图像
    (train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

    # 归一化
    train_images, test_images = train_images / 255.0, test_images / 255.0
    return train_labels, test_images

1.1.1、解决下载cifar数据集比较慢的问题

下载一份cifar数据集放到~\.keras\datasets目录下,
然后修改cifar10.py, 点击datasets.cifar10.load_data()找到cifar10.py文件


@keras_export('keras.datasets.cifar10.load_data')
def load_data():
  """Loads CIFAR10 dataset.

  Returns:
      Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
  """
  dirname = 'cifar-10-batches-py'
  origin = 'https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'
  path = get_file(
      dirname,
      origin=origin,
      untar=True,   
      # 此处注释掉,防止验证文件hash
      # file_hash= '6d958be074577803d12ecdefd02955f39262c83c16fe9348329d7fe0b5c001ce'
  )
  ......
}

1.2、验证数据

def verifyData(train_images, train_labels):
    # 查看前25张图片,验证数据集的正确性
    class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
                   'dog', 'frog', 'horse', 'ship', 'truck']

    plt.figure(figsize=(10,10))
    for i in range(25):
        plt.subplot(5,5,i+1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(train_images[i], cmap=plt.cm.binary)
        # The CIFAR labels happen to be arrays,
        # which is why you need the extra index
        plt.xlabel(class_names[train_labels[i][0]])
    plt.show()

classify CIFAR Image With CNN

1.3、创建卷积基

1.3.1、卷积层

# 创建卷积基
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))

classify CIFAR Image With CNN

1.3.2、池化层

	model.add(layers.MaxPooling2D((2, 2)))

classify CIFAR Image With CNN

1.3.3、全连接层

# 全连接层
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))

完整的构建模型

train_images, train_labels, test_images, test_labels = loadData()
print(train_images.shape)
# verifyData(train_images, train_labels)

# 创建卷积基
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))

# 全连接层
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
# 分类
model.add(layers.Dense(10, activation='softmax')) # 输出层:10个分类, 使用softmax**函数



print(model.summary())

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 30, 30, 32)        896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 13, 13, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64)          0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 4, 4, 64)          36928     
_________________________________________________________________
flatten (Flatten)            (None, 1024)              0         
_________________________________________________________________
dense (Dense)                (None, 64)                65600     
_________________________________________________________________
dense_1 (Dense)              (None, 10)                650       
=================================================================
Total params: 122,570
Trainable params: 122,570
Non-trainable params: 0
_________________________________________________________________
None

1.4、编译模型

# 编译模型
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

1.5、训练模型

history = model.fit(train_images, train_labels, epochs=10,
                    validation_data=(test_images, test_labels))

49472/50000 [============================>.] - ETA: 0s - loss: 0.6245 - accuracy: 0.7791
49728/50000 [============================>.] - ETA: 0s - loss: 0.6242 - accuracy: 0.7792
49984/50000 [============================>.] - ETA: 0s - loss: 0.6248 - accuracy: 0.7789
50000/50000 [==============================] - 12s 249us/sample - loss: 0.6247 - accuracy: 0.7790 - val_loss: 0.8589 - val_accuracy: 0.7174

cudnn未安装

tensorflow.python.framework.errors_impl.UnknownError:  Failed to get convolution algorithm. 
This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
  [[node sequential/conv2d/Conv2D (defined at \setup\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py:1751) ]] [Op:__inference_distributed_function_1055]

1.6、评估模型

plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
plt.show()
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)

10000/1 - 1s - loss: 0.7876 - accuracy: 0.7174

classify CIFAR Image With CNN