Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and use the Keras library

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to install and use Keras library". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Installation

Using Anaconda's conda install, you can install the Keras library:

Conda install keras

This command will also help you install the relevant dependencies immediately.

Backend configuration

Keras can use one of several available libraries as its back end, which is part of dealing with low-level operations such as tensors. We will use TensorFlow, which is the default setting.

First, we will adjust the configuration of TensorFlow slightly. Specifically, we set the allow_growth option to true, which allows TensorFlow to dynamically increase the amount of GPU memory used instead of pre-allocating everything. If we don't, TensorFlow may try to allocate too much memory, causing your GPU to run out of memory immediately (which is true for me). To do this, put the following code at the beginning of the file:

If your backend is TensorFlow 1.x:

From keras.backend.tensorflow_backend import set_sessionimport tensorflow as tfconfig = tf.ConfigProto () config.gpu_options.allow_growth = Trueset_session (tf.Session (config=config))

For TensorFlow 2.x, you must set set_memory_growth to your GPU to call this function. You can read more about the details behind this in the tf.config.experimental.set_memory_growth documentation.

How to check the version you have? You can view your version of TensorFlow through conda list. Even using the same Anaconda installation commands, for example, I installed 1.13.1 on one computer and 2.1.0 on another.

If you want to force Keras to use your CPU instead of your GPU, add it before the first Keras import:

Import osos.environ ['CUDA_VISIBLE_DEVICES'] ='-1'

Although this is much slower than on GPU, if you don't have enough GPU memory, you should really consider doing so.

Using Keras for image classification

We will use the Intel image classification dataset to demonstrate Keras for image classification. The dataset contains six categories of images, divided into six different directories, which is convenient because Keras provides built-in functionality for processing data in this format.

Although you don't have to worry too much about the math behind the neural network, you need to understand it enough so that you can specify exactly which layers your model consists of.

One of the models provided by Keras is the sequential model, which is a bunch of layers. Creating sequential models and adding layers is very simple:

From keras.models import Sequentialmodel = Sequential () model.add (layer_one) model.add (layer_two) #...

Here is what our model looks like for image classification:

From keras.models import Sequentialfrom keras.layers import Conv2D, MaxPooling2Dfrom keras.layers import Dropout, Flatten, Densefrom keras.layers.normalization import BatchNormalizationmodel = Sequential () model.add (Conv2D (32, kernel_size= (3,3), activation= "relu", input_shape= (150,150,3)) # our images are 150*150model.add (MaxPooling2D (pool_size= (2,2)) model.add (BatchNormalization ()) model.add (Conv2D (64, kernel_size= (3,3)) Activation= "relu") model.add (MaxPooling2D (pool_size= (2,2)) model.add (BatchNormalization ()) model.add (Conv2D (128,kernel_size= (3,3), activation= "relu")) model.add (MaxPooling2D (pool_size= (2,2)) model.add (BatchNormalization () model.add (Flatten ()) model.add (Dense (128activation= "relu") model.add (Dropout (0.15)) model.add (Dense (64, activation= "relu")) model.add (Dense (6) Activation= "softmax"))

The construction of the neural network model is beyond the scope of this module, but in short: the repetitive pattern of the convolution layer (as the pattern becomes more complex, the number of filters is increasing), the maximum pool layer and batch normalization are often used as the first step in image classification. The output of this step is multidimensional, and we flatten it to an one-dimensional vector with a flattening layer. We end up with several densely connected layers with a dropout layer in the middle to help combat overfitting. The last layer must output a vector containing six elements, because we have six categories.

Next, we compile the model, which means we configure it for training:

Model.compile (loss='sparse_categorical_crossentropy', optimizer='adam', metrics= ['accuracy'])

Our loss function sparse_categorical_crossentropy is very suitable for classification problems, and there is no overlap between categories. For a complete discussion of the loss function, see the Keras documentation. We are using the Adam optimizer. A complete list of optimizers can be found in the relevant documentation. And metrics specifies what the model evaluates during training and testing.

Fitting model

Now that we have the structure of the model, we can fit it into our data set.

From keras.preprocessing.image import ImageDataGeneratordatagen = ImageDataGenerator (validation_split=0.2) train_generator = datagen.flow_from_directory ("intel-images/seg_train/seg_train", batch_size=32, target_size= (150150), class_mode= "sparse", subset= "training") validation_generator = datagen.flow_from_directory ("intel-images/seg_train/seg_train", batch_size=32, target_size= (150,150), class_mode= "sparse" Subset= "validation") model.fit_generator (train_generator, epochs=40, validation_data=validation_generator)

(if you want to complete the process faster with lower accuracy, customize to reduce the number of epoch, especially if you are using CPU. Forty epoch takes quite a long time. )

According to the Keras documentation, this is the ImageDataGenerator function of an:

Real-time data enhancement is used to generate batch tensor image data. The data will be cycled (in batches).

Our example does not make any data enhancements. We will introduce this feature later.

In the previous code, validation_split=0.2 means that we will use 20% of the training set for verification. Since the dataset has only one training set and one test set, we must use a subset of the training set as the verification set.

Flow_from_directory is a simple built-in function that fits well with dataset structures like ours: subdirectories for each category.

Class_mode= "sparse" means that we are working on one-dimensional integer tags.

Fit_generatorImageDataGenerator fitted the model to a number of times we specified.

Test model

After the training, we can test the model with the evaluate_generator function. Just like fit_generator, it requires a generator as a parameter. We can create one for our test data, similar to what we do for training data:

Test_datagen = ImageDataGenerator () test_generator = datagen.flow_from_directory ("intel-images/seg_test/seg_test", target_size= (150150), class_mode= "sparse") print (model.evaluate_generator (test_generator))

This will return an array of two values for our example: loss and accuracy. (you can also check by looking at the model.metrics_names value. )

My accuracy is 81.7%, which is not bad for relatively simple models on non-trivial data sets.

Generate prediction

You can now use this model.predict method to generate predictions for any image. This method takes an array of NumPy images as input, where the image is also a NumPy array of shape (150,150,3). To predict an image, you can do the following:

Import skimage.ioimport numpy as npmodel.predict (np.expand_dims (skimage.io.imread ("file.jpg"), axis=0))

Skimage.io.imread reads the image expand_dims and adds another dimension to the array. The output is an array of predictions, where each prediction is an array of values indicating the probability of each category.

Data enhancement

Data enhancement refers to the generation of new training data based on existing training sets to improve accuracy and generalization and reduce over-fitting.

For images, data enhancement can be accomplished by a variety of transformations: rotation, flip, scaling, shift, clipping and so on.

ImageDataGenerator makes this easy. To apply data enhancement to our model, you only need to change two lines of code.

First, ImageDataGenerator instantiates with more parameters to specify the transformation we want:

Python copy code

Datagen = ImageDataGenerator (rotation_range=30, horizontal_flip=True, zoom_range=0.2, shear_range=0.2)

There are more possibilities-- see the image preprocessing documentation for a complete list of parameters.

The second line is the fit_generator phone. This function has optional steps_per_epoch and validation_steps parameters, and we can leave the front because we have a fixed number of training samples. With data enhancement, we may have more training samples, so we have to specify the number to use. If we don't, the function will only use our fixed-size sample set. One step corresponds to a given batch of batch_size.

Model.fit_generator (train_generator, epochs=40, validation_data=validation_generator, steps_per_epoch=1600, validation_steps=32)

Similarly, if you want the process to be faster, feel free to reduce the number of epoch or steps. After 2-3 hours of training, my accuracy is 85.5%.

Save and restore the model

Keras allows you to save trained models in HDF5 format:

Model.save ("images_model.h6")

The recovery model is also one line:

Import keras.modelsmodel = keras.models.load_model ("images_model.h6")

This requires a h6py package if you are using conda install. If not, run this pip command in the Jupyter Notebook unit:

Python copy code

! pip install-- upgrade h6py "how to install and use the Keras Library" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report