【OpenCV】 实现神经网络
2018-02-11 11:36
239 查看
一、代码
发个CSDN,证明我看过
import cv2
import numpy as np
from random import randint
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, 6, 4])) #[3, 6, 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)] # 3x1
def dog_class():
return [1, 0, 0, 0]
def condor_sample():
return [randint(3,10), randint(3,5), 0] # 3x1
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()))
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))
发个CSDN,证明我看过
import cv2
import numpy as np
from random import randint
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, 6, 4])) #[3, 6, 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)] # 3x1
def dog_class():
return [1, 0, 0, 0]
def condor_sample():
return [randint(3,10), randint(3,5), 0] # 3x1
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()))
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))
相关文章推荐
- OpenCV实现车牌识别,OCR分割,ANN神经网络
- 使用opencv的SVM和神经网络实现车牌识别
- 使用opencv的SVM和神经网络实现车牌识别
- OpenCV实现车牌识别,OCR分割,ANN神经网络
- 基于opencv的神经网络算法实现两类分类问题的可视化演示
- 使用opencv的SVM和神经网络实现车牌识别(高质量的文章******)
- 使用opencv的SVM和神经网络实现车牌识别
- C++从零实现神经网络(收藏版:两万字长文)
- 神经网络:多层网络与C++实现
- PyCharm中运行Caffe(Win7) —— 用Caffe实现一个神经网络
- 简单神经网络实现 01
- C++实现BP 神经网络模拟二维机械臂运动_智能计算期末2
- Python20行代码实现多层神经网络的学习
- 简单神经网络实现 02
- [吴恩达 DL] Class1 Week4 深层神经网络+代码实现
- 深度学习基础模型算法原理及编程实现--06.循环神经网络
- Python编程实现的简单神经网络算法示例
- 训练神经网络的简单例子(TensorFlow平台下Python实现)
- 一个 11 行 Python 代码实现的神经网络
- c++实现mlp神经网络