RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 3 and 2 in dimension 1

root = "/home/hu/.PyCharmCE2018.2/config/scratches/data/corel_5k/"
best_F1 = 0
lr = 0.001
step = 0
viz = visdom.Visdom()
# 定义是否使用GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# 参数设置,使得我们能够手动输入命令行参数,就是让风格变得和Linux命令行差不多
parser = argparse.ArgumentParser(description = 'PyTorch CIFAR100 Training')
parser.add_argument('--resume', default = '', type = str, metavar = 'PATH',
	help = 'path to latest checkpoint (default:none)')
parser.add_argument('--epochs', default = 160, type = int, metavar = 'N',
	help = 'number of total epochs to run')
parser.add_argument('--start_epoch', default = 0, type = int, metavar = 'N',
	help = 'number of total epochs to run')
parser.add_argument('-e', '--evaluate', dest = 'evaluate',
	help = 'evaluate model on validation set')

args = parser.parse_args()
resnet50 = models.resnet50(pretrained = True)

resnet50.fc = nn.Linear(2048, 50)

def feature_layer():
	layers = []
	for name, layer in resnet50._modules.items():
		layers += [layer]
	features = nn.Sequential(*layers)
	return features

class net(nn.Module):
	def __init__(self):
		super(net, self).__init__()
		self.features = feature_layer()
	def forward(self, x):
		x = self.features(x)
		return x
model = net().to(device)
pretrained_dict = resnet50.state_dict()
model_dict = model.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
if device == 'cuda':
    model = torch.nn.DataParallel(model)
    cudnn.benchmark == True


class LoadDataset():
	def __init__(self, txt, transform):
		self.txt = txt
		fh = open(self.txt, 'r')
		imgs = []
		self.transform = transform
		for line in fh:
			line = line.strip('\n')
			line = line.rstrip()
			words = line.split()
			image1 = words[0]
			image1 = int(image1)
			image1 = image1 // 1000
			image1 = image1 * 1000
			image1 = '%d' %image1
			imageList = root + 'images/' + image1 + '/' + words[0] + '.jpeg'
			words.pop(0)
			lableList = list(map(int, words))
			lableList = np.array(lableList)
			lableList = torch.from_numpy(lableList)
			imgs.append((imageList, lableList))
		self.imgs = imgs
	def __getitem__(self, item):
		image, label = self.imgs[item]
		image = Image.open(image)
		img = transform(image)
		return img, label
	def __len__(self):
		return len(self.imgs)
transform = transforms.Compose([
	transforms.Resize((224, 224)),
	transforms.RandomHorizontalFlip(),
	transforms.ToTensor(),
	transforms.Normalize((0.3865, 0.3995, 0.3425), (0.2316, 0.2202, 0.2197)),
	])
trainset = LoadDataset(txt = root + 'labels/training_label', transform = transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True, num_workers=2)
valset = LoadDataset(txt = root + 'labels/val_label', transform = transform)
valloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True, num_workers=2)

x, train_F1, test_F1 = 0, 0, 0
win = viz.line(
	X = np.array([x]),
	Y = np.column_stack((np.array([train_F1]), np.array([test_F1]))),
	opts = dict(
		legend = ["train_F1", "test_F1"]
		)
	)

def main():
	global args, best_prec1, lr
	args = parser.parse_args()
	print("=> loading checkpoint '{}'".format('model_best.pth.tar'))
	checkpoint = torch.load('model_best.pth.tar')
	args.start_epoch = checkpoint['epoch']
	best_F1 = checkpoint['best_F1']
	model.load_state_dict(checkpoint['state_dict'])
	optimizer.load_state_dict(checkpoint['optimizer'])
	print("=> loaded checkpoint '{}' (epoch {})"
		.format('model_best.pth.tar', checkpoint['epoch']))

	criterion = nn.CrossEntropyLoss().cuda()
	optimizer = torch.optim.Adam(model.parameters(), lr = lr)

	if args.evaluate:
		validate(valloader, model, criterion)
		return

	for epoch in range(args.start_epoch, args.epochs):
		print("epoch = %d" %epoch)
		adjust_LR(optimizer, epoch)     #adjust learning_rate
		train_loss, train_F1 = train(trainloader, model, criterion, optimizer, epoch)
		test_loss, test_F1 = validate(valloader, model, criterion)
		is_best = test_F1 > best_F1
		best_F1 = max(test_F1, best_F1)
		viz.line(
			X = np.array([epoch]),
			Y = np.column_stack((np.array([train_F1]), np.array([test_F1]))),
			win = win,
			update = "append"
			)
		save_checkpoint({
			'epoch': i + 1,
			'state_dict': model.state_dict(),
			'best_F1': best_F1,
			'optimizer': optimizer.state_dict(),
			}, is_best)
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
	torch.save(state, filename)
	if is_best:
		shutil.copyfile(filename, 'model_best.pth.tar')
			
def adjust_LR(optimizer, epoch):
	lr= lr* (0.1 ** (epoch // 40))
	for param_group in optimizer.param_group:
		param_group['lr'] = lr

def train(trainloader, model, criterion, optimizer, epoch):
	model.train()
	for i, (input, target) in enumerate(trainloader):
		step += 1
		input = input.to(device)
		if torch.cuda.is_available():
			target = target.cuda()	
		output = model(input)
		loss = criterion(output, target)
		optimizer.zero_grad()
		loss.backward()
		optimizer.step()
		F1 = metrics.f1_score(target, output, average = 'weighted')
		print ('train : step = %d, F1 = %.3f' %(step, loss, F1))
	return loss, F1
def validate(valloader, model, criterion):
	model.eval()
	f1_total = 0
	loss_total = 0
	total = 0
	for i, (input, target) in  enumerate(valloader):
		input = input.to(device)
		if torch.cuda.is_available():
			target = target.cuda()					
		output = model(input)
		loss = criterion(output, target)
		f1 = metrics.f1_score(target, output, average = 'weighted')
		loss_total += loss
		f1_total += f1
		total += 1
	loss = loss_total / total
	f1 = f1_total / total
	print('val: test_loss = %.4f, test_F1' %(loss, f1))
	return loss, f1

print("start test")
testset = LoadDataset(txt = root + 'labels/test_label', transform = transform)
testloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True, num_workers=2)
criterion = nn.CrossEntropyLoss().cuda()
optimizer = torch.optim.Adam(model.parameters(), lr = lr)
f1_total = 0
loss_total = 0
total = 0
for i, (input, target) in  enumerate(testloader):
	input = input.to(device)
	if torch.cuda.is_available():
		target = target.cuda()
	output = model(input)
	loss = criterion(output, target)
	f1 = metrics.f1_score(target, output, average = 'weighted')
	loss_total += loss
	f1_total += f1
	total += 1
loss = loss_total / total
f1 = f1_total / total
print('test: test_loss = %.4f, test_F1' %(loss, f1))

Above is my code,when I ran it,it told me:

RuntimeError: Traceback (most recent call last):
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 57, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in <listcomp>
    return [default_collate(samples) for samples in transposed]
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 115, in default_collate
    return torch.stack(batch, 0, out=out)

How can I solve it?
I’m a begginer.Appreciate for answering:)

1 Like

The error message doesn’t show where in your file the error occurs.
Please paste full error message.

Exception in user code:
------------------------------------------------------------
Traceback (most recent call last):
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 171, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 79, in create_connection
    raise err
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 69, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 196, in connect
    conn = self._new_conn()
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 180, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f685cc1a6a0>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hu/.local/lib/python3.6/site-packages/requests/adapters.py", line 445, in send
    timeout=timeout
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/util/retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=8097): Max retries exceeded with url: /env/main (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f685cc1a6a0>: Failed to establish a new connection: [Errno 111] Connection refused',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hu/.local/lib/python3.6/site-packages/visdom/__init__.py", line 446, in _send
    data=json.dumps(msg),
  File "/home/hu/.local/lib/python3.6/site-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/home/hu/.local/lib/python3.6/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/hu/.local/lib/python3.6/site-packages/requests/sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/hu/.local/lib/python3.6/site-packages/requests/sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "/home/hu/.local/lib/python3.6/site-packages/requests/adapters.py", line 513, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8097): Max retries exceeded with url: /env/main (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f685cc1a6a0>: Failed to establish a new connection: [Errno 111] Connection refused',))
Traceback (most recent call last):
Exception in user code:
------------------------------------------------------------
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 171, in _new_conn
start test
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 79, in create_connection
    raise err
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 69, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 196, in connect
    conn = self._new_conn()
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 180, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f685b0b72e8>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hu/.local/lib/python3.6/site-packages/requests/adapters.py", line 445, in send
    timeout=timeout
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/hu/.local/lib/python3.6/site-packages/urllib3/util/retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=8097): Max retries exceeded with url: /events (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f685b0b72e8>: Failed to establish a new connection: [Errno 111] Connection refused',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hu/.local/lib/python3.6/site-packages/visdom/__init__.py", line 446, in _send
    data=json.dumps(msg),
  File "/home/hu/.local/lib/python3.6/site-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/home/hu/.local/lib/python3.6/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/hu/.local/lib/python3.6/site-packages/requests/sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/hu/.local/lib/python3.6/site-packages/requests/sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "/home/hu/.local/lib/python3.6/site-packages/requests/adapters.py", line 513, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8097): Max retries exceeded with url: /events (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f685b0b72e8>: Failed to establish a new connection: [Errno 111] Connection refused',))
Traceback (most recent call last):
  File "/home/hu/下载/Corel5k (3).py", line 215, in <module>
    for i, (input, target) in  enumerate(testloader):
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 286, in __next__
    return self._process_next_batch(batch)
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 307, in _process_next_batch
    raise batch.exc_type(batch.exc_msg)
RuntimeError: Traceback (most recent call last):
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 57, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in <listcomp>
    return [default_collate(samples) for samples in transposed]
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 115, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 3 and 2 in dimension 1 at /pytorch/aten/src/TH/generic/THTensorMath.c:3586

It seems that one of the images have only 2 channels. can you print the name and size of each image in this method and check which image causes the problem?

1 Like

The dataset I use is corel5k , all images are belong to it . Do you mean that one of them doesn’t fit my model ?

If so , what can I do ?

Thanks for reply!

1 Like

I mean, this error (RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 3 and 2 in dimension 1) tells that one of the loaded images is of 2 channels. maybe, as a diagnosis, change the __getitem__() method as:

	def __getitem__(self, item):
		image, label = self.imgs[item]
		image = Image.open(image)
		img = transform(image)
        print(image, img.shape)
		return img, label

and check if any of the image gives 2-channels.

1 Like
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hu/.local/lib/python3.6/site-packages/visdom/__init__.py", line 446, in _send
    data=json.dumps(msg),
  File "/home/hu/.local/lib/python3.6/site-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/home/hu/.local/lib/python3.6/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/hu/.local/lib/python3.6/site-packages/requests/sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/hu/.local/lib/python3.6/site-packages/requests/sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "/home/hu/.local/lib/python3.6/site-packages/requests/adapters.py", line 513, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8097): Max retries exceeded with url: /events (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f92289e3358>: Failed to establish a new connection: [Errno 111] Connection refused',))
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A45F8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8> torch.Size([3, 224, 224])
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588> torch.Size([3, 224, 224])
Traceback (most recent call last):
  File "/home/hu/下载/Corel5k (3).py", line 217, in <module>
    for i, (input, target) in  enumerate(testloader):
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 286, in __next__
    return self._process_next_batch(batch)
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 307, in _process_next_batch
    raise batch.exc_type(batch.exc_msg)
RuntimeError: Traceback (most recent call last):
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 57, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in <listcomp>
    return [default_collate(samples) for samples in transposed]
  File "/home/hu/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 115, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 4 and 3 in dimension 1 at /pytorch/aten/src/TH/generic/THTensorMath.c:3586

Here is the printed result.

I couldn’t figure out what exactly wrong so far.

Add this in your __getitem__() method. It might solve the issue.

image = Image.open(image).convert('RGB')

I will let you know if I get any pointers.

3 Likes

Ok , anyway , thanks a lot for your answer !

ps. After adding image = Image.open(image).convert('RGB') the error information didn’t change.

There is a mistake in your code?
you are using trainset in your testloader? Is it intended?

No , it’s my partner’s mistake .Thanks to your message , I’ve fixed it just now .

After some discussion , I think the reason is :

Normally , for one-label image , dataset return the label (type:list) , and dataLoader make it turn to tensor .

But for multi-label image , dataLoader can’t turn it to tensor , which cause a bug we meet before : type ‘list’ doesn’t have the attribute ‘to’. (the code that makes it is “target.to(device)”)

To fix that problem , we use torch.from_numpy to turn it to tensor in dataset

lableList = list(map(int, words))
			lableList = np.array(lableList)
			lableList = torch.from_numpy(lableList)
			imgs.append((imageList, lableList))
		self.imgs = imgs

And here is the problem . The number of our image’s labels is not certain (some pictures got 3 , some got 4, some 2),and that makes the length of the labeList is not certain .When it turn to tensor , the dimension 0 is not certain (some 3,some 2), and this is the reason that cause the error.

Is my thought right ? If so , how can I solve the problem caused by “number of labels is not certain” ?
Appreciate for reply!:slight_smile:

I see. You can use a custom collate function to get around this. Similar to:

Thanks for reply , however ,the quote seems not dealing with the problem we are facing .

We want to deal with the problem that the number of labels is not certain , but the quote takes care of the variable size of the images.

I tried to imitate it to write “my_collate” , but it didn’t work .

Yes. It is not dealing with your exact issue. I was hoping, it might give you some idea.
Please try-out the following collate function:

import torch
import numpy as np
import torch.utils.data as data

def my_collate(batch):
    data = torch.stack([item[0].unsqueeze(0) for item in batch], 0)
    target = torch.Tensor([item[1] for item in batch])
    return [data, target]

class dataset(data.Dataset):
    def __init__(self):
        super(dataset, self).__init__()

    def __len__(self):
        return 100
    
    def __getitem__(self, index):
        return torch.rand(5, 6), list(range(index))

dataloader = data.DataLoader(dataset=dataset(),
                      batch_size=4,
                      shuffle=True,
                      collate_fn=my_collate, # use custom collate function here
                      pin_memory=True)

for instance in dataloader:
    print(instance[0].shape, len(instance[1]))
    for labels in instance[1]:
        print('length', len(labels))
    raw_input()

It didn’t work :slightly_frowning_face: Error message told me that " ‘list’ has no attribute ‘cuda’ " , seems target didn’t turn to tensor.

I hope you had already solved the issue!

i have exactly problem as yours description,have you solve this problem?i also try this Manually calculate loss over a number of images and then back propagate the average loss and update network weight but the net didn’t converge,i 'd be very glad for your reply

RuntimeError Traceback (most recent call last)
in ()
6
7 for epoch in range(epochs):
----> 8 for inputs, labels in trainloader:
9 steps += 1
10 inputs, labels = inputs.to(device), labels.to(device)

~/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py in next(self)
613 if self.num_workers == 0: # same-process loading
614 indices = next(self.sample_iter) # may raise StopIteration
–> 615 batch = self.collate_fn([self.dataset[i] for i in indices])
616 if self.pin_memory:
617 batch = pin_memory_batch(batch)

~/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py in default_collate(batch)
230 elif isinstance(batch[0], container_abcs.Sequence):
231 transposed = zip(*batch)
–> 232 return [default_collate(samples) for samples in transposed]
233
234 raise TypeError((error_msg.format(type(batch[0]))))

~/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py in (.0)
230 elif isinstance(batch[0], container_abcs.Sequence):
231 transposed = zip(*batch)
–> 232 return [default_collate(samples) for samples in transposed]
233
234 raise TypeError((error_msg.format(type(batch[0]))))

~/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py in default_collate(batch)
207 storage = batch[0].storage().new_shared(numel)
208 out = batch[0].new(storage)
–> 209 return torch.stack(batch, 0, out=out)
210 elif elem_type.module == ‘numpy’ and elem_type.name != 'str

211 and elem_type.name != ‘string_’:

RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 344 and 339 in dimension 3 at /pytorch/aten/src/TH/generic/THTensorMoreMath.cpp:1307