NN

吴恩达 ---cat识别

import numpy as np
import matplotlib.pyplot as plt
import h5py

import imageio

from PIL import Image


# '''加载数据集'''
def load_dataset():
    train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:])
    train_set_y_orig = np.array(train_dataset["train_set_y"][:])

    test_dataset = h5py.File('datasets/test_catvnoncat.h5', 'r')
    test_set_x_orig = np.array(test_dataset["test_set_x"][:])
    test_set_y_orig = np.array(test_dataset['test_set_y'][:])

    classes = np.array(test_dataset["list_classes"][:])

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))

    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes


train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()

# 画出一个图像
index = 56
# plt.imshow(train_set_x_orig[index])
# plt.show()
print("y= "+str(train_set_y[:, index])+ ", it's a '"+ classes[np.squeeze(train_set_y[:, index])].decode('utf-8')+"' picture.")

m_train = train_set_y.shape[1]  # number of training examples
m_test = test_set_y.shape[1]  # number of test examples
num_px = train_set_x_orig.shape[1]  # (=height, =width of a training image

print("Number of training examples: "+str(m_train))
print("Number of test examples: "+str(m_test))
print("Height/Width of each image: "+str(num_px))

# Reshape the training and test examples
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T

print("Train_set_x_flatten shape: "+str(train_set_x_flatten.shape))
print("Test_set_x_flatten shape: "+str(test_set_x_flatten.shape))

# standardize
train_set_x = train_set_x_flatten / 255
test_set_x = test_set_x_flatten / 255


# sigmoid activation function
def sigmoid(z):
    s = 1/(1 + np.exp(-z))
    return s


# initialize w and b
def initialize_with_zeros(dim):
    w = np.zeros((dim, 1))
    b = 0

    assert(w.shape == (dim, 1))
    assert(isinstance(b, float) or isinstance(b, int))

    return w, b


# calculate cost, dw,db
def propagate(w, b, x, y):
    Z = np.dot(w.T, x)+b
    A = sigmoid(Z)

    m = x.shape[1]
    cost = (-1 / m) * np.sum(y * np.log(A) + (1-y) * (np.log(1-A)))

    dw = (1/m) * np.dot(x, (A-y).T)
    db = (1/m) * np.sum(A - y)
    cost = np.squeeze(cost)

    grads = {"dw": dw,
             "db": db}

    return grads, cost


# update w,b
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost=False):

    costs = []

    for i in range(num_iterations):
        grads, cost = propagate(w, b, X, Y)
        dw = grads.get("dw")
        db = grads.get("db")

        w -= (learning_rate * dw)
        b -= (learning_rate * db)

        if i % 100 == 0:
            costs.append(cost)
            if print_cost:
                print("Cost after iteration %i:  is : %f" % (i, cost))

    params = {"w": w,
              "b": b}
    grads = {"dw": dw,
             "db": db}

    return params, grads, costs


# predict function
def predict(w, b, X):
    w = w.reshape((X.shape[0], 1))
    z = np.dot(w.T, X) + b
    A = sigmoid(z)

    m = X.shape[1]
    y_predict = np.zeros((1, m))

    for i in range(A.shape[1]):
        y_predict[0, i] = 1 if A[0, i] > 0.5 else 0

    return y_predict


# combine whole function
def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=.5, print_cost=False):
    w, b = initialize_with_zeros(X_train.shape[0])

    parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)

    w = parameters["w"]
    b = parameters["b"]

    Y_prediction_test = predict(w, b, X_test)
    Y_prediction_train = predict(w, b, X_train)

    print("Train accuracy:{} %".format(100-np.mean(np.abs(Y_prediction_train- Y_train)) * 100))
    print("Test accuracy:{} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))

    d = {"costs": costs,
         "Y_prediction_test": Y_prediction_test,
         "Y_prediction_train": Y_prediction_train,
         "w": w,
         "b": b,
         "learning_rate": learning_rate,
         "num_iterations": num_iterations}

    return d


d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=1000, learning_rate=0.005, print_cost=True)

# 更换新图片进行测试
my_image = 'timg.jpg'
fname = 'D:/BaiduNetdiskDownload/'+my_image
image = np.array(imageio.imread(fname))
my_image = np.array(Image.fromarray(image).resize(size=(num_px, num_px))).reshape((1, num_px*num_px*3)).T
my_predicted_image = predict(d['w'], d['b'], my_image)

print('y = ' + str(np.squeeze(my_predicted_image)) + ', your algorithm predicts a \"'+classes[int(
    np.squeeze(my_predicted_image)), ].decode("utf-8") + '\" picture')
plt.imshow(image)
plt.show()
#
# costs = np.squeeze(d['costs'])
# plt.plot(costs)
# plt.ylabel('cost')
# plt.xlabel('iterations (per hundreds)')
# plt.title('Learning rate=' + str(d['learning_rate']))
# plt.show()

'''
 下面的代码用于比较不同的学习率
'''
# learning_rates = [0.01, 0.001, 0.0001]
#
# models = {}
# for i in learning_rates:
#     print("learning rate is: "+str(i))
#     models[str(i)] = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=1500, learning_rate=i,
#                            print_cost=False)
#     print('\n ------------------------------------\n')
#
# for i in learning_rates:
#     plt.plot(np.squeeze(models[str(i)]["costs"]), label=str(models[str(i)]["learning_rate"]))
#
# plt.ylabel('cost')
# plt.xlabel('iterations')
#
# legend = plt.legend(loc='upper center', shadow=True)
# frame = legend.get_frame()
# frame.set_facecolor('0.90')
# plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值