저는 fMRI 분석을 처음 접했습니다. 나는 뇌 영상을 보면서 어떤 객체 (9 개의 객체 중 하나)에 대해 생각하고 있는지 결정하려고합니다. https://openfmri.org/dataset/ds000105/에있는 데이터 집합을 사용하고 있습니다. 그래서 저는 뇌 영상의 2D 슬라이스를 입력하여 신경망을 사용하여 9 개의 객체 중 1 개의 출력을 얻습니다. 아래의 코드에는 모든 단계와 이미지에 대한 세부 정보가 있습니다.신경망 예측은 pyBrain을 사용하여 fMRI 데이터 집합을 테스트하는 동안 항상 동일합니다. 왜?
import os, mvpa2, pyBrain
import numpy as np
from os.path import join as opj
from mvpa2.datasets.sources import OpenFMRIDataset
from pybrain.datasets import SupervisedDataSet,classification
path = opj(os.getcwd() , 'datasets','ds105')
of = OpenFMRIDataset(path)
#12th run of the 1st subject
ds = of.get_model_bold_dataset(model_id=1, subj_id=1,run_ids=[12])
#Get the unique list of 8 objects (sicissors, ...) and 'None'.
target_list = np.unique(ds.sa.targets).tolist()
#Returns Nibabel Image instance
img = of.get_bold_run_image(subj=1,task=1,run=12)
# Getting the actual image from the proxy image
img_data = img.get_data()
#Get the middle voxelds of the brain samples
mid_brain_slices = [x/2 for x in img_data.shape]
# Each image in the img_data is a 3D image of 40 x 64 x 64 voxels,
# and there are 121 such samples taken periodically every 2.5 seconds.
# Thus, a single person's brain is scanned for about 300 seconds (121 x 2.5).
# This is a 4D array of 3 dimensions of space and 1 dimension of time,
# which forms a matrix of (40 x 64 x 64 x 121)
# I only want to extract the slice of the 2D images brain in it's top view
# i.e. a series of 2D images 40 x 64
# So, i take the middle slice of the brain, hence compute the middle_brain_slices
DS = classification.ClassificationDataSet(40*64, class_labels=target_list)
# Loop over every brain image
for i in range(0,121):
#Image of brain at i th time interval
brain_instance = img_data[:,:,:,i]
# We will slice the brain to create 2D plots and use those 'pixels'
# as the features
slice_0 = img_data[mid_brain_slices[0],:,:,i] #64 x 64
slice_1 = img_data[:,mid_brain_slices[1],:,i] #40 x 64
slice_2 = img_data[:,:,mid_brain_slices[2],i] #40 x 64
#Note : we may actually only need one of these slices (the one with top view)
X = slice_2 #Possibly top view
# Reshape X from 40 x 64 to 1D vector 2560 x 1
X = np.reshape(X,40*64)
#Get the target at this intance (y)
y = ds.sa.targets[i]
y = target_list.index(y)
DS.appendLinked(X,y)
print DS.calculateStatistics()
print DS.classHist
print DS.nClasses
print DS.getClass(1)
# Generate y as a 9 x 1 matrix with eight 0's and only one 1 (in this training set)
DS._convertToOneOfMany(bounds=[0, 1])
#Split into Train and Test sets
test_data, train_data = DS.splitWithProportion(0.25)
#Note : I think splitWithProportion will also internally shuffle the data
#Build neural network
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure.modules import SoftmaxLayer
nn = buildNetwork(train_data.indim, 64, train_data.outdim, outclass=SoftmaxLayer)
from pybrain.supervised.trainers import BackpropTrainer
trainer = BackpropTrainer(nn, dataset=train_data, momentum=0.1, learningrate=0.01 , verbose=True, weightdecay=0.01)
trainer.trainUntilConvergence(maxEpochs = 20)
라인 nn.activate(X_test[i])
는 2,560 입력을 받아, 오른쪽 확률 출력을 생성 할 것인가? 예측 된 y 벡터 (모양 9 x 1)에서
따라서 9 개의 값 중 가장 높은 값이 대답으로 지정되어야한다고 가정합니다. 그러나 y_test [i]로 검증 할 때가 아닙니다. 또한 모든 테스트 샘플에 대해 X_test에 대해 비슷한 값을 얻습니다. 이게 왜 그렇게? I 위의 코드를 포함하면
#Just splitting the test and trainset
X_train = train_data.getField('input')
y_train = train_data.getField('target')
X_test = test_data.getField('input')
y_test = test_data.getField('target')
#Testing the network
for i in range(0,len(X_test)):
print nn.activate(X_test[i])
print y_test[i]
여기 X_test 일부 값은 다음
.
.
.
nn.activated = [ 0.44403205 0.06144328 0.04070154 0.09399672 0.08741378 0.05695479 0.08178353 0.0623408 0.07133351]
y_test [0 1 0 0 0 0 0 0 0]
nn.activated = [ 0.44403205 0.06144328 0.04070154 0.09399672 0.08741378 0.05695479 0.08178353 0.0623408 0.07133351]
y_test [1 0 0 0 0 0 0 0 0]
nn.activated = [ 0.44403205 0.06144328 0.04070154 0.09399672 0.08741378 0.05695479 0.08178353 0.0623408 0.07133351]
y_test [0 0 0 0 0 0 1 0 0]
.
.
.
따라서 44.4 %에 관계없이 샘플 값의 모든 사례 ID의 인덱스 0 인 시험 샘플의 확률. 실제 값은 계속 변합니다.
print 'print predictions: ' , trainer.testOnClassData (dataset=test_data)
x = []
for item in y_test:
x.extend(np.where(item == 1)[0])
print 'print actual: ' , x
여기서, 상기 출력 비교이다
print predictions: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
print actual: [7, 0, 4, 8, 2, 0, 2, 1, 0, 6, 1, 4]
모든 예측 제 항목이다. 나는 그 문제가 뭔지 모른다. 내가 함께 전에 이러한 모든 도구를 사용하거나 이런 종류의에서 구체적으로 일을하지 않았기 때문에 -
Total error: 0.0598287764931
Total error: 0.0512272330797
Total error: 0.0503835076374
Total error: 0.0486402801867
Total error: 0.0498354140541
Total error: 0.0495447833038
Total error: 0.0494208449895
Total error: 0.0491162599037
Total error: 0.0486775862084
Total error: 0.0486638648161
Total error: 0.0491337891419
Total error: 0.0486965691406
Total error: 0.0490016912735
Total error: 0.0489939195858
Total error: 0.0483910986235
Total error: 0.0487459940103
Total error: 0.0485516142106
Total error: 0.0477407360102
Total error: 0.0490661144891
Total error: 0.0483103097669
Total error: 0.0487965594586
헤이 TommyP을, 빠른 답변 감사합니다. 제 의도는 당신이 언급 한 것처럼 FeedForwardNetwork를 만드는 것이 었습니다. 이 모델에서는 40 x 64 = 2560 기능을 사용합니다. 하지만 나는 약 100 개의 샘플에 대해서만 훈련을했고 테스트를 위해 나머지는 사용했다. 나는 샘플 수를 늘려야한다고 생각 했으므로 나는 약 1400 개로했다. 나는 여전히 같은 문제로 끝납니다. pybrain에서 함수를 사용할 때마다 docs 코드를 참조합니다. 문제 없습니다. 어리석은 실수가 없기 때문에 좋은 해결책을 찾아야 할 것입니다. 입력 해 주셔서 다시 한 번 감사드립니다. :) –
예, 미안 해요, 나는 명백한 것을 볼 수 없었습니다. 그러나 저는 비슷한 상황에 처해 있었고 도서관의 가정에 의해 넘어졌습니다. 추적하는 것은 어려운 일입니다. 이전에 실제로 소스 코드로 이동하여 성공한 적이 있었고 코드에서 함수 호출을 라이브러리의 소스 코드로 바꾸면 디버깅 인쇄 문을 추가하고 가정 된 매개 변수 등을 수정할 수 있습니다. 행운을 빕니다! –