Open Source For You — December 2017

(Steven Felgate) #1
How To Developers

http://www.OpenSourceForU.com | OPEN SOURCE FOR YOU | DECEMBER 2017 | 65

Image classification with pre-trained models
An image classification code with pre-trained ResNet50 is as
follows (https://keras.io/applications/):

from keras.applications.resnet50
import ResNet50 from keras.preprocessing
import image from keras.applications.resnet50
import preprocess_input, decode_predictions
import numpy as np

model = ResNet50(weights=’imagenet’)
img_path = ‘elephant.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)
preds = model.predict(x)

# decode the results into a list of tuples (class,
description, probability) # (one such list for each sample in

the batch)
print(‘Predicted:’, decode_predictions(preds, top=3)[0])

# Predicted: [(u’n02504013’, u’Indian_elephant’, 0.82658225),
(u’n01871265’, u’tusker’, 0.1122357), (u’n02504458’,
u’African_elephant’, 0.061040461)]

The simplicity with which the classification tasks are
carried out can be inferred from the above code.
Overall, Keras is a simple, extensible and easy-to-
implement neural network API, which can be used to build
deep learning applications with high level abstraction.

By: Dr K.S. Kuppusamy
The author is an assistant professor of computer science
at the School of Engineering and Technology, Pondicherry
Central University. He has 12+ years of teaching and research
experience in academia and in industry. He can be reached at
[email protected].

All the clients you find on GitHub implement this
signature technique, so you should not have to do it manually.
Now that you have explored the API through the UI and you
understand how to make low level calls, pick your favourite
client or use CloudMonkey. This is a sub-project of Apache
CloudStack and gives operators/developers the ability to use
any of the API methods.
Testing the AWS API interface: While the native
CloudStack API is not a standard, CloudStack provides an
AWS EC2 compatible interface. A great advantage of this is
that existing tools written with EC2 libraries can be reused
against a CloudStack based cloud. In the installation section,
we described how to run this interface by installing packages.
In this section, we find out how to compile the interface with
Maven and test it with the Python Boto module.
Using a running management server (with DevCloud for
instance), start the AWS API interface in a separate shell with
the following command:


mvn -Pawsapi -pl :cloud-awsapi jetty:run


Log into the CloudStack UI http://localhost:8080/client, go
to Service Offerings and edit one of the compute offerings to have
the name m1.small or any of the other AWS EC2 instance types.
With access and secret keys generated for a user, you should
now be able to use the Python Boto module:


import boto
import boto.ec2


accesskey=”2IUSA5xylbsPSnBQ


FoWXKg3RvjHgsufcKhC1SeiCbeEc0obKwUlwJamB_
gFmMJkFHYHTIafpUx0pHcfLvt-dzw”
secretkey=”oxV5Dhhk5ufNowey 7OVHgWxCBVS4deTl9qL0EqMthfP
Buy3ScHPo2fifDxw1aXeL5cyH10hnLOKjyKphcXGeDA”

region = boto.ec2.regioninfo.RegionInfo(name=”ROOT”,
endpoint=”localhost”)
conn = boto.connect_ec2(aws_access_key_id=accesskey, aws_
secret_access_key=secretkey, is_secure=False, region=region,
port=7080, path=”/awsapi”, api_version=”2012-08-15”)

images=conn.get_all_images()
print images
res = images[0].run(instance_type=’m1.small’,security_
groups=[‘default’])

Note the new api_version number in the connection
object, and also note that there was no need to perform a user
registration as in the case of previous CloudStack releases.
Let us thank those at Apache for contributing yet another
outstanding product to the open source community, along
with the detailed documentation they have provided for
CloudStack. All the contents, samples and pictures in this
article are extracts from CloudStack online documentation
and you may explore more about it at http://docs.cloudstack.
apache.org/en/latest/.

By: Somanath T.V.
The author has 14+ years’ experience in the IT industry and
is currently doing research in machine learning and related
areas, along with his tenure at SS Consulting, Kochi. He can
be reached at [email protected].

Continued from page...61

Free download pdf