Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove python2 compability code #2033

Merged
merged 3 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions references/classification/train.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import print_function
import datetime
import os
import time
import sys

import torch
import torch.utils.data
Expand Down Expand Up @@ -141,12 +139,9 @@ def load_data(traindir, valdir, cache_dataset, distributed):


def main(args):
if args.apex:
if sys.version_info < (3, 0):
raise RuntimeError("Apex currently only supports Python 3. Aborting.")
if amp is None:
raise RuntimeError("Failed to import apex. Please install apex from https://www.github.com/nvidia/apex "
"to enable mixed-precision training.")
if args.apex and amp is None:
raise RuntimeError("Failed to import apex. Please install apex from https://www.github.com/nvidia/apex "
"to enable mixed-precision training.")

if args.output_dir:
utils.mkdir(args.output_dir)
Expand Down
1 change: 0 additions & 1 deletion references/classification/train_quantization.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import datetime
import os
import time
Expand Down
1 change: 0 additions & 1 deletion references/classification/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from collections import defaultdict, deque
import datetime
import time
Expand Down
2 changes: 0 additions & 2 deletions references/detection/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

from collections import defaultdict, deque
import datetime
import pickle
Expand Down
1 change: 0 additions & 1 deletion references/segmentation/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from collections import defaultdict, deque
import datetime
import math
Expand Down
12 changes: 3 additions & 9 deletions references/video_classification/train.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from __future__ import print_function
import datetime
import os
import time
import sys

import torch
import torch.utils.data
from torch.utils.data.dataloader import default_collate
Expand Down Expand Up @@ -95,12 +92,9 @@ def collate_fn(batch):


def main(args):
if args.apex:
if sys.version_info < (3, 0):
raise RuntimeError("Apex currently only supports Python 3. Aborting.")
if amp is None:
raise RuntimeError("Failed to import apex. Please install apex from https://www.github.com/nvidia/apex "
"to enable mixed-precision training.")
if args.apex and amp is None:
raise RuntimeError("Failed to import apex. Please install apex from https://www.github.com/nvidia/apex "
"to enable mixed-precision training.")

if args.output_dir:
utils.mkdir(args.output_dir)
Expand Down
1 change: 0 additions & 1 deletion references/video_classification/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from collections import defaultdict, deque
import datetime
import time
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import os
import io
import re
Expand Down
8 changes: 1 addition & 7 deletions test/fakedata_generation.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import os
import sys
import contextlib
import tarfile
import json
import numpy as np
import PIL
import torch
from common_utils import get_tmp_dir

PYTHON2 = sys.version_info[0] == 2
if PYTHON2:
import cPickle as pickle
else:
import pickle
import pickle


@contextlib.contextmanager
Expand Down
1 change: 0 additions & 1 deletion test/test_functional_tensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import division
import torch
from torch import Tensor
import torchvision.transforms as transforms
Expand Down
1 change: 0 additions & 1 deletion test/test_ops.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import division
import math
import unittest

Expand Down
1 change: 0 additions & 1 deletion test/test_transforms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import division
import os
import mock
import torch
Expand Down
1 change: 0 additions & 1 deletion test/test_transforms_video.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import division
import torch
import torchvision.transforms._transforms_video as transforms
from torchvision.transforms import Compose
Expand Down
6 changes: 1 addition & 5 deletions test/test_video_reader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import collections
import math
import os
import sys
import time
import unittest
from fractions import Fraction
Expand All @@ -22,10 +21,7 @@
av = None


if sys.version_info < (3,):
from urllib2 import URLError
else:
from urllib.error import URLError
from urllib.error import URLError


VIDEO_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "videos")
Expand Down
1 change: 0 additions & 1 deletion torchvision/datasets/caltech.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from PIL import Image
import os
import os.path
Expand Down
18 changes: 3 additions & 15 deletions torchvision/datasets/cifar.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
from __future__ import print_function
from PIL import Image
import os
import os.path
import numpy as np
import sys

if sys.version_info[0] == 2:
import cPickle as pickle
else:
import pickle
import pickle

from .vision import VisionDataset
from .utils import check_integrity, download_and_extract_archive
Expand Down Expand Up @@ -79,10 +73,7 @@ def __init__(self, root, train=True, transform=None, target_transform=None,
for file_name, checksum in downloaded_list:
file_path = os.path.join(self.root, self.base_folder, file_name)
with open(file_path, 'rb') as f:
if sys.version_info[0] == 2:
entry = pickle.load(f)
else:
entry = pickle.load(f, encoding='latin1')
entry = pickle.load(f, encoding='latin1')
self.data.append(entry['data'])
if 'labels' in entry:
self.targets.extend(entry['labels'])
Expand All @@ -100,10 +91,7 @@ def _load_meta(self):
raise RuntimeError('Dataset metadata file not found or corrupted.' +
' You can use download=True to download it')
with open(path, 'rb') as infile:
if sys.version_info[0] == 2:
data = pickle.load(infile)
else:
data = pickle.load(infile, encoding='latin1')
data = pickle.load(infile, encoding='latin1')
self.classes = data[self.meta['key']]
self.class_to_idx = {_class: i for i, _class in enumerate(self.classes)}

Expand Down
6 changes: 1 addition & 5 deletions torchvision/datasets/lsun.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
else:
from collections.abc import Iterable

if sys.version_info[0] == 2:
import cPickle as pickle
else:
import pickle

import pickle
from .utils import verify_str_arg, iterable_to_str


Expand Down
1 change: 0 additions & 1 deletion torchvision/datasets/mnist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from .vision import VisionDataset
import warnings
from PIL import Image
Expand Down
1 change: 0 additions & 1 deletion torchvision/datasets/omniglot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from PIL import Image
from os.path import join
import os
Expand Down
1 change: 0 additions & 1 deletion torchvision/datasets/semeion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from PIL import Image
import os
import os.path
Expand Down
1 change: 0 additions & 1 deletion torchvision/datasets/stl10.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from PIL import Image
import os
import os.path
Expand Down
1 change: 0 additions & 1 deletion torchvision/datasets/svhn.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from .vision import VisionDataset
from PIL import Image
import os
Expand Down
1 change: 0 additions & 1 deletion torchvision/datasets/usps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from PIL import Image
import os
import numpy as np
Expand Down
8 changes: 1 addition & 7 deletions torchvision/datasets/voc.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import os
import sys
import tarfile
import collections
from .vision import VisionDataset

if sys.version_info[0] == 2:
import xml.etree.cElementTree as ET
else:
import xml.etree.ElementTree as ET

import xml.etree.ElementTree as ET
from PIL import Image
from .utils import download_url, check_integrity, verify_str_arg

Expand Down
2 changes: 0 additions & 2 deletions torchvision/models/detection/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import division

import math

import torch
Expand Down
2 changes: 0 additions & 2 deletions torchvision/models/detection/image_list.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
from __future__ import division

import torch
from torch.jit.annotations import List, Tuple
from torch import Tensor
Expand Down
1 change: 0 additions & 1 deletion torchvision/models/detection/roi_heads.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import division
import torch
import torchvision

Expand Down
2 changes: 0 additions & 2 deletions torchvision/models/detection/rpn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import division

# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import torch
from torch.nn import functional as F
Expand Down
2 changes: 0 additions & 2 deletions torchvision/models/detection/transform.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import division

import random
import math
import torch
Expand Down
2 changes: 0 additions & 2 deletions torchvision/models/googlenet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import division

import warnings
from collections import namedtuple
import torch
Expand Down
2 changes: 0 additions & 2 deletions torchvision/models/inception.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import division

from collections import namedtuple
import warnings
import torch
Expand Down
2 changes: 0 additions & 2 deletions torchvision/ops/boxes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import division

import torch
from torch.jit.annotations import Tuple
from torch import Tensor
Expand Down
1 change: 0 additions & 1 deletion torchvision/ops/misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import division
from collections import OrderedDict
from torch.jit.annotations import Optional, List
from torch import Tensor
Expand Down
2 changes: 0 additions & 2 deletions torchvision/ops/poolers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import division

# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import torch
import torch.nn.functional as F
Expand Down
1 change: 0 additions & 1 deletion torchvision/transforms/functional.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import division
import torch
import sys
import math
Expand Down
1 change: 0 additions & 1 deletion torchvision/transforms/functional_tensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import division
import torch
import torchvision.transforms.functional as F
from torch import Tensor
Expand Down
1 change: 0 additions & 1 deletion torchvision/transforms/transforms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import division
import torch
import math
import sys
Expand Down
21 changes: 3 additions & 18 deletions travis-scripts/run-clang-format/run-clang-format.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

"""

from __future__ import print_function, unicode_literals

import argparse
import codecs
import difflib
Expand Down Expand Up @@ -129,19 +127,14 @@ def run_clang_format_diff(args, file):
# > Each translation completely replaces the format string
# > for the diagnostic.
# > -- http://clang.llvm.org/docs/InternalsManual.html#internals-diag-translation
#
# It's not pretty, due to Python 2 & 3 compatibility.
encoding_py3 = {}
if sys.version_info[0] >= 3:
encoding_py3['encoding'] = 'utf-8'

try:
proc = subprocess.Popen(
invocation,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
**encoding_py3)
encoding='utf-8')
except OSError as exc:
raise DiffError(
"Command '{}' failed to start: {}".format(
Expand All @@ -150,12 +143,7 @@ def run_clang_format_diff(args, file):
)
proc_stdout = proc.stdout
proc_stderr = proc.stderr
if sys.version_info[0] < 3:
# make the pipes compatible with Python 3,
# reading lines should output unicode
encoding = 'utf-8'
proc_stdout = codecs.getreader(encoding)(proc_stdout)
proc_stderr = codecs.getreader(encoding)(proc_stderr)

# hopefully the stderr pipe won't get full and block the process
outs = list(proc_stdout.readlines())
errs = list(proc_stderr.readlines())
Expand Down Expand Up @@ -203,10 +191,7 @@ def red(s):
def print_diff(diff_lines, use_color):
if use_color:
diff_lines = colorize(diff_lines)
if sys.version_info[0] < 3:
sys.stdout.writelines((l.encode('utf-8') for l in diff_lines))
else:
sys.stdout.writelines(diff_lines)
sys.stdout.writelines(diff_lines)


def print_trouble(prog, message, use_colors):
Expand Down