【发布时间】:2019-09-17 21:03:24
【问题描述】:
大家好,我想使用感知器对狗和猫进行分类,但出现了一些错误
首先我从训练集中取 20 张图像,10 只猫,然后 10 只狗,猫被标记为 0y_train.append(0),狗被标记为 1y_train.append(1)
x_train,y_train = [],[]
for i in range(10):
img = cv2.imread('C:\\Users\\Hi-XV\\Desktop\\dogs-vs-cats-redux-kernels-edition\\train\\cat.' + str(i) + '.jpg')
img = cv2.resize(img,(64,64))
x_train.append(img)
y_train.append(0)
img2 = cv2.imread('C:\\Users\\Hi-XV\\Desktop\\dogs-vs-cats-redux-kernels-edition\\train\\dog.' + str(i) + '.jpg')
img2 = cv2.resize(img,(64,64))
x_train.append(img2)
y_train.append(1)
我是这样处理的:
x_train = np.array(x_train)
y_train = np.array(y_train)
y_train = y_train.reshape(-1, 1)
x_train_flatten = x_train.reshape(x_train.shape[0], -1).T
x_train = x_train_flatten / 255
这是我的 sigmoid 函数总是返回 0 到 1 之间的值:
def sigmoid(self,z):
return 1/(1+np.exp(-z))
这是我的反向传播函数:
def propaganate(self,X,Y,w,b):
A = self.sigmoid(np.dot(w.T,X) +b)
m = X.shape[1]
dw = np.dot(X, (A - Y).T) / m
db = np.sum(A-Y)/m
cost = (-1 / m) * np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A))
return dw,db,cost
这是我的梯度下降的主要功能:
def optimize(self,learningRate=0.005,steps=2000):
X = self.x_train
Y = self.y_train
w = self.w
b = self.b
costs =[]
for i in range(steps):
dw,db,cost =self.propaganate(X,Y,w,b)
w = w - learningRate*dw
b = b - learningRate*db
if i%100 ==0:
costs.append(cost)
print('cost after %i: %f' %(i,cost))
return w,b
这是我的预测函数:
def predict(self,image):
w,b = self.optimize()
m = image.shape[1]
w = w.reshape((image.shape[0],-1))
Y_prediction = np.zeros((1,m))
A = self.sigmoid(np.dot(w.T,image)+b)
for i in range(A.shape[1]):
Y_prediction[0,i] =A[0,i]
print(Y_prediction)
return Y_prediction
最后我打电话给pct.predict(predict_imgs),它是这样记录的:
0 之后的成本:13.862944 100 后的成本:0.017974 200 之后的成本:0.011118 300 之后的成本:0.008078 400 之后的成本:0.006354 500 之后的成本:0.005242 600 后的成本:0.004465 700 之后的成本:0.003890 800 之后的成本:0.003447 900 后的成本:0.003096 1000 之后的成本:0.002810 1100 之后的成本:0.002573 1200 之后的成本:0.002373 1300 之后的成本:0.002202 1400 之后的成本:0.002054 1500 之后的成本:0.001926 1600 之后的成本:0.001812 1700 之后的成本:0.001711 1800 年后的成本:0.001621 1900 年后的成本:0.001540
所以成本似乎是正确的,因为它几乎是 0 但后来我预测了一张狗的图像,这就是我的做法:
predict_imgs = []
pd_img = cv2.imread('C:\\Users\\Hi-XV\\Desktop\\dogs-vs-cats-redux-kernels-
edition\\train\\dog.1.jpg')
pd_img = cv2.resize(pd_img,(64,64))
predict_imgs.append(pd_img)
predict_imgs = np.array(predict_imgs)
predict_imgs_flatten = predict_imgs.reshape(pd_img.shape[0],-1).T
predict_imgs = predict_imgs_flatten/255
pct.predict(predict_imgs)
这是它的记录方式:
[[0.47129622 0.47146358 0.47072547 0.46926181 0.46849233 0.4705466 0.4713464 0.47103178 0.47406489 0.47669844 0.47609287 0.47602436 0.47432492 0.46869344 0.4653232 0.46576656 0.46390416 0.46274703 0.46455358 0.46425507 0.46637787 0.46493939 0.46585933 0.46551723 0.46313767 0.46074716 0.45894883 0.45560602 0.45442201 0.45338179 0.45419183 0.45414762 0.45349525 0.45224447 0.45072343 0.45040515 0.44871289 0.44694917 0.44369839 0.44729202 0.44997111 0.44890832 0.44254292 0.43972149 0.4354109 0.43391902 0.43312538 0.43134105 0.42976022 0.42922733 0.42829998 0.42911856 0.42773902 0.42823065 0.4274165 0.42786264 0.42790718 0.42816487 0.42216149 0.41795934 0.41516696 0.41230804 0.41243036 0.41221888]]
我尝试了一张猫图片:
[[0.46602192 0.46570703 0.46540704 0.4669786 0.46794146 0.46773242 0.4684889 0.4683816 0.46921272 0.46943627 0.46954064 0.47158274 0.4749414 0.47375206 0.47201231 0.47086452 0.47094515 0.47293698 0.47381821 0.47411287 0.47467158 0.47491538 0.47760668 0.47640458 0.47514657 0.47359331 0.47391838 0.47318598 0.47173989 0.47296217 0.47173741 0.47185791 0.47241618 0.47475851 0.47406301 0.4755808 0.47666993 0.47613153 0.47499163 0.475437 0.47435883 0.47370117 0.47281707 0.47372429 0.47287648 0.47400302 0.47556063 0.47517845 0.47593115 0.47595672 0.47693075 0.47990405 0.47702912 0.47646767 0.47643149 0.47786475 0.47577853 0.47806219 0.4775023 0.47835029 0.47919827 0.48055778 0.48172249 0.48003663]]
与上面那张狗的图片几乎一样。这里有问题。 我需要帮助。 这是我的完整代码:
https://github.com/lanlehoang67/PerceptronDogCatClassification/blob/master/perceptron.py
这是数据集:
https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data
感谢您阅读本文。
【问题讨论】:
标签: python machine-learning neural-network classification perceptron