mikwieczorek / centroids-reid

On the Unreasonable Effectiveness of Centroids in Image Retrieval
MIT License
216 stars 58 forks source link

Running inference gives AttributeError: Missing attribute "MODEL #23

Open majrblais opened 2 years ago

majrblais commented 2 years ago

(centro) [user@remote centroids-reid]$ python inference/create_embeddings.py --config_file="configs/256_resnet50.yml" GPU_IDS [1] DATASETS.ROOT_DIR './test/probe/' TEST.IMS_PER_BATCH 128 OUTPUT_DIR './output-dir' TEST.ONLY_TEST True MODEL.PRETRAIN_PATH './logs/market1501/256_resnet50/train_ctl_model/version_0/auto_checkpoints/resnet50-19c8e357.pth'

INFO:main:Preparing data using <class 'inference_utils.ImageDataset'> dataset class ./test/probe/ 8 ['./test/probe/ursula.jpg', './test/probe/angela.jpg', './test/probe/charles.jpg', './test/probe/biden.jpg', './test/probe/Yoshi.jpg', './test/probe/justin.jpg', './test/probe/macron.jpg', './test/probe/boris.jpg'] Traceback (most recent call last): File "/home/user/anaconda3/envs/centro/lib/python3.7/site-packages/pytorch_lightning/utilities/parsing.py", line 184, in getattr return self[key] KeyError: 'MODEL'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "inference/create_embeddings.py", line 76, in model = CTLModel.load_from_checkpoint(cfg.MODEL.PRETRAIN_PATH) File "/home/user/anaconda3/envs/centro/lib/python3.7/site-packages/pytorch_lightning/core/saving.py", line 158, in load_from_checkpoint model = cls._load_model_state(checkpoint, strict=strict, kwargs) File "/home/user/anaconda3/envs/centro/lib/python3.7/site-packages/pytorch_lightning/core/saving.py", line 198, in _load_model_state model = cls(_cls_kwargs) File "./train_ctl_model.py", line 29, in init super().init(cfg, **kwargs) File "./modelling/bases.py", line 70, in init self.backbone = Baseline(self.hparams) File "./modelling/baseline.py", line 50, in init last_stride = cfg.MODEL.LAST_STRIDE File "/home/user/anaconda3/envs/centro/lib/python3.7/site-packages/pytorch_lightning/utilities/parsing.py", line 186, in getattr raise AttributeError(f'Missing attribute "{key}"') from exp AttributeError: Missing attribute "MODEL"

I can run the training but anytime I run any of the inference I receive this error.

majrblais commented 2 years ago

I found a complex solve, if anyone get simplify thanks. (a condition could also allow to do this without creating a new file)

1) In inference/create_embeddings.py CHANGE [13] from train_ctl_model import CTLModel to from test_ctl_model import CTLModel and CHANGE [77] model = CTLModel.load_from_checkpoint(cfg.MODEL.PRETRAIN_PATH) to model = CTLModel.load_from_checkpoint(cfg.MODEL.PRETRAIN_PATH,cfg=cfg)

2) In /centroids-reid/ COPY train_ctl_model.py and name it test_ctl_model.py in the same directory.

3) In test_ctl_model.py CHANGE [23] from modelling.bases import ModelBase to from modelling.bases_test import ModelBase

4) In /centroids/modelling/ COPY bases.py and name it bases_test.py.

5) In bases_test.py Remove the following lines: [75]self.contrastive_loss = TripletLoss(self.hparams.SOLVER.MARGIN, self.hparams.SOLVER.DISTANCE_FUNC) [78]self.xent = CrossEntropyLabelSmooth(num_classes=self.hparams.num_classes) [79-81]self.center_loss = CenterLoss(num_classes=self.hparams.num_classes, feat_dim=d_model) [82]self.center_loss_weight = self.hparams.SOLVER.CENTER_LOSS_WEIGHT [87]self.fc_query = torch.nn.Linear(d_model, self.hparams.num_classes, bias=False) [88]self.fc_query.apply(weights_init_classifier) [90]self.losses_names = ["query_xent", "query_triplet", "query_center"] [91]self.losses_dict = {n: [] for n in self.losses_names}

6) In /home/user/anaconda3/envs/centro/lib/python3.7/site-packages/pytorch_lightning/core/saving.py ADD [210] if "state_dict" in checkpoint: before model.load_state_dict(checkpoint['state_dict'], strict=strict)

This has allowed me to run create_embeddings.py and load/run a model on a single image

anchalgupta2412 commented 1 year ago

thanks @majrblais! This worked for me

tpnam0901 commented 1 year ago

Hi @majrblais @anchalgupta2412 I think you are using the wrong weights for the model.

Based on this script

python inference/create_embeddings.py --config_file="configs/256_resnet50.yml" GPU_IDS [1] DATASETS.ROOT_DIR './test/probe/' TEST.IMS_PER_BATCH 128 OUTPUT_DIR './output-dir' TEST.ONLY_TEST True MODEL.PRETRAIN_PATH './logs/market1501/256_resnet50/train_ctl_model/version_0/auto_checkpoints/resnet50-19c8e357.pth'

you are using the imagenet weight resnet50-19c8e357.pth rather than the weight which is provided by the author | link

You need to download and store the resnet50-19c8e357.pth/r50_ibn_a.pth in the models folder and pass the market1501_resnet50_256_128_epoch_120.ckpt to MODEL.PRETRAIN_PATH. Here is the example command:

python inference/create_embeddings.py \
--config_file="configs/256_resnet50.yml" \
GPU_IDS [0] \
DATASETS.ROOT_DIR '/data/my-image-dataset/' \
TEST.IMS_PER_BATCH 2 \
OUTPUT_DIR 'output-dir' \
TEST.ONLY_TEST True \
MODEL.PRETRAIN_PATH "path/to/your/market1501_resnet50_256_128_epoch_120.ckpt"

P/s: About step 6. In /home/user/anaconda3/envs/centro/lib/python3.7/site-packages/pytorch_lightning/core/saving.py ADD [210] if "state_dict" in checkpoint: before model.load_state_dict(checkpoint['state_dict'], strict=strict) I do not recommend you change the source code of the library, this will result in many think like wrong code behavior, hard to maintain, etc. In my opinion, I think this modification will ignore the weight and not load the weight in the model. This means you are using the model which is initialized randomly from scratch or using the Imagenet weights

CobaltConcrete commented 1 year ago

@namphuongtran9196 hi i followed your instructions for create_embeddings but still am stuck with missing attribute 'MODEL'. I also have the same issue for get_similar file. This was the error given:

`INFO:main:Preparing data using <class 'inference_utils.ImageDataset'> dataset class D:\QingRong\centroids-reid\data\query-images 1 ['D:\QingRong\centroids-reid\data\query-images\rdj-query.jpg'] Traceback (most recent call last): File "D:\QingRong\Programs\anaconda3\envs\ctlreid\lib\site-packages\pytorch_lightning\utilities\parsing.py", line 184, in getattr return self[key] KeyError: 'MODEL'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "D:\QingRong\centroids-reid\inference\create_embeddings.py", line 76, in model = CTLModel.load_from_checkpoint(cfg.MODEL.PRETRAIN_PATH) File "D:\QingRong\Programs\anaconda3\envs\ctlreid\lib\site-packages\pytorch_lightning\core\saving.py", line 158, in load_from_checkpoint model = cls._load_model_state(checkpoint, strict=strict, kwargs) File "D:\QingRong\Programs\anaconda3\envs\ctlreid\lib\site-packages\pytorch_lightning\core\saving.py", line 198, in _load_model_state model = cls(_cls_kwargs) File "D:\QingRong\centroids-reid.\train_ctl_model.py", line 29, in init super().init(cfg, **kwargs) File "D:\QingRong\centroids-reid.\modelling\bases.py", line 70, in init self.backbone = Baseline(self.hparams) File "D:\QingRong\centroids-reid.\modelling\baseline.py", line 50, in init last_stride = cfg.MODEL.LAST_STRIDE File "D:\QingRong\Programs\anaconda3\envs\ctlreid\lib\site-packages\pytorch_lightning\utilities\parsing.py", line 186, in getattr raise AttributeError(f'Missing attribute "{key}"') from exp AttributeError: Missing attribute "MODEL"`

tpnam0901 commented 1 year ago

Hi @CobaltConcrete You can try to follow these commands

git clone git@github.com:mikwieczorek/centroids-reid.git
cd centroids-reid
pip install -r requirements.txt
mkdir models && wget https://download.pytorch.org/models/resnet50-19c8e357.pth -O models/resnet50-19c8e357.pth
mkdir samples && wget https://th.bing.com/th/id/OIP.muvVOaVid2C2LRH26luRggHaJX?pid=ImgDet&rs=1 -O samples/person.jpg
python inference/create_embeddings.py --config_file=configs/256_resnet50.yml GPU_IDS "[0]" DATASETS.ROOT_DIR './samples' TEST.IMS_PER_BATCH 1 OUTPUT_DIR 'outputs' TEST.ONLY_TEST True MODEL.PRETRAIN_PATH "market1501_resnet50_256_128_epoch_120.ckpt"

Maybe you will meet some problems with the CUDA device. You can try to use docker to build the library. Here is the Dockerfile

#
# Created on Fri Mar 24 2023
#
# Copyright (c) 2023 Tran Phuong Nam
# Contact: namphuongtran9196@gmail.com
#

FROM namphuongtran9196/pycuda:python3.8.16-cuda11.0.3-cudnn8-ubuntu20.04 AS build

ENV DEBIAN_FRONTEND noninteractive

ARG USER=vscode

ENV DEBIAN_FRONTEND noninteractive
# Update apt
RUN apt update -y
RUN apt install -y git vim nano unzip 

COPY ./centroids-reid /centroids-reid

# Install python dependencies
RUN pip3 install --upgrade pip
RUN cd /centroids-reid && pip3 install -r requirements.txt