How to do it...

  1. Persist the model using ModelSerializer:
File file = new File("cnntrainedmodel.zip");
ModelSerializer.writeModel(model,file,true);
ModelSerializer.addNormalizerToModel(file,scaler);
  1. Restore the trained model using ModelSerializer to perform predictions:
MultiLayerNetwork network = ModelSerializer.restoreMultiLayerNetwork(modelFile);
NormalizerStandardize normalizerStandardize = ModelSerializer.restoreNormalizerFromFile(modelFile);
  1. Design an API method that accepts inputs from users and returns results. An example API method would look like the following:
public static INDArray generateOutput(File file) throws IOException, InterruptedException {
final File modelFile = new File("cnnmodel.zip");
final MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(modelFile);
final RecordReader imageRecordReader = generateReader(file);
final NormalizerStandardize normalizerStandardize = ModelSerializer.restoreNormalizerFromFile(modelFile);
final DataSetIterator dataSetIterator = new RecordReaderDataSetIterator.Builder(imageRecordReader,1).build();
normalizerStandardize.fit(dataSetIterator);
dataSetIterator.setPreProcessor(normalizerStandardize);
return model.output(dataSetIterator);
}
  1. Create a URI mapping to service client requests, as shown in the following example:
@GetMapping("/")
public String main(final Model model){
model.addAttribute("message", "Welcome to Java deep learning!");
return "welcome";
}

@PostMapping("/")
public String fileUpload(final Model model, final @RequestParam("uploadFile")MultipartFile multipartFile) throws IOException, InterruptedException {
final List<String> results = cookBookService.generateStringOutput(multipartFile);
model.addAttribute("message", "Welcome to Java deep learning!");
model.addAttribute("results",results);
return "welcome";
}
  1. Build a cookbookapp-cnn project and add the API dependency to your Spring project:
<dependency>
<groupId>com.javadeeplearningcookbook.app</groupId>
<artifactId>cookbookapp-cnn</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
  1. Create the generateStringOutput() method in the service layer to serve API content:
@Override
public List<String> generateStringOutput(MultipartFile multipartFile) throws IOException, InterruptedException {
//TODO: MultiPartFile to File conversion (multipartFile -> convFile)
INDArray indArray = ImageClassifierAPI.generateOutput(convFile);

for(int i=0; i<indArray.rows();i++){
for(int j=0;j<indArray.columns();j++){
DecimalFormat df2 = new DecimalFormat("#.####");
results.add(df2.format(indArray.getDouble(i,j)*100)+"%");
//Later add them from list to the model display on UI.
}
}
convFile.deleteOnExit();
return results;
}

  1. Download and install the Google Cloud SDK: https://cloud.google.com/sdk/.
  1. Install the Cloud SDK app-engine-java component by running the following command on the Google Cloud console:
gcloud components install app-engine-java
  1. Log in and configure Cloud SDK using the following command:
gcloud init
  1. Add the following dependency for Maven App Engine in pom.xml:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.1.0</version>
</plugin>
  1. Create an app.yaml file in your project as per the Google Cloud documentation:
    https://cloud.google.com/appengine/docs/flexible/java/configuring-your-app-with-app-yaml.
  2. Navigate to Google App Engine and click on the Create Application button:

  1. Pick a region and click on Create app:

  1. Select Java and click the Next button:

Now, your app engine has been created at Google Cloud.

  1. Build the spring boot application using Maven:
mvn clean install
  1. Deploy the application using the following command:
mvn appengine:deploy
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset