基于卷积神经网络的猫狗识别代码
时间: 2023-09-14 16:09:41 浏览: 58
以下是一个基于卷积神经网络的猫狗识别的Python代码示例,基于TensorFlow框架实现:
```python
import tensorflow as tf
import numpy as np
import os
import cv2
# 定义模型
def model_fn(features, labels, mode):
# 输入层
input_layer = tf.reshape(features["x"], [-1, 128, 128, 3])
# 卷积层1
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu)
# 池化层1
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
# 卷积层2
conv2 = tf.layers.conv2d(
inputs=pool1,
filters=64,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu)
# 池化层2
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
# 全连接层
pool2_flat = tf.reshape(pool2, [-1, 32 * 32 * 64])
dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
dropout = tf.layers.dropout(inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)
# 输出层
logits = tf.layers.dense(inputs=dropout, units=2)
predictions = {
"classes": tf.argmax(input=logits, axis=1),
"probabilities": tf.nn.softmax(logits, name="softmax_tensor")
}
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
eval_metric_ops = {
"accuracy": tf.metrics.accuracy(labels=labels, predictions=predictions["classes"])
}
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
# 加载数据
def load_data(data_dir):
images = []
labels = []
for file in os.listdir(data_dir):
if file.endswith(".jpg"):
file_path = os.path.join(data_dir, file)
image = cv2.imread(file_path)
image = cv2.resize(image, (128, 128))
images.append(image)
if "cat" in file:
labels.append(0)
elif "dog" in file:
labels.append(1)
return np.array(images), np.array(labels)
# 主函数
def main(argv):
# 加载训练数据和测试数据
train_images, train_labels = load_data("train")
test_images, test_labels = load_data("test")
# 创建Estimator
classifier = tf.estimator.Estimator(model_fn=model_fn)
# 创建输入函数
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": train_images},
y=train_labels,
batch_size=100,
num_epochs=None,
shuffle=True)
test_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": test_images},
y=test_labels,
num_epochs=1,
shuffle=False)
# 训练模型
classifier.train(input_fn=train_input_fn, steps=1000)
# 评估模型
eval_result = classifier.evaluate(input_fn=test_input_fn)
print("Test set accuracy: {accuracy:0.3f}".format(**eval_result))
if __name__ == "__main__":
tf.app.run(main=main)
```
这个代码示例中,我们使用了一个包含两个卷积层、两个池化层和一个全连接层的卷积神经网络模型来进行猫狗识别。我们使用了128 * 128的图像作为输入,并使用了随机梯度下降进行优化。我们使用了一个包含训练集和测试集的数据集来进行训练和测试。在训练结束后,我们评估了模型在测试集上的准确率。
阅读全文