在本教程中,我们将使用 TensorFlow (Keras API) 实现一个用于多分类任务的深度学习模型,该任务需要对阿拉伯语手写字符数据集进行识别。
在本教程中,我们将使用 TensorFlow (Keras API) 实现一个用于多分类任务的深度学习模型,该任务需要对阿拉伯语手写字符数据集进行识别。
数据集下载地址:https://www.kaggle.com/mloey1/ahcd1
数据集介绍
该数据集由 60 名参与者书写的16,800 个字符组成,年龄范围在 19 至 40 岁之间,90% 的参与者是右手。
每个参与者在两种形式上写下每个字符(从“alef”到“yeh”)十次,如图 7(a)和 7(b)所示。表格以 300 dpi 的分辨率扫描。使用 Matlab 2016a 自动分割每个块以确定每个块的坐标。该数据库分为两组:训练集(每类 13,440 个字符到 480 个图像)和测试集(每类 3,360 个字符到 120 个图像)。数据标签为1到28个类别。在这里,所有数据集都是CSV文件,表示图像像素值及其相应标签,并没有提供对应的图片数据。
导入模块
import numpy as np
import pandas as pd
#允许对dataframe使用display()
from IPython.display import display
# 导入读取和处理图像所需的库
import csv
from PIL import Image
from scipy.ndimage import rotate
读取数据
# 训练数据images
letters_training_images_file_path = "../input/ahcd1/csvTrainImages 13440x1024.csv"
# 训练数据labels
letters_training_labels_file_path = "../input/ahcd1/csvTrainLabel 13440x1.csv"
# 测试数据images和labels
letters_testing_images_file_path = "../input/ahcd1/csvTestImages 3360x1024.csv"
letters_testing_labels_file_path = "../input/ahcd1/csvTestLabel 3360x1.csv"
# 加载数据
training_letters_images = pd.read_csv(letters_training_images_file_path, header=None)
training_letters_labels = pd.read_csv(letters_training_labels_file_path, header=None)
testing_letters_images = pd.read_csv(letters_testing_images_file_path, header=None)
testing_letters_labels = pd.read_csv(letters_testing_labels_file_path, header=None)
print("%d个32x32像素的训练阿拉伯字母图像。" %training_letters_images.shape[0])
print("%d个32x32像素的测试阿拉伯字母图像。" %testing_letters_images.shape[0])
training_letters_images.head()
13440个32x32像素的训练阿拉伯字母图像。3360个32x32像素的测试阿拉伯字母图像。
查看训练数据的head
np.unique(training_letters_labels)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28], dtype=int32)
下面需要将csv值转换为图像,我们希望展示对应图像的像素值图像。
def convert_values_to_image(image_values, display=False):
image_array = np.asarray(image_values)
image_array = image_array.reshape(32,32).astype('uint8')
# 原始数据集被反射,因此我们将使用np.flip翻转它,然后通过rotate旋转,以获得更好的图像。
image_array = np.flip(image_array, 0)
image_array = rotate(image_array, -90)
new_image = Image.fromarray(