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

end_to_end.py 4.0 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
117
118
119
120
121
122
123
124
125
126
127
  1. import tensorboard
  2. import tensorflow as tf
  3. from tensorflow import keras
  4. from datetime import datetime
  5. from src.sup.evaluation import *
  6. from src.sup.support import *
  7. from src.sup.test_set_eval import *
  8. from tensorflow.keras.optimizers import RMSprop,SGD,Adam
  9. from tensorflow.keras.layers import Dense,BatchNormalization,Dropout
  10. from tensorflow.keras.layers import Conv2D,MaxPool2D,Flatten
  11. model_name = "end-to-end"
  12. # Load the TensorBoard notebook extension.
  13. #%load_ext tensorboard
  14. # call inline plt.
  15. # Clear any logs from previous runs
  16. #!del -rf
  17. classes = ['Heart','Brain','Eye','Kidney','Skull','Other']
  18. root_dir = 'F:/JetBrain Project Files/Pycharm/human_organs_image_classifiaction/datasets/'
  19. train_dir = os.path.join(root_dir,'train/')
  20. validation_dir = os.path.join(root_dir,'validation/')
  21. tr_heart_dir,tr_brain_dir,tr_eye_dir,tr_kidney_dir,tr_skull_dir,tr_other_dir = path_update(train_dir,classes)
  22. vl_heart_dir,vl_brain_dir,vl_eye_dir,vl_kidney_dir,vl_skull_dir,vl_other_dir = path_update(validation_dir,classes)
  23. plot_sample_of_img(4,4,path_update(tr_heart_dir,os.listdir(tr_heart_dir))[10:18]+path_update(tr_eye_dir,os.listdir(tr_eye_dir))[12:19])
  24. train_gen_tmp = ImageDataGenerator(rescale=1./255,
  25. rotation_range=40,
  26. width_shift_range=0.2,
  27. height_shift_range=0.2,
  28. shear_range=0.2,
  29. zoom_range=0.2,
  30. horizontal_flip=True,
  31. fill_mode='nearest')
  32. validation_gen_tmp = ImageDataGenerator(rescale=1/225.)
  33. train_gen = train_gen_tmp.flow_from_directory(train_dir,
  34. target_size=(150,150),
  35. color_mode='rgb',
  36. class_mode='categorical',
  37. batch_size= 20,
  38. shuffle=True,
  39. seed=42)
  40. validation_gen = validation_gen_tmp.flow_from_directory(validation_dir,
  41. target_size=(150,150),
  42. color_mode='rgb',
  43. class_mode='categorical',
  44. batch_size= 20,
  45. shuffle=True,
  46. seed=42)
  47. STEP_SIZE_TRAIN=train_gen.n//train_gen.batch_size
  48. STEP_SIZE_VALID=validation_gen.n//validation_gen.batch_size
  49. clToInt_dict = train_gen.class_indices
  50. clToInt_dict = dict((v,k) for v,k in clToInt_dict.items())
  51. model = tf.keras.models.Sequential()
  52. model.add(Conv2D(32,(3,3),activation='relu',input_shape=(150,150,3)))
  53. model.add(MaxPool2D(2,2))
  54. model.add(Conv2D(64,(3,3),activation='relu'))
  55. model.add(MaxPool2D(2,2))
  56. model.add(Conv2D(128,(3,3),activation='relu'))
  57. model.add(Flatten())
  58. model.add(Dense(256,activation='relu'))
  59. model.add(Dense(128,activation='relu'))
  60. model.add(Dense(6,activation='softmax'))
  61. model.compile(optimizer='adam',
  62. loss='categorical_crossentropy',
  63. metrics=['accuracy'])
  64. # Define the Keras TensorBoard callback.
  65. logdir="logs/fit/" + datetime.now().strftime("%Y%m%d-%H%M%S")
  66. tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)
  67. history = model.fit_generator(train_gen,
  68. steps_per_epoch=STEP_SIZE_TRAIN,
  69. validation_data=validation_gen,
  70. validation_steps=STEP_SIZE_VALID,
  71. epochs=20,
  72. verbose=1)
  73. #visualize_model(model,img_path)
  74. #call to the tensorboard
  75. #%tensorboard --logdir logs
  76. #look at training model performance
  77. acc_n_loss(history)
  78. model.evaluate_generator(validation_gen,
  79. steps=STEP_SIZE_VALID)
  80. y_pred,y_test = test_eval(model,classes)
  81. plot_confusion_metrix(y_test,y_pred,classes)
  82. ROC_classes(6,y_test,y_pred,classes)
  83. model_path,model_weight_path = save(model,datetime.now()+model_name)
  84. #rnd_predict(model_path,model_weight_path,img_path,clToInt_dict)
Tip!

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

Comments

Loading...