mykcs / MedSAM_lite-0424

Apache License 2.0
0 stars 0 forks source link

to jupyter notebook #6

Open mykcs opened 2 months ago

mykcs commented 2 months ago

先把 from segment_anything.modeling import MaskDecoder, PromptEncoder, TwoWayTransformer 这类语句改成ipynb里的类

error

image
gemini202403 commented 2 months ago

解决ipykernel_launcher.py错误的中文解释

错误信息分析:

您遇到的错误信息为:

usage: ipykernel_launcher.py [-h] [-task_name TASK_NAME]
               [-model_type MODEL_TYPE] [-data_root DATA_ROOT]
               [-pretrained_checkpoint PRETRAINED_CHECKPOINT]
               [-resume RESUME] [-work_dir WORK_DIR]
               [-num_epochs NUM_EPOCHS] [-batch_size BATCH_SIZE]
               [-num_workers NUM_WORKERS] [-device DEVICE]
               [-bbox_shift BBOX_SHIFT] [-lr LR]
               [-weight_decay WEIGHT_DECAY]
               [-iou_loss_weight IOU_LOSS_WEIGHT]
               [-seg_loss_weight SEG_LOSS_WEIGHT]
               [-ce_loss_weight CE_LOSS_WEIGHT] [--sanity_check]
               [-use_wandb USE_WANDB]
ipykernel_launcher.py: error: unrecognized arguments: -f /Users/myk/Library/Jupyter/runtime/kernel-db109728-dc4b-4b65-83ab-bce86fbb47d4.json

An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

/Users/myk/anaconda3/envs/env-MedSAM-0422/lib/python3.10/site-packages/IPython/core/interactiveshell.py:3585: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

错误原因:

  1. 错误的脚本执行方式:

    您可能是在 Jupyter 环境中直接使用 ipykernel_launcher.py 工具来执行您的 Python 脚本 (train_mps-6.py)。 ipykernel_launcher.py 主要用于启动 Jupyter 内核,而不是直接运行带有命令行参数的 Python 脚本。

  2. 参数解析问题:

    您的脚本使用 argparse 来处理命令行参数,例如 -task_name-model_type 等。 错误消息表明 -f ... 参数未被您的脚本的 argparse 解析器识别。

解决方案:

  1. 从终端运行:

    • 打开终端或命令提示符窗口。
    • 导航到包含脚本 (train_mps-6.py) 的目录。
    • 使用 Python 解释器并传递所需参数来执行脚本:

      python train_mps-6.py -task_name MedSAM-lite -data_root ./data/npy -num_epochs 10 ... # 添加其他参数
  2. 检查冲突参数:

    • 可能是 Jupyter 环境在尝试执行您的脚本时向 ipykernel_launcher.py 传递了一些默认参数(例如 -f 参数)。这可能会与您脚本定义的参数冲突。
    • 如果必须从 Jupyter 中运行,您可以修改 argparse 设置以潜在处理这种情况。但是,通常直接从终端运行会更简单。

关于错误消息的解释:

mykcs commented 2 months ago

将parser.parse_args() 改为 parser.parse_known_args()[0]

ref https://blog.csdn.net/wmq104/article/details/123534597