初测vgg16

来源:互联网 发布:android chroot linux 编辑:程序博客网 时间:2024/05/29 06:27
# coding: utf-8import matplotlib.pyplot as pltimport numpy as npimport tensorflow as tffrom six.moves import xrange# get_ipython().magic(u'matplotlib inline')train_dataset = np.load('/home/sys-04/vgg/train_dataset.npy')train_labels = np.load('/home/sys-04/vgg/train_labels.npy')test_dataset = np.load('/home/sys-04/vgg/test_dataset.npy')test_labels = np.load('/home/sys-04/vgg/test_labels.npy')map = {key: val for key, val in enumerate('hs')}def plot_check(matrix, key):    plt.imshow(matrix)    plt.show()    print('the picture should be  ', map[key])    return None"""length = test_dataset.shape[0]-1for _ in range(10):    index = np.random.randint(length)    plot_check(test_dataset[index,:,:], test_labels[index]) """image_height = 2000image_width = 1600num_channels = 1num_labels = 2x = tf.placeholder(tf.float32, [None, 2000, 1600,1])y_ = tf.placeholder(tf.float32, shape=[None, 2])# 卷积神经网络需要四维的数据,one-hot的标签def reformat(dataset, labels):    dataset = dataset.reshape((-1, image_height, image_width, num_channels)).astype(np.float32)    labels = (np.arange(num_labels) == labels[:, None]).astype(np.float32)    return dataset, labelstrain_dataset, train_labels = reformat(train_dataset, train_labels)test_dataset, test_labels = reformat(test_dataset, test_labels)#mean = tf.constant([123.68, 116.779, 103.939], dtype=tf.float32, shape=[1, 1, 1, 3])#x = x - meanprint(train_dataset.shape, train_labels.shape)print(test_dataset.shape, test_labels.shape)sess = tf.InteractiveSession()# dataset = tf.placeholder(tf.float32, shape = [None, image_height, image_width, 1])# labels = tf.placeholder(tf.float32, shape = [None, 2])def weight_variable(shape, name):    initial = tf.truncated_normal(shape, stddev=0.1, name=name)    return tf.Variable(initial)def bias_variable(shape, name):    initial = tf.constant(0.1, shape=shape, name=name)    return tf.Variable(initial)def conv2d(x, W, name):    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME', name=name)def max_pool_2x2(x, name):    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)# 构建计算图# conv-pool-1with tf.name_scope('conv-pool-1-1'):    W_conv1 = weight_variable([3, 3, 1, 64], 'W_conv1')    b_conv1 = bias_variable([64], 'b_conv1')    h_conv1 = tf.nn.relu(conv2d(x, W_conv1, 'h_conv1') + b_conv1)with tf.name_scope('conv-pool-1-2'):    W_conv1 = weight_variable([3, 3, 64, 64], 'W_conv1')    b_conv1 = bias_variable([64], 'b_conv1')    h_conv1 = tf.nn.relu(conv2d(h_conv1, W_conv1, 'h_conv1') + b_conv1)    h_pool1 = max_pool_2x2(h_conv1, 'h_pool1')# conv-pool-2with tf.name_scope('conv-pool-2-1'):    W_conv2 = weight_variable([3, 3, 64, 128], 'W_conv2')    b_conv2 = bias_variable([128], 'b_conv2')    h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2, 'h_conv2') + b_conv2)with tf.name_scope('conv-pool-2-2'):    W_conv2 = weight_variable([3, 3, 128, 128], 'W_conv2')    b_conv2 = bias_variable([128], 'b_conv2')    h_conv2 = tf.nn.relu(conv2d(h_conv2, W_conv2, 'h_conv2') + b_conv2)    h_pool2 = max_pool_2x2(h_conv2, 'h_pool2')# conv-pool-3with tf.name_scope('conv-pool-3-1'):    W_conv3 = weight_variable([3, 3, 128, 256], 'W_conv3')    b_conv3 = bias_variable([256], 'b_conv3')    h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3, 'h_conv3') + b_conv3)with tf.name_scope('conv-pool-3-2'):    W_conv3 = weight_variable([3, 3, 256, 256], 'W_conv3')    b_conv3 = bias_variable([256], 'b_conv3')    h_conv3 = tf.nn.relu(conv2d(h_conv3, W_conv3, 'h_conv3') + b_conv3)with tf.name_scope('conv-pool-3-3'):    W_conv3 = weight_variable([3, 3, 256, 256], 'W_conv3')    b_conv3 = bias_variable([256], 'b_conv3')    h_conv3 = tf.nn.relu(conv2d(h_conv3, W_conv3, 'h_conv3') + b_conv3)    h_pool3 = max_pool_2x2(h_conv3, 'h_pool3')# conv-pool-4with tf.name_scope('conv-pool-4-1'):    W_conv4 = weight_variable([3, 3, 256, 512], 'W_conv4')    b_conv4 = bias_variable([512], 'b_conv4')    h_conv4 = tf.nn.relu(conv2d(h_pool3, W_conv4, 'h_conv4') + b_conv4)with tf.name_scope('conv-pool-4-2'):    W_conv4 = weight_variable([3, 3, 512, 512], 'W_conv4')    b_conv4 = bias_variable([512], 'b_conv4')    h_conv4 = tf.nn.relu(conv2d(h_conv4, W_conv4, 'h_conv4') + b_conv4)with tf.name_scope('conv-pool-4-3'):    W_conv4 = weight_variable([3, 3, 512, 512], 'W_conv4')    b_conv4 = bias_variable([512], 'b_conv4')    h_conv4 = tf.nn.relu(conv2d(h_conv4, W_conv4, 'h_conv4') + b_conv4)    h_pool4 = max_pool_2x2(h_conv4, 'h_pool4')with tf.name_scope('conv-pool-5-1'):    W_conv5 = weight_variable([3, 3, 512, 512], 'W_conv4')    b_conv5 = bias_variable([512], 'b_conv4')    h_conv5 = tf.nn.relu(conv2d(h_pool4, W_conv5, 'h_conv4') + b_conv5)with tf.name_scope('conv-pool-5-2'):    W_conv5 = weight_variable([3, 3, 512, 512], 'W_conv4')    b_conv5 = bias_variable([512], 'b_conv4')    h_conv5 = tf.nn.relu(conv2d(h_conv5, W_conv5, 'h_conv4') + b_conv5)with tf.name_scope('conv-pool-5-3'):    W_conv5 = weight_variable([3, 3, 512, 512], 'W_conv4')    b_conv5 = bias_variable([512], 'b_conv4')    h_conv5 = tf.nn.relu(conv2d(h_conv5, W_conv5, 'h_conv4') + b_conv5)    h_pool5 = max_pool_2x2(h_conv5, 'h_pool4')"""# 全链接层with tf.name_scope('Densely-Layer'):    W_fc1 = weight_variable([125 * 100 * 256, 4096], 'W_fc1')    b_fc1 = bias_variable([4096], 'b_fc1')    h_pool4_flat = tf.reshape(h_pool5, [-1, 125 * 100 * 256])    h_fc1 = tf.nn.relu(tf.matmul(h_pool4_flat, W_fc1) + b_fc1, name='h_fc1')with tf.name_scope('Densely-Layer'):    W_fc1 = weight_variable([4096, 4096], 'W_fc1')    b_fc1 = bias_variable([4096], 'b_fc1')    h_fc1 = tf.nn.relu(tf.matmul(h_fc1, W_fc1) + b_fc1, name='h_fc1')with tf.name_scope('Densely-Layer'):    W_fc1 = weight_variable([4096, 1000], 'W_fc1')    b_fc1 = bias_variable([1000], 'b_fc1')    h_fc1 = tf.nn.relu(tf.matmul(h_fc1, W_fc1) + b_fc1, name='h_fc1')"""with tf.name_scope('fc1') as scope:            shape = int(np.prod(h_pool5.get_shape()[1:]))            fc1w = tf.Variable(tf.truncated_normal([shape, 4096],                                                         dtype=tf.float32,                                                         stddev=1e-1), name='weights')            fc1b = tf.Variable(tf.constant(1.0, shape=[4096], dtype=tf.float32),                                 trainable=True, name='biases')            pool5_flat = tf.reshape(h_pool5, [-1, shape])            fc1 = tf.nn.relu(tf.matmul(pool5_flat, fc1w) + fc1b, name='h_fc1')with tf.name_scope('fc2') as scope:            fc2w = tf.Variable(tf.truncated_normal([4096, 4096],                                                         dtype=tf.float32,                                                         stddev=1e-1), name='weights')            fc2b = tf.Variable(tf.constant(1.0, shape=[4096], dtype=tf.float32),                                 trainable=True, name='biases')            fc2 = tf.nn.relu(tf.matmul(fc1, fc2w) + fc2b, name='h_fc2')with tf.name_scope('fc3') as scope:            fc3w = tf.Variable(tf.truncated_normal([4096, 1000],                                                         dtype=tf.float32,                                                         stddev=1e-1), name='weights')            fc3b = tf.Variable(tf.constant(1.0, shape=[1000], dtype=tf.float32),                                 trainable=True, name='biases')            fc3 = tf.nn.relu(tf.matmul(fc2, fc3w) + fc3b, name='h_fc3')# dropout 处理过拟合keep_prob = tf.placeholder(tf.float32)h_fc1_drop = tf.nn.dropout(fc3, keep_prob)# 输出层with tf.name_scope('output'):    W_fc2 = weight_variable([1000, 2], 'W_fc2')    b_fc2 = bias_variable([2], 'b_fc2')    y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2# 训练模型,评估模型# 交叉熵,计算损失函数cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)  # 优化器降低损失# tf.argmax(y_conv, 1)最有可能的分类,tf.argmax(labels,1)真实的标签 ,得到一组bool的列表correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))# 转换为数字,然后求均值。估计模型的准确度accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))  # 计算准确度sess.run(tf.global_variables_initializer())print('initialized')train_batch_size = 20test_batch_size = 10for i in xrange(801):    print('step %s' % i)    offset = (i * train_batch_size) % (train_labels.shape[0] - train_batch_size)    xs = train_dataset[offset:(offset + train_batch_size), :, :, :]    ys = train_labels[offset:(offset + train_batch_size), :]    feed_dict = {x: xs, y_: ys, keep_prob: 0.5}    if i % 10 == 0:        train_accuracy = accuracy.eval(feed_dict=feed_dict)        print("step %d, training accuracy %g" % (i, train_accuracy))    train_step.run(feed_dict=feed_dict)

按照VGG16的2-2-3-3-3-3套路,自己瞎写的vgg16,刚开始把batch_size设20都训练不了,超级耗内存,然后设成了10,step100在0.7-1之间浮动,超级慢。
image_height和image_width太大了?一般好像224。
还是数据集预处理没弄好,还是按照之前的办法对原始图片进行的预处理,出来的数据集太大了,占空间。这次只拿了两个classes做实验,生成的数据集太大了,没办法。下次试试TFRecord好不好使。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 二周宝宝便秘怎么办 孩子发烧不吃药怎么办 3岁宝宝不喝药怎么办 婆婆老师说孩子怎么办 初生婴儿不吃药怎么办 讨厌婆婆带孩子怎么办 孩子写字手没劲怎么办 孩子一口药不吃怎么办 小孩不爱喝药怎么办 小孩恶心想吐怎么办 孩子和婆婆不好怎么办 孩子吃东西不消化吐怎么办 小孩吃东西吐了怎么办 儿童吃饭容易吐怎么办 宝宝吃饭会吐怎么办 小孩咳嗽还呕吐怎么办 咳嗽严重到呕吐怎么办 幼儿园中班不会写字怎么办 胃难受吐了怎么办 小孩不肯学写字怎么办 孩子不爱穿内裤怎么办 孩子长期不吃肉怎么办 小孩子不吃肉怎么办呢? 一年级小孩写字慢怎么办 听障碍放弃了怎么办 宝宝开始写字该怎么办 恢复的文档乱码怎么办 小孩做作业磨蹭怎么办 宝宝用左手写字怎么办 小孩动作太慢怎么办 幼儿园不去上学怎么办 嫌弃婆婆带孩子怎么办 孩子写字特别慢怎么办 幼儿园孩子不愿写字怎么办 孩子不愿用力写字怎么办 老公得了懒癌怎么办 太懒不想上班怎么办 写字太多手臂痛怎么办 小孩读书务工证怎么办 一年级孩子撕书怎么办 孩子上幼儿园不适应怎么办