Exemple #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)
"""
Created on Thu Mar 26 01:33:24 2020

@author: xuezhichao
"""

import numpy as np
from PIL import Image
from dnnbrain.dnn.models import AlexNet
from dnnbrain.dnn.core import Mask
from os.path import join as pjoin
import matplotlib.pyplot as plt
import os
from dnnbrain.dnn.core import Stimulus

dnn = AlexNet()

#layer = 'fc1_relu'
#chn = 'all'
#mask = Mask(layer, chn)
#stim = Stimulus()
#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)
Exemple #3
0
Created on Mon Apr 20 11:49:40 2020

@author: 贺琦琦
"""

import os, random
from os.path import join as pjoin
from PIL import Image
import numpy as np
from dnnbrain.dnn.base import ip
from dnnbrain.dnn.models import AlexNet
from dnnbrain.dnn.algo import GuidedSaliencyImage
import matplotlib.pyplot as plt

# prepare parameters
dnn = AlexNet()
stim_main_path = '/nfs/e3/ImgDatabase/ImageNet_2017/ILSVRC2017_DET/ILSVRC/Data/DET/train/ILSVRC2013_train/'
out_sal_path = '/nfs/s2/userhome/heqiqi/workingdir/trials/saliency_libresult_fc3/'
out_pos_path = '/nfs/s2/userhome/heqiqi/workingdir/trials/extra_10kinds/'
ratio_std = 0.5
ratio_percent = 0.7
i = 0
m = 0
type_num = 20
pic_num = 1000

# generate random typelist
typedir = os.listdir(stim_main_path)
typelist = []
for pictype in typedir:
    tmp = len(os.listdir(pjoin(stim_main_path, pictype))) > pic_num
Exemple #4
0
from os.path import join as pjoin

# load pic to compute activation
main_path = '/nfs/s2/userhome/zhouming/workingdir/numerosity/'
model_path = pjoin(main_path, 'out/model/alexnet_train/BS_128_LR_0.001')
out_path = pjoin(main_path, 'out/activation/weber_develop')
stim_path = pjoin(main_path, 'stim/csv/num_discrim.stim.csv')
mask_path = pjoin(main_path, 'stim/csv/sig80_numerosity.dmask.csv')

stim = Stimulus()
stim.load(stim_path)
dmask = Mask()
dmask.load(mask_path)
layer_all = ['fc1_relu', 'fc2_relu']
chn = 'all'
models = os.listdir(model_path)
models.sort(key=lambda x: int(x.split('_')[1]))

#%% start computing
for model in models:
    epoch = model.split('_')[1]
    dnn = AlexNet(pretrained=False)
    dnn.model.load_state_dict(
        torch.load(pjoin(model_path, model))['state_dict'])
    act_all = dnn.compute_activation(stim, dmask, cuda='GPU')
    for layer in layer_all:
        act = act_all.get(layer)
        np.save(pjoin(out_path, f'act_weber_epoch{epoch}_{layer}.npy'), act)
        del act
    print(f'Finish computing epoch{epoch}!')