def __init__(self): self.filepath = os.path.join(os.path.dirname(__file__), '..') self.module_name = 'lif_convnet' # --- LIF vision network weights configurations --- self.vision_network_filename = \ os.path.join(self.filepath, self.module_name, 'params.pkl') self.vision_network_data = \ load_model_pickle(self.vision_network_filename) self.mem_okey = 'fc1' self.classify_okey = 'fc10' # --- Visual associative memory configurations --- centers_filename = \ os.path.join(self.filepath, self.module_name, 'class_centers.npz') sps = np.load(centers_filename, encoding='latin1')['centers'] self.num_classes = sps.shape[0] self.sps_output_scale = 1.0 / 32.0 # For magic number 32.0, see reference_code/vision_3/data_analysis.py # (print np.mean(norms) for sp_2000) means_filename = \ os.path.join(self.filepath, self.module_name, 'fc10_class_means.npz') self.sps_fc10 = np.eye(self.num_classes) self.sps_fc10_means = np.load(means_filename, encoding='latin1')['means'] # For magic number 10.1, see reference_code/vision_3/data_analysis.py # (print np.mean(norms) for sp_10) # --- Mandatory data object attributes --- self.am_threshold = 0.5 self.sps = sps self.dimensions = self.sps.shape[1] self.sps_element_scale = 1.0 * self.sps_output_scale
# retrieve from https://figshare.com/s/cdde71007405eb11a88f filename = 'ilsvrc-2012-batches-test3.tar.gz' X_test, Y_test, data_mean, label_names = load_ilsvrc2012(filename, n_files=1) X_test = X_test.astype('float32') # crop data X_test = X_test[:, :, 16:-16, 16:-16] data_mean = data_mean[:, 16:-16, 16:-16] image_shape = X_test.shape[1:] # subtract mean X_test -= data_mean # retrieve from https://figshare.com/s/f343c68df647e675af28 cc_model = load_model_pickle('ilsvrc2012-lif-48.pkl') # --- Run model in Nengo presentation_time = 0.2 model = nengo.Network() with model: u = nengo.Node(nengo.processes.PresentInput(X_test, presentation_time)) ccnet = CudaConvnetNetwork(cc_model, synapse=nengo.synapses.Alpha(0.001)) nengo.Connection(u, ccnet.inputs['data'], synapse=None) # input_p = nengo.Probe(u) output_p = nengo.Probe(ccnet.output) # --- image display
import os from pathlib import Path import nengo_extras.data from nengo_extras.cuda_convnet import load_model_pickle REPO_LOCAL_PATH = Path(os.getcwd()) # layers, weights, etc. REPO_LOCAL_DATA_PATH = REPO_LOCAL_PATH.joinpath('data') VIS_NET_DATA_FNAME = 'ilsvrc2012-lif-48.pkl' VIS_NET_DATA_PATH = REPO_LOCAL_DATA_PATH.joinpath( 'neural_net_weights/{}'.format(VIS_NET_DATA_FNAME) ) VIS_NET_DATA_PATH = str(VIS_NET_DATA_PATH) VIS_NET_DATA_URL = 'https://ndownloader.figshare.com/files/5370917?private_link=f343c68df647e675af28' # download if not already VIS_NET_DATA_PATH = nengo_extras.data.get_file(filename=VIS_NET_DATA_PATH, url=VIS_NET_DATA_URL) VIS_NET_DATA = load_model_pickle(str(VIS_NET_DATA_PATH))
def __init__(self): self.filepath = os.path.join(os.path.dirname(__file__), '..') self.module_name = 'lif_imagenet' # --- LIF vision network weights configurations --- # retrieve from https://figshare.com/s/f343c68df647e675af28 self.vision_network_filename = \ os.path.join(self.filepath, self.module_name, 'params.pkl') # Try alternate params filename if can't locate default file if not os.path.exists(self.vision_network_filename): self.vision_network_filename = \ os.path.join(self.filepath, self.module_name, 'ilsvrc2012-lif-48.pkl') self.vision_network_data = \ load_model_pickle(self.vision_network_filename) # --- Spaun symbol associative memory configurations --- self.spaun_am_threshold = 0.5 self.spaun_am_input_bias = -9.0 self.spaun_am_input_scale = 10.0 centers_filename = \ os.path.join(self.filepath, self.module_name, 'spaun_sym_class_centers.npz') self.sps_spaun_sym = np.load(centers_filename, encoding='latin1')['class_centers'] self.spaun_sym_num_classes = self.sps_spaun_sym.shape[0] self.spaun_sym_out_dimensions = self.sps_spaun_sym.shape[1] # --- Imagenet associative memory configurations --- # self.imagenet_am_threshold = 0.5 # self.imagenet_am_threshold = 0.30 self.imagenet_am_threshold = 0.21 means_filename = \ os.path.join(self.filepath, self.module_name, 'class_means.npz') self.imagenet_sps_means = np.load(means_filename, encoding='latin1')['means'] self.imagenet_num_classes = self.imagenet_sps_means.shape[0] self.imagenet_out_dimensions = self.imagenet_num_classes self.sps_fc1000 = np.eye(self.imagenet_num_classes) # --- Combined configurations --- # Here, we are going to output layer 'pool2' to the spaun_sym AM # (output dimensions = self.spaun_sym_out_dimensions) + 'fc1000' to # imagenet AM (output_dimensions = self.imagenet_out_dimensions) self.spaun_sym_okey = 'pool2' self.output_dimensions = (self.spaun_sym_out_dimensions + self.imagenet_out_dimensions) # --- Mandatory data object attributes --- self.sps = np.zeros((self.spaun_sym_num_classes + # HACK: To avoid self.imagenet_num_classes, 10)) # creating vis_wm self.dimensions = self.sps.shape[1] # of 1000+ dims self.sps_element_scale = 1.0 self.am_threshold = 0.5
# crop data Xtest = Xtest[:, :, 16:-16, 16:-16] data_mean = data_mean[:, 16:-16, 16:-16] image_shape = Xtest.shape[1:] # subtract mean Xtest -= data_mean if 0: # shuffle images i = rng.permutation(Xtest.shape[0]) Xtest, Ytest = Xtest[i], Ytest[i] # retrieve from https://figshare.com/s/f343c68df647e675af28 cc_model = load_model_pickle('~/data/ilsvrc2012-lif-48.pkl') # --- Run model in Nengo presentation_time = 0.2 c0 = 0.14 # classification start time model = nengo.Network() with model: u = nengo.Node(nengo.processes.PresentInput(Xtest, presentation_time)) ccnet = CudaConvnetNetwork(cc_model, synapse=nengo.synapses.Alpha(0.003)) nengo.Connection(u, ccnet.input, synapse=None) # input_p = nengo.Probe(u) output_p = nengo.Probe(ccnet.output) spike_ps = [ nengo.Probe(ccnet.layers_by_name['conv1_neuron'].ensemble.neurons),
(X_train, y_train), (X_test, y_test), label_names = load_cifar10(label_names=True) X_train = X_train.reshape(-1, 3, 32, 32).astype('float32') X_test = X_test.reshape(-1, 3, 32, 32).astype('float32') # crop data X_train = X_train[:, :, 4:-4, 4:-4] X_test = X_test[:, :, 4:-4, 4:-4] # subtract mean data_mean = X_train.mean(axis=0) X_train -= data_mean X_test -= data_mean # retrieve from https://figshare.com/s/49741f9e2d0d29f68871 cc_model = load_model_pickle('cifar10-lif-1628.pkl') # --- Run model in Nengo presentation_time = 0.2 model = nengo.Network() with model: u = nengo.Node(nengo.processes.PresentInput(X_test, presentation_time)) ccnet = CudaConvnetNetwork(cc_model, synapse=nengo.synapses.Alpha(0.005)) nengo.Connection(u, ccnet.inputs['data'], synapse=None) input_p = nengo.Probe(u) output_p = nengo.Probe(ccnet.output) # --- image display image_shape = X_test.shape[1:]
# retrieve from https://figshare.com/s/cdde71007405eb11a88f filename = 'ilsvrc-2012-batches-test3.tar.gz' X_test, Y_test, data_mean, label_names = load_ilsvrc2012(filename, n_files=1) X_test = X_test.astype('float32') # crop data X_test = X_test[:, :, 16:-16, 16:-16] data_mean = data_mean[:, 16:-16, 16:-16] image_shape = X_test.shape[1:] # subtract mean X_test -= data_mean # retrieve from https://figshare.com/s/f343c68df647e675af28 cc_model = load_model_pickle('ilsvrc2012-lif-48.pkl') # --- Run model in Nengo presentation_time = 0.2 model = nengo.Network() with model: u = nengo.Node(nengo.processes.PresentInput(X_test, presentation_time)) ccnet = CudaConvnetNetwork(cc_model, synapse=nengo.synapses.Alpha(0.001)) nengo.Connection(u, ccnet.inputs['data'], synapse=None) # input_p = nengo.Probe(u) output_p = nengo.Probe(ccnet.output) # --- image display display_f = image_display_function(image_shape, scale=1., offset=data_mean)