def plot_dreams(model_file, test_imagepath, itr_per_octave, step_size, max_octaves, octave_scale, layer_idx_of_interest, **kwargs): assert(model_file) runid = model_runid(model_file) nn_image_size = get_nn_image_size(model_file) # precalc octaves and tiles, as well as max batch size dsb = DreamStudyBuffer(test_imagepath, nn_image_size, max_octaves, octave_scale) max_nn_pass = len(dsb.octave_sizes) * itr_per_octave column = load_column(model_file, batch_size=dsb.max_batch_size, layer_idx_of_interest=layer_idx_of_interest, **kwargs) try: nn_pass = 0 for octave, octave_size in reversed(list(enumerate(dsb.octave_sizes))): for itr in range(itr_per_octave): next_batch = dsb.tile_source_into_batch( octave, column.ds.mean, column.ds.std ) column.x_buffer.set_value(lasagne.utils.floatX(next_batch), borrow=True) batch_updates = column.dream_batch() dsb.update_source( batch_updates, octave, step_size ) nn_pass += 1 if (nn_pass in set([0] + [int(i) for i in numpy.logspace(0,numpy.log10(max_nn_pass),10)])): name = 'data/dreams/%i_nnpass_%i_itr_%i_octave.png' % (nn_pass, itr, octave) print("saving %s" % name) scipy.misc.toimage(numpy.uint8(dsb.source)).save(name, "PNG") except KeyboardInterrupt: print "[ERROR] User terminated Dream Study" print "Done"
def plot_occluded_activations(model_file, test_path, patch_size, **kwargs): assert(model_file) runid = model_runid(model_file) column = load_column(model_file, **kwargs) os = OcclusionStudy(column.ds, patch_size) try: for path in file_iter(test_path): os.test_imagepath = path all_test_predictions, all_test_output = column.test(override_buffer=os.buffer_occluded_dataset, override_num_examples=os.num_patch_centers) os.accumulate_patches_into_heatmaps(all_test_output, runid) except KeyboardInterrupt: print "[ERROR] User terminated Occlusion Study" print "Done"
def plot_occluded_activations(model_file, test_path, patch_size, **kwargs): assert (model_file) runid = model_runid(model_file) column = load_column(model_file, **kwargs) os = OcclusionStudy(column.ds, patch_size) try: for path in file_iter(test_path): os.test_imagepath = path all_test_predictions, all_test_output = column.test( override_buffer=os.buffer_occluded_dataset, override_num_examples=os.num_patch_centers) os.accumulate_patches_into_heatmaps(all_test_output, runid) except KeyboardInterrupt: print "[ERROR] User terminated Occlusion Study" print "Done"
import sys from os import listdir, path import re import cPickle import numpy import natsort from my_code.predict import model_runid, save_prediction import pdb TEST_IMAGE_DIR = 'data/test/centered_crop/' # usage: if __name__ == '__main__': raw_output_files = sys.argv[1].split(',') raw_outputs = [cPickle.load(open(f)) for f in raw_output_files] avg = sum(raw_outputs) / float(len(raw_output_files)) runids = [model_runid(n) for n in raw_output_files] name = '+'.join(runids) labels = (avg > 0.5).sum(axis=1) img_names = numpy.array([ path.splitext(f)[0] for f in listdir(TEST_IMAGE_DIR) if re.search('\.(jpeg|png)', f) ]) save_prediction(name, natsort.natsorted(img_names), labels)
img = scipy.misc.toimage(filt[m][:,:,n]) else: img = scipy.misc.toimage(filt[n][m,:,:]) nd = int(filter_rows*filter_enlargement) new_size = nd, nd img_3x = img.resize(new_size, Image.NEAREST) out_img[i_px_start:i_px_end, j_px_start:j_px_end] = img_3x return out_img if __name__ == '__main__': model_file = sys.argv[1] if len(sys.argv) == 3: filter_shape = sys.argv[2] else: filter_shape = 'c01b' runid = model_runid(model_file) f = open(model_file) # discard first slot all_saves = [] while True: try: all_saves.append(cPickle.load(f)) except EOFError: break all_layers_flat = all_saves[-1] all_layers = zip(all_layers_flat[0::2], all_layers_flat[1::2]) conv_layers = [(W,b) for W,b in all_layers if len(W.shape) == 4] outpaths = [] for i,Wb in enumerate(conv_layers): W,b = Wb
import sys from os import listdir, path import re import cPickle import numpy import natsort from my_code.predict import model_runid, save_prediction import pdb TEST_IMAGE_DIR = 'data/test/centered_crop/' # usage: if __name__ == '__main__': raw_output_files = sys.argv[1].split(',') raw_outputs = [cPickle.load(open(f)) for f in raw_output_files] avg = sum(raw_outputs) / float(len(raw_output_files)) runids = [model_runid(n) for n in raw_output_files] name = '+'.join(runids) labels = (avg > 0.5).sum(axis=1) img_names = numpy.array([path.splitext(f)[0] for f in listdir(TEST_IMAGE_DIR) if re.search('\.(jpeg|png)', f)]) save_prediction(name, natsort.natsorted(img_names), labels)