图像识别1
来源:互联网 发布:下载办公软件wpsoffice 编辑:程序博客网 时间:2024/06/03 17:33
import pickle
import os.path
import numpy as np
def load_CIFAR_batch(filename):
""" load single batch of cifar """
with open(filename, 'rb') as f:
datadict = pickle.load(f)
X = datadict['data']
Y = datadict['labels']
X = X.reshape(10000, 3, 32,32).transpose(0,2,3,1).astype("float")
Y = np.array(Y)
return X, Y
def load_CIFAR10(ROOT):
""" load all of cifar """
xs = []
ys = []
for b in range(1,3):
f = os.path.join(ROOT, 'data_batch_%d' % (b, ))
X, Y = load_CIFAR_batch(f)
xs.append(X)
ys.append(Y)
print("finish")
Xtr = np.concatenate(xs)
Ytr = np.concatenate(ys)
del X, Y
Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch'))
return Xtr, Ytr, Xte, Yte
Xtr, Ytr, Xte, Yte = load_CIFAR10('/opt/cifar/cifar-10-batches-py')
class NearestNeighbor(object):
def __init__(self):
pass
def train(self, X, y):
""" X is N x D where each row is an example. Y is 1-dimension of size N """
# the nearest neighbor classifier simply remembers all the training data
self.Xtr = X
self.ytr = y
def predict(self, X):
""" X is N x D where each row is an example we wish to predict label for """
num_test = X.shape[0]
Ypred = np.zeros(num_test, dtype = self.ytr.dtype)
for i in xrange(num_test):
#distances = np.sqrt(np.sum(np.square(self.Xtr - X[i,:]), axis = 1))
distances = np.sum(np.abs(self.Xtr - X[i,:]), axis = 1)
min_index = np.argmin(distances) # get the index with smallest distance
Ypred[i] = self.ytr[min_index] # predict the label of the nearest example
return Ypred
Xtr_rows = Xtr.reshape(Xtr.shape[0], 32 * 32 * 3) # Xtr_rows becomes 50000 x 3072
Xte_rows = Xte.reshape(Xte.shape[0], 32 * 32 * 3) # Xte_rows becomes 10000 x 3072
nn = NearestNeighbor() # create a Nearest Neighbor classifier class
nn.train(Xtr_rows, Ytr) # train the classifier on the training images and labels
Yte_predict = nn.predict(Xte_rows) # predict labels on the test images print
'accuracy: %f' % ( np.mean(Yte_predict == Yte) )
import os.path
import numpy as np
def load_CIFAR_batch(filename):
""" load single batch of cifar """
with open(filename, 'rb') as f:
datadict = pickle.load(f)
X = datadict['data']
Y = datadict['labels']
X = X.reshape(10000, 3, 32,32).transpose(0,2,3,1).astype("float")
Y = np.array(Y)
return X, Y
def load_CIFAR10(ROOT):
""" load all of cifar """
xs = []
ys = []
for b in range(1,3):
f = os.path.join(ROOT, 'data_batch_%d' % (b, ))
X, Y = load_CIFAR_batch(f)
xs.append(X)
ys.append(Y)
print("finish")
Xtr = np.concatenate(xs)
Ytr = np.concatenate(ys)
del X, Y
Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch'))
return Xtr, Ytr, Xte, Yte
Xtr, Ytr, Xte, Yte = load_CIFAR10('/opt/cifar/cifar-10-batches-py')
class NearestNeighbor(object):
def __init__(self):
pass
def train(self, X, y):
""" X is N x D where each row is an example. Y is 1-dimension of size N """
# the nearest neighbor classifier simply remembers all the training data
self.Xtr = X
self.ytr = y
def predict(self, X):
""" X is N x D where each row is an example we wish to predict label for """
num_test = X.shape[0]
Ypred = np.zeros(num_test, dtype = self.ytr.dtype)
for i in xrange(num_test):
#distances = np.sqrt(np.sum(np.square(self.Xtr - X[i,:]), axis = 1))
distances = np.sum(np.abs(self.Xtr - X[i,:]), axis = 1)
min_index = np.argmin(distances) # get the index with smallest distance
Ypred[i] = self.ytr[min_index] # predict the label of the nearest example
return Ypred
Xtr_rows = Xtr.reshape(Xtr.shape[0], 32 * 32 * 3) # Xtr_rows becomes 50000 x 3072
Xte_rows = Xte.reshape(Xte.shape[0], 32 * 32 * 3) # Xte_rows becomes 10000 x 3072
nn = NearestNeighbor() # create a Nearest Neighbor classifier class
nn.train(Xtr_rows, Ytr) # train the classifier on the training images and labels
Yte_predict = nn.predict(Xte_rows) # predict labels on the test images print
'accuracy: %f' % ( np.mean(Yte_predict == Yte) )
阅读全文
0 0
- 图像识别随笔1
- 图像识别1
- Java文字图像识别(1)
- Java文字图像识别(1)
- 图像识别
- 图像识别
- 图像识别
- 图像识别
- 图像识别
- 图像识别
- 图像识别
- 图像识别
- 图像识别(1)——手写笔&手势识别
- 【太虚AR_v0.1】使用教程 | 云识别(图像识别)
- Java文字图像识别(1)[88250原创]
- Java文字图像识别(1)[88250原创]
- python 验证码识别(1)----图像出来
- 图像识别算法研究(1)---二值化概述
- 【线上讲座】如果转型为AI人工智能?
- android 动态设置屏幕控件宽高度
- mt2503 [MODIS]60平台上MODIS屏幕黑屏
- Eclipse导入Gradle项目
- sqlserver SP
- 图像识别1
- Actionbar与recyclerview的用法
- How to build docker registry for local use by shell script
- LitePal的基础用法
- 汇编语言简单小程序——运算类编程实验
- 中断的新认识
- CSS3 input[type=file] 样式美化,input上传按钮美化
- 招聘黑话大全,能听懂证明你是历经磨难的老司机
- 一个据稿通知