搜索
bottom↓
回复: 44

自己写的tensorflow实现的卷积神经网络NIN结构的代码

  [复制链接]

出0入0汤圆

发表于 2017-4-1 06:06:52 | 显示全部楼层 |阅读模式
开始神经网络的实验一个月了先前试验了传统卷积神经网络的结构在MNIST数据集上试验达到99.4%的test accuracy。
然后又试验了Network In Network (NIN)结构的卷积网络,在CIFAR-10数据集上有83%的test accuracy。 所有试验都
在tensorflow上用python实现。
使用CIFAR-10 训练的这个网络结构是以下这个样子的
32x32 image----
--->conv(5x5, 192)--->normaliz--->LRelu---->conv(1x1,160)-->normaliz--->LRelu---->conv(1x1,96)--->max_pool(2x2)--
-->conv(5x5, 192)--->normaliz--->LRelu---->conv(1x1,192)-->normaliz--->LRelu---->conv(1x1,192)--->max_pool(2x2)--
->conv(3x3, 192)--->normaliz--->LRelu---->conv(1x1,192)-->normaliz--->LRelu---->conv(1x1,10)--->global_avg_pool--
->softmax
这是一个9层的NIN结构的卷积网络
下图CIFAR-10数据集的train accuracy曲线


代码
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cPickle
import tensorflow as tf
import sys

def concatenate_kernel_map(kernel, kXSize, kYSize, outx, outy, padding):
  "kernel:  groups of kernel to display [xdim,ydim,1,batch]"
  "kXSize:  kernel x size"
  "kYSize:  kernel y size"
  "outx:    how many kernel patches in output kernel map x direction"
  "outy:    how many kernel patches in putput kernel map y direction"
  "padding: number of padding of zero patches for the last row"
  if padding!=0:
    ZeroPad     = tf.zeros([kXSize,kYSize,1,padding], dtype=tf.float32) #[5,5,1,4]
    KernelGroup = tf.concat([kernel, ZeroPad],3) #[5,5,1,36]
  else:
    KernelGroup = kernel
  print (KernelGroup.get_shape())
  KernelLine  = tf.split(KernelGroup, outy*outx, 3) #[5,5,1,1]

  k_map = tf.concat([tf.concat(KernelLine[0*outx:(0+1)*outx],0), tf.concat(KernelLine[1*outx:(1+1)*outx],0)], 1)
  for i in range(2,outy):
    k_map = tf.concat([k_map,tf.concat(KernelLine[i*outx:(i+1)*outx], 0)], 1)

  print (k_map.get_shape())
  k_map = tf.reshape(k_map, [1,kXSize*outx,kYSize*outy,1])
  return k_map

#input:   shape of a weights
#return:  initialized weight
def weight_variable(shape, std_value):
  initial = tf.truncated_normal(shape, dtype=tf.float32, stddev=std_value)
  return tf.Variable(initial)

def bias_variable(shape):
  initial = tf.constant(0., shape=shape)
  return tf.Variable(initial)


# input x and weight W
#return the conv result
def conv2d(x,W):
  return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding = 'SAME')

def max_pool_2x2(x):
  return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')

def lrelu(x, leak=0.2, name="lrelu"):
  with tf.variable_scope(name):
    f1 = 0.5 * (1 + leak)
    f2 = 0.5 * (1 - leak)
    return f1 * x + f2 * abs(x)


def mlpconv(x, x_channel, conv_k_size, conv_k_num, mlp1_k_num, mlp2_k_num, has_2x2_pooling, weight_std_value):
  'construct mlp convolution layer'
  '(conv+relu)-->(MLP+relu)-->(MLE+relu)'
  'x          : input image'
  'x_channel  : x input number of channel'
  'conv_k_size: kernel x,y dimmension. x and y are same, so this is a scalar'
  'conv_k_num : number of kernel'
  'mlp1_k_num : mlp1 layer number of 1x1 kernel'
  'mlp2_k_num : mlp2 layer number of 1x1 kernel'
  'has_2x2_pooling: if do max pooling to the output 1 is yes 0 is no'

  #conv layer weights
  W_conv1 = weight_variable([conv_k_size, conv_k_size ,x_channel, conv_k_num], weight_std_value)
  b_conv1 = bias_variable([conv_k_num])

  W_mlp1  = weight_variable([1, 1 ,conv_k_num, mlp1_k_num], weight_std_value)
  b_mlp1  = bias_variable([mlp1_k_num])

  W_mlp2  = weight_variable([1, 1 ,mlp1_k_num, mlp2_k_num], weight_std_value)
  b_mlp2  = bias_variable([mlp2_k_num])  
  #network cascade
  h  = conv2d(x, W_conv1) + b_conv1
  h  = tf.nn.local_response_normalization(h, depth_radius=5, bias=0.01 )
  h  = lrelu(h)

  h  = conv2d(h, W_mlp1) + b_mlp1
  h  = tf.nn.local_response_normalization(h, depth_radius=5, bias=0.01 )
  h  = lrelu(h)

  h  = conv2d(h, W_mlp2) + b_mlp2
  h  = tf.nn.local_response_normalization(h, depth_radius=5, bias=0.01 )
  h  = lrelu(h)
  #h  = tf.nn.dropout(h, 0.5)

  if has_2x2_pooling==1:   
    h  = max_pool_2x2(h)
  return h

def global_ave_pool(x, x_shape):
  'global average pooling the input x'
  'x       :input feature map [1,x,y,1]'
  'x_shape :should be [1,x,y,1]'
  return tf.nn.avg_pool(x, x_shape,[1,2,2,1], 'VALID')

def fc_relu_layer(x, in_length, out_length):
  w = weight_variable([in_length, out_length])
  b = bias_variable([out_length])

  y = tf.nn.relu(tf.matmul(x, w) + b)
  return y
def dropout_layer(x, rate):
  return tf.nn.dropout(x, rate)

def GetCIFARBatch(dataset, batch_size, pick_range):
  '''
  randomely get several image and label pairs from the dataset
  dataset:    dataset with format [image, label]
  batch_size: batch size
  return:     [image, label]
  '''
  import random
  image = dataset[0]
  label = dataset[1]  

  batch_image=[]
  batch_label=[]
  for i in range(batch_size):
    idxf = random.random()
    idxf = idxf*pick_range
    idxint = int(idxf)
    #print(idxint)
    batch_image.append(image[idxint])
    batch_label.append(label[idxint])

  batch_image = np.array(batch_image)
  batch_label = np.array(batch_label)
  return (batch_image, batch_label)
  

def LoadCIFARName(file_name):
  '''
  load CIFAT-10 data set
  file_name:  the cifar-10 data file path
  return:  [train_image, train_label]
      train_image: image in shape [10000,3,32,32]
      train_label: label of each image in shape [10000,10]. with each item
                   [0,0,0,0,0,0,0,1,0,0]
  '''
  fo = open(file_name,'rb')
  datadict = cPickle.load(fo)
  fo.close()
  train_image = datadict["data"]
  train_array = datadict['labels']
  train_image = train_image.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float")
  train_array = np.array(train_array)

  train_label=[]
  for i in range(len(train_array)):
    item=[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.]
    item[train_array] = 1.
    train_label.append(item)

  train_label= np.array(train_label)
  return [train_image,train_label]
   
def ShowTenImage(data_set):
  labels = data_set[1]
  images = data_set[0]
  for i in range(40):
    plt.figure(1)
    plt.imshow(images[i,:,:,:])
    print(labels)
    plt.show()
  plt.show()

def image_normalize(img):
  '''
  normalize an image with shape [x,y,channel]
  '''
  mean      = np.mean(img)
  size = img.shape
  img = img-mean
  std = np.std(img)
  img = img/std
  return img


def image_set_normalize(img_set):
  '''
  normalize an image set with shape [N,x,y,channel]
  where N is number of image in the set
  channel is color channels
  '''
  size = img_set.shape
  for i in range(size[0]):
    img_set[i,:,:,:] = image_normalize(img_set[i,:,:,:])
  return img_set

def display_image_blocked(img):
  '''
  display a image in a new window program will be blocked until the wndows been close
  '''
  plt.imshow(img, cmap='gray')
  
  plt.show()
#['airplane',     0
# 'automobile',   1
# 'bird',         2
# 'cat',          3
# 'deer',         4
# 'dog',          5
# 'frog',         6
# 'horse',        7  
# 'ship',         8
# 'truck']        9
#[6 9 9 4 1 1 2 7 8 3]


#open data set
#fo = open('cifar10data/data_batch_1','rb')
#datadict = cPickle.load(fo)
#fo.close()
#train_image = datadict["data"]
#train_label = datadict['labels']
#train_image = train_image.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float")
#train_label = np.array(train_label)

#for i in range(10):
#  plt.figure(i)
#  plt.imshow(train_image[i,:,:,:])
#print(train_label[0:10])
#plt.show()
##image input 32x32




#command line arguments
# sys.argv[1]   if initialize all the weights from a file
#   when "0"     do not initialize all the weights from a file
#   when "file"  initialize all the weights from file
# sys.argv[2]   if write the final weights to a file
#   when "0"     do not
#   when "file"  write to file


#check the number of input parameter
if len(sys.argv)!=3:
  print "too few arguement"
  quit()


train_batch_size = 20

train_set  = LoadCIFARName('cifar10data/data_batch_1')
train_set1 = LoadCIFARName('cifar10data/data_batch_2')
train_set2 = LoadCIFARName('cifar10data/data_batch_3')
train_set3 = LoadCIFARName('cifar10data/data_batch_4')
train_set4 = LoadCIFARName('cifar10data/data_batch_5')



#ShowTenImage(train_set4)


train_set[0]= np.concatenate((train_set[0], train_set1[0]), axis=0)
train_set[1]= np.concatenate((train_set[1], train_set1[1]), axis=0)

train_set[0]= np.concatenate((train_set[0], train_set2[0]), axis=0)
train_set[1]= np.concatenate((train_set[1], train_set2[1]), axis=0)

train_set[0]= np.concatenate((train_set[0], train_set3[0]), axis=0)
train_set[1]= np.concatenate((train_set[1], train_set3[1]), axis=0)

train_set[0]= np.concatenate((train_set[0], train_set4[0]), axis=0)
train_set[1]= np.concatenate((train_set[1], train_set4[1]), axis=0)
print(train_set[0].shape)
print(train_set[1].shape)

display_image_blocked(train_set[0][0,:,:,:])
image_set_normalize(train_set[0])



#train_set -= np.mean(train_set, axis=(1,2,3))
#train_set /= np.std (train_set, axis=(1,2,3))

#ShowTenImage(train_set)



#placeholders
#x : input pixel arrays
#y_: output result
x  = tf.placeholder(tf.float32, shape=[None,32,32,3])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

x_image = tf.reshape(x, [-1,32,32,3])

#Network structure
network1 = mlpconv(x_image,  3 ,  5, 192, 160, 96,  1, 0.01)
network2 = mlpconv(network1, 96,  5, 192, 192, 192, 1, 0.01)
network3 = mlpconv(network2, 192, 3, 192, 192, 10,  0, 0.1)
ave_vec  = global_ave_pool(network3, [1,8,8,1])
h        = tf.reshape(ave_vec, [-1, 10])
y_conv   = h








#h = fc_relu_layer(h, 128, 256)
#h = dropout_layer(h, 0.3)
#h = fc_relu_layer(h, 256, 10)

ave = tf.Print(ave_vec, [ave_vec])
#dropout
keep_prob = tf.placeholder(tf.float32)
#h_fc1_drop = tf.nn.dropout(h_pool2_flat, keep_prob)

#readout layer
#W_fc2 = weight_variable([10, 10])
#b_fc2 = bias_variable([10])



#y_conv = tf.matmul(h_pool2_flat, W_fc2) + b_fc2
#y_conv = tf.nn.softmax(y_conv, -1)




#training and model
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)
#train_step         = tf.train.GradientDescentOptimizer(0.0001).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy           = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

saver = tf.train.Saver()
init = tf.initialize_all_variables()
sess = tf.Session()

#initialize from a file or not
if sys.argv[1]=="0":
  sess.run(init)
  print "Random initialize all weights bias"
else:
  saver.restore(sess, sys.argv[1])
  print "Restore weights and bias from file:",sys.argv[1]

#tb
#concatenate kernel map for conv layer1  
#KernelMap1=concatenate_kernel_map(W_conv1, 5, 5, 6, 6, 4)#KernelMap0=concatenate_kernel_map(W_conv1)
#KernelMap1 = tf.reshape(KernelMap1, [1,30,30,1])


#output_fm = tf.split(output_fm,train_batch_size,0)
#output_fm = tf.concat([output_fm[0:10]],3)
#for i in range(10):
#  feature_map = tf.reshape(output_fm, [4,4,1,10])
#  global_ave_fm = concatenate_kernel_map(feature_map, 4, 4, 1, 10, 0)
#  fm_out        = tf.summary.image("before global ave pooling 0", global_ave_fm, max_outputs=1)

#print(global_ave_fm.get_shape())
#global_ave_fm = tf.reshape(global_ave_fm, [1,35,14,1])
#fm_out        = tf.summary.image("before global ave pooling", global_ave_fm, max_outputs=1)
#concatenate kernel map for conv layer2
#KernelMap2Group  = tf.split(W_conv2, 32, 2)
#KernelMap2Group2  = KernelMap2Group[0]#tf.split(KernelMap2Group[0], 2, 3)
#print ('dimision kernelgroup2', KernelMap2Group2.get_shape())
#KernelMap2=concatenate_kernel_map(KernelMap2Group2, 5, 5, 8, 8, 0)#KernelMap20=concatenate_kernel_map(KernelMap2Group2[0])
#KernelMap2 = tf.reshape(KernelMap2, [1,40,40,1])

#conv1_k = tf.summary.image("conv1 kernels", KernelMap1, max_outputs=1)
#conv2_k = tf.summary.image("conv2 kernels", KernelMap2, max_outputs=1)
accuracy_chart = tf.summary.scalar("accuracy", accuracy)
#weights_conv1 = tf.summary.histogram("global average pooling", ave_vec)



merged_summary_op = tf.summary.merge_all()
summary_writer = tf.summary.FileWriter('/home/yuf/dev/deep_learning/prj/CIFAR/log', sess.graph)

label_name = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
label_name = np.arange(len(label_name))
#training loop
for i in range(300000):
  batch = GetCIFARBatch(train_set, train_batch_size, 50000)
  if i%50 == 0:

  
    train_accuracy = sess.run(accuracy, feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
    summary_str    = sess.run(merged_summary_op, feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
    summary_writer.add_summary(summary_str, i)
    print("step %d, training accuracy %g"%(i, train_accuracy))
    print(batch[1][0,:])
    ave_vec_num = sess.run(ave_vec, feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
    print(ave_vec_num[0,0,0,:])
    #plt.bar(label_name, ave_vec_num[0,0,0,:], alpha=0.5)
    #plt.ion()
    #plt.show()
    #plt.pause(0.05)

  if i%1000 == 0:
    if sys.argv[2]=="0":
      sess.run(init)
      print "Discard all weights & bias"
    else:
      saver.save(sess, sys.argv[2])
      print "Save weights and bias to file:",sys.argv[2]
  sess.run(train_step, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
  


test_set   = LoadCIFARName('cifar10data/test_batch')
test_set[0]   = image_set_normalize(test_set[0])

test_batch      = GetCIFARBatch(test_set, 500, 10000)
print("test accuracy %g"%sess.run(accuracy, feed_dict={
    x: test_batch[0], y_:test_batch[1], keep_prob: 1.0}))
test_batch      = GetCIFARBatch(test_set, 500, 10000)
print("test accuracy %g"%sess.run(accuracy, feed_dict={
    x: test_batch[0], y_:test_batch[1], keep_prob: 1.0}))

test_batch      = GetCIFARBatch(test_set, 500, 10000)
print("test accuracy %g"%sess.run(accuracy, feed_dict={
    x: test_batch[0], y_:test_batch[1], keep_prob: 1.0}))

test_batch      = GetCIFARBatch(test_set, 500, 10000)
print("test accuracy %g"%sess.run(accuracy, feed_dict={
    x: test_batch[0], y_:test_batch[1], keep_prob: 1.0}))

test_batch      = GetCIFARBatch(test_set, 500, 10000)
print("test accuracy %g"%sess.run(accuracy, feed_dict={
    x: test_batch[0], y_:test_batch[1], keep_prob: 1.0}))


if sys.argv[2]=="0":
  sess.run(init)
  print "Discard all weights & bias"
else:
  saver.save(sess, sys.argv[2])
  print "Save weights and bias to file:",sys.argv[2]


sess.close()

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

发表于 2017-4-1 07:16:17 来自手机 | 显示全部楼层
仰望呀!

出0入0汤圆

发表于 2017-4-1 08:35:51 | 显示全部楼层

出0入0汤圆

发表于 2017-4-1 08:57:43 | 显示全部楼层
你是装的linux虚拟机吗?

出0入0汤圆

发表于 2017-4-1 09:01:52 | 显示全部楼层
之前也想入门学习TensorFlow,但一直不知道如何开展,希望楼主继续分享经验

出0入25汤圆

发表于 2017-4-1 09:13:03 | 显示全部楼层

楼主这个高级了,,不过电子论坛里懂这个的应该很少吧

而且机器学习、深度学习这些对数学要求非常高,,数学不好很难学得会啊,,看《机器学习实战》这本书,看完第二章就卡主看不下去了

出0入0汤圆

发表于 2017-4-1 11:16:14 | 显示全部楼层
用的啥配置的电脑和显卡?

出0入0汤圆

发表于 2017-4-1 11:26:25 来自手机 | 显示全部楼层
有支持cuda显卡就可以,没有显卡也可以学。不过大部分代码最好有Ubuntu上面跑。需要熟悉linux

出100入101汤圆

发表于 2017-4-1 11:45:06 | 显示全部楼层
人工智能,很高级

出0入0汤圆

发表于 2017-4-1 15:11:58 来自手机 | 显示全部楼层
也是很想玩,不懂从何下手

出0入0汤圆

发表于 2017-4-1 16:15:33 | 显示全部楼层
楼主是在搞深度学习吗?

出0入0汤圆

发表于 2017-4-1 16:17:42 | 显示全部楼层
XIVN1987 发表于 2017-4-1 09:13
楼主这个高级了,,不过电子论坛里懂这个的应该很少吧

而且机器学习、深度学习这些对数学要求非常高,, ...

估计要看算法大全吧?呵呵。

出0入0汤圆

 楼主| 发表于 2017-4-1 16:39:51 来自手机 | 显示全部楼层
tomtone 发表于 2017-4-1 11:16
用的啥配置的电脑和显卡?

I7 加 GT740

出0入0汤圆

 楼主| 发表于 2017-4-1 16:41:07 来自手机 | 显示全部楼层
jxx315315 发表于 2017-4-1 08:57
你是装的linux虚拟机吗?

直接是ubuntu

出0入0汤圆

 楼主| 发表于 2017-4-1 16:42:40 来自手机 | 显示全部楼层
znsword 发表于 2017-4-1 16:15
楼主是在搞深度学习吗?

是的现在在练手熟悉tensorflow 八月底要实现3D点云的识别。

出0入0汤圆

 楼主| 发表于 2017-4-1 16:46:21 来自手机 | 显示全部楼层
XIVN1987 发表于 2017-4-1 09:13
楼主这个高级了,,不过电子论坛里懂这个的应该很少吧

而且机器学习、深度学习这些对数学要求非常高,, ...

一眼带过就好了 很多算法只需要知道它可以解决什么问题有什么优缺点,等到要用的时候再回来找他就好了 而且有算法已经有了更好的改进 比如  RCNN fast-RCNN  faster-RCNN  YOLO-RCNN

出0入0汤圆

 楼主| 发表于 2017-4-1 16:47:30 来自手机 | 显示全部楼层
深度学习类的论文作者署名放眼望去半壁江山都是汉语拼音

出0入0汤圆

发表于 2017-4-2 08:20:43 来自手机 | 显示全部楼层
YFM 发表于 2017-4-1 16:47
深度学习类的论文作者署名放眼望去半壁江山都是汉语拼音

楼主能推荐几本入门的书籍吗

出0入0汤圆

发表于 2017-4-2 12:55:26 来自手机 | 显示全部楼层
pxclihai 发表于 2017-4-2 08:20
楼主能推荐几本入门的书籍吗

你要机器学习?深度学习?如果机器学习可以先看机器学习实战 和coursa 上ng吴恩达的视频

出0入0汤圆

 楼主| 发表于 2017-4-2 15:34:23 来自手机 | 显示全部楼层
pxclihai 发表于 2017-4-2 08:20
楼主能推荐几本入门的书籍吗

相关论文都是2013到2016年的 书应该都还没出来 只能不断收集坊间的知识碎片然后重组起来 不过最基本的神经网络,梯度下降这些知识是得要会的

出0入0汤圆

发表于 2017-4-2 20:37:27 来自手机 | 显示全部楼层
深度学习ng也有视频的ufldl教程

出0入0汤圆

发表于 2017-4-3 06:33:19 | 显示全部楼层
XIVN1987 发表于 2017-4-1 09:13
楼主这个高级了,,不过电子论坛里懂这个的应该很少吧

而且机器学习、深度学习这些对数学要求非常高,, ...

不做理论研究,做工程应用还好!周志华那本《机器学习》讲的挺好的!

出0入0汤圆

发表于 2017-4-3 06:34:56 | 显示全部楼层
brentcao 发表于 2017-4-2 12:55
你要机器学习?深度学习?如果机器学习可以先看机器学习实战 和coursa 上ng吴恩达的视频 ...


那个视频现在播放不了了!
网易公开课上也有他讲的课,但是偏理论一些!

出0入0汤圆

发表于 2017-4-3 07:59:39 来自手机 | 显示全部楼层
往事如烟 发表于 2017-4-3 06:34
那个视频现在播放不了了!
网易公开课上也有他讲的课,但是偏理论一些! ...

可以放啊,用手机版的coursa可以看的

出0入0汤圆

发表于 2017-4-3 09:39:17 | 显示全部楼层
brentcao 发表于 2017-4-3 07:59
可以放啊,用手机版的coursa可以看的

我手机版也播放不了!你是不是翻墙了!

出0入0汤圆

发表于 2017-4-4 10:58:28 来自手机 | 显示全部楼层
我没有翻墙啊?

出0入0汤圆

发表于 2017-4-24 16:18:25 | 显示全部楼层
厉害了,老铁

出0入0汤圆

发表于 2017-7-5 20:29:34 | 显示全部楼层
仰望,谢谢lz分享,未来的发展方向。

出0入0汤圆

发表于 2017-12-21 00:09:51 | 显示全部楼层
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.image as mpimg
  4. import cPickle
  5. import tensorflow as tf
  6. import sys

  7. def concatenate_kernel_map(kernel, kXSize, kYSize, outx, outy, padding):
  8.   "kernel:  groups of kernel to display [xdim,ydim,1,batch]"
  9.   "kXSize:  kernel x size"
  10.   "kYSize:  kernel y size"
  11.   "outx:    how many kernel patches in output kernel map x direction"
  12.   "outy:    how many kernel patches in putput kernel map y direction"
  13.   "padding: number of padding of zero patches for the last row"
  14.   if padding!=0:
  15.     ZeroPad     = tf.zeros([kXSize,kYSize,1,padding], dtype=tf.float32) #[5,5,1,4]
  16.     KernelGroup = tf.concat([kernel, ZeroPad],3) #[5,5,1,36]
  17.   else:
  18.     KernelGroup = kernel
  19.   print (KernelGroup.get_shape())
  20.   KernelLine  = tf.split(KernelGroup, outy*outx, 3) #[5,5,1,1]

  21.   k_map = tf.concat([tf.concat(KernelLine[0*outx:(0+1)*outx],0), tf.concat(KernelLine[1*outx:(1+1)*outx],0)], 1)
  22.   for i in range(2,outy):
  23.     k_map = tf.concat([k_map,tf.concat(KernelLine[i*outx:(i+1)*outx], 0)], 1)

  24.   print (k_map.get_shape())
  25.   k_map = tf.reshape(k_map, [1,kXSize*outx,kYSize*outy,1])
  26.   return k_map

  27. #input:   shape of a weights
  28. #return:  initialized weight
  29. def weight_variable(shape, std_value):
  30.   initial = tf.truncated_normal(shape, dtype=tf.float32, stddev=std_value)
  31.   return tf.Variable(initial)

  32. def bias_variable(shape):
  33.   initial = tf.constant(0., shape=shape)
  34.   return tf.Variable(initial)


  35. # input x and weight W
  36. #return the conv result
  37. def conv2d(x,W):
  38.   return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding = 'SAME')

  39. def max_pool_2x2(x):
  40.   return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')

  41. def lrelu(x, leak=0.2, name="lrelu"):
  42.   with tf.variable_scope(name):
  43.     f1 = 0.5 * (1 + leak)
  44.     f2 = 0.5 * (1 - leak)
  45.     return f1 * x + f2 * abs(x)


  46. def mlpconv(x, x_channel, conv_k_size, conv_k_num, mlp1_k_num, mlp2_k_num, has_2x2_pooling, weight_std_value):
  47.   'construct mlp convolution layer'
  48.   '(conv+relu)-->(MLP+relu)-->(MLE+relu)'
  49.   'x          : input image'
  50.   'x_channel  : x input number of channel'
  51.   'conv_k_size: kernel x,y dimmension. x and y are same, so this is a scalar'
  52.   'conv_k_num : number of kernel'
  53.   'mlp1_k_num : mlp1 layer number of 1x1 kernel'
  54.   'mlp2_k_num : mlp2 layer number of 1x1 kernel'
  55.   'has_2x2_pooling: if do max pooling to the output 1 is yes 0 is no'

  56.   #conv layer weights
  57.   W_conv1 = weight_variable([conv_k_size, conv_k_size ,x_channel, conv_k_num], weight_std_value)
  58.   b_conv1 = bias_variable([conv_k_num])

  59.   W_mlp1  = weight_variable([1, 1 ,conv_k_num, mlp1_k_num], weight_std_value)
  60.   b_mlp1  = bias_variable([mlp1_k_num])

  61.   W_mlp2  = weight_variable([1, 1 ,mlp1_k_num, mlp2_k_num], weight_std_value)
  62.   b_mlp2  = bias_variable([mlp2_k_num])  
  63.   #network cascade
  64.   h  = conv2d(x, W_conv1) + b_conv1
  65.   h  = tf.nn.local_response_normalization(h, depth_radius=5, bias=0.01 )
  66.   h  = lrelu(h)

  67.   h  = conv2d(h, W_mlp1) + b_mlp1
  68.   h  = tf.nn.local_response_normalization(h, depth_radius=5, bias=0.01 )
  69.   h  = lrelu(h)

  70.   h  = conv2d(h, W_mlp2) + b_mlp2
  71.   h  = tf.nn.local_response_normalization(h, depth_radius=5, bias=0.01 )
  72.   h  = lrelu(h)
  73.   #h  = tf.nn.dropout(h, 0.5)

  74.   if has_2x2_pooling==1:   
  75.     h  = max_pool_2x2(h)
  76.   return h

  77. def global_ave_pool(x, x_shape):
  78.   'global average pooling the input x'
  79.   'x       :input feature map [1,x,y,1]'
  80.   'x_shape :should be [1,x,y,1]'
  81.   return tf.nn.avg_pool(x, x_shape,[1,2,2,1], 'VALID')

  82. def fc_relu_layer(x, in_length, out_length):
  83.   w = weight_variable([in_length, out_length])
  84.   b = bias_variable([out_length])

  85.   y = tf.nn.relu(tf.matmul(x, w) + b)
  86.   return y
  87. def dropout_layer(x, rate):
  88.   return tf.nn.dropout(x, rate)

  89. def GetCIFARBatch(dataset, batch_size, pick_range):
  90.   '''
  91.   randomely get several image and label pairs from the dataset
  92.   dataset:    dataset with format [image, label]
  93.   batch_size: batch size
  94.   return:     [image, label]
  95.   '''
  96.   import random
  97.   image = dataset[0]
  98.   label = dataset[1]  

  99.   batch_image=[]
  100.   batch_label=[]
  101.   for i in range(batch_size):
  102.     idxf = random.random()
  103.     idxf = idxf*pick_range
  104.     idxint = int(idxf)
  105.     #print(idxint)
  106.     batch_image.append(image[idxint])
  107.     batch_label.append(label[idxint])

  108.   batch_image = np.array(batch_image)
  109.   batch_label = np.array(batch_label)
  110.   return (batch_image, batch_label)
  111.   

  112. def LoadCIFARName(file_name):
  113.   '''
  114.   load CIFAT-10 data set
  115.   file_name:  the cifar-10 data file path
  116.   return:  [train_image, train_label]
  117.       train_image: image in shape [10000,3,32,32]
  118.       train_label: label of each image in shape [10000,10]. with each item
  119.                    [0,0,0,0,0,0,0,1,0,0]
  120.   '''
  121.   fo = open(file_name,'rb')
  122.   datadict = cPickle.load(fo)
  123.   fo.close()
  124.   train_image = datadict["data"]
  125.   train_array = datadict['labels']
  126.   train_image = train_image.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float")
  127.   train_array = np.array(train_array)

  128.   train_label=[]
  129.   for i in range(len(train_array)):
  130.     item=[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.]
  131.     item[train_array] = 1.
  132.     train_label.append(item)

  133.   train_label= np.array(train_label)
  134.   return [train_image,train_label]
  135.    
  136. def ShowTenImage(data_set):
  137.   labels = data_set[1]
  138.   images = data_set[0]
  139.   for i in range(40):
  140.     plt.figure(1)
  141.     plt.imshow(images[i,:,:,:])
  142.     print(labels)
  143.     plt.show()
  144.   plt.show()

  145. def image_normalize(img):
  146.   '''
  147.   normalize an image with shape [x,y,channel]
  148.   '''
  149.   mean      = np.mean(img)
  150.   size = img.shape
  151.   img = img-mean
  152.   std = np.std(img)
  153.   img = img/std
  154.   return img


  155. def image_set_normalize(img_set):
  156.   '''
  157.   normalize an image set with shape [N,x,y,channel]
  158.   where N is number of image in the set
  159.   channel is color channels
  160.   '''
  161.   size = img_set.shape
  162.   for i in range(size[0]):
  163.     img_set[i,:,:,:] = image_normalize(img_set[i,:,:,:])
  164.   return img_set

  165. def display_image_blocked(img):
  166.   '''
  167.   display a image in a new window program will be blocked until the wndows been close
  168.   '''
  169.   plt.imshow(img, cmap='gray')
  170.   
  171.   plt.show()
  172. #['airplane',     0
  173. # 'automobile',   1
  174. # 'bird',         2
  175. # 'cat',          3
  176. # 'deer',         4
  177. # 'dog',          5
  178. # 'frog',         6
  179. # 'horse',        7  
  180. # 'ship',         8
  181. # 'truck']        9
  182. #[6 9 9 4 1 1 2 7 8 3]


  183. #open data set
  184. #fo = open('cifar10data/data_batch_1','rb')
  185. #datadict = cPickle.load(fo)
  186. #fo.close()
  187. #train_image = datadict["data"]
  188. #train_label = datadict['labels']
  189. #train_image = train_image.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float")
  190. #train_label = np.array(train_label)

  191. #for i in range(10):
  192. #  plt.figure(i)
  193. #  plt.imshow(train_image[i,:,:,:])
  194. #print(train_label[0:10])
  195. #plt.show()
  196. ##image input 32x32




  197. #command line arguments
  198. # sys.argv[1]   if initialize all the weights from a file
  199. #   when "0"     do not initialize all the weights from a file
  200. #   when "file"  initialize all the weights from file
  201. # sys.argv[2]   if write the final weights to a file
  202. #   when "0"     do not
  203. #   when "file"  write to file


  204. #check the number of input parameter
  205. if len(sys.argv)!=3:
  206.   print "too few arguement"
  207.   quit()


  208. train_batch_size = 20

  209. train_set  = LoadCIFARName('cifar10data/data_batch_1')
  210. train_set1 = LoadCIFARName('cifar10data/data_batch_2')
  211. train_set2 = LoadCIFARName('cifar10data/data_batch_3')
  212. train_set3 = LoadCIFARName('cifar10data/data_batch_4')
  213. train_set4 = LoadCIFARName('cifar10data/data_batch_5')



  214. #ShowTenImage(train_set4)


  215. train_set[0]= np.concatenate((train_set[0], train_set1[0]), axis=0)
  216. train_set[1]= np.concatenate((train_set[1], train_set1[1]), axis=0)

  217. train_set[0]= np.concatenate((train_set[0], train_set2[0]), axis=0)
  218. train_set[1]= np.concatenate((train_set[1], train_set2[1]), axis=0)

  219. train_set[0]= np.concatenate((train_set[0], train_set3[0]), axis=0)
  220. train_set[1]= np.concatenate((train_set[1], train_set3[1]), axis=0)

  221. train_set[0]= np.concatenate((train_set[0], train_set4[0]), axis=0)
  222. train_set[1]= np.concatenate((train_set[1], train_set4[1]), axis=0)
  223. print(train_set[0].shape)
  224. print(train_set[1].shape)

  225. display_image_blocked(train_set[0][0,:,:,:])
  226. image_set_normalize(train_set[0])



  227. #train_set -= np.mean(train_set, axis=(1,2,3))
  228. #train_set /= np.std (train_set, axis=(1,2,3))

  229. #ShowTenImage(train_set)



  230. #placeholders
  231. #x : input pixel arrays
  232. #y_: output result
  233. x  = tf.placeholder(tf.float32, shape=[None,32,32,3])
  234. y_ = tf.placeholder(tf.float32, shape=[None, 10])

  235. x_image = tf.reshape(x, [-1,32,32,3])

  236. #Network structure
  237. network1 = mlpconv(x_image,  3 ,  5, 192, 160, 96,  1, 0.01)
  238. network2 = mlpconv(network1, 96,  5, 192, 192, 192, 1, 0.01)
  239. network3 = mlpconv(network2, 192, 3, 192, 192, 10,  0, 0.1)
  240. ave_vec  = global_ave_pool(network3, [1,8,8,1])
  241. h        = tf.reshape(ave_vec, [-1, 10])
  242. y_conv   = h








  243. #h = fc_relu_layer(h, 128, 256)
  244. #h = dropout_layer(h, 0.3)
  245. #h = fc_relu_layer(h, 256, 10)

  246. ave = tf.Print(ave_vec, [ave_vec])
  247. #dropout
  248. keep_prob = tf.placeholder(tf.float32)
  249. #h_fc1_drop = tf.nn.dropout(h_pool2_flat, keep_prob)

  250. #readout layer
  251. #W_fc2 = weight_variable([10, 10])
  252. #b_fc2 = bias_variable([10])



  253. #y_conv = tf.matmul(h_pool2_flat, W_fc2) + b_fc2
  254. #y_conv = tf.nn.softmax(y_conv, -1)




  255. #training and model
  256. cross_entropy      = tf.reduce_mean(
  257.                      tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
  258. train_step         = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
  259. #train_step         = tf.train.GradientDescentOptimizer(0.0001).minimize(cross_entropy)
  260. correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
  261. accuracy           = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

  262. saver = tf.train.Saver()
  263. init = tf.initialize_all_variables()
  264. sess = tf.Session()

  265. #initialize from a file or not
  266. if sys.argv[1]=="0":
  267.   sess.run(init)
  268.   print "Random initialize all weights bias"
  269. else:
  270.   saver.restore(sess, sys.argv[1])
  271.   print "Restore weights and bias from file:",sys.argv[1]

  272. #tb
  273. #concatenate kernel map for conv layer1  
  274. #KernelMap1=concatenate_kernel_map(W_conv1, 5, 5, 6, 6, 4)#KernelMap0=concatenate_kernel_map(W_conv1)
  275. #KernelMap1 = tf.reshape(KernelMap1, [1,30,30,1])


  276. #output_fm = tf.split(output_fm,train_batch_size,0)
  277. #output_fm = tf.concat([output_fm[0:10]],3)
  278. #for i in range(10):
  279. #  feature_map = tf.reshape(output_fm, [4,4,1,10])
  280. #  global_ave_fm = concatenate_kernel_map(feature_map, 4, 4, 1, 10, 0)
  281. #  fm_out        = tf.summary.image("before global ave pooling 0", global_ave_fm, max_outputs=1)

  282. #print(global_ave_fm.get_shape())
  283. #global_ave_fm = tf.reshape(global_ave_fm, [1,35,14,1])
  284. #fm_out        = tf.summary.image("before global ave pooling", global_ave_fm, max_outputs=1)
  285. #concatenate kernel map for conv layer2
  286. #KernelMap2Group  = tf.split(W_conv2, 32, 2)
  287. #KernelMap2Group2  = KernelMap2Group[0]#tf.split(KernelMap2Group[0], 2, 3)
  288. #print ('dimision kernelgroup2', KernelMap2Group2.get_shape())
  289. #KernelMap2=concatenate_kernel_map(KernelMap2Group2, 5, 5, 8, 8, 0)#KernelMap20=concatenate_kernel_map(KernelMap2Group2[0])
  290. #KernelMap2 = tf.reshape(KernelMap2, [1,40,40,1])

  291. #conv1_k = tf.summary.image("conv1 kernels", KernelMap1, max_outputs=1)
  292. #conv2_k = tf.summary.image("conv2 kernels", KernelMap2, max_outputs=1)
  293. accuracy_chart = tf.summary.scalar("accuracy", accuracy)
  294. #weights_conv1 = tf.summary.histogram("global average pooling", ave_vec)



  295. merged_summary_op = tf.summary.merge_all()
  296. summary_writer = tf.summary.FileWriter('/home/yuf/dev/deep_learning/prj/CIFAR/log', sess.graph)

  297. label_name = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
  298. label_name = np.arange(len(label_name))
  299. #training loop
  300. for i in range(300000):
  301.   batch = GetCIFARBatch(train_set, train_batch_size, 50000)
  302.   if i%50 == 0:

  303.   
  304.     train_accuracy = sess.run(accuracy, feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
  305.     summary_str    = sess.run(merged_summary_op, feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
  306.     summary_writer.add_summary(summary_str, i)
  307.     print("step %d, training accuracy %g"%(i, train_accuracy))
  308.     print(batch[1][0,:])
  309.     ave_vec_num = sess.run(ave_vec, feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
  310.     print(ave_vec_num[0,0,0,:])
  311.     #plt.bar(label_name, ave_vec_num[0,0,0,:], alpha=0.5)
  312.     #plt.ion()
  313.     #plt.show()
  314.     #plt.pause(0.05)

  315.   if i%1000 == 0:
  316.     if sys.argv[2]=="0":
  317.       sess.run(init)
  318.       print "Discard all weights & bias"
  319.     else:
  320.       saver.save(sess, sys.argv[2])
  321.       print "Save weights and bias to file:",sys.argv[2]
  322.   sess.run(train_step, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
  323.   


  324. test_set   = LoadCIFARName('cifar10data/test_batch')
  325. test_set[0]   = image_set_normalize(test_set[0])

  326. test_batch      = GetCIFARBatch(test_set, 500, 10000)
  327. print("test accuracy %g"%sess.run(accuracy, feed_dict={
  328.     x: test_batch[0], y_:test_batch[1], keep_prob: 1.0}))
  329. test_batch      = GetCIFARBatch(test_set, 500, 10000)
  330. print("test accuracy %g"%sess.run(accuracy, feed_dict={
  331.     x: test_batch[0], y_:test_batch[1], keep_prob: 1.0}))

  332. test_batch      = GetCIFARBatch(test_set, 500, 10000)
  333. print("test accuracy %g"%sess.run(accuracy, feed_dict={
  334.     x: test_batch[0], y_:test_batch[1], keep_prob: 1.0}))

  335. test_batch      = GetCIFARBatch(test_set, 500, 10000)
  336. print("test accuracy %g"%sess.run(accuracy, feed_dict={
  337.     x: test_batch[0], y_:test_batch[1], keep_prob: 1.0}))

  338. test_batch      = GetCIFARBatch(test_set, 500, 10000)
  339. print("test accuracy %g"%sess.run(accuracy, feed_dict={
  340.     x: test_batch[0], y_:test_batch[1], keep_prob: 1.0}))


  341. if sys.argv[2]=="0":
  342.   sess.run(init)
  343.   print "Discard all weights & bias"
  344. else:
  345.   saver.save(sess, sys.argv[2])
  346.   print "Save weights and bias to file:",sys.argv[2]


  347. sess.close()
复制代码

出25入84汤圆

发表于 2018-1-25 17:01:27 | 显示全部楼层
我运行出来是 “too few arguement”

出0入0汤圆

发表于 2018-1-25 22:08:24 | 显示全部楼层
高手,顶顶顶。
多谢!

出0入17汤圆

发表于 2018-1-26 09:05:15 | 显示全部楼层
终于找到数学的用处了~~~~~~~~~

出0入0汤圆

发表于 2018-7-5 17:57:12 | 显示全部楼层
仰望一下,本来还幻想看下tensorflow的,一看数学就挡住了,楼主能出来指点下大家吗?给个学习之路

出0入0汤圆

发表于 2018-7-5 18:06:27 | 显示全部楼层
这个MARK一下。学习。

出0入0汤圆

发表于 2018-12-11 15:20:35 | 显示全部楼层
有意思吗?

出0入0汤圆

发表于 2019-2-15 16:56:19 | 显示全部楼层
强高手谢谢资料,学习了

出0入0汤圆

发表于 2019-2-15 16:58:37 | 显示全部楼层
仰望高手

出0入114汤圆

发表于 2019-2-15 17:58:14 来自手机 | 显示全部楼层
下周运行试试这个程序

出0入0汤圆

发表于 2019-2-15 19:09:02 | 显示全部楼层
TensorFlow高级货 仰望一下

出0入0汤圆

发表于 2019-8-23 15:41:13 | 显示全部楼层

TensorFlow高级货 仰望一下 并且要实现一下的。

出0入0汤圆

发表于 2021-7-19 15:47:56 | 显示全部楼层
刚刚学习,谢谢分享

出95入100汤圆

发表于 2021-7-19 17:27:20 | 显示全部楼层
仰望,给点学习路径

出0入0汤圆

发表于 2022-7-28 16:29:01 | 显示全部楼层
楼主,学习BP神经网络有什么好的资料吗?

出0入0汤圆

发表于 2022-8-6 00:34:13 | 显示全部楼层
上个学期我们matlab结课让做点工程,我的结课作业就是用二层全连接网络实现MLX90640传回的红外图像二分类,分的是人像,用matlab手写了一遍正相反相传播,二分类正确率70%-80%,然后用MATLAB的机器学习库做了个带卷积网络的,二分类准确率95%上下,最近在做Mask RCNN的分类,不过那个带FPN感觉太难了,我数学功底差,二阶偏导矩阵的牛顿优化器都看不明白,最近在看计网和数字电路,也没时间学数学,唉

出0入0汤圆

发表于 2022-8-6 00:35:49 | 显示全部楼层
MaskRCNN用的是脸书的maskrcnn-benchmark工程,git上有,基于pytorch
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-11 11:28

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表