Learn Keras – Model Evaluation and Prediction work project make money

Keras – Model Evaluation and Model Prediction This chapter deals with the model evaluation and model prediction in Keras. Let us begin by understanding the model evaluation. Model Evaluation Evaluation is a process during development of the model to check whether the model is best fit for the given problem and corresponding data. Keras model provides a function, evaluate which does the evaluation of the model. It has three main arguments, Test data Test data label verbose – true or false Let us evaluate the model, which we created in the previous chapter using test data. score = model.evaluate(x_test, y_test, verbose = 0) print(”Test loss:”, score[0]) print(”Test accuracy:”, score[1]) Executing the above code will output the below information. 0 The test accuracy is 98.28%. We have created a best model to identify the handwriting digits. On the positive side, we can still scope to improve our model. Model Prediction Prediction is the final step and our expected outcome of the model generation. Keras provides a method, predict to get the prediction of the trained model. The signature of the predict method is as follows, predict( x, batch_size = None, verbose = 0, steps = None, callbacks = None, max_queue_size = 10, workers = 1, use_multiprocessing = False ) Here, all arguments are optional except the first argument, which refers the unknown input data. The shape should be maintained to get the proper prediction. Let us do prediction for our MPL model created in previous chapter using below code − pred = model.predict(x_test) pred = np.argmax(pred, axis = 1)[:5] label = np.argmax(y_test,axis = 1)[:5] print(pred) print(label) Here, Line 1 call the predict function using test data. Line 2 gets the first five prediction Line 3 gets the first five labels of the test data. Line 5 – 6 prints the prediction and actual label. The output of the above application is as follows − [7 2 1 0 4] [7 2 1 0 4] The output of both array is identical and it indicate that our model predicts correctly the first five images.

Learn Keras – Applications work project make money

Keras – Applications Keras applications module is used to provide pre-trained model for deep neural networks. Keras models are used for prediction, feature extraction and fine tuning. This chapter explains about Keras applications in detail. Pre-trained models Trained model consists of two parts model Architecture and model Weights. Model weights are large file so we have to download and extract the feature from ImageNet database. Some of the popular pre-trained models are listed below, ResNet VGG16 MobileNet InceptionResNetV2 InceptionV3 Loading a model Keras pre-trained models can be easily loaded as specified below − import keras import numpy as np from keras.applications import vgg16, inception_v3, resnet50, mobilenet #Load the VGG model vgg_model = vgg16.VGG16(weights = ”imagenet”) #Load the Inception_V3 model inception_model = inception_v3.InceptionV3(weights = ”imagenet”) #Load the ResNet50 model resnet_model = resnet50.ResNet50(weights = ”imagenet”) #Load the MobileNet model mobilenet_model = mobilenet.MobileNet(weights = ”imagenet”) Once the model is loaded, we can immediately use it for prediction purpose. Let us check each pre-trained model in the upcoming chapters.

Learn Keras – Convolution Neural Network work project make money

Keras – Convolution Neural Network Let us modify the model from MPL to Convolution Neural Network (CNN) for our earlier digit identification problem. CNN can be represented as below − The core features of the model are as follows − Input layer consists of (1, 8, 28) values. First layer, Conv2D consists of 32 filters and ‘relu’ activation function with kernel size, (3,3). Second layer, Conv2D consists of 64 filters and ‘relu’ activation function with kernel size, (3,3). Thrid layer, MaxPooling has pool size of (2, 2). Fifth layer, Flatten is used to flatten all its input into single dimension. Sixth layer, Dense consists of 128 neurons and ‘relu’ activation function. Seventh layer, Dropout has 0.5 as its value. Eighth and final layer consists of 10 neurons and ‘softmax’ activation function. Use categorical_crossentropy as loss function. Use Adadelta() as Optimizer. Use accuracy as metrics. Use 128 as batch size. Use 20 as epochs. Step 1 − Import the modules Let us import the necessary modules. import keras from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D from keras import backend as K import numpy as np Step 2 − Load data Let us import the mnist dataset. (x_train, y_train), (x_test, y_test) = mnist.load_data() Step 3 − Process the data Let us change the dataset according to our model, so that it can be feed into our model. img_rows, img_cols = 28, 28 if K.image_data_format() == ”channels_first”: x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) input_shape = (1, img_rows, img_cols) else: x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1) x_train = x_train.astype(”float32”) x_test = x_test.astype(”float32”) x_train /= 255 x_test /= 255 y_train = keras.utils.to_categorical(y_train, 10) y_test = keras.utils.to_categorical(y_test, 10) The data processing is similar to MPL model except the shape of the input data and image format configuration. Step 4 − Create the model Let us create tha actual model. model = Sequential() model.add(Conv2D(32, kernel_size = (3, 3), activation = ”relu”, input_shape = input_shape)) model.add(Conv2D(64, (3, 3), activation = ”relu”)) model.add(MaxPooling2D(pool_size = (2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation = ”relu”)) model.add(Dropout(0.5)) model.add(Dense(10, activation = ”softmax”)) Step 5 − Compile the model Let us compile the model using selected loss function, optimizer and metrics. model.compile(loss = keras.losses.categorical_crossentropy, optimizer = keras.optimizers.Adadelta(), metrics = [”accuracy”]) Step 6 − Train the model Let us train the model using fit() method. model.fit( x_train, y_train, batch_size = 128, epochs = 12, verbose = 1, validation_data = (x_test, y_test) ) Executing the application will output the below information − Train on 60000 samples, validate on 10000 samples Epoch 1/12 60000/60000 [==============================] – 84s 1ms/step – loss: 0.2687 – acc: 0.9173 – val_loss: 0.0549 – val_acc: 0.9827 Epoch 2/12 60000/60000 [==============================] – 86s 1ms/step – loss: 0.0899 – acc: 0.9737 – val_loss: 0.0452 – val_acc: 0.9845 Epoch 3/12 60000/60000 [==============================] – 83s 1ms/step – loss: 0.0666 – acc: 0.9804 – val_loss: 0.0362 – val_acc: 0.9879 Epoch 4/12 60000/60000 [==============================] – 81s 1ms/step – loss: 0.0564 – acc: 0.9830 – val_loss: 0.0336 – val_acc: 0.9890 Epoch 5/12 60000/60000 [==============================] – 86s 1ms/step – loss: 0.0472 – acc: 0.9861 – val_loss: 0.0312 – val_acc: 0.9901 Epoch 6/12 60000/60000 [==============================] – 83s 1ms/step – loss: 0.0414 – acc: 0.9877 – val_loss: 0.0306 – val_acc: 0.9902 Epoch 7/12 60000/60000 [==============================] – 89s 1ms/step – loss: 0.0375 -acc: 0.9883 – val_loss: 0.0281 – val_acc: 0.9906 Epoch 8/12 60000/60000 [==============================] – 91s 2ms/step – loss: 0.0339 – acc: 0.9893 – val_loss: 0.0280 – val_acc: 0.9912 Epoch 9/12 60000/60000 [==============================] – 89s 1ms/step – loss: 0.0325 – acc: 0.9901 – val_loss: 0.0260 – val_acc: 0.9909 Epoch 10/12 60000/60000 [==============================] – 89s 1ms/step – loss: 0.0284 – acc: 0.9910 – val_loss: 0.0250 – val_acc: 0.9919 Epoch 11/12 60000/60000 [==============================] – 86s 1ms/step – loss: 0.0287 – acc: 0.9907 – val_loss: 0.0264 – val_acc: 0.9916 Epoch 12/12 60000/60000 [==============================] – 86s 1ms/step – loss: 0.0265 – acc: 0.9920 – val_loss: 0.0249 – val_acc: 0.9922 Step 7 − Evaluate the model Let us evaluate the model using test data. score = model.evaluate(x_test, y_test, verbose = 0) print(”Test loss:”, score[0]) print(”Test accuracy:”, score[1]) Executing the above code will output the below information − Test loss: 0.024936060590433316 Test accuracy: 0.9922 The test accuracy is 99.22%. We have created a best model to identify the handwriting digits. Step 8 − Predict Finally, predict the digit from images as below − pred = model.predict(x_test) pred = np.argmax(pred, axis = 1)[:5] label = np.argmax(y_test,axis = 1)[:5] print(pred) print(label) The output of the above application is as follows − [7 2 1 0 4] [7 2 1 0 4] The output of both array is identical and it indicate our model correctly predicts the first five images.

Learn Keras – Regression Prediction using MPL work project make money

Keras – Regression Prediction using MPL In this chapter, let us write a simple MPL based ANN to do regression prediction. Till now, we have only done the classification based prediction. Now, we will try to predict the next possible value by analyzing the previous (continuous) values and its influencing factors. The Regression MPL can be represented as below − The core features of the model are as follows − Input layer consists of (13,) values. First layer, Dense consists of 64 units and ‘relu’ activation function with ‘normal’ kernel initializer. Second layer, Dense consists of 64 units and ‘relu’ activation function. Output layer, Dense consists of 1 unit. Use mse as loss function. Use RMSprop as Optimizer. Use accuracy as metrics. Use 128 as batch size. Use 500 as epochs. Step 1 − Import the modules Let us import the necessary modules. import keras from keras.datasets import boston_housing from keras.models import Sequential from keras.layers import Dense from keras.optimizers import RMSprop from keras.callbacks import EarlyStopping from sklearn import preprocessing from sklearn.preprocessing import scale Step 2 − Load data Let us import the Boston housing dataset. (x_train, y_train), (x_test, y_test) = boston_housing.load_data() Here, boston_housing is a dataset provided by Keras. It represents a collection of housing information in Boston area, each having 13 features. Step 3 − Process the data Let us change the dataset according to our model, so that, we can feed into our model. The data can be changed using below code − x_train_scaled = preprocessing.scale(x_train) scaler = preprocessing.StandardScaler().fit(x_train) x_test_scaled = scaler.transform(x_test) Here, we have normalized the training data using sklearn.preprocessing.scale function. preprocessing.StandardScaler().fit function returns a scalar with the normalized mean and standard deviation of the training data, which we can apply to the test data using scalar.transform function. This will normalize the test data as well with the same setting as that of training data. Step 4 − Create the model Let us create the actual model. model = Sequential() model.add(Dense(64, kernel_initializer = ”normal”, activation = ”relu”, input_shape = (13,))) model.add(Dense(64, activation = ”relu”)) model.add(Dense(1)) Step 5 − Compile the model Let us compile the model using selected loss function, optimizer and metrics. model.compile( loss = ”mse”, optimizer = RMSprop(), metrics = [”mean_absolute_error”] ) Step 6 − Train the model Let us train the model using fit() method. history = model.fit( x_train_scaled, y_train, batch_size=128, epochs = 500, verbose = 1, validation_split = 0.2, callbacks = [EarlyStopping(monitor = ”val_loss”, patience = 20)] ) Here, we have used callback function, EarlyStopping. The purpose of this callback is to monitor the loss value during each epoch and compare it with previous epoch loss value to find the improvement in the training. If there is no improvement for the patience times, then the whole process will be stopped. Executing the application will give the below information as output − Train on 323 samples, validate on 81 samples Epoch 1/500 2019-09-24 01:07:03.889046: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not co mpiled to use: AVX2 323/323 [==============================] – 0s 515us/step – loss: 562.3129 – mean_absolute_error: 21.8575 – val_loss: 621.6523 – val_mean_absolute_erro r: 23.1730 Epoch 2/500 323/323 [==============================] – 0s 11us/step – loss: 545.1666 – mean_absolute_error: 21.4887 – val_loss: 605.1341 – val_mean_absolute_error : 22.8293 Epoch 3/500 323/323 [==============================] – 0s 12us/step – loss: 528.9944 – mean_absolute_error: 21.1328 – val_loss: 588.6594 – val_mean_absolute_error : 22.4799 Epoch 4/500 323/323 [==============================] – 0s 12us/step – loss: 512.2739 – mean_absolute_error: 20.7658 – val_loss: 570.3772 – val_mean_absolute_error : 22.0853 Epoch 5/500 323/323 [==============================] – 0s 9us/step – loss: 493.9775 – mean_absolute_error: 20.3506 – val_loss: 550.9548 – val_mean_absolute_error: 21.6547 ………. ………. ………. Epoch 143/500 323/323 [==============================] – 0s 15us/step – loss: 8.1004 – mean_absolute_error: 2.0002 – val_loss: 14.6286 – val_mean_absolute_error: 2. 5904 Epoch 144/500 323/323 [==============================] – 0s 19us/step – loss: 8.0300 – mean_absolute_error: 1.9683 – val_loss: 14.5949 – val_mean_absolute_error: 2. 5843 Epoch 145/500 323/323 [==============================] – 0s 12us/step – loss: 7.8704 – mean_absolute_error: 1.9313 – val_loss: 14.3770 – val_mean_absolute_error: 2. 4996 Step 7 − Evaluate the model Let us evaluate the model using test data. score = model.evaluate(x_test_scaled, y_test, verbose = 0) print(”Test loss:”, score[0]) print(”Test accuracy:”, score[1]) Executing the above code will output the below information − Test loss: 21.928471583946077 Test accuracy: 2.9599233234629914 Step 8 − Predict Finally, predict using test data as below − prediction = model.predict(x_test_scaled) print(prediction.flatten()) print(y_test) The output of the above application is as follows − [ 7.5612316 17.583357 21.09344 31.859276 25.055613 18.673872 26.600405 22.403967 19.060272 22.264952 17.4191 17.00466 15.58924 41.624374 20.220217 18.985565 26.419338 19.837091 19.946192 36.43445 12.278508 16.330965 20.701359 14.345301 21.741161 25.050423 31.046402 27.738455 9.959419 20.93039 20.069063 14.518344 33.20235 24.735163 18.7274 9.148898 15.781284 18.556862 18.692865 26.045074 27.954073 28.106823 15.272034 40.879818 29.33896 23.714525 26.427515 16.483374 22.518442 22.425386 33.94826 18.831465 13.2501955 15.537227 34.639984 27.468002 13.474407 48.134598 34.39617 22.8503124.042334 17.747198 14.7837715 18.187277 23.655672 22.364983 13.858193 22.710032 14.371148 7.1272087 35.960033 28.247292 25.3014 14.477208 25.306196 17.891165 20.193708 23.585173 34.690193 12.200583 20.102983 38.45882 14.741723 14.408362 17.67158 18.418497 21.151712 21.157492 22.693687 29.809034 19.366991 20.072294 25.880817 40.814568 34.64087 19.43741 36.2591 50.73806 26.968863 43.91787 32.54908 20.248306 ] [ 7.2 18.8 19. 27. 22.2 24.5 31.2 22.9 20.5 23.2 18.6 14.5 17.8 50. 20.8 24.3 24.2 19.8 19.1 22.7 12. 10.2 20. 18.5 20.9 23. 27.5 30.1 9.5 22. 21.2 14.1 33.1 23.4 20.1 7.4 15.4 23.8 20.1 24.5 33. 28.4 14.1 46.7 32.5 29.6 28.4 19.8 20.2 25. 35.4 20.3 9.7 14.5 34.9 26.6 7.2 50. 32.4 21.6 29.8 13.1 27.5 21.2 23.1 21.9 13. 23.2 8.1 5.6 21.7 29.6 19.6 7. 26.4 18.9 20.9 28.1 35.4 10.2 24.3 43.1 17.6 15.4 16.2 27.1 21.4 21.5 22.4 25. 16.6 18.6 22. 42.8 35.1 21.5 36. 21.9 24.1 50. 26.7 25. ] The output of both array have around 10-30% difference and it indicate our model predicts with reasonable range.

Learn Keras – Discussion work project make money

Discuss Keras Keras is an open source deep learning framework for python. It has been developed by an artificial intelligence researcher at Google named Francois Chollet. Leading organizations like Google, Square, Netflix, Huawei and Uber are currently using Keras. This tutorial walks through the installation of Keras, basics of deep learning, Keras models, Keras layers, Keras modules and finally conclude with some real-time applications.

Learn Keras – Model Compilation work project make money

Keras – Model Compilation Previously, we studied the basics of how to create model using Sequential and Functional API. This chapter explains about how to compile the model. The compilation is the final step in creating a model. Once the compilation is done, we can move on to training phase. Let us learn few concepts required to better understand the compilation process. Loss In machine learning, Loss function is used to find error or deviation in the learning process. Keras requires loss function during model compilation process. Keras provides quite a few loss function in the losses module and they are as follows − mean_squared_error mean_absolute_error mean_absolute_percentage_error mean_squared_logarithmic_error squared_hinge hinge categorical_hinge logcosh huber_loss categorical_crossentropy sparse_categorical_crossentropy binary_crossentropy kullback_leibler_divergence poisson cosine_proximity is_categorical_crossentropy All above loss function accepts two arguments − y_true − true labels as tensors y_pred − prediction with same shape as y_true Import the losses module before using loss function as specified below − from keras import losses Optimizer In machine learning, Optimization is an important process which optimize the input weights by comparing the prediction and the loss function. Keras provides quite a few optimizer as a module, optimizers and they are as follows: SGD − Stochastic gradient descent optimizer. keras.optimizers.SGD(learning_rate = 0.01, momentum = 0.0, nesterov = False) RMSprop − RMSProp optimizer. keras.optimizers.RMSprop(learning_rate = 0.001, rho = 0.9) Adagrad − Adagrad optimizer. keras.optimizers.Adagrad(learning_rate = 0.01) Adadelta − Adadelta optimizer. keras.optimizers.Adadelta(learning_rate = 1.0, rho = 0.95) Adam − Adam optimizer. keras.optimizers.Adam( learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, amsgrad = False ) Adamax − Adamax optimizer from Adam. keras.optimizers.Adamax(learning_rate = 0.002, beta_1 = 0.9, beta_2 = 0.999) Nadam − Nesterov Adam optimizer. keras.optimizers.Nadam(learning_rate = 0.002, beta_1 = 0.9, beta_2 = 0.999) Import the optimizers module before using optimizers as specified below − from keras import optimizers Metrics In machine learning, Metrics is used to evaluate the performance of your model. It is similar to loss function, but not used in training process. Keras provides quite a few metrics as a module, metrics and they are as follows accuracy binary_accuracy categorical_accuracy sparse_categorical_accuracy top_k_categorical_accuracy sparse_top_k_categorical_accuracy cosine_proximity clone_metric Similar to loss function, metrics also accepts below two arguments − y_true − true labels as tensors y_pred − prediction with same shape as y_true Import the metrics module before using metrics as specified below − from keras import metrics Compile the model Keras model provides a method, compile() to compile the model. The argument and default value of the compile() method is as follows compile( optimizer, loss = None, metrics = None, loss_weights = None, sample_weight_mode = None, weighted_metrics = None, target_tensors = None ) The important arguments are as follows − loss function Optimizer metrics A sample code to compile the mode is as follows − from keras import losses from keras import optimizers from keras import metrics model.compile(loss = ”mean_squared_error”, optimizer = ”sgd”, metrics = [metrics.categorical_accuracy]) where, loss function is set as mean_squared_error optimizer is set as sgd metrics is set as metrics.categorical_accuracy Model Training Models are trained by NumPy arrays using fit(). The main purpose of this fit function is used to evaluate your model on training. This can be also used for graphing model performance. It has the following syntax − model.fit(X, y, epochs = , batch_size = ) Here, X, y − It is a tuple to evaluate your data. epochs − no of times the model is needed to be evaluated during training. batch_size − training instances. Let us take a simple example of numpy random data to use this concept. Create data Let us create a random data using numpy for x and y with the help of below mentioned command − import numpy as np x_train = np.random.random((100,4,8)) y_train = np.random.random((100,10)) Now, create random validation data, x_val = np.random.random((100,4,8)) y_val = np.random.random((100,10)) Create model Let us create simple sequential model − from keras.models import Sequential model = Sequential() Add layers Create layers to add model − from keras.layers import LSTM, Dense # add a sequence of vectors of dimension 16 model.add(LSTM(16, return_sequences = True)) model.add(Dense(10, activation = ”softmax”)) compile model Now model is defined. You can compile using the below command − model.compile( loss = ”categorical_crossentropy”, optimizer = ”sgd”, metrics = [”accuracy”] ) Apply fit() Now we apply fit() function to train our data − model.fit(x_train, y_train, batch_size = 32, epochs = 5, validation_data = (x_val, y_val)) Create a Multi-Layer Perceptron ANN We have learned to create, compile and train the Keras models. Let us apply our learning and create a simple MPL based ANN. Dataset module Before creating a model, we need to choose a problem, need to collect the required data and convert the data to NumPy array. Once data is collected, we can prepare the model and train it by using the collected data. Data collection is one of the most difficult phase of machine learning. Keras provides a special module, datasets to download the online machine learning data for training purposes. It fetches the data from online server, process the data and return the data as training and test set. Let us check the data provided by Keras dataset module. The data available in the module are as follows, CIFAR10 small image classification CIFAR100 small image classification IMDB Movie reviews sentiment classification Reuters newswire topics classification MNIST database of handwritten digits Fashion-MNIST database of fashion articles Boston housing price regression dataset Let us use the MNIST database of handwritten digits (or minst) as our input. minst is a collection of 60,000, 28×28 grayscale images. It contains 10 digits. It also contains 10,000 test images. Below code can be used to load the dataset − from keras.datasets import mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() where Line 1 imports minst from the keras dataset module. Line 3 calls the load_data function, which will fetch the data from online server and return the data as 2 tuples, First tuple, (x_train, y_train) represent the training data with shape, (number_sample, 28, 28) and its digit label with shape, (number_samples, ). Second tuple,

Learn Keras – Pre-Trained Models work project make money

Keras – Pre-Trained Models In this chapter, we will learn about the pre-trained models in Keras. Let us begin with VGG16. VGG16 VGG16 is another pre-trained model. It is also trained using ImageNet. The syntax to load the model is as follows − keras.applications.vgg16.VGG16( include_top = True, weights = ”imagenet”, input_tensor = None, input_shape = None, pooling = None, classes = 1000 ) The default input size for this model is 224×224. MobileNetV2 MobileNetV2 is another pre-trained model. It is also trained uing ImageNet. The syntax to load the model is as follows − keras.applications.mobilenet_v2.MobileNetV2 ( input_shape = None, alpha = 1.0, include_top = True, weights = ”imagenet”, input_tensor = None, pooling = None, classes = 1000 ) Here, alpha controls the width of the network. If the value is below 1, decreases the number of filters in each layer. If the value is above 1, increases the number of filters in each layer. If alpha = 1, default number of filters from the paper are used at each layer. The default input size for this model is 224×224. InceptionResNetV2 InceptionResNetV2 is another pre-trained model. It is also trained using ImageNet. The syntax to load the model is as follows − keras.applications.inception_resnet_v2.InceptionResNetV2 ( include_top = True, weights = ”imagenet”, input_tensor = None, input_shape = None, pooling = None, classes = 1000) This model and can be built both with ‘channels_first’ data format (channels, height, width) or ‘channels_last’ data format (height, width, channels). The default input size for this model is 299×299. InceptionV3 InceptionV3 is another pre-trained model. It is also trained uing ImageNet. The syntax to load the model is as follows − keras.applications.inception_v3.InceptionV3 ( include_top = True, weights = ”imagenet”, input_tensor = None, input_shape = None, pooling = None, classes = 1000 ) Here, The default input size for this model is 299×299. Conclusion Keras is very simple, extensible and easy to implement neural network API, which can be used to build deep learning applications with high level abstraction. Keras is an optimal choice for deep leaning models.

Learn Keras – Real Time Prediction using ResNet Model work project make money

Real Time Prediction using ResNet Model ResNet is a pre-trained model. It is trained using ImageNet. ResNet model weights pre-trained on ImageNet. It has the following syntax − keras.applications.resnet.ResNet50 ( include_top = True, weights = ”imagenet”, input_tensor = None, input_shape = None, pooling = None, classes = 1000 ) Here, include_top refers the fully-connected layer at the top of the network. weights refer pre-training on ImageNet. input_tensor refers optional Keras tensor to use as image input for the model. input_shape refers optional shape tuple. The default input size for this model is 224×224. classes refer optional number of classes to classify images. Let us understand the model by writing a simple example − Step 1: import the modules Let us load the necessary modules as specified below − >>> import PIL >>> from keras.preprocessing.image import load_img >>> from keras.preprocessing.image import img_to_array >>> from keras.applications.imagenet_utils import decode_predictions >>> import matplotlib.pyplot as plt >>> import numpy as np >>> from keras.applications.resnet50 import ResNet50 >>> from keras.applications import resnet50 Step 2: Select an input Let us choose an input image, Lotus as specified below − >>> filename = ”banana.jpg” >>> ## load an image in PIL format >>> original = load_img(filename, target_size = (224, 224)) >>> print(”PIL image size”,original.size) PIL image size (224, 224) >>> plt.imshow(original) <matplotlib.image.AxesImage object at 0x1304756d8> >>> plt.show() Here, we have loaded an image (banana.jpg) and displayed it. Step 3: Convert images into NumPy array Let us convert our input, Banana into NumPy array, so that it can be passed into the model for the purpose of prediction. >>> #convert the PIL image to a numpy array >>> numpy_image = img_to_array(original) >>> plt.imshow(np.uint8(numpy_image)) <matplotlib.image.AxesImage object at 0x130475ac8> >>> print(”numpy array size”,numpy_image.shape) numpy array size (224, 224, 3) >>> # Convert the image / images into batch format >>> image_batch = np.expand_dims(numpy_image, axis = 0) >>> print(”image batch size”, image_batch.shape) image batch size (1, 224, 224, 3) >>> Step 4: Model prediction Let us feed our input into the model to get the predictions >>> prepare the image for the resnet50 model >>> >>> processed_image = resnet50.preprocess_input(image_batch.copy()) >>> # create resnet model >>>resnet_model = resnet50.ResNet50(weights = ”imagenet”) >>> Downloavding data from https://github.com/fchollet/deep-learning-models/releas es/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5 102858752/102853048 [==============================] – 33s 0us/step >>> # get the predicted probabilities for each class >>> predictions = resnet_model.predict(processed_image) >>> # convert the probabilities to class labels >>> label = decode_predictions(predictions) Downloading data from https://storage.googleapis.com/download.tensorflow.org/ data/imagenet_class_index.json 40960/35363 [==================================] – 0s 0us/step >>> print(label) Output [ [ (”n07753592”, ”banana”, 0.99229723), (”n03532672”, ”hook”, 0.0014551596), (”n03970156”, ”plunger”, 0.0010738898), (”n07753113”, ”fig”, 0.0009359837) , (”n03109150”, ”corkscrew”, 0.00028538404) ] ] Here, the model predicted the images as banana correctly.

Learn Keras – Quick Guide work project make money

Keras – Quick Guide Keras – Introduction Deep learning is one of the major subfield of machine learning framework. Machine learning is the study of design of algorithms, inspired from the model of human brain. Deep learning is becoming more popular in data science fields like robotics, artificial intelligence(AI), audio & video recognition and image recognition. Artificial neural network is the core of deep learning methodologies. Deep learning is supported by various libraries such as Theano, TensorFlow, Caffe, Mxnet etc., Keras is one of the most powerful and easy to use python library, which is built on top of popular deep learning libraries like TensorFlow, Theano, etc., for creating deep learning models. Overview of Keras Keras runs on top of open source machine libraries like TensorFlow, Theano or Cognitive Toolkit (CNTK). Theano is a python library used for fast numerical computation tasks. TensorFlow is the most famous symbolic math library used for creating neural networks and deep learning models. TensorFlow is very flexible and the primary benefit is distributed computing. CNTK is deep learning framework developed by Microsoft. It uses libraries such as Python, C#, C++ or standalone machine learning toolkits. Theano and TensorFlow are very powerful libraries but difficult to understand for creating neural networks. Keras is based on minimal structure that provides a clean and easy way to create deep learning models based on TensorFlow or Theano. Keras is designed to quickly define deep learning models. Well, Keras is an optimal choice for deep learning applications. Features Keras leverages various optimization techniques to make high level neural network API easier and more performant. It supports the following features − Consistent, simple and extensible API. Minimal structure – easy to achieve the result without any frills. It supports multiple platforms and backends. It is user friendly framework which runs on both CPU and GPU. Highly scalability of computation. Benefits Keras is highly powerful and dynamic framework and comes up with the following advantages − Larger community support. Easy to test. Keras neural networks are written in Python which makes things simpler. Keras supports both convolution and recurrent networks. Deep learning models are discrete components, so that, you can combine into many ways. Keras – Installation This chapter explains about how to install Keras on your machine. Before moving to installation, let us go through the basic requirements of Keras. Prerequisites You must satisfy the following requirements − Any kind of OS (Windows, Linux or Mac) Python version 3.5 or higher. Python Keras is python based neural network library so python must be installed on your machine. If python is properly installed on your machine, then open your terminal and type python, you could see the response similar as specified below, Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32 Type “help”, “copyright”, “credits” or “license” for more information. >>> As of now the latest version is ‘3.7.2’. If Python is not installed, then visit the official python link – and download the latest version based on your OS and install it immediately on your system. Keras Installation Steps Keras installation is quite easy. Follow below steps to properly install Keras on your system. Step 1: Create virtual environment Virtualenv is used to manage Python packages for different projects. This will be helpful to avoid breaking the packages installed in the other environments. So, it is always recommended to use a virtual environment while developing Python applications. Linux/Mac OS Linux or mac OS users, go to your project root directory and type the below command to create virtual environment, python3 -m venv kerasenv After executing the above command, “kerasenv” directory is created with bin,lib and include folders in your installation location. Windows Windows user can use the below command, py -m venv keras Step 2: Activate the environment This step will configure python and pip executables in your shell path. Linux/Mac OS Now we have created a virtual environment named “kerasvenv”. Move to the folder and type the below command, $ cd kerasvenv kerasvenv $ source bin/activate Windows Windows users move inside the “kerasenv” folder and type the below command, .envScriptsactivate Step 3: Python libraries Keras depends on the following python libraries. Numpy Pandas Scikit-learn Matplotlib Scipy Seaborn Hopefully, you have installed all the above libraries on your system. If these libraries are not installed, then use the below command to install one by one. numpy pip install numpy you could see the following response, Collecting numpy Downloading https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/ numpy-3.1.1-cp36-cp36m-macosx_10_6_intel. macosx_10_9_intel.macosx_10_9_x86_64. macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) |████████████████████████████████| 14.4MB 2.8MB/s pandas pip install pandas We could see the following response, Collecting pandas Downloading https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/ pandas-3.1.1-cp36-cp36m-macosx_10_6_intel. macosx_10_9_intel.macosx_10_9_x86_64. macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) |████████████████████████████████| 14.4MB 2.8MB/s matplotlib pip install matplotlib We could see the following response, Collecting matplotlib Downloading https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/ matplotlib-3.1.1-cp36-cp36m-macosx_10_6_intel. macosx_10_9_intel.macosx_10_9_x86_64. macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) |████████████████████████████████| 14.4MB 2.8MB/s scipy pip install scipy We could see the following response, Collecting scipy Downloading https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8 /scipy-3.1.1-cp36-cp36m-macosx_10_6_intel. macosx_10_9_intel.macosx_10_9_x86_64. macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) |████████████████████████████████| 14.4MB 2.8MB/s scikit-learn It is an open source machine learning library. It is used for classification, regression and clustering algorithms. Before moving to the installation, it requires the following − Python version 3.5 or higher NumPy version 1.11.0 or higher SciPy version 0.17.0 or higher joblib 0.11 or higher. Now, we install scikit-learn using the below command − pip install -U scikit-learn Seaborn Seaborn is an amazing library that allows you to easily visualize your data. Use the below command to install − pip install seaborn You could see the message similar as specified below − Collecting seaborn Downloading https://files.pythonhosted.org/packages/a8/76/220ba4420459d9c4c9c9587c6ce607bf56c25b3d3d2de62056efe482dadc /seaborn-0.9.0-py3-none-any.whl (208kB) 100% |████████████████████████████████| 215kB 4.0MB/s Requirement already satisfied: numpy> = 1.9.3 in ./lib/python3.7/site-packages (from seaborn) (1.17.0) Collecting pandas> = 0.15.2 (from seaborn) Downloading https://files.pythonhosted.org/packages/39/b7/441375a152f3f9929ff8bc2915218ff1a063a59d7137ae0546db616749f9/ pandas-0.25.0-cp37-cp37m-macosx_10_9_x86_64. macosx_10_10_x86_64.whl (10.1MB) 100% |████████████████████████████████| 10.1MB 1.8MB/s Requirement already satisfied: scipy>=0.14.0 in ./lib/python3.7/site-packages (from seaborn) (1.3.0) Collecting matplotlib> = 1.4.3 (from seaborn) Downloading https://files.pythonhosted.org/packages/c3/8b/af9e0984f 5c0df06d3fab0bf396eb09cbf05f8452de4e9502b182f59c33b/ matplotlib-3.1.1-cp37-cp37m-macosx_10_6_intel. macosx_10_9_intel.macosx_10_9_x86_64 .macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) 100% |████████████████████████████████| 14.4MB 1.4MB/s ……………………………….. ……………………………….. Successfully installed cycler-0.10.0 kiwisolver-1.1.0 matplotlib-3.1.1 pandas-0.25.0 pyparsing-2.4.2 python-dateutil-2.8.0 pytz-2019.2 seaborn-0.9.0 Keras Installation Using Python As of now, we have completed basic requirements for the installtion of Kera.