Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. base_model = VGG19(weights=None, include_top=False, pooling='avg', input_shape=(LEFT, RIGHT, 3))
  2.  
  3. # add a global spatial average pooling layer
  4. x = base_model.output
  5. x = Dense(1024, activation='relu')(x)
  6.  
  7. # and a logistic layer -- let's say we have 2 classes
  8. predictions = Dense(2, activation='softmax')(x)
  9.  
  10. # this is the model we will train
  11. model = Model(inputs=base_model.input, outputs=predictions)
  12.  
  13. # Print the layers
  14. for i, layer in enumerate(model.layers):
  15. print(i, layer.name, layer.output_shape)
  16. plot_model(model, show_shapes=True, to_file=MODELDIR + IDENTNAME + '_model.png')
  17.  
  18. # we chose to train the top inception blocks, i.e. we will freeze
  19. # the first 5 layers and unfreeze the rest:
  20. for layer in model.layers[:10]:
  21. layer.trainable = True
  22. for layer in model.layers[10:]:
  23. layer.trainable = True
  24.  
  25. # we need to recompile the model for these modifications to take effect
  26.  
  27. from keras.optimizers import Adam
  28.  
  29. optimizer = Adam(lr=0.00008, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=True)
  30.  
  31. model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
  32.  
  33. history = model.fit_generator(generator(BATCHSIZE, DATADIR), steps_per_epoch=DATASTEPS,
  34. validation_data=generator(NUMVALIDATIONFILES, VALIDATIONDIR), validation_steps=1,
  35. epochs=EPOCHS, verbose=1, class_weight={0: 1, 1: 1})
  36.  
  37. # Save model and weights....
  38. # serialize model to YAML
  39. model_yaml = model.to_yaml()
  40. with open(MODELDIR + IDENTNAME + '_model.yaml', "w") as yaml_file:
  41. yaml_file.write(model_yaml)
  42. # serialize weights to HDF5
  43. model.save_weights(MODELDIR + IDENTNAME + '_weights.h5')
  44. print("Saved model to disk")
  45.  
  46. #######predict one image#####
  47. from keras.preprocessing.image import load_img
  48. image = load_img('picture.png', target_size=(64, 64))
  49. from keras.preprocessing.image import img_to_array
  50. image = img_to_array(image)
  51. image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
  52. from keras.applications.vgg19 import preprocess_input
  53. image = preprocess_input(image)
  54. yhat = model.predict(image)
  55.  
  56. # create a list containing the class labels
  57. class_labels = ['class1', 'class2']
  58.  
  59. # find the index of the class with maximum score
  60. pred = np.argmax(class_labels, axis=-1)
  61.  
  62. # print the label of the class with maximum score
  63. print(class_labels[pred[0]])
  64.  
  65. batch_features[i, :, :, :] = imageio.imread(t)[:, :, :3]
  66.  
  67. from keras.preprocessing.image import load_img
  68. image = load_img('picture.png', target_size=(64, 64, 3))
  69. np.expand_dims(image, axis=0)
  70. from keras.preprocessing.image import img_to_array
  71. image = img_to_array(image)
  72. image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
  73.  
  74. from keras.applications.vgg19 import preprocess_input
  75. image = preprocess_input(image)
  76. yhat = model.predict(image)
  77. # create a list containing the class labels
  78. class_labels = ['class1', 'class2']
  79. # find the index of the class with maximum score
  80. pred = np.argmax(class_labels, axis=-1)
  81. # print the label of the class with maximum score
  82. print(class_labels[pred[0]])
  83.  
  84. image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
  85.  
  86. ?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement