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

【再開催】Raspberry PiとUSBカメラではじめようディープラーニング

Yamada3
August 23, 2019

【再開催】Raspberry PiとUSBカメラではじめようディープラーニング

ももち浜TECHカフェ
「【再開催】Raspberry PiとUSBカメラではじめようディープラーニング」

ももち浜TECHカフェは福岡市ももち浜から発信する、IoT・AR・VRなどをテーマにしたハンズオン企画です。
今回のテーマはRaspberry Pi & 機械学習・ディープラーニング。追加開催版です。

Raspberry Piはイギリスのラズベリーパイ財団が開発しているシングルボードタイプのコンピュータです。安価で小型なためIoT機器への組込み・プロトタイピング等で日本でも趣味・業務問わず広く普及が進んできています。

今回は、そんなRaspberry PiとUSBカメラを使って、機械学習やディープラーニングの初歩を学んでいただけます。

Yamada3

August 23, 2019
Tweet

More Decks by Yamada3

Other Decks in Technology

Transcript

  1. ຊ೔࢖༻͢Δ3BTQCFSSZ1J.PEFM#͸ ɹ"3.$PSUFY"ʢίΞɺ()[ʣ ɹ #SPBEDPN#$.  ετϨʔδɹNJDSP4% ɹ64#Y )%.*º -"/#"4&5 ɹΦʔσΟΦɹNNεςϨΦ

    ɹిݯɹ7ʗ"ʢ64#NJDSP#ʣ ɹେ͖͞ɹºNN ɹ8J'J͸()[ଳͷΈ࢖༻Ͱ͖·͢ɻ 3BTQCFSSZ1JͷجૅΛֶͿ 3BTQCFSSZ1J̏.PEFM#ͱ͸ ˞3BTQCFSSZ1J͸օ͞Μ͕࢖͍ͬͯΔ1$ΑΓ൓Ԡ଎౓͕ͱͯ΋஗͍Ͱ͢ɻ ɹʢ͙͢൓Ԡ͠ͳ͍͔ΒͱԿ౓΋ΫϦοΫͨ͠ΓίϚϯυΛ࠶౓ೖྗ͠ͳ͍ʣ
  2. ȊQZͷΠϯετʔϧखॱ IQZ ΤΠνϑΝΠϒύΠ ͷΠϯετʔϧ QZUIPOͰ )%' )JFSBSDIJDBM %BUB'PSNBU ܗࣜΛ ѻ͑ΔΑ͏ʹ͠·͢ɻ

    TVEPBQUHFUJOTUBMMQZUIPOIQZ ్தͰΠϯετʔϧ͕ࢭ·ͬͯ ଓߦ͠·͔͢ʁ<:O> ͱฉ͔Ε·͢ͷͰ :Λೖྗޙ&OUFSͯ͠ ଓ͚͍ͯͩ͘͞ɻ ˞LFSBTͳͲͷχϡʔϥϧωοτϫʔΫϥΠϒϥϦͷֶश݁ՌΛอଘ͢ΔϑΥʔϚοτ ˞ ஫ҙɿ طʹΠϯετʔϧࡁΈͰ͢ ίϚϯυΛೖྗ͢Δඞཁ͸͋ Γ·ͤΜɻ
  3. from keras.datasets import mnist import matplotlib.pyplot as plt (X_train, y_train),

    (X_test, y_test) = mnist.load_data() print("label",y_train[0]) plt.imshow(X_train[0].reshape(28,28),cmap='Greys') plt.show() खॻ͖਺ࣈͷσʔληοτ./*45Λ࢖͓͏
  4. from keras.datasets import mnist import matplotlib.pyplot as plt (X_train, y_train),

    (X_test, y_test) = mnist.load_data() print("label",y_train[0]) plt.imshow(X_train[0].reshape(28,28),cmap='Greys') plt.show() खॻ͖਺ࣈͷσʔληοτ./*45Λ࢖͓͏ LFSBTʹ༻ҙ͞Εͯ ͍Δ./*45ΛΠϯ ϙʔτͯ͠ɺը૾Λ දࣔͰ͖ΔΑ͏ʹ QMUͱઃఆ͢Δ
  5. from keras.datasets import mnist import matplotlib.pyplot as plt (X_train, y_train),

    (X_test, y_test) = mnist.load_data() print("label",y_train[0]) plt.imshow(X_train[0].reshape(28,28),cmap='Greys') plt.show() खॻ͖਺ࣈͷσʔληοτ./*45Λ࢖͓͏ NOJTUΛಡΈࠐΉ
  6. from keras.datasets import mnist import matplotlib.pyplot as plt (X_train, y_train),

    (X_test, y_test) = mnist.load_data() print("label",y_train[0]) plt.imshow(X_train[0].reshape(28,28),cmap='Greys') plt.show() खॻ͖਺ࣈͷσʔληοτ./*45Λ࢖͓͏ NOJTUͷ࠷ॳͷσʔ λͷϥϕϧͱը૾ Λग़ྗ
  7. 5IPOOZ 1ZUIPO*%&1ZUIPOͷॳ৺ऀ༻౷߹؀ڥ Λ࢖͏ 3VOϘλϯΛԡ࣮ͯ͠ߦͤ͞Α͏ʂ खॻ͖਺ࣈͷσʔληοτ./*45 &Run mnist1.py Using TensorFlow backend.

    WARNING: Logging before flag parsing goes to stderr. W0823 09:50:29.745067 1995954896 deprecation_wrapper.py:118] From /home/pi/.local/lib/python3.7/site- packages/tensorflow/__init__.py:98: The name tf.AUTO_REUSE is deprecated. Please use tf.compat.v1.AUTO_REUSE instead. WARNING͕͍͔ͭ͘ग़·͕͢ ໰୊ͳ͍ͷͰ͠͹Β͘଴ͪ·͢
  8. from keras.datasets import mnist import matplotlib.pyplot as plt (X_train, y_train),

    (X_test, y_test) = mnist.load_data() print("label",y_train[0]) plt.imshow(X_train[0].reshape(28,28),cmap='Greys') plt.show() खॻ͖਺ࣈͷσʔληοτ./*45Λ࢖͓͏ ×Ͱด͡Δ
  9. खॻ͖਺ࣈͷσʔληοτ./*45Λ࢖ͬͯ ֶशϞσϧΛ࡞Ζ͏ʂ from keras.datasets import mnist from keras.models import Sequential

    from keras.layers.core import Dense, Activation from keras.utils import np_utils (X_train, y_train), (X_test, y_test) = mnist.load_data() σʔληοτͷಡΈࠐΈ 9@USBJOֶशͤ͞Δը૾σʔλ Z@USBJOֶशͤ͞Δϥϕϧσʔλ 9@UFTUֶशͤͨ͞ϞσϧΛςετ͢Δςετ༻ը૾σʔλ Z@UFTUֶशͤͨ͞ϞσϧΛςετ͢Δςετ༻ϥϕϧσʔλ NOJTUͷΠϯϙʔτ
  10. खॻ͖਺ࣈͷσʔληοτ./*45Λ࢖ͬͯ ֶशϞσϧΛ࡞Ζ͏ʂ 9@USBJO9@USBJOSFTIBQF    9@UFTU9@UFTUSFTIBQF   

    ֶशͤ͞Δը૾σʔλͱςετ༻ը૾σʔλΛ ϦγΣΠϓ͠·͢ɻ ./*45ͷը૾σʔλ ɹɹɹɹɹɹϐΫηϧºϐΫηϧϐΫηϧ ̍ϐΫηϧͷάϨʔεέʔϧ஋Λ̌ʙ̎̑̑ͷ਺ࣈͰදݱ ɹɹͰׂΔ͜ͱͰ̍ϐΫηϧͷ஋Λ̌ʙ̍ͷؒʹ͠·͢ ʢਖ਼نԽɿ̌ʙ̍·Ͱ͔͠ѻ͑ͳ͍ͷͰ߹Θͤ·͢ʣ  
  11. खॻ͖਺ࣈͷσʔληοτ./*45Λ࢖ͬͯ ֶशϞσϧΛ࡞Ζ͏ʂ NPEFM4FRVFOUJBM < %FOTF  JOQVU@TIBQF   "DUJWBUJPO

    bTJHNPJE  %FOTF   "DUJWBUJPO TPGUNBY  >  ֶशϞσϧΛ࡞͍ͬͯ·͢ɻ
  12. 5IPOOZ 1ZUIPO*%&1ZUIPOͷॳ৺ऀ༻౷߹؀ڥ Λ࢖͏ 3VOϘλϯΛԡ࣮ͯ͠ߦͤ͞Α͏ʂ खॻ͖਺ࣈͷσʔληοτ./*45 &Run mnist2.py Using TensorFlow backend.

    WARNING: Logging before flag parsing goes to stderr. W0823 09:50:29.745067 1995954896 deprecation_wrapper.py:118] From /home/pi/.local/lib/python3.7/site- packages/tensorflow/__init__.py:98: The name tf.AUTO_REUSE is deprecated. Please use tf.compat.v1.AUTO_REUSE instead. WARNING͕͍͔ͭ͘ग़·͕͢ ໰୊ͳ͍ͷͰ͠͹Β͘଴ͪ·͢
  13. վળͯ͠ਖ਼౴཰Λ͋͛Α͏ʂ NPEFM4FRVFOUJBM < %FOTF  JOQVU@TIBQF   "DUJWBUJPO bTJHNPJE

     %FOTF   "DUJWBUJPO TPGUNBY  >  TJHNPJEΛ SFMVʹมߋ͢Δ
  14. 5IPOOZ 1ZUIPO*%&1ZUIPOͷॳ৺ऀ༻౷߹؀ڥ Λ࢖͏ 3VOϘλϯΛԡ࣮ͯ͠ߦͤ͞Α͏ʂ खॻ͖਺ࣈͷσʔληοτ./*45 &Run mnist2.py Using TensorFlow backend.

    WARNING: Logging before flag parsing goes to stderr. W0823 09:50:29.745067 1995954896 deprecation_wrapper.py:118] From /home/pi/.local/lib/python3.7/site- packages/tensorflow/__init__.py:98: The name tf.AUTO_REUSE is deprecated. Please use tf.compat.v1.AUTO_REUSE instead. WARNING͕͍͔ͭ͘ग़·͕͢ ໰୊ͳ͍ͷͰ͠͹Β͘଴ͪ·͢
  15. NPEFMDPNQJMF MPTTDBUFHPSJDBM@DSPTTFOUSPQZ PQUJNJ[FSTHE NFUSJDT<BDDVSBDZ>  NPEFMpU 9@USBJO Z@USBJO CBUDI@TJ[F WFSCPTF

    FQPDIT  WBMJEBUJPO@TQMJU  FQPDIT͔ΒFQPDIT ʹมߋ͠Α͏ վળͯ͠ਖ਼౴཰Λ͋͛Α͏ʂ
  16. 9@USBJO Z@USBJO  9@UFTU Z@UFTU NOJTUMPBE@EBUB  9@USBJO9@USBJOSFTIBQF  

     9@UFTU9@UFTUSFTIBQF    Z@USBJOOQ@VUJMTUP@DBUFHPSJDBM Z@USBJO  Z@UFTUOQ@VUJMTUP@DBUFHPSJDBM Z@UFTU  NPEFM4FRVFOUJBM < %FOTF  JOQVU@TIBQF   "DUJWBUJPO SFMV  %FOTF   "DUJWBUJPO TPGUNBY  >  NPEFMDPNQJMF MPTTDBUFHPSJDBM@DSPTTFOUSPQZ PQUJNJ[FSTHE NFUSJDT<BDDVSBDZ>  NPEFMpU 9@USBJO Z@USBJO CBUDI@TJ[F WFSCPTF FQPDIT WBMJEBUJPO@TQMJU  TDPSFNPEFMFWBMVBUF 9@UFTU Z@UFTU WFSCPTF  QSJOU UFTUBDDVSBDZ TDPSF<>  ઌ΄ͲͷNOJTUQZͱಉ͡ॲཧͰ ਖ਼౴཰Λग़͠·͢ɻ ʢ࣌ؒͷؔ܎ͰFQPDITʹͱ͠·͢ʣ ࣮ࡍʹखͰॻ͍ͨ਺ࣈΛಡΈࠐΜͰ ./*45Λ࢖ͬͯ൑ผ͠Α͏ʂ
  17. 9@USBJO Z@USBJO  9@UFTU Z@UFTU NOJTUMPBE@EBUB  9@USBJO9@USBJOSFTIBQF  

     9@UFTU9@UFTUSFTIBQF    Z@USBJOOQ@VUJMTUP@DBUFHPSJDBM Z@USBJO  Z@UFTUOQ@VUJMTUP@DBUFHPSJDBM Z@UFTU  NPEFM4FRVFOUJBM < %FOTF  JOQVU@TIBQF   "DUJWBUJPO SFMV  %FOTF   "DUJWBUJPO TPGUNBY  >  NPEFMDPNQJMF MPTTDBUFHPSJDBM@DSPTTFOUSPQZ PQUJNJ[FSTHE NFUSJDT<BDDVSBDZ>  NPEFMpU 9@USBJO Z@USBJO CBUDI@TJ[F WFSCPTF FQPDIT WBMJEBUJPO@TQMJU  TDPSFNPEFMFWBMVBUF 9@UFTU Z@UFTU WFSCPTF  QSJOU UFTUBDDVSBDZ TDPSF<>  ઌ΄ͲͷNOJTUQZͱಉ͡ॲཧͰ ਖ਼౴཰Λग़͠·͢ɻ ʢ࣌ؒͷؔ܎ͰFQPDITʹͱ͠·͢ʣ ࣮ࡍʹखͰॻ͍ͨ਺ࣈΛಡΈࠐΜͰ ./*45Λ࢖ͬͯ൑ผ͠Α͏ʂ
  18. NPEFMTBWF ./*45I  JNHDWJNSFBE KQH    ઌ΄ͲͷNOJTUQZͱಉ͡ॲཧͰ ࡞ֶͬͨशϞσϧΛอଘ͠·͢ɻ

    ࠓճ͸ࢲ͕ॻ͍ͨ਺ࣈͷ̓ͷը૾Λ ೖΕ͓͍ͯͨͷͰɺ ͦΕΛ൑ผͯ͠Έ·͠ΐ͏ʂ ࣮ࡍʹखͰॻ͍ͨ਺ࣈΛಡΈࠐΜͰ ./*45Λ࢖ͬͯ൑ผ͠Α͏ʂ KQH ʻʻ͜ͷը૾͸ ɹɹJ1IPOFͷϝϞͰॻ͍ͯɺΩϟϓνϟʔͨ͠ ɹɹࣸਅͷαΠζΛฤूͰτϦϛϯάͨ͠΋ͷͰ͢ɻ
  19. HSBZDWDWU$PMPS JNH DW$0-03@#(3(3":  DWJNXSJUF HSBZQOH HSBZ   PQFODWΛ࢖ͬͯը૾ΛάϨʔεέʔϧʹ͠·͢

    ࣮ࡍʹखͰॻ͍ͨ਺ࣈΛಡΈࠐΜͰ ./*45Λ࢖ͬͯ൑ผ͠Α͏ʂ KQH HSBZQOH
  20. @ CJOBSZDWUISFTIPME HSBZ   DW5)3&4)@#*/"3:  DWJNXSJUF CJOBSZQOH CJOBSZ

      PQFODWΛ࢖ͬͯը૾Λ̎஋Խ͠·͢ ࣮ࡍʹखͰॻ͍ͨ਺ࣈΛಡΈࠐΜͰ ./*45Λ࢖ͬͯ൑ผ͠Α͏ʂ CJOBSZQOH HSBZQOH
  21. CMVSDW(BVTTJBO#MVS OFHBQPTJ     DWJNXSJUF CMVSQOH CMVS 

     PQFODWΛ࢖ͬͯը૾ʹ΅͔͠Λ͍ΕΔ ࣮ࡍʹखͰॻ͍ͨ਺ࣈΛಡΈࠐΜͰ ./*45Λ࢖ͬͯ൑ผ͠Α͏ʂ OFHBQPTJQOH CMVSQOH
  22. 3VOϘλϯΛԡ࣮ͯ͠ߦͤ͞Α͏ʂ &Run mnist2.py Using TensorFlow backend. WARNING: Logging before flag

    parsing goes to stderr. W0823 09:50:29.745067 1995954896 deprecation_wrapper.py:118] From /home/pi/.local/lib/python3.7/site- packages/tensorflow/__init__.py:98: The name tf.AUTO_REUSE is deprecated. Please use tf.compat.v1.AUTO_REUSE instead. WARNING͕͍͔ͭ͘ग़·͕͢ ໰୊ͳ͍ͷͰ͠͹Β͘଴ͪ·͢ ࣮ࡍʹखͰॻ͍ͨ਺ࣈΛಡΈࠐΜͰ ./*45Λ࢖ͬͯ൑ผ͠Α͏ʂ
  23. ֶशࡁΈϞσϧΛ࢖ͬͯΈΑ͏ʂ ୈ̍ϑΣʔζɿֶशϞσϧΛͭ͘Δ ୈ̎ϑΣʔζɿֶशࡁΈϞσϧΛ࢖͏ σʔλ ॲཧ σʔλ ऩू ػցֶश ਂ૚ֶश ֶशͤ͞Δը૾

    ֶशࡁΈ Ϟσϧ ֶश༻ σʔληοτ ೝ͍ࣝͤͨ͞ը૾ ೖྗ ग़ྗ ֶशࡁΈ Ϟσϧ ਪ࿦ ը૾ೝࣝΛ ࢖ͬͨॲཧ
  24. from keras.preprocessing import image from keras.applications.inception_v3 \ import preprocess_input, decode_predictions,

    InceptionV3 import numpy as np model = InceptionV3(weights='imagenet') img_path=' banana.jpg' img = image.load_img(img_path, target_size=(299,299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) ֶशࡁΈϞσϧΛ࢖ͬͯը૾ೝࣝ͢Δ޻ఔͱ͸ CBOBOBKQH ɹɹɹˣ *ODFQUJPO7ͱ ͍͏ֶशࡁΈ ϞσϧΛ࢖ͬͯ ը૾ೝࣝ ɹɹˣ είΞͰ݁Ռ͕ ͰΔ
  25. from keras.preprocessing import image from keras.applications.inception_v3 \ import preprocess_input, decode_predictions,

    InceptionV3 import numpy as np model = InceptionV3(weights='imagenet') img_path=' banana.jpg' img = image.load_img(img_path, target_size=(299,299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) print('Predicted:') for p in decode_predictions(preds, top=5)[0]: print("Score {}, Label {}".format(p[2],p[1]))  ֶशࡁΈϞσϧΛ࢖ͬͨॲཧͷઆ໌ɿͦͷ̍ ը૾ͷಡΈࠐΈ΍ ֶशࡁΈϞσϧͷ *ODFQUJPO7ͷಡΈ ࠐΈɺ1ZUIPOֶज़ ܭࢉϥΠϒϥϦʔ ͷOVNQZΛΠϯ ϙʔτ͢Δ
  26. from keras.preprocessing import image from keras.applications.inception_v3 \ import preprocess_input, decode_predictions,

    InceptionV3 import numpy as np model = InceptionV3(weights='imagenet') img_path=' banana.jpg' img = image.load_img(img_path, target_size=(299,299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) print('Predicted:') for p in decode_predictions(preds, top=5)[0]: print("Score {}, Label {}".format(p[2],p[1]))  ֶशࡁΈϞσϧΛ࢖ͬͨॲཧͷઆ໌ɿͦͷ *NBHF/FUͰֶश͠ ͨॏΈͱ *ODFQUJPO7 ͷجຊϞσϧ͕ಡ Έࠐ·ΕΔ
  27. from keras.preprocessing import image from keras.applications.inception_v3 \ import preprocess_input, decode_predictions,

    InceptionV3 import numpy as np model = InceptionV3(weights='imagenet') img_path=' banana.jpg' img = image.load_img(img_path, target_size=(299,299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) print('Predicted:') for p in decode_predictions(preds, top=5)[0]: print("Score {}, Label {}".format(p[2],p[1]))  ֶशࡁΈϞσϧΛ࢖ͬͨॲཧͷઆ໌ɿͦͷ ೝ͍ࣝͤͨ͞ը૾ ͷࢦఆͱը૾ͷα Πζͷॲཧ
  28. from keras.preprocessing import image from keras.applications.inception_v3 \ import preprocess_input, decode_predictions,

    InceptionV3 import numpy as np model = InceptionV3(weights='imagenet') img_path=' banana.jpg' img = image.load_img(img_path, target_size=(299,299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) print('Predicted:') for p in decode_predictions(preds, top=5)[0]: print("Score {}, Label {}".format(p[2],p[1]))  ֶशࡁΈϞσϧΛ࢖ͬͨॲཧͷઆ໌ɿͦͷ ը૾ͷલॲཧ
  29. from keras.preprocessing import image from keras.applications.inception_v3 \ import preprocess_input, decode_predictions,

    InceptionV3 import numpy as np model = InceptionV3(weights='imagenet') img_path=' banana.jpg' img = image.load_img(img_path, target_size=(299,299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) print('Predicted:') for p in decode_predictions(preds, top=5)[0]: print("Score {}, Label {}".format(p[2],p[1]))  ֶशࡁΈϞσϧΛ࢖ͬͨॲཧͷઆ໌ɿͦͷ ਪ࿦
  30. from keras.preprocessing import image from keras.applications.inception_v3 \ import preprocess_input, decode_predictions,

    InceptionV3 import numpy as np model = InceptionV3(weights='imagenet') img_path=' banana.jpg' img = image.load_img(img_path, target_size=(299,299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) print('Predicted:') for p in decode_predictions(preds, top=5)[0]: print("Score {}, Label {}".format(p[2],p[1]))  ֶशࡁΈϞσϧΛ࢖ͬͨॲཧͷઆ໌ɿͦͷ ਪ࿦݁Ռͷग़ྗ
  31. from keras.preprocessing import image from keras.applications.inception_v3 \ import preprocess_input, decode_predictions,

    InceptionV3 import numpy as np model = InceptionV3(weights='imagenet') img_path=' apple.jpg' img = image.load_img(img_path, target_size=(299,299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) ֶशࡁΈϞσϧΛ࢖ͬͯը૾ೝࣝ͢Δ޻ఔͱ͸ BQQMFKQH ɹˣ ɹߦ໨ͷ CBOBOBKQHΛ ɹBQQMFKQHʹ มߋ͢Δ ɹɹˣ ಉ͡Α͏ʹ࣮ߦ ͯ݁͠ՌΛΈΔ