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

problem about predict #91

Closed
azyslzp opened this issue Sep 17, 2020 · 11 comments
Closed

problem about predict #91

azyslzp opened this issue Sep 17, 2020 · 11 comments
Labels

Comments

@azyslzp
Copy link

azyslzp commented Sep 17, 2020

due to lack of predict examples, I am unable to use the model.predict correctly, there also exists a bug as:
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [256, 1, 1, 3], but got 3-dimensional input of size [31, 2, 132] instead
I used :output=model.predict_on_batch(Singledata)
and my Singledata is a array of size (31,2,128), float type

@azyslzp azyslzp added the bug label Sep 17, 2020
@freud14
Copy link
Collaborator

freud14 commented Sep 17, 2020

Hi, thank you for your question. Is it possible to get the whole stack trace of the error? Otherwise, it should work the way you're using the function.

Thank you.

@azyslzp
Copy link
Author

azyslzp commented Sep 17, 2020

Hi, thank you for your question. Is it possible to get the whole stack trace of the error? Otherwise, it should work the way you're using the function.

Thank you.

Hi, thank you for your question. Is it possible to get the whole stack trace of the error? Otherwise, it should work the way you're using the function.

Thank you.

Hi, thanks for your reply. I have used:
idx=109000
Singledata=sX[idx-1:idx]# sX is a array of size (120000, 2, 128) , which contains 120000 samples of I and Q signal data
Singledata=torch.cuda.FloatTensor(Singledata).unsqueeze(0)
output=model.predict(Singledata)
to solve the predict problem,
but it seems that this way only worked on single sample, when I use
predict_batch or predict_generator,
I still met the dimension problem
and I loaded the model by
checkname=os.path.join(checkdir+"/checkpoint.optim")
model.load_optimizer_state(checkname)#get the checkpoint model to predict

@freud14
Copy link
Collaborator

freud14 commented Sep 17, 2020

Is it possible to have the code of your network? Also, to load your checkpoint, you should use the load_weights method, not the load_optimizer_state. load_optimizer_state is used to load back the optimizer state for further training. It does not load the weights of the network.

@azyslzp
Copy link
Author

azyslzp commented Sep 17, 2020

Is it possible to have the code of your network? Also, to load your checkpoint, you should use the load_weights method, not the load_optimizer_state. load_optimizer_state is used to load back the optimizer state for further training. It does not load the weights of the network.

Sure, I have uploaded my code at https://github.com/azyslzp/LZP-Signal-recognition
and the predict problem existed in the test.py.
In train.py, I have demo it and i t could output a pth file which is created by ModelCheckpoint.
In test.py, I reload the model file , but it is unable to complete the prediction, and
thanks for your advice~

@freud14
Copy link
Collaborator

freud14 commented Sep 17, 2020

Hi,

In your Dataset __getitem__ method, you also do an .unsqueeze(0). So, my guess would be that you can do an .unsqueeze(1) so that your examples have the same shape as in training. Also, I think your error stems from the fact that nn.Conv2d takes in a 4d tensor and your Singledata tensor is only a 3d tensor. Let me know if that helps you.

Thank you.

@azyslzp
Copy link
Author

azyslzp commented Sep 18, 2020

Hi,

In your Dataset __getitem__ method, you also do an .unsqueeze(0). So, my guess would be that you can do an .unsqueeze(1) so that your examples have the same shape as in training. Also, I think your error stems from the fact that nn.Conv2d takes in a 4d tensor and your Singledata tensor is only a 3d tensor. Let me know if that helps you.

Thank you.

Hi,
I have tried to modify my code by .unsqueeze(1), but it still unable to work, which also brings the training problem. I also have a key issue:
If I use ModelCheckpoint to save the model weight, should I use

model = Model(
    network=net,
    optimizer="Adam",
    loss_function=nn.CrossEntropyLoss(),
    batch_metrics=metrics
)
checkname=os.path.join(MODEL_DIR+'/'+args.model+'.pth')
out1=model.load_weights(checkname)

to load? But it seems not work for out1.predict(s1) by
AttributeError: 'NoneType' object has no attribute 'predict'

Thanks

@freud14
Copy link
Collaborator

freud14 commented Sep 18, 2020

Hi, load_weights does not return anything, it modifies the model in-place. Also, could you print the shape of your input tensor in the forward during train and during test? Maybe, we could see the difference.

@azyslzp
Copy link
Author

azyslzp commented Sep 18, 2020

Hi, I have checked the ModelCheckpoint source, it seem only use the model.save_weights. For verification, I add the predict in the end of training directly:

    model.fit_generator(
        train_dataloader,
        val_dataloader,
        epochs=EPOCHS,
        initial_epoch=1,
        callbacks=callbacks
    )
    checkname=os.path.join(MODEL_DIR+'/'+args.model+'1.pth')
    model.save_weights(checkname)
    model.load_weights(checkname)
    output=model.predict(s1)

And I found it works in vtcnn model, but not in mrresnet model. So I think the problem may came from the model structure.
As for the tensor shape, it is

    for i, (x, y) in enumerate(train_dataloader):
        x2=x
        y2=y
        print(x2)
x2.shape
Out[61]: torch.Size([192, 1, 2, 128])
s1.shape
Out[64]: torch.Size([1, 1, 2, 128])

@freud14
Copy link
Collaborator

freud14 commented Sep 18, 2020

Hi, it seems that you are using Conv1d instead of Conv2d in MRResNet. Conv1d takes in 3d tensors. You also do x = x.squeeze() at the beginning of your forward which removes all dimensions of size 1 which in the case of s1 gives you a 2d tensor of shape [2, 128].

@azyslzp
Copy link
Author

azyslzp commented Sep 20, 2020

Hi, I have solved this problem by input 192 samples a time for prediction( although it is not a good idea)

@freud14
Copy link
Collaborator

freud14 commented Sep 20, 2020

Alright, I will close the issue. However, I will advise you to really look the shapes of your tensors everywhere in your networks (from input to output and in the middle) to make sure that the shapes you get are what you expect them to be. Let me know if you have any question related to Poutyne.

@freud14 freud14 closed this as completed Sep 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants