Skip to content

Commit afffd9b

Browse files
vedanujapsdehal
authored andcommitted
[fix] Fix flake8 errors (#42)
* [fix] Fix flake8 errors * [enhancement] Add .flake8
1 parent 501a896 commit afffd9b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+110
-137
lines changed

.flake8

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This is an example .flake8 config used when developing *Black* itself.
2+
3+
[flake8]
4+
max-line-length = 88
5+
max-complexity = 18
6+
select = B,C,E,F,W,T4,B9
7+
ignore = E203, E266, C901, C408, W503

projects/M4C/scripts/extract_ocr_frcn_feature.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def main():
159159
print("Faster R-CNN OCR features")
160160
print("\textracting from", IMDB_FILE)
161161
print("\tsaving to", SAVE_DIR)
162-
for n, info in enumerate(tqdm.tqdm(imdb)):
162+
for _, info in enumerate(tqdm.tqdm(imdb)):
163163
image_path = os.path.join(IMAGE_DIR, info["image_path"])
164164
save_feat_path = os.path.join(SAVE_DIR, info["feature_path"])
165165
save_info_path = save_feat_path.replace(".npy", "_info.npy")

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.isort]
22
skip_glob = ["*/build/*"]
3-
known_third_party = ["PIL", "caffe2", "coco_caption_eval", "common", "cv2", "datasets", "demjson", "git", "h5py", "lmdb", "maskrcnn_benchmark", "numpy", "omegaconf", "pycocoevalcap", "pytorch_pretrained_bert", "recommonmark", "requests", "setuptools", "sklearn", "sphinx_rtd_theme", "torch", "torchtext", "torchvision", "tqdm", "transformers", "utils", "yaml"]
3+
known_third_party = ["PIL", "caffe2", "coco_caption_eval", "common", "cv2", "demjson", "git", "h5py", "lmdb", "maskrcnn_benchmark", "numpy", "omegaconf", "pycocoevalcap", "pytorch_pretrained_bert", "recommonmark", "requests", "setuptools", "sklearn", "sphinx_rtd_theme", "torch", "torchtext", "torchvision", "tqdm", "transformers", "utils", "yaml"]
44
multi_line_output = 3
55
line_length = 88
66
include_trailing_comma = true

pythia/common/constants.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Copyright (c) Facebook, Inc. and its affiliates.
2-
import os
32

43
imdb_version = 1
54
FASTTEXT_WIKI_URL = (

pythia/common/dataset_loader.py

-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
# Copyright (c) Facebook, Inc. and its affiliates.
2-
import os
32

4-
import yaml
5-
from torch.utils.data import DataLoader
6-
7-
from pythia.common.batch_collator import BatchCollator
83
from pythia.common.sample import SampleList
94
from pythia.common.test_reporter import TestReporter
105
from pythia.datasets.multi_dataset import MultiDataset
11-
from pythia.datasets.samplers import DistributedSampler
12-
from pythia.utils.general import get_batch_size
136

147

158
class DatasetLoader:

pythia/common/report.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88

99
class Report(OrderedDict):
10-
def __init__(self, batch, model_output={}, *args):
10+
def __init__(self, batch, model_output=None, *args):
1111
super().__init__(self)
12+
if model_output is None:
13+
model_output = {}
1214
if self._check_and_load_tuple(batch):
1315
return
1416

pythia/common/sample.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import collections
1515
from collections import OrderedDict
16-
from copy import deepcopy
1716

1817
import torch
1918

@@ -33,7 +32,9 @@ class Sample(OrderedDict):
3332
>>> sample.context = torch.tensor(4)
3433
"""
3534

36-
def __init__(self, init_dict={}):
35+
def __init__(self, init_dict=None):
36+
if init_dict is None:
37+
init_dict = {}
3738
super().__init__(init_dict)
3839

3940
def __setattr__(self, key, value):
@@ -82,8 +83,10 @@ class SampleList(OrderedDict):
8283

8384
_TENSOR_FIELD_ = "_tensor_field"
8485

85-
def __init__(self, samples=[]):
86+
def __init__(self, samples=None):
8687
super().__init__(self)
88+
if samples is None:
89+
samples = []
8790

8891
if len(samples) == 0:
8992
return

pythia/common/test_reporter.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ def get_dataloader(self):
9999
**other_args
100100
)
101101

102-
def _add_extra_args_for_dataloader(self, other_args={}):
102+
def _add_extra_args_for_dataloader(self, other_args=None):
103+
if other_args is None:
104+
other_args = {}
103105
training = self.config.training
104106

105107
if training.local_rank is not None and training.distributed:

pythia/datasets/base_dataset.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ class BaseDataset(Dataset):
1818
config (ConfigNode): Configuration for the current dataset
1919
"""
2020

21-
def __init__(self, name, dataset_type, config={}):
21+
def __init__(self, name, dataset_type, config=None):
2222
super(BaseDataset, self).__init__()
23+
if config is None:
24+
config = {}
2325
self.config = config
2426
self._name = name
2527
self._dataset_type = dataset_type

pythia/datasets/base_dataset_builder.py

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def _build(self, dataset_type, config, *args, **kwargs):
3636
"""
3737

3838
from pythia.utils.distributed_utils import is_master, synchronize
39-
from pythia.common.registry import registry
4039

4140

4241
class BaseDatasetBuilder:

pythia/datasets/captioning/coco/masked_dataset.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import random
22

3-
import numpy as np
4-
import torch
5-
63
from pythia.common.sample import Sample
74
from pythia.datasets.captioning.coco import COCODataset
85

@@ -23,7 +20,6 @@ def load_item(self, idx):
2320
if self._use_features is True:
2421
features = self.features_db[idx]
2522
image_labels = []
26-
overlaps = np.ones((features["image_feature_0"].shape[0]))
2723

2824
for i in range(features["image_feature_0"].shape[0]):
2925
prob = random.random()

pythia/datasets/dialog/visual_dialog/database.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
23
import torch
34

45

@@ -40,7 +41,6 @@ def __getitem__(self, idx):
4041
data["id"] = idx
4142
data["dialog_id"] = dialog_id
4243
data["round_id"] = round_id
43-
caption = dialog["caption"]
4444
round = dialog["dialog"][round_id]
4545
data["question"] = self._questions[round["question"]]
4646
# data["answers"] = [self.]

pythia/datasets/dialog/visual_dialog/scripts/build_imdb.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def get_tokens(self, sentences):
9090
if not isinstance(sentences, list):
9191
sentences = [sentences]
9292
final_sentences = []
93-
for idx, sentence in enumerate(sentences):
93+
for _, sentence in enumerate(sentences):
9494
tokens = text_tokenize(sentence)
9595
final_sentences.append(tokens)
9696

pythia/datasets/multi_dataset.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import sys
88

99
import numpy as np
10-
from torch.utils.data import DataLoader, Dataset
10+
from torch.utils.data import DataLoader
1111

1212
from pythia.common.batch_collator import BatchCollator
1313
from pythia.common.registry import registry
1414
from pythia.datasets.samplers import DistributedSampler
15-
from pythia.utils.distributed_utils import broadcast_scalar, is_master, synchronize
15+
from pythia.utils.distributed_utils import broadcast_scalar, is_master
1616
from pythia.utils.general import get_batch_size
1717

1818
# from torch.utils.data.distributed import DistributedSampler
@@ -241,7 +241,9 @@ def build_dataloader(self, dataset, opts):
241241

242242
return loader, other_args.get("sampler", None)
243243

244-
def _add_extra_args_for_dataloader(self, dataset, opts, other_args={}):
244+
def _add_extra_args_for_dataloader(self, dataset, opts, other_args=None):
245+
if other_args is None:
246+
other_args = {}
245247
training = self._global_config.training
246248
dataset_type = self._dataset_type
247249

pythia/datasets/processors/processors.py

-3
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,13 @@ def __call__(self, item, *args, **kwargs):
6969
text = [t.strip() for t in text.split(" ")]
7070
return {"text": text}
7171
"""
72-
import multiprocessing
7372
import os
7473
import re
7574
import warnings
7675
from collections import Counter, defaultdict
7776

7877
import numpy as np
7978
import torch
80-
from transformers.tokenization_bert import BertTokenizer
8179

8280
from pythia.common.registry import registry
8381
from pythia.utils.configuration import ConfigNode
@@ -745,7 +743,6 @@ def __call__(self, item):
745743
length = min(len(tokens), self.max_length)
746744

747745
gt_answers = list(enumerate(answers))
748-
unique_answers = set(answers)
749746

750747
if self.context_preprocessor is not None:
751748
tokens = [

pythia/datasets/reasoning/mmimdb/masked_dataset.py

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import random
22

3-
import numpy as np
4-
53
from pythia.common.sample import Sample
64
from pythia.datasets.vqa.vqa2.dataset import VQA2Dataset
75

@@ -20,7 +18,6 @@ def load_item(self, idx):
2018
features = self.features_db[idx]
2119
current_sample.update(features)
2220
image_labels = []
23-
overlaps = np.ones((features["image_feature_0"].shape[0]))
2421

2522
for i in range(features["image_feature_0"].shape[0]):
2623
prob = random.random()

pythia/datasets/vqa/vqa2/masked_dataset.py

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import random
22

3-
import numpy as np
4-
53
from pythia.common.sample import Sample
64
from pythia.datasets.vqa.vqa2.dataset import VQA2Dataset
75

@@ -20,7 +18,6 @@ def load_item(self, idx):
2018
features = self.features_db[idx]
2119
current_sample.update(features)
2220
image_labels = []
23-
overlaps = np.ones((features["image_feature_0"].shape[0]))
2421

2522
for i in range(features["image_feature_0"].shape[0]):
2623
prob = random.random()

pythia/models/ban.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def _init_bilinear_attention(self):
4949
b_net = []
5050
q_prj = []
5151

52-
for i in range(module_config["gamma"]):
52+
for _ in range(module_config["gamma"]):
5353
b_net.append(
5454
BCNet(
5555
v_dim, num_hidden, num_hidden, None, k=module_config["bc_net"]["k"]

pythia/models/base_model.py

-3
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,11 @@ def forward(self, sample_list):
4343
import warnings
4444
from copy import deepcopy
4545

46-
from omegaconf import OmegaConf
4746
from torch import nn
4847

4948
from pythia.common.registry import registry
50-
from pythia.common.report import Report
5149
from pythia.modules.losses import Losses
5250
from pythia.modules.metrics import Metrics
53-
from pythia.utils.configuration import load_yaml
5451

5552

5653
class BaseModel(nn.Module):

pythia/models/butd.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _build_word_embedding(self):
2727
self.word_embedding = self.vocab.get_embedding(
2828
torch.nn.Embedding, embedding_dim=self.config["embedding_dim"]
2929
)
30-
setattr(self, "text_embeddings_out_dim", self.config["embedding_dim"])
30+
self.text_embeddings_out_dim = self.config["embedding_dim"]
3131

3232
def _init_classifier(self):
3333
self.classifier = ClassifierLayer(

pythia/models/lorra.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# Copyright (c) Facebook, Inc. and its affiliates.
2-
import torch
32

43
from pythia.common.registry import registry
54
from pythia.models.pythia import Pythia
6-
from pythia.modules.layers import ClassifierLayer
75

86

97
@registry.register_model("lorra")

pythia/models/m4c.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def _forward_mmt_and_output(self, sample_list, fwd_results):
277277
fwd_results["prev_inds"][:, 0] = self.answer_processor.BOS_IDX
278278

279279
# greedy decoding at test time
280-
for t in range(dec_step_num):
280+
for _ in range(dec_step_num):
281281
self._forward_mmt(sample_list, fwd_results)
282282
self._forward_output(sample_list, fwd_results)
283283

pythia/models/pythia.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313
TextEmbedding,
1414
)
1515
from pythia.modules.encoders import ImageEncoder
16-
from pythia.modules.layers import (
17-
ClassifierLayer,
18-
ModalCombineLayer,
19-
ReLUWithWeightNormFC,
20-
)
16+
from pythia.modules.layers import ClassifierLayer, ModalCombineLayer
2117
from pythia.utils.configuration import ConfigNode
2218

2319

@@ -208,8 +204,10 @@ def process_text_embedding(
208204
return text_embeddding_total
209205

210206
def process_feature_embedding(
211-
self, attr, sample_list, text_embedding_total, extra=[], batch_size_t=None
207+
self, attr, sample_list, text_embedding_total, extra=None, batch_size_t=None
212208
):
209+
if extra is None:
210+
extra = []
213211
feature_embeddings = []
214212
feature_attentions = []
215213
features = []
@@ -427,8 +425,10 @@ def _init_feature_embeddings(self, attr):
427425
)
428426

429427
def process_feature_embedding(
430-
self, attr, sample_list, text_embedding_total, extra=[], batch_size_t=None
428+
self, attr, sample_list, text_embedding_total, extra=None, batch_size_t=None
431429
):
430+
if extra is None:
431+
extra = []
432432
feature_embeddings = []
433433
feature_attentions = []
434434
features = []

pythia/models/pythia_bert.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
from transformers.modeling_bert import (
44
BertConfig,
55
BertEmbeddings,
6-
BertEncoder,
76
BertForPreTraining,
8-
BertLayer,
97
BertLayerNorm,
108
BertPooler,
119
BertPredictionHeadTransform,
@@ -184,9 +182,11 @@ def process_feature_embedding(
184182
text_embedding_total,
185183
key_padding_mask=None,
186184
attn_mask=None,
187-
extra=[],
185+
extra=None,
188186
batch_size_t=None,
189187
):
188+
if extra is None:
189+
extra = []
190190
feature_embeddings = []
191191
feature_attentions = []
192192
features = []
@@ -332,7 +332,6 @@ def forward(self, sample_list):
332332
else:
333333
joint_embedding = image_embedding_total
334334

335-
dataset_name = sample_list.dataset_name
336335
output_dict = {}
337336

338337
pooled_output = self.pooler(joint_embedding)

pythia/models/top_down_bottom_up.py

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
# Copyright (c) Facebook, Inc. and its affiliates.
22
import torch
3-
from torch import nn
43

54
from pythia.common.registry import registry
65
from pythia.models.base_model import BaseModel
7-
from pythia.modules.embeddings import (
8-
ImageEmbedding,
9-
PreExtractedEmbedding,
10-
TextEmbedding,
11-
)
12-
from pythia.modules.encoders import ImageEncoder
13-
from pythia.modules.layers import (
14-
ClassifierLayer,
15-
Identity,
16-
ModalCombineLayer,
17-
ReLUWithWeightNormFC,
18-
)
6+
from pythia.modules.layers import ReLUWithWeightNormFC
197

208

219
# Note: Doesn't work currently. Needs to be migrated to new API

0 commit comments

Comments
 (0)