Tensorflow和Caffe对比实验代码

caffe的网络配置文件:train_val_lenet.prototxt

name: "LeNet"
layer {
  name: "mnist"
  type: "ImageData"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  #transform_param {
  #  scale: 0.00390625
  #}
  image_data_param {
    source: "/home/dl/caoyi/MNIST/train/label.txt"
    root_folder: "/home/dl/caoyi/MNIST/train/"
    batch_size: 64
  }
}
layer {
  name: "mnist"
  type: "ImageData"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  #transform_param {
  #  scale: 0.00390625
  #}
  image_data_param {
    source: "/home/dl/caoyi/MNIST/query/label.txt"
    root_folder: "/home/dl/caoyi/MNIST/query/"
    batch_size: 10
  }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 20
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "drop1"
  type: "Dropout"
  bottom: "ip1"
  top: "ip1"
  dropout_param {
    dropout_ratio: 0.5
  }
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

tensorflow的网络配置文件:layer.py

import tensorflow as tf

def get_weight(shape):
	w = tf.Variable(tf.truncated_normal(shape,stddev=0.1)) 
	return w

def get_bias(shape): 
	b = tf.Variable(tf.zeros(shape))  
	return b

def network(data, label):

    #input data
    print('data: %s\n' % data)
    print('label: %s\n' % label)

    #conv1
    num_output = 20
    kernel_size = 5
    stride = 1
    conv1 = tf.nn.conv2d(data, get_weight([kernel_size,kernel_size,1,num_output]), 
        strides=[1,stride,stride,1], padding='SAME')
    print('conv1: %s\n' % conv1)

    #relu1
    relu1 = tf.nn.relu(tf.nn.bias_add(conv1, get_bias([num_output])))
    print('relu1: %s\n' % relu1)

    #pool1
    kernel_size = 2
    stride = 2
    pool1 = tf.nn.max_pool(relu1, ksize=[1,kernel_size,kernel_size,1], 
        strides=[1,stride,stride,1,], padding='SAME')
    print('pool1: %s\n' % pool1)

    #conv2
    num_output = 50
    kernel_size = 5
    stride = 1
    conv2 = tf.nn.conv2d(pool1, get_weight([kernel_size,kernel_size,20,num_output]), 
        strides=[1,stride,stride,1], padding='SAME')
    print('conv2: %s\n' % conv2)

    #relu2
    relu2 = tf.nn.relu(tf.nn.bias_add(conv2, get_bias([num_output])))
    print('relu2: %s\n' % relu2)

    #pool2
    kernel_size = 2
    stride = 2
    pool2 = tf.nn.max_pool(relu2, ksize=[1,kernel_size,kernel_size,1], 
        strides=[1,stride,stride,1,], padding='SAME')
    print('pool2: %s\n' % pool2)

    #pool2 respahe
    pool2_shape = pool2.get_shape().as_list() 
    pool2_reshape_size = pool2_shape[1] * pool2_shape[2] * pool2_shape[3]
    pool2_reshape = tf.reshape(pool2, [pool2_shape[0], pool2_reshape_size]) 
    print('pool2_reshape: %s\n' % pool2_reshape)

    #fc1
    fc1_num_output = 500
    fc1 = tf.matmul(pool2_reshape, get_weight([pool2_reshape_size, fc1_num_output])) 
    print('fc1: %s\n' % fc1)

    #relu3
    relu3 = tf.nn.relu(fc1 + get_bias([fc1_num_output]))
    print('relu3: %s\n' % relu3)

    #dropout1
    dropout_ratio = 0.5
    dropout1 = tf.nn.dropout(relu3, dropout_ratio)
    print('dropout1: %s\n' % dropout1)

    #fc2
    fc2_num_output = 10
    fc2 = tf.matmul(dropout1, get_weight([fc1_num_output, fc2_num_output])) 
    print('fc2: %s\n' % fc2)

    #output
    output = fc2 + get_bias([fc2_num_output])
    print('output: %s\n' % output)

    return output

tensorflow的运行文件:main.py

import os
import cv2 as cv
import tensorflow as tf
import layer

STEPS=50000             #迭代次数
BATCH_SIZE=64           #训练批次
TRAIN_NUM=5000          #训练样本数量
TEST_NUM=1000           #测试样本数量
DISPLAY_ITER=100        #迭代多少次打印
TEST_ITER=5000          #迭代多少次测试
SNAPSHOT=20000          #迭代多少次保存

def train_lenet(train_path):

    #训练集
    list = os.listdir(train_path)
    train_data=[]
    train_label=[]
    
    for filename in list:
        filepath = '%s\\%s' % (train_path, filename)
        img = cv.imread(filepath, 0)
        if img is None:
            continue
        img = img / 255
        rows,cols = img.shape
        img = img.reshape([rows,cols,1])
        train_data.append(img)
        labels = [0] * 10
        labels[int(filename.split('_')[0])] = 1
        train_label.append(labels)#数据标签
 
    print('train data load!\n')

    x = tf.placeholder(tf.float32, shape=(BATCH_SIZE,28,28,1))
    y_ = tf.placeholder(tf.float32, shape=(BATCH_SIZE,10))
    y = layer.network(x, y_)

    global_step = tf.Variable(0, trainable=False)
    learning_rate = tf.train.exponential_decay(
        0.005,
        global_step,
        TRAIN_NUM/BATCH_SIZE,
        0.99,
        staircase=True)

    ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
    loss = tf.reduce_mean(ce) 
    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)

    saver = tf.train.Saver() 

    with tf.Session() as sess: 
        init_op = tf.global_variables_initializer() 
        sess.run(init_op) 

        for i in range(STEPS+1):
            start = (i*BATCH_SIZE) % TRAIN_NUM
            end = start + BATCH_SIZE

            if start > 5000 or end > 5000:
                continue
 
            _, loss_value, lr_value = sess.run([train_step,loss,learning_rate], 
                                            feed_dict={x:train_data[start:end], y_:train_label[start:end]})
 
            if i % DISPLAY_ITER == 0:
                print("[iter:{}] [lr:{}] [train loss:{}]".format(i, lr_value, loss_value))
 
            if i % SNAPSHOT == 0:
                saver.save(sess, './model_%d' % i)

def val_lenet(val_path):

    #测试集
    list = os.listdir(val_path)
    val_data=[]
    val_label=[]
 
    for filename in list:
        filepath = '%s\\%s' % (val_path, filename)
        img = cv.imread(filepath, 0)
        if img is None:
            continue
        img = img / 255
        rows,cols = img.shape
        img = img.reshape([rows,cols,1])
        val_data.append(img)
        labels = [0] * 10
        labels[int(filename.split('_')[0])] = 1
        val_label.append(labels)#数据标签
 
    print('test data load!\n')

    x = tf.placeholder(tf.float32, shape=(1000,28,28,1))
    y_ = tf.placeholder(tf.float32, shape=(1000,10))
    y = layer.network(x, y_)

    saver = tf.train.Saver()
    	
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

    while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state('./')
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)

                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                    accuracy_score = sess.run(accuracy, feed_dict={x:val_data,y_:val_label}) 
                    print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score))
                else:
                    print('No checkpoint file found')
                    return

if __name__ == '__main__':
    #train_lenet('E:\\[1]Paper\\Datasets\\MINST\\train')
    val_lenet('E:\\[1]Paper\\Datasets\\MINST\\query')

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值