Skip to content

Files

Latest commit

Jan 10, 2022
2242394 · Jan 10, 2022

History

History
executable file
·
454 lines (371 loc) · 26 KB

13_Keras_and_TensorFlow_how-tos.md

File metadata and controls

executable file
·
454 lines (371 loc) · 26 KB

Helpful for me and I hope will be helpful for you:


All video courses and tutorials about Machine Learning I'm working on are placed here.


All code examples are placed here.


Additional useful tools everybody should know about:

  • Jupyter Notebook:

    • Six easy ways to run your Jupyter Notebook in the cloud: Binder, Kaggle Kernels, Google Colaboratory (Colab), Microsoft Azure Notebooks, CoCalc, Datalore
    • Google Colab — Colaboratory is a free Jupyter notebook environment that requires no setup and runs entirely in the cloud. With Colaboratory you can write and execute code, save and share your analyses, and access powerful computing resources, all for free from your browser.
    • Kaggle Kernels — Free Jupyter notebook environment. If you tired to read documentation, just try to find and see some videos.
    • K3D Jupyter — Jupyter notebook extension for 3D visualization with examples. Just start to view the examples and you'll like it :-)
    • nbviewer — A simple way to share Jupyter Notebooks. It is useful if your Jupyter Notebook *.ipynb won't load on Github. Enter the location of a Jupyter Notebook to have it rendered.
    • 10 Simple hacks to speed up your Data Analysis in Python
    • Use Jupyter Notebook with RISE plug-in to make presentations right in the notebook.
    • Juxtapose — Easy-to-make frame comparisons. Useful for presentations. Generate an embedable code snippet that you can use on any website or Jupyter notebook. There must be an Internet connection to obtain URL. Copy-paste code below into your Jupyter notebook and replace the url:
# Display the associated webpage in a new window
import IPython
url = 'https://cdn.knightlab.com/libs/juxtapose/latest/embed/index.html?uid=7e8015a0-4be7-11e9-8106-0edaf8f81e27'
iframe = '<iframe frameborder="0" class="juxtapose" width="100%" height="600" src="' + url + '"></iframe>'
IPython.display.HTML(iframe)
  • Downloading tools:

  • Datasets Search:

  • Deep Learning add-on for Adaptive Vision Studio Lite — If you need out-of-the-box solutions, you could try Adaptive Vision tool with Deep Learning add-on. Unfortunately, Deep Learning add-on has only trial version for 30 days for educational purposes. Lite version is a limited-functionality freeware available for trial and non-commercial use. Recommended especially for students who want to learn how to develop complex algorithms with images loaded from files.

  • Annotation tools:

    • Annotation tool labelImg. This annotation tool supports both Windows and Linux, it will generate xml files inside image directories. Tip: use shortcuts (w: draw box, d: next file, a: previous file, etc.) to accelerate the annotation.
    • My own annotation tools: Manual image annotation with polygons and Manual image annotation with rectangles can annotate and cut rectangular images.
    • VGG Image Annotator is a simple and standalone manual annotation software for image, audio and video. VIA runs in a web browser and does not require any installation or setup. The complete VIA software fits in a single self-contained HTML page of size less than 400 Kilobyte that runs as an offline application in most modern web browsers.
    • Computer Vision Annotation Tool (CVAT). CVAT is free, online, interactive video and image annotation tool for computer vision.
    • https://supervise.ly The leading platform for entire computer vision lifecycle.
  • Model Zoo:

    • TensorFlow Hub is a library for the publication, discovery, and consumption of reusable parts of machine learning models. A module is a self-contained piece of a TensorFlow graph, along with its weights and assets, that can be reused across different tasks in a process known as transfer learning. If you think of a model like a binary, think of a module like a library (video).
    • mmdetection is an open source object detection toolbox based on PyTorch. Plus model zoo.
    • Detectron is Facebook AI Research's software system that implements state-of-the-art object detection algorithms. It is written in Python and powered by the Caffe2 deep learning framework.
    • Model Zoo website curates and provides a platform for deep learning researchers to easily find pre-trained models for a variety of platforms and uses.
  • Data visualization:

  • OpenAI builds free software for training, benchmarking, and experimenting with AI. Reinforcement Learning (RL).

  • Resize multiple images with ratio resize_with_ratio.py script.

  • Kite autocomplete plugin for Python IDE (PyCharm, Spyder, etc.).


Web sites for ML/DL:

Video channels to explain scientific publications in ML/DL:

Newsletters:


For iOS review this blog of Matthijs Hollemans.

TensorFlow Lite guide — this is your starting point.

Video course Introduction to TensorFlow Lite.

TensorFlow Lite vs TensorFlow Mobile (link):

  • Lite is the next version of Mobile. Generally, applications developed on Lite will have better performance and less binary file size than Mobile.
  • Lite is still in early stages so not all the cases cover which is not the case for Mobile.
  • Lite supports selective sets of operators, therefore not all the models will work on Lite by default. Whereas, Mobile has fully covered functionality.

I'm not familiar with Android development. So start with Android quickstart and Android image classification example:

  • Download and install Android Studio.
  • Set device in developer mode with USB debugging enabled. Enable developer mode and USB debugging on Xiaomi Redmi or Samsung Galaxy Tab. Google it to set developer and debugging on other phone models.
  • Take USB cable (to connect Android device to your computer).

Configuration steps for MIUI 9 and above: Settings → Additional Settings → Developer options →

  1. Turn off Turn on MIUI optimization at the very bottom of Developer options and Restart the phone.
  2. Turn On USB Debugging.
  3. Turn On Install via USB.
  4. Set USB Configuration to Charging. MTP(Media Transfer Protocol) is the default mode. Works even in MTP in some cases.

After finishing previous example run object detection example using a quantized MobileNet SSD model trained on the COCO dataset.

Result for image classification demo is decent (2019.09.13). I don't know which dataset they used, but the quality of classification is decent.

TF image classification

Result for object detection is poor (2019.09.13, COCO dataset). It is not for production.

TF object detection

For further Android development examples and TensorFlow Lite, visit this repository.


Links to read:


By default TensorFlow occupies all GPUs on the platform. Also you should exit Python (ipython, jupyter) console to free GPU resources.

# Calculate on the 2nd GPU
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"  # so the IDs match nvidia-smi
os.environ["CUDA_VISIBLE_DEVICES"] = "1"        # "0,1" for multiple GPU or "-1" for CPU

# Note: it seems option "0,1" does not work for different GPU models.
# For Jupyter Notebook one has to restart the kernel
# after changing of environment variables.

# Monitor GPU permanently. To exit, press <Ctrl>+<C> keys.
watch -n 0.5 nvidia-smi
# Monitor CPU permanently. To exit, press <Q> key.
htop

By default TensorFlow occupies all memory on GPU. Limiting GPU memory growth.

You can dynamically reserve only necessary, but not all available memory:

# Reserve necessary GPU memory
import tensorflow as tf

# For TensorFlow 2.x
gpus = tf.config.list_physical_devices('GPU')
if gpus:
    try:
        # Currently, memory growth needs to be the same across GPUs
        for gpu in gpus:
            tf.config.experimental.set_memory_growth(gpu, True)
        logical_gpus = tf.config.list_logical_devices('GPU')
        print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
    except RuntimeError as e:
        # Memory growth must be set before GPUs have been initialized
        print(e)

You can set the fraction of GPU memory to be allocated when you construct a tf.Session by passing a tf.GPUOptions as part of the optional config argument:

# Reserve GPU memory fraction
import tensorflow as tf

# For TensorFlow 2.x
gpus = tf.config.list_physical_devices('GPU')
if gpus:
    # Restrict TensorFlow to only allocate 1GB of memory on the first GPU
    try:
        tf.config.set_logical_device_configuration(
            gpus[0],
            [tf.config.LogicalDeviceConfiguration(memory_limit=1024)])
        logical_gpus = tf.config.list_logical_devices('GPU')
        print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
    except RuntimeError as e:
        # Virtual devices must be set before GPUs have been initialized
        print(e)

The per_process_gpu_memory_fraction acts as a hard upper bound on the amount of GPU memory that will be used by the process on each GPU on the same machine. Currently, this fraction is applied uniformly to all of the GPUs on the same machine


import os
import tensorflow as tf

# 0 - all messages are logged (default behavior)
# 1 - INFO messages are not printed
# 2 - INFO and WARNING messages are not printed
# 3 - INFO, WARNING, and ERROR messages are not printed
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # show errors

After calculations everyone should clean up resources and exit from Python, iPython and Jupyter. Place this code at the end of your program:

# Clean up resources. Place this code at the end of the program.
import os, signal
os.kill(os.getpid(), signal.SIGTERM)  # you can use signal.SIGKILL for Linux, but not for Windows

Root can forcibly clean up resources:

# Shows PIDs of processes
# using the specified files or file systems
sudo fuser -v /dev/nvidia*

                     USER         PID ACCESS COMMAND
/dev/nvidia0:        root        4051 F...m Xorg
                     username1   8138 F...m python
                     username2  11791 F.... python3
                     username3  14282 F...m python3
                     username3  14295 F...m python3
/dev/nvidia1:        root        4051 F...m Xorg
                     username1   8138 F...m python
                     username2  11791 F...m python3
/dev/nvidiactl:      root        4051 F...m Xorg
                     username1   8138 F...m python
                     username2  11791 F...m python3
                     username3  14282 F...m python3
                     username3  14295 F...m python3
/dev/nvidia-modeset: root        4051 F.... Xorg
/dev/nvidia-uvm:     username1   8138 F...m python
                     username2  11791 F.... python3
                     username3  14282 F...m python3
                     username3  14295 F...m python3

# Finish 2 processes for nvidia0
sudo kill -9 14282
sudo kill -9 14295

sudo fuser -v /dev/nvidia*
nvidia-smi  # show nvidia system management interface