(四)tensorflow--猫狗分类
猫狗数据集介绍
对kaggle中的猫狗数据集使⽤Alexnet⽹络进⾏训练,该数据集包括25000张训练图⽚,12500张测试图⽚,包括猫和狗两种图⽚。代码整体介绍
1.alexnet.py:定义alexnet⽹络
2.datagenerator.py: 对数据集做预处理,定义输⼊的⽅式
3.validate_image.py:对图像进⾏测试
4.main.py:主函数,在训练集上训练
alexnet.py
import tensorflow as tf
def alexnet(x,keep_prob,num_class):
#conv1
with tf.name_scope('conv1') as scope:
kernel = tf.uncated_normal([11,11,3,96],dtype=tf.float32,stddev=1e-1),name='weights')
conv = v2d(x,kernel,[1,4,4,1],padding='SAME')
bias = tf.stant(0.0,shape=[96],dtype=tf.float32),trainable=True,name='bias')
bias = tf.nn.bias_add(conv,bias)
conv1 = lu(bias,name=scope)
#lr1
with tf.name_scope('lrn1') as scope:
lrn1 = tf.nn.local_respon_normalization(conv1,alpha=1e-4,beta=0.75,depth_radius=2,bias=2.0)
#pool1
with tf.name_scope('pool1') as scope:
驻村第一书记职责
pool1 = tf.nn.max_pool(lrn1,ksize=[1,3,3,1],strides=[1,2,2,1],padding='VALID')
湖南有什么山#conv2
with tf.name_scope('conv2') as scope:
pool1_groups = tf.split(axis=3,value=pool1,num_or_size_splits=2)
kernel = tf.uncated_normal([5,5,48,256],dtype=tf.float32,stddev=1e-1),name='weights')
kernel_groups = tf.split(axis=3,value=kernel,num_or_size_splits=2)
conv_up = v2d(pool1_groups[0],kernel_groups[0],[1,1,1,1],padding='SAME')
conv_down = v2d(pool1_groups[1],kernel_groups[1],[1,1,1,1],padding='SAME')
bias = tf.stant(0.0,shape=[256],dtype=tf.float32),trainable=True,name='bias')
bias_groups = tf.split(axis=0,value=bias,num_or_size_splits=2)
bias_up = tf.nn.bias_add(conv_up,bias_groups[0])
bias_down = tf.nn.bias_add(conv_down,bias_groups[1])
bias = tf.concat(axis=3,values=[bias_up,bias_down])
conv2 = lu(bias,name=scope)
#lrn2
with tf.name_scope('lrn2') as scope:
lrn2 = tf.nn.local_respon_normalization(conv2,alpha=1e-4,beta=0.75,depth_radius=2,bias=2.0)
#pool2
with tf.name_scope('pool2') as scope:
pool2 = tf.nn.max_pool(lrn2,ksize=[1,3,3,1],strides=[1,2,2,1],padding='VALID')
#conv3
with tf.name_scope('conv3') as scope:
茭白炒肉的做法
kernel = tf.uncated_normal([3,3,256,384],dtype=tf.float32,stddev=1e-1),name='weights')
conv = v2d(pool2,kernel,[1,1,1,1],padding='SAME')
bias = tf.stant(0.0,shape=[384],dtype=tf.float32),trainable=True,name='bias')
bias = tf.nn.bias_add(conv,bias)
conv3 = lu(bias,name=scope)
with tf.name_scope("conv4") as scope:
conv3_groups = tf.split(axis=3,value=conv3,num_or_size_splits=2)
kernel = tf.uncated_normal([3,3,192,384],dtype=tf.float32,stddev=1e-1),name='weights')
kernel_groups = tf.split(axis=3,value=kernel,num_or_size_splits=2)
conv_up = v2d(conv3_groups[0],kernel_groups[0],[1,1,1,1],padding="SAME")
conv_down = v2d(conv3_groups[1],kernel_groups[1],[1,1,1,1],padding="SAME")
bias = tf.stant(0.0,shape=[384],dtype=tf.float32),trainable=True,name='bias')
香港攻略自由行bias_groups = tf.split(axis=0,value=bias,num_or_size_splits=2)
bias_up = tf.nn.bias_add(conv_up,bias_groups[0])
bias_down = tf.nn.bias_add(conv_down,bias_groups[1])
checkpoint_path = './checkpoints/'
file_name_of_class = ['cat','dog']
image_format = "jpg"
train_datat_paths="/home/datat/kaggle/train/"
#训练数据预处理
train_image_paths = []
train_labels = []
train_image_paths = np.array(glob.glob(train_datat_paths+'*.'+image_format)).tolist()
print("train_image_length:",len(train_image_paths))
for image_path in train_image_paths:
image_file_name = image_path.split('/')[-1]
for i in range(num_class):
if file_name_of_class[i] in image_file_name:
train_labels.append(i)
break
#调⽤⽣成器
train_data = ImageDataGenerator(
images = train_image_paths,
labels = train_labels,
batch_size = train_batch_size,
num_class = num_class,
image_format = image_format,
shuffle = True)
#定义迭代器
print(train_data.data.output_types,train_data.data.output_shapes)
with tf.name_scope("input"):
train_iterator = Iterator.from_structure(train_data.data.output_types,train_data.data.output_shapes)
training_initalizer = train_iterator.make_initializer(train_data.data)
train_next_batch = _next()
x = tf.placeholder(tf.float32,[None,227,227,3])
y = tf.placeholder(tf.float32,[None,num_class])
keep_prob = tf.placeholder(tf.float32)
#定义alexnet⽹络
logits = alexnet(x,keep_prob,num_class)
#定义vgg⽹络
#x = size_images(x,format_size)
#logits = vgg16(x,num_class,isTrain=True,keep_prob=0.6)
with tf.name_scope("loss"):
loss_op = tf.reduce_softmax_cross_entropy_with_logits_v2(logits=logits,labels=y))
with tf.name_scope('optimizer'):
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)
train_prediction = tf.nn.softmax(logits)
init = tf.global_variables_initializer()
感恩结尾
'''
#tensorboard
tf.summary.scalar('loss',loss_op)
merged_summary = _all()
给书包减肥writer = tf.summary.FileWriter(filewriter_path)
'''
saver = tf.train.Saver()
train_batches_per_epoch = int(np.floor(train_data.data_size/train_batch_size))
金朝都城
print(train_data.data_size)
print(train_batches_per_epoch)
with tf.Session() as ss:
ss.run(init)
#writer.add_aph)
print("{}: ".w()))穿越人海找到你
print("{}: openning tensorboard at --logdir{}".w(),filewriter_path))
for epoch in range(num_epochs):
ss.run(training_initalizer)
print("{}:epoch number:{} start".w(),epoch+1))
for step in range(500):
img_batch,label_batch = ss.run(train_next_batch)
loss,_,predictions = ss.run([loss_op,train_op,train_prediction],feed_dict={x:img_batch,y:label_batch,keep_prob:dropout_rate}) if step % display_step == 0:
print("{}:loss={}".w(),loss))
print("accuracy = {}".format(accuracy(predictions,label_batch)))
#s = ss.run(merged_summary,feed_dict={x:img_batch,y:label_batch,keep_prob:1.})
#writer.add_summary(s,epoch*train_batches_per_epoch+step)
#save model
print("{}:saving checkpoint ".w()))
checkpoint_name = os.path.join(checkpoint_path,'model_epoch' + str(epoch+1)+'.ckpt')