Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

support.py 4.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  1. import tensorflow as tf
  2. import matplotlib.pyplot as plt
  3. import matplotlib.image as mpimg
  4. import os
  5. import signal
  6. import numpy as np
  7. import random
  8. from tensorflow.keras.models import load_model
  9. from tensorflow.keras.preprocessing.image import img_to_array, load_img
  10. def path_update(root_dir, composing_dirs):
  11. composed_dirs = []
  12. for path in composing_dirs:
  13. if type(path) == list:
  14. dirs = [os.path.join(root_dir, i) for i in path]
  15. composed_dirs.extend(dirs)
  16. else:
  17. dirs = os.path.join(root_dir, path)
  18. composed_dirs.append(dirs)
  19. return composed_dirs
  20. def visualize_model(model_path,model_weight_path, img_paths):
  21. model = load_model(model_path)
  22. model.load_weights(model_weight_path)
  23. successive_outputs = [layer.output for layer in model.layers[1:]]
  24. # visualization_model = Model(img_input, successive_outputs)
  25. visualization_model = tf.keras.models.Model(inputs=model.input, outputs=successive_outputs)
  26. # prepare a random input image from the training set.
  27. #img_path = random.choice(img_paths) ## comment for enter only one perticular pic.
  28. img = load_img(img_paths, target_size=(224, 224)) # this is a PIL image
  29. x = img_to_array(img) # Numpy array with shape (150, 150, 3)
  30. x = x.reshape((1,) + x.shape) # Numpy array with shape (1, 150, 150, 3)
  31. # Rescale by 1/255
  32. x /= 255.
  33. # run our image through our network, thus obtaining all
  34. # intermediate representations for this image.
  35. successive_feature_maps = visualization_model.predict(x)
  36. # These are the names of the layers, so can have them as part of our plot
  37. layer_names = [layer.name for layer in model.layers[1:]]
  38. # display our representations
  39. for layer_name, feature_map in zip(layer_names, successive_feature_maps):
  40. if len(feature_map.shape) == 4:
  41. n_features = feature_map.shape[-1] # number of features in feature map
  42. # The feature map has shape (1, size, size, n_features)
  43. size = feature_map.shape[1]
  44. display_grid = np.zeros((size, size * n_features))
  45. for i in range(n_features):
  46. # Postprocess the feature to make it visually palatable
  47. x = feature_map[0, :, :, i]
  48. x -= x.mean()
  49. x /= x.std()
  50. x *= 64
  51. x += 128
  52. x = np.clip(x, 0, 255).astype('uint8')
  53. # We'll tile each filter into this big horizontal grid
  54. display_grid[:, i * size: (i + 1) * size] = x
  55. # Display the grid
  56. scale = 20. / n_features
  57. plt.figure(figsize=(scale * n_features, scale))
  58. plt.title(layer_name)
  59. plt.grid(False)
  60. plt.imshow(display_grid, aspect='auto', cmap='viridis')
  61. def plot_sample_of_img(nrows, ncols, img_paths):
  62. fig = plt.gcf()
  63. fig.set_size_inches(ncols * 4, nrows * 4)
  64. for i, img_path in enumerate(img_paths):
  65. # Set up subplot; subplot indices start at 1
  66. sp = plt.subplot(nrows, ncols, i + 1)
  67. sp.axis('Off') # Don't show axes (or gridlines)
  68. img = mpimg.imread(img_path)
  69. plt.imshow(img)
  70. plt.show()
  71. def save(model, name):
  72. model_save_path = '../../h5_files/models/'
  73. weight_save_path = '../../h5_files/weights/'
  74. if not os.path.exists(model_save_path):
  75. os.mkdir(model_save_path)
  76. if not os.path.exists(weight_save_path):
  77. os.mkdir(weight_save_path)
  78. model.save(os.path.join(model_save_path, name))
  79. model.save_weights(os.path.join(weight_save_path, name))
  80. return os.path.join(model_save_path, name),os.path.join(weight_save_path, name)
  81. def rnd_predict(model_path, model_weight_path, img_path, clToInt_dict):
  82. model = load_model(model_path)
  83. model.load_weights(model_weight_path)
  84. x_img = load_img(img_path, target_size=(224, 224))
  85. x = img_to_array(x_img)
  86. x = np.expand_dims(x, axis=0)
  87. result = model.predict(x)
  88. img_class = np.argmax(result[0])
  89. str_img_class = clToInt_dict[img_class]
  90. plt.imshow(x_img)
  91. plt.title(str_img_class)
  92. plt.show()
  93. return str_img_class, img_class
  94. #os.kill(os.getpid(), signal.SIGKILL)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...