Skip to content

Commit

Permalink
Add missing copyright headers, code linting
Browse files Browse the repository at this point in the history
Reviewed By: rotabulo

Differential Revision: D32619633

fbshipit-source-id: fd4e66980cb63a46aed071b6ea3849eb5601cc11
  • Loading branch information
ducksoup authored and facebook-github-bot committed Nov 24, 2021
1 parent a8a3f82 commit 01d23bf
Show file tree
Hide file tree
Showing 29 changed files with 1,248 additions and 557 deletions.
60 changes: 37 additions & 23 deletions inplace_abn/_backend.pyi
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
# Copyright (c) Facebook, Inc. and its affiliates.

"""Stubs for the native methods"""
from typing import Tuple, Optional

import torch


def statistics(x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: ...


def reduce_statistics(all_mean: torch.Tensor, all_var: torch.Tensor, all_count: torch.Tensor) \
-> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: ...


def forward(x: torch.Tensor, mean: torch.Tensor, var: torch.Tensor,
weight: Optional[torch.Tensor], bias: Optional[torch.Tensor],
eps: float, activation, activation_param: float) -> None: ...


def backward_reduce(y_act: torch.Tensor, dy_act: torch.Tensor, weight: Optional[torch.Tensor],
bias: Optional[torch.Tensor], eps: float, activation, activation_param: float) \
-> Tuple[torch.Tensor, torch.Tensor]: ...


def backward_train(xhat: torch.Tensor, dy: torch.Tensor, var: torch.Tensor, count: torch.Tensor, sum_dy: torch.Tensor,
sum_xhat_dy: torch.Tensor, weight: Optional[torch.Tensor], eps: float) -> torch.Tensor: ...


def backward_test(dy: torch.Tensor, var: torch.Tensor, weight: Optional[torch.Tensor], eps: float) -> torch.Tensor: ...

def reduce_statistics(
all_mean: torch.Tensor, all_var: torch.Tensor, all_count: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: ...
def forward(
x: torch.Tensor,
mean: torch.Tensor,
var: torch.Tensor,
weight: Optional[torch.Tensor],
bias: Optional[torch.Tensor],
eps: float,
activation,
activation_param: float,
) -> None: ...
def backward_reduce(
y_act: torch.Tensor,
dy_act: torch.Tensor,
weight: Optional[torch.Tensor],
bias: Optional[torch.Tensor],
eps: float,
activation,
activation_param: float,
) -> Tuple[torch.Tensor, torch.Tensor]: ...
def backward_train(
xhat: torch.Tensor,
dy: torch.Tensor,
var: torch.Tensor,
count: torch.Tensor,
sum_dy: torch.Tensor,
sum_xhat_dy: torch.Tensor,
weight: Optional[torch.Tensor],
eps: float,
) -> torch.Tensor: ...
def backward_test(
dy: torch.Tensor, var: torch.Tensor, weight: Optional[torch.Tensor], eps: float
) -> torch.Tensor: ...

class Activation:
LeakyReLU = ...
Expand Down
2 changes: 2 additions & 0 deletions inplace_abn/abn.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) Facebook, Inc. and its affiliates.

from typing import Optional, Any

import torch
Expand Down
2 changes: 2 additions & 0 deletions inplace_abn/functions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) Facebook, Inc. and its affiliates.

from typing import Optional, Any
from warnings import warn

Expand Down
2 changes: 2 additions & 0 deletions inplace_abn/group.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) Facebook, Inc. and its affiliates.

import torch
import torch.distributed as distributed
import torch.nn as nn
Expand Down
14 changes: 9 additions & 5 deletions scripts/dataset/dataset.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) Facebook, Inc. and its affiliates.

import glob
from itertools import chain
from os import path
Expand All @@ -18,13 +20,15 @@ def __init__(self, in_dir, transform):

# Find all images
self.images = []
for img_path in chain(*(glob.iglob(path.join(self.in_dir, ext)) for ext in SegmentationDataset._EXTENSIONS)):
for img_path in chain(
*(
glob.iglob(path.join(self.in_dir, ext))
for ext in SegmentationDataset._EXTENSIONS
)
):
_, name_with_ext = path.split(img_path)
idx, _ = path.splitext(name_with_ext)
self.images.append({
"idx": idx,
"path": img_path
})
self.images.append({"idx": idx, "path": img_path})

def __len__(self):
return len(self.images)
Expand Down
7 changes: 5 additions & 2 deletions scripts/dataset/sampler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) Facebook, Inc. and its affiliates.

import torch
import torch.distributed as dist

Expand Down Expand Up @@ -29,14 +31,15 @@ def __init__(self, dataset, num_replicas=None, rank=None):
self.num_replicas = num_replicas
self.rank = rank
self.num_samples = (len(self.dataset) // self.num_replicas) + int(
(len(self.dataset) % self.num_replicas) < self.rank)
(len(self.dataset) % self.num_replicas) < self.rank
)

def __iter__(self):
# deterministically shuffle based on epoch
indices = torch.arange(0, len(self.dataset))

# subsample
indices = indices[self.rank::self.num_replicas]
indices = indices[self.rank :: self.num_replicas]

return iter(indices)

Expand Down
6 changes: 4 additions & 2 deletions scripts/dataset/transform.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) Facebook, Inc. and its affiliates.

from PIL import Image
from torchvision.transforms import functional as tfn

Expand All @@ -10,8 +12,8 @@ def __init__(self, longest_max_size, rgb_mean, rgb_std):

def __call__(self, img):
# Scaling
scale = self.longest_max_size/float(max(img.size[0],img.size[1]))
if scale != 1.:
scale = self.longest_max_size / float(max(img.size[0], img.size[1]))
if scale != 1.0:
out_size = tuple(int(dim * scale) for dim in img.size)
img = img.resize(out_size, resample=Image.BILINEAR)

Expand Down
16 changes: 9 additions & 7 deletions scripts/imagenet/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) Facebook, Inc. and its affiliates.

import json

DEFAULTS = {
Expand All @@ -10,23 +12,23 @@
"classes": 1000,
"dilation": 1,
"weight_gain_multiplier": 1, # note: this is ignored if weight_init == kaiming_*
"weight_init": "xavier_normal" # supported: xavier_[normal,uniform], kaiming_[normal,uniform], orthogonal
"weight_init": "xavier_normal", # supported: xavier_[normal,uniform], kaiming_[normal,uniform], orthogonal
},
"optimizer": {
"batch_size": 256,
"type": "SGD", # supported: SGD, Adam
"momentum": 0.9,
"weight_decay": 1e-4,
"clip": 1.,
"clip": 1.0,
"learning_rate": 0.1,
"classifier_lr": -1., # If -1 use same learning rate as the rest of the network
"classifier_lr": -1.0, # If -1 use same learning rate as the rest of the network
"nesterov": False,
"schedule": {
"type": "constant", # supported: constant, step, multistep, exponential, linear
"mode": "epoch", # supported: epoch, step
"epochs": 10,
"params": {}
}
"params": {},
},
},
"input": {
"scale_train": -1, # If -1 do not scale
Expand All @@ -36,8 +38,8 @@
"scale_val": 256, # If -1 do not scale
"crop_val": 224,
"mean": [0.485, 0.456, 0.406],
"std": [0.229, 0.224, 0.225]
}
"std": [0.229, 0.224, 0.225],
},
}


Expand Down
21 changes: 17 additions & 4 deletions scripts/imagenet/transforms.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Copyright (c) Facebook, Inc. and its affiliates.

from random import sample

import torch

# Default augmentation values compatible with ImageNet data augmentation pipeline
_DEFAULT_ALPHASTD = 0.1
_DEFAULT_EIGVAL = [0.2175, 0.0188, 0.0045]
_DEFAULT_EIGVEC = [[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]]
_DEFAULT_EIGVEC = [
[-0.5675, 0.7192, 0.4009],
[-0.5808, -0.0045, -0.8140],
[-0.5836, -0.6948, 0.4203],
]
_DEFAULT_BCS = [0.4, 0.4, 0.4]


Expand All @@ -19,13 +25,15 @@ def _blend(img1, img2, alpha):


class Lighting:
def __init__(self, alphastd=_DEFAULT_ALPHASTD, eigval=_DEFAULT_EIGVAL, eigvec=_DEFAULT_EIGVEC):
def __init__(
self, alphastd=_DEFAULT_ALPHASTD, eigval=_DEFAULT_EIGVAL, eigvec=_DEFAULT_EIGVEC
):
self._alphastd = alphastd
self._eigval = eigval
self._eigvec = eigvec

def __call__(self, img):
if self._alphastd == 0.:
if self._alphastd == 0.0:
return img

alpha = torch.normal(img.new_zeros(3), self._alphastd)
Expand Down Expand Up @@ -68,7 +76,12 @@ def __call__(self, img):


class ColorJitter(object):
def __init__(self, saturation=_DEFAULT_BCS[0], brightness=_DEFAULT_BCS[1], contrast=_DEFAULT_BCS[2]):
def __init__(
self,
saturation=_DEFAULT_BCS[0],
brightness=_DEFAULT_BCS[1],
contrast=_DEFAULT_BCS[2],
):
self._transforms = []
if saturation is not None:
self._transforms.append(Saturation(saturation))
Expand Down
Loading

0 comments on commit 01d23bf

Please sign in to comment.