image_similarity icon indicating copy to clipboard operation
image_similarity copied to clipboard

UnboundLocalError: local variable 'loss' referenced before assignment

Open dckakon opened this issue 4 years ago • 7 comments

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 avatar Sep 24 '21 10:09 dckakon

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.

oke-aditya avatar Sep 25 '21 06:09 oke-aditya

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`

tibiv111 avatar Nov 18 '21 12:11 tibiv111

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

loss_fn = nn.MSELoss()

oke-aditya avatar Nov 18 '21 12:11 oke-aditya

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

tibiv111 avatar Nov 21 '21 12:11 tibiv111

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 avatar Nov 21 '21 12:11 oke-aditya

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

oke-aditya avatar Nov 21 '21 12:11 oke-aditya

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)

hxtkyne avatar Jan 24 '22 08:01 hxtkyne