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

sample_prediction.py 2.1 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
  1. from tensorflow.keras.models import load_model
  2. from tensorflow.keras.preprocessing.image import img_to_array, load_img
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import matplotlib.image as mpimg
  6. import os
  7. class Sampling:
  8. def __call__(self, model_path,model_weight_path,numOfImages,img_path,img_root="",
  9. clToInt_dict={0: 'Brain', 1: 'Eye', 2: 'Heart', 3: 'Kidney', 4: 'Other', 5: 'Skeleton'}):
  10. if numOfImages == 1:
  11. self.pred_sample(model_path,model_weight_path,img_path,clToInt_dict)
  12. else:
  13. self.pred_samples(model_path,model_weight_path,img_root,img_path,clToInt_dict)
  14. def pred_sample(self,model_path, model_weight_path, img_path, clToInt_dict):
  15. model = load_model(model_path)
  16. model.load_weights(model_weight_path)
  17. x_img = load_img(img_path, target_size=(224, 224))
  18. x = img_to_array(x_img)
  19. x = np.expand_dims(x, axis=0)
  20. result = model.predict(x)
  21. img_class = np.argmax(result[0])
  22. str_img_class = clToInt_dict[img_class]
  23. plt.imshow(x_img)
  24. plt.title(str_img_class)
  25. plt.show()
  26. return
  27. def pred_samples(self,model_path, model_weight_path, img_root_path,image_name_list, clToInt_dict):
  28. model = load_model(model_path)
  29. model.load_weights(model_weight_path)
  30. num_of_rows = len(image_name_list)//4
  31. fig = plt.gcf()
  32. fig.set_size_inches(4 * 4, num_of_rows * 4)
  33. for i, img_path in enumerate(image_name_list):
  34. x_img = load_img(os.path.join(img_root_path,img_path), target_size=(224, 224))
  35. x = img_to_array(x_img)
  36. x = np.expand_dims(x, axis=0)
  37. result = model.predict(x)
  38. img_class = np.argmax(result[0])
  39. str_img_class = clToInt_dict[img_class]
  40. sp = plt.subplot(4, num_of_rows, i + 1)
  41. sp.axis('Off') # Don't show axes (or gridlines)
  42. img = mpimg.imread(img_path)
  43. plt.imshow(img)
  44. plt.title(str_img_class)
  45. plt.show()
  46. return
  47. sample = Sampling()
Tip!

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

Comments

Loading...