コード例 #1
0
# This file is the old version, used to compute activation
# but the order in act.npy is wrong

import numpy as np
from dnnbrain.dnn.models import AlexNet
from dnnbrain.dnn.core import Mask, Stimulus
from os.path import join as pjoin

# load pic to compute activation
main_path = '/nfs/s2/userhome/zhouming/workingdir/numerosity/stimulus' 
out_path = '/nfs/s2/userhome/zhouming/workingdir/numerosity/out/activation' 
stim_path = '/nfs/s2/userhome/zhouming/workingdir/numerosity/stim/csv/sci_BD.stim.csv' 
dnn = AlexNet()
stim = Stimulus()
stim.load(stim_path)
layer_all = ['fc1_relu', 'fc2_relu']
chn = 'all'
# start computing
for layer in layer_all:
    mask = Mask(layer,chn)
    act = dnn.compute_activation(stim, mask).get(layer)
    np.save(pjoin(out_path, f'act_{layer}_test_numerosity.npy'), act)
    del act

# using activation to select the numerosity unit
#for stim in stim_set:
#    stim_path = pjoin(main_path, stim)
#    pic_all = np.zeros((len(os.listdir(stim_path)), 3, 224, 224), dtype=np.uint8)
#    for idx,pic_name in enumerate(os.listdir(stim_path)):
#        pic_path = pjoin(stim_path, pic_name)
#        pic = plt.imread(pic_path).astype(np.uint8)
コード例 #2
0
if net == 'AlexNet':
    layer_name = [
        'conv1' + pf, 'conv2' + pf, 'conv3' + pf, 'conv4' + pf, 'conv5' + pf,
        'fc1' + pf, 'fc2' + pf
    ]
elif net == 'Vgg11':
    layer_name = [
        'conv1' + pf, 'conv2' + pf, 'conv3' + pf, 'conv4' + pf, 'conv5' + pf,
        'conv6' + pf, 'conv7' + pf, 'conv8' + pf, 'fc1' + pf, 'fc2' + pf
    ]

#%% Load DNN, stimuli and define ablated models
from dnnbrain.dnn import models as db_models  # used by eval

dnn = eval('db_models.{}()'.format(net))  # load DNN
stimuli = Stimulus()
stimuli.load(stim_path)  # load stimuli
dmask = gen_dmask(layer_name)  # generate DNN mask


# permut weights pretrained network for each layer, finest granularity purmutation
def permut_weight_model(dnn):
    new_dict = {}
    for key in dnn.model.state_dict().keys():
        if key.split('.')[-1] == 'weight':
            para = dnn.model.state_dict()[key]
            ori_shape = para.shape
            new_dict[key] = para.view(-1)[torch.randperm(
                *para.view(-1).size())].reshape(ori_shape)
        else:
            new_dict[key] = dnn.model.state_dict()[key]
コード例 #3
0
        # print preliminary selection number
        # print(class_name[:5], ' has ', len(filtered_class_path), ' images\n')

        # construct data dict
        data = dict()
        data['stimID'] = class_path
        # create .stim.csv
        stim_file = StimulusFile(file_path)
        # write .stim.csv
        stim_file.write('image', pjoin(data_path, train_fold), data,
                        class_index={'index': index, 'name': class_name})
        del class_path, label_number, class_name

        # generate activation
        # create stimulus
        stimuli = Stimulus()
        stimuli.load(file_path)
        # create mask for absolute value
        dmask = Mask()
        layer = 'fc'
        # activation value
        dmask.set(layer, channels='all')

        # extract activation
        dnn = Resnet152()
        activation = np.array(dnn.compute_activation(stimuli, dmask).get(layer)).astype(np.float32)
        activation = activation.squeeze()

        # # delete .stim.csv file
        # os.remove(file_path)
コード例 #4
0
#stim.load('sci_test_simple.stim.csv')
#act_fc1_relu = dnn.compute_activation(stim, mask).get(layer)
#np.save('act_fc1_relu_test_simple.npy', act_fc1_relu)
#
#layer = 'fc2_relu'
#chn = 'all'
#mask = Mask(layer, chn)
#stim = Stimulus()
#stim.load('sci_test_simple.stim.csv')
#act_fc2_relu = dnn.compute_activation(stim, mask).get(layer)
#np.save('act_fc2_relu_test_simple.npy', act_fc2_relu)

layer = 'fc3'
chn = 'all'
mask = Mask(layer, chn)
stim = Stimulus()
stim.load('sci_test_simple.stim.csv')
act_fc3 = dnn.compute_activation(stim, mask).get(layer)
np.save('act_fc3_test_simple.npy', act_fc3)

layer = 'fc1_relu'
chn = 'all'
mask = Mask(layer, chn)
stim = Stimulus()
stim.load('sci_test.stim.csv')
act_fc1_relu = dnn.compute_activation(stim, mask).get(layer)
np.save('act_fc1_relu_test.npy', act_fc1_relu)

layer = 'fc2_relu'
chn = 'all'
mask = Mask(layer, chn)
コード例 #5
0
import numpy as np
from dnnbrain.dnn.core import Stimulus, Mask
from dnnbrain.dnn import models as db_models

model = eval('db_models.{}()'.format('alexnet'))

stim = Stimulus()
stim.load(r"C:\Users\xuwei\Desktop\test\test.dmask.csv")

dmask = Mask(r"C:\Users\xuwei\Desktop\test\test.stim.csv")

activation = model.compute_activation(stim, dmask)

activation.layers

activation.get('conv5')