Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Who's Afraid Of Machine Learning?

Who's Afraid Of Machine Learning?

Devfest Pisa 2018

Intro to Machine Learning and the crazy stuff everyone's now taking about.. and a bit of TensorFlow Lite for Android.

Britt Barak

March 10, 2018
Tweet

More Decks by Britt Barak

Other Decks in Technology

Transcript

  1. 0.64 0.75 0.4 Input Red Seeds pattern Top leaves 0.5

    0.8 0.3 0.5 * 0.64 + 0.8 * 0.75 + 0.3 * 0.4
  2. 0.64 0.75 0.4 Input Red Seeds pattern Top leaves 0.5

    0.8 0.3 0.5 * 0.64 + 0.8 * 0.75 + 0.3 * 0.4 ___________ 1.04
  3. 0.64 0.75 0.4 Red Seeds pattern Top leaves 0.5 0.8

    0.3 0.5 * 0.64 + 0.8 * 0.75 + 0.3 * 0.4 ___________ 1.04 + 0.7 ___________ Input
  4. 0.64 0.75 0.4 Red Seeds pattern Top leaves 1.74 0.5

    0.8 0.3 0.5 * 0.64 + 0.8 * 0.75 + 0.3 * 0.4 ___________ 1.04 + 0.7 ___________ 1.74 Input
  5. 0.64 0.75 0.4 Red Seeds pattern Top leaves 1.02 1.74

    0.97 Output Strawberry Not Strawberry Input
  6. 0.64 0.75 0.4 Red Seeds pattern Top leaves 1.02 1.74

    0.97 0.87 0.13 Strawberry Not Strawberry Output Input
  7. 0.7 0.03 0.01 Red Seeds pattern Top leaves 3.72 0.89

    1.92 Strawberry Not Strawberry Output Input
  8. 0.7 0.03 0.01 Red Seeds pattern Top leaves 3.72 0.89

    1.92 0.2 0.8 Strawberry Not Strawberry Output Input
  9. Strawberry Not Not Strawberry Not Not Strawberry Not Not 0.5

    * 0.64 + 0.8 * 0.75 + 0.3 * 0.4 ___________ 1.04 + 0.7 ___________ 1.74
  10. 0.64 0.75 0.4 Red Seeds pattern Top leaves 1.02 1.74

    0.97 0.89 0.11 Strawberry Not Strawberry Output Input
  11. TensorFlow - Open source - Widely used - Flexible for

    scale: - 1 or more CPUs / GPUs - desktop, server, mobile device
  12. TensorFlow Mobile - Speech Recognition - Image Recognition - Object

    Localization - Gesture Recognition - Translation - Text Classification - Voice Synthesis
  13. labels.txt strawberry orange lemon fig pineapple banana jackfruit custard apple

    pomegranate hay carbonara chocolate sauce dough meat loaf pizza
  14. repositories { maven { url 'https://google.bintray.com/tensorflow' } } dependencies {

    // ... implementation 'org.tensorflow:tensorflow-lite:+' } build.gradle
  15. MappedByteBuffer loadModelFile() { AssetFileDescriptor descriptor= getAssets().openFd(MODEL_PATH); FileInputStream inputStream = new

    FileInputStream(descriptor.getFileDescriptor()); FileChannel channel = inputStream.getChannel(); }
  16. MappedByteBuffer loadModelFile() { AssetFileDescriptor descriptor= getAssets().openFd(MODEL_PATH); FileInputStream inputStream = new

    FileInputStream(descriptor.getFileDescriptor()); FileChannel channel = inputStream.getChannel(); long start = descriptor.getStartOffset(); long length = descriptor.getDeclaredLength(); return channel.map(FileChannel.MapMode.READ_ONLY, start, length); }
  17. labels.txt strawberry orange lemon fig pineapple banana jackfruit custard apple

    pomegranate hay carbonara chocolate sauce dough meat loaf pizza
  18. List<String> loadLabelList() throws IOException { ` InputStreamReader inputStream = new

    InputStreamReader(getAssets().open(LABEL_PATH)); BufferedReader reader = new BufferedReader(inputStream); }
  19. List<String> loadLabelList() throws IOException { ` InputStreamReader inputStream = new

    InputStreamReader(getAssets().open(LABEL_PATH)); BufferedReader reader = new BufferedReader(inputStream); List<String> labelList = new ArrayList<String>(); String line; while ((line = reader.readLine()) != null) { labelList.add(line); } }
  20. List<String> loadLabelList() throws IOException { ` InputStreamReader inputStream = new

    InputStreamReader(getAssets().open(LABEL_PATH)); BufferedReader reader = new BufferedReader(inputStream); List<String> labelList = new ArrayList<String>(); String line; while ((line = reader.readLine()) != null) { labelList.add(line); } reader.close(); return labelList; }
  21. Image Classifier [ [0..6] , [ 0.1 ] , ...

    ] [strawberry, apple, ... ] probArray labels.txt
  22. probArray = { [0.7], [0.3], [0], [0], } labelList =

    { strawberry, apple, pineapple, banana } 0.3
  23. ImageClassifier.java model = loadModelFile(); tflite = new Interpreter(model); labelList =

    loadLabelList(); probArray = new float[1][labelList.size()];
  24. Image Classifier [......] [ [0..6] , [ 0.1 ] ,

    ... ] [strawberry, apple, ... ] ByteBuffer probArray labels.txt
  25. ImageClassifier.java model = loadModelFile(); tflite = new Interpreter(model); labelList =

    loadLabelList(); probArray = new float[1][labelList.size()]; imgData = ByteBuffer.allocateDirect( DIM_IMG_SIZE_X * DIM_IMG_SIZE_Y * DIM_PIXEL_SIZE); imgData.order(ByteOrder.nativeOrder());
  26. classifier .classify(bitmap) Image Classifier [......] [ [0..6] , [ 0.1

    ] , ... ] [strawberry, apple, ... ] ByteBuffer probArray labels.txt
  27. void convertBitmapToByteBuffer(Bitmap bitmap) { //... bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0,bitmap.getWidth(),

    bitmap.getHeight()); int pixel = 0; for (int i = 0; i < DIM_IMG_SIZE_X; ++i) { for (int j = 0; j < DIM_IMG_SIZE_Y; ++j) { final int val = intValues[pixel++]; imgData.put((byte) ((val >> 16) & 0xFF)); imgData.put((byte) ((val >> 8) & 0xFF)); imgData.put((byte) (val & 0xFF)); } } }
  28. private String getTopKLabels() { for (int i = 0; i

    < labelList.size(); ++i) { sortedLabels.add( new AbstractMap.SimpleEntry<>(labelList.get(i), (labelProbArray[0][i] & 0xff) / 255.0f)); if (sortedLabels.size() > RESULTS_TO_SHOW) { sortedLabels.poll(); } } String textToShow = ""; final int size = sortedLabels.size(); for (int i = 0; i < size; ++i) { Map.Entry<String, Float> label = sortedLabels.poll(); textToShow = String.format("\n%s: %4.2f", label.getKey(), label.getValue()) + textToShow; } return textToShow; }
  29. Links - Tensorflow - https://www.tensorflow.org/ - Tensorflow lite - https://www.tensorflow.org/mobile/tflite/

    - Codes labs - codelabs.developers.google.com/codelabs/tensorflow-for-poets-2-tflite/ - Google’s Machine Learning Crash Course - developers.google.com/machine-learning/crash-course/ - [Dr. Joe Dispenza]