Skip to content

Commit

Permalink
bug fix in dataset object
Browse files Browse the repository at this point in the history
  • Loading branch information
tongdaxu committed Apr 27, 2019
1 parent 310a432 commit 5278448
Show file tree
Hide file tree
Showing 5 changed files with 769 additions and 183 deletions.
536 changes: 509 additions & 27 deletions MouseProj/06-BBox Proposal Network-HPC Version.ipynb

Large diffs are not rendered by default.

273 changes: 132 additions & 141 deletions MouseProj/06-BBox Proposal Network.ipynb

Large diffs are not rendered by default.

34 changes: 23 additions & 11 deletions MouseProj/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def toTensorBV (sample):
# use if you use 370 BV image

image, label = sample['image'], sample['label']
imageTensor = torch.from_numpy(image-0.25) # normalize the image by mean reduction, the mean=0.25 here
labelTensor = torch.from_numpy(label) # no need to conduct anything special to it, I think
imageTensor = torch.from_numpy(((image-0.25)*256).copy()) # normalize the image by mean reduction, the mean=0.25 here
labelTensor = torch.from_numpy(label.copy()) # no need to conduct anything special to it, I think
labelTensor = torch.round(labelTensor)

return {'image': imageTensor, 'label': labelTensor}
Expand Down Expand Up @@ -97,7 +97,7 @@ def toTensor (sample):

return {'image': imageTensor, 'label': labelTensor}

def AffineFun(img, xr, yr, zr, xm, ym, zm, s, order):
def AffineFun(img, xr, yr, zr, xm, ym, zm, s, DS, order):
'''
Notes:
Rotate and move
Expand All @@ -122,6 +122,12 @@ def AffineFun(img, xr, yr, zr, xm, ym, zm, s, order):
xc = img[0].shape[0]//2
yc = img[0].shape[1]//2
zc = img[0].shape[2]//2

xout = np.round(img[0].shape[0]/DS).astype(np.int16)
yout = np.round(img[0].shape[1]/DS).astype(np.int16)
zout = np.round(img[0].shape[2]/DS).astype(np.int16)

imgout = np.zeros((xout, yout, zout))

Mc = np.array([ [1, 0, 0, xc],
[0, 1, 0, yc],
Expand All @@ -132,7 +138,12 @@ def AffineFun(img, xr, yr, zr, xm, ym, zm, s, order):
[0, 1/s, 0, 0],
[0, 0, 1/s, 0],
[0, 0, 0, 1]])


MD = np.array([ [DS, 0, 0, 0],
[0, DS, 0, 0],
[0, 0, DS, 0],
[0, 0, 0, 1]])

Rx = np.array([ [cosx, sinx, 0, 0],
[-sinx, cosx, 0, 0],
[0, 0, 1, 0],
Expand All @@ -158,9 +169,9 @@ def AffineFun(img, xr, yr, zr, xm, ym, zm, s, order):
[0, 0, 1, zm],
[0 ,0, 0, 1]])

Matrix = np.linalg.multi_dot([Mc, Ms, Rx, Ry, Rz, Mb, MM])
img[0] = affine_transform(img[0], Matrix, output_shape=img[0].shape, order=order)
return img
Matrix = np.linalg.multi_dot([Mc, Ms, Rx, Ry, Rz, Mb, MM, MD])
imgout = affine_transform(img[0], Matrix, output_shape=(xout, yout, zout), order=order)
return imgout[None,:,:,:]

def filpFun(img, x, y, z):
'''
Expand Down Expand Up @@ -248,11 +259,12 @@ class RandomAffine(object):
'''
Random rotation and move
'''
def __init__(self, fluR=0, fluM=0, fluS=1):
def __init__(self, fluR=0, fluM=0, fluS=1, DS=1):

self.fluR = fluR
self.fluM = fluM
self.fluS = fluS
self.DS = DS

def __call__(self, sample):

Expand All @@ -261,8 +273,8 @@ def __call__(self, sample):
s = np.random.uniform(1/self.fluS, self.fluS, size=1)

image, label = sample['image'], sample['label']
return {'image': AffineFun(image, xr, yr, zr, xm, ym, zm, s, 3), \
'label': AffineFun(label, xr, yr, zr, xm, ym, zm, s, 0)}
return {'image': AffineFun(image, xr, yr, zr, xm, ym, zm, s, self.DS, 3), \
'label': AffineFun(label, xr, yr, zr, xm, ym, zm, s, self.DS, 0)}

class niiDataset(Dataset):
'''
Expand Down Expand Up @@ -404,7 +416,7 @@ def __getitem__(self, indice):
BBox = torch.from_numpy(BBox)
# Get the BBox ground truth

sample = toTensor(sample)
sample = toTensorBV(sample)
imageTensor = sample['image']

sample = {'image':imageTensor, 'label':BBox}
Expand Down
8 changes: 4 additions & 4 deletions MouseProj/vnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ def __init__(self, img_size, out_size=6, elu=True):
x, y, z = img_size

self.in_tr = InputTransition(16, elu)
self.down_tr32 = DownTransition(16, 2, elu, dropout=False) # /2
self.down_tr64 = DownTransition(32, 3, elu, dropout=False) # /4
self.down_tr32 = DownTransition(16, 2, elu, dropout=True) # /2
self.down_tr64 = DownTransition(32, 3, elu, dropout=True) # /4
self.down_tr128 = DownTransition(64, 3, elu, dropout=True) # /8
self.down_tr256 = DownTransition(128, 3, elu, dropout=True) # /16
self.down_tr512 = DownTransition(256, 4, elu, dropout=True, res=True) # /32, 8 res fcn layers
self.down_tr512 = DownTransition(256, 6, elu, dropout=True, res=True) # /32, 8 res fcn layers

self.gap = nn.AvgPool3d(kernel_size = (x//32,y//32,z//32)) # N, C, 1, 1, 1

Expand All @@ -238,7 +238,7 @@ def forward(self, x):

out = self.fc1(out)
out = self.fc2(out)

return out


Expand Down
101 changes: 101 additions & 0 deletions Untitled1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import torch as tf"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'GeForce GTX 1080'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tf.cuda.get_device_name(0)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%capture output \n",
"while True:\n",
" print (time.time())"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'GeForce GTX 1080'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tf.cuda.get_device_name(0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 5278448

Please sign in to comment.