boniolp / dCAM

[SIGMOD 2022] Python code for "Dimension-wise Class Activation Map for Multivariate Time Series Classification"
MIT License
15 stars 1 forks source link

Unable to run dInceptionModel or dResNetBaseline #2

Closed Victordmz closed 1 year ago

Victordmz commented 1 year ago

Running dInception or dResNetBaseline leaves me with an error RuntimeError: Expected 2D (unbatched) or 3D (batched) input to conv1d, but got input of size: [<batch size>, <nr of dimensions>, <nr of dimensions>, <time series length>]. I tried this with the FingerMovements dataset.

Here is a minimally reproducible example following the flow of script_exp.py:

from CNN_models import *
from DCAM import *
from sklearn.model_selection import train_test_split

with open("FingerMovements.pickle",'rb') as f:
    X,y = pickle.load(f)

# Convert the UCR-UEA format into a list of list
def generate_list_instance(x):
    res = []
    for i in range(len(x)):
        res.append(list(x[i]))
    return np.array(res)

dict_label = {}
count = 0
for val in set(y.values):
    dict_label[val] = count
    count += 1

all_class_all = []
all_label = []
for i in range(len(X)):
    all_class_all.append(generate_list_instance(X.values[i]))
    all_label.append(dict_label[y.values[i]])

original_length = len(all_class_all[0][0])
num_classes = len(set(y.values)) 
original_dim = len(all_class_all[0])
nb_instance = len(all_class_all) 

all_class, all_class_test, label, label_test = train_test_split(all_class_all, all_label,stratify=all_label, test_size=1-0.8,random_state=11081994)

# Generate C-wised input for d-based models (i.e., dCNN, dResNet, and dInceptionTime)
def gen_cube(instance):
    result = []
    for i in range(len(instance)):
        result.append([instance[(i+j)%len(instance)] for j in range(len(instance))])
    return result 

x = np.array([gen_cube(acl) for acl in all_class])
dataset_mat = TSDataset(x,label)
dataloader_cl1 = data.DataLoader(dataset_mat, batch_size=32, shuffle=True)

x = np.array([gen_cube(acl) for acl in all_class_test])
dataset_mat_test = TSDataset(x,label_test)
dataloader_cl1_test = data.DataLoader(dataset_mat_test, batch_size=1, shuffle=True)

# This is dInceptionTime
modelarch = dInceptionModel(num_blocks=3, in_channels=original_dim, out_channels=64,
                           bottleneck_channels=64, kernel_sizes=[10,20,40],
                           use_residuals=True, num_pred_classes=num_classes).to('cuda')

# dResNet gives the same error
# modelarch = dResNetBaseline(original_dim,mid_channels=128,num_pred_classes=num_classes).to(device)

model = ModelCNN(modelarch,'cuda')

model.train(num_epochs=70,dataloader_cl1=dataloader_cl1,dataloader_cl1_test=dataloader_cl1_test)

When I swap the modelarch to modelarch = ConvNet2D(original_length,original_dim,original_dim,num_classes).to('cuda'), as in Synthetic_experiment_DCAM.ipynb, training works fine.

Victordmz commented 1 year ago

I retried with the Synthetic_experiment_DCAM.ipynb notebook and the error can be reproduced in that way too.

boniolp commented 1 year ago

Hi,

Thank you for your interest in our code and for pointing out this error. Unfortunately, I am not able to reproduce the error. Here is a notebook (pdf format) with the code you sent me (using CPU instead of GPU) using the dCAM-env conda environment.

test.pdf

This code runs on my side without error (the last error is a keyboard interrupt error) for dInception and dResNet. Do you still have the same error when using device='cpu'?

Best,

Paul

Victordmz commented 1 year ago

Hello,

Thank you for your answer. I ran the exact same code in Google Colab and I unfortunately still get the error. Here is a link to the notebook that I used to reproduce the error: https://colab.research.google.com/drive/1RQpa4PqS7tvMIVMKXOFr6KJxeVAeaquL?usp=sharing. You would still have to upload FingerMovements.pickle and CNN_models.py for it to run. The first two cells are me trying to install the recommended PyTorch version, however, there is no difference between that version and the default in Colab, which is PyTorch 2.

boniolp commented 1 year ago

Hi,

Thank you for sharing this notebook! I opened the Google colab notebook you shared in your previous message; I uploaded CNN_model.py and FingerMovement.pickle and ran it without error. Here is a screenshot.

Screen Shot 2023-04-19 at 3 33 41 PM

The problem might be with the CNN_model.py file version. Are you sure your CNN_model.py file is identical to the one in the repo?

Victordmz commented 1 year ago

Hello,

Thank you for your response and your suggestion. I tried running the notebook again by downloading CNN_model.py from source, and uploaded FingerMovements.pickle to my Google Drive, shared it publicly and let Colab download it (it was downloaded from the zip at parisdescartes.fr and I re-uploaded this particular pickle file only). Unfortunately, the error remains. The updated notebook can still be found here, it is the same link. The runtime I ran it on is CPU only.

It should be noted that the PyTorch version on the current version of the notebook is 2.0.0, but in my local system it is 1.13.1 and I have the same error.

boniolp commented 1 year ago

Hi,

Thanks for the new notebook! This time, I managed to reproduce the error. I pushed a new version of CNN_model.py, and it should work now for both torch 2.0.0 and torch 1.7.1.

Let me know if you still get the error.

Victordmz commented 1 year ago

Hello,

This seems to have done the job. Thank you very much!