ラズパイでkerasのimagenetモデルを使ってみる。

#概要
ラズパイでkerasのimagenetモデルを使ってみる。

#写真

![](https://camo.qiitausercontent.com/e0f4a17a2975f2d1872173cdd099b138ab706779/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f31383130342f61646532636635382d626564612d613131352d313631302d6261306233366539626636302e6a706567)


#Xceptionの場合

```
('Predicted:', [(u'n04179913', u'sewing_machine', 1.0), (u'n15075141', u'toilet_tissue', 0.0), (u'n02317335', u'starfish', 0.0)])
Time elapsed: 88
```


サンプルコード


```
from tensorflow.contrib.keras.python.keras.applications.xception import Xception
from tensorflow.contrib.keras.python.keras.preprocessing import image
from tensorflow.contrib.keras.python.keras.applications.imagenet_utils import preprocess_input, decode_predictions
import numpy as np
import time

img_path = 'p1m.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)
begin = time.clock()
model = Xception(weights = 'imagenet')
preds = model.predict(x)
print ('Predicted:', decode_predictions(preds, top = 3)[0])
print ('Time elapsed: %.0f' % (time.clock() - begin))
```

#ResNet50の場合

```
('Predicted:', [(u'n02098286', u'West_Highland_white_terrier', 1.0), (u'n15075141', u'toilet_tissue', 0.0), (u'n02319095', u'sea_urchin', 0.0)])
Time elapsed: 94
```

サンプルコード

```
from tensorflow.contrib.keras.python.keras.applications.resnet50 import ResNet50
from tensorflow.contrib.keras.python.keras.preprocessing import image
from tensorflow.contrib.keras.python.keras.applications.imagenet_utils import preprocess_input, decode_predictions
import numpy as np
import time

img_path = 'p1m.jpg'
img = image.load_img(img_path, target_size = (224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
x = preprocess_input(x)
begin = time.clock()
model = ResNet50(weights = 'imagenet')
preds = model.predict(x)
print ('Predicted:', decode_predictions(preds, top = 3)[0])
print ('Time elapsed: %.0f' % (time.clock() - begin))
```

#InceptionV3の場合

```
('Predicted:', [(u'n01924916', u'flatworm', 0.7561847), (u'n04328186', u'stopwatch', 0.21795489), (u'n06359193', u'web_site', 0.01172213)])
Time elapsed: 123
```

#サンプルコード

```
from tensorflow.contrib.keras.python.keras.applications.inception_v3 import InceptionV3
from tensorflow.contrib.keras.python.keras.preprocessing import image
from tensorflow.contrib.keras.python.keras.applications.imagenet_utils import preprocess_input, decode_predictions
import numpy as np
import time

img_path = 'p1m.jpg' #0.png 1.png
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)
begin = time.clock()
model = InceptionV3(weights = 'imagenet')
preds = model.predict(x)
print ('Predicted:', decode_predictions(preds, top = 3)[0])
print ('Time elapsed: %.0f' % (time.clock() - begin))
```


#VGG16の場合

```
Resource exhausted: OOM when allocating tensor with shape[25088,4096]
強制終了

```

#サンプルコード

```
from tensorflow.contrib.keras.python.keras.applications.vgg16 import VGG16
from tensorflow.contrib.keras.python.keras.preprocessing import image
from tensorflow.contrib.keras.python.keras.applications.imagenet_utils import preprocess_input, decode_predictions
import numpy as np
import time

img_path = 'p1m.jpg' #0.png 1.png
img = image.load_img(img_path, target_size = (224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
x = preprocess_input(x)
begin = time.clock()
model = VGG16(weights = 'imagenet')
preds = model.predict(x)
print ('Predicted:', decode_predictions(preds, top = 3)[0])
print ('Time elapsed: %.0f' % (time.clock() - begin))
```


以上。