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

Git-oNmE: <pre><code class="lang-auto">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("=&gt; 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("=&gt; 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 &gt; 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))
</code></pre>
<p><strong>Above is my code,when I ran it,it told me:</strong></p>
<pre><code class="lang-auto">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 &lt;listcomp&gt;
    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)
</code></pre>
<p><strong>How can I solve it?<br>
I’m a begginer.Appreciate for answering:)</strong></p>

InnovArul: <p>The error message doesn’t show where in your file the error occurs.<br>
Please paste full error message.</p>

Git-oNmE: <pre><code class="lang-auto">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: &lt;urllib3.connection.HTTPConnection object at 0x7f685cc1a6a0&gt;: 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('&lt;urllib3.connection.HTTPConnection object at 0x7f685cc1a6a0&gt;: 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('&lt;urllib3.connection.HTTPConnection object at 0x7f685cc1a6a0&gt;: 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: &lt;urllib3.connection.HTTPConnection object at 0x7f685b0b72e8&gt;: 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('&lt;urllib3.connection.HTTPConnection object at 0x7f685b0b72e8&gt;: 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('&lt;urllib3.connection.HTTPConnection object at 0x7f685b0b72e8&gt;: Failed to establish a new connection: [Errno 111] Connection refused',))
Traceback (most recent call last):
  File "/home/hu/下载/Corel5k (3).py", line 215, in &lt;module&gt;
    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 &lt;listcomp&gt;
    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
</code></pre>

InnovArul: <aside class="quote no-group quote-modified" data-username="Git-oNmE" data-post="1" data-topic="23890">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/a8b319/48.png" class="avatar"> Git-oNmE:</div>
<blockquote>
<p>def <strong>getitem</strong>(self, item): image, label = self.imgs[item] image = Image.open(image) img = transform(image) return img, label</p>
</blockquote>
</aside>
<p>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?</p>

Git-oNmE: <p>The dataset I use is corel5k , all images are belong to it . Do you mean that one of them doesn’t fit my model ?</p>
<p>If so , what can I do ?</p>
<p>Thanks for reply!</p>

InnovArul: <p>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 <code>__getitem__()</code> method as:</p>
<pre><code class="lang-auto">	def __getitem__(self, item):
		image, label = self.imgs[item]
		image = Image.open(image)
		img = transform(image)
        print(image, img.shape)
		return img, label
</code></pre>
<p>and check if any of the image gives 2-channels.</p>

Git-oNmE: <pre><code class="lang-auto">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('&lt;urllib3.connection.HTTPConnection object at 0x7f92289e3358&gt;: Failed to establish a new connection: [Errno 111] Connection refused',))
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4518&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x192 at 0x7F92289A45F8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A44A8&gt; torch.Size([3, 224, 224])
&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x128 at 0x7F92289A4588&gt; torch.Size([3, 224, 224])
Traceback (most recent call last):
  File "/home/hu/下载/Corel5k (3).py", line 217, in &lt;module&gt;
    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 &lt;listcomp&gt;
    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
</code></pre>
<p>Here is the printed result.</p>

InnovArul: <p>I couldn’t figure out what exactly wrong so far.</p>
<p>Add this in your <code>__getitem__()</code> method. It might solve the issue.</p>
<pre><code class="lang-auto">image = Image.open(image).convert('RGB')
</code></pre>
<p>I will let you know if I get any pointers.</p>

Git-oNmE: <p>Ok , anyway , thanks a lot for your answer !</p>

Git-oNmE: <p>ps. After adding <code>image = Image.open(image).convert('RGB')</code> the error information didn’t change.</p>

InnovArul: <aside class="quote no-group quote-modified" data-username="Git-oNmE" data-post="1" data-topic="23890">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/a8b319/48.png" class="avatar"> Git-oNmE:</div>
<blockquote>
<p>testset = LoadDataset(txt = root + ‘labels/test_label’, transform = transform) testloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True, num_workers=2)</p>
</blockquote>
</aside>
<p>There is a mistake in your code?<br>
you are using <code>trainset</code> in your <code>testloader</code>? Is it intended?</p>

Git-oNmE: <p>No , it’s my partner’s mistake .Thanks to your message , I’ve fixed it just now .</p>

Git-oNmE: <p>After some discussion , I think the reason is :</p>
<p>Normally , for one-label image , dataset return the label (type:list) , and dataLoader make it turn to tensor .</p>
<p>But for multi-label image , dataLoader can’t turn it to tensor , which cause a bug we meet before : <em>type ‘list’ doesn’t have the attribute ‘to’</em>. (the code that makes it is “<em>target.to(device)</em>”)</p>
<p>To fix that problem , we use torch.from_numpy to turn it to tensor in dataset</p>
<pre><code class="lang-auto">lableList = list(map(int, words))
			lableList = np.array(lableList)
			lableList = torch.from_numpy(lableList)
			imgs.append((imageList, lableList))
		self.imgs = imgs
</code></pre>
<p>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.</p>
<p>Is my thought right ? If so , how can I solve the problem caused by “number of labels is not certain” ?<br>
Appreciate for reply!<img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=6" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

InnovArul: <p>I see. You can use a custom collate function to get around this. Similar to:</p>
<aside class="quote quote-modified" data-post="3" data-topic="8278">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jdhao/48/2558_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-to-create-a-dataloader-with-variable-size-input/8278/3">How to create a dataloader with variable-size input</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    By default, torch stacks the input image to from a tensor of size N*C*H*W, so every image in the batch must have the same height and width. In order to load a batch with variable size input image, we have to use our own collate_fn which is used to pack a batch of images. 
For image classification, the input to collate_fn is a list of with size batch_size. Each element is a tuple where the first element is the input image(a torch.FloatTensor) and the second element is the image label which is sim…
  </blockquote>
</aside>


Git-oNmE: <p>Thanks for reply , however ,the quote seems not dealing with the problem we are facing .</p>
<p>We want to deal with the problem that <strong><em>the number of labels is not certain</em></strong> , but the quote takes care of the variable size of the images.</p>
<p>I tried to imitate it to write “my_collate” , but it didn’t work .</p>

InnovArul: <p>Yes. It is not dealing with your exact issue. I was hoping, it might give you some idea.<br>
Please try-out the following collate function:</p>
<pre><code class="lang-auto">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()

</code></pre>

Git-oNmE: <p>It didn’t work <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_frowning_face.png?v=6" title=":slightly_frowning_face:" class="emoji" alt=":slightly_frowning_face:"> Error message told me that " ‘list’ has no attribute ‘cuda’ " , seems target didn’t turn to tensor.</p>

InnovArul: <p>I hope you had already solved the issue!</p>

yajun_li: <p>i have exactly problem as yours description,have you solve this problem?i also try this <a href="https://discuss.pytorch.org/t/manually-calculate-loss-over-a-number-of-images-and-then-back-propagate-the-average-loss-and-update-network-weight/9530?u=yajun_li" class="inline-onebox">Manually calculate loss over a number of images and then back propagate the average loss and update network weight</a> but the net didn’t converge,i 'd be very glad for your reply</p>

Sarvabhowma_Sanapala: <p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
6<br>
7 for epoch in range(epochs):<br>
----&gt; 8     for inputs, labels in trainloader:<br>
9         steps += 1<br>
10         inputs, labels = inputs.to(device), labels.to(device)</p>
<p>~/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py in <strong>next</strong>(self)<br>
613         if self.num_workers == 0:  # same-process loading<br>
614             indices = next(self.sample_iter)  # may raise StopIteration<br>
–&gt; 615             batch = self.collate_fn([self.dataset[i] for i in indices])<br>
616             if self.pin_memory:<br>
617                 batch = pin_memory_batch(batch)</p>
<p>~/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py in default_collate(batch)<br>
230     elif isinstance(batch[0], container_abcs.Sequence):<br>
231         transposed = zip(*batch)<br>
–&gt; 232         return [default_collate(samples) for samples in transposed]<br>
233<br>
234     raise TypeError((error_msg.format(type(batch[0]))))</p>
<p>~/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py in (.0)<br>
230     elif isinstance(batch[0], container_abcs.Sequence):<br>
231         transposed = zip(*batch)<br>
–&gt; 232         return [default_collate(samples) for samples in transposed]<br>
233<br>
234     raise TypeError((error_msg.format(type(batch[0]))))</p>
<p>~/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py in default_collate(batch)<br>
207             storage = batch[0].storage().<em>new_shared(numel)<br>
208             out = batch[0].new(storage)<br>
–&gt; 209         return torch.stack(batch, 0, out=out)<br>
210     elif elem_type.<strong>module</strong> == ‘numpy’ and elem_type.<strong>name</strong> != 'str</em>’ <br>
211             and elem_type.<strong>name</strong> != ‘string_’:</p>
<p>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</p>