From 94f360e06f8a2110082fabc774d5ec57e8d6f156 Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Thu, 9 Jul 2020 14:09:51 +0200 Subject: [PATCH] Cleann the tensorflow-common dependencies --- common/tensorflow-common/pom.xml | 58 ++++++------------- .../tensorflow/AbstractGraphRunner.java | 9 ++- .../fn/common/tensorflow/GraphRunner.java | 7 ++- .../tensorflow/deprecated/GraphicsUtils.java | 8 ++- .../tensorflow/util/CachedModelExtractor.java | 14 ++--- .../tensorflow/util/ModelExtractor.java | 26 +++++---- 6 files changed, 52 insertions(+), 70 deletions(-) diff --git a/common/tensorflow-common/pom.xml b/common/tensorflow-common/pom.xml index 4355d404..8724da07 100644 --- a/common/tensorflow-common/pom.xml +++ b/common/tensorflow-common/pom.xml @@ -19,15 +19,23 @@ true - 5.1.6.RELEASE + 1.15.0 + 5.2.7.RELEASE 2.11.0 1.20 2.7 - 1.15.0 + 3.10 + 3.0.3 + 1.7.26 - + + org.springframework + spring-core + ${spring-core.version} + + org.tensorflow tensorflow @@ -50,6 +58,11 @@ commons-io ${commons-io.version} + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + com.fasterxml.jackson.core @@ -69,45 +82,12 @@ org.slf4j slf4j-api - 1.7.26 + ${slf4j-api.version} org.pcollections pcollections - 3.0.3 - - - - org.springframework.boot - spring-boot-starter-validation - - - org.springframework - spring-messaging - - - org.springframework.boot - spring-boot-starter-webflux - - - org.springframework.boot - spring-boot-starter-test - test - - - org.junit.vintage - junit-vintage-engine - - - - - io.projectreactor - reactor-test - - - org.springframework.boot - spring-boot-configuration-processor - provided + ${pcollections.version} @@ -120,6 +100,4 @@ - - diff --git a/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/AbstractGraphRunner.java b/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/AbstractGraphRunner.java index c3bd6335..2958df5e 100644 --- a/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/AbstractGraphRunner.java +++ b/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/AbstractGraphRunner.java @@ -22,11 +22,10 @@ import java.util.List; import java.util.Map; import java.util.function.Function; +import org.apache.commons.lang3.Validate; import org.tensorflow.Session; import org.tensorflow.Tensor; -import org.springframework.util.Assert; - /** * @author Christian Tzolov */ @@ -103,8 +102,8 @@ public abstract class AbstractGraphRunner implements Function feedNames, String fetchedName) { super(feedNames, Arrays.asList(fetchedName)); } + public GraphRunner(String feedName, List fetchedNames) { super(Arrays.asList(feedName), fetchedNames); } @@ -68,7 +69,7 @@ public class GraphRunner extends AbstractGraphRunner implements AutoCloseable { } public GraphRunner withGraphDefinition(GraphDefinition graphDefinition) { - Assert.isNull(this.savedModelBundle, "Either SavedModel or GraphDefinition can be set! " + + Validate.isTrue(this.savedModelBundle == null, "Either SavedModel or GraphDefinition can be set! " + "SavedModelBundle is found: " + this.savedModelBundle); this.autoCloseableSession = new AutoCloseableSession() { @@ -82,7 +83,7 @@ public class GraphRunner extends AbstractGraphRunner implements AutoCloseable { } public GraphRunner withSavedModel(String savedModelDir, String... tags) { - Assert.isNull(this.autoCloseableSession, "Either SavedModel or GraphDefinition can be set! " + + Validate.isTrue(this.autoCloseableSession == null, "Either SavedModel or GraphDefinition can be set! " + "AutoCloseableSession is found: " + this.autoCloseableSession); this.savedModelBundle = SavedModelBundle.load(savedModelDir, tags); return this; diff --git a/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/deprecated/GraphicsUtils.java b/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/deprecated/GraphicsUtils.java index f3d51c0f..246b14a4 100644 --- a/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/deprecated/GraphicsUtils.java +++ b/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/deprecated/GraphicsUtils.java @@ -33,9 +33,11 @@ import java.io.InputStream; import javax.imageio.ImageIO; +import org.apache.commons.io.IOUtils; + import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; -import org.springframework.util.StreamUtils; + /** * Utility class used to provide some handy image manipulation functions. Among others it can provide contrast colors @@ -617,7 +619,7 @@ public final class GraphicsUtils { public static byte[] toImageToBytes(String imageUri) throws IOException { try (InputStream is = new DefaultResourceLoader().getResource(imageUri).getInputStream()) { - return StreamUtils.copyToByteArray(is); + return IOUtils.toByteArray(is); } } @@ -630,7 +632,7 @@ public final class GraphicsUtils { public static byte[] loadAsByteArray(String resourceUri) throws IOException { Resource expectedPoseResponse = new DefaultResourceLoader().getResource(resourceUri); try (InputStream is = expectedPoseResponse.getInputStream()) { - return StreamUtils.copyToByteArray(is); + return IOUtils.toByteArray(is); } } diff --git a/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/util/CachedModelExtractor.java b/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/util/CachedModelExtractor.java index 5b8cfbe0..f3414984 100644 --- a/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/util/CachedModelExtractor.java +++ b/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/util/CachedModelExtractor.java @@ -19,15 +19,15 @@ package org.springframework.cloud.fn.common.tensorflow.util; import java.io.File; import java.io.FileInputStream; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.Validate; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; -import org.springframework.util.Assert; -import org.springframework.util.FileCopyUtils; -import org.springframework.util.StreamUtils; -import org.springframework.util.StringUtils; /** * Extends the {@link ModelExtractor} to allow keeping a local copy (cache) of the loaded model (protobuf) files. @@ -72,7 +72,7 @@ public class CachedModelExtractor extends ModelExtractor { rootFolder.mkdirs(); } - Assert.isTrue(rootFolder.isDirectory(), "The cache root folder must be a Directory"); + Validate.isTrue(rootFolder.isDirectory(), "The cache root folder must be a Directory"); String fileName = modelResource.getFilename(); String fragment = modelResource.getURI().getFragment(); @@ -80,13 +80,13 @@ public class CachedModelExtractor extends ModelExtractor { new File(rootFolder, fileName + "_" + fragment); if (cachedFile.exists()) { logger.info("Load model " + modelResource.toString() + " from cache: " + cacheRootDirectory); - return StreamUtils.copyToByteArray(new FileInputStream(cachedFile)); + return IOUtils.toByteArray(new FileInputStream(cachedFile)); } byte[] model = super.getModel(modelResource); // cache the file - FileCopyUtils.copy(model, cachedFile); + FileUtils.writeByteArrayToFile(cachedFile, model); logger.info("Caching the " + modelResource.toString() + " model at: " + cachedFile); return model; diff --git a/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/util/ModelExtractor.java b/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/util/ModelExtractor.java index 9d7a3f3d..16502410 100644 --- a/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/util/ModelExtractor.java +++ b/common/tensorflow-common/src/main/java/org/springframework/cloud/fn/common/tensorflow/util/ModelExtractor.java @@ -36,12 +36,13 @@ import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.compressors.CompressorInputStream; import org.apache.commons.compress.compressors.CompressorStreamFactory; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.Validate; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; -import org.springframework.util.Assert; -import org.springframework.util.StreamUtils; -import org.springframework.util.StringUtils; + /** * Extracts a pre-trained (frozen) Tensorflow model URI into byte array. The 'http://', 'file://' and 'classpath://' @@ -79,7 +80,7 @@ public class ModelExtractor { public byte[] getModel(Resource modelResource) { - Assert.notNull(modelResource, "Not null model resource is required!"); + Validate.notNull(modelResource, "Not null model resource is required!"); try (InputStream is = modelResource.getInputStream(); InputStream bi = new BufferedInputStream(is)) { @@ -88,27 +89,28 @@ public class ModelExtractor { String compressor = archiveCompressor[1]; String fragment = modelResource.getURI().getFragment(); - if (StringUtils.hasText(compressor)) { + + if (StringUtils.isNotBlank(compressor)) { try (CompressorInputStream cis = new CompressorStreamFactory().createCompressorInputStream(compressor, bi)) { - if (StringUtils.hasText(archive)) { + if (StringUtils.isNotBlank(archive)) { try (ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(archive, cis)) { // Compressor fromMemory Archive return findInArchiveStream(fragment, ais); } } else { // Compressor only - return StreamUtils.copyToByteArray(cis); + return IOUtils.toByteArray(cis); } } } - else if (StringUtils.hasText(archive)) { // Archive only + else if (StringUtils.isNotBlank(archive)) { // Archive only try (ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(archive, bi)) { return findInArchiveStream(fragment, ais); } } else { // No compressor nor Archive - return StreamUtils.copyToByteArray(bi); + return IOUtils.toByteArray(bi); } } catch (Exception e) { @@ -131,9 +133,9 @@ public class ModelExtractor { //System.out.println(entry.getName() + " : " + entry.isDirectory()); if (archive.canReadEntryData(entry) && !entry.isDirectory()) { - if ((StringUtils.hasText(modelFileNameInArchive) && entry.getName().endsWith(modelFileNameInArchive)) || - (!StringUtils.hasText(modelFileNameInArchive) && entry.getName().endsWith(this.frozenGraphFileExtension))) { - return StreamUtils.copyToByteArray(archive); + if ((StringUtils.isNotBlank(modelFileNameInArchive) && entry.getName().endsWith(modelFileNameInArchive)) || + (!StringUtils.isNotBlank(modelFileNameInArchive) && entry.getName().endsWith(this.frozenGraphFileExtension))) { + return IOUtils.toByteArray(archive); } } }