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

Who's Afraid Of Machine Learning? & first steps...

Who's Afraid Of Machine Learning? & first steps with TensorFlow

Chicago Roboto & Android Makers 2018

Britt Barak

April 23, 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 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 + 0.7
  4. 0.64 0.75 0.4 1.74 0.5 * 0.64 + 0.8 *

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

    pattern Top leaves 1.02 1.74 0.97 0.87 0.13
  6. 0.64 0.75 0.4 0.87 0.13 Strawberry Not Strawberry Output Input

    Red Seeds pattern Top leaves 1.02 1.74 0.97
  7. 0.7 0.03 0.01 3.72 0.89 1.92 Strawberry Not Strawberry Output

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

    Input Red Seeds pattern Top leaves 0.2 0.8
  9. 0.7 0.03 0.01 3.72 0.89 1.92 0.2 0.8 Strawberry Not

    Strawberry Output Input Red Seeds pattern Top leaves
  10. 0.5 * 0.64 + 0.8 * 0.75 + 0.3 *

    0.4 ___________ 1.04 + 0.7 ___________ 1.74 Strawberry Not Not Strawberry Not Not Strawberry Not Not
  11. 0.64 0.75 0.4 1.02 1.74 0.97 0.89 0.11 Strawberry Not

    Strawberry Output Input Red Seeds pattern Top leaves
  12. TensorFlow - Open source - Widely used - Flexible for

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

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

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

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

    FileInputStream(descriptor.getFileDescriptor()); FileChannel channel = inputStream.getChannel();

  17. 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); }
  18. labels.txt strawberry orange lemon fig pineapple banana jackfruit custard apple

    pomegranate hay carbonara chocolate sauce dough meat loaf
  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 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 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. 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)); } } }
  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]