当前位置:   article > 正文

Keras_keras csdn

keras csdn

内容来自百度百科和CS230作业

Keras是一个由Python编写的开源人工神经网络库,可以作为Tensorflow、Microsoft-CNTK和Theano的高阶应用程序接口,进行深度学习模型的设计、调试、评估、应用和可视化 。

Keras在代码结构上由面向对象方法编写,完全模块化并具有可扩展性,其运行机制和说明文档有将用户体验和使用难度纳入考虑,并试图简化复杂算法的实现难度。Keras支持现代人工智能领域的主流算法,包括前馈结构和递归结构的神经网络,也可以通过封装参与构建统计学习模型。在硬件和开发环境方面,Keras支持多操作系统下的多GPU并行计算,可以根据后台设置转化为Tensorflow、Microsoft-CNTK等系统下的组件。

Keras tutorial - the Happy House

For your next vacation, you decided to spend a week with five of your friends from school. It is a very convenient house with many things to do nearby. But the most important benefit is that everybody has commited to be happy when they are in the house. So anyone wanting to enter the house must prove their current state of happiness.

As a deep learning expert, to make sure the "Happy" rule is strictly applied, you are going to build an algorithm which that uses pictures from the front door camera to check if the person is happy or not. The door should open only if the person is happy.

You have gathered pictures of your friends and yourself, taken by the front-door camera. The dataset is labbeled.

Building a model in Keras

Keras is very good for rapid prototyping. In just a short time you will be able to build a model that achieves outstanding results.

Exercise: Implement a HappyModel(). This assignment is more open-ended than most. We suggest that you start by implementing a model using the architecture we suggest, and run through the rest of this assignment using that as your initial model. But after that, come back and take initiative to try out other model architectures. For example, you might take inspiration from the model above, but then vary the network architecture and hyperparameters however you wish. You can also use other functions such as AveragePooling2D()GlobalMaxPooling2D()Dropout().

  1. # GRADED FUNCTION: HappyModel
  2. def HappyModel(input_shape):
  3. """
  4. Implementation of the HappyModel.
  5. Arguments:
  6. input_shape -- shape of the images of the dataset
  7. Returns:
  8. model -- a Model() instance in Keras
  9. """
  10. ### START CODE HERE ###
  11. # Feel free to use the suggested outline in the text above to get started, and run through the whole
  12. # exercise (including the later portions of this notebook) once. The come back also try out other
  13. # network architectures as well.
  14. X_input = Input(input_shape)
  15. X = ZeroPadding2D((1, 1))(X_input)
  16. # 66*66*3
  17. print(X.shape)
  18. X = Conv2D(8, (5, 5), strides = (1, 1))(X)
  19. X = BatchNormalization(axis = 3)(X)
  20. # 62*62*8
  21. print(X.shape)
  22. X = Activation('relu')(X)
  23. X = MaxPooling2D((2, 2), strides=(2, 2))(X)
  24. # 31*31*8
  25. print(X.shape)
  26. X = ZeroPadding2D((1, 1))(X)
  27. # 32*32*8
  28. print(X.shape)
  29. X = Conv2D(16, (3, 3), strides = (1, 1))(X)
  30. X = BatchNormalization(axis = 3)(X)
  31. # 30*30*16
  32. print(X.shape)
  33. X = Activation('relu')(X)
  34. X = MaxPooling2D((2, 2), strides=(2, 2))(X)
  35. # 15*15*16
  36. print(X.shape)
  37. X = ZeroPadding2D((1, 1))(X)
  38. # 16*16*16
  39. print(X.shape)
  40. X = Conv2D(32, (3, 3), strides = (1, 1))(X)
  41. X = BatchNormalization(axis = 3)(X)
  42. # 14*14*32
  43. print(X.shape)
  44. X = Activation('relu')(X)
  45. X = MaxPooling2D((2, 2), strides=(2, 2))(X)
  46. # 7*7*32
  47. print(X.shape)
  48. # FLATTEN X (means convert it to a vector) + FULLYCONNECTED
  49. X = Flatten()(X)
  50. Y = Dense(1, activation='sigmoid', name='fc')(X)
  51. # Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
  52. model = Model(inputs = X_input, outputs = Y, name='HappyModel')
  53. ### END CODE HERE ###
  54. return model

create the model

happyModel = HappyModel((64,64,3))

compile the model to configure the learning process

  1. adam = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999)
  2. happyModel.compile(optimizer="adam", loss=keras.losses.mean_squared_error, metrics = ["accuracy"])

 train the model. Choose the number of epochs and the batch size.

happyModel.fit(x=X_train, y=Y_train, epochs=20)

  

test/evaluate the model.

  1. preds = happyModel.evaluate(x=X_test, y=Y_test)
  2. print()
  3. print ("Loss = " + str(preds[0]))
  4. print ("Test Accuracy = " + str(preds[1]))

 怀疑过拟合,减少迭代数

happyModel.fit(x=X_train, y=Y_train, epochs=10)

测试自己的图片

  1. img_path = 'images/my_image.jpg'
  2. img = image.load_img(img_path, target_size=(64, 64))
  3. imshow(img)
  4. x = image.img_to_array(img)
  5. x = np.expand_dims(x, axis=0)
  6. x = preprocess_input(x)
  7. print(happyModel.predict(x))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/978647
推荐阅读
相关标签
  

闽ICP备14008679号