P85
# P85
import numpy as np
import cv2
# initialize the class labels and set the seed of the pseudorandom
# number generator so we can reproduce our results
labels = ["dogs","cats","panda"]
np.random.seed(1)
'''
seed 参考:
seed( ) 用于指定随机数生成时所用算法开始的整数值。
1.如果使用相同的seed( )值,则每次生成的随即数都相同;
2.如果不设置这个值,则系统根据时间来自己选择这个值,此时每次生成的随机数因时间差异而不同。
3.设置的seed()值仅一次有效
另外参考链接:
https://blog.youkuaiyun.com/qinglu000/article/details/46119621
'''
# randomly initialize our weight matrix and bias vertor -- in a
# *real* training and classification task,these parameters would
# be *learned* by our model, but for the sake of this example,
# let's use random values
W = np.random.randn(3,3072)
b = np.random.randn(3)
# load our example image,resize it,and then flatten it into our
# "feature vector" representation
orig = cv2.imread("beagle.png")
image = cv2.resize(orig,(32,32)).flatten()
# compute the output scores by taking the dot product between the
# weight matrix and image pixels,followed by adding in the bias
scores = W.dot(image) + b
# loop over the scores + labels and display them
# 合成一个元组形式
for (label,score) in zip(labels,scores):
print("[INFO] {}:{:.2f}".format(label,score))
# draw the label with the highest score on the image as our
# prediction
cv2.putText(orig,"Label:{}".format(labels[np.argmax(scores)]),\
(10,30),cv2.FONT_HERSHEY_SIMPLEX,0.9,(0,255,0),2)
# display our input image
cv2.imshow("Image",orig)
cv2.waitKey(0)
本文介绍了一种使用随机初始化的权重矩阵和偏置向量进行图像分类的方法,通过加载和预处理图像,将其转换为特征向量,然后计算得分并展示预测结果。
472

被折叠的 条评论
为什么被折叠?



