Guest User

Untitled

a guest
Nov 21st, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. from keras.applications.inception_v3 import InceptionV3
  2. from keras.preprocessing import image
  3. from keras.models import Model
  4. from keras.layers import Dense, GlobalAveragePooling2D
  5. from keras import backend as K
  6. from keras.preprocessing.image import ImageDataGenerator
  7. from keras.layers import Input
  8.  
  9. # dimensions of our images.
  10. img_width, img_height = 150, 150
  11.  
  12. train_data_dir = '/Users/michael/testdata/train' #contains two classes cats and dogs
  13. validation_data_dir = '/Users/michael/testdata/validation' #contains two classes cats and dogs
  14.  
  15. nb_train_samples = 1200
  16. nb_validation_samples = 800
  17. nb_epoch = 50
  18.  
  19. # create the base pre-trained model
  20. base_model = InceptionV3(weights='imagenet', include_top=False)
  21.  
  22. # add a global spatial average pooling layer
  23. x = base_model.output
  24. x = GlobalAveragePooling2D()(x)
  25. # let's add a fully-connected layer
  26. x = Dense(1024, activation='relu')(x)
  27. # and a logistic layer -- let's say we have 200 classes
  28. predictions = Dense(200, activation='softmax')(x)
  29.  
  30. # this is the model we will train
  31. model = Model(input=base_model.input, output=predictions)
  32.  
  33. # first: train only the top layers (which were randomly initialized)
  34. # i.e. freeze all convolutional InceptionV3 layers
  35. for layer in base_model.layers:
  36. layer.trainable = False
  37.  
  38. # compile the model (should be done *after* setting layers to non-trainable)
  39. #model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
  40. model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics= ['accuracy'])
  41.  
  42. # prepare data augmentation configuration
  43. train_datagen = ImageDataGenerator(
  44. rescale=1./255)#,
  45. # shear_range=0.2,
  46. # zoom_range=0.2,
  47. # horizontal_flip=True)
  48.  
  49. test_datagen = ImageDataGenerator(rescale=1./255)
  50.  
  51. train_generator = train_datagen.flow_from_directory(
  52. train_data_dir,
  53. target_size=(img_width, img_height),
  54. batch_size=16,
  55. class_mode='categorical'
  56. )
  57.  
  58. validation_generator = test_datagen.flow_from_directory(
  59. validation_data_dir,
  60. target_size=(img_width, img_height),
  61. batch_size=16,
  62. class_mode='categorical'
  63. )
  64.  
  65. print "start history model"
  66. history = model.fit_generator(
  67. train_generator,
  68. nb_epoch=nb_epoch,
  69. samples_per_epoch=128,
  70. validation_data=validation_generator,
  71. nb_val_samples=nb_validation_samples) #1020
  72.  
  73. Found 1199 images belonging to 2 classes.
  74. Found 800 images belonging to 2 classes.
  75. start history model
  76. Epoch 1/50
  77. Traceback (most recent call last):
  78. File "/Users/michael/PycharmProjects/keras-imaging/fine-tune-v3-new- classes.py", line 75, in <module>
  79. nb_val_samples=nb_validation_samples) #1020
  80. File "/usr/local/lib/python2.7/site-packages/keras/engine/training.py", line 1508, in fit_generator
  81. class_weight=class_weight)
  82. File "/usr/local/lib/python2.7/site-packages/keras/engine/training.py", line 1261, in train_on_batch
  83. check_batch_dim=True)
  84. File "/usr/local/lib/python2.7/site-packages/keras/engine/training.py", line 985, in _standardize_user_data
  85. exception_prefix='model target')
  86. File "/usr/local/lib/python2.7/site-packages/keras/engine/training.py", line 113, in standardize_input_data
  87. str(array.shape))
  88. ValueError: Error when checking model target: expected dense_2 to have shape (None, 200) but got array with shape (16, 2)
  89. Exception in thread Thread-1:
  90. Traceback (most recent call last):
  91. File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/pytho n2.7/threading.py", line 810, in __bootstrap_inner
  92. self.run()
  93. File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/pytho n2.7/threading.py", line 763, in run
  94. self.__target(*self.__args, **self.__kwargs)
  95. File "/usr/local/lib/python2.7/site-packages/keras/engine/training.py", line 409, in data_generator_task
  96. generator_output = next(generator)
  97. File "/usr/local/lib/python2.7/site-packages/keras/preprocessing/image.py", line 691, in next
  98. target_size=self.target_size)
  99. File "/usr/local/lib/python2.7/site-packages/keras/preprocessing/image.py", line 191, in load_img
  100. img = img.convert('RGB')
  101. File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 844, in convert
  102. self.load()
  103. File "/usr/local/lib/python2.7/site-packages/PIL/ImageFile.py", line 248, in load
  104. return Image.Image.load(self)
  105. AttributeError: 'NoneType' object has no attribute 'Image'
  106.  
  107. ValueError: Error when checking model target: expected dense_2 to have shape (None, 200) but got array with shape (16, 2)
  108.  
  109. # and a logistic layer -- let's say we have 200 classes
  110. predictions = Dense(200, activation='softmax')(x)
Add Comment
Please, Sign In to add comment