Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. from __future__ import absolute_import, division, print_function
  2.  
  3. import tensorflow as tf
  4. from tensorflow.keras import layers
  5. from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
  6. from tensorflow.keras.layers import ZeroPadding3D, Conv3D, MaxPooling3D, BatchNormalization
  7. import numpy as np
  8. import nibabel
  9. import os
  10. import skimage
  11. import h5py
  12. import json
  13.  
  14. with open("config.json") as config_json:
  15. config = json.load(config_json)
  16.  
  17. print("loading masks")
  18. images = []
  19. images_class = []
  20. input_shape = (64,64,64,1)
  21. for file in os.listdir(config["masks"]):
  22. img = nibabel.load(config["masks"]+"/"+file)
  23. data = img.get_fdata()
  24.  
  25. #fit to input shape
  26. bounds = np.sort(np.vstack(np.nonzero(data)))[:, [0, -1]]
  27. x, y, z = bounds
  28. cropped_data = data[x[0]:x[1], y[0]:y[1], z[0]:z[1]]
  29. #print(cropped_data.shape)
  30.  
  31. #resize to fit 64x64x64 box.
  32. #TODO - I should resize it while retaining the overall shape. resize
  33. #would just stretch/shrink to fit the box in all axes
  34. resized_data = skimage.transform.resize(cropped_data, input_shape, anti_aliasing=True, mode='reflect')
  35. images.append(resized_data)
  36. images_class.append(file)
  37.  
  38. print("loading model")
  39. model = tf.keras.models.load_model('fitmodel.h5')
  40. model.summary()
  41.  
  42. #predict using model all the way to flatten
  43. print("predicting")
  44. int_model = tf.keras.Model(inputs=model.input, outputs=model.get_layer('flatten').output)
  45. x = np.array(images)
  46. y = int_model.predict(x)
  47.  
  48. print("outputting signature.json")
  49. out = {}
  50. for i in range(0, len(y)):
  51. out[images_class[i]] = y[i].tolist()
  52. with open("signature.json", "w") as f:
  53. json.dump(out, f)
  54.  
  55. print("all done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement