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

UnboundLocalError: local variable 'loss' referenced before assignment #7

Open
dckakon opened this issue Sep 24, 2021 · 7 comments
Open
Labels
bug Something isn't working

Comments

@dckakon
Copy link

dckakon commented Sep 24, 2021

I was trying to test an image using the train-image-inference.ipynb file. But in the last block of code, I am getting an error.


ValueError Traceback (most recent call last)
in ()
10 embedding = np.load(EMBEDDING_PATH, allow_pickle=True)
11
---> 12 indices_list = compute_similar_images(TEST_IMAGE_PATH, NUM_IMAGES, embedding, device)
13 plot_similar_images(indices_list)

7 frames
/usr/local/lib/python3.7/dist-packages/sklearn/metrics/pairwise.py in check_pairwise_arrays(X, Y, precomputed, dtype, accept_sparse, force_all_finite, copy)
153 raise ValueError("Incompatible dimension for X and Y matrices: "
154 "X.shape[1] == %d while Y.shape[1] == %d" % (
--> 155 X.shape[1], Y.shape[1]))
156
157 return X, Y

ValueError: Incompatible dimension for X and Y matrices: X.shape[1] == 65536 while Y.shape[1] == 262144

Please help me how to solve this issue

@dckakon dckakon added the bug Something isn't working label Sep 24, 2021
@oke-aditya
Copy link
Owner

oke-aditya commented Sep 25, 2021

Hi! most probably the dimensions of your image are not same. You would need to resize them.
This is due to assumption int the tutorial that all the images have shape of (512, 512). For images of different size, you would need different embeddings dimensions.

@tibiv111
Copy link

Hi! I have the same problem, however changing the dimensions of the image doesn't fix the problem.
`Setting Seed for the run, seed = 42
------------ Creating Dataset ------------
------------ Dataset Created ------------
------------ Creating DataLoader ------------
------------ Dataloader Cretead ------------
MSELoss()
GPU Availaible moving models to GPU
------------ Training started ------------
0%| | 0/30 [00:00<?, ?it/s]
Traceback (most recent call last):
File "E:/Work/UNI/Third_year_first_semester/Szoftver tervezes/Projekt/Image recognition repos/image_similarity/image_similarity-master/image_similarity/torch_train.py", line 79, in
train_loss = torch_engine.train_step(
File "E:\Work\UNI\Third_year_first_semester\Szoftver tervezes\Projekt\Image recognition repos\image_similarity\image_similarity-master\image_similarity\torch_engine.py", line 47, in train_step
return loss.item()
UnboundLocalError: local variable 'loss' referenced before assignment

Process finished with exit code 1`

@oke-aditya
Copy link
Owner

Have you written in the torch_train.py code. If yes then it should work fine.

loss_fn = nn.MSELoss()

@tibiv111
Copy link

Unfortunately it does not. The train-image-similarity.ipynb also has this problem.

@oke-aditya
Copy link
Owner

I'm still perplexed. It should run as I had tested this code (long time ago) and it ran fine.
Even the ipynb has the output.

https://github.com/oke-aditya/image_similarity/blob/master/image_similarity/train-image-similarity.ipynb

@oke-aditya
Copy link
Owner

Can you share a reproducible script over colab so that I can debug?

@hxtkyne
Copy link

hxtkyne commented Jan 24, 2022

I think you just use a small size images for test. So you can modify drop_last from True to False in torch_train.py.

train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=config.TRAIN_BATCH_SIZE, shuffle=True, drop_last=False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants