Commit 1610268e authored by Alykhan Tejani's avatar Alykhan Tejani
Browse files

use integer division in tests for array slicing + various PEP-8 fixes

parent 6c7733f0
import torch
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import numpy as np
import unittest
import random
class Tester(unittest.TestCase):
def test_crop(self):
height = random.randint(10, 32) * 2
......@@ -13,9 +12,9 @@ class Tester(unittest.TestCase):
owidth = random.randint(5, (width - 2) / 2) * 2
img = torch.ones(3, height, width)
oh1 = (height - oheight) / 2
ow1 = (width - owidth) / 2
imgnarrow = img[:, oh1 :oh1 + oheight, ow1 :ow1 + owidth]
oh1 = (height - oheight) // 2
ow1 = (width - owidth) // 2
imgnarrow = img[:, oh1:oh1 + oheight, ow1:ow1 + owidth]
imgnarrow.fill_(0)
result = transforms.Compose([
transforms.ToPILImage(),
......@@ -23,7 +22,7 @@ class Tester(unittest.TestCase):
transforms.ToTensor(),
])(img)
assert result.sum() == 0, "height: " + str(height) + " width: " \
+ str( width) + " oheight: " + str(oheight) + " owidth: " + str(owidth)
+ str(width) + " oheight: " + str(oheight) + " owidth: " + str(owidth)
oheight += 1
owidth += 1
result = transforms.Compose([
......@@ -33,7 +32,7 @@ class Tester(unittest.TestCase):
])(img)
sum1 = result.sum()
assert sum1 > 1, "height: " + str(height) + " width: " \
+ str( width) + " oheight: " + str(oheight) + " owidth: " + str(owidth)
+ str(width) + " oheight: " + str(oheight) + " owidth: " + str(owidth)
oheight += 1
owidth += 1
result = transforms.Compose([
......@@ -43,9 +42,9 @@ class Tester(unittest.TestCase):
])(img)
sum2 = result.sum()
assert sum2 > 0, "height: " + str(height) + " width: " \
+ str( width) + " oheight: " + str(oheight) + " owidth: " + str(owidth)
+ str(width) + " oheight: " + str(oheight) + " owidth: " + str(owidth)
assert sum2 > sum1, "height: " + str(height) + " width: " \
+ str( width) + " oheight: " + str(oheight) + " owidth: " + str(owidth)
+ str(width) + " oheight: " + str(oheight) + " owidth: " + str(owidth)
def test_scale(self):
height = random.randint(24, 32) * 2
......@@ -100,19 +99,19 @@ class Tester(unittest.TestCase):
transforms.Pad(padding),
transforms.ToTensor(),
])(img)
assert result.size(1) == height + 2*padding
assert result.size(2) == width + 2*padding
assert result.size(1) == height + 2 * padding
assert result.size(2) == width + 2 * padding
def test_lambda(self):
trans = transforms.Lambda(lambda x: x.add(10))
x = torch.randn(10)
y = trans(x)
assert(y.equal(torch.add(x, 10)))
assert (y.equal(torch.add(x, 10)))
trans = transforms.Lambda(lambda x: x.add_(10))
x = torch.randn(10)
y = trans(x)
assert(y.equal(x))
assert (y.equal(x))
if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment