当前位置:   article > 正文

theano lasagna nolearn 实践,显著性检测深度学习_shallow and deep convolutional networks for salien

shallow and deep convolutional networks for saliency predictio

七小时入门theano

官方文档还是很值得参考的,

lasagne

theano


上面的源码是参考这个git的 

Shallow and Deep Convolutional Networks for Saliency Prediction

不过我改了训练集目前为止还没训练成功。。。就当是学习一下相关框架吧,以后如果训练成功了在更新

shallow-fig

数据预处理
  1. import cv2
  2. import numpy as np
  3. import cPickle as pickle
  4. #PIC_PATH='/home/zcb/datasets/trainSet/Stimuli/Alldata/'
  5. #SALMAP_PATH='/home/zcb/datasets/trainSet/FIXATIONMAPS/Alldata/'
  6. PIC_PATH='/home/zcb/datasets/ecssd/images/'
  7. SALMAP_PATH='/home/zcb/datasets/ecssd/ground_truth_mask/'
  8. datalist = open(PIC_PATH+'list.txt','r')
  9. namelist=[l.strip('\n') for l in datalist.readlines()]
  10. sallist = open(SALMAP_PATH+'list.txt','r')
  11. sallist=[l.strip('\n') for l in sallist.readlines()]
  12. input_h=48
  13. input_w=48
  14. output_h=24
  15. output_w=24
  16. #NumSample=len(namelist)
  17. NumSample=32
  18. X1 = np.zeros((NumSample, 3,input_h, input_w), dtype='float32')
  19. Y1 = np.zeros((NumSample, output_w*output_h), dtype='float32')
  20. print NumSample
  21. for i in range(NumSample):
  22. img = cv2.imread(PIC_PATH+namelist[i], cv2.IMREAD_COLOR)
  23. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  24. #print img.shape
  25. img = cv2.resize(img,(input_w,input_h),interpolation=cv2.INTER_CUBIC)
  26. #print img.shape
  27. #cv2.imshow('show',img)
  28. img=img.astype(np.float32)
  29. img = img /255.
  30. #img -= 1.
  31. if(cmp(img.shape , (input_h,input_w,3)) == 0):
  32. img = img.transpose(2,0,1).reshape(3, input_h, input_w)
  33. X1[i]=img
  34. else:
  35. print 'error'
  36. #np.set_printoptions(threshold='nan')
  37. label = cv2.imread(SALMAP_PATH+sallist[i],cv2.IMREAD_GRAYSCALE)
  38. #label = loadSaliencyMapSUN(names[i])
  39. label = cv2.resize(label,(output_w,output_h),interpolation=cv2.INTER_CUBIC)
  40. #cv2.imshow('label',label)
  41. #cv2.waitKey(0)
  42. label = label.astype(np.float32)/255.
  43. #label = misc.imresize(label,(48,48)) /
  44. #label = label -1.
  45. # print 'data',X1[i]
  46. # print 'label',label
  47. #Y1.append(label.reshape(1,48*48))
  48. Y1[i]=label.reshape(1,output_h*output_w)
  49. data_to_save = (X1, Y1)
  50. f = file('data_ecssd_T.cPickle', 'wb')
  51. pickle.dump(data_to_save, f, protocol=pickle.HIGHEST_PROTOCOL)
  52. f.close()


训练代码
代码如下:

  1. # add to kfkd.py
  2. import lasagne
  3. from lasagne.layers import *
  4. from lasagne.updates import nesterov_momentum
  5. from nolearn.lasagne import NeuralNet,BatchIterator
  6. import os
  7. import numpy as np
  8. from sklearn.utils import shuffle
  9. import cPickle as pickle
  10. import matplotlib.pyplot as plt
  11. import Image
  12. import ImageOps
  13. from scipy import misc
  14. import scipy.io
  15. import theano
  16. import sys
  17. sys.setrecursionlimit(10**8) # set the maximum depth
  18. def load():
  19. f = file('data_ecssd_T.cPickle', 'rb')
  20. loaded_obj = pickle.load(f)
  21. f.close()
  22. X, y = loaded_obj
  23. return X, y
  24. def float32(k):
  25. return np.cast['float32'](k)
  26. class AdjustVariable(object):
  27. def __init__(self, name, start=0.03, stop=0.001):
  28. self.name = name
  29. self.start, self.stop = start, stop
  30. self.ls = None
  31. def __call__(self, nn, train_history):
  32. if self.ls is None:
  33. self.ls = np.linspace(self.start, self.stop, nn.max_epochs)
  34. epoch = train_history[-1]['epoch']
  35. #if epoch > 200:
  36. # getattr(nn, self.name).set_value(0.01)
  37. #if epoch > 800:
  38. # getattr(nn, self.name).set_value(0.005)
  39. new_value = float32(self.ls[epoch - 1])
  40. getattr(nn, self.name).set_value(new_value)
  41. input_h=48
  42. input_w=48
  43. output_h=24
  44. output_w=24
  45. class FlipBatchIterator(BatchIterator):
  46. def transform(self, Xb, yb):
  47. Xb, yb = super(FlipBatchIterator, self).transform(Xb, yb)
  48. # Flip half of the images in this batch at random:
  49. bs = Xb.shape[0]
  50. indices = np.random.choice(bs, bs / 2, replace=False)
  51. Xb[indices] = Xb[indices, :, :, ::-1]
  52. tmp = yb[indices].reshape(bs/2,1,output_h,output_w)
  53. mirror = tmp[ :,:,:, ::-1]
  54. yb[indices] = mirror.reshape(bs/2,output_w*output_h)
  55. return Xb, yb
  56. class EarlyStopping(object):
  57. def __init__(self, patience=100):
  58. self.patience = patience
  59. self.best_valid = np.inf
  60. self.best_valid_epoch = 0
  61. self.best_weights = None
  62. def __call__(self, nn, train_history):
  63. current_valid = train_history[-1]['valid_loss']
  64. current_epoch = train_history[-1]['epoch']
  65. if current_valid < self.best_valid:
  66. self.best_valid = current_valid
  67. self.best_valid_epoch = current_epoch
  68. self.best_weights = nn.get_all_params_values()
  69. elif self.best_valid_epoch + self.patience < current_epoch:
  70. print("Early stopping.")
  71. print("Best valid loss was {:.6f} at epoch {}.".format(
  72. self.best_valid, self.best_valid_epoch))
  73. nn.load_params_from(self.best_weights)
  74. raise StopIteration()
  75. def my_loss(a,b):
  76. return theano.tensor.abs_(a-b)
  77. layers0 = [
  78. # layer dealing with the input data
  79. (InputLayer, {'shape':(None, 3, input_h, input_w)}),
  80. # first stage of our convolutional layers
  81. (Conv2DLayer, {'num_filters':32 , 'filter_size': 5,'pad':1,'W':lasagne.init.Uniform()}),
  82. #(Conv2DLayer, {'num_filters':32 , 'filter_size': 7,'pad':1,'W':lasagne.init.Uniform()}),
  83. (MaxPool2DLayer, {'pool_size': 2}),
  84. # second stage of our convolutional layers
  85. (Conv2DLayer, {'num_filters':64 , 'filter_size': 3,'pad':1,'W':lasagne.init.Uniform()}),
  86. #(Conv2DLayer, {'num_filters':16 , 'filter_size': 3,'pad':1,'W':lasagne.init.Uniform()}),
  87. (MaxPool2DLayer, {'pool_size': 2}),
  88. # third stage of our convolutional layers
  89. (Conv2DLayer, {'num_filters': 128, 'filter_size': 3,'pad':1,'W':lasagne.init.Uniform()}),
  90. #(Conv2DLayer, {'num_filters': 32, 'filter_size': 3,'pad':1,'W':lasagne.init.Uniform()}),
  91. (MaxPool2DLayer, {'pool_size': 2}),
  92. #(Conv2DLayer, {'num_filters': 32, 'filter_size': 3,'pad':1,'W':lasagne.init.Uniform()}),
  93. #(Conv2DLayer, {'num_filters': 32, 'filter_size': 3,'pad':1,'W':lasagne.init.Uniform()}),
  94. #(Upscale2DLayer,{'scale_factor':2}),
  95. #(Conv2DLayer, {'num_filters': 16, 'filter_size': 3,'pad':1,'W':lasagne.init.Uniform()}),
  96. #(Conv2DLayer, {'num_filters': 16, 'filter_size': 3,'pad':1,'W':lasagne.init.Uniform()}),
  97. #(Upscale2DLayer,{'scale_factor':2}),
  98. #(Conv2DLayer, {'num_filters': 8, 'filter_size': 3,'pad':1,'W':lasagne.init.Uniform()}),
  99. #(Conv2DLayer, {'num_filters': 8, 'filter_size': 3,'pad':1,'W':lasagne.init.Uniform()}),
  100. #(Conv2DLayer, {'num_filters': 1, 'filter_size': 3,'pad':1,'nonlinearity':lasagne.nonlinearities.sigmoid}),
  101. #(Conv2DLayer, {'num_filters': 1, 'filter_size': 3,'pad':1,'nonlinearity':None}),
  102. #(FlattenLayer,{}),
  103. # two dense layers with dropout
  104. #(DropoutLayer, {}),
  105. (DenseLayer, {'num_units': output_w*output_h*2}),
  106. (FeaturePoolLayer, {'pool_size':2}),
  107. # the output layer
  108. #(DenseLayer, {'num_units': output_w*output_h,'nonlinearity':lasagne.nonlinearities.sigmoid}),
  109. (DenseLayer, {'num_units': output_w*output_h,'nonlinearity':None}),
  110. #(DenseLayer, {'num_units': 10, 'nonlinearity': softmax}),
  111. ]
  112. net2 = NeuralNet(
  113. # layers=[
  114. # ('input', layers.InputLayer),
  115. # ('conv1', layers.Conv2DLayer),
  116. # ('pool1', layers.MaxPool2DLayer),
  117. # ('conv2', layers.Conv2DLayer),
  118. # ('pool2', layers.MaxPool2DLayer),
  119. # ('conv3', layers.Conv2DLayer),
  120. # ('pool3', layers.MaxPool2DLayer),
  121. # ('hidden4', layers.DenseLayer),
  122. # ('maxout6',layers.FeaturePoolLayer),
  123. # ('output', layers.DenseLayer),
  124. # ],
  125. # input_shape=(None, 3, input_h, input_w),
  126. # #conv1
  127. # conv1_num_filters=16, conv1_filter_size=(5, 5),
  128. # conv1_nonlinearity=lasagne.nonlinearities.rectify,
  129. # conv1_W=lasagne.init.GlorotUniform(),
  130. # conv1_pad=1,
  131. # conv1_stride=1,
  132. # conv4_num_filters=16, conv4_filter_size=(3, 3),
  133. # conv4_nonlinearity=lasagne.nonlinearities.rectify,
  134. # conv4_W=lasagne.init.GlorotUniform(),
  135. # conv4_pad=1,
  136. # conv4_stride=1,
  137. # #pool1
  138. # pool1_pool_size=(2, 2),
  139. # #conv2
  140. # conv2_num_filters=32, conv2_filter_size=(3, 3),
  141. # conv2_nonlinearity=lasagne.nonlinearities.rectify,
  142. # conv2_W=lasagne.init.GlorotUniform(),
  143. # conv2_pad=1,
  144. # conv2_stride=1,
  145. # #pool2
  146. # pool2_pool_size=(2, 2),
  147. # #conv3
  148. # conv3_num_filters=32, conv3_filter_size=(3, 3),
  149. # conv3_nonlinearity=lasagne.nonlinearities.rectify,
  150. # conv3_W=lasagne.init.GlorotUniform(),
  151. # conv3_pad=1,
  152. # #pool3
  153. # pool3_pool_size=(2, 2),
  154. # hidden4_num_units=output_w*output_h*2,
  155. # maxout6_pool_size=2,output_num_units=output_h*output_w,output_nonlinearity=None,
  156. layers=layers0,
  157. #update=lasagne.updates.rmsprop,
  158. update_learning_rate=theano.shared(float32(0.05)),
  159. update_momentum=theano.shared(float32(0.9)),
  160. #objective_loss_function=my_loss,#lasagne.objectives.squared_error,
  161. regression=True,
  162. on_epoch_finished=[
  163. AdjustVariable('update_learning_rate', start=0.03, stop=0.0001),
  164. AdjustVariable('update_momentum', start=0.9, stop=0.999),
  165. #EarlyStopping(patience=150),
  166. ],
  167. batch_iterator_train=FlipBatchIterator(batch_size=128),
  168. max_epochs=800,
  169. verbose=1,
  170. )
  171. X, y = load()
  172. print net2.objective_loss_function,net2.train_split.eval_size
  173. #print("X.shape == {}; X.min == {:.3f}; X.max == {:.3f}".format(X.shape, X.min(), X.max()))
  174. #print("y.shape == {}; y.min == {:.3f}; y.max == {:.3f}".format(y.shape, y.min(), y.max()))
  175. X = X.astype(np.float32)
  176. y = y.astype(np.float32)
  177. net2.fit(X, y)
  178. with open('JuntingNet_SALICON.pickle', 'wb') as f:
  179. pickle.dump(net2, f, -1)



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/649892
推荐阅读
相关标签
  

闽ICP备14008679号