Skip to content

Instantly share code, notes, and snippets.

View omarsar's full-sized avatar
🐙

Elvis Saravia omarsar

🐙
View GitHub Profile
test_acc = 0.0
for i, data in enumerate(testloader, 0):
inputs, labels = data
inputs = inputs.view(-1, 28, 28)
outputs = model(inputs)
test_acc += get_accuracy(outputs, labels, BATCH_SIZE)
print('Test Accuracy: %.2f'%( test_acc/i))
tensor([[0.6741],
[0.9680],
[0.9973],
[0.9993]])
tensor([[ 0.9889],
[-0.4191],
[ 0.8347],
[-0.0384]])
@omarsar
omarsar / data_mining_fall_2018.md
Last active October 17, 2018 05:15
Instructions for Data Mining Lab Session 1

Computing Resources

  • Operating system: Preferably Linux or MacOS. If you have Windows, things may crash unexpectedly (try installing a virtual machine if you need to)
  • RAM: Minimum 8GB
  • Disk space: Mininium 16GB

Software Requirements

In this lab session we are going to be using Python as our main programming language. If you are not familiar with it, I recommend you start with this free Python course offered by Codecademy.

Here is a list of the required programs and libraries necessary for this lab session. (Please install them before coming to our lab session on Tuesday; this will save us a lot of time, plus these include some of the same libraries you may need for your first assignment).

pip install medicaltorch
from collections import defaultdict
import time
import os
import numpy as np
from tqdm import tqdm
from tensorboardX import SummaryWriter
!pip3 install http://download.pytorch.org/whl/cu80/torch-0.4.0-cp36-cp36m-linux_x86_64.whl
!pip3 install torchvision
!pip install medicaltorch
!pip3 install numpy==1.14.1
ROOT_DIR_GMCHALLENGE = "/gdrive/My Drive/DAIR RESOURCES/PyTorch/medical_imaging/train/"
mri_input_filename = os.path.join(ROOT_DIR_GMCHALLENGE,
'site1-sc01-image.nii.gz')
mri_gt_filename = os.path.join(ROOT_DIR_GMCHALLENGE,
'site1-sc01-mask-r1.nii.gz')
pair = mt_datasets.SegmentationPair2D(mri_input_filename, mri_gt_filename)
slice_pair = pair.get_pair_slice(0)
input_slice = slice_pair["input"]
gt_slice = slice_pair["gt"]
# transformer
composed_transform = transforms.Compose([
mt_transforms.Resample(0.25, 0.25),
mt_transforms.CenterCrop2D((200, 200)),
mt_transforms.ToTensor(),
])
# load data
train_dataset = mt_datasets.SCGMChallenge2DTrain(root_dir=ROOT_DIR_GMCHALLENGE, transform=composed_transform)
def threshold_predictions(predictions, thr=0.999):
thresholded_preds = predictions[:]
low_values_indices = thresholded_preds < thr
thresholded_preds[low_values_indices] = 0
low_values_indices = thresholded_preds >= thr
thresholded_preds[low_values_indices] = 1
return thresholded_preds
# training dataset
train_transform = transforms.Compose([
mt_transforms.Resample(0.25, 0.25),
mt_transforms.CenterCrop2D((200, 200)),
mt_transforms.ElasticTransform(alpha_range=(28.0, 30.0),
sigma_range=(3.5, 4.0),
p=0.3),
mt_transforms.RandomAffine(degrees=4.6,
scale=(0.98, 1.02),
translate=(0.03, 0.03)),