[关闭]
@BruceWang 2018-07-09T21:30:46.000000Z 字数 73033 阅读 2542

基于深度学习的心音分类模型代码说明

课题


1. requirement.txt

python程序使用的packages,以及程序需求。

  1. ######
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri May 4 10:54:49 2018
  5. @author: Wang
  6. platform: win_10_x64
  7. """
  8. ######
  9. absl-py 0.2.0
  10. astor 0.6.2
  11. audioread 2.1.5
  12. bleach 1.5.0
  13. certifi 2016.2.28
  14. cycler 0.10.0
  15. decorator 4.3.0
  16. Django 2.0.4
  17. enum34 1.1.6
  18. gast 0.2.0
  19. grpcio 1.11.0
  20. html5lib 0.9999999
  21. icu 57.1
  22. image 1.5.20
  23. joblib 0.11
  24. jpeg 9b
  25. Keras 2.1.2
  26. libpng 1.6.30
  27. librosa 0.6.0
  28. llvmlite 0.23.0
  29. Markdown 2.6.10
  30. matplotlib 2.0.2
  31. mkl 2017.0.3
  32. numba 0.38.0
  33. numpy 1.13.1
  34. numpy 1.13.3
  35. opencv-python 3.4.1
  36. openssl 1.0.2l
  37. pandas 0.20.3
  38. Pillow 5.1.0
  39. pip 9.0.1
  40. pip 10.0.1
  41. protobuf 3.5.1
  42. pyparsing 2.2.0
  43. pyqt 5.6.0
  44. pyreadline 2.1
  45. python 3.5.4
  46. python-dateutil 2.6.1
  47. pytz 2017.2
  48. PyYAML 3.12
  49. qt 5.6.2
  50. resampy 0.2.0
  51. scikit-learn 0.19.0
  52. scikit-learn 0.19.1
  53. scipy 0.19.1
  54. setuptools 38.2.5
  55. setuptools 36.4.0
  56. sip 4.18
  57. six 1.10.0
  58. six 1.11.0
  59. tensorboard 1.8.0
  60. tensorflow 1.8.0
  61. tensorflow-gpu 1.4.0
  62. tensorflow-tensorboard 0.4.0rc3
  63. termcolor 1.1.0
  64. tk 8.5.18
  65. vc 14
  66. vs2015_runtime 14.0.25420
  67. Werkzeug 0.13
  68. wheel 0.30.0
  69. wheel 0.29.0
  70. wincertstore 0.2
  71. zlib

2. test_and_details.py

文件读取和说明;以及核心packages安装说明。

  1. # -*- coding: utf-8 -*-
  2. """
  3. notepad++ Editor
  4. """
  5. # pydub 需要用到的模块
  6. # pydub 安装教程:https://blog.csdn.net/qq_25883823/article/details/52749279
  7. # github_API:https://github.com/jiaaro/pydub/blob/master/API.markdown
  8. # 中文说明:https://blog.csdn.net/tyfbhlxd/article/details/72046552
  9. # 数据链接:https://bhichallenge.med.auth.gr/ 我使用的是第110个数据
  10. from pydub import AudioSegment
  11. import os, re
  12. wav_path = r"C:\Users\aixin\Desktop\lungsound\LungSoundFromICBHIchallenge\ICBHI_final_database\110_1p1_Al_sc_Meditron.wav"
  13. txt_path = r"C:\Users\aixin\Desktop\lungsound\LungSoundFromICBHIchallenge\ICBHI_final_database\110_1p1_Al_sc_Meditron.txt"
  14. save_path = r"C:\Users\aixin\Desktop\lungsound\LungSoundFromICBHIchallenge\database_segmentation"
  15. wav = AudioSegment.from_wav(wav_path)
  16. filename_wav = os.listdir(root_wav_path)
  17. filename_txt = os.listdir(root_txt_path)
  18. # 得到音频基本信息
  19. ################################################################################
  20. from pydub import AudioSegment
  21. # sound = AudioSegment.from_file("sound1.wav")
  22. # loudness = sound.dBFS
  23. # 取得音频文件音量分贝数
  24. # channel_count = sound.channels
  25. # 取得音频文件声道数
  26. # bytes_per_sample = sound.sample_width
  27. # 取得音频文件采样宽度
  28. # frames_per_second = sound.frame_rate
  29. # 取得音频文件采样频率
  30. # loudness = sound.rms
  31. # 获取音频音量大小,该值通常用来计算分贝数(dB= 20×lgX)
  32. # assert sound.duration_seconds == (len(sound) / 1000.0)
  33. # 取得音频的持续时间,同 len()
  34. # number_of_frames_in_sound = sound.frame_count()
  35. # number_of_frames_in_200ms_of_sound = sound.frame_count(ms=200)
  36. # 取得音频的frame数量
  37. ################################################################################
  38. '''
  39. with open(txt_path,'r') as f:
  40. tag_00 = 0
  41. tag_11 = 0
  42. lines = f.readlines()
  43. print(type(lines)) # list
  44. for line in lines:
  45. t = [float(i) for i in line.strip().split('\t')]
  46. ts = [round(i*1000) for i in t] # 转换成毫秒, pydub中的标准时间为毫秒
  47. if ts[2:] == [0, 0]:
  48. # print(ts)
  49. part1 = wav[ts[0]:ts[1]] # 把标签是0,0的切割出来
  50. tag_00 += part1
  51. else:
  52. print(ts)
  53. part2 = wav[ts[0]:ts[1]]
  54. tag_11 += part2
  55. print(tag_00)
  56. print(type(tag_00))
  57. print(tag_00.shape)
  58. tag_00.export(save_path + "\\" + str(wav_name) + '_' + str('00') + '.wav', format="wav")
  59. tag_11.export(save_path + "\\" + str(wav_name) + '_' + str('11') + '.wav', format="wav")
  60. '''
  61. '''
  62. # default 100 ms crossfade
  63. combined = sound1.append(sound2)
  64. # 5000 ms crossfade
  65. combined_with_5_sec_crossfade = sound1.append(sound2, crossfade=5000)
  66. # no crossfade
  67. no_crossfade1 = sound1.append(sound2, crossfade=0)
  68. root_path = r"C:\Users\aixin\Desktop\lungsound\LungSoundFromICBHIchallenge\ICBHI_final_database"
  69. i = 0
  70. for each in os.listdir(root_path):
  71. filename_wav = re.findall(r"(.*?)\.wav", each)
  72. # filename_txt = re.findall(r"(.*?)\.txt", each)
  73. if filename_wav:
  74. print(filename_wav)
  75. i += 1
  76. print(i)
  77. # if filename_wav == filename_txt:
  78. # print(filename_wav)
  79. # print(filename_txt)
  80. # else:
  81. # print("there is some filenames are not compitable")
  82. # 最后的每一个文件保存的应该有两个位置:
  83. # 这里的00代表一种病也没的,11代表其他的;
  84. # 我不想分成四类了,只要两类搞定了,四类我肯定也会的。
  85. # save_path + each_filename + str("00") + '.wav'
  86. # save_path + each_filename + str("11") + '.wav'
  87. '''
  88. #------------------------------------------------------------------------------#

3.data_preprocess.py

数据预处理,生成训练数据,得到数据和标签文件,结构化数据。

  1. ###############################################################################
  2. # 预备函数
  3. def endWith(*endstring):
  4. ends = endstring
  5. def run(s):
  6. f = map(s.endswith,ends)
  7. if True in f: return s
  8. return run
  9. def lss_seg(wav_path,wav_name):
  10. lung_sound = AudioSegment.from_file(wav_path)
  11. five_seconds = 5 * 1000
  12. ten_seconds = 10 * 1000
  13. fi = lung_sound[:ten_seconds]
  14. mi = lung_sound[five_seconds:ten_seconds]
  15. la = lung_sound[-five_seconds:]
  16. fi.export(seg_path+'\\'+ wav_name[:-4] + '_'+ 'fi.wav', format="wav")
  17. mi.export(seg_path+'\\'+ wav_name[:-4] + '_'+ 'mi.wav', format="wav")
  18. la.export(seg_path+'\\'+ wav_name[:-4] + '_'+ 'la.wav', format="wav")
  19. # fi.export(seg_path+'\\'+ wav_name[:-4] + '_'+ 'fi.wav', format="wav")
  20. # mi.export(seg_path+'\\'+ wav_name[:-4] + '_'+ 'mi.wav', format="wav")
  21. # la.export(seg_path+'\\'+ wav_name[:-4] + '_'+ 'la.wav', format="wav")
  22. def get_wavedata(wav_name):
  23. f = wave.open(filepath+'\\'+wav_name,'rb')
  24. params = f.getparams()
  25. nchannels, sampwidth, framerate, nframes = params[:4]
  26. strData = f.readframes(nframes)
  27. #读取音频,字符串格式
  28. waveData = np.fromstring(strData,dtype=np.int16)
  29. #将字符串转化为int
  30. waveData = waveData*1.0/(max(abs(waveData)))
  31. #wave幅值归一化
  32. waveData = np.reshape(waveData,[nframes,nchannels]).T
  33. f.close()
  34. return waveData, framerate
  35. def save_new_img(spec_path):
  36. fig = plt.figure()
  37. fig.set_size_inches(0.5, 0.5)
  38. plt.axis('off') # no axis
  39. plt.axes([0., 0., 1., 1.], frameon=False, xticks=[], yticks=[]) # Remove the white edge
  40. plt.specgram(waveData[0],Fs = framerate, scale_by_freq = True, sides = 'default')
  41. plt.savefig(spec_path, bbox_inches=None, pad_inches=0)
  42. plt.close()
  43. #################################################################################
  44. for f in files:
  45. if not f.endswith('.mp3'):
  46. # Skip any non-MP3 files
  47. continue
  48. mp3_file = os.path.join(path, f)
  49. #################################################################################
  50. import os, re
  51. import wave
  52. import numpy as np
  53. import matplotlib.pyplot as plt
  54. import matplotlib.image as mpimg
  55. from pydub import AudioSegment
  56. #################################################################################
  57. ## 读取频谱图数据--得到X, y, numpy数据
  58. image_base_path = r"C:\Users\aixin\Desktop\lungsound\LSS_seg_img"
  59. filenames= os.listdir(image_base_path)
  60. img0 = mpimg.imread(image_base_path +'\\'+ filenames[0])
  61. img0 = np.expand_dims(img0,axis=0)
  62. print("开始将图片转为数组")
  63. label = []
  64. i = 0
  65. j = 0
  66. k = 0
  67. for jpg_name in filenames:
  68. print(jpg_name)
  69. if jpg_name.startswith('0_'):
  70. print(jpg_name)
  71. print('00000000000000000000')
  72. i = i+1
  73. label.append(0)
  74. else:
  75. print(jpg_name)
  76. print('11111111111111111111')
  77. j = j+1
  78. label.append(1)
  79. img = mpimg.imread(image_base_path +'\\'+ jpg_name)
  80. img = np.expand_dims(img,axis=0)
  81. X = np.concatenate((img0,img),axis=0)
  82. img0 = X
  83. k = k+1
  84. X_path = r"C:\Users\aixin\Desktop\lungsound\LSS_x_y\X.npy"
  85. np.save(X_path, X)
  86. yarray = np.array(label)
  87. y_path = r"C:\Users\aixin\Desktop\lungsound\LSS_x_y\y.npy"
  88. np.save(y_path, yarray)
  89. ###############################################################################
  90. ## 读取5秒音频文件--生成频谱图--保存成无白边的jpg图像格式
  91. filepath = r"C:\Users\aixin\Desktop\lungsound\LSS_seg"
  92. save_path = r"C:\Users\aixin\Desktop\lungsound\LSS_seg_img"
  93. filenames= os.listdir(filepath)
  94. #得到文件夹下的所有文件名称
  95. i = 0
  96. j = 0
  97. a = endWith('.wav')
  98. f_file = filter(a,filenames)
  99. for wav_name in f_file:
  100. print(wav_name)
  101. waveData,framerate = get_wavedata(wav_name)
  102. if re.findall(r'正常',wav_name):
  103. print(wav_name)
  104. if re.findall(r'_fi',wav_name):
  105. spec_path0_fi = save_path+'\\'+ '0_' + str(i) + '_fi' + '.jpg'
  106. save_new_img(spec_path0_fi)
  107. elif re.findall(r'_mi',wav_name):
  108. spec_path0_mi = save_path+'\\'+ '0_' + str(i) + '_mi' + '.jpg'
  109. save_new_img(spec_path0_mi)
  110. else:
  111. spec_path0_la = save_path+'\\'+ '0_' + str(i) + '_la' + '.jpg'
  112. save_new_img(spec_path0_la)
  113. i = i+1
  114. else:
  115. print(wav_name)
  116. if re.findall(r'_fi',wav_name):
  117. spec_path1_fi = save_path+'\\'+ '1_' + str(j) + '_fi' + '.jpg'
  118. save_new_img(spec_path1_fi)
  119. elif re.findall(r'_mi',wav_name):
  120. spec_path1_mi = save_path+'\\'+ '1_' + str(j) + '_mi' + '.jpg'
  121. save_new_img(spec_path1_mi)
  122. else:
  123. spec_path1_la = save_path+'\\'+ '1_' + str(j) + '_la' + '.jpg'
  124. save_new_img(spec_path1_la)
  125. j = j+1
  126. print('len of filenames:', len(filenames))
  127. #############################################################################
  128. ## 重新分数据,5秒一个数据集,原来是15秒长度,如:现在是原来数据的3倍、
  129. filepath = r"C:\Users\aixin\Desktop\lungsound\LSS_Nname_545_both"
  130. seg_path = r"C:\Users\aixin\Desktop\lungsound\LSS_seg"
  131. filenames = os.listdir(filepath)
  132. a = endWith('.wav')
  133. f_file = filter(a,filenames)
  134. for wav_name in f_file:
  135. print(wav_name)
  136. wav_path = filepath + '\\' + wav_name
  137. lss_seg(wav_path,wav_name)
  138. ######################################################################
  139. # 读取频谱图数据--得到 x,y, numpy数据
  140. import os,re
  141. import cv2
  142. import matplotlib.image as mpimg
  143. import numpy as np
  144. image_base_path = r"C:\Users\aixin\Desktop\lungsound\minst_fake"
  145. filenames= os.listdir(image_base_path)
  146. n = filenames.__len__()
  147. # 获取图片的个数
  148. print("开始将图片转为数组")
  149. l = []
  150. label = []
  151. i = 0
  152. j = 0
  153. k = 0
  154. for jpg_name in filenames:
  155. # img = cv2.imread(image_base_path +'\\'+ filenames[k])
  156. img = mpimg.imread(image_base_path +'\\'+ filenames[k])
  157. l.append(img)
  158. if re.findall(r'0_',jpg_name):
  159. print(jpg_name)
  160. i = i+1
  161. label.append(0)
  162. else:
  163. print(jpg_name)
  164. j = j+1
  165. label.append(1)
  166. print(k)
  167. k = k+1
  168. Xarray = np.array(l)
  169. X_path = r"C:\Users\aixin\Desktop\lungsound\minst_fake\X.npy"
  170. np.save(X_path, Xarray)
  171. yarray = np.array(label)
  172. y_path = r"C:\Users\aixin\Desktop\lungsound\minst_fake\y.npy"
  173. np.save(y_path, yarray)
  174. # 图像先升维,然后合并
  175. a = np.expand_dims(np.random.randint(2,size=(2,3)), axis=0)
  176. b = np.expand_dims(np.random.randint(2,size=(2,3)), axis=0)
  177. c = np.concatenate((a,b),axis=0)
  178. #
  179. ########################################################################
  180. # 读取频谱图数据--得到y, numpy数据
  181. filepath = r"C:\Users\aixin\Desktop\lungsound\LSS"
  182. filenames = os.listdir(filepath)
  183. a = endWith('.jpg')
  184. f_file = filter(a,filenames)
  185. i = 0
  186. j = 0
  187. label = []
  188. for jpg_name in f_file:
  189. if re.findall(r'0_',jpg_name):
  190. print(jpg_name)
  191. i = i+1
  192. label.append(0)
  193. else:
  194. print(jpg_name)
  195. j = j+1
  196. label.append(1)
  197. y_label = np.array(label)
  198. print(label)
  199. label_path = r"C:\Users\aixin\Desktop\lungsound\LSS\label.npy"
  200. np.save(label_path, y_label)
  201. ########################################################################
  202. ########################## 以下部分使用待定 ############################
  203. ########################## 以下部分使用待定 ############################
  204. ########################################################################
  205. # 读取音频文件--保存图像数据,并且切割成正方形
  206. import os, re
  207. import cv2
  208. import wave
  209. import matplotlib.pyplot as plt
  210. import numpy as np
  211. filepath = r"C:\Users\aixin\Desktop\lungsound\LSS"
  212. filenames= os.listdir(filepath)
  213. #得到文件夹下的所有文件名称
  214. i = 0
  215. j = 0
  216. a = endWith('.wav')
  217. f_file = filter(a,filenames)
  218. for wav_name in f_file:
  219. print(wav_name)
  220. waveData,framerate = get_wavedata(wav_name)
  221. if re.findall(r'正常',wav_name):
  222. print(wav_name)
  223. spec_path0 = filepath+'\\'+ '0_' + str(i) + '.jpg'
  224. i = i+1
  225. save_img(spec_path0)
  226. else:
  227. spec_path1 = filepath+'\\'+ '1_' + str(j) + '.jpg'
  228. j = j+1
  229. save_img(spec_path1)
  230. filepath = r"C:\Users\aixin\Desktop\lungsound\LSS"
  231. filenames = os.listdir(filepath)
  232. a = endWith('.jpg')
  233. f_file = filter(a,filenames)
  234. for jpg_name in f_file:
  235. print(jpg_name)
  236. save_cropped_img(filepath+'\\'+jpg_name, filepath+'\\'+jpg_name)
  237. ############################################################################
  238. # 画语谱图
  239. # 获取音频信息:Wave_read.getparams用法:
  240. import wave
  241. import matplotlib.pyplot as plt
  242. import numpy as np
  243. import os
  244. wav_path = r"C:\Users\aixin\Desktop\lungsound\LSS\FT___鼾音__李庆_男_74_000_000_现在吸烟_26_20150418101410_000163_025.wav"
  245. filepath = r"C:\Users\aixin\Desktop\lungsound\LSS"
  246. filenames= os.listdir(filepath) #得到文件夹下的所有文件名称
  247. # f = wave.open(filepath+'\\' + filenames[1],'rb')
  248. f = wave.open(wav_path,'rb')
  249. params = f.getparams()
  250. nchannels, sampwidth, framerate, nframes = params[:4]
  251. strData = f.readframes(nframes)#读取音频,字符串格式
  252. waveData = np.fromstring(strData,dtype=np.int16)#将字符串转化为int
  253. waveData = waveData*1.0/(max(abs(waveData)))#wave幅值归一化
  254. waveData = np.reshape(waveData,[nframes,nchannels]).T
  255. f.close()
  256. # plot the wave
  257. spec_path = filepath+'\\'+ '0000000000000000' + '.jpg'
  258. plt.specgram(waveData[0],Fs = framerate, scale_by_freq = True, sides = 'default')
  259. # plt.axis('tight')
  260. plt.axis('off')
  261. plt.savefig(spec_path, dpi=100)
  262. # plt.savefig(spec_path, dpi=80)
  263. # plt.savefig(spec_path, dpi=50)
  264. ############################################################################
  265. import audiosegment
  266. print("Reading in the wave file...")
  267. seg = audiosegment.from_file(filename)
  268. print("Information:")
  269. print("Channels:", seg.channels)
  270. print("Bits per sample:", seg.sample_width * 8)
  271. print("Sampling frequency:", seg.frame_rate)
  272. print("Length:", seg.duration_seconds, "seconds")
  273. #
  274. freqs, times, amplitudes = seg.spectrogram(window_length_s=0.03, overlap=0.5)
  275. amplitudes = 10 * np.log10(amplitudes + 1e-9)
  276. # Plot
  277. plt.pcolormesh(times, freqs, amplitudes)
  278. plt.xlabel("Time in Seconds")
  279. plt.ylabel("Frequency in Hz")
  280. plt.show()

4. ls_cnn_train.py, ls_cnn_test.py , ls_data.py, cnn_model.py

CNN模型训练、测试、集成

  1. # Some code was borrowed from
  2. # https://github.com/petewarden/tensorflow_makefile/blob/master/tensorflow/models/image/mnist/convolutional.py
  3. from __future__ import absolute_import
  4. from __future__ import division
  5. from __future__ import print_function
  6. import numpy
  7. import tensorflow as tf
  8. import tensorflow.contrib.slim as slim
  9. import ls_data
  10. import cnn_model
  11. MODEL_DIRECTORY = "model/model.ckpt"
  12. LOGS_DIRECTORY = "logs/train"
  13. # Params for Train
  14. training_epochs = 10# 10 for augmented training data, 20 for training data
  15. TRAIN_BATCH_SIZE = 50
  16. display_step = 100
  17. validation_step = 500
  18. # Params for test
  19. TEST_BATCH_SIZE = 5000
  20. def train():
  21. # Some parameters
  22. batch_size = TRAIN_BATCH_SIZE
  23. num_labels = ls_data.NUM_LABELS
  24. # Prepare ls_data data
  25. train_total_data, train_size, validation_data, validation_labels, test_data, test_labels = ls_data.prepare_ls_data(True)
  26. # Boolean for MODE of train or test
  27. is_training = tf.placeholder(tf.bool, name='MODE')
  28. # tf Graph input
  29. x = tf.placeholder(tf.float32, [None, 784])
  30. y_ = tf.placeholder(tf.float32, [None, 2]) #answer
  31. # Predict
  32. y = cnn_model.CNN(x)
  33. # Get loss of model
  34. with tf.name_scope("LOSS"):
  35. loss = slim.losses.softmax_cross_entropy(y,y_)
  36. # loss = slim.losses.softmax_cross_entropy(y,y_)
  37. #########
  38. # Create a summary to monitor loss tensor
  39. tf.scalar_summary('loss', loss)
  40. # Define optimizer
  41. with tf.name_scope("ADAM"):
  42. # Optimizer: set up a variable that's incremented once per batch and
  43. # controls the learning rate decay.
  44. batch = tf.Variable(0)
  45. # batch = tf.Variable(0)
  46. # batch = tf.Variable(0)
  47. # batch = tf.Variable(0)
  48. learning_rate = tf.train.exponential_decay(
  49. 1e-4, # Base learning rate.
  50. batch * batch_size, # Current index into the dataset.
  51. train_size, # Decay step.
  52. 0.95, # Decay rate.
  53. staircase=True)
  54. # Use simple momentum for the optimization.
  55. train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss,global_step=batch)
  56. # Create a summary to monitor learning_rate tensor
  57. tf.scalar_summary('learning_rate', learning_rate)
  58. # Get accuracy of model
  59. with tf.name_scope("ACC"):
  60. correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
  61. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  62. # accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  63. # accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  64. # none
  65. # accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  66. # Create a summary to monitor accuracy tensor
  67. tf.scalar_summary('acc', accuracy)
  68. # Merge all summaries into a single op
  69. merged_summary_op = tf.merge_all_summaries()
  70. # Add ops to save and restore all the variables
  71. saver = tf.train.Saver()
  72. sess = tf.InteractiveSession()
  73. sess.run(tf.global_variables_initializer(), feed_dict={is_training: True})
  74. # sess.run(tf.global_variables_initializer(), feed_dict={is_training: True})
  75. # sess.run(tf.global_variables_initializer(), feed_dict={is_training: True})
  76. # sess.run(tf.global_variables_initializer(), feed_dict={is_training: True})
  77. # Training cycle
  78. total_batch = int(train_size / batch_size)
  79. # op to write logs to Tensorboard
  80. summary_writer = tf.train.SummaryWriter(LOGS_DIRECTORY, graph=tf.get_default_graph())
  81. # Save the maximum accuracy value for validation data
  82. max_acc = 0.
  83. # Loop for epoch
  84. for epoch in range(training_epochs):
  85. # Random shuffling
  86. numpy.random.shuffle(train_total_data)
  87. train_data_ = train_total_data[:, :-num_labels]
  88. train_labels_ = train_total_data[:, -num_labels:]
  89. # Loop over all batches
  90. for i in range(total_batch):
  91. # Compute the offset of the current minibatch in the data.
  92. offset = (i * batch_size) % (train_size)
  93. batch_xs = train_data_[offset:(offset + batch_size), :]
  94. batch_ys = train_labels_[offset:(offset + batch_size), :]
  95. # Run optimization op (backprop), loss op (to get loss value)
  96. # and summary nodes
  97. _, train_accuracy, summary = sess.run([train_step, accuracy, merged_summary_op] , feed_dict={x: batch_xs, y_: batch_ys, is_training: True})
  98. # Write logs at every iteration
  99. summary_writer.add_summary(summary, epoch * total_batch + i)
  100. # Display logs
  101. if i % display_step == 0:
  102. print("Epoch:", '%04d,' % (epoch + 1),
  103. "batch_index %4d/%4d, training accuracy %.5f" % (i, total_batch, train_accuracy))
  104. # Get accuracy for validation data
  105. if i % validation_step == 0:
  106. # Calculate accuracy
  107. validation_accuracy = sess.run(accuracy,
  108. feed_dict={x: validation_data, y_: validation_labels, is_training: False})
  109. print("Epoch:", '%04d,' % (epoch + 1),
  110. "batch_index %4d/%4d, validation accuracy %.5f" % (i, total_batch, validation_accuracy))
  111. # Save the current model if the maximum accuracy is updated
  112. if validation_accuracy > max_acc:
  113. max_acc = validation_accuracy
  114. save_path = saver.save(sess, MODEL_DIRECTORY)
  115. print("Model updated and saved in file: %s" % save_path)
  116. print("Optimization Finished!")
  117. # Restore variables from disk
  118. saver.restore(sess, MODEL_DIRECTORY)
  119. # Calculate accuracy for all ls_data test images
  120. test_size = test_labels.shape[0]
  121. batch_size = TEST_BATCH_SIZE
  122. total_batch = int(test_size / batch_size)
  123. acc_buffer = []
  124. # Loop over all batches
  125. for i in range(total_batch):
  126. # Compute the offset of the current minibatch in the data.
  127. offset = (i * batch_size) % (test_size)
  128. batch_xs = test_data[offset:(offset + batch_size), :]
  129. batch_ys = test_labels[offset:(offset + batch_size), :]
  130. y_final = sess.run(y, feed_dict={x: batch_xs, y_: batch_ys, is_training: False})
  131. correct_prediction = numpy.equal(numpy.argmax(y_final, 1), numpy.argmax(batch_ys, 1))
  132. acc_buffer.append(numpy.sum(correct_prediction) / batch_size)
  133. print("test accuracy for the stored model: %g" % numpy.mean(acc_buffer))
  134. if __name__ == '__main__':
  135. train()

  1. # Some code was borrowed from
  2. # https://github.com/petewarden/tensorflow_makefile/blob/master/tensorflow/models/image/mnist/convolutional.py
  3. from __future__ import absolute_import
  4. from __future__ import division
  5. from __future__ import print_function
  6. import tensorflow as tf
  7. import tensorflow.contrib.slim as slim
  8. # Create model of CNN with slim api
  9. def CNN(inputs, is_training=True):
  10. batch_norm_params = {'is_training': is_training, 'decay': 0.9, 'updates_collections': None}
  11. with slim.arg_scope([slim.conv2d, slim.fully_connected],
  12. normalizer_fn=slim.batch_norm,
  13. normalizer_params=batch_norm_params):
  14. x = tf.reshape(inputs, [-1, 28, 28, 1])
  15. # For slim.conv2d, default argument values are like
  16. # normalizer_fn = None, normalizer_params = None, <== slim.arg_scope changes these arguments
  17. # padding='SAME', activation_fn=nn.relu,
  18. # weights_initializer = initializers.xavier_initializer(),
  19. # biases_initializer = init_ops.zeros_initializer,
  20. net = slim.conv2d(x, 32, [5, 5], scope='conv1')
  21. net = slim.max_pool2d(net, [2, 2], scope='pool1')
  22. net = slim.conv2d(net, 64, [5, 5], scope='conv2')
  23. net = slim.max_pool2d(net, [2, 2], scope='pool2')
  24. net = slim.flatten(net, scope='flatten3')
  25. # For slim.fully_connected, default argument values are like
  26. # activation_fn = nn.relu,
  27. # normalizer_fn = None, normalizer_params = None, <== slim.arg_scope changes these arguments
  28. # weights_initializer = initializers.xavier_initializer(),
  29. # biases_initializer = init_ops.zeros_initializer,
  30. net = slim.fully_connected(net, 1024, scope='fc3')
  31. net = slim.dropout(net, is_training=is_training, scope='dropout3')
  32. # 0.5 by default
  33. outputs = slim.fully_connected(net, 10, activation_fn=None, normalizer_fn=None, scope='fco')
  34. return outputs

  1. # Some code was borrowed from https://github.com/petewarden/tensorflow_makefile/blob/master/tensorflow/models/image/mnist/convolutional.py
  2. from __future__ import absolute_import
  3. from __future__ import division
  4. from __future__ import print_function
  5. import numpy
  6. import os
  7. import tensorflow as tf
  8. import tensorflow.contrib.slim as slim
  9. import ls_data
  10. import cnn_model
  11. # user input
  12. from argparse import ArgumentParser
  13. # refernce argument values
  14. MODEL_DIRECTORY = "model"
  15. TEST_BATCH_SIZE = 5000
  16. ENSEMBLE = True
  17. # build parser
  18. def build_parser():
  19. parser = ArgumentParser()
  20. parser.add_argument('--model-dir',
  21. dest='model_directory', help='directory where model to be tested is stored',
  22. metavar='MODEL_DIRECTORY', required=True)
  23. parser.add_argument('--batch-size', type=int,
  24. dest='batch_size', help='batch size for test',
  25. metavar='TEST_BATCH_SIZE', required=True)
  26. parser.add_argument('--use-ensemble',
  27. dest='ensemble', help='boolean for usage of ensemble',
  28. metavar='ENSEMBLE', required=True)
  29. return parser
  30. # test with test data given by ls_data.py
  31. def test(model_directory, batch_size):
  32. # Import data
  33. PIXEL_DEPTH = ls_data.PIXEL_DEPTH
  34. ls = input_data.read_data_sets('data/', one_hot=True)
  35. is_training = tf.placeholder(tf.bool, name='MODE')
  36. # tf Graph input
  37. x = tf.placeholder(tf.float32, [None, 784])
  38. y_ = tf.placeholder(tf.float32, [None, 2]) # answer
  39. y = cnn_model.CNN(x, is_training=is_training)
  40. # Add ops to save and restore all the variables
  41. sess = tf.InteractiveSession()
  42. sess.run(tf.global_variables_initializer(), feed_dict={is_training: True})
  43. # Restore variables from disk
  44. saver = tf.train.Saver()
  45. # Calculate accuracy for all ls_data test images
  46. test_size = ls.test.num_examples
  47. total_batch = int(test_size / batch_size)
  48. saver.restore(sess, model_directory)
  49. acc_buffer = []
  50. # Loop over all batches
  51. for i in range(total_batch):
  52. batch = ls.test.next_batch(batch_size)
  53. batch_xs = (batch[0] - (PIXEL_DEPTH / 2.0) / PIXEL_DEPTH) # make zero-centered distribution as in ls_data.extract_data()
  54. batch_ys = batch[1]
  55. y_final = sess.run(y, feed_dict={x: batch_xs, y_: batch_ys, is_training: False})
  56. correct_prediction = numpy.equal(numpy.argmax(y_final, 1), numpy.argmax(batch_ys, 1))
  57. acc_buffer.append(numpy.sum(correct_prediction) / batch_size)
  58. print("test accuracy for the stored model: %g" % numpy.mean(acc_buffer))
  59. # test with test data given by ls_data.py
  60. def test_org(model_directory, batch_size):
  61. # Import data
  62. PIXEL_DEPTH = ls_data.PIXEL_DEPTH
  63. train_total_data, train_size, validation_data, validation_labels, test_data, test_labels = ls_data.prepare_ls_data(
  64. False)
  65. is_training = tf.placeholder(tf.bool, name='MODE')
  66. # tf Graph input
  67. x = tf.placeholder(tf.float32, [None, 784])
  68. y_ = tf.placeholder(tf.float32, [None, 2]) # answer
  69. y = cnn_model.CNN(x, is_training=is_training)
  70. # Add ops to save and restore all the variables
  71. sess = tf.InteractiveSession()
  72. sess.run(tf.global_variables_initializer(), feed_dict={is_training: True})
  73. # Restore variables from disk
  74. saver = tf.train.Saver()
  75. # Calculate accuracy for all ls_data test images
  76. test_size = test_labels.shape[0]
  77. total_batch = int(test_size / batch_size)
  78. saver.restore(sess, model_directory)
  79. acc_buffer = []
  80. # Loop over all batches
  81. for i in range(total_batch):
  82. # Compute the offset of the current minibatch in the data.
  83. offset = (i * batch_size) % (test_size)
  84. batch_xs = test_data[offset:(offset + batch_size), :]
  85. batch_ys = test_labels[offset:(offset + batch_size), :]
  86. y_final = sess.run(y, feed_dict={x: batch_xs, y_: batch_ys, is_training: False})
  87. correct_prediction = numpy.equal(numpy.argmax(y_final, 1), numpy.argmax(batch_ys, 1))
  88. acc_buffer.append(numpy.sum(correct_prediction) / batch_size)
  89. print("test accuracy for the stored model: %g" % numpy.mean(acc_buffer))
  90. # For a given matrix, each row is converted into a one-hot row vector
  91. def one_hot_matrix(a):
  92. a_ = numpy.zeros_like(a)
  93. for i, j in zip(numpy.arange(a.shape[0]), numpy.argmax(a, 1)): a_[i, j] = 1
  94. return a_
  95. # test with test data given by ls_data.py
  96. def test_ensemble(model_directory_list, batch_size):
  97. # Import data
  98. PIXEL_DEPTH = ls_data.PIXEL_DEPTH
  99. ls = input_data.read_data_sets('data/', one_hot=True)
  100. is_training = tf.placeholder(tf.bool, name='MODE')
  101. # tf Graph input
  102. x = tf.placeholder(tf.float32, [None, 784])
  103. y_ = tf.placeholder(tf.float32, [None, 2]) # answer
  104. y = cnn_model.CNN(x, is_training=is_training)
  105. # Add ops to save and restore all the variables
  106. sess = tf.InteractiveSession()
  107. sess.run(tf.global_variables_initializer(), feed_dict={is_training: True})
  108. # Restore variables from disk
  109. saver = tf.train.Saver()
  110. # Calculate accuracy for all ls_data test images
  111. test_size = ls.test.num_examples
  112. total_batch = int(test_size / batch_size)
  113. acc_buffer = []
  114. # Loop over all batches
  115. for i in range(total_batch):
  116. batch = ls.test.next_batch(batch_size)
  117. batch_xs = (batch[0] - (PIXEL_DEPTH / 2.0) / PIXEL_DEPTH) # make zero-centered distribution as in ls_data.extract_data()
  118. batch_ys = batch[1]
  119. y_final = numpy.zeros_like(batch_ys)
  120. for dir in model_directory_list:
  121. saver.restore(sess, dir+'/model.ckpt')
  122. pred = sess.run(y, feed_dict={x: batch_xs, y_: batch_ys, is_training: False})
  123. y_final += one_hot_matrix(pred)
  124. # take a majority vote as an answer
  125. # note
  126. correct_prediction = numpy.equal(numpy.argmax(y_final, 1), numpy.argmax(batch_ys, 1))
  127. acc_buffer.append(numpy.sum(correct_prediction) / batch_size)
  128. print("test accuracy for the stored model: %g" % numpy.mean(acc_buffer))
  129. if __name__ == '__main__':
  130. # Parse argument
  131. parser = build_parser()
  132. options = parser.parse_args()
  133. ensemble = options.ensemble
  134. model_directory = options.model_directory
  135. batch_size = options.batch_size
  136. # Select ensemble test or a single model test
  137. if ensemble=='True': # use ensemble model
  138. model_directory_list = [x[0] for x in os.walk(model_directory)]
  139. test_ensemble(model_directory_list[1:], batch_size)
  140. else: # test a single model
  141. # test_org(model_directory,
  142. # batch_size) #test with test data given by ls_data.py
  143. test(model_directory+'/model.ckpt',
  144. batch_size) # test with test data given by tensorflow.examples.tutorials.ls.input_data()

  1. # Some code was borrowed from
  2. # https://github.com/petewarden/tensorflow_makefile/blob/master/tensorflow/models/image/2/convolutional.py
  3. from __future__ import absolute_import
  4. from __future__ import division
  5. from __future__ import print_function
  6. import gzip
  7. import os
  8. import numpy
  9. from scipy import ndimage
  10. from six.moves import urllib
  11. from sklearn.model_selection import train_test_split
  12. import tensorflow as tf
  13. DATA_DIRECTORY = "data"
  14. # Params for ls_data
  15. IMAGE_SIZE = 28
  16. NUM_CHANNELS = 1
  17. PIXEL_DEPTH = 255
  18. NUM_LABELS = 2
  19. VALIDATION_SIZE = 5000 # Size of the validation set.
  20. #########################
  21. # Extract the images
  22. def extract_data(filename, num_images):
  23. """Extract the images into a 4D tensor [image index, y, x, channels].
  24. Values are rescaled from [0, 255] down to [-0.5, 0.5].
  25. """
  26. print('Extracting', filename)
  27. with gzip.open(filename) as bytestream:
  28. bytestream.read(16)
  29. buf = bytestream.read(IMAGE_SIZE * IMAGE_SIZE * num_images * NUM_CHANNELS)
  30. data = numpy.frombuffer(buf, dtype=numpy.uint8).astype(numpy.float32)
  31. data = (data - (PIXEL_DEPTH / 2.0)) / PIXEL_DEPTH
  32. data = data.reshape(num_images, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS)
  33. data = numpy.reshape(data, [num_images, -1])
  34. return data
  35. # Extract the labels
  36. def extract_labels(filename, num_images):
  37. """Extract the labels into a vector of int64 label IDs."""
  38. print('Extracting', filename)
  39. with gzip.open(filename) as bytestream:
  40. bytestream.read(8)
  41. buf = bytestream.read(1 * num_images)
  42. labels = numpy.frombuffer(buf, dtype=numpy.uint8).astype(numpy.int64)
  43. num_labels_data = len(labels)
  44. one_hot_encoding = numpy.zeros((num_labels_data,NUM_LABELS))
  45. one_hot_encoding[numpy.arange(num_labels_data),labels] = 1
  46. one_hot_encoding = numpy.reshape(one_hot_encoding, [-1, NUM_LABELS])
  47. return one_hot_encoding
  48. # Augment training data
  49. def expend_training_data(images, labels):
  50. expanded_images = []
  51. expanded_labels = []
  52. j = 0 # counter
  53. for x, y in zip(images, labels):
  54. j = j+1
  55. if j%100==0:
  56. print ('expanding data : %03d / %03d' % (j,numpy.size(images,0)))
  57. # register original data
  58. expanded_images.append(x)
  59. expanded_labels.append(y)
  60. # get a value for the background
  61. # zero is the expected value,
  62. # but median() is used to estimate background's value
  63. bg_value = numpy.median(x)
  64. # this is regarded as background's value
  65. image = numpy.reshape(x, (-1, 28))
  66. for i in range(4):
  67. # rotate the image with random degree
  68. angle = numpy.random.randint(-15,15,1)
  69. new_img = ndimage.rotate(image,angle,reshape=False, cval=bg_value)
  70. # shift the image with random distance
  71. shift = numpy.random.randint(-2, 2, 2)
  72. new_img_ = ndimage.shift(new_img,shift, cval=bg_value)
  73. # register new training data
  74. expanded_images.append(numpy.reshape(new_img_, 784))
  75. expanded_labels.append(y)
  76. # images and labels are concatenated
  77. # for random-shuffle at each epoch
  78. # notice that pair of image
  79. # and label should not be broken
  80. expanded_train_total_data = numpy.concatenate((expanded_images, expanded_labels), axis=1)
  81. numpy.random.shuffle(expanded_train_total_data)
  82. return expanded_train_total_data
  83. # Prepare data
  84. def prepare_ls_data(use_data_augmentation=True):
  85. # Get the data.
  86. X = np.load(r"C:\Users\aixin\Desktop\lungsound\LSS_x_y\X.npy")
  87. y = np.load(r"C:\Users\aixin\Desktop\lungsound\LSS_x_y\y.npy")
  88. X = X[:-1]
  89. y = y[:-1]
  90. print('Cutted data shape: ', X.shape)
  91. print('Cutted data shape: ', y.shape)
  92. '''
  93. Cutted data shape: (230, 36, 36, 3)
  94. Cutted data shape: (230,)
  95. '''
  96. ################################################################################
  97. # 避免过拟合,采用交叉验证
  98. # 验证集占训练集30%,固定随机种子(random_state)
  99. train_data , train_labels, test_data ,test_labels
  100. = train_test_split(X, y,test_size=0.302, random_state=40)
  101. # As a sanity check,
  102. # we print out the size of the training and test data.
  103. print('Training data shape: ', train_data.shape)
  104. print('Training labels shape: ', train_labels.shape)
  105. print('Test data shape: ', test_data.shape)
  106. print('Test labels shape: ', test_labels.shape)
  107. ################################################################################
  108. # Split the data into train, val, and test sets. In addition we will
  109. # create a small development set as a subset of the training data;
  110. # we can use this for development so our code runs faster.
  111. # Generate a validation set.
  112. validation_data = train_data[:VALIDATION_SIZE, :]
  113. validation_labels = train_labels[:VALIDATION_SIZE,:]
  114. train_data = train_data[VALIDATION_SIZE:, :]
  115. train_labels = train_labels[VALIDATION_SIZE:,:]
  116. # Concatenate train_data & train_labels for random shuffle
  117. if use_data_augmentation:
  118. train_total_data = expend_training_data(train_data, train_labels)
  119. else:
  120. train_total_data = numpy.concatenate((train_data, train_labels), axis=1)
  121. train_size = train_total_data.shape[0]
  122. return train_total_data, train_size, validation_data, validation_labels, test_data, test_labels

5. COV_TEST.py

卷积结果测试

  1. import skimage.data
  2. import numpy
  3. import matplotlib
  4. import numpycnn
  5. """
  6. The project is tested using Python 3.5.2 installed inside Anaconda 4.2.0 (64-bit)
  7. NumPy version used is 1.14.0
  8. """
  9. # Reading the image
  10. #img = skimage.io.imread("test.jpg")
  11. #img = skimage.data.checkerboard()
  12. img = skimage.data.chelsea()
  13. #img = skimage.data.camera()
  14. # Converting the image into gray.
  15. img = skimage.color.rgb2gray(img)
  16. ##
  17. # First conv layer
  18. #l1_filter = numpy.random.rand(2,7,7)*20
  19. # Preparing the filters randomly.
  20. l1_filter = numpy.zeros((2,3,3))
  21. l1_filter[0, :, :] = numpy.array([[[-1, 0, 1],
  22. [-1, 0, 1],
  23. [-1, 0, 1]]])
  24. l1_filter[1, :, :] = numpy.array([[[1, 1, 1],
  25. [0, 0, 0],
  26. [-1, -1, -1]]])
  27. print("\n**Working with conv layer 1**")
  28. l1_feature_map = numpycnn.conv(img, l1_filter)
  29. print("\n**ReLU**")
  30. l1_feature_map_relu = numpycnn.relu(l1_feature_map)
  31. print("\n**Pooling**")
  32. l1_feature_map_relu_pool = numpycnn.pooling(l1_feature_map_relu, 2, 2)
  33. print("**End of conv layer 1**\n")
  34. # Second conv layer
  35. l2_filter = numpy.random.rand(3, 5, 5, l1_feature_map_relu_pool.shape[-1])
  36. print("\n**Working with conv layer 2**")
  37. l2_feature_map = numpycnn.conv(l1_feature_map_relu_pool, l2_filter)
  38. print("\n**ReLU**")
  39. l2_feature_map_relu = numpycnn.relu(l2_feature_map)
  40. print("\n**Pooling**")
  41. l2_feature_map_relu_pool = numpycnn.pooling(l2_feature_map_relu, 2, 2)
  42. print("**End of conv layer 2**\n")
  43. ################################################################################
  44. # Third conv layer
  45. l3_filter = numpy.random.rand(1, 7, 7, l2_feature_map_relu_pool.shape[-1])
  46. print("\n**Working with conv layer 3**")
  47. l3_feature_map = numpycnn.conv(l2_feature_map_relu_pool, l3_filter)
  48. print("\n**ReLU**")
  49. l3_feature_map_relu = numpycnn.relu(l3_feature_map)
  50. print("\n**Pooling**")
  51. l3_feature_map_relu_pool = numpycnn.pooling(l3_feature_map_relu, 2, 2)
  52. print("**End of conv layer 3**\n")
  53. # Graphing
  54. # results
  55. fig0, ax0 = matplotlib.pyplot.subplots(nrows=1, ncols=1)
  56. ax0.imshow(img).set_cmap("gray")
  57. ax0.set_title("Input Image")
  58. ax0.get_xaxis().set_ticks([])
  59. ax0.get_yaxis().set_ticks([])
  60. matplotlib.pyplot.savefig("in_img.png", bbox_inches="tight")
  61. matplotlib.pyplot.close(fig0)
  62. # Layer 1
  63. ################################################################################
  64. fig1, ax1 = matplotlib.pyplot.subplots(nrows=3, ncols=2)
  65. ax1[0, 0].imshow(l1_feature_map[:, :, 0]).set_cmap("gray")
  66. ax1[0, 0].get_xaxis().set_ticks([])
  67. ax1[0, 0].get_yaxis().set_ticks([])
  68. ax1[0, 0].set_title("L1-Map1")
  69. ax1[0, 1].imshow(l1_feature_map[:, :, 1]).set_cmap("gray")
  70. ax1[0, 1].get_xaxis().set_ticks([])
  71. ax1[0, 1].get_yaxis().set_ticks([])
  72. ax1[0, 1].set_title("L1-Map2")
  73. ax1[1, 0].imshow(l1_feature_map_relu[:, :, 0]).set_cmap("gray")
  74. ax1[1, 0].get_xaxis().set_ticks([])
  75. ax1[1, 0].get_yaxis().set_ticks([])
  76. ax1[1, 0].set_title("L1-Map1ReLU")
  77. ax1[1, 1].imshow(l1_feature_map_relu[:, :, 1]).set_cmap("gray")
  78. ax1[1, 1].get_xaxis().set_ticks([])
  79. ax1[1, 1].get_yaxis().set_ticks([])
  80. ax1[1, 1].set_title("L1-Map2ReLU")
  81. ax1[2, 0].imshow(l1_feature_map_relu_pool[:, :, 0]).set_cmap("gray")
  82. ax1[2, 0].get_xaxis().set_ticks([])
  83. ax1[2, 0].get_yaxis().set_ticks([])
  84. ax1[2, 0].set_title("L1-Map1ReLUPool")
  85. ax1[2, 1].imshow(l1_feature_map_relu_pool[:, :, 1]).set_cmap("gray")
  86. ax1[2, 0].get_xaxis().set_ticks([])
  87. ax1[2, 0].get_yaxis().set_ticks([])
  88. ax1[2, 1].set_title("L1-Map2ReLUPool")
  89. matplotlib.pyplot.savefig("L1.png", bbox_inches="tight")
  90. matplotlib.pyplot.close(fig1)
  91. # Layer 2
  92. ################################################################################
  93. fig2, ax2 = matplotlib.pyplot.subplots(nrows=3, ncols=3)
  94. ax2[0, 0].imshow(l2_feature_map[:, :, 0]).set_cmap("gray")
  95. ax2[0, 0].get_xaxis().set_ticks([])
  96. ax2[0, 0].get_yaxis().set_ticks([])
  97. ax2[0, 0].set_title("L2-Map1")
  98. ax2[0, 1].imshow(l2_feature_map[:, :, 1]).set_cmap("gray")
  99. ax2[0, 1].get_xaxis().set_ticks([])
  100. ax2[0, 1].get_yaxis().set_ticks([])
  101. ax2[0, 1].set_title("L2-Map2")
  102. ax2[0, 2].imshow(l2_feature_map[:, :, 2]).set_cmap("gray")
  103. ax2[0, 2].get_xaxis().set_ticks([])
  104. ax2[0, 2].get_yaxis().set_ticks([])
  105. ax2[0, 2].set_title("L2-Map3")
  106. ax2[1, 0].imshow(l2_feature_map_relu[:, :, 0]).set_cmap("gray")
  107. ax2[1, 0].get_xaxis().set_ticks([])
  108. ax2[1, 0].get_yaxis().set_ticks([])
  109. ax2[1, 0].set_title("L2-Map1ReLU")
  110. ax2[1, 1].imshow(l2_feature_map_relu[:, :, 1]).set_cmap("gray")
  111. ax2[1, 1].get_xaxis().set_ticks([])
  112. ax2[1, 1].get_yaxis().set_ticks([])
  113. ax2[1, 1].set_title("L2-Map2ReLU")
  114. ax2[1, 2].imshow(l2_feature_map_relu[:, :, 2]).set_cmap("gray")
  115. ax2[1, 2].get_xaxis().set_ticks([])
  116. ax2[1, 2].get_yaxis().set_ticks([])
  117. ax2[1, 2].set_title("L2-Map3ReLU")
  118. ax2[2, 0].imshow(l2_feature_map_relu_pool[:, :, 0]).set_cmap("gray")
  119. ax2[2, 0].get_xaxis().set_ticks([])
  120. ax2[2, 0].get_yaxis().set_ticks([])
  121. ax2[2, 0].set_title("L2-Map1ReLUPool")
  122. ax2[2, 1].imshow(l2_feature_map_relu_pool[:, :, 1]).set_cmap("gray")
  123. ax2[2, 1].get_xaxis().set_ticks([])
  124. ax2[2, 1].get_yaxis().set_ticks([])
  125. ax2[2, 1].set_title("L2-Map2ReLUPool")
  126. ax2[2, 2].imshow(l2_feature_map_relu_pool[:, :, 2]).set_cmap("gray")
  127. ax2[2, 2].get_xaxis().set_ticks([])
  128. ax2[2, 2].get_yaxis().set_ticks([])
  129. ax2[2, 2].set_title("L2-Map3ReLUPool")
  130. matplotlib.pyplot.savefig("L2.png", bbox_inches="tight")
  131. matplotlib.pyplot.close(fig2)
  132. # Layer 3
  133. ################################################################################
  134. fig3, ax3 = matplotlib.pyplot.subplots(nrows=1, ncols=3)
  135. ax3[0].imshow(l3_feature_map[:, :, 0]).set_cmap("gray")
  136. ax3[0].get_xaxis().set_ticks([])
  137. ax3[0].get_yaxis().set_ticks([])
  138. ax3[0].set_title("L3-Map1")
  139. ax3[1].imshow(l3_feature_map_relu[:, :, 0]).set_cmap("gray")
  140. ax3[1].get_xaxis().set_ticks([])
  141. ax3[1].get_yaxis().set_ticks([])
  142. ax3[1].set_title("L3-Map1ReLU")
  143. ax3[2].imshow(l3_feature_map_relu_pool[:, :, 0]).set_cmap("gray")
  144. ax3[2].get_xaxis().set_ticks([])
  145. ax3[2].get_yaxis().set_ticks([])
  146. ax3[2].set_title("L3-Map1ReLUPool")
  147. matplotlib.pyplot.savefig("L3.png", bbox_inches="tight")
  148. matplotlib.pyplot.close(fig3)

6. SVM_classifier.py

方法二:使用SVM分类器对数据进行分类

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri May 4 10:54:49 2018
  4. @author: aixin
  5. """
  6. import numpy as np
  7. from sklearn.model_selection import train_test_split
  8. X = np.load(r"C:\Users\aixin\Desktop\lungsound\LSS_x_y\X.npy")
  9. y = np.load(r"C:\Users\aixin\Desktop\lungsound\LSS_x_y\y.npy")
  10. X = X[:-1]
  11. y = y[:-1]
  12. print('Cutted data shape: ', X.shape)
  13. print('Cutted data shape: ', y.shape)
  14. '''
  15. Cutted data shape: (230, 36, 36, 3)
  16. Cutted data shape: (230,)
  17. '''
  18. ################################################################################
  19. # 避免过拟合,采用交叉验证,# 验证集占训练集30%,固定随机种子(random_state)
  20. train_data , train_labels, test_data ,test_labels
  21. = train_test_split(X, y,test_size=0.302, random_state=40)
  22. # As a sanity check, we print out the size of the training and test data.
  23. print('Training data shape: ', X_train.shape)
  24. print('Training labels shape: ', y_train.shape)
  25. print('Test data shape: ', X_test.shape)
  26. print('Test labels shape: ', y_test.shape)
  27. ################################################################################
  28. # Split the data into train,
  29. # val, and test sets. In addition we will
  30. # create a small development
  31. # set as a subset of the training data;
  32. # we can use this for
  33. # development so our code runs faster.
  34. num_training = 140 # 训练数据
  35. num_validation = 20 # 验证数据
  36. num_test = 10 # 测试数据
  37. num_dev = 5 # small development 数据
  38. ################################################################################
  39. # Our validation set will be
  40. # num_validation points from the original
  41. # training set.
  42. mask = range(num_training, num_training + num_validation) # 49000-50000的数据
  43. X_val = X_train[mask]
  44. y_val = y_train[mask]
  45. # Our training set will be the first num_train points from the original
  46. # training set.
  47. mask = range(num_training)
  48. X_train = X_train[mask]
  49. y_train = y_train[mask]
  50. # We will also make a
  51. # development set, which is a small subset of
  52. # the training set.
  53. mask = np.random.choice(num_training, num_dev, replace=False)
  54. X_dev = X_train[mask]
  55. y_dev = y_train[mask]
  56. # We use the first num_test points of the original test set as our
  57. # test set.
  58. mask = range(num_test)
  59. X_test = X_test[mask]
  60. y_test = y_test[mask]
  61. print('Train data shape: ', X_train.shape)
  62. print('Train labels shape: ', y_train.shape)
  63. print('Validation data shape: ', X_val.shape)
  64. ###
  65. print('Validation labels shape: ', y_val.shape)
  66. print('Test data shape: ', X_test.shape)
  67. print('Test labels shape: ', y_test.shape)
  68. ################################################################################
  69. X_train = np.hstack([X_train, np.ones((X_train.shape[0], 1))])
  70. X_val = np.hstack([X_val, np.ones((X_val.shape[0], 1))])
  71. X_test = np.hstack([X_test, np.ones((X_test.shape[0], 1))])
  72. X_dev = np.hstack([X_dev, np.ones((X_dev.shape[0], 1))])
  73. print(X_train.shape, X_val.shape, X_test.shape, X_dev.shape)
  74. # (140, 3889) (20, 3889) (10, 3889) (5, 3889)
  75. ################################################################################
  76. # generate a random
  77. # SVM weight matrix of small numbers
  78. from classifiers.linear_svm import svm_loss_naive
  79. W = np.random.randn(X_train.shape[1], 10) * 0.0001
  80. loss, grad = svm_loss_naive(W, X_dev, y_dev, 0.00005)
  81. print('loss: %f' % (loss, ))
  82. ################################################################################
  83. # In the file linear_classifier.py, implement SGD in the function
  84. # LinearClassifier.train() and then run it with the code below.
  85. from classifiers import LinearSVM
  86. svm = LinearSVM()
  87. tic = time.time()
  88. loss_hist = svm.train(X_train, y_train, learning_rate=1e-7, reg=2.5e4,
  89. num_iters=1500, verbose=True)
  90. toc = time.time()
  91. print('That took %fs' % (toc - tic))
  92. plt.plot(loss_hist)
  93. plt.xlabel('Iteration number')
  94. plt.ylabel('Loss value')
  95. plt.show()
  96. ################################################################################
  97. # training and validation set
  98. y_train_pred = svm.predict(X_train) # 49000x3073
  99. print('training accuracy: %f' % (np.mean(y_train == y_train_pred), ))
  100. y_val_pred = svm.predict(X_val) # 1000x3073
  101. print('validation accuracy: %f' % (np.mean(y_val == y_val_pred), ))
  102. ################################################################################
  103. '''
  104. training accuracy: 0.814286
  105. validation accuracy: 0.850000
  106. '''
  107. ################################################################################

  1. import tensorflow as tf
  2. import numpy as np
  3. import image
  4. def weight_variable(shape, dtype, name):
  5. initial = tf.truncated_normal(shape = shape, stddev = 0.1, dtype = dtype, name = name)
  6. return tf.Variable(initial)
  7. def bias_variable(shape, dtype, name):
  8. initial = tf.constant(0.1, shape = shape, dtype = dtype, name = name)
  9. return tf.Variable(initial)
  10. def conv2d(x, W):
  11. return tf.nn.conv2d(x, W, strides = [1, 1, 1, 1], padding = 'SAME')
  12. def max_pool_2x2(x):
  13. return tf.nn.max_pool(x, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = 'SAME')
  14. # Lungsound_path = r"C:\Users\aixin\Desktop\lungsound"
  15. Lungsound_path = r"C:\Users\aixin\Desktop\lungsound"
  16. Lungsound = input_data.read_data_sets(Lungsound_path, one_hot = True)
  17. x = tf.placeholder("float", [None, 784])
  18. y = tf.placeholder("float", [None, 2])
  19. x_image = tf.reshape(x, [-1, 28, 28, 1])
  20. # convolution 1
  21. weight_conv1 = weight_variable([5, 5, 1, 32], dtype = "float", name = 'weight_conv1')
  22. bias_conv1 = bias_variable([32], dtype = "float", name = 'bias_conv1')
  23. hidden_conv1 = tf.nn.relu(conv2d(x_image, weight_conv1) + bias_conv1)
  24. hidden_pool1 = max_pool_2x2(hidden_conv1)
  25. # convolution 2
  26. weight_conv2 = weight_variable([5, 5, 32, 64], dtype = "float", name = 'weight_conv2')
  27. bias_conv2 = bias_variable([64], dtype = "float", name = 'bias_conv2')
  28. hidden_conv2 = tf.nn.relu(conv2d(hidden_pool1, weight_conv2) + bias_conv2)
  29. hidden_pool2 = max_pool_2x2(hidden_conv2)
  30. # function 1
  31. hidden_pool2_flat = tf.reshape(hidden_pool2, [-1, 7 * 7 * 64])
  32. weight_fc1 = weight_variable([7 * 7 * 64, 1024], dtype = "float", name = 'weight_fc1')
  33. bias_fc1 = bias_variable([1024], dtype = "float", name = 'bias_fc1')
  34. hidden_fc1 = tf.nn.relu(tf.matmul(hidden_pool2_flat, weight_fc1) + bias_fc1)
  35. keep_prob = tf.placeholder("float")
  36. hidden_fc1_dropout = tf.nn.dropout(hidden_fc1, keep_prob)
  37. # function 2
  38. weight_fc2 = weight_variable([1024, 2], dtype = "float", name = 'weight_fc2')
  39. bias_fc2 = bias_variable([2], dtype = "float", name = 'weight_fc2')
  40. y_fc2 = tf.nn.softmax(tf.matmul(hidden_fc1_dropout, weight_fc2) + bias_fc2)
  41. # create tensorflow structure
  42. cross_entropy = -tf.reduce_sum(y * tf.log(y_fc2))
  43. optimize = tf.train.AdamOptimizer(0.0001)
  44. train = optimize.minimize(cross_entropy)
  45. correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_fc2, 1))
  46. accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
  47. # initial all variables
  48. init = tf.initialize_all_variables()
  49. session = tf.Session()
  50. session.run(init)
  51. # train
  52. def Train() :
  53. for i in range(5000):
  54. batch = Lungsound.train.next_batch(50)
  55. session.run(train, feed_dict = {x:batch[0], y:batch[1], keep_prob:0.5})
  56. if i % 100 == 0:
  57. print("step %4d: " % i)
  58. print(session.run(accuracy, feed_dict = {x:batch[0], y:batch[1], keep_prob:1}))
  59. print(session.run(accuracy, feed_dict = {x:Lungsound.test.images, y:Lungsound.test.labels, keep_prob:1}))
  60. # save variables
  61. def save() :
  62. saver = tf.train.Saver()
  63. saver.save(session, save_path)
  64. # restore variables
  65. def restore() :
  66. saver = tf.train.Saver()
  67. saver.restore(session, save_path)
  68. def getTestPicArray(filename) :
  69. im = Image.open(filename)
  70. x_s = 28
  71. y_s = 28
  72. out = im.resize((x_s, y_s), Image.ANTIALIAS)
  73. im_arr = np.array(out.convert('L'))
  74. num0 = 0
  75. num255 = 0
  76. threshold = 100
  77. for x in range(x_s):
  78. for y in range(y_s):
  79. if im_arr[x][y] > threshold :
  80. num255 = num255 + 1
  81. else :
  82. num0 = num0 + 1
  83. if(num255 > num0) :
  84. print("convert!")
  85. for x in range(x_s):
  86. for y in range(y_s):
  87. im_arr[x][y] = 255 - im_arr[x][y]
  88. if(im_arr[x][y] < threshold) :
  89. im_arr[x][y] = 0
  90. out = Image.fromarray(np.uint8(im_arr))
  91. out.save(filename.split('/')[0] + '/28pix/' + filename.split('/')[1])
  92. #print im_arr
  93. nm = im_arr.reshape((1, 784))
  94. nm = nm.astype(np.float32)
  95. nm = np.multiply(nm, 1.0 / 255.0)
  96. return nm
  97. def testMyPicture() :
  98. testNum = input("input the number of test picture:")
  99. for i in range(testNum) :
  100. testPicture = raw_input("input the test picture's path:")
  101. oneTestx = getTestPicArray(testPicture)
  102. ans = tf.argmax(y_fc2, 1)
  103. print("The prediction answer is:")
  104. print(session.run(ans, feed_dict = {x:oneTestx, keep_prob:1}))
  105. save_path = "network/cnn.ckpt"
  106. # train...........model
  107. Train()
  108. #save ...........model
  109. save()
  110. #restore()
  111. testMyPicture()
  112. session.close()

Classifier Folder


  1. import numpy as np
  2. from random import shuffle
  3. def softmax_loss_naive(W, X, y, reg):
  4. """
  5. Softmax loss function, naive implementation (with loops)
  6. Inputs have dimension D, there are C classes, and we operate on minibatches
  7. of N examples.
  8. Inputs:
  9. - W: A numpy array of shape (D, C) containing weights.
  10. - X: A numpy array of shape (N, D) containing a minibatch of data.
  11. - y: A numpy array of shape (N,) containing training labels; y[i] = c means
  12. that X[i] has label c, where 0 <= c < C.
  13. - reg: (float) regularization strength
  14. Returns a tuple of:
  15. - loss as single float
  16. - gradient with respect to weights W; an array of same shape as W
  17. """
  18. # Initialize the loss and gradient to zero.
  19. loss = 0.0
  20. dW = np.zeros_like(W)
  21. #############################################################################
  22. # TODO: Compute the softmax loss and its gradient using explicit loops. #
  23. # Store the loss in loss and the gradient in dW. If you are not careful #
  24. # here, it is easy to run into numeric instability. Don't forget the #
  25. # regularization! #
  26. #############################################################################
  27. # pass
  28. # num_train = X.shape[0]
  29. # num_classes = W.shape[1]
  30. # for i in range(num_train):
  31. # scores = X[i].dot(W)
  32. # prevent_explo_scores = scores - max(scores)
  33. # 这里减去最大值是防止数值爆炸
  34. # loss_i = - prevent_explo_scores[y[i]] + np.log(sum(np.exp(prevent_explo_scores)))
  35. # loss += loss_i
  36. # for j in range(num_classes):
  37. # softmax_output = np.exp(prevent_explo_scores[j]) / sum(np.exp(prevent_explo_scores))
  38. # if j == y[i]:
  39. # dW[:, j] += (-1 + softmax_output) * X[i]
  40. # else:
  41. # dW[:, j] =softmax_output * X[i]
  42. # loss /= num_train
  43. # loss += 0.5 *reg *np.sum(W *W)
  44. # dW = dW/num_train + reg *W
  45. ########################另一个 GitHub-observerspy 的办法#####################
  46. num_classes = W.shape[1]
  47. num_train = X.shape[0]
  48. loss = 0.0
  49. for i in range(num_train):
  50. scores = X[i].dot(W)
  51. correct_class_score = scores[y[i]]
  52. exp_sum = np.sum(np.exp(scores))
  53. loss += np.log(exp_sum) - correct_class_score
  54. dW[:, y[i]] += -X[i]
  55. for j in range(num_classes):
  56. dW[:, j] += (np.exp(scores[j]) / exp_sum) * X[i]
  57. loss /= num_train
  58. dW /= num_train
  59. loss += 0.5 *reg *np.sum(W*W)
  60. dW += reg*W
  61. #############################################################################
  62. # END OF YOUR CODE #
  63. #############################################################################
  64. return loss, dW
  65. def softmax_loss_vectorized(W, X, y, reg):
  66. """
  67. Softmax loss function, vectorized version.
  68. Inputs and outputs are the same as softmax_loss_naive.
  69. """
  70. # Initialize the loss and gradient to zero.
  71. loss = 0.0
  72. dW = np.zeros_like(W)
  73. #############################################################################
  74. # TODO: Compute the softmax loss and its gradient using no explicit loops. #
  75. # Store the loss in loss and the gradient in dW. If you are not careful #
  76. # here, it is easy to run into numeric instability. Don't forget the #
  77. # regularization! #
  78. #############################################################################
  79. # pass
  80. loss = 0.0
  81. num_classes = W.shape[1] # C 10
  82. num_train = X.shape[0] # N 49000
  83. scores = X.dot(W) # NxD * DxC = NxC 49000*10
  84. prevent_explo_scores = scores - np.max(scores, axis=1).reshape(-1,1) # N*1
  85. softmax_output = np.exp(prevent_explo_scores)/np.sum(np.exp(prevent_explo_scores), axis =1).reshape(-1,1)
  86. loss = -np.sum(np.log(softmax_output[range(num_train), list(y)]))
  87. loss /= num_train
  88. loss += 0.5* reg* np.sum(W* W)
  89. dS = softmax_output.copy()
  90. dS[range(num_train), list(y)] += -1 # 减去那个-1项,看我的笔记就知道了
  91. dW = (X.T).dot(dS) # DxN * NxC = DxC 3073*10
  92. dW = dW / num_train + reg *W
  93. ########################另一个 GitHub-observerspy 的办法#####################
  94. # num_train = X.shape[0]
  95. # num_classes = W.shape[1]
  96. # scores = X.dot(W)
  97. # correct_class_score = scores[np.arange(num_train), y].reshape(-1,1)
  98. # exp_sum = np.sum(np.exp(scores), axis=1).reshape(-1,1)
  99. # loss += np.sum(np.log(exp_sum) - correct_class_score)
  100. # margin = np.exp(scores) / exp_sum
  101. # margin[np.arange(num_train),y] += 1
  102. # dW = X.T.dot(margin)
  103. # loss /= num_train
  104. # dW /= num_train
  105. # loss += 0.5*reg*np.sum(W*W)
  106. # dW += reg*W
  107. #############################################################################
  108. # END OF YOUR CODE #
  109. #############################################################################
  110. return loss, dW

  1. from __future__ import print_function
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. class TwoLayerNet(object):
  5. """
  6. A two-layer fully-connected neural network. The net has an input dimension of
  7. N, a hidden layer dimension of H,
  8. and performs classification over C classes.
  9. We train the network with a softmax loss function and L2 regularization
  10. on the weight matrices. The network uses a ReLU nonlinearity after the first fully
  11. connected layer.
  12. In other words, the network has the following architecture:
  13. input - fully connected layer - ReLU - fully connected layer - softmax
  14. The outputs of the second fully-connected layer are the scores for each class.
  15. 两层层全连接---神经网络
  16. 网络的输入维数N、H隐藏层维度,然后执行C类以上的分类。
  17. 网络使用softmax损失函数和使用L2正则化。
  18. 第一个全连接层后,使用的是ReLU非线性连接层
  19. 网络结构就是:输入层——全连接层——ReLU-全连接层——softmax
  20. 第二个全连接层的输出就是:每个类的分数。
  21. """
  22. def __init__(self, input_size, hidden_size, output_size, std=1e-4):
  23. """
  24. Initialize the model. Weights are initialized to small random values and
  25. biases are initialized to zero. Weights and biases are stored in the
  26. variable self.params, which is a dictionary with the following keys:
  27. W1: First layer weights; has shape (D, H)
  28. b1: First layer biases; has shape (H,)
  29. W2: Second layer weights; has shape (H, C)
  30. b2: Second layer biases; has shape (C,)
  31. Inputs:
  32. - input_size: The dimension D of the input data.
  33. - hidden_size: The number of neurons H in the hidden layer.
  34. - output_size: The number of classes C.
  35. 初始化模型中权重是比较小的随机值,偏置初始化为零,存在: self.params字典中
  36. 分别有w1,b1,w2,b2四个键值,对应的是四个初始化矩阵
  37. 网络大概是这个样子
  38. 输入 D ----> DxH && Hx1 -----> H -----> HxC && Cx1 ----> C
  39. """
  40. self.params = {}
  41. self.params['W1'] = std * np.random.randn(input_size, hidden_size)
  42. self.params['b1'] = np.zeros(hidden_size)
  43. self.params['W2'] = std * np.random.randn(hidden_size, output_size)
  44. self.params['b2'] = np.zeros(output_size)
  45. def loss(self, X, y=None, reg=0.0):
  46. """
  47. Compute the loss and gradients for a two layer fully connected neural
  48. network.
  49. Inputs:
  50. - X: Input data of shape (N, D). Each X[i] is a training sample.
  51. - y: Vector of training labels. y[i] is the label for X[i], and each y[i] is
  52. an integer in the range 0 <= y[i] < C.
  53. This parameter is optional; if it
  54. is not passed then we only return scores, and if it is passed then we
  55. instead return the loss and gradients.
  56. - reg: Regularization strength.
  57. Returns:
  58. If y is None, return a matrix scores of shape (N, C) where scores[i, c] is
  59. the score for class c on input X[i].
  60. If y is not None, instead return a tuple of:
  61. - loss: Loss (data loss and regularization loss) for this batch of training
  62. samples.
  63. - grads: Dictionary mapping parameter names to gradients of those parameters
  64. with respect to the loss function; has the same keys as self.params.
  65. 计算损失:
  66. X = NxD y 就是X对应的分数 Nx1
  67. 如果传入参数y 则返回损失和梯度 grads, loss,
  68. 如果没有参数y 则返回的 分数: NxC 的, 分数[i, c] 对应输入的X[i] 对每一类的分数
  69. """
  70. # Unpack variables from the params dictionary
  71. W1, b1 = self.params['W1'], self.params['b1']
  72. W2, b2 = self.params['W2'], self.params['b2']
  73. N, D = X.shape
  74. # Compute the forward pass
  75. scores = None
  76. #############################################################################
  77. # TODO: Perform the forward pass, computing the class scores for the input. #
  78. # Store the result in the scores variable, which should be an array of #
  79. # shape (N, C). #
  80. #############################################################################
  81. H_out = np.maximum(0, X.dot(W1) + b1) # ReLU 就是np.maximum H_out = NxH
  82. scores = H_out.dot(W2) + b2 # NxC
  83. # pass
  84. #############################################################################
  85. # END OF YOUR CODE #
  86. #############################################################################
  87. # If the targets are not given then jump out, we're done
  88. # 这里就是看是否有y给出,给出就继续,没有就返回scores
  89. if y is None:
  90. return scores
  91. # Compute the loss
  92. loss = None
  93. #############################################################################
  94. # TODO: Finish the forward pass, and compute the loss. This should include #
  95. # both the data loss and L2 regularization for W1 and W2. Store the result #
  96. # in the variable loss, which should be a scalar. Use the Softmax #
  97. # classifier loss. #
  98. #############################################################################
  99. prevent_explo_scores = scores - np.max(scores, axis=1).reshape(-1,1) # N*1
  100. softmax_output = np.exp(prevent_explo_scores)/np.sum(np.exp(prevent_explo_scores), axis =1).reshape(-1,1) # 分母按行求和, 最后得到 NxC
  101. loss = -np.sum(np.log(softmax_output[range(N), list(y)])) #
  102. # np.sum()
  103. # 直接就是所有的和得到一个数值, 如果axis=None
  104. loss /= N
  105. loss += 0.5* reg* (np.sum(W1 * W1) + np.sum(W2 * W2))
  106. # pass
  107. #############################################################################
  108. # END OF YOUR CODE #
  109. #############################################################################
  110. # Backward pass: compute gradients
  111. grads = {}
  112. #############################################################################
  113. # TODO: Compute the backward pass, computing the derivatives of the weights #
  114. # and biases. Store the results in the grads dictionary. For example, #
  115. # grads['W1'] should store the gradient on W1, and be a matrix of same size #
  116. #############################################################################
  117. dscores = softmax_output.copy() # NxC
  118. dscores[range(N), list(y)] -= 1
  119. dscores /= N
  120. dW2 = H_out.T.dot(dscores) + reg * W2 # HxC
  121. # 这里别忘了正则项
  122. grads['W2'] = dW2
  123. grads['b2'] = np.sum(dscores, axis=0)
  124. dH = dscores.dot(W2.T)
  125. dH_Relu = (H_out > 0) * dH
  126. # 这个语句就包含了:dH_Relu[out1 <= 0] = 0
  127. grads['W1'] = X.T.dot(dH_Relu) + reg * W1
  128. grads['b1'] = np.sum(dH_Relu, axis = 0)
  129. # pass
  130. #############################################################################
  131. # END OF YOUR CODE #
  132. #############################################################################
  133. return loss, grads
  134. def train(self, X, y, X_val, y_val,
  135. learning_rate=1e-3, learning_rate_decay=0.95,
  136. reg=5e-6, num_iters=100,
  137. batch_size=200, verbose=False):
  138. """
  139. Train this neural network using stochastic gradient descent.
  140. Inputs:
  141. - X: A numpy array of shape (N, D) giving training data.
  142. - y: A numpy array f shape (N,) giving training labels; y[i] = c means that
  143. X[i] has label c, where 0 <= c < C.
  144. - X_val: A numpy array of shape (N_val, D) giving validation data.
  145. - y_val: A numpy array of shape (N_val,) giving validation labels.
  146. - learning_rate: Scalar giving learning rate for optimization.
  147. - learning_rate_decay: Scalar giving factor used to decay the learning rate
  148. after each epoch.
  149. - reg: Scalar giving regularization strength.
  150. - num_iters: Number of steps to take when optimizing.
  151. - batch_size: Number of training examples to use per step.
  152. - verbose: boolean; if true print progress during optimization.
  153. """
  154. num_train = X.shape[0] # NxD
  155. iterations_per_epoch = max(num_train / batch_size, 1)
  156. # Use SGD to optimize the parameters in self.model
  157. loss_history = []
  158. train_acc_history = []
  159. val_acc_history = []
  160. for it in range(num_iters):
  161. X_batch = None
  162. y_batch = None
  163. #########################################################################
  164. # TODO: Create a random minibatch of training data and labels, storing #
  165. # them in X_batch and y_batch respectively. #
  166. #########################################################################
  167. # pass
  168. idx = np.random.choice(num_train, batch_size, replace=True)
  169. X_batch = X[idx]
  170. y_batch = y[idx]
  171. #########################################################################
  172. # END OF YOUR CODE #
  173. #########################################################################
  174. # Compute loss and gradients using the current minibatch
  175. loss, grads = self.loss(X_batch, y=y_batch, reg=reg)
  176. loss_history.append(loss)
  177. #########################################################################
  178. # TODO: Use the gradients in the grads dictionary to update the #
  179. # parameters of the network (stored in the dictionary self.params) #
  180. # using stochastic gradient descent. You'll need to use the gradients #
  181. # stored in the grads dictionary defined above. #
  182. #########################################################################
  183. # pass
  184. self.params['W2'] += -learning_rate * grads['W2']
  185. self.params['b2'] += -learning_rate * grads['b2']
  186. self.params['W1'] += -learning_rate * grads['W1']
  187. self.params['b1'] += -learning_rate * grads['b1']
  188. #########################################################################
  189. # END OF YOUR CODE #
  190. #########################################################################
  191. if verbose and it % 10 == 0:
  192. print('iteration %d / %d: loss %f' % (it, num_iters, loss))
  193. # Every epoch, check train
  194. # and val accuracy and decay learning rate.
  195. if it % iterations_per_epoch == 0:
  196. # Check accuracy
  197. train_acc = (self.predict(X_batch) == y_batch).mean()
  198. val_acc = (self.predict(X_val) == y_val).mean()
  199. train_acc_history.append(train_acc)
  200. val_acc_history.append(val_acc)
  201. # Decay learning rate
  202. learning_rate *= learning_rate_decay
  203. return {
  204. 'loss_history': loss_history,
  205. 'train_acc_history': train_acc_history,
  206. 'val_acc_history': val_acc_history,
  207. }
  208. def predict(self, X):
  209. """
  210. Use the trained weights of this two-layer network to predict labels for
  211. data points. For each data point we predict scores for each of the C
  212. classes, and assign each data point to the class with the highest score.
  213. Inputs:
  214. - X: A numpy array of shape (N, D) giving N D-dimensional data points to
  215. classify.
  216. Returns:
  217. - y_pred: A numpy array of shape (N,) giving predicted labels for each of
  218. the elements of X. For all i, y_pred[i] = c means that X[i] is predicted
  219. to have class c, where 0 <= c < C.
  220. """
  221. y_pred = None
  222. ###########################################################################
  223. # TODO: Implement this function; it should be VERY simple! #
  224. ###########################################################################
  225. # pass
  226. H = np.maximum(0, X.dot(self.params['W1']) + self.params['b1'])
  227. final_scores = H.dot(self.params['W2']) + self.params['b2']
  228. y_pred = np.argmax(final_scores, axis = 1)
  229. ###########################################################################
  230. # END OF YOUR CODE #
  231. ###########################################################################
  232. return y_pred

  1. import numpy as np
  2. from random import shuffle
  3. # 这是naive 的损失函数:就是有循环,看到了吧, 鄙视你循环!
  4. # 其实更新梯度有两种方法,1. 倒数的定义出发,2.直接微分分析
  5. ######################## 输入:#############################
  6. # #### W numpy (维度【权重D维度】, 类【类别个数】) 3073x10 #
  7. # #### X numpy (N个数据,权重D维度) 100x3073 #
  8. # #### y numpy (N,) y[i] =c, X[i]的分类是c, c < C! 100 #
  9. # #### reg float 正则化强度 ,或者正则化系数 #
  10. ######################## 输出:#############################
  11. # #### loss, dW, 单精度float, dW 和 W 一样的维度 #
  12. ##################################################
  13. def svm_loss_naive(W, X, y, reg):
  14. """
  15. Structured SVM loss function, naive implementation (with loops).
  16. Inputs have dimension D, there are C classes, and we operate on minibatches
  17. of N examples.
  18. Inputs:
  19. - W: A numpy array of shape (D, C) containing weights.
  20. - X: A numpy array of shape (N, D) containing a minibatch of data.
  21. - y: A numpy array of shape (N,) containing training labels; y[i] = c means
  22. that X[i] has label c, where 0 <= c < C.
  23. - reg: (float) regularization strength
  24. Returns a tuple of:
  25. - loss as single float
  26. - gradient with respect to weights W; an array of same shape as W
  27. """
  28. dW = np.zeros(W.shape) # initialize the gradient as zero
  29. # compute the loss and the gradient
  30. num_classes = W.shape[1] # 10
  31. num_train = X.shape[0] # 100x3073
  32. loss = 0.0
  33. for i in range(num_train):
  34. scores = X[i].dot(W) # X[i].dot(W) = (1*D) · (D*C) = 1*C = 1*10
  35. correct_class_score = scores[y[i]]
  36. for j in range(num_classes):
  37. if j == y[i]:
  38. continue
  39. margin = scores[j] - correct_class_score + 1 # note delta = 1
  40. # 每个大于0的maxmargin会产生两个贡献
  41. if margin > 0:
  42. loss += margin
  43. dW[:,j] += X[i].T
  44. # 分类错误的添加一个xi
  45. dW[:,y[i]] -=X[i].T
  46. # 分类正确的产生一个-xi
  47. # Right now the loss is a sum over all training examples, but we want it
  48. # to be an average instead so we divide by num_train.
  49. loss /= num_train
  50. # 这里就是那个 ( 1/N )
  51. dW /= num_train
  52. # Add regularization to the loss.
  53. loss += 0.5 * reg * np.sum(W * W)
  54. # 加正则化
  55. dW += reg*W
  56. #############################################################################
  57. # TODO: #
  58. # Compute the gradient of the loss function and store it dW. #
  59. # Rather that first computing the loss and then computing the derivative, #
  60. # it may be simpler to compute the derivative at the same time that the #
  61. # loss is being computed. As a result you may need to modify some of the #
  62. # code above to compute the gradient. #
  63. #############################################################################
  64. return loss, dW
  65. # 构建向量化SVM 损失函数, 这里得到的输出和 非向量化的相同
  66. # 先存储,score,和loss, 然后计算dW.
  67. def svm_loss_vectorized(W, X, y, reg):
  68. """
  69. Structured SVM loss function, vectorized implementation.
  70. Inputs and outputs are the same as svm_loss_naive.
  71. """
  72. loss = 0.0
  73. dW = np.zeros(W.shape) # initialize the gradient as zero
  74. num_train = X.shape[0]
  75. num_classes = W.shape[1]
  76. #############################################################################
  77. # TODO: #
  78. # Implement a vectorized version of the structured SVM loss, storing the #
  79. # result in loss. #
  80. #############################################################################
  81. # pass
  82. scores = X.dot(W) # N*C
  83. correct_class_score = scores[range(num_train), list(y)].reshape(-1,1)
  84. margin = np.maximum(0, scores - correct_class_score + 1)
  85. # margin[range(num_train), list(y)] = 0 # sj-si + 1 >0 ,所以不算这些.
  86. loss = np.sum(margin) / num_train + 0.5 * reg * np.sum(W * W)
  87. #############################################################################
  88. # END OF YOUR CODE #
  89. #############################################################################
  90. #############################################################################
  91. # TODO: #
  92. # Implement a vectorized version of the gradient for the structured SVM #
  93. # loss, storing the result in dW. #
  94. # #
  95. # Hint: Instead of computing the gradient from scratch, it may be easier #
  96. # to reuse some of the intermediate values that you used to compute the #
  97. # loss. #
  98. #############################################################################
  99. # pass
  100. # 这里是来自 lightatime的GitHub
  101. # coeff_mat = np.zeros((num_train, num_classes))
  102. # coeff_mat[margin>0] = 1
  103. # coeff_mat[(range(num_train), list[y])] = 0
  104. # coeff_mat[(range(num_train), list[y])] = -np.sum(coeff_mat, axis=1)
  105. #
  106. # dW = (x.T).dot(coeff_mat)
  107. # dW = dW / num_train + reg*W
  108. #############################################################################
  109. ######## 下面是另一种方法,好像这个简单,不需要中间矩阵 coeff_mat ##########
  110. margin[margin>0] = 1 # 或者写成 margin = (margin>0)*1
  111. row_sum = np.sum(margin,axis=1) #
  112. margin[range(num_train), list(y)] = -row_sum
  113. dW = X.T.dot(margin) / num_train + reg*W # D*C
  114. #############################################################################
  115. # END OF YOUR CODE #
  116. #############################################################################
  117. return loss, dW

  1. from __future__ import print_function
  2. import numpy as np
  3. from cs231n.classifiers.linear_svm import *
  4. from cs231n.classifiers.softmax import *
  5. # 这里只需要说一下:verbose,就是训练优化过程中要不要打印过程
  6. class LinearClassifier(object):
  7. def __init__(self):
  8. self.W = None
  9. def train(self, X, y, learning_rate=1e-3, reg=1e-5, num_iters=100,
  10. batch_size=200, verbose=False):
  11. """
  12. Train this linear classifier using stochastic gradient descent.
  13. Inputs:
  14. - X: A numpy array of shape (N, D) containing training data; there are N
  15. training samples each of dimension D.
  16. - y: A numpy array of shape (N,) containing training labels; y[i] = c
  17. means that X[i] has label 0 <= c < C for C classes.
  18. - learning_rate: (float) learning rate for optimization.
  19. - reg: (float) regularization strength.
  20. - num_iters: (integer) number of steps to take when optimizing
  21. - batch_size: (integer) number of training examples to use at each step.
  22. - verbose: (boolean) If true, print progress during optimization.
  23. Outputs:
  24. A list containing the value of the loss function at each training iteration.
  25. """
  26. num_train, dim = X.shape
  27. num_classes = np.max(y) + 1 # assume y takes values 0...K-1 where K is number of classes
  28. if self.W is None:
  29. # lazily initialize W
  30. self.W = 0.001 * np.random.randn(dim, num_classes)
  31. # Run stochastic gradient descent to optimize W
  32. loss_history = []
  33. for it in range(num_iters):
  34. X_batch = None
  35. y_batch = None
  36. #########################################################################
  37. # TODO: #
  38. # Sample batch_size elements from the training data and their #
  39. # corresponding labels to use in this round of gradient descent. #
  40. # Store the data in X_batch and their corresponding labels in #
  41. # y_batch; after sampling X_batch should have shape (dim, batch_size) #
  42. # and y_batch should have shape (batch_size,) #
  43. # #
  44. # Hint: Use np.random.choice to generate indices. Sampling with #
  45. # replacement is faster than sampling without replacement. #
  46. #########################################################################
  47. # pass
  48. mask = np.random.choice(num_train, batch_size, replace=True)
  49. X_batch = X[mask]
  50. # 随机从数据中选取数据
  51. y_batch = y[mask]
  52. # 用来随机梯度下降啊
  53. #########################################################################
  54. # END OF YOUR CODE #
  55. #########################################################################
  56. # evaluate loss and gradient
  57. loss, grad = self.loss(X_batch, y_batch, reg)
  58. loss_history.append(loss)
  59. # perform parameter update
  60. #########################################################################
  61. # TODO: #
  62. # Update the weights using the gradient and the learning rate. #
  63. #########################################################################
  64. # pass
  65. self.W += -learning_rate * grad
  66. #########################################################################
  67. # END OF YOUR CODE #
  68. #########################################################################
  69. if verbose and it % 100 == 0:
  70. print('iteration %d / %d: loss %f' % (it, num_iters, loss))
  71. return loss_history
  72. def predict(self, X):
  73. """
  74. Use the trained weights of this linear classifier to predict labels for
  75. data points.
  76. Inputs:
  77. - X: A numpy array of shape (N, D) containing training data; there are N
  78. training samples each of dimension D.
  79. Returns:
  80. - y_pred: Predicted labels for the data in X. y_pred is a 1-dimensional
  81. array of length N, and each element is an integer giving the predicted
  82. class.
  83. """
  84. y_pred = np.zeros(X.shape[1])
  85. ###########################################################################
  86. # TODO: #
  87. # Implement this method. Store the predicted labels in y_pred. #
  88. ###########################################################################
  89. # pass
  90. y_pred = np.argmax(X.dot(self.W), axis=1)
  91. ###########################################################################
  92. # END OF YOUR CODE #
  93. ###########################################################################
  94. return y_pred
  95. def loss(self, X_batch, y_batch, reg):
  96. """
  97. Compute the loss function and its derivative.
  98. Subclasses will override this.
  99. Inputs:
  100. - X_batch: A numpy array of shape (N, D) containing a minibatch of N
  101. data points; each point has dimension D.
  102. - y_batch: A numpy array of shape (N,) containing labels for the minibatch.
  103. - reg: (float) regularization strength.
  104. Returns: A tuple containing:
  105. - loss as a single float
  106. - gradient with respect to self.W; an array of the same shape as W
  107. """
  108. pass
  109. class LinearSVM(LinearClassifier):
  110. """ A subclass that uses the Multiclass SVM loss function """
  111. def loss(self, X_batch, y_batch, reg):
  112. return svm_loss_vectorized(self.W, X_batch, y_batch, reg)
  113. class Softmax(LinearClassifier):
  114. """ A subclass that uses the Softmax + Cross-entropy loss function """
  115. def loss(self, X_batch, y_batch, reg):
  116. return softmax_loss_vectorized(self.W, X_batch, y_batch, reg)

  1. import numpy as np
  2. from past.builtins import xrange
  3. # pass 空语句块,是为了保持程序结构的完整性,一般用做占位语句
  4. class KNearestNeighbor(object):
  5. """ a kNN classifier with L2 distance """
  6. def __init__(self):
  7. pass
  8. def train(self, X, y):
  9. """
  10. Train the classifier. For k-nearest neighbors this is just
  11. memorizing the training data.
  12. Inputs:
  13. - X: A numpy array of shape (num_train, D) containing the training data
  14. consisting of num_train samples each of dimension D.
  15. - y: A numpy array of shape (N,) containing the training labels, where
  16. y[i] is the label for X[i].
  17. """
  18. self.X_train = X # 5000x3072
  19. self.y_train = y # 500x3072
  20. def predict(self, X, k=1, num_loops=0):
  21. """
  22. Predict labels for test data using this classifier.
  23. Inputs:
  24. - X: A numpy array of shape (num_test, D) containing test data consisting
  25. of num_test samples each of dimension D.
  26. - k: The number of nearest neighbors that vote for the predicted labels.
  27. - num_loops: Determines which implementation to use to compute distances
  28. between training points and testing points.
  29. Returns:
  30. - y: A numpy array of shape (num_test,) containing predicted labels for the
  31. test data, where y[i] is the predicted label for the test point X[i].
  32. 现在是用分类器给测试数据分类啦!
  33. 输入:
  34. X numpy (num_test, D),比如(5000,3072)
  35. k 选取几个最近的label
  36. num_loops 哪个计算距离的方法,0:no_loops 1:one_loops 2:two_loops
  37. 输出:
  38. y numpy (num_test,) 比如(500) 其实就是标签啦
  39. test_data y[i] 就是X[i] 的标签
  40. """
  41. if num_loops == 0:
  42. dists = self.compute_distances_no_loops(X)
  43. elif num_loops == 1:
  44. dists = self.compute_distances_one_loop(X)
  45. elif num_loops == 2:
  46. dists = self.compute_distances_two_loops(X)
  47. else:
  48. raise ValueError('Invalid value %d for num_loops' % num_loops)
  49. return self.predict_labels(dists, k=k)
  50. def compute_distances_two_loops(self, X):
  51. """
  52. ######################
  53. ##### 计算L2 距离 #####
  54. ######################
  55. Compute the distance between each test point in X and each training point
  56. in self.X_train using a nested loop over both the training data and the
  57. test data.
  58. Inputs:
  59. - X: A numpy array of shape (num_test, D) containing test data.
  60. Returns:
  61. - dists: A numpy array of shape (num_test, num_train) where dists[i, j]
  62. is the Euclidean distance between the ith test point and the jth training
  63. point.
  64. """
  65. num_test = X.shape[0]
  66. # 500
  67. num_train = self.X_train.shape[0]
  68. # 5000
  69. dists = np.zeros((num_test, num_train))
  70. # 500x5000 全零矩阵
  71. for i in xrange(num_test):
  72. for j in xrange(num_train):
  73. #####################################################################
  74. # TODO: #
  75. # Compute the l2 distance between the ith test point and the jth #
  76. # training point, and store the result in dists[i, j]. You should #
  77. # not use a loop over dimension.
  78. #
  79. #####################################################################
  80. dists[i,j] = np.sqrt(np.sum(np.square(X[i] - self.X_train[j])))
  81. # 另一种向量化方法
  82. # dicts[i,j] = np.sqrt(np.dot(X[i]-self.X_train[i], X[i]-X_train[j]))
  83. # 使用 函数 numpy.linalg.norm 来实现
  84. # dicts[i,j] = np.linalg.norm(self.X_train[j,:] - X[i])
  85. # pass
  86. #####################################################################
  87. # END OF YOUR CODE #
  88. #####################################################################
  89. return dists
  90. def compute_distances_one_loop(self, X):
  91. """
  92. Compute the distance between each test point in X and each training point
  93. in self.X_train using a single loop over the test data.
  94. Input / Output: Same as compute_distances_two_loops
  95. """
  96. num_test = X.shape[0]
  97. num_train = self.X_train.shape[0]
  98. dists = np.zeros((num_test, num_train))
  99. for i in xrange(num_test):
  100. #######################################################################
  101. # TODO: #
  102. # Compute the l2 distance between the ith test point and all training #
  103. # points, and store the result in dists[i, :]. #
  104. #######################################################################
  105. # pass
  106. dists[i] = np.sqrt(np.sum(np.square(self.X_train - X[i]),axis =1))
  107. #######################################################################
  108. # END OF YOUR CODE #
  109. #######################################################################
  110. return dists
  111. def compute_distances_no_loops(self, X):
  112. """
  113. Compute the distance between each test point in X and each training point
  114. in self.X_train using no explicit loops.
  115. Input / Output: Same as compute_distances_two_loops
  116. """
  117. num_test = X.shape[0]
  118. num_train = self.X_train.shape[0]
  119. dists = np.zeros((num_test, num_train))
  120. #########################################################################
  121. # TODO: #
  122. # Compute the l2 distance between all test points and all training #
  123. # points without using any explicit loops, and store the result in #
  124. # dists.
  125. #
  126. # #
  127. # You should implement this function using only basic array operations; #
  128. # in particular you should not use functions from scipy. #
  129. # #
  130. # HINT: Try to formulate the l2 distance using matrix multiplication #
  131. # and two broadcast sums. #
  132. #########################################################################
  133. # pass
  134. # 基本思想就是 (a-b)2 = a2+b2-2ab
  135. A = np.sum(np.square(self.X_train), axis = 1)
  136. B = np.transpose([np.sum(np.square(X), axis =1)])
  137. er_AB = 2*np.dot(X, self.X_train.T)
  138. dists = np.sqrt(A + B - er_AB)
  139. #########################################################################
  140. # END OF YOUR CODE #
  141. #########################################################################
  142. return dists
  143. def predict_labels(self, dists, k=1):
  144. """
  145. Given a matrix of distances between test points and training points,
  146. predict a label for each test point.
  147. Inputs:
  148. - dists: A numpy array of shape (num_test, num_train) where dists[i, j]
  149. gives the distance betwen the ith test point and the jth training point.
  150. Returns:
  151. - y: A numpy array of shape (num_test,) containing predicted labels for the
  152. test data, where y[i] is the predicted label for the test point X[i].
  153. """
  154. num_test = dists.shape[0]
  155. y_pred = np.zeros(num_test)
  156. for i in xrange(num_test):
  157. # A list of length k storing the labels of the k nearest neighbors to
  158. # the ith test point.
  159. closest_y = []
  160. #########################################################################
  161. # TODO: #
  162. # Use the distance matrix to find the k nearest neighbors of the ith #
  163. # testing point, and use self.y_train to find the labels of these #
  164. # neighbors. Store these labels in closest_y. #
  165. # Hint: Look up the function numpy.argsort. #
  166. #########################################################################
  167. # pass
  168. closest_y = self.y_train[np.argsort(dists[i])[:k]]
  169. #argsort函数返回的是数组值从小到大的索引值
  170. #########################################################################
  171. # TODO: #
  172. # Now that you have found the labels of the k nearest neighbors, you #
  173. # need to find the most common label in the list closest_y of labels. #
  174. # Store this label in y_pred[i]. Break ties by choosing the smaller #
  175. # label. #
  176. #########################################################################
  177. # pass
  178. y_pred[i] = np.argmax(np.bincount(closest_y))
  179. # argmax 返回最值所在的索引
  180. #########################################################################
  181. # #
  182. #########################################################################
  183. return y_pred

gpu 使用环境

  1. # packages in environment at C:\Anaconda3\envs\py35_gpu:
  2. #
  3. _nb_ext_conf 0.4.0 py35_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  4. absl-py 0.2.0 <pip>
  5. anaconda-client 1.6.3 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  6. astor 0.6.2 <pip>
  7. audioread 2.1.5 <pip>
  8. bleach 1.5.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  9. bleach 1.5.0 <pip>
  10. certifi 2016.2.28 py35_0 defaults
  11. certifi 2018.4.16 <pip>
  12. chardet 3.0.4 <pip>
  13. cloudpickle 0.5.3 <pip>
  14. clyent 1.2.2 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  15. colorama 0.3.9 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  16. cycler 0.10.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  17. dask 0.18.1 <pip>
  18. decorator 4.3.0 <pip>
  19. decorator 4.1.2 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  20. Django 2.0.4 <pip>
  21. entrypoints 0.2.3 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  22. enum34 1.1.6 <pip>
  23. gast 0.2.0 <pip>
  24. grpcio 1.11.0 <pip>
  25. html5lib 0.9999999 <pip>
  26. html5lib 0.9999999 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  27. icu 57.1 vc14_0 [vc14] https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  28. idna 2.7 <pip>
  29. image 1.5.20 <pip>
  30. imageio 2.3.0 <pip>
  31. ipykernel 4.6.1 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  32. ipython 6.1.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  33. ipython_genutils 0.2.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  34. ipywidgets 6.0.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  35. jedi 0.10.2 py35_2 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  36. jinja2 2.9.6 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  37. joblib 0.11 <pip>
  38. jpeg 9b vc14_0 [vc14] https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  39. jsonschema 2.6.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  40. jupyter_client 5.1.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  41. jupyter_core 4.3.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  42. Keras 2.1.2 <pip>
  43. kiwisolver 1.0.1 <pip>
  44. libpng 1.6.30 vc14_1 [vc14] https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  45. librosa 0.6.0 <pip>
  46. llvmlite 0.23.0 <pip>
  47. lxml 4.2.3 <pip>
  48. Markdown 2.6.10 <pip>
  49. markupsafe 1.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  50. matplotlib 2.2.2 <pip>
  51. matplotlib 2.0.2 np113py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  52. mistune 0.7.4 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  53. mkl 2017.0.3 0 defaults
  54. nb_anacondacloud 1.4.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  55. nb_conda 2.2.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  56. nb_conda_kernels 2.1.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  57. nbconvert 5.2.1 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  58. nbformat 4.4.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  59. nbpresent 3.0.2 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  60. networkx 2.1 <pip>
  61. nltk 3.3 <pip>
  62. notebook 5.0.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  63. numba 0.38.0 <pip>
  64. numpy 1.13.1 py35_0 defaults
  65. numpy 1.14.5 <pip>
  66. opencv-python 3.4.1 <pip>
  67. openssl 1.0.2l vc14_0 [vc14] https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  68. pandas 0.20.3 py35_0 defaults
  69. pandocfilters 1.4.2 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  70. path.py 10.3.1 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  71. pickleshare 0.7.4 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  72. Pillow 5.1.0 <pip>
  73. pip 10.0.1 <pip>
  74. pip 9.0.1 py35_1 defaults
  75. progressbar2 3.38.0 <pip>
  76. prompt_toolkit 1.0.15 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  77. protobuf 3.5.1 <pip>
  78. pygments 2.2.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  79. pyparsing 2.2.0 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  80. pyqt 5.6.0 py35_2 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  81. pyreadline 2.1 <pip>
  82. python 3.5.4 0 defaults
  83. python-dateutil 2.6.1 py35_0 defaults
  84. python-utils 2.3.0 <pip>
  85. pytz 2017.2 py35_0 defaults
  86. PyWavelets 0.5.2 <pip>
  87. pyyaml 3.12 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  88. PyYAML 3.12 <pip>
  89. pyzmq 16.0.2 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  90. qt 5.6.2 vc14_6 [vc14] https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  91. requests 2.19.1 <pip>
  92. requests 2.14.2 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  93. resampy 0.2.0 <pip>
  94. scikit-image 0.14.0 <pip>
  95. scikit-learn 0.19.0 np113py35_0 defaults
  96. scikit-learn 0.19.1 <pip>
  97. scipy 1.1.0 <pip>
  98. scipy 0.19.1 np113py35_0 defaults
  99. setuptools 36.4.0 py35_1 defaults
  100. setuptools 38.2.5 <pip>
  101. simplegeneric 0.8.1 py35_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  102. sip 4.18 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  103. six 1.10.0 py35_1 defaults
  104. six 1.11.0 <pip>
  105. tensorboard 1.8.0 <pip>
  106. tensorflow 1.8.0 <pip>
  107. tensorflow-gpu 1.4.0 <pip>
  108. tensorflow-tensorboard 0.4.0rc3 <pip>
  109. tensorlayer 1.9.0 <pip>
  110. termcolor 1.1.0 <pip>
  111. testpath 0.3.1 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  112. tk 8.5.18 vc14_0 [vc14] https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  113. toolz 0.9.0 <pip>
  114. torch 0.4.0 <pip>
  115. torchvision 0.2.1 <pip>
  116. tornado 4.5.2 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  117. tqdm 4.23.4 <pip>
  118. traitlets 4.3.2 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  119. urllib3 1.23 <pip>
  120. vc 14 0 defaults
  121. vs2015_runtime 14.0.25420 0 defaults
  122. wcwidth 0.1.7 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  123. Werkzeug 0.13 <pip>
  124. wheel 0.29.0 py35_0 defaults
  125. wheel 0.30.0 <pip>
  126. widgetsnbextension 3.0.2 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  127. win_unicode_console 0.5 py35_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  128. wincertstore 0.2 py35_0 defaults
  129. wrapt 1.10.11 <pip>
  130. zlib 1.2.11 vc14_0 [vc14] https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注