GithubHelp home page GithubHelp logo

extract feature about tflearn HOT 12 CLOSED

tflearn avatar tflearn commented on May 20, 2024
extract feature

from tflearn.

Comments (12)

aymericdamien avatar aymericdamien commented on May 20, 2024

Yes, it is fairly easy, taking the example https://github.com/tflearn/tflearn/blob/master/examples/images/dnn.py, you can add at the end of the file:

# Save the model
model.save('my_model')

# Redefine a model with desired layer as output
model = tflearn.DNN(dense2)
# Load weights from saved model
model.load('my_model')
# Get features for X[0]
model.predict([X[0]])

from tflearn.

mhasnat avatar mhasnat commented on May 20, 2024

Hi,

My Network definition performs data pre-processing and augmentation using the following code before network definition:
===== CODE Pre-processing =====
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()

I would like to know that: "is it necessary to do the above pre-processing before extracting features for new images?". If yes, then please let me know how.

Thanks a lot in advance.

from tflearn.

aymericdamien avatar aymericdamien commented on May 20, 2024

If you trained your images using pre-processing, then pictures should always be pre-processed before you are going to predict (and extract features), otherwise results might changed a lot and not be consistent. But this pre-processing is done automatically by TFLearn (When training, testing and predicting), so you do not have to worry about it. Note that only data preprocessing is applied when predicting, not augmentation.

Also, I realized there was a bug when using model.predict(); it was not performing the pre-processing. I just fixed it, please re-install TFLearn and it should work fine.

from tflearn.

mhasnat avatar mhasnat commented on May 20, 2024

Thank you for your answer. Indeed, I was applying the pre-processing before predicting and not getting the expected results. Therefore, I was wondering whether the TFLearn pre-processing has an additional impact on the test data.

Thanks for fixing the bug. I re-installed TFLearn and tried to test it again. However, I observe the following error message now:
tensorflow.python.framework.errors.NotFoundError: Tensor name "DataPreprocessing/pc" not found in checkpoint files

It seems the change you have made is not compatible with the pre-tained models. Is it possible to fix this error.

Thanks.

from tflearn.

aymericdamien avatar aymericdamien commented on May 20, 2024

Arf... yes, I added ZCA Whitening (That added the Principal Component variable to the graph). You can either retrain your model if possible, or just add a quick fix:
In the file: https://github.com/tflearn/tflearn/blob/master/tflearn/data_preprocessing.py#L30, Replace the PersistentParameter by 'None'. This should works well as long as your are not using ZCA Whitening.

Or without modifying tflearn library, add to your script (before loading model):

tf.add_to_collection(tf.GraphKeys.EXCL_RESTORE_VARS, 'pc')

or

tf.add_to_collection(tf.GraphKeys.EXCL_RESTORE_VARS, img_prep.global_pc.var)
tf.add_to_collection(tf.GraphKeys.EXCL_RESTORE_VARS, img_prep.global_pc.var_r)

from tflearn.

mhasnat avatar mhasnat commented on May 20, 2024

Thanks a lot. Unfortunately adding those lines did not solve the problem. Therefore, I prefer to re-train from the beginning.

from tflearn.

mhasnat avatar mhasnat commented on May 20, 2024

After re-training I tried to extract the features following the same way as before. However, I get the following error message now when I execute "ft = model.predict([X[0]])":

Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/tflearn/models/dnn.py", line 199, in predict
return self.predictor.predict(feed_dict)
File "/usr/local/lib/python2.7/dist-packages/tflearn/helpers/evaluator.py", line 63, in predict
feed_dict[k] = dprep_dict[k].apply(feed_dict[k])
File "/usr/local/lib/python2.7/dist-packages/tflearn/data_preprocessing.py", line 37, in apply
batch = m(batch)
File "/usr/local/lib/python2.7/dist-packages/tflearn/data_preprocessing.py", line 187, in _featurewise_zero_center
batch[i] -= self.global_mean.value
TypeError: ufunc 'subtract' output (typecode 'O') could not be coerced to provided output parameter (typecode 'f') according to the casting rule ''same_kind''

from tflearn.

aymericdamien avatar aymericdamien commented on May 20, 2024

Thanks for reporting the issue, there was actually a bug that the global mean variable was not restored when loading a model. I updated the code so it should work now, please reinstall TFLearn.

from tflearn.

mhasnat avatar mhasnat commented on May 20, 2024

Thanks a lot. Now it is working as expected. Moreover, it verified my earlier assumption that the additional pre-processing has impact on the performance.

from tflearn.

mkhodabandeh avatar mkhodabandeh commented on May 20, 2024

Hi,
After updating tflearn today I faced similar issue:

File "/cs/vml4/smuralid/.pyenv/versions/2.7.12/lib/python2.7/site-packages/tflearn/data_preprocessing.py", line 201, in _featurewise_zero_center
batch[i] -= self.global_mean.value
TypeError: ufunc 'subtract' output (typecode 'O') could not be coerced to provided output parameter (typecode 'd') according to the casting rule ''same_kind''

Please help me fix it.

UPDATE:
Sorry I found the problem. I created the preprocessing function outside of the scope of my session/graph.

from tflearn.

srichakradhar avatar srichakradhar commented on May 20, 2024

I also have the same issue.
I'm using tflearn-0.3.2
which version is this updated in?

from tflearn.

stanleynguyen avatar stanleynguyen commented on May 20, 2024

I'm having the same issue as well
tflearn==0.3.2

from tflearn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.