from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR

from helper import get_data, split_data, visualize

name = 'Support vector'

if __name__ == '__main__':
    x, y = get_data()
    y = y.reshape(len(y), 1)
    x_train, x_test, y_train, y_test = split_data(x, y)

    x_scaler = StandardScaler()
    y_scaler = StandardScaler()

    x_train = x_scaler.fit_transform(x_train)
    x_test = x_scaler.transform(x_test)

    y_train = y_scaler.fit_transform(y_train)
    y_test = y_scaler.transform(y_test)

    regression = SVR(kernel='rbf')
    regression.fit(x_train, y_train)

    y_predicted = regression.predict(x_test)
    y_predicted = y_scaler.inverse_transform(y_predicted)
    y_test = y_scaler.inverse_transform(y_test)

    visualize(y_test, y_predicted, name)
Exemplo n.º 2
0
    Linear_test_aug = transforms.Compose(
        [transforms.ToPILImage(),
         transforms.ToTensor()])

    Linear_testset = ImageDataset(root_dir=test_root,
                                  class_file=classFile,
                                  transforms=Linear_test_aug)

    Linear_testloader = DataLoader(Linear_testset,
                                   batch_size=512,
                                   shuffle=False,
                                   num_workers=num_worker)

    # ========== [visualize] ==========
    if batch_size >= 64:
        visualize(Linear_trainloader, dir_log + '/' + 'visual.png')

    # ========== [device] =============
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    # ========== [cnn model] ==========
    ckpt = torch.load(dir_ckpt + '/' + 'best.pt')
    model = ResNet(pretrain=False)
    model.load_state_dict(ckpt["cnn"])
    model.to(device)
    linear_clf = Linear_Classifier(classNum=10)
    linear_clf.load_state_dict(ckpt["clf"])
    linear_clf.to(device)
    # opt_clf = optim.SGD(linear_clf.parameters(),
    #                      lr=1e-2,
    #                      momentum=0.9,
Exemplo n.º 3
0
f_ishift = np.fft.ifftshift(s)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)

fig, ax = plt.subplots(figsize=(20, 10))
plt.subplot(141), plt.imshow(img.numpy(), cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(142), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('unmasked k-space'), plt.xticks([]), plt.yticks([])
plt.subplot(143), plt.imshow(20 * np.log(np.abs(s)), cmap='gray')
plt.title('masked k-space'), plt.xticks([]), plt.yticks([])
plt.subplot(144), plt.imshow(img_back, cmap='gray')
plt.title('ifft'), plt.xticks([]), plt.yticks([])
plt.show()

helper.visualize(helper.difference(img.numpy(), img_back))

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
transform = transforms.Compose([
    transforms.Resize(256),
    transforms.Grayscale(num_output_channels=1),
    transforms.ToTensor()
])
batch_size = 20
# load the training and test datasets
train_data = datasets.ImageFolder(
    root=
    'C:\\Users\\tobi9\\Documents\\GitHub\\RefineGAN\\data\\brain\\db_train',
    transform=transform)
test_data = datasets.ImageFolder(
Exemplo n.º 4
0
from sklearn import svm
import numpy 
import pylab
from helper import visualize

X=numpy.array([[0,0],[1,1],[1,0],[0,1]])
Y=[0,1,1,1]
svc=svm.SVC(C=1.0,kernel='linear')
trainedsvm=svc.fit(X,Y)
visualize(trainedsvm,X,Y,[[-1.5,1.5],[-1.5,1.5]],100)
print trainedsvm.predict([0,0])
print trainedsvm.predict([1,0])
Exemplo n.º 5
0
    Linear_testset = ImageDataset(
        root_dir=test_root,
        class_file=classFile,
        transforms=Linear_test_aug
    )

    Linear_testloader = DataLoader(
        Linear_testset,
        batch_size=256,
        shuffle=False,
        num_workers=num_worker
    )

    # ========== [visualize] ==========
    if batch_size >= 64:
        visualize(trainloader, dir_log + '/' + 'visual.png')

    # ========== [device] =============
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    # ========== [cnn model] ==========
    model = Resnet18()
    model.to(device)
    g = Projector(input_size=project_in, hidden_size=project_hidden, output_size=project_out)
    g.to(device)

    # ========== [optim for cnn] ==========
    opt_cnn = optim.SGD(
        list(model.parameters()) + list(g.parameters()),
        lr=lr,
        momentum=momentnum,
from sklearn import svm
import numpy 
import pylab
from helper import visualize ,dataFromFile

obj1=dataFromFile('object1.txt')
obj2=dataFromFile('object2.txt')
label1= [1]*len(obj1)
label2=[0]*len(obj2)

trainingData=numpy.array( obj1+obj2)
labels=numpy.array(label1+label2)

svc=svm.SVC(C=1.0,kernel='linear')
trainedsvm=svc.fit(trainingData,labels)

visualize(trainedsvm,trainingData,labels,[[-1,25],[-1,25]],100)

#X=numpy.array([[0,0],[1,1],[1,0],[0,1]])
#Y=[0,1,1,1]
#trainedsvm=svc.fit(X,Y)
#print "True Label for [0,0] :" ,0, " Predicted :" ,trainedsvm.predict([0,0])
#print "True Label for [1,0:" ,1, " Predicted :" ,trainedsvm.predict([1,0])

#visualize(trainedsvm,X,Y,[[-1.5,1.5],[-1.5,1.5]],100)

#sample_weight=[2,1,1,1]
Exemplo n.º 7
0
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 90, 1)
img_rot = np.rot90(img)
img_180 = np.rot90(img_rot)

img_rot = [[1, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 0]]
img_180 = np.rot90(img_rot)

f_90 = np.fft.fft2(img_rot)
fshift_90 = np.fft.fftshift(f_90)
magnitude_spectrum_90 = (20 * np.log(np.abs(fshift_90)))

f_180 = np.fft.fft2(img_180)
fshift_180 = np.fft.fftshift(f_180)
magnitude_spectrum_180 = (20 * np.log(np.abs(fshift_180)))

helper.visualize(helper.difference(img, img_back))
#helper.visualize(helper.difference(magnitude_spectrum,magnitude_spectrum_180))

fig, ax = plt.subplots(figsize=(20, 10))
plt.subplot(141), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(142), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('unmasked k-space'), plt.xticks([]), plt.yticks([])
plt.subplot(143), plt.imshow(ims, cmap='gray')
plt.title('masked k-space'), plt.xticks([]), plt.yticks([])
plt.subplot(144), plt.imshow(img_back, cmap='gray')
plt.title('ifft'), plt.xticks([]), plt.yticks([])
plt.show()

fig, ax = plt.subplots(figsize=(20, 10))
plt.subplot(141), plt.imshow(img_rot, cmap='gray')
Exemplo n.º 8
0
    return t, total


def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())


if __name__ == '__main__':

    info('main line')

    # make larger file
    file_iterations('filedata.txt', 100000, 'newfile.txt')

    # # of processes
    n = [1, 2, 3, 4, 5]
    # times of processing, and total size of data streamed into buffer
    times = []
    sizes = []

    for i in n:
        result = test_mfmb(i)
        times.append(result[0])
        sizes.append(result[1])

    visualize(times, n, sizes)