ai字母组合发音规律
目录: (Table of contents:)
- Introduction 介绍
- About the Dataset 关于数据集
- Setting up Monk and Prerequisites 设置和尚和先决条件
- Downloading Dataset 下载数据集
- Creating Projects and Experiments 创建项目和实验
- Approach 方法
- Selecting the best model 选择最佳型号
- Conclusion 结论
介绍: (Introduction:)
It is an easy task for us to classify handwritten information but to computers, it is a disconcerting and daunting job. Handwritten character classification in general is a challenging task because there are innumerable ways in which any character can be written.
对我们来说,对手写信息进行分类是一项容易的任务,但是对于计算机而言,这是一项令人不安和艰巨的工作。 通常,手写字符分类是一项艰巨的任务,因为可以使用无数种方法来写入任何字符。
Neural networks have although redefined this task and also play a huge role in developing such classifiers, provided they are fed with a huge amount of data for it to be trained.
神经网络虽然重新定义了这项任务,并且在开发此类分类器的过程中也发挥了巨大作用,但前提是它们会被提供大量数据进行训练。
In this particular blog, we will explore the Russian alphabets and classify them in their handwritten form.
在这个特别的博客中,我们将探索俄语字母并将它们以手写形式分类。
关于数据集: (About The Dataset:)
In the data set, we have got 14190 colored images distributed among 3 image folders, consisting of all 33 categories of Russian alphabets.
在数据集中,我们获得了14190张彩色图像,分布在3个图像文件夹中,其中包括所有33个俄语字母类别。
The first folder has striped backgrounds (with few horizontal and/or vertical lines), the second folder has white backgrounds and the third one has graph type backgrounds (with many ordered horizontal and vertical lines).
第一个文件夹具有条纹背景(水平和/或垂直线很少),第二个文件夹具有白色背景,第三个文件夹具有图形类型背景(水平和垂直线有序许多)。
Here are some images from the dataset:
以下是数据集中的一些图像:
![Image for post](https://miro.medium.com/max/9999/1*csKzYdpQodDgHgbHXD_lCg.png)
![Image for post](https://miro.medium.com/max/9999/1*_UzpZMVjO65K3ZeIXX3SUA.png)
![Image for post](https://miro.medium.com/max/9999/1*IU0PjgWC4OQx21CJoC3IyA.png)
![Image for post](https://miro.medium.com/max/9999/1*UaJtPweCihTjTK8ynePUVQ.png)
![Image for post](https://miro.medium.com/max/9999/1*7Yz1TFhJWbVJGgZIhZVR8Q.png)
![Image for post](https://miro.medium.com/max/9999/1*0ZyIN9qZDWRJnMmtR0wUDA.png)
![Image for post](https://miro.medium.com/max/9999/1*5Fb-seAASmXWQ4fNa-i29Q.png)
![Image for post](https://miro.medium.com/max/9999/1*czH4zKldLGN6I7Q3TpARYA.png)
![Image for post](https://miro.medium.com/max/9999/1*BAYcfWd-Un2pmhRPKCcl9g.png)
Here are the Russian letters and their corresponding numerics used for prediction purpose:
以下是用于预测目的的俄语字母及其对应的数字:
а=>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, ы=>29, ь=>30, э=>31, ю=>32, я=>33
а=> 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,ы=> 29,ь=> 30, э=> 31,ю=> 32,я=> 33
So don’t worry, even if we don’t get the article, we are going to leave with some Russian alphabets impression atleast!The complete dataset is prepared and uploaded by Olga Belitskaya and can be found right here:
所以不用担心,即使我们没有得到这篇文章,我们也将至少留下一些俄语字母的印象!完整的数据集是由Olga Belitskaya准备并上传的,可以在这里找到:
Now let’s get going by setting up the monk and some prerequisites.
现在,让我们开始设置和尚和一些先决条件。
设置和尚和先决条件 (Setting up Monk and Prerequisites)
We start by installing the monk library.
我们首先安装和尚库。
1.)We have used colab here, so here is the installation process for the same.
1.)我们在这里使用了colab,因此这里是相同的安装过程。
#Installation process for colab
pip install -U monk-colab
Although we can explore other ways of the installation at the monk library.
尽管我们可以在和尚库中探索其他安装方式。
2.)Add this to system path (Required for every terminal or kernel run)
2.)将其添加到系统路径(每个终端或内核运行必需)
import sys
sys.path.append("monk_v1/");
下载数据集 (Downloading Dataset)
Next, we will get the dataset from Kaggle directly to colab. For that, we have to first create an API token from Kaggle.
接下来,我们将直接从Kaggle获取数据集到colab。 为此,我们必须首先从Kaggle创建API令牌。
Go to your Kaggle profile>> My Account >> Scroll down to API section >> Click on Create new API Token( remember to expire all other tokens if used previously)
转到您的Kaggle个人资料>>我的帐户>>向下滚动到API部分>>单击创建新的API令牌(请记住,如果以前使用过,所有其他令牌都将过期)
After clicking on this, the kaggle.json file will be installed in your system.Next, go the dataset page again and on the right of the new notebook option, we can find an icon to copy the API command. This command will be used to download the dataset.For now, upload the kaggle.json file in google colab as shown here.
单击此按钮后,kaggle.json文件将安装在您的系统中。接下来,再次进入数据集页面,在新笔记本选项的右侧,我们可以找到一个图标来复制API命令。 此命令将用于下载数据集。现在,如下所示,将kaggle.json文件上传到google colab中。
! pip install -q kaggle
from google.colab import files
files.upload()
#Upload the kaggle.json file here
Further, use the API command to download the dataset.
此外,使用API命令下载数据集。
! mkdir ~/.kaggle
! cp kaggle.json ~/.kaggle/
! chmod 600 ~/.kaggle/kaggle.json#Download the full dataset zip file to Colab
! kaggle datasets download -d 'olgabelitskaya/classification-of-handwritten-letters'
Now, unzip this file and create a new file to store all the datasets in one place.
现在,解压缩该文件并创建一个新文件以将所有数据集存储在一个位置。
#Unzip the downloaded zip file and put it under a new files section! unzip -qq sample.zip
import zipfile
zip_ref = zipfile.ZipFile('classification-of-handwritten-letters.zip', 'r')
zip_ref.extractall('files')
zip_ref.close()
After this is done, we import these libraries and the appropriate backend.
完成此操作后,我们将导入这些库和相应的后端。
import os
import sys
sys.path.append("monk_v1/");
sys.path.append("monk_v1/monk/");#Importing MXNet Gluon API backend
from monk.gluon_prototype import prototype
I have used MXNet Gluon API as Backend here, with just one line of code we can choose our desired backend and not worry about encountering different syntax later. This is possible with the monk library, by using just one syntax we can work across different frameworks like PyTorch, MXNet, Keras, TensorFlow.
我在这里使用MXNet Gluon API作为后端,只需一行代码,我们就可以选择所需的后端,而不必担心以后遇到不同的语法。 和尚库可以做到这一点,只需使用一种语法,我们就可以跨不同的框架(如PyTorch,MXNet,Keras,TensorFlow)工作。
创建项目和实验 (Creating Project and Experiments)
To create the project name and the experiment name we use the prototype function. We can now create multiple experiments under a project and make comparisons among them on various aspects.
为了创建项目名称和实验名称,我们使用了原型函数。 现在,我们可以在一个项目下创建多个实验,并在各个方面进行比较。
#Setup Project Name and Experiment Namegtf = prototype(verbose=1);
gtf.Prototype("Pilot", "Densenet_121");
![Image for post](https://miro.medium.com/max/9999/1*M8YzeKrm8vMqnUVLv343aQ.png)
方法 (Approach)
Approach for model training :
模型训练方法:
We have not concatenated the three image folders, which would have allowed us to train the entire dataset together.Our approach to building a robust model is training the model first by using images that are least affected by the background i.e. second image folder.Also, this folder is reasonably big (with a large number of images), so it can train the model effectively at first and we can understand how the model is performing with simpler data.
我们没有串联三个图像文件夹,这可以让我们一起训练整个数据集。我们构建健壮模型的方法是首先使用受背景影响最小的图像(即第二个图像文件夹)来训练模型。此文件夹相当大(包含大量图像),因此它可以在一开始就有效地训练模型,并且我们可以了解模型如何使用更简单的数据来执行。
This would somewhat ensure if the model learns to extract desired features and we can verify this by analyzing plots for the same.If it is observed that the model has learned to extract features properly, then we can continue training the model with the next folder containing graphic type background images, even this folder is big enough so hopefully, it makes the model learn to ignore the background.
这一定程度上可以确保模型是否学会提取所需的特征,我们可以通过分析相同特征的图来验证这一点。如果观察到模型已学会正确提取特征,则可以继续使用包含以下内容的下一个文件夹训练模型图形类型的背景图像,即使这个文件夹足够大,也希望它使模型学会忽略背景。
Finally, we update and train the model with images containing stripped background.
最后,我们使用包含剥离背景的图像更新和训练模型。
This approach allows us to figure out which part of the dataset is not being classified properly and in case of unsatisfactory results, we can analyze all plots and identify where the model is performing poorly and focus on that part.
通过这种方法,我们可以找出数据集的哪个部分没有正确分类,如果结果不令人满意,我们可以分析所有图并确定模型在哪里表现不佳,然后集中精力关注该部分。
Approach for developing models :1.)First, the backend is chosen (MXNet Gluon), some basic models are selected which can be appropriate for this particular task.2.)Less dense variants are chosen and their performances are compared.
开发模型的方法:1.)首先,选择后端(MXNet Gluon),然后选择一些适合于此特定任务的基本模型。2.)选择较少的密集变体,并比较它们的性能。
#Analysing basic models
analysis_name = "analyse_models";models = [["resnet34_v2", False, True],["densenet121", False, True],["vgg16", False, True],["alexnet",False,True],["mobilenetv2_1.0",False , True]];epochs=5
percent_data=15
analysis = gtf.Analyse_Models(analysis_name, models, percent_data, num_epochs=epochs, state="keep_all");
3.)Parameters are tuned for the models. This is the most crucial part of developing a good model.
3.)针对模型调整参数。 这是开发良好模型的最关键部分。
#Shuffle the datagtf.update_shuffle_data(True);#For densenets batch size of 2 and 4 did not work well, batch size of 8 worked just fine , after which validation loss starts to increasegtf.update_batch_size(8);#learning rate 0.1 and 0.05 were not working well ,lr lesser than that didn't vary much w.r.t val and training loss .0.01 best choice#Though optimizers like Adam and its variants are very fast at converging but the problem with them are they sometimes get stuck at local optima's.#Whereas famous optimizers linke SGD and SGD plus momentum are slower to converge , but they dont get stuck at local optima easily.#Here after analysing all these optimizers , sgd worked better.#sgd was the best optimizer even for densenetsgtf.optimizer_sgd(0.01);gtf.Reload();
This analysis is very important while tuning parameters.
调整参数时,此分析非常重要。
4.)With the same tuned parameters, more dense variants for the same models are tried.
4)使用相同的调整参数,尝试使用相同模型的更密集的变体。
#Comparing DenseNets for different depth of densenet variants.analysis_name = "analyse_models";
models = [["densenet121", False, True], ["densenet161", False, True], ["densenet169", False, True],["densenet201", False, True]];
epochs=10
percent_data=15
analysis = get.Analyse_Models(analysis_name, models, percent_data, num_epochs=epochs, state="keep_none");
If we would have directly gone for dense networks, maybe due to exploding/diminishing gradients it wouldn’t perform well and we may end up not considering that option at all.
如果我们直接去密集的网络,也许是由于梯度的爆炸/减小,它可能无法很好地工作,最终我们可能根本不考虑该选项。
选择最佳型号 (Selecting the best model)
After all these efforts it is quite easy to figure out which model can give you the best results when exposed to unseen data. For this case it turns out to be Densenets, the appropriate depth of the Densenet chosen is 121, it is enough to give desirable results.Resnets architecture ranks second, in terms of overall performance after being tuned. Mobilenets can potentially perform very well, although it took a lot of training time and space for this particular task.
经过所有这些努力,很容易找出暴露于看不见的数据时哪种模型可以为您带来最佳结果。 对于这种情况,结果证明是Densenet,所选择的Densenet的适当深度为121,足以给出理想的结果。Resnets体系结构在调整后的总体性能方面排名第二。 Mobilenet的性能可能很好,尽管为此特定任务花费了大量的培训时间和空间。
![Image for post](https://miro.medium.com/max/9999/1*WddKAvOxQXR58ruEp5lKqQ.png)
Three plots here are of the same model but with updated data. Densenet_121_3 is trained on the complete dataThe plot indicates that the model is learning fairly well on all three kinds of datasets.We got a hard to beat validation performance from our model.
这里的三个图具有相同的模型,但具有更新的数据。 Densenet_121_3在完整数据上进行了训练该图表明该模型在所有三种数据集上的学习都非常好,我们的模型在验证性能方面无与伦比。
![Image for post](https://miro.medium.com/max/9999/1*SlDTYwuk6skUxfuxxRS0eg.png)
The model doesn’t look like it is over-fitting and performs well on classifying new images, ensuring that the model is good to go.If there are any improvements that we could do in the present model, it would be focusing on improving/increasing the images with graphic type backgrounds. If we carefully observe, after those images are updated and the model is trained, the model’s performance has dipped a bit.This is another area of work that can be explored.
该模型看起来不太合适,并且在对新图像进行分类时表现出色,从而确保该模型能够很好地运行。如果在当前模型中我们可以做任何改进,它将专注于改进/使用图形类型的背景增加图像。 如果我们仔细观察,在更新了这些图像并训练了模型之后,模型的性能就会有所下降,这是可以探索的另一个工作领域。
结论 (Conclusion)
we have performed Transfer learning using various architectures, used a well-defined approach to build a solid classifier. Although there is one thing to be noted here, creating projects and experiments made it very easy for us to manage and compare experiments/models.
我们已经使用各种架构进行了转移学习,并使用了定义明确的方法来构建可靠的分类器。 尽管这里需要注意一件事,但是创建项目和实验使我们很容易管理和比较实验/模型。
Also, we could perform so many complex operations using very few lines of code. If we would have gone for the conventional approach, making such comparisons across models would cost us undesirable stretched out code, which can be very hard to debug. Also updating the data set and re-training models was made an elementary task when we used the monk library.
同样,我们可以使用很少的代码行执行许多复杂的操作。 如果我们会采用传统方法,那么在模型之间进行这样的比较将使我们付出不必要的扩展代码,这可能很难调试。 当我们使用和尚库时,更新数据集和重新训练模型也成为一项基本任务。
There are also certain areas mentioned where more work can be done which could further increase the performance and reliability of the model.
还提到了某些可以做更多工作的领域,这些工作可以进一步提高模型的性能和可靠性。
We have also made inferences using some test images, visit the code link below to view them.
我们还使用一些测试图像进行了推断,请访问下面的代码链接以查看它们。
For the entire code :
对于整个代码:
or you can view code at image classification zoo.
或者,您可以在图像分类Zoo上查看代码。
For more such applications:
对于更多此类应用程序:
Monk Tutorials:
和尚教程:
Please share this article if it helped you learn something new!
如果可以帮助您学习新知识,请分享此文章!
Thanks for Reading!
谢谢阅读!
翻译自: https://towardsdatascience.com/russian-alphabets-classification-using-monk-ai-4df7d1ad8542
ai字母组合发音规律