<jupyter_start><jupyter_text>Image Demo 2Here we will collect a dataset from our cameras and use it to train an AI-agorithm to detect new objects.   run global setup<jupyter_code>try:
    with open("../global_setup.py") as setupfile:
        exec(setupfile.read())
except FileNotFoundError:
    print('Setup already completed')<jupyter_output><empty_output><jupyter_text>run local setup<jupyter_code>%%capture
import warnings

from notebooks.experiments.src.image_demos import TwoClassCameraDashboard, SimpleBinaryClassifier, ImageCollector
from src.real_time_plotting.keras_learning_curves import KerasLearningPlotter<jupyter_output><empty_output><jupyter_text>Image Demo 2 - Training an ML classifierIn this exercise you will collaborate with four other groups to design and create a classifier for a common object of your choice. The classifier should be able to detect whether the object is in a picture.You will go through the following basic steps of training an ML model:* Data preparation    * Obtain data    * Preprocess data    * Augment data* Data modelling    * Choose algorithm    * Train algorithm     * Evaluation    * Visualization of training    * Testing of edge cases    * Analyzing result ![ML framework](figures\ml_framework.PNG "ML framework")  Obtain data* Find three other groups around you that you will work together with* Agree on a common object that all of you have in your possession (e.g. a pen, glasses, a coffe cup)* Each of the groups takes ten pictures *with their object* and ten pictures *without the object*.     * Take care that part of your object is always in the red frame    * Use different backgrounds for the pictures and rotate your object<jupyter_code>%matplotlib qt5
data_dashboard = TwoClassCameraDashboard()
data_dashboard.start<jupyter_output><empty_output><jupyter_text>Train modelWe will now create the classifier. We will use a simple neural network consisting of one dense layer.<jupyter_code>%matplotlib inline
(x_train, y_train), (x_val, y_val) = data_dashboard.load_ml_data()
my_net = SimpleBinaryClassifier()
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    my_net.train((x_train, y_train), num_epochs=15, verbose=0, callbacks=[KerasLearningPlotter()])<jupyter_output><empty_output><jupyter_text>The two diagrams above show the accuracy of the neural network during the training as well as the average loss, i.e. how 'bad' the neural network did according to the self chosen quality metric.  Analyze model We will now test the neural network we just trained on the validation set.<jupyter_code>my_net.evaluate((x_val, y_val))<jupyter_output><empty_output><jupyter_text>Now we will use the neural network to classify a live video feed. Evaluate how your model does with varying lighting conditions and backgrounds previously not encountered. Do five tests of each condition and calculate the accuracy<jupyter_code>%matplotlib qt5
my_net.run_live(video_length=40)<jupyter_output><empty_output><jupyter_text>****double click here to edit cell*|  | Normal light | Darker | Brighter | Noisy background |  | --- | --- | --- |--- |--- |--- ||  correct | 0 | 0 |0 | 0|   What can you conclude on the stability of your algorithm when encountering conditions not in the dataset?  Augment dataWe can increase the number of training images for the classifier by using our knowledge about images. If an image is flipped or slightly rotated, it will most likely still display the same object but for our classifier look entirely different. This can be exploited by adding the flipped image and slightly rotated versions of the same image to the dataset dataset. In our case, this will increase the number of training images ten times while not increasing the amount of work we have to put in. This   is called *data augmentation*.- Run the cell below to see examples of your own images in the original and augmented version. Check if the images still    - look natural    - are different from your original image<jupyter_code>%matplotlib inline
data_dashboard.collector.show_augmented()<jupyter_output><empty_output><jupyter_text>Now repeat the training process while using augmented data. Do this by checking "Augmented" and clicking on the save button again. After that, repeat the network training process.- How does the accuracy of your network change? - Is the live-demo more or less accurate with data augmentation? Why is that?  Obtain more dataNow you will collaborate with the other groups.- Exchange your object with one of the other groups. Compare the accuracy of your network on your own object vs. the other groups object by running the cells below and taking images of your own and the other object. - Do you obtain better accuracy on your own object or on the other group's object?- What can you conclude on the importance of variety while collecting data?<jupyter_code>%matplotlib qt5
compare_pics = ImageCollector(num_pictures=5)
compare_pics.run_collector()
%matplotlib inline
compare_pics.show_images(net = my_net.model)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.05974593092497023, 'fraction_numerical': 0.0029773719730051607, 'mean_word_length': 5.1601466992665035, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '174297', 'license': 'Apache-2.0', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1306, 'n_words': 620, 'path': 'notebooks/experiments/Image Demo 2 - Training an ML classifier.ipynb', 'repo_name': 'fronovics/AI_playground', 'size': '5038'}