当前位置:   article > 正文

深度学习 keras_Keras深度学习教程

the shared vision model

深度学习 keras

什么是Keras(What is Keras?)

Keras is a high-level neural networks API. It is written in Python and can run on top of Theano, TensorFlow or CNTK. It was developed with the idea of:

Keras是高级神经网络API。 它是用Python编写的,可以在Theano,TensorFlow或CNTK之上运行。 它的开发思想是:

Being able to go from idea to result with the least possible delay is key to doing good research.
能够以尽可能少的延迟将想法付诸实践是进行良好研究的关键。

Keras is a user-friendly, extensible and modular library which makes prototyping easy and fast. It supports convolutional networks, recurrent networks and even the combination of both.

Keras是一个用户友好,可扩展的模块化库,可轻松快速地制作原型。 它支持卷积网络,循环网络,甚至两者的组合。

Initial development of Keras was a part of the research of project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System).

Keras的最初开发是ONEIROS(开放式神经电子智能机器人操作系统)项目研究的一部分。

为什么选择Keras? (Why Keras?)

There are countless deep-learning frameworks available today, but there are some of the areas in which Keras proved better than other alternatives.

当今有无数的深度学习框架可用,但是Keras在某些领域被证明比其他选择要好。

Keras focuses on minimal user action requirement when common use cases are concerned also if the user makes an error, clear and actionable feedback is provided. This makes keras easy to learn and use.

当涉及到常见用例时,如果用户犯了错误,提供了清晰且可行的反馈,Keras专注于最小化用户操作要求。 这使喀拉拉邦易于学习和使用

When you want to put your Keras models to use into some application, you need to deploy it on other platforms which is comparatively easy if you are using keras. It also supports multiple backends and also allows portability across backends i.e. you can train using one backend and load it with another.

当您想将Keras模型用于某些应用程序时,您需要将其部署在其他平台上,如果您使用的是keras,这相对容易。 它还支持多个后端,并且还允许跨后端进行可移植性,即,您可以使用一个后端进行培训,然后将其加载到另一个后端。

It has got a strong back with built-in multiple GPU support, it also supports distributed training.

凭借内置的多个GPU支持获得了强大的支持,它还支持分布式培训。

Keras教程 (Keras Tutorial)

安装Keras (Installing Keras)

We need to install one of the backend engines before we actually get to installing Keras. Let’s go and install any of TensorFlow or Theano or CNTK modules.

在实际安装Keras之前,我们需要安装一个后端引擎。 我们去安装任何TensorFlowTheanoCNTK模块。

Now, we are ready to install keras. We can either use pip installation or clone the repository from git. To install using pip, open the terminal and run the following command:

现在,我们准备安装keras。 我们可以使用pip安装或从git克隆存储库。 要使用pip进行安装,请打开终端并运行以下命令:

pip install keras

In case pip installation doesn’t work or you want another method, you can clone the git repository using

如果无法安装pip或需要其他方法,则可以使用以下命令克隆git存储库

git clone https://github.com/keras-team/keras.git

Once cloned, move to the cloned directory and run:

克隆后,移至克隆目录并运行:

sudo python setup.py install

使用Keras (Using Keras)

To use Keras in any of your python scripts we simply need to import it using:

要在您的任何python脚本中使用Keras,我们只需要使用以下命令导入即可:

import keras

密集连接的网络 (Densely Connected Network)

A Sequential model is probably a better choice to create such network, but we are just getting started so it’s a better choice to start with something really simple:

顺序模型可能是创建此类网络的更好选择,但是我们才刚刚开始,因此从一个非常简单的东西开始是一个更好的选择:

  1. from keras.layers import Input, Dense
  2. from keras.models import Model
  3. # This returns a tensor
  4. inputs = Input(shape=(784,))
  5. # a layer instance is callable on a tensor, and returns a tensor
  6. x = Dense(64, activation='relu')(inputs)
  7. x = Dense(64, activation='relu')(x)
  8. predictions = Dense(10, activation='softmax')(x)
  9. # This creates a model that includes
  10. # the Input layer and three Dense layers
  11. model = Model(inputs=inputs, outputs=predictions)
  12. model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

Now that you have seen how to create a simple Densely Connected Network model you can train it with your training data and may use it in your deep learning module.

既然您已经了解了如何创建简单的密集连接网络模型,则可以将其与训练数据一起训练,并可以在深度学习模块中使用它。

顺序模型 (Sequential Model)

Model is core data structure of Keras. The simplest type of model is a linear stack of layers, we call it Sequential Model. Let’s put our hands in code and try to build one:

模型是Keras的核心数据结构。 模型的最简单类型是层的线性堆栈,我们称之为顺序模型。 让我们动手编写代码,尝试构建一个:

  1. # import required modules
  2. from keras.models import Sequential
  3. from keras.layers import Dense
  4. import numpy as np
  5. # Create a model
  6. model= Sequential()
  7. # Stack Layers
  8. model.add(Dense(units=64, activation='relu', input_dim=100))
  9. model.add(Dense(units=10, activation='softmax'))
  10. # Configure learning
  11. model.compile(loss='categorical_crossentropy', optimizer='sgd',metrics=['accuracy'])
  12. # Create Numpy arrays with random values, use your training or test data here
  13. x_train = np.random.random((64,100))
  14. y_train = np.random.random((64,10))
  15. x_test = np.random.random((64,100))
  16. y_test = np.random.random((64,10))
  17. # Train using numpy arrays
  18. model.fit(x_train, y_train, epochs=5, batch_size=32)
  19. # evaluate on existing data
  20. loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)
  21. # Generate predictions on new data
  22. classes = model.predict(x_test, batch_size=128)

Let’s run the program to see the results:

keras tutorial, keras deep learning tutorial

让我们运行程序以查看结果:

Let’s try a few more models and how to create them like, Residual Connection on a Convolution Layer:

让我们尝试其他一些模型,以及如何在卷积层上创建“残留连接”,例如:

  1. from keras.layers import Conv2D, Input
  2. # input tensor for a 3-channel 256x256 image
  3. x = Input(shape=(256, 256, 3))
  4. # 3x3 conv with 3 output channels (same as input channels)
  5. y = Conv2D(3, (3, 3), padding='same')(x)
  6. # this returns x + y.
  7. z = keras.layers.add([x, y])

共享视觉模型 (Shared Vision Model)

Shared Vision Model helps to classify whether two MNIST digits are the same digit or different digits by reusing the same image-processing module on two inputs. Let’s create one as shown below.

共享视觉模型通过在两个输入上重用相同的图像处理模块,有助于对两个MNIST数字是相同数字还是不同数字进行分类。 让我们创建一个如下所示。

  1. from keras.layers import Conv2D, MaxPooling2D, Input, Dense, Flatten
  2. from keras.models import Model
  3. import keras
  4. # First, define the vision modules
  5. digit_input = Input(shape=(27, 27, 1))
  6. x = Conv2D(64, (3, 3))(digit_input)
  7. x = Conv2D(64, (3, 3))(x)
  8. x = MaxPooling2D((2, 2))(x)
  9. out = Flatten()(x)
  10. vision_model = Model(digit_input, out)
  11. # Then define the tell-digits-apart model
  12. digit_a = Input(shape=(27, 27, 1))
  13. digit_b = Input(shape=(27, 27, 1))
  14. # The vision model will be shared, weights and all
  15. out_a = vision_model(digit_a)
  16. out_b = vision_model(digit_b)
  17. concatenated = keras.layers.concatenate([out_a, out_b])
  18. out = Dense(1, activation='sigmoid')(concatenated)
  19. classification_model = Model([digit_a, digit_b], out)

视觉问答模型 (Visual Question Answering Model)

Let’s create a model which can choose the correct one-word answer to a natural-language question about a picture.

让我们创建一个模型,该模型可以为有关图片的自然语言问题选择正确的单字答案。

It can be done by encoding the question and image into two separate vectors, concatenating both of them and training on top a logistic regression over some vocabulary of potential answers. Let’s try the model:

可以通过将问题和图像编码为两个单独的向量,并将它们连接在一起,并在一些潜在答案的词汇表上进行逻辑回归来训练来完成。 让我们尝试一下模型:

  1. from keras.layers import Conv2D, MaxPooling2D, Flatten
  2. from keras.layers import Input, LSTM, Embedding, Dense
  3. from keras.models import Model, Sequential
  4. import keras
  5. # First, let's define a vision model using a Sequential model.
  6. # This model will encode an image into a vector.
  7. vision_model = Sequential()
  8. vision_model.add(Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(224, 224, 3)))
  9. vision_model.add(Conv2D(64, (3, 3), activation='relu'))
  10. vision_model.add(MaxPooling2D((2, 2)))
  11. vision_model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
  12. vision_model.add(Conv2D(128, (3, 3), activation='relu'))
  13. vision_model.add(MaxPooling2D((2, 2)))
  14. vision_model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
  15. vision_model.add(Conv2D(256, (3, 3), activation='relu'))
  16. vision_model.add(Conv2D(256, (3, 3), activation='relu'))
  17. vision_model.add(MaxPooling2D((2, 2)))
  18. vision_model.add(Flatten())
  19. # Now let's get a tensor with the output of our vision model:
  20. image_input = Input(shape=(224, 224, 3))
  21. encoded_image = vision_model(image_input)
  22. # Next, let's define a language model to encode the question into a vector.
  23. # Each question will be at most 100 word long,
  24. # and we will index words as integers from 1 to 9999.
  25. question_input = Input(shape=(100,), dtype='int32')
  26. embedded_question = Embedding(input_dim=10000, output_dim=256, input_length=100)(question_input)
  27. encoded_question = LSTM(256)(embedded_question)
  28. # Let's concatenate the question vector and the image vector:
  29. merged = keras.layers.concatenate([encoded_question, encoded_image])
  30. # And let's train a logistic regression over 1000 words on top:
  31. output = Dense(1000, activation='softmax')(merged)
  32. # This is our final model:
  33. vqa_model = Model(inputs=[image_input, question_input], outputs=output)
  34. # The next stage would be training this model on actual data.

If you want to learn more about Visual Question Answering (VQA), check out this beginner’s guide to VQA.

如果您想了解有关视觉问答(VQA)的更多信息,请查阅此VQA初学者指南

训练神经网络 (Training Neural Network)

Now that we have seen how to build different models using Keras, let’s put things together and work on a complete example. The following example trains a Neural Network on MNIST data set:

既然我们已经了解了如何使用Keras构建不同的模型,那么让我们将它们放在一起并研究一个完整的示例。 以下示例在MNIST数据集上训练神经网络:

  1. import keras
  2. from keras.datasets import mnist
  3. from keras.models import Sequential
  4. from keras.layers import Dense, Dropout
  5. from keras.optimizers import RMSprop
  6. batch_size = 128
  7. num_classes = 10
  8. epochs = 20
  9. # the data, shuffled and split between train and test sets
  10. (x_train, y_train), (x_test, y_test) = mnist.load_data()
  11. x_train = x_train.reshape(60000, 784)
  12. x_test = x_test.reshape(10000, 784)
  13. x_train = x_train.astype('float32')
  14. x_test = x_test.astype('float32')
  15. x_train /= 255
  16. x_test /= 255
  17. print(x_train.shape[0], 'train samples')
  18. print(x_test.shape[0], 'test samples')
  19. # convert class vectors to binary class matrices
  20. y_train = keras.utils.to_categorical(y_train, num_classes)
  21. y_test = keras.utils.to_categorical(y_test, num_classes)
  22. model = Sequential()
  23. model.add(Dense(512, activation='relu', input_shape=(784,)))
  24. model.add(Dropout(0.2))
  25. model.add(Dense(512, activation='relu'))
  26. model.add(Dropout(0.2))
  27. model.add(Dense(num_classes, activation='softmax'))
  28. model.summary()
  29. # Compile model
  30. model.compile(loss='categorical_crossentropy',
  31. optimizer=RMSprop(),
  32. metrics=['accuracy'])
  33. history = model.fit(x_train, y_train,
  34. batch_size=batch_size,
  35. epochs=epochs,
  36. verbose=1,
  37. validation_data=(x_test, y_test))
  38. score = model.evaluate(x_test, y_test, verbose=0)
  39. # Print the results
  40. print('Test loss:', score[0])
  41. print('Test accuracy:', score[1])

Let’s run this example and wait for results:

keras example, keras neural network tutorial

The output shows only the final part, it might take a few minutes for the program to finish execution depending on machine

让我们运行此示例并等待结果:

输出仅显示最后一部分,根据机器的不同,程序可能需要几分钟才能完成执行

结论 (Conclusion)

In this tutorial, we discovered that Keras is a powerful framework and makes it easy for the user to create prototypes and that too very quickly. We have also seen how different models can be created using keras. These models can be used for feature extraction, fine-tuning and prediction. We have also seen how to train a neural network using keras.

在本教程中,我们发现Keras是一个功能强大的框架,它使用户易于创建原型,而且开发起来太快了。 我们还看到了如何使用keras创建不同的模型。 这些模型可用于特征提取,微调和预测。 我们还看到了如何使用keras训练神经网络。

Keras has grown popular with other frameworks and it is one of the most popular frameworks on Kaggle.

Keras在其他框架中变得越来越流行,它是Kaggle上最受欢迎的框架之一。

翻译自: https://www.journaldev.com/18314/keras-deep-learning-tutorial

深度学习 keras

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

闽ICP备14008679号