Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. from keras.preprocessing.image import ImageDataGenerator
  2. from keras.models import Sequential
  3. from keras.layers import Conv2D, MaxPooling2D
  4. from keras.layers import Activation, Dropout, Flatten, Dense
  5. from keras import backend as K
  6. from numpy.core import multiarray
  7.  
  8.  
  9. # dimensions of our images.
  10. img_width, img_height = 150, 150
  11.  
  12. train_data_dir = 'arcDataset'
  13. validation_data_dir = 'arcDataset'
  14. nb_train_samples = 10000
  15. nb_validation_samples = 10000
  16. epochs = 50
  17. batch_size = 64
  18.  
  19. if K.image_data_format() == 'channels_first':
  20. input_shape = (3, img_width, img_height)
  21. else:
  22. input_shape = (img_width, img_height, 3)
  23.  
  24. model = Sequential()
  25. model.add(Conv2D(32, (3, 3), input_shape=input_shape))
  26. model.add(Activation('relu'))
  27. model.add(MaxPooling2D(pool_size=(2, 2)))
  28.  
  29. model.add(Conv2D(32, (3, 3)))
  30. model.add(Activation('relu'))
  31. model.add(MaxPooling2D(pool_size=(2, 2)))
  32.  
  33. model.add(Conv2D(64, (3, 3)))
  34. model.add(Activation('relu'))
  35. model.add(MaxPooling2D(pool_size=(2, 2)))
  36.  
  37. model.add(Flatten())
  38. model.add(Dense(64))
  39. model.add(Activation('relu'))
  40. model.add(Dropout(0.5))
  41. model.add(Dense(9))
  42. model.add(Activation('softmax'))
  43.  
  44. # this is the augmentation configuration we will use for training
  45. train_datagen = ImageDataGenerator(
  46. rescale=1. / 255,
  47. shear_range=0.2,
  48. zoom_range=0.2,
  49. horizontal_flip=True)
  50.  
  51. # this is the augmentation configuration we will use for testing:
  52. # only rescaling
  53. test_datagen = ImageDataGenerator(rescale=1. / 255)
  54.  
  55. model.compile(loss='binary_crossentropy', # or categorical_crossentropy
  56. optimizer='rmsprop',# or adagrad
  57. metrics=['accuracy'])
  58.  
  59. train_generator = train_datagen.flow_from_directory(
  60. train_data_dir,
  61. target_size=(img_width, img_height),
  62. batch_size=batch_size,
  63. class_mode='categorical')
  64.  
  65. print(train_generator.class_indices)
  66.  
  67. validation_generator = test_datagen.flow_from_directory(
  68. validation_data_dir,
  69. target_size=(img_width, img_height),
  70. batch_size=batch_size,
  71. class_mode='categorical')
  72.  
  73. model.fit_generator(
  74. train_generator,
  75. steps_per_epoch=nb_train_samples // batch_size,
  76. epochs=epochs,
  77. validation_data=validation_generator,
  78. validation_steps=nb_validation_samples // batch_size)
  79.  
  80. model.save('model.h5')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement