AI (Artificial Intelligence, aka AI) with Python, tensorflow and keras
Even without in-depth knowledge of 2-D and 3-D graphics processing, Python - thanks to the tensorflow module provided by the American defense company google - can be used to implement artificial intelligence applications.
The aim of the script solution presented here: A neural network is set up and trained with the images from a free image database. The database chosen by cifar10 includes images from the categories "Airplane", "Car", "Bird", "Cat", "Deer", "Dog", "Frog", "Horse", "Ship" and "Truck" (unfortunally in grman language...). After the training, the network is presented with its own pictures, in which the NeuroNetz should identify known objects (according to the features learned).
First, the inventory of an image database is loaded via cifar10 and the database may be reduced:
# fetch cifar10-images: (training_images, training_label) , (testing_images, testing_label) = datasets.cifar10.load_data() #training_images = training_images[:20000] #training_label = training_label[:20000] #testing_images = testing_images[:2000] #testing_label = testing_label[:2000]
(if the last four lines are activated, the stock of image files is limited to 20,000 training images and 2,000 test images)
Then the neural network is built up (model = models.Sequential ()). Then the cascaded filters, which are connected one after the other, follow, starting with a convolutional layer that holds the 32 * 32-pixel images up to the final dense -Layer with 10 neurons and the Softmax function.
# Sequential Neural Network: model = models.Sequential() # And now, the cascades filters of our network, starting with a Convolutional Layer # relu = rectified linear unit, or: f(x) = max(0, x) # Convolutional Layer consisting of 32 filters with 3x3 matrixes, shape of 32x32-images with RGB-colors: model.add(layers.Conv2D(32, (3,3), activation = 'relu', input_shape = (32, 32, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3,3), activation = 'relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3,3), activation = 'relu')) model.add(layers.Flatten()) model.add(layers.Dense(64, activation = 'relu')) # 64 neurons model.add(layers.Dense(10, activation = 'softmax')) # 10 neurons, softmax gives procentual values
After the network has been defined, it is compiled and then trained. For the training, the image material is 'epochs' presented to the network.
# compile with Adam Optimizer and use 'Sparse Categorical Crossentropy' as Loss function: model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy']) # apply data 22 times at the model (epochs) to improve accurancy: model.fit(training_images, training_label, epochs = 22, validation_data = (testing_images, testing_label))
On stderr you can follow the training and see the increase in accuracy.
compile, train and test neuro network Epoch 1/22 1563/1563 [==============================] - 30s 19ms/step - loss: 1.5239 - accuracy: 0.4438 - val_accuracy: 0.5467 - val_loss: 1.2610 Epoch 2/22 1563/1563 [==============================] - 30s 19ms/step - loss: 1.1611 - accuracy: 0.5895 - val_accuracy: 0.6284 - val_loss: 1.0483 Epoch 3/22 1563/1563 [==============================] - 29s 19ms/step - loss: 1.0166 - accuracy: 0.6454 - val_accuracy: 0.6305 - val_loss: 1.0500 Epoch 4/22 1563/1563 [==============================] - 30s 19ms/step - loss: 0.9271 - accuracy: 0.6753 - val_accuracy: 0.6726 - val_loss: 0.9469 Epoch 5/22 1563/1563 [==============================] - 23s 15ms/step - loss: 0.8552 - accuracy: 0.6980 - val_accuracy: 0.6738 - val_loss: 0.9305 Epoch 6/22 1563/1563 [==============================] - 23s 15ms/step - loss: 0.7972 - accuracy: 0.7192 - val_accuracy: 0.6831 - val_loss: 0.9180 Epoch 7/22 1563/1563 [==============================] - 26s 17ms/step - loss: 0.7475 - accuracy: 0.7378 - val_accuracy: 0.7073 - val_loss: 0.8500 Epoch 8/22 1563/1563 [==============================] - 23s 15ms/step - loss: 0.7063 - accuracy: 0.7513 - val_accuracy: 0.6963 - val_loss: 0.8900 Epoch 9/22 1563/1563 [==============================] - 23s 14ms/step - loss: 0.6685 - accuracy: 0.7635 - val_accuracy: 0.6813 - val_loss: 0.9468 Epoch 10/22 1563/1563 [==============================] - 23s 14ms/step - loss: 0.6301 - accuracy: 0.7756 - val_accuracy: 0.7074 - val_loss: 0.8841 Epoch 11/22 1563/1563 [==============================] - 23s 15ms/step - loss: 0.5977 - accuracy: 0.7878 - val_accuracy: 0.7162 - val_loss: 0.8579
It is also interesting to play with both the epochs value and the number of images used. The functional impact is more important than the impact on the runtime. For example, the result with epochs = 1 (accuracy = 0.44) - a pony (horse) likes to become a bird.
Finally, n own pictures are presented to the network, the prediction received indicating which class the own picture was assigned to.
The pictures can only be seen on www.thomas-boor.de/ki_tensorflow.html
# let's detect own photos by the neuro network: for prefix in ['petra_and_pops', 'pops_front', 'sunny']: pic = prefix + '_32_32.jpg' print("operating %s" % (pic)) image = cv.imread(pic) image = cv.cvtColor(image, cv.COLOR_BGR2RGB) #plt.imshow(image, cmap = plt.cm.binary) #plt.show() prediction = model.predict(np.array([image]) / 255) index = np.argmax(prediction) print(classes[index])
The results on the screen (stdout):
operating petra_and_pops_32_32.jpg Horse operating pops_front_32_32.jpg Horse operating sunny_32_32.jpg Horse done
(The first picture only shows the horse, because the class 'human' was not included in the learned database.)
Anyone who owns a kindle will find the free eBook "Python for neural networks; the quick start" by Florian Dedov in the kindle store, which offers a variety of materials.
Before the script can run, you may have to install various Python packages. The sequence is available for TensorFlow
- pip install -U pip
- pip install -U setuptools
- pip3 install TensorFlow or: pip3 install tf-nightly
at. tf-nightly is the most current version and is useful if new functions (as shown here) are required.
#!/usr/bin/env python3 # # t1.py # # to install TensorFlow: # pip install -U pip # pip install -U setuptools # # pip3 install tf-nightly # import cv2 as cv import numpy as np import matplotlib.pyplot as plt from tensorflow.keras import datasets, layers, models # fetch cifar10-images: (training_images, training_label) , (testing_images, testing_label) = datasets.cifar10.load_data() # uncopmment lines below to improve speed by q-loss: #training_images = training_images[:20000] #training_label = training_label[:20000] #testing_images = testing_images[:2000] #testing_label = testing_label[:2000] # normalize colors to a range from 0.0 to 1.0: training_images, testing_images = training_images / 255.0, testing_images / 255.0 # names of the categories provided by the cifar10-fetch: classes = ['Flugzeug', 'Auto', 'Vogel', 'Katze', 'Reh', 'Hund', 'Frosch', 'Pferd', 'Schiff', 'LKW'] # display images of the classes: #for i in range(16): # plt.subplot(4, 4, i + 1) # plt.xticks([]) # plt.yticks([]) # plt.imshow(training_images[i], cmap=plt.cm.binary) # plt.xlabel(classes[training_label[i][0]]) # #plt.show() print("build up neuro network") # Sequential Neural Network: model = models.Sequential() # And now, the cascades filters of our network, starting with a Convolutional Layer # relu = rectified linear unit, or: f(x) = max(0, x) # Convolutional Layer consisting of 32 filters with 3x3 matrixes, shape of 32x32-images with RGB-colors: model.add(layers.Conv2D(32, (3,3), activation = 'relu', input_shape = (32, 32, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3,3), activation = 'relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3,3), activation = 'relu')) model.add(layers.Flatten()) model.add(layers.Dense(64, activation = 'relu')) # 64 neurons model.add(layers.Dense(10, activation = 'softmax')) # 10 neurons, softmax gives procentual values print("compile, train and test neuro network") # compile with Adam Optimizer and use 'Sparse Categorical Crossentropy' as Loss function: model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy']) # apply data 22 times at the model (epochs) to improve accurancy: model.fit(training_images, training_label, epochs = 22, validation_data = (testing_images, testing_label)) testing_loss, testing_acc = model.evaluate(testing_images, testing_label, verbose = 2) # let's detect own photos by the neuro network: for prefix in ['petra_and_pops', 'pops_front', 'sunny']: pic = prefix + '_32_32.jpg' print("operating %s" % (pic)) image = cv.imread(pic) image = cv.cvtColor(image, cv.COLOR_BGR2RGB) #plt.imshow(image, cmap = plt.cm.binary) #plt.show() prediction = model.predict(np.array([image]) / 255) index = np.argmax(prediction) print(classes[index]) print("done")