TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not int!

Du-Minmin: <p>Please, could anyone help me check where I did wrong?<br>
When I run, an error occurs. I can’t solve it.<br>
please any help or suggestions would be highly appreciated</p>
<pre><code class="lang-auto">model = torch.load('./media/model_best.pth')
model = torch.nn.DataParallel(model['model_def'](20))
model.load_state_dict(model['state_dict'])
model.cuda()
model.eval()
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/2/b21cf5aec3941b10d00a62bf74f6b6db058c3380.png" data-download-href="https://discuss.pytorch.org/uploads/default/b21cf5aec3941b10d00a62bf74f6b6db058c3380" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/2/b21cf5aec3941b10d00a62bf74f6b6db058c3380.png" alt="image" data-base62-sha1="ppF2cS3cTo8Ca9yIn8iuVWH1gvm" width="690" height="260" data-dominant-color="363534"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1005×380 53.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>Your current code snippet doesn’t show the usage of the model, but based on the error message it seems your are passing an <code>Int</code> into the model via:</p>
<pre><code class="lang-python">data = 1
output = model(data)
</code></pre>
<p>Could you check the dtype of the inputs and make sure they are not overwritten by other variables with the same name?</p>

Du-Minmin: <p>I don’t know where to view the dtype.The model is the .pth file saved at the end of training.The model is the pth file saved at the end of training.I don’t know why <code>Int</code> is passed in.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/a/3a194117378c6226345eb07525ae57454dabf172.png" data-download-href="https://discuss.pytorch.org/uploads/default/3a194117378c6226345eb07525ae57454dabf172" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/a/3a194117378c6226345eb07525ae57454dabf172.png" alt="image" data-base62-sha1="8hXNCjqQxXDfvUd2VINWPL9oUoi" width="690" height="264" data-dominant-color="46494D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1178×452 55.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>I don’t think the model is loaded in a wrong way, but the data you are trying to pass to the model.<br>
You can check it via:</p>
<pre><code class="lang-python">print(type(data))
</code></pre>
<p>assuming <code>data</code> is the input to your model.</p>

Du-Minmin: <p>Can I send the code to your mailbox? Could you please help me to see it?</p>

ptrblck: <p>Please post code here directly by wrapping it into three backticks ```.</p>

Du-Minmin: <pre><code class="lang-auto">import os
import cv2
import time
import torch
import numpy as np
from argparse import ArgumentParser

import transforms

parser = ArgumentParser(description='e-Lab Segmentation Visualizer')
_ = parser.add_argument
_('--model_path', type=str, default='/media/', help='model to load')
_('--data_path',  type=str, default='/media/', help='image folder')
_('--mode',       type=int, default=0, help='mode 0, 1, 2')
_('--fullscreen',   action='store_true', help='Show output in full screen')

args = parser.parse_args()

# Clear screen
print('\033[0;0f\033[0J')
# Color Palette
CP_R = '\033[31m'
CP_G = '\033[32m'
CP_B = '\033[34m'
CP_Y = '\033[33m'
CP_C = '\033[0m'

# Define color scheme
color_map = np.array([
    [0, 0, 0],        # Unlabled
    [128, 64, 128],   # Road
    [244, 35, 232],   # Sidewalk
    [70, 70, 70],     # Building
    [102, 102, 156],  # Wall
    [190, 153, 153],  # Fence
    [153, 153, 153],  # Pole
    [250, 170, 30],   # Traffic light
    [220, 220, 0],    # Traffic signal
    [107, 142, 35],   # Vegetation
    [152, 251, 152],  # Terrain
    [70, 130, 180],   # Sky
    [220, 20, 60],    # Person
    [255, 0, 0],      # Rider
    [0, 0, 142],      # Car
    [0, 0, 70],       # Truck
    [0, 60, 100],     # Bus
    [0, 80, 100],     # Train
    [0, 0, 230],      # Motorcycle
    [119, 11, 32]     # Bicycle
], dtype=np.uint8)
#device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# Load model
model = torch.load('./media/model_best.pth')
model = torch.nn.DataParallel(model['model_def'](20))
model.load_state_dict(model['state_dict'])
model.cuda()
model.eval()

root_dir = os.path.join(args.data_path, 'stuttgart_0' + str(args.mode))
first_idx = [1, 3500, 5100]
last_idx = [599, 4599, 6299]
idx = first_idx[args.mode]
fps = 'NA'
pred_map = np.zeros((256, 512, 3), dtype=np.uint8)
pred_map = np.zeros((512, 1024, 3), dtype=np.uint8)

win_title = 'Overlayed Image'
if args.fullscreen:
    cv2.namedWindow(win_title, cv2.WND_PROP_FULLSCREEN)
    cv2.setWindowProperty(win_title, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

while idx &lt;= last_idx[args.mode]:
    # Load image, resize and convert into a 'batchified' cuda tensor
    start_time = time.time()
    filename = '{}/stuttgart_0{:d}_000000_{:06d}_leftImg8bit.png'.format(root_dir, args.mode, idx)

    if os.path.isfile(filename):
        x = cv2.imread(filename)
        read_time = time.time() - start_time

        resize = transforms.Resize(0.5)
        x = resize(x)
        prep_data = transforms.Compose([
            #transforms.Crop((512, 512)),
            transforms.ToTensor(),
            transforms.Normalize([[0.485, 0.456, 0.406], [0.229, 0.224, 0.225]])
            ])
        input_image = prep_data(x)
        #input_image = torch.from_numpy(cv2.cvtColor(x, cv2.COLOR_BGR2RGB).transpose(2, 0, 1))/255
        input_image = input_image.unsqueeze(0).float().cuda()
        prep_time = time.time() - start_time - read_time

        # Get neural network output
        y = model(torch.autograd.Variable(input_image))
        y = y.squeeze()
        pred = y.data.gpu().numpy()
        model_time = time.time() - start_time - read_time - prep_time

        # Calculate prediction and colorized segemented output
        prediction = np.argmax(pred, axis=0)
        num_classes = 20
        pred_map *= 0
        for i in range(num_classes):
            pred_map[prediction == i] = color_map[i]

        pred_map_BGR = cv2.cvtColor(pred_map, cv2.COLOR_RGB2BGR)
        overlay = cv2.addWeighted(x, 0.5, pred_map_BGR, 0.5, 0)
        pred_time = time.time() - start_time - read_time - prep_time - model_time

        #cv2.imshow('Original Image', x_rescaled)
        #cv2.imshow('Segmented Output', pred_map_BGR)
        cv2.imshow(win_title, overlay)
        disp_time = time.time() - start_time - read_time - prep_time - model_time - pred_time
        fps = 1/(time.time() - start_time)

        print("{}Read: {}{:4.2f} ms | {}Norm:: {}{:4.2f} ms | {}Model: {}{:4.2f} ms | {}Predict: {}{:4.2f} ms | {}Display: {}{:4.2f} ms".format(
            CP_Y, CP_C, read_time*1000, CP_G, CP_C, prep_time*1000, CP_G, CP_C, model_time*1000,
            CP_R, CP_C, pred_time*1000, CP_B, CP_C, disp_time*1000))
    else:
        print("{}Warning{}!!! {}{} image unavailable{}.".format(CP_R, CP_C, filename, CP_R, CP_C))

    idx += 1
    if cv2.waitKey(1) == 27: # ESC to stop
        break


</code></pre>

ptrblck: <p>The code looks generally alright, besides the usage of the deprecated <code>Variable</code>, which should be removed.</p>
<p>Nevertheless <code>input_image</code> should be a tensor. Otherwise the previous lines of code should already create errors.</p>
<p>Since the code is not executable, I guess that in the model’s <code>forward</code> method some integers might be passed to specific layers.</p>

Du-Minmin: <p>Okay, thank you very much for your answers, I will try to debug it myself</p>

ptrblck: <p>Sure, if you get stuck feel free to post more code snippets we can have a look at and, in the best case, we could execute to isolate the issue.</p>

Du-Minmin: <p>okk,I use this code at <a href="https://github.com/e-lab/pytorch-linknet" rel="noopener nofollow ugc">https://github.com/e-lab/pytorch-linknet</a>.</p>

ptrblck: <p>Which part of the code are you using, i.e. is the error reproducible by creating I creating the <code>LinkNet</code> and passing some tensor inputs to it? If so, which shape should these inputs have?</p>

Du-Minmin: <p>I run the <code>main.py</code>,this error appeared when I  run <code>visualize.py</code>.</p>

ptrblck: <p>Please to try write an executable code snippet using <code>visualize.py</code> so that we can further debug.</p>

Du-Minmin: <p>Thank you very much for your patient answers, I have solved this problem.</p>