成交量积神经网络在Keras和Tensorflow中的多类分类示例

在前面的文章中,我们研究了回归问题和二进制分类问题。现在让我们来看另一个常见的监督学习问题,即多类分类。

多类别分类的主要训练练习是MNIST数据集,这是一组手写的罗马数字,虽然特别有用,但我们可以对其加一点点香料,并使用Kaggle上可用的 Kannada MNIST数据集

卡纳达语在印度南部地区使用,大约有4500万人,与罗马数字相比,它的优势是大多数人不那么熟悉,而且由于其某些数字之间的相似性,还带来了一些额外的挑战。

让我们开始吧

首先,从Kaggle竞赛网站下载数据

解压缩数据并启动Jupyter Notebook(或Python IDE,无论您喜欢使用什么)后,让我们开始导入所需的模块:

import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout, Dense, Conv2D, MaxPool2D, Flatten
from tensorflow.keras.callbacks import TensorBoard
import matplotlib.pyplot as plt
import seaborn as sns
import random
import time
print(tf.__version__)

如果您运行的是Tensorflow的GPU版本,最好检查一下GPU是否确实可用。特别是在成交量积神经网络(或简称为CNN)的情况下,GPU可以将您的训练过程加速多达100倍。让我们确保GPU的功能触手可及。

gpus = tf.config.experimental.list_physical_devices('GPU')
print("GPUs Available: ", len(gpus))
tf.config.experimental.set_memory_growth(gpus[0], True)python

火车数据集包含60,000个样本,但是我们没有验证集,因此我们将为验证步骤留出大约8-10%的样本。

VALIDATION_SET = 5000 # define the length of the validation set

# load the data

VALIDATION_SET = 5000

train_raw_data = pd.read_csv('../input/Kannada-MNIST/train.csv')
test_raw_data = pd.read_csv('../input/Kannada-MNIST/test.csv')

def split_sets(x):
    x = x.sample(frac=1, axis=0) # shuffling the content to ensure the model doesn't learn about the order of the items
    val = x[:VALIDATION_SET]
    train = x[VALIDATION_SET:]
    val.reset_index(drop=True, inplace=True)
    train.reset_index(drop=True, inplace=True)
    y_train = train['label']
    x_train = train.drop(['label'], axis=1) / 255 # normalizing the 0 - 255 scale to 0 -1
    y_val = val['label']
    x_val = val.drop(['label'], axis=1) / 255 # normalizing the 0 - 255 scale to 0 -1
    return y_val, x_val, y_train, x_train

y_val, x_val, y_train, x_train = split_sets(train_raw_data)

我们还想确保我们的随机训练集在不同类别之间显示出一定的平衡。

sns.distplot(y_val, color='red')
sns.distplot(y_train, color='green')

现在让我们预览一些示例,这个数字实际上是什么样的?

# Let's have a look at some random images

x_train = x_train.values.reshape(x_train.shape[0], 28, 28) # We need to reshape the images to be arranged in a square format

fig, ax = plt.subplots(1,6, figsize=(20,8))

for i in range(6):
    index = random.randint(0, len(x_train))
    ax[i].imshow(x_train[index],cmap=plt.cm.binary)
    ax[i].set_title(y_train[index], fontSize=24)

最后,让我们再次重塑图像,以为我们的Conv2D图层做好准备:

x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_val = x_val.values.reshape(x_val.shape[0], 28, 28, 1)
y_train = y_train.values
y_val = y_val.values

现在我们准备建立和训练我们的模型

def build_model():
    model = Sequential()
    model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', activation ='relu', input_shape = (28, 28, 1)))
    model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same',  activation ='relu'))
    model.add(MaxPool2D(pool_size=(2,2)))
    model.add(Dropout(0.25))

    model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu'))
    model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu'))
    model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(256, activation = "relu"))
    model.add(Dropout(0.5))
    model.add(Dense(10, activation = "softmax"))

    model.compile(
        optimizer='adam',
        loss=['sparse_categorical_crossentropy'],
        metrics=['accuracy']
    )
    return model

tensorboard = TensorBoard(log_dir=f"logs/{time.time()}", histogram_freq=1)

model = build_model()

history = model.fit(
    x_train,
    y_train,
    epochs=6,
    batch_size=32,
    validation_data=(
        x_val,
        y_val
    ),
    callbacks=[
        tensorboard
    ]
)

片刻之后,您应该以大约99%左右的验证精度对模型进行训练,真棒让我们在测试数据上进行测试:

# Preparing the test data first
test_ids = test_raw_data[['id']] # we'll need this for the Kaggle submission
test_data = test_raw_data.drop(['id'], axis=1)
test_data = test_data / 255
test_data = test_data.values.reshape(test_data.shape[0], 28, 28, 1)

# Let's get the model to actually predict the labels
predictions = model.predict(test_data)

# Finally, let's render some of these images
ROWS = 6
COLUMNS = 6
fig, ax = plt.subplots(ROWS,COLUMNS, figsize=(40,40))

for row in range(ROWS):
    for column in range(COLUMNS):
        imgIndex = random.randint(0, len(test_data))
        image = test_data[imgIndex]
        image = image.reshape(28,28)
        ax[row,column].imshow(image,cmap=plt.cm.binary)
        ax[row, column].set_title(np.argmax(predictions[imgIndex]), fontSize=24)
提示:投资有风险,入市需谨慎,本资讯不作为投资理财建议。请理性投资,切实提高风险防范意识;如有发现的违法犯罪线索,可积极向有关部门举报反映。
你可能还喜欢