Skip to content

Commit 04d7511

Browse files
committed
Upgrade
1 parent 06dfbb3 commit 04d7511

8 files changed

Lines changed: 1081 additions & 74 deletions

File tree

Makefile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,30 @@ CHECKPOINT=weights/vqa_checkpoint_DeeperLSTM_200.pth
2323
main: train evaluate
2424

2525
train:
26-
python main.py $(TRAIN_ANN) $(TRAIN_QUES) $(VAL_ANN) $(VAL_QUES) --images $(TRAIN_IMGS) --val_images $(VAL_IMGS) \
26+
uv run python main.py $(TRAIN_ANN) $(TRAIN_QUES) $(VAL_ANN) $(VAL_QUES) --images $(TRAIN_IMGS) --val_images $(VAL_IMGS) \
2727
--arch $(ARCH) --batch_size ${BATCH} --num_workers ${WORKERS} --image_root $(IMAGE_ROOT) --image_dim $(IMAGE_DIM)
2828

2929
train_mcb:
30-
python main.py $(TRAIN_ANN) $(TRAIN_QUES) $(VAL_ANN) $(VAL_QUES) --images $(TRAIN_IMGS) --val_images $(VAL_IMGS) \
30+
uv run python main.py $(TRAIN_ANN) $(TRAIN_QUES) $(VAL_ANN) $(VAL_QUES) --images $(TRAIN_IMGS) --val_images $(VAL_IMGS) \
3131
--arch MCBModel --batch_size 64 --num_workers ${WORKERS} --image_root $(IMAGE_ROOT) --raw_images --img_size 448 --image_dim $(IMAGE_DIM)
3232

3333

3434
raw_images:
35-
python main.py $(TRAIN_ANN) $(TRAIN_QUES) $(VAL_ANN) $(VAL_QUES) \
35+
uv run python main.py $(TRAIN_ANN) $(TRAIN_QUES) $(VAL_ANN) $(VAL_QUES) \
3636
--raw_images --image_root $(IMAGE_ROOT) --arch $(ARCH) --batch_size 32
3737

3838
options:
39-
python main.py -h
39+
python main.py -h
4040

4141
evaluate:
42-
python evaluate.py $(TRAIN_ANN) $(TRAIN_QUES) $(VAL_ANN) $(VAL_QUES) --images $(TRAIN_IMGS) --val_images $(VAL_IMGS) \
43-
--batch_size 1 --resume $(CHECKPOINT) --num_workers ${WORKERS} --image_dim $(IMAGE_DIM)
42+
python evaluate.py $(TRAIN_ANN) $(TRAIN_QUES) $(VAL_ANN) $(VAL_QUES) --images $(TRAIN_IMGS) --val_images $(VAL_IMGS) \
43+
--batch_size 1 --resume $(CHECKPOINT) --num_workers ${WORKERS} --image_dim $(IMAGE_DIM)
4444

4545
preprocess_train:
46-
python preprocess_images.py $(IMAGE_ROOT)/annotations/instances_train2014.json --root $(IMAGE_ROOT) --split train --arch resnet152
46+
uv run python preprocess_images.py $(IMAGE_ROOT)/annotations/instances_train2014.json --root $(IMAGE_ROOT) --split train --arch resnet152
4747

4848
preprocess_val:
49-
python preprocess_images.py $(IMAGE_ROOT)/annotations/instances_val2014.json --root $(IMAGE_ROOT) --split val --arch resnet152
49+
python preprocess_images.py $(IMAGE_ROOT)/annotations/instances_val2014.json --root $(IMAGE_ROOT) --split val --arch resnet152
5050

5151
demo:
52-
python demo.py demo_img.jpg "what room is this?" $(TRAIN_QUES) $(TRAIN_ANN) --checkpoint $(CHECKPOINT)
52+
python demo.py demo_img.jpg "what room is this?" $(TRAIN_QUES) $(TRAIN_ANN) --checkpoint $(CHECKPOINT)

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
Visual Question Answering in PyTorch
44

5-
65
## Setup
76

87
- Run `pip install -r requirements.txt` to install all the required python packages.
@@ -11,36 +10,40 @@ Visual Question Answering in PyTorch
1110
## Preprocess Images
1211

1312
We assume you use image embeddings, which you can process using `preprocess_images.py`.
13+
1414
```shell
1515
python preprocess_images.py <path to instances_train2014.json> \
1616
--root <path to dataset root "train2014|val2014"> \
1717
--split <train|val> --arch <vgg16|vgg19_bn|resnet152>
1818
```
19+
1920
I have already pre-processed all the COCO images (both train and test sets) using the VGG-16, VGG-19-BN, and ResNet-152 models. To download them, please go into the `image_embeddings` directory and run `make <model>`.</br>
2021
Here `<model>` can be either `vgg16`, `vgg19_bn` or `resnet152` depending on which model's embeddings you need. E.g. `make resnet152`
2122

2223
Alternatively, you can find them [here](https://1drv.ms/f/s!Au18pri6pxSNlop81AhX4bATqy1VJA).
2324

24-
2525
## Running
2626

2727
### Training
2828

2929
To run the training and evaluation code with default values, just type
3030

31-
```
31+
```shell
3232
make
3333
```
3434

3535
If you wish to only run the training code, you can run
36-
```
36+
37+
```shell
3738
make train
3839
```
3940

4041
If you want to use the raw RGB images from COCO, you can type
42+
4143
```shell
4244
make raw_images
4345
```
46+
4447
This takes the same arguments as `make train`.
4548

4649
You can get a list of options with `make options` or `python main.py -h`.
@@ -49,7 +52,6 @@ Check out the `Makefile` to get an idea of how to run the code.
4952

5053
> *NOTE* The code will take care of all the text preprocessing. Just sit back and relax.
5154
52-
5355
The minimum arguments required are:
5456

5557
1. The VQA train annotations dataset
@@ -61,15 +63,15 @@ The minimum arguments required are:
6163

6264
### Evaluation
6365

64-
Evaluating the performance of the model on a fine-grained basis is important. Thus this repo supports evaluating
65-
answers to questions based on answer type (e.g. "yes/no" questions).
66+
Evaluating the performance of the model on a fine-grained basis is important. Thus this repo supports evaluating answers to questions based on answer type (e.g. "yes/no" questions).
6667

6768
To evaluate the model, run
69+
6870
```shell
6971
make evaluate
7072
```
7173

72-
You are required to pass in the `--resume` argument to point to the trained model weights. The other arguments are
74+
You are required to pass in the `--resume` argument to point to the trained model weights. The other arguments are
7375
the same as in training.
7476

7577
### Demo
@@ -86,7 +88,6 @@ You can use your own image or question:
8688
python demo.py demo_img.jpg "what room is this?"
8789
```
8890

89-
9091
## Results
9192

9293
**NOTE** We train and evaluate on the balanced datasets.

demo.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import argparse
2-
from models import Models
3-
from utils import image, text
4-
from PIL import Image, ImageFont, ImageDraw
5-
import numpy as np
2+
63
import torch
7-
from torch import nn
4+
from PIL import Image, ImageDraw,
85
from torchvision import transforms
6+
97
from dataset import process_vqa_dataset
8+
from models import Models
9+
from utils import image, text
1010

1111

1212
def parse_args():
1313
parser = argparse.ArgumentParser("VQA Demo")
1414
parser.add_argument("image", help="Path to image file")
1515
parser.add_argument("question", help="Question text")
16-
parser.add_argument(
17-
"questions", help="Path to VQA Questions training file")
18-
parser.add_argument(
19-
"annotations", help="Path to COCO Annotations training file")
16+
parser.add_argument("questions",
17+
help="Path to VQA Questions training file")
18+
parser.add_argument("annotations",
19+
help="Path to COCO Annotations training file")
2020
parser.add_argument("--model", default="DeeperLSTM")
21-
parser.add_argument(
22-
"--checkpoint", default="weights/deeper_lstm_best_weights.pth.tar")
21+
parser.add_argument("--checkpoint",
22+
default="weights/deeper_lstm_best_weights.pth.tar")
2323
parser.add_argument("--preprocessed_cache",
2424
default="vqa_train_dataset_cache.pickle")
2525
parser.add_argument("--embedding_arch", default="vgg19_bn")
@@ -66,8 +66,10 @@ def main():
6666

6767
try:
6868
weights = torch.load(args.checkpoint)
69-
except (Exception,):
70-
print("ERROR: Default weights missing. Please specify weights for the VQA model")
69+
except (Exception, ):
70+
print(
71+
"ERROR: Default weights missing. Please specify weights for the VQA model"
72+
)
7173
exit(0)
7274

7375
model.load_state_dict(weights["model"])

evaluate.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import json
12
import os.path as osp
2-
import dataset
3-
from models.model import Models
4-
import numpy as np
3+
54
import torch
6-
from torch.nn import functional
75
from torchvision import transforms
8-
from arguments import parse_args
96
from tqdm import tqdm
10-
import json
7+
8+
import dataset
9+
from arguments import parse_args
10+
from models.model import Models
1111

1212

1313
@torch.no_grad()

metrics.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
"""Metrics module."""
2+
3+
14
def accuracy(output, target):
2-
pred, indices = output.max(dim=1)
5+
"""Compute the accuracy between the `output` and the `target`."""
6+
_, indices = output.max(dim=1)
37
acc = indices.eq(target).float().sum()
48
acc = acc / target.size(0)
59
return acc

pyproject.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[project]
2+
name = "VisualQA"
3+
version = "1.0.0"
4+
5+
description = "Visual Question Answering in PyTorch"
6+
readme = "README.md"
7+
authors = [
8+
{ name = "Varun Agrawal", email = "varagrawal@gmail.com" }
9+
]
10+
requires-python = ">= 3.11"
11+
dependencies = [
12+
"nltk>=3.9.2",
13+
"numpy>=2.0.0",
14+
"pillow>=12.0.0",
15+
"torch>=2.6.0",
16+
"torchvision>=0.24",
17+
"tqdm>=4.67.1",
18+
"visdom>=0.2.4",
19+
]
20+
21+
classifiers = [
22+
"Operating System :: MacOS",
23+
"Operating System :: Microsoft :: Windows",
24+
"Operating System :: POSIX :: Linux",
25+
"Programming Language :: Python",
26+
"Programming Language :: Python :: 3.11",
27+
]
28+
29+
[project.license]
30+
text = "MIT"
31+
32+
[project.urls]
33+
Homepage = "https://github.com/varunagrawal/VisualQA"
34+
Repository = "https://github.com/varunagrawal/VisualQA"
35+

requirements.txt

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)