CNN猫狗识别

该博客介绍了如何运用卷积神经网络(CNN)训练一个猫狗分类模型。包括了训练脚本(train.py)、数据集处理(dataset.py)以及预测脚本(predict.py),并验证了模型的识别效果。
摘要由CSDN通过智能技术生成

train.py

import dataset
import tensorflow as tf
import time
from datetime import timedelta
import math
import random
import numpy as np
# conda install --channel https://conda.anaconda.org/menpo opencv3
#Adding Seed so that random initialization is consistent
from numpy.random import seed
seed(10)
from tensorflow import set_random_seed
set_random_seed(20)

#一次迭代32张图片
batch_size = 32

#Prepare input data
classes = ['dogs','cats']
num_classes = len(classes)

#数值可修改
# 20% of the data will automatically be used for validation
#20%做验证 80%做训练
validation_size = 0.2
#图片输入大小64*64
img_size = 64
num_channels = 3
train_path='D:/hh/twst/test/training_data'

# We shall load all the training and validation images and labels into memory using openCV and use that during training
data = dataset.read_train_sets(train_path, img_size, classes, validation_size=validation_size)


print("Complete reading input data. Will Now print a snippet of it")
print("Number of files in Training-set:\t\t{}".format(len(data.train.labels)))
print("Number of files in Validation-set:\t{}".format(len(data.valid.labels)))



session = tf.Session()
x = tf.placeholder(tf.float32, shape=[None, img_size,img_size,num_channels], name='x')

## labels
y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')
y_true_cls = tf.argmax(y_true, dimension=1)



##Network graph params 网络结构 值可以改
filter_size_conv1 = 3 
num_filters_conv1 = 32

filter_size_conv2 = 3
num_filters_conv2 = 32

filter_size_conv3 = 3
num_filters_conv3 = 64
  
#全连接  将卷积的结果映射成1024个特征
fc_layer_size = 1024

#权重参数 随机取一个值
def create_weights(shape):
    return tf.Variable(tf.truncated_normal(shape, stddev=0.05))

#偏置参数
def create_biases(size):
    return tf.Variable(tf.constant(0.05, shape=[size]))


#创建卷积层 
def create_convolutional_layer(input,
               num_input_channels, 
               conv_filter_size,        
               num_filters):  
    
    ## We shall define the weights that will be trained using create_weights function. 3 3 3 32
    weights = create_weights(shape=[conv_filter_size, conv_filter_size, num_input_channels, num_filters])
    ## We create biases using the create_biases function. These are also trained.
    biases = create_biases(num_filters)

    ## Creating the convolutional layer 执行一次卷积
    layer = tf.nn.conv2d(input=input,
                     filter=weights,
                     strides=[1, 1, 1, 1],
                     padding='SAME')

    layer += biases
    
	#激活
    layer = tf.nn.relu(layer)
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值