New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to load pre-trained weights? #4
Comments
I also wanted to play around with the pre-trained weights of the holistic mode, so i downloaded 'vgg16_weights_th_dim_ordering_th_kernels_Holistic_91.11.h5' I used Keras with Tensorflow and assumed that using the vgg16 from keras.applications should work
Turns out you don't even have to convert the weights from theano to tensorflow on your own, since keras does this internally in model.load_weights (which is called inside vgg16 if you provide a weightsfile). Initialization of the model + loading the weights seem to work, i didn't get any errors. Beeing suspicious about the weight-conversion, i set up a new project and installed keras with theano. And again, loading the model with weights worked but all test-images were classified as memo. In 'IV-B Preprocessing' of the paper it is said that:
Can someone clarify what "standardized" means? Mean Pixel Substraction? Rescaling? I would appreciate if someone could confirm that the provided weights actually work. |
Tks @lgaida, I successfully load the trained weights as you suggest. My input is preprocessed as:
But when I tried to predict with
|
@hiepph too bad 😢 |
By "standardized", we mean subtract the mean and divide by the standard deviation. Regarding the data loading issues, I can try to look into our old code and configurations and try to elaborate. |
I can confirm however that everything we did was using theano as the backend. So the input dimensions as well as the weights are in theano ordering. If you are using tensorflow as the backend, then you have to either switch backends to theano or change weight orderings for everything to work I think.
I cannot however neither confirm nor deny this since I have not worked with that functionality myself. |
Hi @saikat-roy thank you for replying 👍 |
Hey @lgaida. We apologize for not replying sooner but the source code of the project was never really written for what you might call, public consumption (also known as, it's an absolute mess) so we are scrambling to dig it out of storage.
I am digging through our old files and this is the preprocessing snippet that I found we had used. I should however warn you that the |
Thanks for replying so quickly. I'm going to play around with your code snippet, and i'm currently implementing something very similar on my own. To reduce even more assumptions: |
While the first case you suggested might be more experimentally sound, we actually ran this snippet separately for train, test and validation sets, standardizing each dataset with their own mean and standard deviation. |
Hello again, |
That's odd. I'm guessing you did the whole changes in the keras.json configuration file by setting the "backend" and "image_data_format" already. Strange that it wouldn't work.
Sure. Give us a little time, like a day or so, and we'll give you the version of the code that we had used. |
Hey @lgaida. I was digging around our code and I saw something. I know the last version I gave you didn't have a NaN guard for the standardization. Did your version have one?
|
Kind of, i initialized the array with zeroes.
Sounds great 👍 I'll be waiting until then :) Feel free to contact me via github or email (see github-profile) |
I mean to say that (as far as I remember) the std of |
Hello @lgaida , thanks for your interest in our work and reaching out to us.
I would repeat the same thing as mentioned by @saikat-roy that even though initialization was done with all zeros, unfortunately that doesn't guarantee that you won't get NaN. Please consider adding this safe guard in your code and let us know. |
I just added the guard but still get Label 8 for every tested sample 😢 |
Hi @saikat-roy, can you provide the |
Hey sorry for the late reply.
I'm really sorry but we don't have the computational environment setup, that we had set up for processing the dataset, available currently.
We will however, be looking into releasing more of our code and testing the model weights ourselves since it is disturbing to hear the model weights do not load as expected. While we cannot do it immediately, we do plan to try it in a week or two. So I would request your patience for a while longer and hopefully we can get back to you with better news than "we don't know what's wrong, this shouldn't be happening". |
Just want to remind you that i could also take a look at the code 👋 |
Hi Saikat, Arindam, First of all thanks for writing this great article from keras import applications |
Okay so first and foremost we are sincerely sorry about the ridiculously late updates to this issue. Unfortunately as we mentioned, we have since stopped working on this project and have literally no hardware or software setup available to test the models any more. I know its frustrating to have your queries not answered but we have gotten little to no time to really go through the code for this bug - we have thought a lot about it and simply put, it did NOT exist when we worked on it. The reason I am writing this update is to mention that we recently went through multiple issues on the keras forums regarding issues with I thank you for being patient with us and again we sincerely apologize for not actively helping out with the issue. To anyone who needs our code, we will attempt to simply just release the .py files with some minor cleaning soon - since we can't help out actively this is the least we can do at this point. |
An attempt to solve the weight loading has been added to the readme. So we'll be closing this issue. |
For anyone looking to run this with Tensorflow 2.0, the following will work. Install dependencies:
Download a weights file, e.g. Download the convert script from this repo. Open the convert script, and make the following changes:
Run A new folder is created ( Open the folder and create new file called from keras import applications
vgg = applications.VGG16(include_top=True, weights='./vgg16_weights_th_dim_ordering_th_kernels_Holistic_91.11.h5', classes=16)
import numpy as np
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
class_map = ['letter', 'form', 'email', 'handwritten', 'advertisement',
'scientific report', 'scientific publication', 'specification', 'file folder',
'news article', 'budget', 'invoice', 'presentation', 'questionnaire',
'resume', 'memo']
def test(path):
img = image.load_img(path, target_size=(224, 224))
img = image.img_to_array(img)
img = preprocess_input(img)
x = np.expand_dims(img, 0)
y = vgg.predict(x)
print(y)
idx = np.argmax(y)
print('predicted class: {}', class_map[idx])
test('../form.jpg') Now run the code: |
@martinnormark Hey thanks for the guide. I've added a link to this on the main Readme. |
I see you post the link to pre-trained weights but no tutorial how to use them.
Tried with keras:
but failed
ValueError: No model found in config file
.How do I load these h5 weights? Can you provide the model source so I can use
model.load_weights(...)
The text was updated successfully, but these errors were encountered: