Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

number of channels in Conv1d #1

Open
gianlucarloni opened this issue Oct 9, 2023 · 6 comments
Open

number of channels in Conv1d #1

gianlucarloni opened this issue Oct 9, 2023 · 6 comments

Comments

@gianlucarloni
Copy link

https://github.com/zc2024/Causal_CXR/blob/907e62059b70cead10b9d9dfaf22fa60363a3ecc/lib/models/net.py#L153C13-L153C13

Hi, thank you for your repository. I am facing runtime error while executing this line for the Sequential object:

nn.Conv1d(1, 1, kernel_size=ekernel_size, padding=(ekernel_size - 1) // 2)

the error is RuntimeError: Expected 2D (unbatched) or 3D (batched) input to conv1d, but got input of size: [1, 2048, 1, 1], which suggests that the input tensor at that point in code, say [batch_size, 2048, 14, 14], would need to be flattened along the two last dimensions to properly enter the Conv1d, or just use the Conv2d instead. Is it correct?

Also, why do we have 1 input channel and 1 output channel for the Conv1d, and not 2048 channels?

I tried to implement that in the following mode:

self.seq = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(2), nn.Conv1d(2048, 2048, kernel_size=1), nn.Sigmoid(), )

but then the code has new issues on the layernorm in lib/models/transformer.py.

If I am missing something or misinterpreting anything, please let me know. Thank you! Gianluca

@jianmingzhuang
Copy link

jianmingzhuang commented Nov 28, 2023

I also encountered problems when running the code in it. Can I ask you where the pickle file in the code came from? If you can help me, I will be very grateful!

class NIHDataset(data.Dataset):
    defclass NIHDataset(data.Dataset):
    def __init__(self, data_path,input_transform=None,
                 used_category=-1,train=True):
        self.data_path = data_path
        if train == True:
            self.data = pickle.load(open('./traindata.pickle','rb'))
        else:
            self.data = pickle.load(open('./testdata.pickle','rb'))
        random.shuffle(self.data)
        self.category_map = category_map
        self.input_transform = input_transform
        self.used_category = used_category __init__(self, data_path,input_transform=None,
                 used_category=-1,train=True):
        self.data_path = data_path
        if train == True:
            self.data = pickle.load(open('./traindata.pickle','rb'))
        else:
            self.data = pickle.load(open('./testdata.pickle','rb'))
        random.shuffle(self.data)
        self.category_map = category_map
        self.input_transform = input_transform
        self.used_category = used_category

@gianlucarloni
Copy link
Author

gianlucarloni commented Nov 28, 2023

I also encountered problems when running the code in it. Can I ask you where the pickle file in the code came from? If you can help me, I will be very grateful!

Hello Jianming. I wondered the same thing too. I solved it by creating the pickle files myself.

The pickle files represent the serialized version of dictionary (dict) objects. Specifically, each of such dict has the image index as keys and a list of three elements as values (img name, img file, img labelvector). For instance, the dictionary for the trainval data would look like this:

trainvaldata = {
                        [0]:["00000003_002", np_img, np_labels],
                        [1]:["00000006_003", np_img, np_labels], ...
                             ...
                        [86523]:["00085471_001", np_img, np_labels], ...
                        }

And the dictionary for test data would be similar.

Therefore, after downloading the dataset, I wrote a script to prepare the pickle version of it. Please, find it attached below.

# -*- coding: utf-8 -*-

Created on Thu Oct  5 15:27:33 2023
@author: gianlucarloni

import os 
import numpy as np
from PIL import Image 
import pandas as pd
from glob import glob
import pickle

cate = ["Cardiomegaly", "Nodule", "Fibrosis", "Pneumonia", "Hernia", "Atelectasis", "Pneumothorax", "Infiltration", "Mass",
       "Pleural_Thickening", "Edema", "Consolidation", "No Finding", "Emphysema", "Effusion"]

#
df = pd.read_csv('Y:/raid/hsome/gianlucacarloni/causal_medimg/Data_Entry_2017_v2020.csv')
df = df.loc[:,["Image Index","Finding Labels"]]

## construct TEST dataset pickle file ############################################################

test_images_path = './your/path/to/images/test/'

test_data = {}

for idx,imgname in enumerate(glob(test_images_path+"*.png")):        
   png_name = os.path.basename(imgname)    
   df_local=df.loc[df["Image Index"] == png_name]
   labels = df_local["Finding Labels"].values
   labels = labels[0].split("|") #eg, "[Cardiomegaly,"Effusion"]
   onehotencoding = [int(elem in labels) for elem in cate] #eg, [1,0,0,0,...,0,1]    
   img = Image.open(imgname)
   img = np.array(img) 
   test_data[idx]=[png_name, img, np.array(onehotencoding)]

with open('testdata.pickle', 'wb') as handle:
   pickle.dump(test_data, handle)

## Check if the saved pickle dataset can be correctly loaded and inspect some key-values pairs
with open('testdata.pickle', 'rb') as h:
   data = pickle.load(h)

print(len(data))
print(data[0][0])
print(data[0][2])
print()
print(data[3][0])
print(data[3][1])

## Then, repeat all of that for the trainval data.

I hope that helps!
Best,
Gianluca

@jianmingzhuang
Copy link

jianmingzhuang commented Nov 28, 2023

Hello,Gianluca.
Thank you very much for your answer, it really helped me a lot!
I would like to know whether the problem you raised above has been solved?
And whether the code of this paper can be completely reproduced?
thanks again!
Best,
Jianming

@gianlucarloni
Copy link
Author

gianlucarloni commented Nov 28, 2023

Hello,Gianluca. Thank you very much for your answer, it really helped me a lot! I would like to know whether the problem you raised above has been solved? And whether the code of this paper can be completely reproduced? thanks again! Best, Jianming

Dear Jianming,

I am happy to have been of help. Regarding your question: from my understanding of the authors' code, I am afraid that the one uploaded by them is a preliminary version of the code. I could not fully reproduce their study, even with the same data. I contacted the authors some time ago, but unfortunately received no reply.

Therefore, I started to modify their code myself, mainly based on what was stated in their paper. This required me to implement certain functionalities from scratch. You can find my code in this github repo of mine. This is not fully tested yet, as I am still working on it. If you would like to have a look at it and discuss it there, I'd be glad.

Best,
Gianluca

@jianmingzhuang
Copy link

Dear Gianluca,
I'm truly impressed by your expertise, and I appreciate your invaluable assistance.
Currently, I'm in the process of replicating the findings outlined in this paper. It would be wonderful to collaborate with someone and engage in discussions.
I'd be honored to have the chance to converse with you about it.
Best,
Jianming

@gianlucarloni
Copy link
Author

Dear Gianluca, I'm truly impressed by your expertise, and I appreciate your invaluable assistance. Currently, I'm in the process of replicating the findings outlined in this paper. It would be wonderful to collaborate with someone and engage in discussions. I'd be honored to have the chance to converse with you about it. Best, Jianming

Dear Jianming, thank you very much, drop me an email if you like. Please, use the email address you find in mio bio (https://github.com/gianlucarloni), that is name.surname@isti.cnr.it

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants