[Solved] TypeError: 'int' object is not callable while using TensorDataset

def load_data():
	labels = np.load('/home/vkv/labels.npy')
	labels = labels.astype(np.float32, copy=False)
	data = np.zeros((2503, 1, 418400,3))
	j = 0
	for i in range(1,2505):
		if i != 1303:
			img = (((cv2.imread('/home/vkv/Chr20/' +'\Image_ALL_'+ str(i+9) + '.png', 1)) / 255) * 2 )- 1
			img = img.astype(np.float32, copy=False)
			img = np.reshape(img, (1,418400,3), order = 'F')
			img = torch.from_numpy(img)
			data[j] = img.type(torch.FloatTensor)
			print('Loading Image:', i, end = "\r")
			j += 1
			#data.append(x)
	labels = np.concatenate((labels[:1303, :], labels[1304:, :]), axis = 0)
	print('Data Shape:', np.shape(data))
	print('Label Shape', np.shape(labels)) 
	labels = torch.from_numpy(labels)
	labels = labels.type(torch.FloatTensor)

	my_dataset = utils.TensorDataset(data,labels)

	return my_dataset

This is my code to load the data.

This is the error trace I’m getting

Traceback (most recent call last):
  File "main.py", line 65, in <module>
    main(args)
  File "main.py", line 18, in main
    **kwargs)
  File "/home/vkv/orbmodel/dataloader.py", line 102, in get_datasplit_loader
    my_dataloader, my_dataset = load_data()
  File "/home/vkv/orbmodel/dataloader.py", line 33, in load_data
    my_dataset = utils.TensorDataset(data,labels)
  File "/home/vkv/anaconda3/envs/tensor/lib/python3.6/site-packages/torch/utils/data/dataset.py", line 38, in __init__
    assert data_tensor.size(0) == target_tensor.size(0)
TypeError: 'int' object is not callable

I have read that, before using TensorDataset the dataset has to be converted from numpy array to tensor, I have done that. I don’t know what is causing this error.

3 Likes

your data is not a tensor

6 Likes

This error is occuring because the batchsize of data and labels is not same. By batchsize I mean the 1st dimension in the size.

try printing data.size() and labels.size()

Also I see that your data is not a tensor, I guess it’s still a numpy array.
Do:

data = torch.Tensor(data) # by default its a float tensor

I would recommend the same for labels