SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # packages 2 install
  3. #
  4.  
  5. # !pip install tqdm
  6. # !conda install -y Pillow
  7.  
  8.  
  9. # ---------------------------------------------------------------------
  10. # Load util
  11. import matplotlib.pyplot as plt
  12.  
  13. import numpy as np
  14. import glob
  15.  
  16. from keras.models import Sequential, Model
  17. from keras import optimizers
  18. from keras.layers import Dense, Activation, Conv2D, MaxPool2D, Flatten, BatchNormalization, Dropout
  19. from keras.preprocessing.image import ImageDataGenerator
  20.  
  21. dataset_folder_path = 'MRI_CT_data'
  22. train_folder = dataset_folder_path + '/train'
  23. test_folder = dataset_folder_path + '/test'
  24.  
  25. test_files = glob.glob(test_folder + '/**/*.jpg')
  26. train_files = glob.glob(train_folder + '/**/*.jpg')
  27.  
  28. train_examples = len(train_files)
  29. test_examples = len(test_files)
  30. print("Number of train examples: " , train_examples)
  31. print("Number of test examples: ", test_examples)
  32.  
  33. #   Download and extract the doge and cate pictures.
  34. # ---------------------------------------------------------------------
  35.  
  36.  
  37. from keras.preprocessing.image import ImageDataGenerator
  38.  
  39. """View some sample images:"""
  40.  
  41. datagen = ImageDataGenerator(
  42.         rescale=1./255,
  43.         rotation_range=5,
  44.         zoom_range=0.2,
  45.         horizontal_flip=True)
  46.  
  47.  
  48. # ---------------------------------------------------------------------
  49. # 2. Display 5 random images
  50. # ---------------------------------------------------------------------
  51. img_height = img_width = 200
  52. channels = 1
  53. if (channels == 1):
  54.     color_mode_ = "grayscale"
  55. else:
  56.     color_mode_ = "rgb"
  57.  
  58. #
  59. # train_generator = datagen.flow_from_directory(
  60. #     train_folder,
  61. #     color_mode = color_mode_,
  62. #     target_size=(img_height, img_width),
  63. #     batch_size=1,
  64. #     class_mode=None)
  65.  
  66.  
  67. """## Convolution Neural Networks (CNN)"""
  68.  
  69. model = Sequential()
  70.  
  71. # TODO: Add a CNN:
  72. # Note 1: The input_shape needs to be specified in this case (input_height, input_width, channels)
  73. # Note 2: The order usually goes Conv2D, Activation, MaxPool,
  74. # Note 3: Must be flattened before passing onto Dense layers
  75. # Note 4: The loss is binary_crossentropy
  76. # Note 5: You can use model.add(BatchNormalization()) after every conv2D from the 2nd conv layer
  77.  
  78.  
  79. model.add(Conv2D(8, kernel_size=(3, 3), padding='same', input_shape=(img_width, img_height, channels)))
  80. model.add(Activation(Activation('relu')))
  81. model.add(MaxPool2D(pool_size=(3, 3)))
  82.  
  83. model.add(Conv2D(16, kernel_size=(3, 3), padding='same'))
  84. model.add(BatchNormalization())
  85. model.add(Activation('relu'))
  86. model.add(MaxPool2D(pool_size=(2, 2)))
  87.  
  88. model.add(Conv2D(32, kernel_size=(3, 3), padding='same'))
  89. model.add(BatchNormalization())
  90. model.add(Activation('relu'))
  91. model.add(MaxPool2D(pool_size=(2, 2)))
  92.  
  93. model.add(Conv2D(32, kernel_size=(3, 3), padding='same'))
  94. model.add(BatchNormalization())
  95. model.add(Activation('relu'))
  96. model.add(MaxPool2D(pool_size=(2, 2)))
  97.  
  98.  
  99. # add flatten and Dense
  100. model.add(Flatten())
  101. model.add(Dense(1, activation='sigmoid'))
  102. # optimizer='rmsprop'
  103. model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
  104.  
  105. model.summary()
  106.  
  107. # ------------------------------------------------------------------------------
  108. # Training
  109. # ------------------------------------------------------------------------------
  110.  
  111. batch_size = 20
  112. epoch_num = 30
  113. train_generator = datagen.flow_from_directory(
  114.     train_folder,
  115.     color_mode=color_mode_,
  116.     target_size=(img_height, img_width),
  117.     batch_size=batch_size,
  118.     shuffle=True,
  119.     class_mode='binary')
  120.  
  121. model.fit_generator(train_generator, train_examples // batch_size, epochs=epoch_num)
  122.  
  123. batch_size = 1
  124. test_generator = datagen.flow_from_directory(
  125.     test_folder,
  126.     color_mode=color_mode_,
  127.     target_size=(img_height, img_width),
  128.     batch_size=batch_size,
  129.     class_mode='binary',
  130.     shuffle=False)
  131. y_pred = model.predict_generator(test_generator, test_examples // batch_size, workers=4)
  132. # model.predict_classes(test_x)
  133. # np.count_nonzero(y_pred == test_y)/len(test_y)
  134.  
  135. correct = 0
  136. for i, f in enumerate(test_generator.filenames):
  137.     if f.startswith('ct') and y_pred[i] < 0.5:
  138.         correct += 1
  139.     if f.startswith('mri') and y_pred[i] >= 0.5:
  140.         correct += 1
  141.  
  142. print('Correct predictions: ' + str(correct / len(test_generator.filenames)), ", num of images: ",
  143.       len(test_generator.filenames))
  144.  
  145. # ------------------------------------------------------------------------------
  146. # plot some images
  147. # ------------------------------------------------------------------------------
  148. batch_size = 5
  149. test_generator = datagen.flow_from_directory(
  150.     test_folder,
  151.     color_mode=color_mode_,
  152.     target_size=(img_height, img_width),
  153.     batch_size=batch_size,
  154.     class_mode='binary',
  155.     shuffle=True)
  156.  
  157. x_test, y_test = next(test_generator)
  158.  
  159. p = model.predict(x_test)
  160. p = np.hstack([y_pred, 1 - y_pred])
  161. label_dict = {0: 'ct', 1: 'mri'}
  162.  
  163. plt.figure(figsize=(12, 12))
  164.  
  165. for i in range(batch_size):
  166.     print(i)
  167.  
  168.     plt.subplot(batch_size, 2, 2 * i + 1)
  169.     # plt.imshow(x_test[i] , cmap='gray')
  170.     plt.imshow(np.squeeze(x_test[i], axis=2), cmap='gray')
  171.  
  172.     plt.title(label_dict[y_test[i]])
  173.  
  174.     plt.subplot(batch_size, 2, 2 * i + 2)
  175.     plt.bar(range(2), p[i])
  176.     plt.xticks(range(2), [label_dict[0], label_dict[1]])
  177.  
  178. plt.show()
  179. #Dana banana
  180. from sklearn.metrics import confusion_matrix
  181.  
  182. loss, acc = model.evaluate(x=x_test, y=y_test)
  183. print(loss, acc)
  184. targets = np.argmax(y_test, axis=-1)
  185. probabilities = model.predict(x=x_test)
  186. predictions = np.argmax(probabilities, axis=-1)
  187. print("targets: ", targets)
  188. print("predictions: ", predictions)
  189.  
  190. cm = confusion_matrix(y_true=targets, y_pred=predictions)
  191. print(cm)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top