Neural Networks and Deep Learning-Dog Classification

1.准备工作

数据下载链接:http://www.cs.toronto.edu/~kriz/cifar.html

  • 该数据库有60000张32*32大小的彩色图片,其中50000张是训练数据,10000张是测试数据
  • 数据库中训练集被分为5个小文件,每个文件有10000张图片用于训练
  • 数据库中测试集被分为1个小文件,1个小文件有10000张图片用于测试
  • 其中50000张训练图片分为10个类型,也就是说每种类型的训练图片有5000张
  • 共有0-9(airplane-truck)十种图片

2.加载数据

load_matrix.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from PIL import Image
import numpy as np
import random


# 加载数据
def unpickle(file_path):
    import cPickle
    with open(file_path, 'rb') as fo:
        dc = cPickle.load(fo)
    return dc


# 生成训练样本和样本标签
def get_matrix(test_num):
    dc = unpickle("data_batch_1")
    count = len(dc['labels'])
    print 'train_set_x_count:', count
    train_set_x = []
    train_set_y = []
    num = 0
    for cn in range(0, count):
        if dc['labels'][cn] == 5:
            train_set_x += [dc['data'][cn]]
            train_set_y += [1]
            # num += 1
            if num == 500:
                break
    num = 0
    for cn in range(0, count):
        if dc['labels'][cn] != 5:
            train_set_x += [dc['data'][cn]]
            train_set_y += [0]
            # num += 1
            if num == 250:
                break
    train_set_x = np.array(train_set_x).T / 255.0  # 图片维度是32*32*3=3072
    train_set_y = np.array(train_set_y).reshape((1, count))

    # 生成测试样本和样本标签
    dc = unpickle("test_batch")
    count = len(dc['labels'])
    print 'test_set_x_count:', count
    test_set_x = []
    test_set_y = []
    num = 0
    start = random.randint(0, count - 1)
    for cn in range(start, count):
        if dc['labels'][cn] == 5:
            test_set_x += [dc['data'][cn]]
            test_set_y += [1]
        elif dc['labels'][cn] != 5:
            # print dc['data'][cn]
            test_set_x += [dc['data'][cn]]
            test_set_y += [0]
        num += 1
        if num == test_num:
            break
    test_set_x = np.array(test_set_x).T / 255.0  # 图片维度是32*32*3=3072
    test_set_y = np.array(test_set_y).reshape((1, test_num))

    print 'train_set_x: ', train_set_x.shape  # (3072L, 209L)
    print 'train_set_y: ', train_set_y.shape  # (1L, 209L)
    print 'test_set_x: ', test_set_x.shape  # (3072L, 50L)
    print 'test_set_y: ', test_set_y.shape  # (1L, 50L)

    return train_set_x, train_set_y, test_set_x, test_set_y


if __name__ == '__main__':
    get_matrix(1)
    # dc = unpickle("data_batch_1")
    print '以下代码是将像素值转换为图片并保存到本地'.decode('utf-8')
    dc = unpickle("test_batch")
    r = 32  # x坐标  通过对txt里的行数进行整数分解
    c = 32  # y坐标  x*y = 行数
    count = len(dc['labels'])
    num = 1
    for cn in range(0, count):
        if dc['labels'][cn] == 5:
            x = dc['data'][cn].reshape(3, 32, 32)
            im = Image.new("RGB", (r, c))  # 创建图片
            for i in range(0, r):
                for j in range(0, c):
                    im.putpixel((i, j), (x[0][i][j], x[1][i][j], x[2][i][j]))
            im.save("dog/" + str(num) + ".png", "png")
            num = num + 1
            if num == 10:
                break

       

3.分类计算

application.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
import load_matrix


# 1 准备数据集
def load_data(test_num):
    # train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()

    # 2 对数据集进行矩阵变换
    train_set_x_orig = np.random.randn(12288, 209)
    train_set_y = np.random.randn(1, 209)
    test_set_x_orig = np.random.randn(12288, 50)
    test_set_y = np.random.randn(1, 50)

    print ("train_set_x shape: " + str(train_set_x_orig.shape))
    print ("train_set_y shape: " + str(train_set_y.shape))
    print ("test_set_x shape: " + str(test_set_x_orig.shape))
    print ("test_set_y shape: " + str(test_set_y.shape))

    # 3对样本数据进行归一化
    return load_matrix.get_matrix(test_num)
    # return train_set_x_orig, train_set_y, test_set_x_orig, test_set_y


# 4定义激活函数
def sigmoid(z):
    s = 1.0 / (1 + np.exp(-z))
    # s = np.tanh(z)
    return s


# 5初始化参数w和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


# 6定义正向和反向传播函数
def propagate(w, b, X, Y):
    m = X.shape[1]
    A = sigmoid(np.dot(w.T, X) + b)  # compute activation
    cost = -(np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A))) / m  # compute cost

    dw = np.dot(X, (A - Y).T) / m
    db = np.sum(A - Y) / m

    assert (dw.shape == w.shape)
    assert (db.dtype == float)
    cost = np.squeeze(cost)
    assert (cost.shape == ())

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

    return grads, cost


# 7定义循环迭代函数
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost=False):
    costs = []
    dw = 0
    db = 0
    for i in range(num_iterations):
        grads, cost = propagate(w, b, X, Y)

        dw = grads["dw"]
        db = grads["db"]

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

        if i % 100 == 0:
            costs.append(cost)

        # Print the cost every 100 training examples
        if print_cost and i % 100 == 0:
            print ("Cost after iteration %i: %f" % (i, cost))

    params = {"w": w,
              "b": b}

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

    return params, grads, costs


# 8进行预测
def predict(w, b, X):
    m = X.shape[1]
    Y_prediction = np.zeros((1, m))
    w = w.reshape(X.shape[0], 1)
    A = sigmoid(np.dot(w.T, X) + b)
    for i in range(A.shape[1]):
        Y_prediction[0, i] = 1 if A[0, i] > 0.5 else 0

    assert (Y_prediction.shape == (1, m))
    return Y_prediction


# 9定义模型
def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.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=False)
    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


if __name__ == '__main__':
    train_set_x, train_set_y, test_set_x, test_set_y = load_data(1)
    d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=2000, learning_rate=0.005,
              print_cost=True)
    print d['Y_prediction_test'][0]
    if d['Y_prediction_test'][0] == 1:
        print 'dog'
    else:
        print 'not dog'
    r = 32  # x坐标  通过对txt里的行数进行整数分解
    c = 32
    test_set_x = test_set_x * 255
    x = test_set_x.reshape(3, 32, 32)
    im = Image.new("RGB", (r, c))  # 创建图片
    for i in range(0, r):
        for j in range(0, c):
            im.putpixel((i, j), (int(x[0][i][j]), int(x[1][i][j]), int(x[2][i][j])))
    im.show()

 

 

posted @ 2017-10-17 17:02  桃源仙居  阅读(160)  评论(0)    收藏  举报