classification_cnn_keras_cross_validation.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. '''This script goes along the blog post
  2. "Building powerful image classification models using very little data"
  3. from blog.keras.io.
  4. ```
  5. data/
  6. train/
  7. final/
  8. final001.png
  9. final002.png
  10. ...
  11. noisy/
  12. noisy001.png
  13. noisy002.png
  14. ...
  15. validation/
  16. final/
  17. final001.png
  18. final002.png
  19. ...
  20. noisy/
  21. noisy001.png
  22. noisy002.png
  23. ...
  24. ```
  25. '''
  26. from keras.preprocessing.image import ImageDataGenerator
  27. from keras.models import Sequential
  28. from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D
  29. from keras.layers import Activation, Dropout, Flatten, Dense
  30. from keras import backend as K
  31. from sklearn.cross_validation import StratifiedKFold
  32. from keras.utils import plot_model
  33. # dimensions of our images.
  34. img_width, img_height = 100, 100
  35. train_data_dir = 'data/train'
  36. validation_data_dir = 'data/validation'
  37. nb_train_samples = 7200
  38. nb_validation_samples = 3600
  39. epochs = 50
  40. batch_size = 16
  41. if K.image_data_format() == 'channels_first':
  42. input_shape = (3, img_width, img_height)
  43. else:
  44. input_shape = (img_width, img_height, 3)
  45. def create_model():
  46. # create your model using this function
  47. model = Sequential()
  48. model.add(Conv2D(60, (2, 2), input_shape=input_shape))
  49. model.add(Activation('relu'))
  50. model.add(MaxPooling2D(pool_size=(2, 2)))
  51. model.add(Conv2D(40, (2, 2)))
  52. model.add(Activation('relu'))
  53. model.add(MaxPooling2D(pool_size=(2, 2)))
  54. model.add(Conv2D(20, (2, 2)))
  55. model.add(Activation('relu'))
  56. model.add(MaxPooling2D(pool_size=(2, 2)))
  57. model.add(Conv2D(40, (2, 2)))
  58. model.add(Activation('relu'))
  59. model.add(MaxPooling2D(pool_size=(2, 2)))
  60. model.add(Conv2D(20, (2, 2)))
  61. model.add(Activation('relu'))
  62. model.add(MaxPooling2D(pool_size=(2, 2)))
  63. model.add(Flatten())
  64. model.add(Dense(256))
  65. model.add(Activation('relu'))
  66. model.add(Dropout(0.2))
  67. model.add(Dense(128))
  68. model.add(Activation('relu'))
  69. model.add(Dropout(0.2))
  70. model.add(Dense(64))
  71. model.add(Activation('relu'))
  72. model.add(Dropout(0.2))
  73. model.add(Dense(32))
  74. model.add(Activation('relu'))
  75. model.add(Dropout(0.05))
  76. model.add(Dense(1))
  77. model.add(Activation('sigmoid'))
  78. model.compile(loss='binary_crossentropy',
  79. optimizer='rmsprop',
  80. metrics=['accuracy'])
  81. model.summary()
  82. plot_model(model, to_file='noise_classification_img100.png', show_shapes=True)
  83. return model
  84. def load_data():
  85. # load your data using this function
  86. # this is the augmentation configuration we will use for training
  87. train_datagen = ImageDataGenerator(
  88. rescale=1. / 255,
  89. shear_range=0.2,
  90. zoom_range=0.2,
  91. horizontal_flip=True)
  92. # this is the augmentation configuration we will use for testing:
  93. # only rescaling
  94. test_datagen = ImageDataGenerator(rescale=1. / 255)
  95. train_generator = train_datagen.flow_from_directory(
  96. train_data_dir,
  97. target_size=(img_width, img_height),
  98. batch_size=batch_size,
  99. class_mode='binary')
  100. return train_generator
  101. #validation_generator = test_datagen.flow_from_directory(
  102. # validation_data_dir,
  103. # target_size=(img_width, img_height),
  104. # batch_size=batch_size,
  105. # class_mode='binary')
  106. def train_and_evaluate_model(model, data_train, data_test):
  107. model.fit_generator(
  108. data_train,
  109. steps_per_epoch=nb_train_samples // batch_size,
  110. epochs=epochs,
  111. shuffle=True,
  112. validation_data=data_test,
  113. validation_steps=nb_validation_samples // batch_size)
  114. if __name__ == "__main__":
  115. n_folds = 10
  116. data_generator = ImageDataGenerator(rescale=1./255, validation_split=0.33)
  117. # check if possible to not do this thing each time
  118. train_generator = data_generator.flow_from_directory(train_data_dir, target_size=(img_width, img_height), shuffle=True, seed=13,
  119. class_mode='binary', batch_size=batch_size, subset="training")
  120. validation_generator = data_generator.flow_from_directory(train_data_dir, target_size=(img_width, img_height), shuffle=True, seed=13,
  121. class_mode='binary', batch_size=batch_size, subset="validation")
  122. model = create_model()
  123. train_and_evaluate_model(model, train_generator, validation_generator)
  124. model.save_weights('noise_classification_img100.h5')