Guest User

Untitled

a guest
Mar 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. def train_model():
  2. batch_size = 1
  3. training_imgs = np.lib.format.open_memmap(filename=os.path.join(data_path, 'data.npy'),mode='r+')
  4. training_masks = np.lib.format.open_memmap(filename=os.path.join(data_path, 'mask.npy'),mode='r+')
  5.  
  6. dl_model = create_model()
  7. print(dl_model.summary())
  8.  
  9. model_checkpoint = ModelCheckpoint(os.path.join(data_path,'mod_weight.hdf5'), monitor='loss',verbose=1, save_best_only=True)
  10.  
  11. dl_model.fit_generator(generator(training_imgs, training_masks, batch_size), steps_per_epoch=(len(training_imgs)/batch_size), epochs=4,verbose=1,callbacks=[model_checkpoint])
  12.  
  13.  
  14. def generator(train_imgs, train_masks=None, batch_size=None):
  15.  
  16. # Create empty arrays to contain batch of features and labels#
  17.  
  18. if train_masks is not None:
  19. train_imgs_batch = np.zeros((batch_size,y_to_res,x_to_res,bands))
  20. train_masks_batch = np.zeros((batch_size,y_to_res,x_to_res,1))
  21. #train_masks = np.expand_dims(train_masks,-1)
  22.  
  23. while True:
  24. for i in range(batch_size):
  25. # choose random index in features
  26.  
  27. index= random.choice(range(len(train_imgs)))
  28.  
  29. train_imgs_batch[i] = train_imgs[index]
  30. train_masks_batch[i] = train_masks[index]
  31.  
  32. yield train_imgs_batch, train_masks_batch
  33.  
  34. else:
  35. rec_imgs_batch = np.zeros((batch_size,y_to_res,x_to_res,bands))
  36. while True:
  37. for i in range(batch_size):
  38. # choose random index in features
  39.  
  40. index= random.choice(range(len(train_imgs)))
  41.  
  42. rec_imgs_batch[i] = train_imgs[index]
  43.  
  44. yield rec_imgs_batch
  45.  
  46. def train_generator(train_images,train_masks,batch_size):
  47. # def train_model_batches():
  48. #image_datagen = ImageDataGenerator(rotation_range=90., horizontal_flip=True, vertical_flip=True, rescale=1./255)
  49. data_gen_args=dict(rotation_range=90.,horizontal_flip=True,vertical_flip=True,rescale=1./255)
  50. #image_datagen = ImageDataGenerator(**data_gen_args)
  51. #mask_datagen = ImageDataGenerator(**data_gen_args)
  52. image_datagen = ImageDataGenerator()
  53. mask_datagen = ImageDataGenerator()
  54. #image_datagen = ImageDataGenerator()
  55. # # Provide the same seed and keyword arguments to the fit and flow methods
  56. seed = 1
  57. image_datagen.fit(train_images, augment=True, seed=seed)
  58. mask_datagen.fit(train_masks, augment=True, seed=seed)
  59. #train_masks = np.expand_dims(train_masks,-1)
  60. image_generator = image_datagen.flow(train_images,batch_size=batch_size)
  61. mask_generator = mask_datagen.flow(train_masks,batch_size=batch_size)
  62. #image_generator = image_datagen.flow_from_directory('data/images',class_mode=None,seed=seed)
  63. #image_generator = image_datagen.flow()
  64. return zip(image_generator, mask_generator)
  65. #mask_generator = mask_datagen.flow_from_directory('data/masks',class_mode=None,seed=seed)
  66.  
  67. Epoch 00001: loss improved from inf to 0.01683, saving model to /home/ubuntu/deep_learn/client_data/mod_weight.hdf5
  68. Epoch 2/4
  69. 7569/7569 [==============================] - 3394s 448ms/step - loss: 0.0049 - binary_crossentropy: 0.0027 - jaccard_coef_int: 0.9983
  70.  
  71. Epoch 00002: loss improved from 0.01683 to 0.00492, saving model to /home/ubuntu/deep_learn/client_data/mod_weight.hdf5
  72. Epoch 3/4
  73. 7569/7569 [==============================] - 3394s 448ms/step - loss: 0.0049 - binary_crossentropy: 0.0026 - jaccard_coef_int: 0.9982
  74.  
  75. Epoch 00003: loss improved from 0.00492 to 0.00488, saving model to /home/ubuntu/deep_learn/client_data/mod_weight.hdf5
  76. Epoch 4/4
  77. 7569/7569 [==============================] - 3394s 448ms/step - loss: 0.0074 - binary_crossentropy: 0.0042 - jaccard_coef_int: 0.9975
  78.  
  79. Epoch 00004: loss did not improve
  80. Traceback (most recent call last):
  81. File "image_rec.py", line 291, in <module>
  82. train_model()
  83. File "image_rec.py", line 208, in train_model
  84. dl_model.fit_generator(train_generator(training_imgs,training_masks,batch_size),steps_per_epoch=1,epochs=1,workers=1)
  85. File "image_rec.py", line 274, in train_generator
  86. image_datagen.fit(train_images, augment=True, seed=seed)
  87. File "/home/ubuntu/pyvirt_test/local/lib/python2.7/site-packages/keras/preprocessing/image.py", line 753, in fit
  88. x = np.copy(x)
  89. File "/home/ubuntu/pyvirt_test/local/lib/python2.7/site-packages/numpy/lib/function_base.py", line 1505, in copy
  90. return array(a, order=order, copy=True)
  91. MemoryError
Add Comment
Please, Sign In to add comment