Skip to content
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

Size mismatch in final layer #1

Open
KathrynSch opened this issue Apr 1, 2020 · 11 comments
Open

Size mismatch in final layer #1

KathrynSch opened this issue Apr 1, 2020 · 11 comments

Comments

@KathrynSch
Copy link

Size mismatch when running inference:
Traceback (most recent call last): File "scripts/predict.py", line 106, in <module> probs = pretrained_model(sample_input)[0] File "/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__ result = self.forward(*input, **kwargs) File "/opt/conda/lib/python3.7/site-packages/torchvision/models/densenet.py", line 198, in forward out = self.classifier(out) File "/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__ result = self.forward(*input, **kwargs) File "/opt/conda/lib/python3.7/site-packages/torch/nn/modules/linear.py", line 87, in forward return F.linear(input, self.weight, self.bias) File "/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py", line 1370, in linear ret = torch.addmm(bias, input, weight.t()) RuntimeError: size mismatch, m1: [1 x 1664], m2: [14976 x 5] at /opt/conda/conda-bld/pytorch_1579022060824/work/aten/src/TH/generic/THTensorMath.cpp:136

I ran into your predict.py script and saw you load torchvision densenet169, and replace the final linear layer to match the number of desired output (5). However, you also change the input features to 14976 instead of 1664. With an input image of size 299x299, the input features for that final layer has shape 1x1664. Where does this 14976 come from ?
I tried changing this back to 1664 but now your pretrained weights don't match.
Thanks for your help.

@kidzik
Copy link
Member

kidzik commented Apr 16, 2020

I've just tested the docker with:

(base) kidzik@kidzik-XPS-8900:~/workspace/kneenet-docker$ bash run.sh

and it seems to be working fine

/opt/conda/lib/python3.6/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
/opt/conda/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/models/densenet.py:212: UserWarning: nn.init.kaiming_normal is now deprecated in favor of nn.init.kaiming_normal_.
Processing file input/example.jpg...
tensor([ 0.4120, -0.3240, -0.0705, -0.6524, -1.2485])
Processing finished.

-- RESULTS (in output/prediction.csv) --
filename           prediction
input/example.jpg  0

The only problem was that after downloading the model it was stored in the main directory rather than in models but after moving it there the script runs fine.
Are you running it in Docker? If not, you may need similar versions of libraries as in the docker, including:

torch            0.4.1    
torchvision      0.2.1    

@Ameerak124
Copy link

I'm also facing the same issue ,would you please expalin it clearly . how to run on Docker

@kidzik
Copy link
Member

kidzik commented Jan 19, 2021

Hi, you just need to install docker and run bash run.sh and images in the inputdirectory will be processed.

@Ameerak124
Copy link

I'm using docker desktop, I have followed the same procedure but no use . Is I need the to build the Docker?
when I start to build I'm getting error with installation of libsm6,libxext6,libxender-dev

@kidzik
Copy link
Member

kidzik commented Jan 20, 2021

No there is no need for building the docker you just need to run the run.sh script or its contents
https://github.com/stanfordnmbl/kneenet-docker/blob/master/run.sh
for docker desktop you may need to adapt the paths for windows but other than that it should work just fine.

@Ameerak124
Copy link

if [ ! -f models/KneeNet.0 ]; then
mkdir -p models
mkdir -p output
curl https://s3-eu-west-1.amazonaws.com/kidzinski/models/KneeNet/KneeNet.0 -O models/KneeNet.0
fi
docker run
-v "${PWD}"/input:/workspace/input
-v "${PWD}"/output:/workspace/output
-v "${PWD}"/scripts:/workspace/scripts
-v "${PWD}"/models:/workspace/models
-it kidzik/kneenet:latest
python scripts\predict.py && printf "\n-- RESULTS (in output\prediction.csv) --\n" && cat output/predictions.csv | column -t -s,

is this script is ok for windows?

Really thanks alot @kidzik for the support

@kidzik
Copy link
Member

kidzik commented Jan 20, 2021

That's linux. For Windows you probably need to change paths, replace ${PWD} with the directory where you cloned the github repo, download the model https://s3-eu-west-1.amazonaws.com/kidzinski/models/KneeNet/KneeNet.0 manually to models/KneeNet.0 and that's essentially it
kidzik/kneenet:latest is the bre-built docker so you don't have to build anything

@Ameerak124
Copy link

Hi @kidzik, Do you have any idea to deploy this model on a url

@kidzik
Copy link
Member

kidzik commented Feb 1, 2021

Sure, you can try it here http://kl.stanford.edu/

@Ameerak124
Copy link

Ameerak124 commented Feb 3, 2021

@kidzik Really thanks alot. where Can I find source code for http://kl.stanford.edu/
and I want know can I productionize it ? if Yes How can I?

@kidzik
Copy link
Member

kidzik commented Feb 3, 2021

It uses the same DenseNet as in this docker and it's converted to tensorflow-js. We are not releasing the code of the website at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants