Deep_learning_for_computer_vision_with_python_线性分类器

本文介绍了一种使用随机初始化的权重矩阵和偏置向量进行图像分类的方法,通过加载和预处理图像,将其转换为特征向量,然后计算得分并展示预测结果。

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)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值