在Python中使用Perceptron的狗猫分类

问题描述 投票:0回答:1

嗨,伙计们我想用Perceptron对狗和猫进行分类,但我有一些错误首先我从训练集拍摄20张图片,10只猫然后10只狗,猫被标记为零y_train.append(0)和狗被标记为一个y_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.003890成本800:0.003447成本900:0.003096 1000后成本:1200后成本:0.002473成本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
1个回答
0
投票

感知器模型在图像分类方面不具备性能。您的数据不是线性可分的和高维的,从理论的角度来看,简单的Perceptron算法没有理由表现良好。通常,卷积神经网络用于对图像进行分类,或者至少使用多层感知器(即使它也不是非常高效)。

如果您意识到这一点,并且询问有关您的代码本身的问题,我可以深入研究您的代码。

© www.soinside.com 2019 - 2024. All rights reserved.