hv0905 / NekoImageGallery

An AI-powered natural language & reverse Image Search Engine powered by CLIP & qdrant.
https://image-insights.edgeneko.com/
GNU Affero General Public License v3.0
80 stars 9 forks source link

[bug]对于有大量文字内容的图片,在上传时可能出现 The size of tensor a (570) must match the size of tensor b (512) at non-singleton dimension 1 #33

Closed Natsukage closed 4 months ago

Natsukage commented 4 months ago

其他图片都可以通过上传端点正常上传

当图片本身包含文字过多时(例如QQ群聊中其他人发送的截图) upload端点会返回200正常上传,但是在NekoImageGallery会报错(日志如下) 此时实际上文件并未真正上传完毕,目录中不存在对应的文件,再次上传时候依然会报告200正常上传并再次出现同样错误。 由于上传端无法得知错误发生,因此陷入了循环

2024-06-07 19:34:58.307 | INFO     | app.Services.upload_service:_upload_task:44 - Start indexing image b9d9ae4a-5b6d-5d84-b5d8-dba85e5d75c4. Local: True. Size: 102099
2024-06-07 19:34:58.307 | INFO     | app.Services.vector_db_context:validate_ids:98 - Validating 1 items from database...
2024-06-07 19:34:58.312 | INFO     | app.Services.transformers_service:get_image_vector:35 - Processing image...
2024-06-07 19:34:58.317 | SUCCESS  | app.Services.transformers_service:get_image_vector:38 - Image processed, now Inferring with CLIP model...
2024-06-07 19:34:58.330 | SUCCESS  | app.Services.transformers_service:get_image_vector:40 - Inference done. Time elapsed: 0.02s
2024-06-07 19:34:58.331 | INFO     | app.Services.ocr_services:ocr_interface:58 - Processing text with EasyPaddleOCR...
2024-06-07 19:34:58.581 | SUCCESS  | app.Services.ocr_services:ocr_interface:60 - OCR processed done. Time elapsed: 0.25s
2024-06-07 19:34:58.581 | INFO     | app.Services.transformers_service:get_bert_vector:58 - Inferring with BERT model...
2024-06-07 19:34:58.584 | ERROR    | app.Services.upload_service:_upload_worker:34 - Error occurred while uploading image b9d9ae4a-5b6d-5d84-b5d8-dba85e5d75c4
2024-06-07 19:34:58.585 | ERROR    | app.Services.upload_service:_upload_worker:35 - The size of tensor a (570) must match the size of tensor b (512) at non-singleton dimension 1
Traceback (most recent call last):

  File "E:\NekoImageGallery\main.py", line 71, in <module>
    uvicorn.run("app.webapp:app", host=args.host, port=args.port, root_path=args.root_path)
    │       │                          │    │          │    │               │    └ ''
    │       │                          │    │          │    │               └ Namespace(show_config=False, init_database=False, migrate_from_version=None, local_index_target_dir=None, local_create_thumbn...
    │       │                          │    │          │    └ 8000
    │       │                          │    │          └ Namespace(show_config=False, init_database=False, migrate_from_version=None, local_index_target_dir=None, local_create_thumbn...
    │       │                          │    └ '0.0.0.0'
    │       │                          └ Namespace(show_config=False, init_database=False, migrate_from_version=None, local_index_target_dir=None, local_create_thumbn...
    │       └ <function run at 0x0000013E7E3B00E0>
    └ <module 'uvicorn' from 'E:\\Users\\Summer\\.conda\\envs\\nekoenv\\Lib\\site-packages\\uvicorn\\__init__.py'>

  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\uvicorn\main.py", line 577, in run
    server.run()
    │      └ <function Server.run at 0x0000013E7E3EB740>
    └ <uvicorn.server.Server object at 0x0000013E7E396410>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\uvicorn\server.py", line 65, in run
    return asyncio.run(self.serve(sockets=sockets))
           │       │   │    │             └ None
           │       │   │    └ <function Server.serve at 0x0000013E7E3EB7E0>
           │       │   └ <uvicorn.server.Server object at 0x0000013E7E396410>
           │       └ <function run at 0x0000013E7DB63D80>
           └ <module 'asyncio' from 'E:\\Users\\Summer\\.conda\\envs\\nekoenv\\Lib\\asyncio\\__init__.py'>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           │      │   └ <coroutine object Server.serve at 0x0000013E7E4E5D50>
           │      └ <function Runner.run at 0x0000013E7DC26840>
           └ <asyncio.runners.Runner object at 0x0000013E7E54A390>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           │    │     │                  └ <Task pending name='Task-1' coro=<Server.serve() running at E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\uvicorn\ser...
           │    │     └ <function BaseEventLoop.run_until_complete at 0x0000013E7DC244A0>
           │    └ <ProactorEventLoop running=True closed=False debug=False>
           └ <asyncio.runners.Runner object at 0x0000013E7E54A390>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\base_events.py", line 641, in run_until_complete
    self.run_forever()
    │    └ <function ProactorEventLoop.run_forever at 0x0000013E7DD3B9C0>
    └ <ProactorEventLoop running=True closed=False debug=False>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\windows_events.py", line 321, in run_forever
    super().run_forever()
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\base_events.py", line 608, in run_forever
    self._run_once()
    │    └ <function BaseEventLoop._run_once at 0x0000013E7DC26200>
    └ <ProactorEventLoop running=True closed=False debug=False>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\base_events.py", line 1936, in _run_once
    handle._run()
    │      └ <function Handle._run at 0x0000013E7DB7C5E0>
    └ <Handle Task.task_wakeup(<Future finis...dimension 1')>)>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\events.py", line 84, in _run
    self._context.run(self._callback, *self._args)
    │    │            │    │           │    └ <member '_args' of 'Handle' objects>
    │    │            │    │           └ <Handle Task.task_wakeup(<Future finis...dimension 1')>)>
    │    │            │    └ <member '_callback' of 'Handle' objects>
    │    │            └ <Handle Task.task_wakeup(<Future finis...dimension 1')>)>
    │    └ <member '_context' of 'Handle' objects>
    └ <Handle Task.task_wakeup(<Future finis...dimension 1')>)>

> File "E:\NekoImageGallery\app\Services\upload_service.py", line 31, in _upload_worker
    await self._upload_task(img, img_data, *args)
          │    │            │    │          └ [b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x03\x02\x02\x03\x02\x02\x03\x03\x03\...
          │    │            │    └ ImageData(id=UUID('b9d9ae4a-5b6d-5d84-b5d8-dba85e5d75c4'), url='/static/b9d9ae4a-5b6d-5d84-b5d8-dba85e5d75c4.png', thumbnail_...
          │    │            └ <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=639x704 at 0x13E299CDAD0>
          │    └ <function UploadService._upload_task at 0x0000013E107567A0>
          └ <app.Services.upload_service.UploadService object at 0x0000013EEF3525D0>

  File "E:\NekoImageGallery\app\Services\upload_service.py", line 57, in _upload_task
    await self._index_service.index_image(img, img_data, skip_ocr=skip_ocr, background=True)
          │    │              │           │    │                  └ False
          │    │              │           │    └ ImageData(id=UUID('b9d9ae4a-5b6d-5d84-b5d8-dba85e5d75c4'), url='/static/b9d9ae4a-5b6d-5d84-b5d8-dba85e5d75c4.png', thumbnail_...
          │    │              │           └ <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=639x704 at 0x13E299CDAD0>
          │    │              └ <function IndexService.index_image at 0x0000013E106C6C00>
          │    └ <app.Services.index_service.IndexService object at 0x0000013EF17DDCD0>
          └ <app.Services.upload_service.UploadService object at 0x0000013EEF3525D0>

  File "E:\NekoImageGallery\app\Services\index_service.py", line 50, in index_image
    await run_in_threadpool(self._prepare_image, image, image_data, skip_ocr)
          │                 │    │               │      │           └ False
          │                 │    │               │      └ ImageData(id=UUID('b9d9ae4a-5b6d-5d84-b5d8-dba85e5d75c4'), url='/static/b9d9ae4a-5b6d-5d84-b5d8-dba85e5d75c4.png', thumbnail_...
          │                 │    │               └ <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=639x704 at 0x13E299CDAD0>
          │                 │    └ <function IndexService._prepare_image at 0x0000013E0CE7B600>
          │                 └ <app.Services.index_service.IndexService object at 0x0000013EF17DDCD0>
          └ <function run_in_threadpool at 0x0000013E7F205080>

  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\starlette\concurrency.py", line 42, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
                 │     │         │        │      └ (<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=639x704 at 0x13E299CDAD0>, ImageData(id=UUID('b9d9ae4a-5b6d-5d84-b5d8...
                 │     │         │        └ <bound method IndexService._prepare_image of <app.Services.index_service.IndexService object at 0x0000013EF17DDCD0>>
                 │     │         └ <function run_sync at 0x0000013E7E498EA0>
                 │     └ <module 'anyio.to_thread' from 'E:\\Users\\Summer\\.conda\\envs\\nekoenv\\Lib\\site-packages\\anyio\\to_thread.py'>
                 └ <module 'anyio' from 'E:\\Users\\Summer\\.conda\\envs\\nekoenv\\Lib\\site-packages\\anyio\\__init__.py'>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\anyio\to_thread.py", line 33, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
                 └ <function get_asynclib at 0x0000013E7E46DC60>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\anyio\_backends\_asyncio.py", line 877, in run_sync_in_worker_thread
    return await future
                 └ <Future finished exception=RuntimeError('The size of tensor a (570) must match the size of tensor b (512) at non-singleton di...
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\anyio\_backends\_asyncio.py", line 807, in run
    result = context.run(func, *args)
             │       │   │      └ (<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=639x704 at 0x13E299CDAD0>, ImageData(id=UUID('b9d9ae4a-5b6d-5d84-b5d8...
             │       │   └ <bound method IndexService._prepare_image of <app.Services.index_service.IndexService object at 0x0000013EF17DDCD0>>
             │       └ <method 'run' of '_contextvars.Context' objects>
             └ <_contextvars.Context object at 0x0000013E29A1E080>

  File "E:\NekoImageGallery\app\Services\index_service.py", line 34, in _prepare_image
    image_data.text_contain_vector = self._transformers_service.get_bert_vector(image_data.ocr_text)
    │          │                     │    │                     │               │          └ '萌小新心82024年06月04日08:28不好意思,起猛了,内部人员用内部号欺负玩家,把氪金巅峰榜第一给打跑卖号了,好小众的词汇出现在了赛尔计划收起Q查看大图向 左旋转向右旋转上午8:27.<群公告**.皮皮2024年6月4日02:3679人已读亲爱...
    │          │                     │    │                     │               └ ImageData(id=UUID('b9d9ae4a-5b6d-5d84-b5d8-dba85e5d75c4'), url='/static/b9d9ae4a-5b6d-5d84-b5d8-dba85e5d75c4.png', thumbnail_...
    │          │                     │    │                     └ <function TransformersService.get_bert_vector at 0x0000013E0CE7A2A0>
    │          │                     │    └ <app.Services.transformers_service.TransformersService object at 0x0000013E1083FC10>
    │          │                     └ <app.Services.index_service.IndexService object at 0x0000013EF17DDCD0>
    │          └ None
    └ ImageData(id=UUID('b9d9ae4a-5b6d-5d84-b5d8-dba85e5d75c4'), url='/static/b9d9ae4a-5b6d-5d84-b5d8-dba85e5d75c4.png', thumbnail_...

  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\torch\utils\_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
           │     │       └ {}
           │     └ (<app.Services.transformers_service.TransformersService object at 0x0000013E1083FC10>, '萌小新心82024年06月04日08:28不好意思,起猛了,内部人员用内部...
           └ <function TransformersService.get_bert_vector at 0x0000013E0CE4CE00>

  File "E:\NekoImageGallery\app\Services\transformers_service.py", line 60, in get_bert_vector
    outputs = self._bert_model(**inputs)
              │    │             └ {'input_ids': tensor([[  101,  5846,  2207,  3173,  2552, 10398,  8682,  2399,  8116,  3299,
              │    │                         8147,  3189,  8142,   ...
              │    └ BertModel(
              │        (embeddings): BertEmbeddings(
              │          (word_embeddings): Embedding(21128, 768, padding_idx=0)
              │          (position_embeddin...
              └ <app.Services.transformers_service.TransformersService object at 0x0000013E1083FC10>

  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\torch\nn\modules\module.py", line 1552, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           │    │           │       └ {'input_ids': tensor([[  101,  5846,  2207,  3173,  2552, 10398,  8682,  2399,  8116,  3299,
           │    │           │                   8147,  3189,  8142,   ...
           │    │           └ ()
           │    └ <function Module._call_impl at 0x0000013E07766B60>
           └ BertModel(
               (embeddings): BertEmbeddings(
                 (word_embeddings): Embedding(21128, 768, padding_idx=0)
                 (position_embeddin...
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\torch\nn\modules\module.py", line 1561, in _call_impl
    return forward_call(*args, **kwargs)
           │             │       └ {'input_ids': tensor([[  101,  5846,  2207,  3173,  2552, 10398,  8682,  2399,  8116,  3299,
           │             │                   8147,  3189,  8142,   ...
           │             └ ()
           └ <bound method BertModel.forward of BertModel(
               (embeddings): BertEmbeddings(
                 (word_embeddings): Embedding(21128, 768, pa...
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\transformers\models\bert\modeling_bert.py", line 1073, in forward
    embedding_output = self.embeddings(
                       └ BertModel(
                           (embeddings): BertEmbeddings(
                             (word_embeddings): Embedding(21128, 768, padding_idx=0)
                             (position_embeddin...
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\torch\nn\modules\module.py", line 1552, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           │    │           │       └ {'input_ids': tensor([[  101,  5846,  2207,  3173,  2552, 10398,  8682,  2399,  8116,  3299,
           │    │           │                   8147,  3189,  8142,   ...
           │    │           └ ()
           │    └ <function Module._call_impl at 0x0000013E07766B60>
           └ BertEmbeddings(
               (word_embeddings): Embedding(21128, 768, padding_idx=0)
               (position_embeddings): Embedding(512, 768)
               (tok...
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\torch\nn\modules\module.py", line 1561, in _call_impl
    return forward_call(*args, **kwargs)
           │             │       └ {'input_ids': tensor([[  101,  5846,  2207,  3173,  2552, 10398,  8682,  2399,  8116,  3299,
           │             │                   8147,  3189,  8142,   ...
           │             └ ()
           └ <bound method BertEmbeddings.forward of BertEmbeddings(
               (word_embeddings): Embedding(21128, 768, padding_idx=0)
               (position...
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\transformers\models\bert\modeling_bert.py", line 216, in forward
    embeddings += position_embeddings
    │             └ tensor([[[-0.0011,  0.0083, -0.0123,  ..., -0.0281, -0.0158, -0.0110],
    │                        [-0.0063,  0.0018, -0.0067,  ..., -0.0717, -0...
    └ tensor([[[ 0.0184,  0.0006, -0.0112,  ...,  0.0269, -0.0065,  0.0012],
               [-0.0313,  0.0595,  0.0315,  ...,  0.0035, -0...

RuntimeError: The size of tensor a (570) must match the size of tensor b (512) at non-singleton dimension 1
2024-06-07 19:34:58.725 | INFO     | app.Services.upload_service:_upload_task:44 - Start indexing image 05edb10a-4ef2-58b7-b47c-85bde768eefd. Local: True. Size: 83603
2024-06-07 19:34:58.725 | INFO     | app.Services.vector_db_context:validate_ids:98 - Validating 1 items from database...
2024-06-07 19:34:58.728 | INFO     | app.Services.transformers_service:get_image_vector:35 - Processing image...
2024-06-07 19:34:58.734 | SUCCESS  | app.Services.transformers_service:get_image_vector:38 - Image processed, now Inferring with CLIP model...
2024-06-07 19:34:58.744 | SUCCESS  | app.Services.transformers_service:get_image_vector:40 - Inference done. Time elapsed: 0.02s
2024-06-07 19:34:58.745 | INFO     | app.Services.ocr_services:ocr_interface:58 - Processing text with EasyPaddleOCR...
2024-06-07 19:34:59.021 | SUCCESS  | app.Services.ocr_services:ocr_interface:60 - OCR processed done. Time elapsed: 0.28s
2024-06-07 19:34:59.021 | INFO     | app.Services.transformers_service:get_bert_vector:58 - Inferring with BERT model...
2024-06-07 19:34:59.024 | ERROR    | app.Services.upload_service:_upload_worker:34 - Error occurred while uploading image 05edb10a-4ef2-58b7-b47c-85bde768eefd
2024-06-07 19:34:59.024 | ERROR    | app.Services.upload_service:_upload_worker:35 - The size of tensor a (583) must match the size of tensor b (512) at non-singleton dimension 1
Traceback (most recent call last):

  File "E:\NekoImageGallery\main.py", line 71, in <module>
    uvicorn.run("app.webapp:app", host=args.host, port=args.port, root_path=args.root_path)
    │       │                          │    │          │    │               │    └ ''
    │       │                          │    │          │    │               └ Namespace(show_config=False, init_database=False, migrate_from_version=None, local_index_target_dir=None, local_create_thumbn...
    │       │                          │    │          │    └ 8000
    │       │                          │    │          └ Namespace(show_config=False, init_database=False, migrate_from_version=None, local_index_target_dir=None, local_create_thumbn...
    │       │                          │    └ '0.0.0.0'
    │       │                          └ Namespace(show_config=False, init_database=False, migrate_from_version=None, local_index_target_dir=None, local_create_thumbn...
    │       └ <function run at 0x0000013E7E3B00E0>
    └ <module 'uvicorn' from 'E:\\Users\\Summer\\.conda\\envs\\nekoenv\\Lib\\site-packages\\uvicorn\\__init__.py'>

  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\uvicorn\main.py", line 577, in run
    server.run()
    │      └ <function Server.run at 0x0000013E7E3EB740>
    └ <uvicorn.server.Server object at 0x0000013E7E396410>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\uvicorn\server.py", line 65, in run
    return asyncio.run(self.serve(sockets=sockets))
           │       │   │    │             └ None
           │       │   │    └ <function Server.serve at 0x0000013E7E3EB7E0>
           │       │   └ <uvicorn.server.Server object at 0x0000013E7E396410>
           │       └ <function run at 0x0000013E7DB63D80>
           └ <module 'asyncio' from 'E:\\Users\\Summer\\.conda\\envs\\nekoenv\\Lib\\asyncio\\__init__.py'>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           │      │   └ <coroutine object Server.serve at 0x0000013E7E4E5D50>
           │      └ <function Runner.run at 0x0000013E7DC26840>
           └ <asyncio.runners.Runner object at 0x0000013E7E54A390>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           │    │     │                  └ <Task pending name='Task-1' coro=<Server.serve() running at E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\uvicorn\ser...
           │    │     └ <function BaseEventLoop.run_until_complete at 0x0000013E7DC244A0>
           │    └ <ProactorEventLoop running=True closed=False debug=False>
           └ <asyncio.runners.Runner object at 0x0000013E7E54A390>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\base_events.py", line 641, in run_until_complete
    self.run_forever()
    │    └ <function ProactorEventLoop.run_forever at 0x0000013E7DD3B9C0>
    └ <ProactorEventLoop running=True closed=False debug=False>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\windows_events.py", line 321, in run_forever
    super().run_forever()
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\base_events.py", line 608, in run_forever
    self._run_once()
    │    └ <function BaseEventLoop._run_once at 0x0000013E7DC26200>
    └ <ProactorEventLoop running=True closed=False debug=False>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\base_events.py", line 1936, in _run_once
    handle._run()
    │      └ <function Handle._run at 0x0000013E7DB7C5E0>
    └ <Handle Task.task_wakeup(<Future finis...dimension 1')>)>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\asyncio\events.py", line 84, in _run
    self._context.run(self._callback, *self._args)
    │    │            │    │           │    └ <member '_args' of 'Handle' objects>
    │    │            │    │           └ <Handle Task.task_wakeup(<Future finis...dimension 1')>)>
    │    │            │    └ <member '_callback' of 'Handle' objects>
    │    │            └ <Handle Task.task_wakeup(<Future finis...dimension 1')>)>
    │    └ <member '_context' of 'Handle' objects>
    └ <Handle Task.task_wakeup(<Future finis...dimension 1')>)>

> File "E:\NekoImageGallery\app\Services\upload_service.py", line 31, in _upload_worker
    await self._upload_task(img, img_data, *args)
          │    │            │    │          └ [b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x03\x02\x02\x03\x02\x02\x03\x03\x03\...
          │    │            │    └ ImageData(id=UUID('05edb10a-4ef2-58b7-b47c-85bde768eefd'), url='/static/05edb10a-4ef2-58b7-b47c-85bde768eefd.png', thumbnail_...
          │    │            └ <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=642x581 at 0x13E299CC590>
          │    └ <function UploadService._upload_task at 0x0000013E107567A0>
          └ <app.Services.upload_service.UploadService object at 0x0000013EEF3525D0>

  File "E:\NekoImageGallery\app\Services\upload_service.py", line 57, in _upload_task
    await self._index_service.index_image(img, img_data, skip_ocr=skip_ocr, background=True)
          │    │              │           │    │                  └ False
          │    │              │           │    └ ImageData(id=UUID('05edb10a-4ef2-58b7-b47c-85bde768eefd'), url='/static/05edb10a-4ef2-58b7-b47c-85bde768eefd.png', thumbnail_...
          │    │              │           └ <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=642x581 at 0x13E299CC590>
          │    │              └ <function IndexService.index_image at 0x0000013E106C6C00>
          │    └ <app.Services.index_service.IndexService object at 0x0000013EF17DDCD0>
          └ <app.Services.upload_service.UploadService object at 0x0000013EEF3525D0>

  File "E:\NekoImageGallery\app\Services\index_service.py", line 50, in index_image
    await run_in_threadpool(self._prepare_image, image, image_data, skip_ocr)
          │                 │    │               │      │           └ False
          │                 │    │               │      └ ImageData(id=UUID('05edb10a-4ef2-58b7-b47c-85bde768eefd'), url='/static/05edb10a-4ef2-58b7-b47c-85bde768eefd.png', thumbnail_...
          │                 │    │               └ <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=642x581 at 0x13E299CC590>
          │                 │    └ <function IndexService._prepare_image at 0x0000013E0CE7B600>
          │                 └ <app.Services.index_service.IndexService object at 0x0000013EF17DDCD0>
          └ <function run_in_threadpool at 0x0000013E7F205080>

  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\starlette\concurrency.py", line 42, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
                 │     │         │        │      └ (<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=642x581 at 0x13E299CC590>, ImageData(id=UUID('05edb10a-4ef2-58b7-b47c...
                 │     │         │        └ <bound method IndexService._prepare_image of <app.Services.index_service.IndexService object at 0x0000013EF17DDCD0>>
                 │     │         └ <function run_sync at 0x0000013E7E498EA0>
                 │     └ <module 'anyio.to_thread' from 'E:\\Users\\Summer\\.conda\\envs\\nekoenv\\Lib\\site-packages\\anyio\\to_thread.py'>
                 └ <module 'anyio' from 'E:\\Users\\Summer\\.conda\\envs\\nekoenv\\Lib\\site-packages\\anyio\\__init__.py'>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\anyio\to_thread.py", line 33, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
                 └ <function get_asynclib at 0x0000013E7E46DC60>
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\anyio\_backends\_asyncio.py", line 877, in run_sync_in_worker_thread
    return await future
                 └ <Future finished exception=RuntimeError('The size of tensor a (583) must match the size of tensor b (512) at non-singleton di...
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\anyio\_backends\_asyncio.py", line 807, in run
    result = context.run(func, *args)
             │       │   │      └ (<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=642x581 at 0x13E299CC590>, ImageData(id=UUID('05edb10a-4ef2-58b7-b47c...
             │       │   └ <bound method IndexService._prepare_image of <app.Services.index_service.IndexService object at 0x0000013EF17DDCD0>>
             │       └ <method 'run' of '_contextvars.Context' objects>
             └ <_contextvars.Context object at 0x0000013E29A1E880>

  File "E:\NekoImageGallery\app\Services\index_service.py", line 34, in _prepare_image
    image_data.text_contain_vector = self._transformers_service.get_bert_vector(image_data.ocr_text)
    │          │                     │    │                     │               │          └ '迪子的智驾,要后来者居上了?今日,工业和信息化部等四部门公布全国首批智能网联汽车准入和上路通行试点名单,比亚迪成功获选!这是继去年7月份比业迪掌到 全国第一张L3自动驾驶测试牌照后,再次验证比业迪强大的智驾研发实力,加速L3自动驾驶技术走向产业化应用...
    │          │                     │    │                     │               └ ImageData(id=UUID('05edb10a-4ef2-58b7-b47c-85bde768eefd'), url='/static/05edb10a-4ef2-58b7-b47c-85bde768eefd.png', thumbnail_...
    │          │                     │    │                     └ <function TransformersService.get_bert_vector at 0x0000013E0CE7A2A0>
    │          │                     │    └ <app.Services.transformers_service.TransformersService object at 0x0000013E1083FC10>
    │          │                     └ <app.Services.index_service.IndexService object at 0x0000013EF17DDCD0>
    │          └ None
    └ ImageData(id=UUID('05edb10a-4ef2-58b7-b47c-85bde768eefd'), url='/static/05edb10a-4ef2-58b7-b47c-85bde768eefd.png', thumbnail_...

  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\torch\utils\_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
           │     │       └ {}
           │     └ (<app.Services.transformers_service.TransformersService object at 0x0000013E1083FC10>, '迪子的智驾, 要后来者居上了?今日,工业和信息化部等四部门公布全国首批智能网...
           └ <function TransformersService.get_bert_vector at 0x0000013E0CE4CE00>

  File "E:\NekoImageGallery\app\Services\transformers_service.py", line 60, in get_bert_vector
    outputs = self._bert_model(**inputs)
              │    │             └ {'input_ids': tensor([[  101,  6832,  2094,  4638,  3255,  7730,  8024,  6206,  1400,  3341,
              │    │                         5442,  2233,   677,   ...
              │    └ BertModel(
              │        (embeddings): BertEmbeddings(
              │          (word_embeddings): Embedding(21128, 768, padding_idx=0)
              │          (position_embeddin...
              └ <app.Services.transformers_service.TransformersService object at 0x0000013E1083FC10>

  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\torch\nn\modules\module.py", line 1552, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           │    │           │       └ {'input_ids': tensor([[  101,  6832,  2094,  4638,  3255,  7730,  8024,  6206,  1400,  3341,
           │    │           │                   5442,  2233,   677,   ...
           │    │           └ ()
           │    └ <function Module._call_impl at 0x0000013E07766B60>
           └ BertModel(
               (embeddings): BertEmbeddings(
                 (word_embeddings): Embedding(21128, 768, padding_idx=0)
                 (position_embeddin...
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\torch\nn\modules\module.py", line 1561, in _call_impl
    return forward_call(*args, **kwargs)
           │             │       └ {'input_ids': tensor([[  101,  6832,  2094,  4638,  3255,  7730,  8024,  6206,  1400,  3341,
           │             │                   5442,  2233,   677,   ...
           │             └ ()
           └ <bound method BertModel.forward of BertModel(
               (embeddings): BertEmbeddings(
                 (word_embeddings): Embedding(21128, 768, pa...
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\transformers\models\bert\modeling_bert.py", line 1073, in forward
    embedding_output = self.embeddings(
                       └ BertModel(
                           (embeddings): BertEmbeddings(
                             (word_embeddings): Embedding(21128, 768, padding_idx=0)
                             (position_embeddin...
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\torch\nn\modules\module.py", line 1552, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           │    │           │       └ {'input_ids': tensor([[  101,  6832,  2094,  4638,  3255,  7730,  8024,  6206,  1400,  3341,
           │    │           │                   5442,  2233,   677,   ...
           │    │           └ ()
           │    └ <function Module._call_impl at 0x0000013E07766B60>
           └ BertEmbeddings(
               (word_embeddings): Embedding(21128, 768, padding_idx=0)
               (position_embeddings): Embedding(512, 768)
               (tok...
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\torch\nn\modules\module.py", line 1561, in _call_impl
    return forward_call(*args, **kwargs)
           │             │       └ {'input_ids': tensor([[  101,  6832,  2094,  4638,  3255,  7730,  8024,  6206,  1400,  3341,
           │             │                   5442,  2233,   677,   ...
           │             └ ()
           └ <bound method BertEmbeddings.forward of BertEmbeddings(
               (word_embeddings): Embedding(21128, 768, padding_idx=0)
               (position...
  File "E:\Users\Summer\.conda\envs\nekoenv\Lib\site-packages\transformers\models\bert\modeling_bert.py", line 216, in forward
    embeddings += position_embeddings
    │             └ tensor([[[-0.0011,  0.0083, -0.0123,  ..., -0.0281, -0.0158, -0.0110],
    │                        [-0.0063,  0.0018, -0.0067,  ..., -0.0717, -0...
    └ tensor([[[ 0.0184,  0.0006, -0.0112,  ...,  0.0269, -0.0065,  0.0012],
               [-0.0208,  0.0657, -0.0859,  ...,  0.0262, -0...

RuntimeError: The size of tensor a (583) must match the size of tensor b (512) at non-singleton dimension 1

样例图片文件:fe1a0a91d9eed722580028fc87ba436d.zip

dosubot[bot] commented 4 months ago

@Natsukage Hello there! I'm a bot here to help you with any bugs, questions, or becoming a contributor. Let me know how I can assist you!

The error you're encountering is due to a mismatch in tensor sizes during the BERT model inference. Specifically, the input text length exceeds the maximum sequence length allowed by the BERT model, which is causing the tensor size mismatch.

To address this issue, you can configure the BertTokenizer to handle long sequences by truncating them to the maximum allowed length. This can be done by setting the max_length and truncation parameters in the get_bert_vector function.

Here is the updated code for the get_bert_vector function:

@no_grad()
def get_bert_vector(self, text: str) -> ndarray:
    start_time = time()
    logger.info("Inferring with BERT model...")
    inputs = self._bert_tokenizer(text.strip().lower(), return_tensors="pt", max_length=512, truncation=True).to(self.device)
    outputs = self._bert_model(**inputs)
    vector = outputs.last_hidden_state.mean(dim=1).squeeze()
    logger.success("BERT inference done. Time elapsed: {:.2f}s", time() - start_time)
    return vector.cpu().numpy()

By setting max_length=512 and truncation=True, the tokenizer will truncate any input text that exceeds 512 tokens, ensuring that the tensor sizes match during the BERT model inference [1].

This should resolve the tensor size mismatch error you're experiencing.

To continue talking to Dosu, mention @dosu.

hv0905 commented 4 months ago

已确认,感谢反馈

hv0905 commented 4 months ago

https://github.com/hv0905/NekoImageGallery/commit/ae19d8a9f5a4b950b1a9e978d815cf202655d017 已修复,现在若图片包含大量文字会进行截断。

更新将在下个版本发布。在此之前,你可以切换到最新的开发版本使用(手动部署切换到master分支,docker部署切换到edge镜像标签)使用。感谢反馈。

关于上传失败仍然200的问题,由于索引过程是异步进行,索引开始前接口已经返回,200仅可以代表图片已经通过校验并进入索引队列,因此若错误发生在索引过程无法汇报500错误。之后可能会添加一个图片查询接口允许用户通过id查询图片状态。

Natsukage commented 4 months ago

好的,辛苦了!