# In[1]: import numpy as np from os.path import expanduser, join import os import json import theano import pyret.filtertools as ft from preprocessing import datagen, loadexpt from utils import rolling_window, mksavedir import h5py from scipy.stats import pearsonr import preprocessing # make save directory save_dir = mksavedir(prefix='Experiment STC') # # Load white noise data # In[2]: whitenoise_train = loadexpt(0, 'whitenoise', 'train', 40, roll=False) # In[5]: import os #f = h5py.File(os.path.join(preprocessing.datadirs['lane.local'], '15-10-07/whitenoise.h5'), 'r') f = h5py.File(os.path.join(preprocessing.datadirs['lenna'], '15-10-07/whitenoise.h5'), 'r')
res_constrained = minimize(model_separation, x0=initial_guess, constraints=constraint, method='COBYLA') optimal_stimulus = res_constrained.x constraint_violation = unit_norm_constraint(optimal_stimulus) temporal_kernel = optimal_stimulus[:40] spatial_profile = optimal_stimulus[40:] low_rank_optimal_stimulus = np.outer(temporal_kernel, spatial_profile) optimal_stimulus = low_rank_optimal_stimulus.reshape((1, 40, 50, 50)) ln_response = ln_model.predict(optimal_stimulus)[0][0] convnet_response = naturalscenes_model.predict(optimal_stimulus)[0][0] responses = np.array([ln_response, convnet_response]) ## SAVE RESULT ## save_dir = mksavedir(prefix='Maximal Differentiated Stimuli') f = h5py.File(join(save_dir, 'differentiated_stimuli.h5'), 'w') f.create_dataset('stimulus', data=optimal_stimulus) f.create_dataset('responses', data=responses) f.create_dataset('constraint', data=constraint_violation) f.close() spatial_profile, time = ft.decompose(optimal_stimulus[0]) fig_filename = 'differentiated_stimuli.png' plt.imshow(spatial_profile, interpolation='nearest') plt.grid('off') plt.savefig(join(save_dir, fig_filename))
def __init__(self, cell_index, stimulus_type, loss, optimizer, mean_adapt): """ Superclass for managing keras models Parameters ---------- cell_index : int stimulus_type : string Either 'naturalscene' or 'whitenoise' loss : string or object, optional The loss function to use. (Default: poisson_loss) See http://keras.io/objectives/ for more information optimizer : string or object The optimizer to use. (Default: sgd) See http://keras.io/optimizers/ for more information """ # compile the model with notify('Compiling'): self.model.compile(loss=loss, optimizer=optimizer) # save architecture as a json file self.savedir = mksavedir(prefix=str(self)) with notify('Saving architecture'): with open(join(self.savedir, 'architecture.json'), 'w') as f: f.write(self.model.to_json()) # function to write data to a CSV file self.save_csv = partial(tocsv, join(self.savedir, 'performance')) self.save_csv(['Epoch', 'Iteration', 'Training CC', 'Test CC']) # load experimental data self.stimulus_type = stimulus_type if str(self) == 'lstm': numTime = self.stim_shape[0] self.holdout = loadexpt(cell_index, self.stimulus_type, 'test', self.stim_shape[1], mean_adapt=mean_adapt) self.training = loadexpt(cell_index, self.stimulus_type, 'train', self.stim_shape[1], mean_adapt=mean_adapt) X_train = self.training.X y_train = self.training.y X_test = self.holdout.X y_test = self.holdout.y numTrain = (int(X_train.shape[0]/numTime))*numTime numTest = (int(X_test.shape[0]/numTime))*numTime X_train = X_train[:numTrain] y_train = y_train[:numTrain] X_test = X_test[:numTest] y_test = y_test[:numTest] X_train = np.reshape(X_train, (int(numTrain/numTime), numTime, self.stim_shape[1], self.stim_shape[2], self.stim_shape[3])) y_train = np.reshape(y_train, (int(numTrain/numTime), numTime, 1)) X_test = np.reshape(X_test, (int(numTest/numTime), numTime, self.stim_shape[1], self.stim_shape[2], self.stim_shape[3])) y_test = np.reshape(y_test, (int(numTest/numTime), numTime, 1)) self.training = Batch(X_train, y_train) self.holdout = Batch(X_test, y_test) else: self.holdout = loadexpt(cell_index, self.stimulus_type, 'test', self.stim_shape[0], mean_adapt=mean_adapt) self.training = loadexpt(cell_index, self.stimulus_type, 'train', self.stim_shape[0], mean_adapt=mean_adapt) # save model information to a markdown file if 'architecture' not in self.__dict__: self.architecture = 'No architecture information specified' metadata = ['# ' + str(self), '## ' + strftime('%B %d, %Y'), 'Started training on: ' + strftime('%I:%M:%S %p'), '### Architecture', self.architecture, '### Stimulus', 'Experiment 10-07-15', stimulus_type, 'Mean adaptation: ' + str(mean_adapt), 'Cell #{}'.format(cell_index), '### Optimization', str(loss), str(optimizer)] tomarkdown(join(self.savedir, 'README'), metadata)
initial_guess = zscore(np.random.randint(0, 2, 40 + 50*50).astype('float32')) res_constrained = minimize(model_separation, x0=initial_guess, constraints=constraint, method='COBYLA') optimal_stimulus = res_constrained.x constraint_violation = unit_norm_constraint(optimal_stimulus) temporal_kernel = optimal_stimulus[:40] spatial_profile = optimal_stimulus[40:] low_rank_optimal_stimulus = np.outer(temporal_kernel, spatial_profile) optimal_stimulus = low_rank_optimal_stimulus.reshape((1,40,50,50)) ln_response = ln_model.predict(optimal_stimulus)[0][0] convnet_response = naturalscenes_model.predict(optimal_stimulus)[0][0] responses = np.array([ln_response, convnet_response]) ## SAVE RESULT ## save_dir = mksavedir(prefix='Maximal Differentiated Stimuli') f = h5py.File(join(save_dir, 'differentiated_stimuli.h5'), 'w') f.create_dataset('stimulus', data=optimal_stimulus) f.create_dataset('responses', data=responses) f.create_dataset('constraint', data=constraint_violation) f.close() spatial_profile, time = ft.decompose(optimal_stimulus[0]) fig_filename = 'differentiated_stimuli.png' plt.imshow(spatial_profile, interpolation='nearest') plt.grid('off') plt.savefig(join(save_dir, fig_filename))
import numpy as np from os.path import expanduser, join import os import json import theano import pyret.filtertools as ft import pyret.visualizations as pyviz from preprocessing import datagen, loadexpt from utils import rolling_window from keras.models import model_from_json import h5py import matplotlib.pyplot as plt from utils import mksavedir save_dir = mksavedir(prefix='Contrast Steps') # GET LN AND CNN RESPONSES TO CONTRAST STEPS architecture_filename = 'architecture.json' naturalscenes_data_dir = expanduser( '~/Dropbox/deep-retina/saved/lenna.salamander/2015-11-07 16.52.44 convnet/' ) naturalscenes_weight_filename = 'epoch038_iter02700_weights.h5' # .53 cc on held-out ln_data_dir = expanduser( '~/Dropbox/deep-retina/saved/lenna.nirum/2015-11-08 04.41.18 LN/') ln_weight_filename = 'epoch010_iter00750_weights.h5' # .468 cc on held-out # LOAD NATURAL SCENES MODEL naturalscenes_architecture_data = open( naturalscenes_data_dir + architecture_filename, 'r') naturalscenes_architecture_string = naturalscenes_architecture_data.read() naturalscenes_model = model_from_json(naturalscenes_architecture_string)
def __init__(self, cell_index, stimulus_type, loss, optimizer, mean_adapt): """ Superclass for managing keras models Parameters ---------- cell_index : int stimulus_type : string Either 'naturalscene' or 'whitenoise' loss : string or object, optional The loss function to use. (Default: poisson_loss) See http://keras.io/objectives/ for more information optimizer : string or object The optimizer to use. (Default: sgd) See http://keras.io/optimizers/ for more information """ # compile the model with notify('Compiling'): self.model.compile(loss=loss, optimizer=optimizer) # save architecture as a json file self.savedir = mksavedir(prefix=str(self)) with notify('Saving architecture'): with open(join(self.savedir, 'architecture.json'), 'w') as f: f.write(self.model.to_json()) # function to write data to a CSV file self.save_csv = partial(tocsv, join(self.savedir, 'performance')) self.save_csv(['Epoch', 'Iteration', 'Training CC', 'Test CC']) # load experimental data self.stimulus_type = stimulus_type if str(self) == 'lstm': numTime = self.stim_shape[0] self.holdout = loadexpt(cell_index, self.stimulus_type, 'test', self.stim_shape[1], mean_adapt=mean_adapt) self.training = loadexpt(cell_index, self.stimulus_type, 'train', self.stim_shape[1], mean_adapt=mean_adapt) X_train = self.training.X y_train = self.training.y X_test = self.holdout.X y_test = self.holdout.y numTrain = (int(X_train.shape[0] / numTime)) * numTime numTest = (int(X_test.shape[0] / numTime)) * numTime X_train = X_train[:numTrain] y_train = y_train[:numTrain] X_test = X_test[:numTest] y_test = y_test[:numTest] X_train = np.reshape( X_train, (int(numTrain / numTime), numTime, self.stim_shape[1], self.stim_shape[2], self.stim_shape[3])) y_train = np.reshape(y_train, (int(numTrain / numTime), numTime, 1)) X_test = np.reshape( X_test, (int(numTest / numTime), numTime, self.stim_shape[1], self.stim_shape[2], self.stim_shape[3])) y_test = np.reshape(y_test, (int(numTest / numTime), numTime, 1)) self.training = Batch(X_train, y_train) self.holdout = Batch(X_test, y_test) else: self.holdout = loadexpt(cell_index, self.stimulus_type, 'test', self.stim_shape[0], mean_adapt=mean_adapt) self.training = loadexpt(cell_index, self.stimulus_type, 'train', self.stim_shape[0], mean_adapt=mean_adapt) # save model information to a markdown file if 'architecture' not in self.__dict__: self.architecture = 'No architecture information specified' metadata = [ '# ' + str(self), '## ' + strftime('%B %d, %Y'), 'Started training on: ' + strftime('%I:%M:%S %p'), '### Architecture', self.architecture, '### Stimulus', 'Experiment 10-07-15', stimulus_type, 'Mean adaptation: ' + str(mean_adapt), 'Cell #{}'.format(cell_index), '### Optimization', str(loss), str(optimizer) ] tomarkdown(join(self.savedir, 'README'), metadata)
import numpy as np from os.path import expanduser, join import os import json import theano import pyret.filtertools as ft import pyret.visualizations as pyviz from preprocessing import datagen, loadexpt from utils import rolling_window from keras.models import model_from_json import h5py import matplotlib.pyplot as plt from utils import mksavedir save_dir = mksavedir(prefix="Contrast Steps") # GET LN AND CNN RESPONSES TO CONTRAST STEPS architecture_filename = "architecture.json" naturalscenes_data_dir = expanduser("~/Dropbox/deep-retina/saved/lenna.salamander/2015-11-07 16.52.44 convnet/") naturalscenes_weight_filename = "epoch038_iter02700_weights.h5" # .53 cc on held-out ln_data_dir = expanduser("~/Dropbox/deep-retina/saved/lenna.nirum/2015-11-08 04.41.18 LN/") ln_weight_filename = "epoch010_iter00750_weights.h5" # .468 cc on held-out # LOAD NATURAL SCENES MODEL naturalscenes_architecture_data = open(naturalscenes_data_dir + architecture_filename, "r") naturalscenes_architecture_string = naturalscenes_architecture_data.read() naturalscenes_model = model_from_json(naturalscenes_architecture_string) naturalscenes_model.load_weights(naturalscenes_data_dir + naturalscenes_weight_filename) # LOAD LN MODEL ln_architecture_data = open(ln_data_dir + architecture_filename, "r")