一、目的

  以imagenet2012作为数据集,用Inception-v3对图像提取特征作为输入,来训练一个自编码器。

  以上作为预训练模型,随后在该自编码器的基础上,中间加入一个自表示层,将最终学习到的自表示层系数,作为相似度矩阵,对imagenet2012的1000类进行聚类。

二、预训练

  1.原理

  inception-v3+自编码器

  2.代码 

  1. import tensorflow as tf
  2. import os
  3. import numpy as np
  4. import random
  5. import tensorflow.contrib.slim as slim
  6. import shutil
  7.  
  8. tf.app.flags.DEFINE_string('model_dir', 'model/inception', 'Inception-v3 pretrain model dir')
  9. tf.app.flags.DEFINE_string('class_list', 'imagenet12/train_class_list.txt', 'ILSVRC2012 image class list')
  10. tf.app.flags.DEFINE_string('img_path', '/media/gpu/bdc7606d-0e3c-4870-9a5d-4926fd9961c0/gpu/Works/imagenet/others/ILSVRC2012_img_train', 'ILSVRC2012 image train path')
  11. tf.app.flags.DEFINE_integer('max_train_steps_pre', 200000, 'max train num')
  12. tf.app.flags.DEFINE_boolean('restore', True, 'wheather restore model and variable from previous saved')
  13. tf.app.flags.DEFINE_string('checkpoint_path', 'model/pre/', 'model saved path')
  14. tf.app.flags.DEFINE_string('feature_train_path','feature_train','ILSVRC2012 train feature save path')
  15. tf.app.flags.DEFINE_integer('large_multi', 100, 'enlarge the feature data')
  16. tf.app.flags.DEFINE_integer('width', 32, 'the width of feature input')
  17. tf.app.flags.DEFINE_integer('inception_out_size', 2048, 'the dim of feature input,inception out dim')
  18. tf.app.flags.DEFINE_integer('train_num_of_every_batch', 2000, 'change the data every 2000 epochs')
  19. FLAGS = tf.app.flags.FLAGS
  20.  
  21. kernel_num_list = [16, 32, 64] #channel num
  22. kernel_size_list = [[3, 3], [3, 3], [3, 3]] #channel size
  23. kernel_stride_list = [2, 2, 2] #stride
  24. batch_size = 500
  25.  
  26. def get_inception_graph():
  27. '''
  28. load inception-v3 gragh for get_inception_output to
  29. get the feature from Inception-v3
  30. '''
  31. with tf.gfile.FastGFile(os.path.join(FLAGS.model_dir, 'inception-v3.pb'), 'rb') as f:
  32. graph_def = tf.GraphDef()
  33. graph_def.ParseFromString(f.read())
  34. inception_out = tf.import_graph_def(graph_def,name='',return_elements=['pool_3/_reshape:0'])
  35. return inception_out
  36.  
  37. def create_graph_pre():
  38. '''
  39. create graph and loss
  40. '''
  41. inception_input = tf.placeholder(tf.float32, [None, FLAGS.width, FLAGS.inception_out_size/FLAGS.width, 1], name='inception_holder')
  42. with tf.variable_scope('DSC'):
  43. with tf.variable_scope('encoder'):
  44. net = slim.conv2d(inception_input, kernel_num_list[0], kernel_size_list[0], stride = kernel_stride_list[0], scope='conv_0')
  45. net = slim.conv2d(net, kernel_num_list[1], kernel_size_list[1], stride=kernel_stride_list[1], scope='conv_1')
  46. net = slim.conv2d(net, kernel_num_list[2], kernel_size_list[2], stride=kernel_stride_list[2], scope='conv_2')
  47.  
  48. with tf.variable_scope('decoder'):
  49. net = slim.conv2d_transpose(net, kernel_num_list[1], kernel_size_list[2], stride=kernel_stride_list[2], scope='deconv_2')
  50. net = slim.conv2d_transpose(net, kernel_num_list[0], kernel_size_list[1], stride=kernel_stride_list[1], scope='deconv_1')
  51. net = slim.conv2d_transpose(net, 1, kernel_size_list[0], stride=kernel_stride_list[0], scope='deconv_0')
  52.  
  53. restruct_loss = tf.losses.mean_squared_error(net, inception_input)
  54. return restruct_loss,inception_input,net
  55.  
  56. def get_inception_output(sess, img, txt_name,inception_out,save):
  57. '''
  58. get the inception-v3 feature for img and save in txt_name
  59. '''
  60. image_data = tf.gfile.FastGFile(img, 'rb').read()
  61. output = sess.run(inception_out, feed_dict={'DecodeJpeg/contents:0': image_data})
  62. output = np.squeeze(output)
  63. output = output.reshape(FLAGS.width,-1)
  64. if save == True:
  65. np.savetxt(txt_name, output, fmt='%.6f')
  66. return output
  67.  
  68. def get_inception_batch(sess,inception_out,save=True):
  69. '''
  70. get inception-v3 feature for a batch as input of the new graph(create_graph_pre)
  71. '''
  72. class_list = np.loadtxt(FLAGS.class_list, dtype= str)[0:batch_size]
  73. batch = []
  74.  
  75. for i, item in enumerate(class_list):
  76. class_img_path = os.path.join(FLAGS.img_path, item)
  77. class_img_list = os.listdir(class_img_path)
  78.  
  79. img_name = random.choice(class_img_list)
  80. txt_name = os.path.join(FLAGS.feature_train_path, item, img_name[:-4]+'txt')
  81. img = os.path.join(class_img_path, img_name)
  82.  
  83. if os.path.exists(txt_name):
  84. print('%s Found!' % os.path.join(item, img_name[:-4]+'txt'))
  85. batch_i = np.loadtxt(txt_name)
  86. else:
  87. #print('%s Extracting!' % os.path.join(item, img_name[:-4]+'txt'))
  88. dir_name = os.path.join(FLAGS.feature_train_path, item)
  89. if not os.path.exists(dir_name):
  90. os.makedirs(dir_name)
  91. batch_i = get_inception_output(sess, img,txt_name, inception_out,save=save)
  92. batch.append(batch_i)
  93. large_batch = np.array(batch) * FLAGS.large_multi
  94.  
  95. return large_batch
  96.  
  97. def reconstruct(sess, net, img_inception):
  98. '''
  99. get the loss for the input(img_inception) to varify the result of reconstruct
  100. '''
  101. output = sess.run([net], feed_dict={'inception_holder:0': img_inception})
  102. img_inception=np.squeeze(img_inception)
  103. output=np.squeeze(np.array(output))
  104. test_loss = pow(img_inception-output,2)
  105.  
  106. return output, sum(sum(test_loss))/(32*64)
  107.  
  108. def interface_pre():
  109.  
  110. total_loss, inception_input, net = create_graph_pre()
  111.  
  112. global_step = tf.Variable(0)
  113. learning_rate = tf.train.exponential_decay(1e-3, global_step, decay_steps=100, decay_rate=0.98, staircase=True)
  114. train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(total_loss)
  115.  
  116. saver = tf.train.Saver(max_to_keep=3)
  117.  
  118. with tf.Session() as sess:
  119.  
  120. if FLAGS.restore:
  121. print('continue training from previous checkpoint')
  122. ckpt = tf.train.latest_checkpoint(FLAGS.checkpoint_path)
  123. pre_step = int(ckpt.replace(FLAGS.checkpoint_path + '-', ''))
  124. saver.restore(sess, ckpt)
  125. else:
  126. #remove previous model
  127. if os.path.exists(FLAGS.checkpoint_path):
  128. shutil.rmtree(FLAGS.checkpoint_path)
  129. os.makedirs(FLAGS.checkpoint_path)
  130. sess.run(tf.global_variables_initializer())
  131. pre_step = 0
  132.  
  133. inception_out = get_inception_graph()
  134.  
  135. for step in range(FLAGS.max_train_steps_pre):
  136. if step % FLAGS.train_num_of_every_batch == 0:
  137. inception_output = get_inception_batch(sess, inception_out, save=False)
  138. inception_output = inception_output.reshape(-1,inception_output.shape[1], inception_output.shape[2], 1)
  139. perm = np.arange(batch_size)
  140. np.random.shuffle(perm)
  141. inception_output = inception_output[perm]
  142.  
  143. inception_output = inception_output.reshape(-1,inception_output.shape[1], inception_output.shape[2], 1)
  144. _, loss_value= sess.run([train_op, total_loss],feed_dict={'inception_holder:0':inception_output})
  145. if step % 100 == 0:
  146. print("step %d :total_loss= %f" % (step, loss_value))
  147. if step % 500 == 0 and step > 0:
  148. # save model
  149. if step > 500 :
  150. write_meta_graph = False
  151. else:
  152. write_meta_graph = True
  153. all_step = pre_step + step
  154. saver.save(sess, FLAGS.checkpoint_path, global_step=all_step, write_meta_graph=write_meta_graph)
  155. #construct
  156. img_inception = get_inception_output(sess, 'cropped_panda.jpg', 'cropped_panda.txt',inception_out,False)
  157. img_out, test_loss = reconstruct(sess, net, FLAGS.large_multi*img_inception.reshape(-1,32,64,1))
  158. print("test loss= %.5f" % test_loss)
  159.  
  160. if __name__ == '__main__':
  161. interface_pre()

三、训练

  1.原理

  以imagenet2012在inception-v3特征上的类平均向量作为输入,来训练模型,获得自表示系数作为聚类输入,从而获得聚类结果并可视化。

  2.代码  

  1. import tensorflow as tf
  2. import os
  3. import numpy as np
  4. import random
  5. import tensorflow.contrib.slim as slim
  6. import tensorflow.contrib.slim.nets as nets
  7. import shutil
  8. from scipy.sparse import coo_matrix
  9. from sklearn.cluster import spectral_clustering
  10. from scipy.sparse.linalg import svds
  11. from sklearn import cluster
  12. from sklearn.preprocessing import normalize
  13.  
  14. tf.app.flags.DEFINE_string('class_list', '../imagenet12/train_class_list.txt', 'ILSVRC2012 image class list')
  15. tf.app.flags.DEFINE_string('img_path', '/media/gpu/bdc7606d-0e3c-4870-9a5d-4926fd9961c0/gpu/Works/imagenet/others/ILSVRC2012_img_train', 'ILSVRC2012 image train path')
  16. tf.app.flags.DEFINE_integer('max_train_steps', 200000, 'max train num')
  17. tf.app.flags.DEFINE_boolean('restore', False, 'wheather restore model and variable from previous saved')
  18. tf.app.flags.DEFINE_string('pretrain_path', '../model/pre/', 'pretrain model path')
  19. tf.app.flags.DEFINE_string('train_path', 'model/train/', 'train model path')
  20. tf.app.flags.DEFINE_string('Coef_path','Coef/','save path of self_express xishu')
  21. tf.app.flags.DEFINE_integer('large_multi', 100, '')
  22. tf.app.flags.DEFINE_integer('width', 32, '')
  23. tf.app.flags.DEFINE_integer('inception_out_size', 2048, '')
  24. tf.app.flags.DEFINE_float('self_express_loss_weight',1,'')
  25. tf.app.flags.DEFINE_float('regularizer_loss_weight',0.01,'')
  26. tf.app.flags.DEFINE_integer('train_num_of_every_batch', 5000, '')
  27. tf.app.flags.DEFINE_string('cluster_path','cluster','cluster result path')
  28. tf.app.flags.DEFINE_string('data_path','avg_train_vector','imagenet2012 average feature path')
  29. FLAGS = tf.app.flags.FLAGS
  30.  
  31. kernel_num_list = [16, 32, 64]
  32. kernel_size_list = [[3, 3], [3, 3], [3, 3]]
  33. kernel_stride_list = [2, 2, 2]
  34. batch_size = 1000
  35. learn_rate=0.001
  36.  
  37. def create_graph_pre():
  38.  
  39. inception_input = tf.placeholder(tf.float32, [None, FLAGS.width, int(FLAGS.inception_out_size/FLAGS.width), 1], name='inception_holder')
  40. with tf.variable_scope('DSC'):
  41. with slim.arg_scope([slim.conv2d], weights_regularizer=slim.l2_regularizer(0.0005)):
  42. with tf.variable_scope('encoder'):
  43. net = slim.conv2d(inception_input, kernel_num_list[0], kernel_size_list[0], stride = kernel_stride_list[0], scope='conv_0')
  44. net = slim.conv2d(net, kernel_num_list[1], kernel_size_list[1], stride=kernel_stride_list[1], scope='conv_1')
  45. net = slim.conv2d(net, kernel_num_list[2], kernel_size_list[2], stride=kernel_stride_list[2], scope='conv_2')
  46. self_express_x = net
  47. net = tf.reshape(net, [batch_size, -1], name='reshape_to_flat')
  48. Coef = slim.model_variable('Coef',
  49. shape=[batch_size, batch_size],
  50. initializer=tf.truncated_normal_initializer(stddev=0.1),
  51. regularizer=slim.l2_regularizer(0.0005), trainable=True)
  52. net = tf.matmul(Coef, net, name='mutmul')
  53.  
  54. with tf.variable_scope('decoder'):
  55. net = tf.reshape(net, [batch_size, int(FLAGS.width/8), int(FLAGS.inception_out_size/FLAGS.width/8), kernel_num_list[2]], name='reshape_to_normal')
  56. self_express_x_c = net
  57. net = slim.conv2d_transpose(net, kernel_num_list[1], kernel_size_list[2], stride=kernel_stride_list[2], scope='deconv_2')
  58. net = slim.conv2d_transpose(net, kernel_num_list[0], kernel_size_list[1], stride=kernel_stride_list[1], scope='deconv_1')
  59. net = slim.conv2d_transpose(net, 1, kernel_size_list[0], stride=kernel_stride_list[0], scope='deconv_0')
  60.  
  61. reconstruct_loss = tf.losses.mean_squared_error(net, inception_input)
  62. self_express_loss = FLAGS.self_express_loss_weight *tf.losses.mean_squared_error(self_express_x, self_express_x_c)
  63. regularizer_loss = FLAGS.regularizer_loss_weight * tf.reduce_sum(tf.pow(Coef, 2.0))
  64. #regularizer_loss = tf.add_n(tf.losses.get_regularization_losses())
  65.  
  66. loss = reconstruct_loss + self_express_loss + regularizer_loss
  67. #loss = self_express_loss
  68. return net, loss, Coef,reconstruct_loss, self_express_loss, regularizer_loss
  69.  
  70. def get_inception_batch_avg():
  71. class_list = np.loadtxt(FLAGS.class_list, dtype=str)[0:batch_size]
  72. res=[]
  73. for i in range(len(class_list)):
  74. data_path = os.path.join(FLAGS.data_path,class_list[i]+'.txt')
  75. data = np.loadtxt(data_path)
  76. data = data.reshape(32,64)
  77. res.append(data*100)
  78. return np.array(res)
  79.  
  80. def interface():
  81. net, total_loss, Coef, reconstruct_loss, self_express_loss, regularizer_loss = create_graph_pre()
  82.  
  83. global_step = tf.Variable(0)
  84. learning_rate = tf.train.exponential_decay(1e-4, global_step, decay_steps=100, decay_rate=0.98, staircase=True)
  85.  
  86. train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(total_loss)
  87. saver = tf.train.Saver(max_to_keep=3)
  88. with tf.Session() as sess:
  89. if FLAGS.restore:
  90. print('continue training from previous checkpoint')
  91. ckpt = tf.train.latest_checkpoint(FLAGS.train_path)
  92. pre_step = int(ckpt.replace(FLAGS.train_path+'-', ''))
  93. saver.restore(sess, ckpt)
  94. else:
  95. # remove previous model and Coef
  96. if os.path.exists(FLAGS.train_path):
  97. shutil.rmtree(FLAGS.train_path)
  98. if os.path.exists(FLAGS.Coef_path):
  99. shutil.rmtree(FLAGS.Coef_path)
  100. os.makedirs(FLAGS.train_path)
  101. os.makedirs(FLAGS.Coef_path)
  102. # restore from pretrain
  103. sess.run(tf.global_variables_initializer())
  104. pre_step = 0
  105. ckpt = tf.train.latest_checkpoint(FLAGS.pretrain_path)
  106. variable_restore_op = slim.assign_from_checkpoint_fn(ckpt,slim.get_variables_to_restore(),ignore_missing_vars=True)
  107. variable_restore_op(sess)
  108.  
  109. inception_out = get_inception_graph()
  110. inception_output = get_inception_batch_avg()
  111. inception_output = inception_output.reshape(-1, inception_output.shape[1], inception_output.shape[2], 1)
  112. for step in range(FLAGS.max_train_steps):
  113. _, loss_value, Coef_val, rec_val, see_val, reg_val= \
  114. sess.run([train_op, total_loss, Coef, reconstruct_loss, self_express_loss, regularizer_loss],
  115. feed_dict={'inception_holder:0':inception_output})
  116. if step % 100 == 0:
  117. print("step %d :total_loss= %f,rec_loss= %f,see_val=%f,reg_val=%f"
  118. % (step,loss_value,rec_val, see_val,reg_val))
  119.  
  120. if step % 1000 == 0 and step > 0:
  121. if step > 500 :
  122. write_meta_graph = False
  123. else:
  124. write_meta_graph = True
  125. all_step = pre_step+step
  126. saver.save(sess, FLAGS.train_path, global_step=all_step,write_meta_graph=write_meta_graph)
  127. np.savetxt(FLAGS.Coef_path+str(all_step)+'.txt',Coef_val,fmt='%.6f')
  128.  
  129. def thrC(C):
  130. row,col = C.shape
  131. for i in range(row):
  132. for j in range(col):
  133. C[i,j]=abs(C[i,j])
  134. return C
  135.  
  136. def post_proC(C,N):
  137. # C: coefficient matrix
  138. C = 0.5 * (C + C.T)
  139. np.savetxt(FLAGS.cluster_path + 'C_abs.txt', C, fmt='%.6f')
  140. graph = coo_matrix(C)
  141. labels = spectral_clustering(graph, n_clusters=N)
  142. return labels
  143.  
  144. def vis(N,labels):
  145. ## visual
  146. for i in range(N):
  147. print(i)
  148. index = [j for j in range(len(labels)) if labels[j]==i]
  149. class_list=np.loadtxt(FLAGS.class_list,dtype=str)
  150.  
  151. sub_class_list = class_list[index]
  152. np.savetxt(os.path.join(FLAGS.cluster_path, str(i) + '.txt'), sub_class_list, fmt='%s')
  153. if vis:
  154. dir_path = os.path.join(FLAGS.cluster_path, str(i))
  155. if os.path.exists(dir_path):
  156. shutil.rmtree(dir_path)
  157. os.makedirs(dir_path)
  158. # copy an example to dir_path
  159. for sub_class_item in sub_class_list:
  160. img_path = os.path.join(FLAGS.img_path, sub_class_item)
  161. random_img = random.choice(os.listdir(img_path))
  162. src = os.path.join(img_path, random_img)
  163. dst = os.path.join(dir_path, random_img)
  164.  
  165. shutil.copyfile(src, dst)
  166.  
  167. if __name__ == '__main__':
  168. interface()
  169.  
  170. C=np.loadtxt('Coef/199000.txt') #系数,相似度矩阵
  171. C=thrC(C)
  172. N=32
  173. grp = post_proC(C,N)
  174.  
  175. vis(N,grp)

tensorflow-用DASC结合Inception-v3对imagenet2012聚类实现的更多相关文章

  1. Inception V3 的 tensorflow 实现

    tensorflow 官方给出的实现:models/inception_v3.py at master · tensorflow/models · GitHub 1. 模型结构 首先来看 Incept ...

  2. 源码分析——迁移学习Inception V3网络重训练实现图片分类

    1. 前言 近些年来,随着以卷积神经网络(CNN)为代表的深度学习在图像识别领域的突破,越来越多的图像识别算法不断涌现.在去年,我们初步成功尝试了图像识别在测试领域的应用:将网站样式错乱问题.无线领域 ...

  3. 微调Inception V3网络-对Satellite分类

    目录 1. 流程概述 2. 准备数据集 2.1 Satellite数据集介绍 3. Inception V3网络 4. 训练 4.1 基于Keras微调Inception V3网络 4.2 Keras ...

  4. 1、VGG16 2、VGG19 3、ResNet50 4、Inception V3 5、Xception介绍——迁移学习

    ResNet, AlexNet, VGG, Inception: 理解各种各样的CNN架构 本文翻译自ResNet, AlexNet, VGG, Inception: Understanding va ...

  5. 脸型分类-Face shape classification using Inception v3

    本文链接:https://blog.csdn.net/u011961856/article/details/77984667函数解析github 代码:https://github.com/adoni ...

  6. 网络结构解读之inception系列四:Inception V3

    网络结构解读之inception系列四:Inception V3   Inception V3根据前面两篇结构的经验和新设计的结构的实验,总结了一套可借鉴的网络结构设计的原则.理解这些原则的背后隐藏的 ...

  7. 从GoogLeNet至Inception v3

    从GoogLeNet至Inception v3 一.CNN发展纵览 我们先来看一张图片: 1985年,Rumelhart和Hinton等人提出了后向传播(Back Propagation,BP)算法( ...

  8. 经典分类CNN模型系列其五:Inception v2与Inception v3

    经典分类CNN模型系列其五:Inception v2与Inception v3 介绍 Inception v2与Inception v3被作者放在了一篇paper里面,因此我们也作为一篇blog来对其 ...

  9. [译]与TensorFlow的第一次接触(三)之聚类

    转自 [译]与TensorFlow的第一次接触(三)之聚类 2016.08.09 16:58* 字数 4316 阅读 7916评论 5喜欢 18 前一章节中介绍的线性回归是一种监督学习算法,我们使用数 ...

  10. 深度学习面试题29:GoogLeNet(Inception V3)

    目录 使用非对称卷积分解大filters 重新设计pooling层 辅助构造器 使用标签平滑 参考资料 在<深度学习面试题20:GoogLeNet(Inception V1)>和<深 ...

随机推荐

  1. cc.Node 的坐标空间与ACTION的学习

    1.创建二维的向量坐标 //创建向量坐标方法一 let new_pos1 = new cc.Vec2(100, 100); //创建向量坐标方法二 let new_pos2 = cc.v2(200, ...

  2. Mac App开发

    1. icns制作 在线工具: https://iconverticons.com/online/ 2. 替换dmg图标 选中dmg文件 右键, 选择显示简介 将icns图表拖拽到简介弹出框的左上角图 ...

  3. css中的宽度

    浏览器通过CSS对元素的盒子模型的描述进行页面渲染的.因此,元素的宽度受到父元素.css描述的影响. 通常,元素的宽度是指盒子模型中content-box所占用的宽度.也就是说,默认box-sizin ...

  4. Python 实现 动态规划 /斐波那契数列

    1.斐波那契数列 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数 ...

  5. spring-aop 的注释用法

    一.书写增强有效代码 //切面注释@Aspectpublic class errorLogger { private static Logger logger = Logger.getLogger(e ...

  6. C# SortedDictionary以及SortedList的浅谈

    msdn叙述:The SortedDictionary<TKey, TValue> generic class is a binary search tree with O(log n) ...

  7. webpack配置css相关loader注意先后顺序

    一.问题描述 在webpack3中,引入animate.css失败. 二.问题分析 1.难道是入口main.js引用方式不对? import animate from 'animate.css' 2. ...

  8. DWM1000 巧用Status 快速Debug

    在Debug DWM1000 的时候,可以巧用Status 加快Debug,例如如下代码 if (status_reg & SYS_STATUS_RXFCG) { …… } else { sp ...

  9. VS2015编译FFMPEG,修改FFmpeg缓冲区大小解决实时流解码丢包问题,FFmpeg错误rtsp流地址卡死的问题,设置超时

    之前尝试过很多网上利用Windows编译FFmpeg的文章,都没有办法编译X64位的FFmpeg,有些教程中有专门提到编译64位的FFmpeg需要下载mingw-w64-install,但是编译的过程 ...

  10. SVN-您的主机中的软件中止了一个已建立的连接

    关于这个问题,网络上有各种解决的办法,关闭防火墙,HTTP/HTTPS切换,改端口... ...但我都试了没有用.本来一直用的好好的,突然就出现了这个问题,而且在几分钟前都是正常的.下面来说说我都干了 ...