Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. # extract_features.py
  2.  
  3. import os
  4. import tensorflow as tf
  5. import numpy as np
  6. import scipy.io as sio
  7. from PIL import Image
  8. import h5py
  9.  
  10.  
  11. image_list = 'data/image_names.txt'
  12. model_file = 'inception-2015-12-05/classify_image_graph_def.pb'
  13. feat_file = 'data/inception_v3_features.mat'
  14. labels_file = 'data/nith_light_labels.mat'
  15.  
  16. #outprobs = np.zeros((0,1008))
  17. outfeats = np.zeros((0,2048))
  18. labels = np.zeros((0,1))
  19.  
  20. f = tf.gfile.FastGFile(model_file,'rb')
  21. graph_def = tf.GraphDef()
  22. graph_def.ParseFromString(f.read())
  23. _ = tf.import_graph_def(graph_def,name='')
  24.  
  25. sess = tf.Session()
  26. #softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
  27. feat_tensor = sess.graph.get_tensor_by_name('pool_3:0')
  28. print('inception v3 model file loaded\n')
  29.  
  30. with open(image_list,'r') as list_file:
  31. image_names = list_file.readlines()
  32. image_num = len(image_names)
  33. curnum = 0
  34. print("total image number: %d\n"%(image_num))
  35. for image_name in image_names:
  36. image_name = image_name.strip()
  37. image_label = image_name.split('/')[-2]
  38. image_label = image_label[0:3]
  39. print(image_label)
  40. if image_label == 'hig':
  41. labels = np.vstack((labels,np.squeeze(2)))
  42. elif image_label == 'med':
  43. labels = np.vstack((labels,np.squeeze(1)))
  44. else :
  45. labels = np.vstack((labels,np.squeeze(0)))
  46.  
  47. curnum+=1
  48. print("processing %d/%d: %s\n"%(curnum,image_num,image_name))
  49.  
  50. #orgimage = Image.open(image_name)
  51. #rgbimage = Image.new("RGB",orgimage.size)
  52. #rgbimage.paste(orgimage)
  53.  
  54. mat_image = sio.loadmat(image_name)
  55. print("datatype")
  56. print(type(mat_image))
  57. print(mat_image)
  58. # image_array = np.asarray([v for v in mat_image.values()])
  59. image_array = mat_image['data']
  60.  
  61. print(type(image_array))
  62. print(image_array.shape)
  63. # f = h5py.File(image_name,'r')
  64. # data = f.get('data/variable1')
  65. # data = np.array(data) # For converting to numpy array
  66. image_array = image_array[:,:,[4,3,2]]
  67. feat = sess.run(feat_tensor,{'DecodeJpeg:0': image_array})
  68. #image_data = tf.gfile.FastGFile(image_name,'rb').read()
  69. #feat = sess.run(feat_tensor,{'DecodeJpeg/contents:0': image_data})
  70. #outprobs = np.vstack((outprobs,pred))
  71. outfeats = np.vstack((outfeats,np.squeeze(feat)))
  72.  
  73. # sio.savemat(prob_file,{'outprobs': outprobs})
  74. sio.savemat(feat_file,{'outfeats': outfeats})
  75. sio.savemat(labels_file,{'labels': labels})
  76. sess.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement