主要为了理解ANN使用方法
import cv2
import numpy as np
from random import randint
# ANN网络初始化
animals_net = cv2.ml.ANN_MLP_create()
animals_net.setTrainMethod(cv2.ml.ANN_MLP_RPROP | cv2.ml.ANN_MLP_UPDATE_WEIGHTS)
animals_net.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
animals_net.setLayerSizes(np.array([3, 8, 4]))
animals_net.setTermCriteria(( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ))
"""Input arrays
weight, length, teeth
"""
"""Output arrays
dog, eagle, dolphin and dragon
"""
#狗,数据
def dog_sample():
return [randint(10, 20), 1, randint(38, 42)]
#狗,标签
def dog_class():
return [1, 0, 0, 0]
#秃鹰,数据
def condor_sample():
return [randint(3,10), randint(3,5), 0]
#秃鹰,标签
def condor_class():
return [0, 1, 0, 0]
#海豚,数据
def dolphin_sample():
return [randint(30, 190), randint(5, 15), randint(80, 100)]
#海豚,标签
def dolphin_class():
return [0, 0, 1, 0]
#龙,数据
def dragon_sample():
return [randint(1200, 1800), randint(30, 40), randint(160, 180)]
#龙,标签
def dragon_class():
return [0, 0, 0, 1]
#记录
def record(sample, classification):
return (np.array([sample], dtype=np.float32), np.array([classification], dtype=np.float32))
records = []
"""
#不执行此段
SAMPLES = 5000
for x in range(0, SAMPLES):
print ("Samples %d/%d" % (x, SAMPLES))
animals_net.train(np.array([dog_sample()], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([dog_class()], dtype=np.float32))
animals_net.train(np.array([condor_sample()], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([condor_class()], dtype=np.float32))
animals_net.train(np.array([dolphin_sample()], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([dolphin_class()], dtype=np.float32))
animals_net.train(np.array([dragon_sample()], dtype=np.float32), cv2.ml.ROW_SAMPLE, np.array([dragon_class()], dtype=np.float32))
"""
#生成训练集
RECORDS = 5000
for x in range(0, RECORDS):
records.append(record(dog_sample(), dog_class()))
records.append(record(condor_sample(), condor_class()))
records.append(record(dolphin_sample(), dolphin_class()))
records.append(record(dragon_sample(), dragon_class()))
print("len records:%d"%len(records))
#迭代200轮,训练
EPOCHS = 2
for e in range(0, EPOCHS):
print("Epoch %d:" % e)
for t, c in records:
animals_net.train(t, cv2.ml.ROW_SAMPLE, c)
#测试
TESTS = 100
dog_results = 0
for x in range(0, TESTS):
clas = int(animals_net.predict(np.array([dog_sample()], dtype=np.float32))[0])
print("class: %d" % clas)
if (clas) == 0:
dog_results += 1
condor_results = 0
for x in range(0, TESTS):
clas = int(animals_net.predict(np.array([condor_sample()], dtype=np.float32))[0])
print("class: %d" % clas)
if (clas) == 1:
condor_results += 1
dolphin_results = 0
for x in range(0, TESTS):
clas = int(animals_net.predict(np.array([dolphin_sample()], dtype=np.float32))[0])
print("class: %d" % clas)
if (clas) == 2:
dolphin_results += 1
dragon_results = 0
for x in range(0, TESTS):
clas = int(animals_net.predict(np.array([dragon_sample()], dtype=np.float32))[0])
print("class: %d" % clas)
if (clas) == 3:
dragon_results += 1
print ("Dog accuracy: %f%%" % (dog_results))
print ("condor accuracy: %f%%" % (condor_results))
print ("dolphin accuracy: %f%%" % (dolphin_results))
print ("dragon accuracy: %f%%" % (dragon_results))
原文地址:https://www.cnblogs.com/jsxyhelu/p/8611935.html
改进到自己的应用
import cv2
import numpy as np
import tensorflow as tf
import random
from random import randint
#字符图像的特征提取方法
#要求:1.无关图像大小;2.输入图像默认为灰度图
def SimpleGridFeature(image):
'''
@description:提取字符图像的简单网格特征
@image:灰度字符图像
@return:长度为64字符图像的特征向量feature
@author:RenHui
'''
#图像扩充
resize_flag = 0
if image.shape[0]%8 !=0:
img_h = image.shape[0] + (8 - image.shape[0]%8)
resize_flag = 1
if image.shape[1]%8 !=0:
img_w = image.shape[1] + (8 - image.shape[1]%8)
resize_flag = 1
if resize_flag:
image = cv2.resize(image,(img_w,img_h))
#二值化
retval,binary = cv2.threshold(image,0,255,cv2.THRESH_OTSU)
#计算网格大小
grid_h = binary.shape[0]/8
grid_w = binary.shape[1]/8
#定义特征向量
feature = np.zeros(64)
for j in range(8):
for i in range(8):
grid = binary[int(j*grid_h):int((j+1)*grid_h),int(i*grid_w):int((i+1)*grid_w)]
feature[j*8+i] = grid[grid==0].size
return feature
#OCR,类标签
# i范围 0-9
def OCR_classLabel(i):
list = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
list[i]=1
return list
#记录
def record(sample, classification):
return (np.array([sample], dtype=np.float32), np.array([classification], dtype=np.float32))
#产生随机数
#random_int_list(1, 1, 64)
def random_int_list(start, stop, length):
start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start))
length = int(abs(length)) if length else 0
random_list = []
for i in range(length):
random_list.append(random.randint(start, stop))
return random_list
############################################################################
# ANN网络初始化
OCR_ANN = cv2.ml.ANN_MLP_create() #创建
OCR_ANN.setTrainMethod(cv2.ml.ANN_MLP_RPROP | cv2.ml.ANN_MLP_UPDATE_WEIGHTS)
OCR_ANN.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM) #激活函数
OCR_ANN.setLayerSizes(np.array([64, 64, 10])) #每层神经元
OCR_ANN.setTermCriteria(( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ))
#生成训练集
traindata = []
features = np.load('features_0-9.npy')
labels = np.load('labels_0-9.npy')
for feature,label in zip (features,labels):
traindata.append(record(feature,label))
# RECORDS = 50
# for x in range(0, RECORDS):
# if (x % 1000 == 0):
# print("生成数据集:%d"% x)
# traindata.append(record(random_int_list(0, 0, 64),OCR_classLabel(0)))
# traindata.append(record(random_int_list(1, 1, 64),OCR_classLabel(1)))
# traindata.append(record(random_int_list(2, 2, 64),OCR_classLabel(2)))
# traindata.append(record(random_int_list(3, 3, 64),OCR_classLabel(3)))
# traindata.append(record(random_int_list(4, 4, 64),OCR_classLabel(4)))
# traindata.append(record(random_int_list(5, 5, 64),OCR_classLabel(5)))
# traindata.append(record(random_int_list(6, 6, 64),OCR_classLabel(6)))
# traindata.append(record(random_int_list(7, 7, 64),OCR_classLabel(7)))
# traindata.append(record(random_int_list(8, 8, 64),OCR_classLabel(8)))
# traindata.append(record(random_int_list(9, 9, 64),OCR_classLabel(9)))
#打乱训练数据集
print("随机打乱数据中...")
random.shuffle(traindata) #打乱数
# 迭代200轮,训练
EPOCHS = 200
for e in range(0, EPOCHS):
print("Epoch %d:" % e)
counter = 0
for data, label in traindata:
# print("data:")
# print(data)
# print("label:")
# print(label)
counter +=1
if (counter % 1000 == 0):
print("Epoch %d: Trained %d" % (e, counter))
OCR_ANN.train(data, cv2.ml.ROW_SAMPLE, label)
#测试
# 读图
path = "E:/sxl_Programs/Python/ANN/2.jpg"
image = cv2.imread(path,0)
#获取特征向量,传入灰度图
ArrayFeature=SimpleGridFeature(image)
print("ArrayFeature:",ArrayFeature)
clas = int(OCR_ANN.predict(np.array([ArrayFeature], dtype=np.float32))[0])
print("class: %d" % clas)
# TESTS = 10
# dog_results = 0
# for x in range(0, TESTS):
# clas = int(OCR_ANN.predict(np.array([random_int_list(1,1, 64)], dtype=np.float32))[0])
# print("class: %d" % clas)