松鼠分类任务

-- coding: utf-8 --

“”"
@Time : 19-10-19 下午7:39
@Author : lei
@Site :
@File : squirrel_image.py
@Software: PyCharm
“”"

松鼠识别 0 1

from sklearn.linear_model import LogisticRegression
from sklearn.metrics import mean_squared_error
import tensorflow as tf
import numpy as np
import h5py

def load_dataset():
“”"
用于从两个.h5文件中分别加载训练数据和测试数据

Args:
Return:
    train_set_x_orig -- 原始训练数据集
    train_set_y -- 原始训练数据标签
    test_set_x_orig -- 原始测试数据集
    test_set_y -- 原始测试数据标签
    classes(cat/non-cat) -- 分类list
"""
train_dataset = h5py.File('./data/SquirrelRegonition/datasets/train_squirrel.h5', "r")
train_set_x_orig = np.array(train_dataset["train_set_x"][:])  # your train set features
train_set_y_orig = np.array(train_dataset["train_set_y"][:])  # your train set labels

test_dataset = h5py.File('./data/SquirrelRegonition/datasets/test_squirrel.h5', "r")
test_set_x_orig = np.array(test_dataset["test_set_x"][:])  # your test set features
test_set_y_orig = np.array(test_dataset["test_set_y"][:])  # your test set labels

classes = np.array(test_dataset["list_classes"][:])  # the list of 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]))

dataset = [train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes]
return dataset

datasets = load_dataset()
x_train = datasets[0]
y_train = np.reshape(datasets[1], [-1])
x_test = datasets[2]
y_test = np.reshape(datasets[3], [-1])

将label进行one-hot编码

y_train = tf.one_hot(y_train, depth=2)
y_test = tf.one_hot(y_test, depth=2)
epochs = 1000

使用卷积神经网络进行分类和预测

训练集数据 (784, 128, 128, 3) label (1, 784)

with tf.name_scope(“data”):
# 构造x和y
x = tf.placeholder(tf.float32, [None, 128, 128, 3])
y_true = tf.placeholder(tf.float32, [None, 2])

定义卷积层

with tf.name_scope(“conv1”):
# 5 × 5 三通道 64个
weight1 = tf.Variable(tf.truncated_normal([5, 5, 3, 32], stddev=0.1))
bias1 = tf.Variable(tf.truncated_normal([32], stddev=0.1))
# 图片使用2d
conv1 = tf.nn.relu(tf.nn.conv2d(x, weight1, strides=[1, 1, 1, 1], padding=“SAME”) + bias1)
# 使用最大池化 64 * 64 * 64 [None, 64, 64, 32]
pool1 = tf.nn.max_pool2d(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding=“VALID”)

定义第二层卷积层

with tf.name_scope(“conv2”):
weight2 = tf.Variable(tf.truncated_normal([3, 3, 32, 64], stddev=0.1))
bias2 = tf.Variable(tf.truncated_normal([64], stddev=0.1))
conv2 = tf.nn.relu(tf.nn.conv2d(pool1, weight2, strides=[1, 1, 1, 1], padding=“SAME”) + bias2)
# 32 * 32 * 128 [None, 32, 32, 64]
pool2 = tf.nn.max_pool2d(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding=“VALID”)

进行全连接层

with tf.name_scope(“correct1”):
# 将卷积后的结果进行reshape
pool2 = tf.reshape(pool2, [-1, 32 * 32 * 64])
keep_prob = tf.placeholder(tf.float32)
h_pool = tf.nn.dropout(pool2, keep_prob)
# 定义全连接
weight3 = tf.Variable(tf.truncated_normal([32 * 32 * 64, 1024], stddev=0.1))
bias3 = tf.Variable(tf.truncated_normal([1024], stddev=0.1))
# 使用relu激活函数 防止梯度消失
logit1 = tf.nn.relu(tf.nn.xw_plus_b(h_pool, weight3, bias3))

进行第二层hidden

with tf.name_scope(“correct2”):
# 进行dropout
h_logit1 = tf.nn.dropout(logit1, keep_prob=keep_prob)
weight4 = tf.Variable(tf.truncated_normal([1024, 128], stddev=0.1))
bias4 = tf.Variable(tf.truncated_normal([128], stddev=0.1))
logit2 = tf.nn.relu(tf.nn.xw_plus_b(h_logit1, weight4, bias4))

输出层

with tf.name_scope(“output”):
weight5 = tf.Variable(tf.truncated_normal([128, 2], stddev=0.1))
bias5 = tf.Variable(tf.truncated_normal([2], stddev=0.1))
y_predict = tf.nn.xw_plus_b(logit2, weight5, bias5)

进行交叉熵计算

with tf.name_scope(“entroxy”):
x_entroxy = tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict)
loss = tf.reduce_mean(x_entroxy)

进行训练

with tf.name_scope(“train_step”):
train_step = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)

求解正确率

with tf.name_scope(“acc”):
equal = tf.equal(tf.argmax(y_true, 1), tf.argmax(y_predict, 1))
accuracy = tf.reduce_mean(tf.cast(equal, tf.float32))

init_op = tf.global_variables_initializer()

with tf.Session() as sess:
# 初始化变量
sess.run(init_op)

for epoch in range(epochs):
    sess.run(train_step, feed_dict={x: x_train, y_true: y_train.eval(), keep_prob: 0.5})
    print("训练集的acc: {}".format(accuracy.eval(feed_dict={x: x_train, y_true: y_train.eval(), keep_prob: 0.5})))

逻辑回归已然爆炸

def log_predict(datasets):

x_train = datasets[0]

y_train = datasets[1]

x_test = datasets[2]

y_test = datasets[3]

x_train = np.reshape(x_train, [-1, 1281283])

x_test = np.reshape(x_test, [-1, 1281283])

print(x_train.shape)

lg = LogisticRegression(max_iter=2000, n_jobs=-1)

lg.fit(x_train, y_train)

y_predict = lg.predict(x_test)

score = lg.score(x_test, y_test)

print(score)

print(mean_squared_error(y_true=y_test, y_pred=y_predict))

if name == ‘main’:

datasets = load_dataset()

log_predict(datasets)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值