Skip to content

Commit

Permalink
Cleanup using autopep8 (#104)
Browse files Browse the repository at this point in the history
* autopep8

* autopep8

* pep8
  • Loading branch information
ieee8023 committed Sep 12, 2022
1 parent 9c01e15 commit 79c0a71
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 377 deletions.
1 change: 1 addition & 0 deletions pep8.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
autopep8 --in-place --select=E1,E2,E3,W1,W2 torchxrayvision/**.py torchxrayvision/**/**.py torchxrayvision/**/**/**.py
71 changes: 34 additions & 37 deletions torchxrayvision/autoencoders.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import torch
import torch.nn as nn
import torchvision
from torch.nn import Module
import urllib
import pathlib
import torch.nn.functional as F
import os
import numpy as np
import sys
import requests

model_urls = {}

model_urls = {}
model_urls['101-elastic'] = {
"description": 'This model was trained on the datasets: nih pc rsna mimic_ch chex datasets.',
"weights_url": 'https://github.com/mlmed/torchxrayvision/releases/download/v1/nihpcrsnamimic_ch-resnet101-2-ae-test2-elastic-e250.pt',
"image_range": [-1024,1024],
"class":"ResNetAE101"
}
"image_range": [-1024, 1024],
"class": "ResNetAE101"
}


class Bottleneck(nn.Module):
expansion = 4
Expand Down Expand Up @@ -104,11 +102,12 @@ def forward(self, x):

return out


# source: https://github.com/ycszen/pytorch-segmentation/blob/master/resnet.py
class _ResNetAE(nn.Module):
def __init__(self, downblock, upblock, num_layers, n_classes):
super(_ResNetAE, self).__init__()

self.in_channels = 64

self.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
Expand All @@ -121,7 +120,7 @@ def __init__(self, downblock, upblock, num_layers, n_classes):
self.layer3 = self._make_downlayer(downblock, 256, num_layers[2], stride=2)
self.layer4 = self._make_downlayer(downblock, 128, num_layers[3], stride=6)

self.uplayer1 = self._make_up_block(upblock, 128, num_layers[3], stride=6)
self.uplayer1 = self._make_up_block(upblock, 128, num_layers[3], stride=6)
self.uplayer2 = self._make_up_block(upblock, 64, num_layers[2], stride=2)
self.uplayer3 = self._make_up_block(upblock, 32, num_layers[1], stride=2)
self.uplayer4 = self._make_up_block(upblock, 16, num_layers[0], stride=2)
Expand All @@ -133,13 +132,13 @@ def __init__(self, downblock, upblock, num_layers, n_classes):
self.uplayer_top = DeconvBottleneck(self.in_channels, 64, 1, 2, upsample)

self.conv1_1 = nn.ConvTranspose2d(64, n_classes, kernel_size=1, stride=1, bias=False)

def __repr__(self):
if self.weights != None:
return "XRV-ResNetAE-{}".format(self.weights)
else:
return "XRV-ResNetAE"

def _make_downlayer(self, block, init_channels, num_layer, stride=1):
downsample = None
if stride != 1 or self.in_channels != init_channels * block.expansion:
Expand All @@ -160,13 +159,13 @@ def _make_up_block(self, block, init_channels, num_layer, stride=1):
# expansion = block.expansion
if stride != 1 or self.in_channels != init_channels * 2:
upsample = nn.Sequential(
nn.ConvTranspose2d(self.in_channels, init_channels * 2,kernel_size=1, stride=stride, bias=False, output_padding=1),
nn.ConvTranspose2d(self.in_channels, init_channels * 2, kernel_size=1, stride=stride, bias=False, output_padding=1),
nn.BatchNorm2d(init_channels * 2),
)
layers = []
for i in range(1, num_layer):
layers.append(block(self.in_channels, init_channels, 4))

layers.append(block(self.in_channels, init_channels, 2, stride, upsample))
self.in_channels = init_channels * 2
return nn.Sequential(*layers)
Expand All @@ -182,11 +181,11 @@ def encode(self, x):
x = self.layer3(x)
x = self.layer4(x)
return x

def features(self, x):
return self.encode(x)

def decode(self, x, image_size=[1,1,224,224]):
def decode(self, x, image_size=[1, 1, 224, 224]):
x = self.uplayer1(x)
x = self.uplayer2(x)
x = self.uplayer3(x)
Expand All @@ -195,14 +194,15 @@ def decode(self, x, image_size=[1,1,224,224]):

x = self.conv1_1(x, output_size=image_size)
return x

def forward(self, x):
ret = {}
ret["z"] = z = self.encode(x)
ret["out"] = self.decode(z, x.size())

return ret



def ResNetAE50(**kwargs):
return _ResNetAE(Bottleneck, DeconvBottleneck, [3, 4, 6, 3], 1, **kwargs)

Expand All @@ -212,21 +212,21 @@ def ResNetAE101(**kwargs):


def ResNetAE(weights=None):

if weights == None:
return ResNetAE101()

if not weights in model_urls.keys():
raise Exception("weights value must be in {}".format(list(model_urls.keys())))

method_to_call = globals()[model_urls[weights]["class"]]
ae = method_to_call()
## load pretrained models

# load pretrained models
url = model_urls[weights]["weights_url"]
weights_filename = os.path.basename(url)
weights_storage_folder = os.path.expanduser(os.path.join("~",".torchxrayvision","models_data"))
weights_filename_local = os.path.expanduser(os.path.join(weights_storage_folder,weights_filename))
weights_storage_folder = os.path.expanduser(os.path.join("~", ".torchxrayvision", "models_data"))
weights_filename_local = os.path.expanduser(os.path.join(weights_storage_folder, weights_filename))

if not os.path.isfile(weights_filename_local):
print("Downloading weights...")
Expand All @@ -239,17 +239,15 @@ def ResNetAE(weights=None):
ae.load_state_dict(state_dict)
except Exception as e:
print("Loading failure. Check weights file:", weights_filename_local)
raise(e)
raise (e)

ae = ae.eval()

ae.weights = weights
ae.description = model_urls[weights]["description"]

return ae

import sys
import requests


# from here https://sumit-ghosh.com/articles/python-download-progress-bar/
def download(url, filename):
Expand All @@ -262,11 +260,10 @@ def download(url, filename):
else:
downloaded = 0
total = int(total)
for data in response.iter_content(chunk_size=max(int(total/1000), 1024*1024)):
for data in response.iter_content(chunk_size=max(int(total / 1000), 1024 * 1024)):
downloaded += len(data)
f.write(data)
done = int(50*downloaded/total)
sys.stdout.write('\r[{}{}]'.format('█' * done, '.' * (50-done)))
done = int(50 * downloaded / total)
sys.stdout.write('\r[{}{}]'.format('█' * done, '.' * (50 - done)))
sys.stdout.flush()
sys.stdout.write('\n')

74 changes: 31 additions & 43 deletions torchxrayvision/baseline_models/chexpert/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import sys, os
thisfolder = os.path.dirname(__file__)
sys.path.insert(0,thisfolder)
import torch
import csv
import numpy as np
import json
import argparse
import urllib
import pathlib
sys.path.insert(0, thisfolder)
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
from .model import Tasks2Models


Expand All @@ -21,52 +12,50 @@ class DenseNet(nn.Module):
CheXpert: A Large Chest Radiograph Dataset with Uncertainty Labels and Expert Comparison.
AAAI Conference on Artificial Intelligence.
http:https://arxiv.org/abs/1901.07031
Setting num_models less than 30 will load a subset of the ensemble.
Modified for torchxrayvision to maintain the pytorch gradient tape
and also to provide the features() argument.
Weights can be found:
https://academictorrents.com/details/5c7ee21e6770308f2d2b4bd829e896dbd9d3ee87
https://archive.org/download/torchxrayvision_chexpert_weights/chexpert_weights.zip
"""

def __init__(self, weights_zip="", num_models=30):

super(DenseNet, self).__init__()

url = "https://academictorrents.com/details/5c7ee21e6770308f2d2b4bd829e896dbd9d3ee87"
self.weights_zip = weights_zip
self.num_models = num_models

if self.weights_zip == "":
raise Exception("Need to specify weights_zip file location. You can download them from {}".format(url))

self.use_gpu = torch.cuda.is_available()
dirname = os.path.dirname(os.path.realpath(__file__))
self.model = Tasks2Models(os.path.join(dirname, 'predict_configs.json'),
weights_zip=self.weights_zip,
num_models=self.num_models,
dynamic=False,
use_gpu=self.use_gpu)
self.model = Tasks2Models(os.path.join(dirname, 'predict_configs.json'),
weights_zip=self.weights_zip,
num_models=self.num_models,
dynamic=False,
use_gpu=self.use_gpu)

self.upsample = nn.Upsample(size=(320, 320), mode='bilinear', align_corners=False)


self.pathologies = ["Atelectasis", "Cardiomegaly", "Consolidation", "Edema", "Effusion"]



def forward(self, x):
x = x.repeat(1, 3, 1, 1)
x = self.upsample(x)
#expecting values between [-1024,1024]
x = x/512
#now between [-2,2] for this model

# expecting values between [-1024,1024]
x = x / 512
# now between [-2,2] for this model

outputs = []
for sample in x: #sorry hard to make parallel
for sample in x: # sorry hard to make parallel
all_task2prob = {}
for tasks in self.model:
task2prob = self.model.infer(sample.unsqueeze(0), tasks)
Expand All @@ -76,30 +65,29 @@ def forward(self, x):
output = [all_task2prob[patho] for patho in ["Atelectasis", "Cardiomegaly", "Consolidation", "Edema", "Pleural Effusion"]]
output = torch.stack(output)
outputs.append(output)

return torch.stack(outputs)

def features(self, x):
x = x.repeat(1, 3, 1, 1)
x = self.upsample(x)
#expecting values between [-1024,1024]
x = x/512
#now between [-2,2] for this model

# expecting values between [-1024,1024]
x = x / 512
# now between [-2,2] for this model

outputs = []
for sample in x: #sorry hard to make parallel
for sample in x: # sorry hard to make parallel
all_feats = []
for tasks in self.model:
task2prob = self.model.features(sample.unsqueeze(0), tasks)
all_feats.append(task2prob)
feats = torch.stack(all_feats)
outputs.append(feats.flatten())

return torch.stack(outputs)



def __repr__(self):
return "CheXpert-DenseNet121-ensemble"


Loading

0 comments on commit 79c0a71

Please sign in to comment.