CNN卷积神经网络层级结构以及简单实现–数据输入层、卷积计算层、 激励层 、 池化层、全连接层

不消说,网上的讲解比个人的精彩千百倍;个人也是为了深入实现验证码识别进行学习的,本来打算把cnn相关的学习与操作留在七月

原理:
https://blog.csdn.net/v_JULY_v/article/details/51812459 先看,重点看
https://blog.csdn.net/weiyongle1996/article/details/78088654
https://blog.csdn.net/vbskj/article/details/72829994
https://blog.csdn.net/whyymlm/article/details/79739840
https://blog.csdn.net/u012162613/article/details/43225445
https://mp.weixin.qq.com/s?__biz=Mzg2MzE2MzUxMg==&mid=2247483854&idx=1&sn=315893072da848b41fe524cc3e1c4ce4&chksm=ce7d84d9f90a0dcf28ad267841a73a45619237db2fb038fd734dfeb52a599222876bbf75181b&scene=27#wechat_redirect

python代码实现:
https://www.edureka.co/blog/convolutional-neural-network/ 卷积神经网络教程(CNN) – 使用TensorFlow在Python中开发图像分类器
http://developer.51cto.com/art/201801/563664.htm 我用Python实现了12500张猫狗图像的精准分类
https://blog.csdn.net/luanpeng825485697/article/details/79088938
https://segmentfault.com/a/1190000014689321

库实现:
https://www.bilibili.com/video/av16753727/ 使用python中pytorch库实现卷积神经网络cnn对mnist的识别
https://www.jiqizhixin.com/articles/2018-08-15-4 教你用Keras和CNN建立模型识别神奇宝贝
https://www.ctolib.com/topics-131471.html 使用Keras对交通标志进行分类

二.

这里以github上的一个源码为例子进行学习:https://github.com/nanfengpo/crack_captcha

运行这套源码,正确率是在提升,但是个人训练了一下午都没有大于50%,太慢了

可以把这套源码分为两个部分来进行学习

1.tf的cnn

参考:https://www.cnblogs.com/denny402/p/5853538.html 会发现这篇和github上训练代码极其类似

当看第二篇tf时,会发现大体也是流程化的东西(至少个人目前是这样感觉的):https://www.jianshu.com/p/e112012a4b2d

2.图片的随机生成

这也是对个人而言极其有学习价值的内容

直接贴原作者生成图片的代码了:

from captcha.image import ImageCaptcha
import numpy as np
import matplotlib.pyplot as plt
# from config import NUMBER, CHAR_SMALL, CHAR_BIG, MAX_CAPTCHA, CHAR_SET_LEN, FONT_SIZE
import src.config as config
from PIL import Image
import random

char_dict = {}
number_dict = {}


# 生成随机的指定的字符串
def __gen_random_captcha_text(char_set=config.VALIDATE_STRING, size=None):
# char_set must be a str
if not char_set or not isinstance(char_set, str):
raise ValueError('get the empty char_set')

# 随机
result = list(char_set)
random.shuffle(result)

# 返回字符串
return ''.join(result[0:size])


def gen_random_captcha_image():
image = ImageCaptcha(width=config.IMAGE_WIDTH, height=config.IMAGE_HEIGHT,font_sizes=[config.FONT_SIZE])

text = __gen_random_captcha_text(size=config.MAX_CAPTCHA)
captcha = image.generate(text)
captcha_image = Image.open(captcha)
captcha_source = np.array(captcha_image)
return text, captcha_source


# always gen the require image height ,and width image
def gen_require_captcha_image():
while 1:
text, image = gen_random_captcha_image()
if image.shape == (config.IMAGE_HEIGHT, config.IMAGE_WIDTH, 3):
return text, image


# 把彩色图像转为灰度图像(色彩对识别验证码没有什么用,对于抽取特征也没啥用)
def convert2gray(img):
if len(img.shape) > 2:
gray = np.mean(img, -1)
# 上面的转法较快,正规转法如下
# r, g, b = img[:,:,0], img[:,:,1], img[:,:,2]
# gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
else:
return img


# prepare the char to index
def prepare_char_dict():
if char_dict:
return char_dict

for index, val in enumerate(config.VALIDATE_STRING):
char_dict[val] = index

return char_dict


def prepare_number_dict():
if number_dict:
return number_dict

for index, val in enumerate(config.VALIDATE_STRING):
number_dict[index] = val

return number_dict


def text_to_array(text):
char_dict_tmp = prepare_char_dict()

arr = np.zeros(config.MAX_CAPTCHA * config.CHAR_SET_LEN, dtype=np.int8)
for i, p in enumerate(text):
key_index = char_dict_tmp[p]
index = i * config.CHAR_SET_LEN + key_index
arr[index] = 1

return arr


def array_to_text(arr):
num_dict_tmp = prepare_number_dict()
text = []
char_pos = arr.nonzero()[0]
for index, val in enumerate(char_pos):
if index == 0:
index = 1
key_index = val % (index * config.CHAR_SET_LEN)
text.append(num_dict_tmp[key_index])
return ''.join(text)

def show_image_text():
text, image = gen_random_captcha_image()

f = plt.figure()
ax = f.add_subplot(111)
ax.text(0.1, 0.9, text, ha='center', va='center', transform=ax.transAxes)
plt.imshow(image)

plt.show()

#
if __name__ == '__main__':
# __do_image_text()
# arr = text_to_array('0142')
# print '==========='
# print array_to_text(arr)
show_image_text()

2019.6.11