Cannot load Tensorflow model in java using SavedModelBundle - java

I am trying to load mobilenet model from this link. The model is stored on disk at the following path
C:\Users\userName\IdeaProjects\ImageClassification\src\main\resources\mobilenet_v2_1.4_224\mobilenet_v2_1.4_224.pb
Following is the code which is located at this path C:\Users\userName\IdeaProjects\ImageClassification\src\main\java\com\DeepLearning\ImgClassification.java
try(SavedModelBundle modelBundle = SavedModelBundle.load("src/main/resources/mobilenet_v2_1.4_224", "serve")){
Session mobileNetSession = modelBundle.session();
System.out.println(mobileNetSession);
}catch (TensorFlowException t){
System.out.println(t.getMessage());
}
I am trying to load the .pb using SavedModelBundle.load(), but I get the following error
2020-07-31 12:40:35.385591: I tensorflow/cc/saved_model/reader.cc:31] Reading SavedModel from: src/main/resources/mobilenet_v2_1.4_224
2020-07-31 12:40:35.570627: I tensorflow/cc/saved_model/reader.cc:54] Reading meta graph with tags { serve }
2020-07-31 12:40:35.577560: I tensorflow/cc/saved_model/loader.cc:311] SavedModel load for tags { serve }; Status: fail. Took 191903 microseconds.
Could not find meta graph def matching supplied tags: { serve }. To inspect available tag-sets in the SavedModel, please use the SavedModel CLI: `saved_model_cli`
How do I resolve the path of the model?
What am I mising here?
Thanks.

Related

How do I use JCEF with Intellij?

I've been trying to figure out if there is a way to use Chromium browser as the UI for a Java application. I found this IntelliJ page: https://jetbrains.org/intellij/sdk/docs/reference_guide/jcef.html The thing I can't figure out is how I actually use this in my project. My IntelliJ version is 2020.3 and it says that in 2020.2 JCEF was enabled by default. I however cannot figure out how I use JCEF in my project. I can't seem to find any clear documentation. When I try to import, for example, com.intellij it can't find the package.
Are there any tutorials or guides to integrate JCEF in my IntelliJ project?
You can run jetty server or use resource provider.
Example of resource provider:
https://medium.com/virtuslab/creating-intellij-plugin-with-webview-3b27c3f87aea
Also this example of abstract WebDialog explain how to pass data to fe:
https://github.com/sergeysenja1992/xm-online-idea-plugin/blob/master/src/main/kotlin/com/icthh/xm/actions/WebDialog.kt
All magic in class BrowserPipe (WebDialog.kt file) on backend side, and same class in frontend class
https://github.com/sergeysenja1992/xm-online-idea-plugin/blob/master/src/main/webapp/src/index.html
Next js file it's one more part of magic
<script src="http://registercallback/events.js"></script>
This js file does not exists, but be listen this request and return generated js code.
CefApp.getInstance().registerSchemeHandlerFactory("http", "registercallback", InjectJsHandlerFactory(inject()))
For more details pls look to this line of code in (WebDialog.kt file)
After all manipulation as result i have ability to write components in simple way:
FE: https://github.com/sergeysenja1992/xm-online-idea-plugin/blob/master/src/main/webapp/src/app/settings/settings.component.ts
constructor(private zone: NgZone) {
let w: any = window;
w.messagePipe.subscribe('initData', (res) => {
console.info('initData', res);
zone.run(() => {
this.updateData(res);
});
});
w.messagePipe.post('componentReady', 'SettingsComponent ready')
}
BE: https://github.com/sergeysenja1992/xm-online-idea-plugin/blob/master/src/main/kotlin/com/icthh/xm/actions/settings/SettingsDialog.kt
override fun callbacks(): List<BrowserCallback> {
val data = ArrayList(project.getSettings().envs.map { it.copy() })
this.data = data;
return listOf(
BrowserCallback("componentReady") {body, pipe ->
logger.info("Update ${body}")
pipe.post("initData", mapper.writeValueAsString(mapOf(
"updateModes" to updateModes,
"branches" to project.getRepository().getLocalBranches(),
"envs" to data,
)))
},
BrowserCallback("envsUpdated") {body, pipe ->
logger.info("envsUpdated ${body}")
val envs = mapper.readValue<List<EnvironmentSettings>>(body)
this.data = ArrayList(envs);
}
)
}

Parse Swagger with refs inside jar with openapi4j

I'm trying to parse the following swagger file with openapi4j:
{
"openapi" : "3.0.0",
"info" : {
"title" : "My Service",
"version" : "1.0.0"
},
"paths" : {
"/endpoint" : { "$ref" : "swagger2.json#/paths/get_endpoint" },
}
}
You can see it has a simple ref to another file within the same folder.
I parse the Swagger file with the following:
URL url = Thread.currentThread().getContextClassLoader().getResource(filePath);
openAPI = new OpenApi3Parser().parse(url, false);
Locally in my IDE, this works great. The ref is loaded and I am able to validate requests against it with no issues. However, when I jar up the project, it is able to load the initial swagger file fine, but none of the refs. I get the following error:
StackTrace: org.openapi4j.core.exception.ResolutionException: Failed to load document from 'swagger2.json'
at org.openapi4j.core.model.reference.AbstractReferenceResolver.registerDocument(AbstractReferenceResolver.java:118)
at org.openapi4j.core.model.reference.AbstractReferenceResolver.findReferences(AbstractReferenceResolver.java:92)
at org.openapi4j.core.model.reference.AbstractReferenceResolver.resolve(AbstractReferenceResolver.java:53)
at org.openapi4j.core.model.v3.OAI3Context.resolveReferences(OAI3Context.java:103)
at org.openapi4j.core.model.v3.OAI3Context.<init>(OAI3Context.java:73)
at org.openapi4j.core.model.v3.OAI3Context.<init>(OAI3Context.java:47)
at org.openapi4j.parser.OpenApi3Parser.parse(OpenApi3Parser.java:34)
at org.openapi4j.parser.OpenApi3Parser.parse(OpenApi3Parser.java:18)
at org.openapi4j.parser.OpenApiParser.parse(OpenApiParser.java:53)
I'm not sure if this is possible. I assume I may have to copy my project resources out at runtime to the filesystem somewhere to be accessed more easily. I would like to avoid that route if possible.
Turns out this was a bug in openapi4j, which was resolving refs with URI instead of URL. This has been fixed in 0.9

saved .pb file(created using darkflow) not loading in java

I used the following command to create a .pb file:
flow --model ../YOLOv2/alexeyAB_darknet/darknet-master/cfg/yolov2-dppedestrian.cfg --load ../YOLOv2/alexeyAB_darknet/darknet-master/backup/yolov2-dppedestrian_33900.weights --savepb
Although the model was created successfully, when I load it into my java tensorflow application, I get the following error:
Exception in thread "Thread-9" org.tensorflow.TensorFlowException: Could not find meta graph def matching supplied tags: { serve }. To inspect available tag-sets in the SavedModel, please use the SavedModel CLI: saved_model_cli
The problem is in the second line of code:
String model_path = "/home/adisys/Desktop/cloudiV2/models/yolo_pedestrian/saved_model";
SavedModelBundle model = SavedModelBundle.load(model_path, "serve");
I tried digging deep and found this link:
Can not load pb file in tensorflow serving
Following the link I ran the following command:
saved_model_cli show --dir saved_model/
The output is as follows:
/home/adisys/anaconda3/lib/python3.6/site-packages/h5py/init.py:34: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type. from ._conv import register_converters as _register_converters
The given SavedModel contains the following tag-sets:
As can be seen, there were no tag-sets displayed.
What could be the issue?
I just saw your post, I'm sure the problem has solved itself now, but I'm leaving the comment for others working with darkflow. The command --savepb needs to be assigned as --savepb True

Jxbtrowser retrieve platform specific artefact at runtime

I'm writing an intelij plugin and would like to download the platform specific artefact at runtime.
I've loaded the platform specific jar into a class loader but the ChromiumExtractor cannot access the nested resources when prefixed with "/". So I can access the resource as "chromium-mac.zip" but the library cannot.
I've tried to unzip the nested zipped chromium artefact into the correct directory but this does not leading to a working solution. So now I've been trying to piece together the way the library extracts the artefact but it's rather tedious as the code is obfuscated.
Does the jxbrowser plugin have some support for retrieving the artefact at runtime. Could such support be added (jxbtrowser devs use SO for support questions etc, this is a message to them :D ) ?
Approach taken :
// inside intelij plugin . The plugin has the jxbrowser-6.6.jar
// and license.jar loaded into the classloader. the platform specific
// artefact will be retrieved manual).
val cl = URLClassLoader(arrayOf(URL("file://.../jxbrowser-mac-6.6.jar")), Browser::class.java.classLoader)
val backup = Thread.currentThread().contextClassLoader
try {
Thread.currentThread().contextClassLoader = cl
// can access like this
Thread.currentThread().contextClassLoader.getResource("chromium-mac.zip")
val ce = ChromiumExtractor.create()
// cannot access as resource is retrieved "/chromium-mac.zip" ?
ce.extract(BrowserPreferences.getChromiumDir())
browser = Browser()
} finally {
Thread.currentThread().contextClassLoader = backup
}
The following does the trick, The resource jar had to be in the same class loader as the client jar (as well as the license). It would be nice if JxBrowser added a helper for this that is capable of performing the download and initialising chromium, perhaps taking just a path for a persistent storage directory.
private fun initializeJxBrowser(): Browser {
if(ChromiumExtractor.create().shouldExtract(BrowserPreferences.getChromiumDir())) {
val cl = URLClassLoader(arrayOf(
URL("file:.../license.jar"),
URL("file:.../jxbrowser-mac-6.6.jar"),
URL("file:../jxbrowser-6.6.jar")
))
cl.loadClass("com.teamdev.jxbrowser.chromium.BrowserContext")
.getMethod("defaultContext")
.invoke(null)
}
return Browser()
}

getting error : java.lang.UnsatisfiedLinkError: Native Library C:\opencv\build\java\x64\opencv_java300.dll

I am running a servlet program to read an image using opencv,
getting error :
java.lang.UnsatisfiedLinkError: Native Library C:\opencv\build\java\x64\opencv_java300.dll already loaded in another classloader . When restarting the IDE it works fine.
I loaded System.loadLibrary ( Core.NATIVE_LIBRARY_NAME ) ; in servlet only ones.
Can anybody suggest a solution for how to unload it. And also anybody know how to read an image from browser using opencv in java.?
It is because the library is not in the system path, it needs to first added to the system path, then load. First extract the OpenCV to C drive something like this c:\opencv\... then use this code below to during initializing, it will automatically load the OpenCV lib in windows environment.
public static void loadOpenCV_Lib() throws Exception {
String model = System.getProperty("sun.arch.data.model");
String libraryPath = "C:/opencv/build/java/x86/";
if(model.equals("64")) {
libraryPath = "C:/opencv/build/java/x64/";
}
System.setProperty("java.library.path", libraryPath);
Field sysPath = ClassLoader.class.getDeclaredField("sys_paths");
sysPath.setAccessible(true);
sysPath.set(null, null);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
And also it will automatically detect the system model and load the lib according to the system model.

Categories