Skip to content

Instantly share code, notes, and snippets.

@shubham7298
Created February 9, 2019 17:11
Show Gist options
  • Save shubham7298/4eb161078018a90902b9a91bcd52da37 to your computer and use it in GitHub Desktop.
Save shubham7298/4eb161078018a90902b9a91bcd52da37 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
import os
import matplotlib.pyplot as plt
from scipy.spatial import distance
folder='cats'
f=os.listdir(folder)
cats=[]
dogs=[]
train_set=np.array([])
y=np.array([0,0,0,0,0,0,1,1,1,1,1,1,1])
one = np.ones(10000)
for i in f:
img=cv2.imread(folder+'/'+i,0)
img=cv2.resize(img,(100,100))
img=img.flatten()
img=np.reshape(img,(10000,1))
train_set=np.append(train_set,img)
cats.append(img)
plt.show()
folder='dogs'
f=os.listdir(folder)
for i in f:
img=cv2.imread(folder+'/'+i,0)
img=cv2.resize(img,(100,100))
img=img.flatten()
img=np.reshape(img,(10000,1))
train_set=np.append(train_set,img)
dogs.append(img)
train_set=np.reshape(train_set,(13,10000))
X=train_set
#==============================================================
w=0
b=0
cos=[]
e=[]
def init(s):
global w,b
w=np.zeros((s,1))
b=0
return w,b
def sigmoid(x):
return (1/(1+np.exp(-x)))
def forward():
z=np.dot(X,w)+b
a=sigmoid(z)
return a
w,b=init(X.shape[1])
print(X.shape)
print(y.shape)
y=y.reshape(13,1)
lr=0.009
def lo():
a=forward()
m=X.shape[0]
#cost=(-1/m)*np.sum(y*np.log(a)+(1-y)*np.log(1-a))
dw=(1/m)*np.dot(X.T,(a-y)) #dw is dL/dw
db=(1/m)*np.sum(a-y) #db is dL/dz
return dw,db#,cost
for i in range(50000):
dw,db=lo()
#cos.append(cost)
e.append(i)
w=w-dw*lr
b=b-db*lr
print("Trained!!",w.shape,b)
img=cv2.imread('test.jpeg',0)
img=cv2.resize(img,(100,100))
img=img.flatten()
img=np.reshape(img,(10000,1))
print(img.shape)
print(np.dot(img.T,w)+b)
#output => [[-310346.95084615]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment