初测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好不好使。
阅读全文
0 0
- 初测vgg16
- VGG16
- [vgg16]vgg16相关文件
- caffe-vgg16
- VGG16结构图
- vgg16构造模型图
- Docker-tensorflow跑VGG16
- Residual Squeeze VGG16
- vgg16构造模型图
- 利用TensorFlow实现VGG16
- 手码VGG16网络
- pvanet-coco-vgg16
- vgg16模型进行图片预测
- fcn import vgg16 weights error
- 从keras看VGG16结构图
- vgg16 on keras for tensorflow
- VGG16预训练学习笔记
- vgg16测试模型的实现
- IDA sp-analysis failed 不能F5的 解决方案
- 程序包databinding不存在的各种坑。
- Activiti工作流学习总结
- 直方图
- MATLAB数字图像处理(3)滤波
- 初测vgg16
- 一个男生如此深情却与我无关
- wxWidgets+CodeBlocks搭建环境
- c++拷贝构造函数(深拷贝,浅拷贝)详解
- linux下C实现客户端服务器通信框架
- 手机端下滑时隐藏标题栏
- 无法实现接口成员,因为它不是公共的-----------interface
- MOOC清华《VC++面向对象与可视化程序设计》第1章:空白窗口的创建(空项目)
- 反编译Unity3d 中的dll文件