Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Error process with input png file #1064

Closed
blinkbink opened this issue Sep 29, 2020 · 6 comments
Closed

Error process with input png file #1064

blinkbink opened this issue Sep 29, 2020 · 6 comments
Labels
bug Something isn't working Stale Stale and schedule for closing soon

Comments

@blinkbink
Copy link

results.txt

results

🐛 Bug

A clear and concise description of what the bug is.
try to detect object with image png, some case png working as well but some case got error :

RuntimeError: Given groups=1, weight of size [32, 12, 3, 3], expected input[1, 16, 224, 320] to have 12 channels, but got 16 channels instead

To Reproduce (REQUIRED)

make a little change for detect under API with flask, from file detect.py
the function of detect from main file :
Input:

def detect(id, base64):
    try:
        out, source, view_img, save_txt, imgsz = \
            'datapredict', '', False, True, 640
        webcam = source.isnumeric() or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt')
        # Initialize
        set_logging()

        # if os.path.exists(out):
        #     shutil.rmtree(out)  # delete output folder
        # os.makedirs(out)  # make new output folder
        half = device.type != 'cpu'  # half precision only supported on CUDA

        imgsz = check_img_size(imgsz, s=model.stride.max())  # check img_size
        if half:
            model.half()  # to FP16

        # Second-stage classifier
        classify = False
        if classify:
            modelc = load_classifier(name='resnet101', n=2)  # initialize
            modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model'])  # load weights
            modelc.to(device).eval()

        save_img = True
        dataset = LoadImages(id, base64, source, img_size=imgsz)

        # Get names and colors
        names = model.module.names if hasattr(model, 'module') else model.names
        colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))]

        # Run inference
        t0 = time.time()
        img = torch.zeros((1, 3, imgsz, imgsz), device=device)  # init img
        _ = model(img.half() if half else img) if device.type != 'cpu' else None  # run once

        for path, img, im0s in dataset:

            img = torch.from_numpy(img).to(device)
            img = img.half() if half else img.float()  # uint8 to fp16/32
            img /= 255.0  # 0 - 255 to 0.0 - 1.0
            if img.ndimension() == 3:
                img = img.unsqueeze(0)

            # Inference
            t1 = time_synchronized()
            pred = model(img, augment=False)[0]

            # Apply NMS
            pred = non_max_suppression(pred, 0.60, 0.7, classes=0, agnostic=False)

            if pred == [None] * len(pred):
                logger.info(id + " : Not E-KTP ")
                return jsonify(object="Not E-KTP", score=0.0, result='16')
            t2 = time_synchronized()

            # Apply Classifier
            if classify:
                pred = apply_classifier(pred, modelc, img, im0s)

            # Process detections
            for i, det in enumerate(pred):  # detections per image
                if webcam:  # batch_size >= 1
                    p, s, im0 = path[i], '%g: ' % i, im0s[i].copy()
                else:
                    p, s, im0 = path, '', im0s

                save_path = str(Path(out) / Path(p).name)
                txt_path = str(Path(out) / Path(p).stem) + ('_%g' % dataset.frame if dataset.mode == 'video' else '')
                s += '%gx%g ' % img.shape[2:]  # print string
                gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  # normalization gain whwh
                if det is not None and len(det):
                    # Rescale boxes from img_size to im0 size
                    det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()

                    # Print results
                    for c in det[:, -1].unique():
                        n = (det[:, -1] == c).sum()  # detections per class
                        s += '%g %ss, ' % (n, names[int(c)])  # add to string

                    # Write results
                    for *xyxy, conf, cls in reversed(det):
                        # if save_txt:  # Write to file
                        #     xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()  # normalized xywh
                        #     with open(txt_path + '.txt', 'a') as f:
                        #         f.write(('%g ' * 5 + '\n') % (cls, *xywh))  # label format

                        if save_img or view_img:  # Add bbox to image
                            label = '%s %.2f' % (names[int(cls)], conf)
                            object = names[int(cls)]

                            score = '%.5f' % (conf)
                            plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)

                # Print time (inference + NMS)
                logger.info(id + ' : %sDone. (%.3fs)' % (s, t2 - t1))

                is_completed, buf = cv2.imencode(".jpg", im0)
                byte_buffer = io.BytesIO(buf.tostring())

                # Stream results
                if view_img:
                    cv2.imshow(p, im0)
                    if cv2.waitKey(1) == ord('q'):  # q to quit
                        raise StopIteration

                # Save results (image with detections)
                cv2.imwrite(save_path, im0)

        # if save_txt or save_img:
        #     if platform.system() == 'Darwin' and not opt.update:  # MacOS
        #         os.system('open ' + save_path)

        logger.info(id + ' : Done. (%.3fs)' % (time.time() - t0))
        logger.info(id + ' : Prediction is E-' + object + " Score : " + score)

        return jsonify(object=str(object), score=str(score), result='15')

        # logger.info(id + " : Send file as image buffer")
        # return send_file(byte_buffer,"image/jpeg",as_attachment=True,attachment_filename="output.jpg")
    except Exception as error:
        logger.error(error)
        traceback.print_exc()
        return jsonify(info='Cant process image', result='88')

then from utils make change on function LoadImages:
`class LoadImages: # for inference
def init(self, id, base64data, path, img_size=640):

    self.img_size = img_size
    self.id = id
    self.mode = 'images'
    self.encodedbase64 = base64data


def __iter__(self):
    self.count = 0
    return self

def __next__(self):
    if self.count == 1:
        raise StopIteration
    path = "/var/www/html/classifier/object_detection/yolov5/datapredict/"+str(self.id)+".jpg"

    self.count += 1
    decoded_data = base64.b64decode(self.encodedbase64)
    np_data = np.fromstring(decoded_data, np.uint8)

    img0 = cv2.imdecode(np_data, cv2.IMREAD_UNCHANGED)

    # img0 = cv2.imread(path)  # BGR
    assert img0 is not None, 'Image Not Found ' + path
    # print('image %g/%g %s: ' % (self.count, self.nf, path), end='')

    # Padded resize
    img = letterbox(img0, new_shape=self.img_size)[0]

    # Convert
    img = img[:, :, ::-1].transpose(2, 0, 1)  # BGR to RGB, to 3x416x416
    img = np.ascontiguousarray(img)

    return path, img, img0

`

Output:

Traceback (most recent call last):
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/var/www/html/classifier/object_detection/yolov5/apidetection.py", line 69, in detectionKTP
    result = detect(id, base64.b64encode(ktp.read()))
  File "/var/www/html/classifier/object_detection/yolov5/apidetection.py", line 118, in detect
    pred = model(img, augment=False)[0]
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/var/www/html/classifier/object_detection/yolov5/models/yolo.py", line 113, in forward
    return self.forward_once(x, profile)  # single-scale inference, train
  File "/var/www/html/classifier/object_detection/yolov5/models/yolo.py", line 133, in forward_once
    x = m(x)  # run
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/var/www/html/classifier/object_detection/yolov5/models/common.py", line 88, in forward
    return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/var/www/html/classifier/object_detection/yolov5/models/common.py", line 32, in fuseforward
    return self.act(self.conv(x))
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 419, in forward
    return self._conv_forward(input, self.weight)
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 416, in _conv_forward
    self.padding, self.dilation, self.groups)
RuntimeError: Given groups=1, weight of size [32, 12, 3, 3], expected input[1, 16, 224, 320] to have 12 channels, but got 16 channels instead

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/var/www/html/classifier/object_detection/yolov5/yoloenvi/lib/python3.6/site-packages/flask_api/app.py", line 96, in handle_user_exception
    app_handlers = self.error_handler_spec[None].get(None, ())
KeyError: None

Expected behavior

can perform with png extension because can't perform detect in some case if the file extension with .png

Environment

  • OS: Ubuntu 18.04
  • GPU Tesla V100 32Gb

Additional context

@blinkbink blinkbink added the bug Something isn't working label Sep 29, 2020
@Aktcob
Copy link

Aktcob commented Sep 30, 2020

it seemed that the input shape of img is [4, H, W], not [3, H, W]. U can check out this by simply printing the shape of input tensor.

@blinkbink
Copy link
Author

blinkbink commented Sep 30, 2020

the input just allowed [3, H, W] ?

@glenn-jocher
Copy link
Member

glenn-jocher commented Oct 4, 2020

@blinkbink image input dimensions are (1,3,H,W) for batch size 1.

@Aktcob
Copy link

Aktcob commented Oct 9, 2020

@blinkbink if the input size is [4, H, W], simply change like that:

class Focus(nn.Module):
# Focus wh information into c-space
def init(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
super(Focus, self).init()
self.conv = Conv(16, c2, k, s, p, g, act). #16 = 4*4

def forward(self, x):  # x(b,c,w,h) -> y(b,4c,w/2,h/2)
    return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))

@blinkbink
Copy link
Author

blinkbink commented Oct 15, 2020

@blinkbink image input dimensions are (1,3,H,W) for batch size 1.

@glenn-jocher the batch size determine on train ? i use --batch-size 64

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale Stale and schedule for closing soon label Nov 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale Stale and schedule for closing soon
Projects
None yet
Development

No branches or pull requests

3 participants