Event-AHU / VcT_Remote_Sensing_Change_Detection

[IEEE TGRS-2023] VcT: Visual change Transformer for Remote Sensing Image Change Detection
43 stars 1 forks source link

IndexError: shape mismatch: indexing tensors could not be broadcast together with shapes [320], [80] #7

Closed heikeyuhuajia closed 6 months ago

heikeyuhuajia commented 6 months ago

Thank you for your work. But when I execute the test file as per the script you have given, I get the following problem, my dataset is also of 256*256 size and no other part of the network has been modified.

True
[0]
initialize network with normal
cuda:0
================ (Fri May 10 22:52:47 2024) ================
gpu_ids: [0] project_name: BestLEVIR print_models: False num_workers: 8 dataset: CDDataset data_name: LEVIR batch_size: 32 split: test img_size: 256 n_class: 2 net_G: Reliable_transformer checkpoint_name: best_ckpt.pt checkpoint_dir: checkpoints/BestLEVIR vis_dir: vis/BestLEVIR loading last checkpoint...
Eval Historical_best_acc = 0.9508 (at epoch 198)

Begin evaluation...
Traceback (most recent call last):
  File "eval_cd.py", line 58, in <module>
    main()
  File "eval_cd.py", line 54, in main
    model.eval_models(checkpoint_name=args.checkpoint_name)
  File "/home/conda_share/Datasets/code/VcT_Remote_Sensing_Change_Detection/VcT_code/models/evaluator.py", line 170, in eval_models
    self._forward_pass(batch)
  File "/home/conda_share/Datasets/code/VcT_Remote_Sensing_Change_Detection/VcT_code/models/evaluator.py", line 154, in _forward_pass
    self.G_pred = self.net_G(img_in1, img_in2)
  File "/home/.conda/envs/cd/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/conda_share/Datasets/code/VcT_Remote_Sensing_Change_Detection/VcT_code/models/networks.py", line 374, in forward
    token1 = self.kmeansToken(token1, self.cluster_nums)
  File "/home/conda_share/Datasets/code/VcT_Remote_Sensing_Change_Detection/VcT_code/models/networks.py", line 342, in kmeansToken
    cluster_ids_x, _ = kmeans(X=x.detach(), num_clusters=num_clusters, distance='euclidean')
  File "/home/conda_share/Datasets/code/VcT_Remote_Sensing_Change_Detection/VcT_code/models/kmeans.py", line 54, in kmeans
    initial_state = initialize(X, num_clusters)
  File "/home/conda_share/Datasets/code/VcT_Remote_Sensing_Change_Detection/VcT_code/models/kmeans.py", line 23, in initialize
    initial_state = X[indices_col, indices_row,:].reshape(X.shape[0],num_clusters,-1)
IndexError: shape mismatch: indexing tensors could not be broadcast together with shapes [320], [80]
heikeyuhuajia commented 6 months ago

Hello, I checked other issues and found that it could be caused by incorrect generation of indice0 and indice1. Q1:I used your default mask.py, may I know how should I generate the corrected mask.py when I batchsize=32:

import numpy as np
import torch
import scipy.io as io
x = 64
b = np.array(np.identity(x ** 2), dtype='int8')
# b=np.identity(x**2)
#b=torch.eye(x**2)
for i in range(x**2):
    if i == 0:
        b[i][i+1]=1
        b[i][i+x]=1
        b[i][i+x+1]=1
        continue
    if i == x - 1:
        b[i][i-1]=1
        b[i][i+x]=1
        b[i][i+x-1]=1
        continue
    if i == x**2 - x:
        b[i][i+1]=1
        b[i][i-x]=1
        b[i][i-x+1]=1
        continue
    if i == x**2 - 1:
        b[i][i-1]=1
        b[i][i-x]=1
        b[i][i-x-1]=1
        continue
    if 0 < i < x - 1:
        b[i][i-1]=1
        b[i][i+1]=1
        b[i][i+x]=1
        b[i][i+x-1]=1
        b[i][i+x+1]=1
        continue
    if x**2-x < i < x**2 - 1:
        b[i][i-1]=1
        b[i][i+1]=1
        b[i][i-x]=1
        b[i][i-x-1]=1
        b[i][i-x+1]=1
        continue
    if i%x==0:
        b[i][i+1]=1
        b[i][i+1+x]=1
        b[i][i+1-x]=1
        b[i][i+x]=1
        b[i][i-x]=1
        continue
    if i%x==x-1:
        b[i][i-1]=1
        b[i][i-1+x]=1
        b[i][i-1-x]=1
        b[i][i+x]=1
        b[i][i-x]=1
        continue
    else:
        b[i][i-1]=1
        b[i][i+1]=1
        b[i][i-x]=1
        b[i][i+x]=1
        b[i][i-x+1]=1
        b[i][i-x-1]=1
        b[i][i+x-1]=1
        b[i][i+x+1]=1

#indice0
length = len(b)
for i in range(3):
   leftmaxtric = np.row_stack((b, np.array(np.zeros((length, length)),dtype='int8')))
   rightmaxtric = np.row_stack((np.array(np.zeros((length, length)),dtype='int8'), b))
   b = np.hstack((leftmaxtric, rightmaxtric))
   length=length*2
b = torch.tensor(b)
print(b.shape)
indice = b.nonzero().t()
indice = indice.numpy()
print(indice.shape)
np.savetxt('indice0.txt', indice)

# indice1
for i in range(3):
    a = b
    b = np.vstack((a, b))

b = torch.tensor(b)
print(b.shape)
indice = b.nonzero().t()
indice = indice.numpy()
print(indice.shape)
np.savetxt('indice1.txt', indice)

Q2: Since I want to compare my paper with yours, I need to use the settings in your paper for comparison, do I still need to make any changes when I load your checkpoints? Previously I was using your default commands for testing:

gpus=0

data_name=LEVIR
net_G=Reliable_transformer
split=test
project_name=BestLEVIR
checkpoint_name=best_ckpt.pt

python eval_cd.py --split ${split} --net_G ${net_G} --checkpoint_name ${checkpoint_name} --gpu_ids ${gpus} --project_name ${project_name} --data_name ${data_name}
chengtianxiu commented 6 months ago
print(indice.shape)

Thank you very much for your interest in this work. A1: If batchsize is 8, index 0 is spliced by 8 identical masks along the main diagonal, and index 1 is spliced horizontally, which can be viewed by print(indice.shape). If batchsize is changed to 32, it is spliced by 32 identical masks. A2: The initialization of Kmeans is fixed here in order to make the experimental results deterministic, and you can try using a different class-centered initialization. No other hyperparameters need to be changed.

heikeyuhuajia commented 6 months ago

Thank you very much for your reply, it has been resolved with your help, good luck with your work.