äžè¬çãªãã¥ãŒã©ã«ãããã¯ãŒã¯ãç¹ã«ãã¿ãŒã³èªèã®åéã§ã®é²æ©ã«ãããç»åãæäœããããã®ãã¥ãŒã©ã«ãããã¯ãŒã¯ã¢ããªã±ãŒã·ã§ã³ãäœæããããšã¯æ¥åžžçãªäœæ¥ã§ãããã®ããã«æãããããšããããŸãã ããæå³ã§ã¯ãããã§ã-ãã¿ãŒã³èªèã«é¢é£ããã¢ã€ãã¢ãæãã€ãããã誰ãããã®ãããªãã®ããã§ã«æžããŠããããšãçã£ãŠã¯ãããŸããã å¿
èŠãªã®ã¯ãGoogleã§å¯Ÿå¿ããã³ãŒããèŠã€ããŠãäœæè
ãããã³ã³ãã€ã«ãããããšã ãã§ãã
ããããã¿ã¹ã¯ã解決ã§ããªãã»ã©éå±ãªãã®ã«ããªãå€ãã®è©³çŽ°ããŸã æ®ã£ãŠããŸãã ç¹ã«ãåå¿è
ã®æ¹ã§ãã¬ã€ãã³ã¹ãã¹ããããã€ã¹ããããç®ã®åã§å®è¡ãããæåããæåŸãŸã§å®äºãããããžã§ã¯ããå¿
èŠãªå Žåã¯ãæéãããããããŸãã ãã®ãããªå Žåãéåžžã®ããã®æãããªéšåãã¹ãããããããšããèšãèš³ã¯ãããŸããã
ãã®èšäºã§ã¯ãDog Breed Identifierãäœæããã¿ã¹ã¯ãæ€èšããŸãããã¥ãŒã©ã«ãããã¯ãŒã¯ãäœæããŠãã¬ãŒãã³ã°ããAndroidã®Javaã«ç§»æ€ããŠGoogle Playã§å
¬éããŸãã
å®æããçµæãã芧ã«ãªãããå Žåã¯ãGoogle Playã®
NeuroDogã¢ããªã
ã芧ãã ããã
ç§ã®ããããïŒé²è¡äžïŒãå«ãWebãµã€ãïŒ
robotics.snowcron.com ã
ã¬ã€ããå«ãããã°ã©ã èªäœã®Webãµã€ãïŒ
NeuroDog User Guide ã
ãããŠãããã«ããã°ã©ã ã®ã¹ã¯ãªãŒã³ã·ã§ããããããŸãïŒ
åé¡ã®å£°æ
ãã¥ãŒã©ã«ãããã¯ãŒã¯ãæäœããããã®Googleã©ã€ãã©ãªã§ããKerasã䜿çšããŸãã ããã¯é«ã¬ãã«ã®ã©ã€ãã©ãªã§ããã€ãŸããç§ãç¥ã£ãŠããéžæè¢ã«æ¯ã¹ãŠäœ¿ããããã§ãã ã©ã¡ãããšããã°-ãããã¯ãŒã¯å
ã®Kerasã«ã¯ãé«å質ã®æç§æžããããããããŸãã
CNN-Convolutional Neural Networksã䜿çšããŸãã CNNïŒããã³ãããã«åºã¥ãããé«åºŠãªæ§æïŒã¯ãç»åèªèã®äºå®äžã®æšæºã§ãã åæã«ããã®ãããªãããã¯ãŒã¯ãèšç·Žããããšã¯å¿
ããã容æã§ã¯ãããŸããããããã¯ãŒã¯æ§é ãšèšç·Žãã©ã¡ãŒã¿ãŒïŒããããã¹ãŠã®åŠç¿çãéåéãL1ããã³L2ãªã©ïŒãéžæããå¿
èŠããããŸãã ãã®ã¿ã¹ã¯ã«ã¯ããªãã®ã³ã³ãã¥ãŒãã£ã³ã°ãªãœãŒã¹ãå¿
èŠãªã®ã§ããã¹ãŠã®ãã©ã¡ãŒã¿ãŒãééããã ãã§è§£æ±ºããã«ã¯å€±æããŸãã
ããã¯ãã»ãšãã©ã®å Žåããããããããã©ãã¢ãããŒãã®ä»£ããã«ãããããã転éç¥èãã䜿çšããçç±ã®1ã€ã§ãã Transfer Knowlegeã¯ãç§ãã¡ã®åã®èª°ãïŒããšãã°ãGoogleïŒã«ãã£ãŠèšç·Žããããã¥ãŒã©ã«ãããã¯ãŒã¯ã䜿çšããŸããéåžžã¯ãåæ§ã®ãããããŸã ç°ãªãã¿ã¹ã¯ã®ããã«ã æåã®ã¬ã€ã€ãŒãååŸããæåŸã®ã¬ã€ã€ãŒãç¬èªã®åé¡åã«çœ®ãæããŸãããããŠããããæ©èœããããŸãæ©èœããŸãã
æåããã®ãããªçµæã¯é©ããããããŸããïŒç«ãšæ€
åãåºå¥ããããã«èšç·ŽãããGoogleãããã¯ãŒã¯ãå©çšããŠãç¬ã®åçš®ãèªèããŠããã®ã¯ã©ãããŠã§ããããïŒ ãããã©ã®ããã«èµ·ããããç解ããã«ã¯ããã¿ãŒã³èªèã«äœ¿çšããããã®ãå«ãããã£ãŒããã¥ãŒã©ã«ãããã¯ãŒã¯ã®åºæ¬ååãç解ããå¿
èŠããããŸãã
å
¥åãšããŠããããã¯ãŒã¯ã«ç»åïŒã€ãŸããæ°åã®é
åïŒããäŸçµŠãããŸããã æåã®ã¬ã€ã€ãŒã¯ãæ°Žå¹³ç·ãå匧ãªã©ã®åçŽãªãã¿ãŒã³ã«ã€ããŠç»åãåæããŸãã 次ã®å±€ã¯ããããã®ãã¿ãŒã³ãå
¥åãšããŠåãåãããæ¯ç®ãããã¢ã€ã¢ã³ã°ã«ããªã©ã®2次ãã¿ãŒã³ãçæããŸããæçµçã«ãç¬ãåæ§ç¯ã§ããããºã«ãååŸããŸãã
äžèšã®ãã¹ãŠã¯ãïŒGoogleãªã©ããïŒç§ãã¡ãååŸããäºåã«ãã¬ãŒãã³ã°ãããã¬ã€ã€ãŒã®å©ããåããŠè¡ãããŸããã 次ã«ãã¬ã€ã€ãŒãè¿œå ãããããã®ãã¿ãŒã³ããåçš®æ
å ±ãæœåºããããšãæããŸãã ããã¯è«ççã«èãããŸãã
èŠçŽãããšããã®èšäºã§ã¯ããããã©ãCNNãšãããŸããŸãªã¿ã€ãã®ãããã¯ãŒã¯ã®ã転éåŠç¿ãããªã¢ã³ãã®äž¡æ¹ãäœæããŸãã ãããã©ãã«ã€ããŠã¯ãäœæããŸããããäºåãã¬ãŒãã³ã°æžã¿ããããã¯ãŒã¯ã®ãã¬ãŒãã³ã°ãšæ§æãã¯ããã«ç°¡åãªã®ã§ããã©ã¡ãŒã¿ãŒãéžæããŠæ§æããäºå®ã¯ãããŸããã
ç¬ã®åçš®ãèªèããããã«ãã¥ãŒã©ã«ãããã¯ãŒã¯ãæããäºå®ãªã®ã§ãããŸããŸãªåçš®ã®ãµã³ãã«ãã衚瀺ãããå¿
èŠããããŸãã 幞ããªããšã«ãåæ§ã®ã¿ã¹ã¯ã®ããã«
ããã§äœæãããäžé£ã®åçã
ãããŸãïŒ
ãªãªãžãã«ã¯ãã¡ãã§ã ïŒã
次ã«ãåä¿¡ãããããã¯ãŒã¯ã®ãã¹ããAndroidã«ç§»æ€ããäºå®ã§ãã Kerasovãããã¯ãŒã¯ãAndroidã«ç§»æ€ããããšã¯æ¯èŒçåçŽã§ãååã«åœ¢åŒåãããŠãããå¿
èŠãªãã¹ãŠã®æé ãå®è¡ããããããã®éšåãåçŸããããšã¯é£ãããããŸããã
ãã®åŸãããããã¹ãŠãGoogle Playã§å
¬éããŸãã åœç¶ãã°ãŒã°ã«ã¯æµæããã®ã§ãè¿œå ã®ããªãã¯ã䜿çšãããŸãã ããšãã°ãã¢ããªã±ãŒã·ã§ã³ã®ãµã€ãºïŒå·šå€§ãªãã¥ãŒã©ã«ãããã¯ãŒã¯ã«ããïŒã¯ãGoogle Playã§åãå
¥ããããAndroid APKã®èš±å®¹ãµã€ãºããã倧ãããªããŸãããã³ãã«ã䜿çšããå¿
èŠããããŸãã ããã«ãGoogleã¯æ€çŽ¢çµæã«ã¢ããªã±ãŒã·ã§ã³ã衚瀺ããŸãããããã¯ãã¢ããªã±ãŒã·ã§ã³ã«æ€çŽ¢ã¿ã°ãç»é²ãããã1ã2é±éåŸ
ã€ã ãã§ä¿®æ£ã§ããŸãã
ãã®çµæãå®å
šã«æ©èœãããåçšãïŒåŒçšç¬Šã§å²ãŸããç¡æã§é
眮ãããïŒã¢ããªã±ãŒã·ã§ã³ããAndroidããã³ãã¥ãŒã©ã«ãããã¯ãŒã¯ã䜿çšããŠååŸãããŸãã
éçºç°å¢
Kerasã®ããã°ã©ã ã¯ã䜿çšããŠããOSïŒUbuntuæšå¥šïŒããããªã«ãŒãã®æç¡ãªã©ã«å¿ããŠãããŸããŸãªæ¹æ³ã§ããã°ã©ãã³ã°ã§ããŸãã ãããæãç°¡åãªæ¹æ³ã§ã¯ãªãããšãé€ããŠãããŒã«ã«ã³ã³ãã¥ãŒã¿ãŒã§éçºããã®ã¯æªããããŸããïŒãããã£ãŠãæ§æããŸãïŒã
æåã«ãå€æ°ã®ããŒã«ãšã©ã€ãã©ãªã®ã€ã³ã¹ããŒã«ãšæ§æã«æéãããããŸãã次ã«ãæ°ããããŒãžã§ã³ããªãªãŒã¹ããããšãåã³æéãè²»ããå¿
èŠããããŸãã 第äºã«ããã¥ãŒã©ã«ãããã¯ãŒã¯ã¯ãã¬ãŒãã³ã°ã«å€§ããªèšç®èœåãå¿
èŠãšããŸãã GPUã䜿çšãããšããã®ããã»ã¹ãïŒ10å以äžïŒé«éåã§ããŸãããã®èšäºã®å·çæç¹ã§ã¯ããã®äœæ¥ã«æé©ãªãããGPUã®äŸ¡æ Œã¯2,000ã7,000ãã«ã§ãã ã¯ããããããæ§æããå¿
èŠããããŸãã
ã ããç§ãã¡ã¯ä»ã®æ¹æ³ã§è¡ããŸãã äºå®ãGoogleã¯ç§ãã¡ã®ãããªè²§åŒ±ãªããªããºããã¯ã©ã¹ã¿ãŒã®GPUã䜿çšã§ããããšãèš±å¯ããŠããŸã-ç¡æã§ããã¥ãŒã©ã«ãããã¯ãŒã¯ã«é¢é£ããèšç®ã®ããã«ãå®å
šã«æ§æãããç°å¢ãæäŸããŸãã ãã®ãµãŒãã¹ã䜿çšãããšãpythonãKerasãããã³æ¢ã«æ§æãããŠããèšå€§ãªæ°ã®ã©ã€ãã©ãªãå«ãJupiter Notebookã«ã¢ã¯ã»ã¹ã§ããŸãã Googleã¢ã«ãŠã³ããååŸããã ãã§ãïŒGmailã¢ã«ãŠã³ããååŸãããšãä»ã®ãã¹ãŠã«ã¢ã¯ã»ã¹ã§ããŸãïŒã
çŸæç¹ã§ã¯ã
ããã§Colabãéãããšãã§ããŸãããGoogleãç¥ã£ãŠããã°ãããã¯ãã€ã§ãå€æŽã§ããŸãã Google Colabãæ€çŽ¢ããã ãã§ãã
Colabã®äœ¿çšã«é¢ããæãããªåé¡ã¯ããããWEBãµãŒãã¹ã§ããããšã§ãã ã©ã®ããã«ããŒã¿ã«ã¢ã¯ã»ã¹ããŸããïŒ ãã¬ãŒãã³ã°åŸã«ãã¥ãŒã©ã«ãããã¯ãŒã¯ãä¿åããŸããããšãã°ãã¿ã¹ã¯åºæã®ããŒã¿ãããŠã³ããŒãããŸããïŒ
ããã€ãã®æ¹æ³ïŒãã®èšäºã®å·çæç¹ã§ã¯3ã€ïŒããããæã䟿å©ã ãšæãããæ¹æ³ã䜿çšããŠããŸããGoogleãã©ã€ãã䜿çšããŠããŸãã
Googleãã©ã€ãã¯ãéåžžã®ããŒããã©ã€ããšã»ãšãã©åãããã«æ©èœããã¯ã©ãŠãããŒã¹ã®ããŒã¿ã¹ãã¬ãŒãžã§ãããGoogle Colabã«ãããã³ã°ã§ããŸãïŒä»¥äžã®ã³ãŒããåç
§ïŒã ãã®åŸãããŒã«ã«ãã£ã¹ã¯äžã®ãã¡ã€ã«ãæäœããã®ãšåãããã«æäœã§ããŸãã ã€ãŸããããšãã°ããã¥ãŒã©ã«ãããã¯ãŒã¯ããã¬ãŒãã³ã°ããããã«ç¬ã®åçã«ã¢ã¯ã»ã¹ããã«ã¯ãããããGoogleãã©ã€ãã«ã¢ããããŒãããå¿
èŠããããŸããããã ãã§ãã
ãã¥ãŒã©ã«ãããã¯ãŒã¯ã®äœæãšãã¬ãŒãã³ã°
以äžã«ãPythonã®ã³ãŒãããããã¯ããšã«ç€ºããŸãïŒJupiter NotebookããïŒã ãããã¯ã¯ç¬ç«ããŠå®è¡ã§ããããããã®ã³ãŒããJupiter Notebookã«ã³ããŒããŠãããã¯åäœã§å®è¡ããããšãã§ããŸãïŒãã¡ãããåæã®ãããã¯ã§å®çŸ©ãããå€æ°ã¯åŸã®ãããã¯ã§å¿
èŠã«ãªãå ŽåããããŸãããããã¯æãããªäŸåé¢ä¿ã§ãïŒã
åæå
ãŸããGoogleãã©ã€ããããŠã³ãããŸãããã ããã2è¡ã§ãã ãã®ã³ãŒãã¯ãColabã»ãã·ã§ã³ã§1åã ãå®è¡ããå¿
èŠããããŸãïŒããšãã°ã6æéã®äœæ¥ã§1åïŒã ã»ãã·ã§ã³ããŸã ãçããŠãããéã«ããäžåºŠåŒã³åºããšããã£ã¹ã¯ããã§ã«ããŠã³ããããŠããããã¹ããããããŸãã
from google.colab import drive drive.mount('/content/drive/')
æåã®éå§æã«ã¯ãæå³ã確èªããããã«æ±ããããŸãããè€éãªããšã¯äœããããŸããã ããã¯æ¬¡ã®ãããªãã®ã§ãã
>>> Go to this URL in a browser: ... >>> Enter your authorization code: >>> ·········· >>> Mounted at /content/drive/
å®å
šã«æšæºã®
includeã»ã¯ã·ã§ã³ã å«ãŸããŠãããã¡ã€ã«ã®äžéšãäžèŠã§ããå¯èœæ§ããããŸãã ãŸããããŸããŸãªãã¥ãŒã©ã«ãããã¯ãŒã¯ããã¹ããããããç¹å®ã®ã¿ã€ãã®ãã¥ãŒã©ã«ãããã¯ãŒã¯ã«å«ãŸããã¢ãžã¥ãŒã«ã®äžéšãã³ã¡ã³ãå/ã³ã¡ã³ã解é€ããå¿
èŠããããŸãïŒããšãã°ãInceptionV3 NNã䜿çšããŠãInceptionV3ã®ã€ã³ã¯ã«ãŒããã³ã¡ã³ã解é€ããããšãã°ResNet50ãã³ã¡ã³ãã¢ãŠãããŸãïŒã ãã©ããïŒããããã®ãã¹ãŠã®å€æŽã¯ã䜿çšãããã¡ã¢ãªã®ãµã€ãºã§ãããããã¯ããŸã匷ããããŸããã
import datetime as dt import pandas as pd import seaborn as sns import matplotlib.pyplot as plt from tqdm import tqdm import cv2 import numpy as np import os import sys import random import warnings from sklearn.model_selection import train_test_split import keras from keras import backend as K from keras import regularizers from keras.models import Sequential from keras.models import Model from keras.layers import Dense, Dropout, Activation from keras.layers import Flatten, Conv2D from keras.layers import MaxPooling2D from keras.layers import BatchNormalization, Input from keras.layers import Dropout, GlobalAveragePooling2D from keras.callbacks import Callback, EarlyStopping from keras.callbacks import ReduceLROnPlateau from keras.callbacks import ModelCheckpoint import shutil from keras.applications.vgg16 import preprocess_input from keras.preprocessing import image from keras.preprocessing.image import ImageDataGenerator from keras.models import load_model from keras.applications.resnet50 import ResNet50 from keras.applications.resnet50 import preprocess_input from keras.applications.resnet50 import decode_predictions from keras.applications import inception_v3 from keras.applications.inception_v3 import InceptionV3 from keras.applications.inception_v3 import preprocess_input as inception_v3_preprocessor from keras.applications.mobilenetv2 import MobileNetV2 from keras.applications.nasnet import NASNetMobile
Googleãã©ã€ãã§ã¯ããã¡ã€ã«çšã®ãã©ã«ããŒãäœæããŸãã 2è¡ç®ã«ã¯ããã®å
容ã衚瀺ãããŸãã
working_path = "/content/drive/My Drive/DeepDogBreed/data/" !ls "/content/drive/My Drive/DeepDogBreed/data" >>> all_images labels.csv models test train valid
ã芧ã®ãšãããç¬ã®åçïŒGoogleãã©ã€ãã®ã¹ã¿ã³ãã©ãŒãããŒã¿ã»ããïŒäžèšåç
§ïŒããã³ããŒïŒã¯ãæåã«
all_imagesãã©ã«ããŒã«ä¿åãããŸãã åŸã§ããããã
trainãvalidããã³
testãã£ã¬ã¯ããªã«ã³ããŒããŸãã ãã¬ãŒãã³ã°æžã¿ã®ã¢ãã«ã
ã¢ãã«ãã©ã«ããŒã«ä¿åããŸãã labels.csvãã¡ã€ã«ã«é¢ããŠã¯ãããã¯åçä»ãã®ããŒã¿ã»ããã®äžéšã§ãããåçãšç¬çš®ã®ååã®å¯Ÿå¿è¡šãå«ãŸããŠããŸãã
Googleããäžæçã«äœ¿çšããããã«åŸããããã®ãæ£ç¢ºã«ææ¡ããããã«å®è¡ã§ãããã¹ãã¯å€æ°ãããŸãã äŸïŒ
ã芧ã®ãšãããGPUã¯å®éã«æ¥ç¶ãããŠããŸããæ¥ç¶ãããŠããªãå Žåã¯ãJupiter Notebookèšå®ã§ãã®ãªãã·ã§ã³ãèŠã€ããŠæå¹ã«ããå¿
èŠããããŸãã
次ã«ãç»åã®ãµã€ãºãªã©ãããã€ãã®å®æ°ã宣èšããå¿
èŠããããŸãã 256x256ãã¯ã»ã«ã®ãµã€ãºã®åçã䜿çšããŸããããã¯ã现éšã倱ããªãããã«ååã«å€§ããç»åã§ããããã¹ãŠãã¡ã¢ãªã«åãŸãã»ã©å°ããç»åã§ãã ãã ãã䜿çšãããã¥ãŒã©ã«ãããã¯ãŒã¯ã®çš®é¡ã«ã¯ã224x224ãã¯ã»ã«ã®ç»åãå¿
èŠã§ãã ãã®ãããªå Žåã256ãã³ã¡ã³ããã224ã®ã³ã¡ã³ããå€ããŸãã
åãæ¹æ³ïŒã³ã¡ã³ã1-ã³ã¡ã³ã解é€ïŒããä¿åããã¢ãã«ã®ååã«é©çšããŸããããã¯ãæçšãªãã¡ã€ã«ãäžæžãããããªãããã§ãã
warnings.filterwarnings("ignore") os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' np.random.seed(7) start = dt.datetime.now() BATCH_SIZE = 16 EPOCHS = 15 TESTING_SPLIT=0.3
ããŒã¿ã®èªã¿èŸŒã¿
ãŸã
ãlabels.csvãã¡ã€ã«ã
ã¢ããããŒãããŠããã¬ãŒãã³ã°ãšæ€èšŒã®éšåã«åããŸãããã ããã«å€ãã®ãã¬ãŒãã³ã°ããŒã¿ãååŸããããã«ããŒããè¡ãããããŸã ãã¹ãéšåã¯ãããŸããã
labels = pd.read_csv(working_path + 'labels.csv') print(labels.head()) train_ids, valid_ids = train_test_split(labels, test_size = TESTING_SPLIT) print(len(train_ids), 'train ids', len(valid_ids), 'validation ids') print('Total', len(labels), 'testing images') >>> id breed >>> 0 000bec180eb18c7604dcecc8fe0dba07 boston_bull >>> 1 001513dfcb2ffafc82cccf4d8bbaba97 dingo >>> 2 001cdf01b096e06d78e9e5112d419397 pekinese >>> 3 00214f311d5d2247d5dfe4fe24b2303d bluetick >>> 4 0021f9ceb3235effd7fcde7f7538ed62 golden_retriever >>> 7155 train ids 3067 validation ids >>> Total 10222 testing images
次ã«ããã¡ã€ã«åã«åŸã£ãŠãç»åãã¡ã€ã«ããã¬ãŒãã³ã°/æ€èšŒ/ãã¹ããã©ã«ããŒã«ã³ããŒããŸãã 次ã®é¢æ°ã¯ãæå®ãããã©ã«ããŒã«ååã転éãããã¡ã€ã«ãã³ããŒããŸãã
def copyFileSet(strDirFrom, strDirTo, arrFileNames): arrBreeds = np.asarray(arrFileNames['breed']) arrFileNames = np.asarray(arrFileNames['id']) if not os.path.exists(strDirTo): os.makedirs(strDirTo) for i in tqdm(range(len(arrFileNames))): strFileNameFrom = strDirFrom + arrFileNames[i] + ".jpg" strFileNameTo = strDirTo + arrBreeds[i] + "/" + arrFileNames[i] + ".jpg" if not os.path.exists(strDirTo + arrBreeds[i] + "/"): os.makedirs(strDirTo + arrBreeds[i] + "/")
ã芧ã®ãšããã
ãã¹ããšããŠç¬çš®ããšã«1ã€ã®ãã¡ã€ã«ã®ã¿ãã³ããŒããŸãã ãŸããã³ããŒãããšãããµããã©ã«ããäœæããŸãïŒååçš®ã«1ã€ïŒã ãããã£ãŠãåçã¯åçš®ããšã«ãµããã©ã«ããŒã«ã³ããŒãããŸãã
ããã¯ãKerasãåæ§ã®æ§é ã®ãã£ã¬ã¯ããªãæäœããå¿
èŠã«å¿ããŠç»åãã¡ã€ã«ãããŒãã§ãããããäžåºŠã«ãã¹ãŠã§ã¯ãªãã¡ã¢ãªãç¯çŽã§ããããã§ãã 15,000æãã¹ãŠã®ç»åãäžåºŠã«ã¢ããããŒãããããšã¯ãå§ãã§ããŸããã
ãã®é¢æ°ã¯ç»åãã³ããŒããã®ã§ãäžåºŠã ãåŒã³åºãå¿
èŠããããŸã-ãã¯ãå¿
èŠãããŸããã ãããã£ãŠãå°æ¥ã®äœ¿çšã®ããã«ãã³ã¡ã³ãããå¿
èŠããããŸãã
ç¬ã®åçš®ã®ãªã¹ããååŸããŸãã
breeds = np.unique(labels['breed']) map_characters = {}
ç»ååŠç
ImageDataGeneratorsãšåŒã°ããKerasã©ã€ãã©ãªæ©èœã䜿çšããŸãã ImageDataGeneratorã¯ãç»åã®åŠçãæ¡å€§çž®å°ãå転ãªã©ãè¡ãããšãã§ããŸãã ãŸããç»åãããã«åŠçã§ãã
åŠçæ©èœãåãå
¥ããããšãã§ããŸãã
def preprocess(img): img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE), interpolation = cv2.INTER_AREA)
次ã®ã³ãŒãã«æ³šæããŠãã ããã
ImageDataGeneratorèªäœã§æ£èŠåã§ããŸãïŒå
ã®0-255ã§ã¯ãªãã0-1ã®ç¯å²ã®ãµãããŒã¿ïŒã ãªãããªããã»ããµãå¿
èŠãªã®ã§ããïŒ äŸãšããŠãäžé®®æãªåŒã³åºããæ€èšããŸãïŒã³ã¡ã³ãåãããŠããŸããã䜿çšããŸããïŒãããã¯ãä»»æã®ã«ã¹ã¿ã ç»åæäœãšåãã§ãã HDRãšã¯å¯Ÿç
§çãªãã®ã
ãã¬ãŒãã³ã°çšãšæ€èšŒçšã®2ã€ã®ç°ãªãImageDataGeneratorã䜿çšããŸãã éãã¯ããã¬ãŒãã³ã°ã«ã¯ããŒã¿ã®ãå€æ§æ§ããé«ããããã«ã¿ãŒã³ãšã¹ã±ãŒãªã³ã°ãå¿
èŠã§ãããæ€èšŒã«ã¯å°ãªããšããã®ã¿ã¹ã¯ã§ã¯å¿
èŠãªããšããããšã§ãã
train_datagen = ImageDataGenerator( preprocessing_function=preprocess,
ãã¥ãŒã©ã«ãããã¯ãŒã¯ã®äœæ
æ¢ã«è¿°ã¹ãããã«ãããã€ãã®ã¿ã€ãã®ãã¥ãŒã©ã«ãããã¯ãŒã¯ãäœæããŸãã å¥ã®é¢æ°ãåŒã³åºããŠäœæããä»ã®ãã¡ã€ã«ãã€ã³ã¯ã«ãŒãããæã«ã¯ç°ãªãç»åãµã€ãºã決å®ãããã³ã«ã ãããã£ãŠãç°ãªãã¿ã€ãã®ãã¥ãŒã©ã«ãããã¯ãŒã¯ãåãæ¿ããã«ã¯ãé©åãªã³ãŒããã³ã¡ã³ãå/ã³ã¡ã³ã解é€ããå¿
èŠããããŸãã
ãŸãããããã©ãCNNãäœæããŸãã ãããã°ã«æéã浪費ããªãããšã«æ±ºãããããããŸããããŸããããå°ãªããšãèŠæãããã°éçºã§ããåºç€ãæäŸããŸãïŒéåžžãäºåèšç·Žããããããã¯ãŒã¯ãæè¯ã®çµæããããããããããã¯æªãèãã§ãïŒã
def createModelVanilla(): model = Sequential()
転éåŠç¿ã䜿çšããŠãããã¯ãŒã¯ãäœæãããšãæé ãå€ãããŸãã
def createModelMobileNetV2():
ä»ã®ã¿ã€ãã®ãããã¯ãŒã¯ã®äœæãåããã¿ãŒã³ã«åŸããŸãã
def createModelResNet50(): base_model = ResNet50(weights='imagenet', include_top=False, pooling='avg', input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3)) x = base_model.output x = Dense(512)(x) x = Activation('relu')(x) x = Dropout(0.5)(x) predictions = Dense(NUM_CLASSES, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions)
èŠåïŒåè
ïŒ ãã®NNã¯æé«ã®çµæã瀺ããŸããã
def createModelInceptionV3():
å¥ã®ãã®ïŒ
def createModelNASNetMobile():
ããŸããŸãªçš®é¡ã®ãã¥ãŒã©ã«ãããã¯ãŒã¯ãããŸããŸãªã¿ã¹ã¯ã«äœ¿çšã§ããŸãã ãã®ãããäºæž¬ç²ŸåºŠã®èŠä»¶ã«å ããŠããµã€ãºïŒã¢ãã€ã«NNã¯Inceptionã®5åïŒãšé床ãéèŠã§ãïŒãããªã¹ããªãŒã ã®ãªã¢ã«ã¿ã€ã åŠçãå¿
èŠãªå Žåã¯ã粟床ãç ç²ã«ããå¿
èŠããããŸãïŒã
ãã¥ãŒã©ã«ãããã¯ãŒã¯ãã¬ãŒãã³ã°
ãŸãã
å®éšäžã§ãã®ã§ãä¿åããã䜿çšããªããªã£ããã¥ãŒã©ã«ãããã¯ãŒã¯ãåé€ã§ããã¯ãã§ãã 次ã®é¢æ°ã¯ãNNãååšããå Žåã¯åé€ããŸãã
ãã¥ãŒã©ã«ãããã¯ãŒã¯ãäœæããã³åé€ããæ¹æ³ã¯ãéåžžã«ã·ã³ãã«ã§ç°¡åã§ãã ãŸããåé€ããŸãã
delete ïŒã®ã¿ïŒãåŒã³åºãå ŽåãJupiter Notebookã«ã¯ãå®è¡éžæãæ©èœãããã䜿çšãããã®ã®ã¿ãéžæããŠå®è¡ããããšã«æ³šæããŠãã ããã
次ã«ããã¡ã€ã«ãååšããªãå Žåã¯ãã¥ãŒã©ã«ãããã¯ãŒã¯ãäœæãããã¡ã€ã«ãååšããå Žåã¯
loadãåŒã³åºã
ãŸã ããã¡ããããdeleteããåŒã³åºããŠNNãååšããããšãæåŸ
ã§ããªããããä¿åããããã¥ãŒã©ã«ãããã¯ãŒã¯ã䜿çšããã«ã¯
deleteãåŒã³åºããªãã§
ãã ãã ã
èšãæããã°ãç¶æ³ãšçŸåšå®éšããŠããããšã«å¿ããŠãæ°ããNNãäœæããããæ¢åã®NNã䜿çšã§ããŸãã åçŽãªã·ããªãªïŒãã¥ãŒã©ã«ãããã¯ãŒã¯ããã¬ãŒãã³ã°ãããã®åŸäŒæãåããŸããã ããããè¿ãããGoogleãã»ãã·ã§ã³ãéä»ãã«ããã®ã§ã以åã«ä¿åãããã®ãããŒãããå¿
èŠããããŸãããdeleteããã³ã¡ã³ãã¢ãŠããããloadãã®ã³ã¡ã³ããå€ããŸãã
deleteSavedNet(working_path + strModelFileName)
ãã§ãã¯ãã€ã³ãã¯ããã°ã©ã ã®éåžžã«éèŠãªèŠçŽ ã§ãã ãã¬ãŒãã³ã°ã®åæ代ã®çµããã«åŒã³åºãããé¢æ°ã®é
åãäœæãããã§ãã¯ãã€ã³ãã«æž¡ãããšãã§ããŸãã ããšãã°ãæ¢ã«ä¿åãããŠããçµæãããè¯ãçµæã瀺ã
å Žå ããã¥ãŒã©ã«ãããã¯ãŒã¯ãä¿åã§ããŸãã
checkpoint = ModelCheckpoint(working_path + strModelFileName, monitor='val_acc', verbose=1, save_best_only=True, mode='auto', save_weights_only=False) callbacks_list = [ checkpoint ]
æåŸã«ããã¬ãŒãã³ã°ã»ããã§ãã¥ãŒã©ã«ãããã¯ãŒã¯ãåŠç¿ããŸãã
æé«ã®æ§æã®ç²ŸåºŠãšæ倱ã®ã°ã©ãã¯æ¬¡ã®ãšããã§ãã
ã芧ã®ãšããããã¥ãŒã©ã«ãããã¯ãŒã¯ã¯åŠç¿äžã§ãéåžžã«ããŸãæ©èœããŠããŸãã
ãã¥ãŒã©ã«ãããã¯ãŒã¯ã®ãã¹ã
ãã¬ãŒãã³ã°ãå®äºããããçµæããã¹ãããå¿
èŠããããŸãã ãã®ãããNNã¯ããããŸã§ã«èŠãããšã®ãªãåçïŒãã¹ããã©ã«ããŒã«ã³ããŒãããã®ïŒãç¬çš®ããšã«1ææ瀺ããŸãã
ãã¥ãŒã©ã«ãããã¯ãŒã¯ãJavaã¢ããªã±ãŒã·ã§ã³ã«ãšã¯ã¹ããŒããã
ãŸãããã£ã¹ã¯ããã®ãã¥ãŒã©ã«ãããã¯ãŒã¯ã®èªã¿èŸŒã¿ãæŽçããå¿
èŠããããŸãã ãã®çç±ã¯æããã§ãããšã¯ã¹ããŒãã¯å¥ã®ã³ãŒããããã¯ã§è¡ãããããããã¥ãŒã©ã«ãããã¯ãŒã¯ãæé©ãªç¶æ
ã«ãªã£ããšãã«ãšã¯ã¹ããŒããåå¥ã«éå§ããå¯èœæ§ãé«ããªããŸãã ã€ãŸãããšã¯ã¹ããŒãã®çŽåã«ãããã°ã©ã ã®åãå®è¡ã§ããããã¯ãŒã¯ããã¬ãŒãã³ã°ããŸããã ããã«ç€ºãã³ãŒãã䜿çšããå Žåãéãã¯ãªããæé©ãªãããã¯ãŒã¯ãéžæãããŠããŸãã ããããèªåã§äœããåŠãã å Žåããã¹ãŠãä¿åããåã«ãä¿åããåã«ãã¹ãŠãæ°ããèšç·Žããã®ã¯æéã®ç¡é§ã§ãã
åãçç±ã§-ã³ãŒããé£ã³è¶ããªãããã«-ãšã¯ã¹ããŒãã«å¿
èŠãªãã¡ã€ã«ãããã«å«ããŸãã ããªãã®çŸããã®æèŠããããå¿
èŠãšãããªãã°ã誰ãããªããããã°ã©ã ã®å§ãã«ããããåããããšãæ°ã«ããŸããïŒ
from keras.models import Model from keras.models import load_model from keras.layers import * import os import sys import tensorflow as tf
ãã¥ãŒã©ã«ãããã¯ãŒã¯ãããŒãããåŸã®å°ããªãã¹ãããã¹ãŠãããŒããããŠããããšã確èªããããã ãã§ãã
img = image.load_img(working_path + "test/affenpinscher.jpg")
次ã«ããããã¯ãŒã¯ã®å
¥åå±€ãšåºåå±€ã®ååãååŸããå¿
èŠããããŸãïŒãã®é¢æ°ãŸãã¯äœæé¢æ°ã®ããããã§ãå±€ã«æ瀺çã«ãååãä»ãããå¿
èŠããããŸãããè¡ããŸããïŒã
model.summary() >>> Layer (type) >>> ====================== >>> input_7 (InputLayer) >>> ______________________ >>> conv2d_283 (Conv2D) >>> ______________________ >>> ... >>> dense_14 (Dense) >>> ====================== >>> Total params: 22,913,432 >>> Trainable params: 1,110,648 >>> Non-trainable params: 21,802,784
ãã¥ãŒã©ã«ãããã¯ãŒã¯ãJavaã¢ããªã±ãŒã·ã§ã³ã«ã€ã³ããŒããããšãã«ãå
¥åå±€ãšåºåå±€ã®ååã䜿çšããŸãã
ãã®ããŒã¿ãååŸããããã«ãããã¯ãŒã¯ãããŒãã³ã°ããå¥ã®ã³ãŒãïŒ
def print_graph_nodes(filename): g = tf.GraphDef() g.ParseFromString(open(filename, 'rb').read()) print() print(filename) print("=======================INPUT===================") print([n for n in g.node if n.name.find('input') != -1]) print("=======================OUTPUT==================") print([n for n in g.node if n.name.find('output') != -1]) print("===================KERAS_LEARNING==============") print([n for n in g.node if n.name.find('keras_learning_phase') != -1]) print("===============================================") print()
ããããç§ã¯åœŒã奜ãã§ã¯ãªãã圌ãæšèŠããŸããã
次ã®ã³ãŒãã¯ãKeras Neural NetworkãAndroidãããã£ããã£ãã
pb圢åŒã«ãšã¯ã¹ããŒãããŸãã
def keras_to_tensorflow(keras_model, output_dir, model_name,out_prefix="output_", log_tensorboard=True): if os.path.exists(output_dir) == False: os.mkdir(output_dir) out_nodes = [] for i in range(len(keras_model.outputs)): out_nodes.append(out_prefix + str(i + 1)) tf.identity(keras_model.output[i], out_prefix + str(i + 1)) sess = K.get_session() from tensorflow.python.framework import graph_util from tensorflow.python.framework graph_io init_graph = sess.graph.as_graph_def() main_graph = graph_util.convert_variables_to_constants( sess, init_graph, out_nodes) graph_io.write_graph(main_graph, output_dir, name=model_name, as_text=False) if log_tensorboard: from tensorflow.python.tools import import_pb_to_tensorboard import_pb_to_tensorboard.import_to_tensorboard( os.path.join(output_dir, model_name), output_dir)
:
model = load_model(working_path + strModelFileName) keras_to_tensorflow(model, output_dir=working_path + strModelFileName, model_name=working_path + "models/dogs.pb") print_graph_nodes(working_path + "models/dogs.pb")
.
Android
Android . , , , ( ) .
, Android Studio . « », â . activity.
, «assets» (, ).
Gradle
. ,
tensorflow-android . , Tensorflow (, , Keras) Java:
:
versionCode versionName . , Google Play. gdadle (, 1 -> 2 -> 3...) , « ».
, «» â 100 Mb Neural Network , instance «» Facebook .
instance :
<activity android:name=".MainActivity" android:launchMode="singleTask">
android:launchMode=«singleTask» MainActivity, Android, () , , instance.
, , - «» :
<intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> </intent-filter>
, , :
<uses-feature android:name="android.hardware.camera" android:required="true" /> <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove" />
Android, .
Layout .
layouts, , .
Portrait layout .
: (view) , (, «»), «Help», File/Gallery , ( ) «Process» .
, enabling/disabling , .
MainActivity
activity (extends) Android Activity:
public class MainActivity extends Activity
, .
, Bitmap. , Bitmap ( ) (m_bitmap), , 256x256 (m_bitmapForNn). (256) :
static Bitmap m_bitmap = null; static Bitmap m_bitmapForNn = null; private int m_nImageSize = 256;
; (. ), , :
private String INPUT_NAME = "input_7_1"; private String OUTPUT_NAME = "output_1";
TensofFlow. , ( assets):
private TensorFlowInferenceInterface tf;
private String MODEL_PATH =
"file:///android_asset/dogs.pb";
, , :
private String[] m_arrBreedsArray;
, Bitmap. , RGB , â , â , . , (, 120 , ):
private float[] m_arrPrediction = new float[120]; private float[] m_arrInput = null;
tensorflow inference library:
static { System.loadLibrary("tensorflow_inference"); }
, , , « », .
class PredictionTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); }
In onCreate() of the MainActivity, we need to add the onClickListener for the «Process» button:
m_btn_process.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { processImage(); } });
processImage() , :
private void processImage() { try { enableControls(false);
UI- , . , .
instances , (flow of control): «» Facebook, , . , «» onCreate , onCreate .
:
1. onCreate MainActivity, onSharedIntent:
protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); .... onSharedIntent(); ....
onNewIntent:
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); onSharedIntent(); }
onSharedIntent:
private void onSharedIntent() { Intent receivedIntent = getIntent(); String receivedAction = receivedIntent.getAction(); String receivedType = receivedIntent.getType(); if (receivedAction.equals(Intent.ACTION_SEND)) {
onCreate ( ) onNewIntent ( ).
é 匵ã£ãŠ , , «» , «»
.