Example #1
0
import torch.nn.functional as F
from loadImDat import loadData
from UTILS.class_dict import dictionary
import torchvision
from keras.datasets import mnist
from loadImDat import scaleTo01
import matplotlib.pyplot as plt

model_dir = "/nvme_ssd/bensCode/SparseCoding/models/model3.pt"
data_size = 32**2
code_size = 1 * data_size
device = "gpu"
net = torch.load(model_dir)
net.eval()

(train_X, train_y), (test_X, test_y) = mnist.load_data()

trainload, testload = loadData("mnist", 32, 10)
train_data = []
for batch_idx, (true_sigs, im_labels) in enumerate(trainload):
    train_data.append(true_sigs)
im_test = train_data[9][0]
im_test2 = im_test.reshape(32, 32)
og_image = plt.imshow(im_test2)
# plt.show()
im_test = im_test.to('cuda:0')
yhat = net(im_test)
yhat = yhat.cpu().data.numpy()
print(yhat.shape)
yhat = yhat.reshape(32, 32)
Example #2
0
learnRate = 2e2
learnRateDecay = 0.999
fistaIters = 200

# Logistics.
USE_CUDA = True
savePath = 'results/'

# **** reproduce results using this function. to play, comment it out. *****
# batchSize, L1_weight, learnRate, learnRateDecay = fetchVizParams(datName)
# ************

#######################################################
# (2) Set up data loader and train dictionary.
#######################################################
trainSet, testSet = loadData(dataset, patchSize, batchSize)

atomImName = dataset + str(patchSize) + '_demoDict'

print('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
print('DICTIONARY TRAINING XXXXXXXXXXXXXXXXXXXX')
Dict, lossHist, errHist, spstyHist = trainDictionary(
    trainSet,
    testSet,
    sigLen,
    codeLen,
    datName,
    maxEpoch=maxEpoch,
    fistaIters=fistaIters,
    l1w=L1_weight,
    batchSize=batchSize,
Example #3
0
def get_tensor_complex_image(filepath):
    hf=h5py.File(filepath)
    volume_kspace=hf['kspace'][()]
    slice_kspace = volume_kspace[20] # Choosing the 20-th slice of this volume
    slice_kspace2 = T.to_tensor(slice_kspace)      # Convert from numpy array to pytorch tensor
    slice_image = T.ifft2(slice_kspace2)           # Apply Inverse Fourier Transform to get the complex image
    slice_image_abs = T.complex_abs(slice_image)   # Compute absolute value to get a real image
    resize1=resize(slice_image_abs,(500,500),anti_aliasing=True)
    # slice_image_abs=slice_image_abs.numpy()
    return resize1

tensorfied_images=[]
for image in all_files:
    c=get_tensor_complex_image(image)
    tensorfied_images.append(c)
thing=loadData(tensorfied_images,500,10)
train_data=[]
for batch_idx, (true_sigs) in enumerate(thing):
    train_data.append(true_sigs)
im_test=train_data[9][0]
print(im_test)
print(type(im_test))
print(im_test.shape)
im_test=im_test.numpy()
im_test2=im_test.reshape(500,500)
plt.imshow(im_test2,cmap='grey')
plt.show()
# print(thing)
# print(len(tensorfied_images))
# print(tensorfied_images[0])
Example #4
0
optimizer_module = getattr(torch.optim, optName)

# Scheduler.
scheduler = None
if optName == 'SGD':
    scheduler = torch.optim.lr_scheduler.StepLR(OPT,
                                                step_size=1,
                                                gamma=args.learn_rate_decay)

# First set up the data using the data-seed.
print('\n* Loading dataset {}'.format(args.dataset))
#if   args.use_validation_size>0:
#  dataName  = 'valid_'
#TODO: valid!!
rand.seed(args.data_seed)
train_loader, test_loader = loadData(dataset, patch_size, batch_size)

if hasattr(train_loader, 'numSamples'):
    numTrData = train_loader.numSamples
    numTeData = test_loader.numSamples
else:
    numTrData = len(train_loader.dataset)
    numTeData = len(test_loader.dataset)

# Now set up CUDA and reset all RNG's.
device = torch.device(
    "cuda:0" if not args.no_cuda and torch.cuda.is_available() else "cpu")
torch.manual_seed(args.seed)
if device.type != 'cpu':
    print('\033[93m' + 'Using CUDA' + '\033[0m')
    torch.cuda.manual_seed(args.seed)