clementfarabet / torch-ios

Torch7 for iOS.
Other
196 stars 42 forks source link

Load pretrained example not loading network. #24

Closed boerjames closed 3 years ago

boerjames commented 8 years ago

I'm getting this error from the loadWithFileName method from Torch.m

2016-01-11 21:41:03.581 XOR_Example_Pretrained[6466:1132568] error running functionf': ...trained.app/Torch.framework/Resources/torch/File.lua:240: unknown object`

I thought pcall(L,1,0,0) should be changed to pcall(L,1,1,0) but that didn't help. Trying to wrap my mind around this lower level stuff.

Ideas? Or resources so I can figure it out?

soumith commented 8 years ago

In your lua code, before you issue the load, you need to "require" the modules needed to load the network. Like: require "nn" for example. Either that, or one of the layers you have is not present in the torch-ios version of nn, which is a bit out of date.

What's the model that you're trying to load?

boerjames commented 8 years ago

I'm using the included model, generated from the code in ios_lua_scripts/XOR_Example_Scripts/create_xor_net.lua.

I've added require 'nn' to main.lua, same problem.

kurtjacobsdev commented 8 years ago

xor_model.net.zip

the require 'MODULE_NAME' is not required in the lua script, the packages are already being loaded. I think Soumith is spot on with the notion that the layer has been modified in the latest version of the nn package for PC. I've attached a copy of the neural network generated with an earlier version of the nn package for PC.

The nn package for iOS needs to be updated but in the meantime you could checkout an earlier revision of the nn package for desktop and generate your models using that?

boerjames commented 8 years ago

Ok yes, I see the Torch modules are loaded in Torch.m. I tried again using the model you provided, same problem.

My master's project involves eye cancer detection, landed on Torch, need to update our iOS and Android apps to use the network we train, unless there is a better option. How much work would it take to get these packages updated with the master Torch branch?

soumith commented 8 years ago

@boerjames it's actually fairly simple. Copy the files over from https://github.com/torch/torch7 into https://github.com/clementfarabet/torch-ios/tree/ios/pkg/torch

Similarly for other folders in pkg/* and lib/, exe/ and 3rdparty/*

Then you'll have to fix any build issues, which is to play around with the build scripts.

I am going to be doing this around the 25th for torch-android, and if you dont get around to it by then i'll try to do the same for torch-ios as well (but no promises).

boerjames commented 8 years ago

Awesome, I'll work on that in the coming days. Thanks man.

boerjames commented 8 years ago

@soumith Any update on this or Android? I monkeyed around with the iOS torch but couldn't get it working, I don't have the necessary knowledge of the things underneath torch to get it working myself :(

soumith commented 8 years ago

yes, yesterday i updated the android build to work out of the box with torch-distro so that we never have to worry about maintenance. On monday i'll push some of the patches upstream and commit it. I can tackle torch-ios next week, now that my memory is very fresh about things to do.

boerjames commented 8 years ago

@soumith wait, does the android torch work now? I didn't see a push on android-torch but it looks like another user has it working?

soumith commented 8 years ago

See the refactor branch

creinders commented 8 years ago

I was struggling with the same problem and I figured out that the example is working in the simulator but not on the device (iPhone 5). I thought that it might have something to do with the encoding, so I change the format from binary to ascii torch.save("xor_model.net", mlp,"ascii") in the file ios_lua_scripts/XOR_Example_Scripts/create_xor_net.lua

Then I copied the xor_model.net file into the the XOR_Example_LoadPretrained project and now it is working! I hope that helps you as well!

boerjames commented 8 years ago

@creinders Yes, but the problem is that it doesn't work with networks trained on new versions of torch. Any progress on the iOS update @soumith ?

soumith commented 8 years ago

i pretty much haven't touched it, and i'm going through a pretty intense work phase, so likely wont touch it. torch-android works now, and someone gotta hopefully model torch-ios starting from there. sorry James :(

romkri commented 8 years ago

Here my solution: just create function call that send path to main bundle to lua function as string argument, and then load model with correct path ...


  lua_State *L = [self.t getLuaState]; 

     lua_getfield(L,
                    LUA_GLOBALSINDEX,
                    [@"LoadModel" cStringUsingEncoding:NSASCIIStringEncoding]
                 );

    lua_pushstring(L, [
                       [[NSBundle mainBundle] resourcePath]
                        cStringUsingEncoding:NSASCIIStringEncoding
                       ]);

in lua:


function LoadModel(path)
   net =  torch.load(path.."/myModelFile.t7")    
end
chankyu-choi commented 8 years ago

It depends on cpu architecture. Check your model whether it saved as 32bit or 64bit. How about saving your model as b32 because iPhone5 is 32bit architecture.

michaelhuang74 commented 7 years ago

Has the original issue posted by boerjames been resolved?

I have the same issue. I tried to load the pre-trained model files, which were generated on 64-bit desktop computer with Ubuntu 14.04, on simulators of Android and iOS. The pre-trained files are of the size of 20MB. The error was "File.lua:168: read error: read 0 blocks instead of 1".

I have tried all possible methods I could find online, for example, using "ascii" mode for saving and loading, using exact path for the model file, providing the "b64" or "apkbinary64" for loading, but no luck so far. On both simulators, I was able to load the sample model file with the torch7-android, but could not load the model files I generated on 64-bit desktop computers.

Is there anyone who had successfully loaded the pre-trained model files on either android or iOS and could provide some help?

Thanks and Regards, Michael

boerjames commented 3 years ago

Closed because I'm cleaning up old issues and this project is dead.