def choose_samples(indices=[0, 3, 6, 16]): import plankton_vis1 samples = plankton_vis1.loadSamplePlanktons(20) # 100,1,28,28 if False: for i, sample in enumerate(samples): print 'Showing sample ', i plt.imshow(sample[0], cmap=cm.Greys_r) plt.show() return samples[indices]
def choose_samples(indices=[0,3,6,16]): import plankton_vis1 samples = plankton_vis1.loadSamplePlanktons(20) # 100,1,28,28 if False: for i, sample in enumerate(samples): print 'Showing sample ',i plt.imshow(sample[0], cmap = cm.Greys_r) plt.show() return samples[indices]
def Find_plankton(model_name="plankton_conv_visualize_model.pkl.params"): """ Find plankton that activates the given layers most """ which_layer = 2 import plankton_vis1 samples = plankton_vis1.loadSamplePlanktons(numSamples=3000) print 'Dimension of Samples', np.shape(samples) Net = DeConvNet(model_name) #kernel_list = [ 2,23,60,12,45,9 ] kernel_list = range(48,64) num_of_maximum = 9 Heaps = findmaxactivation( Net, samples, num_of_maximum, kernel_list, which_layer=which_layer) bigbigmap = None # what is this? for kernel_index in Heaps: print 'dealing with',kernel_index,'th kernel' heap = Heaps[kernel_index] this_sams = [] this_Deconv = [] for pairs in heap: this_sam = pairs.sam this_sams.append( this_sam.reshape([NUM_C,MAX_PIXEL,MAX_PIXEL]) ) this_Deconv.append( Net.DeConv( this_sam, kernel_index, which_layer=which_layer ).reshape([NUM_C,MAX_PIXEL,MAX_PIXEL]) ) this_sams = np.array( this_sams ) this_sams = np.transpose( this_sams, [ 1, 0, 2, 3 ]) this_sams = this_sams.reshape( [ NUM_C, 9, MAX_PIXEL*MAX_PIXEL ]) this_sams = tuple( [ this_sams[0] for i in xrange(3)] + [None] ) this_Deconv = np.array( this_Deconv ) this_Deconv = np.transpose( this_Deconv, [ 1, 0, 2, 3 ]) this_Deconv = this_Deconv.reshape( [ NUM_C, num_of_maximum, MAX_PIXEL*MAX_PIXEL ]) this_Deconv = tuple( [ this_Deconv[0] for i in xrange(3)] + [None] ) this_map = tile_raster_images( this_sams, img_shape = (MAX_PIXEL,MAX_PIXEL), tile_shape = (3,3), tile_spacing=(1, 1), scale_rows_to_unit_interval=True, output_pixel_vals=True) this_Deconv = tile_raster_images( this_Deconv, img_shape = (MAX_PIXEL,MAX_PIXEL), tile_shape = (3,3), tile_spacing=(1, 1), scale_rows_to_unit_interval=True, output_pixel_vals=True) this_pairmap = np.append( this_map, this_Deconv, axis = 0) if bigbigmap == None: bigbigmap = this_pairmap segment_line = 255*np.ones([bigbigmap.shape[0],1,4],dtype='uint8') else: bigbigmap = np.append(bigbigmap, segment_line, axis = 1) bigbigmap = np.append(bigbigmap, this_pairmap, axis = 1) plt.imshow(bigbigmap) plt.show()
def main(model_name="plankton_model0.pkl.params", rotate=False, bScalar=True, which_layer=2, numSamples='All'): import plankton_vis1 samples = plankton_vis1.loadSamplePlanktons(numSamples, rotate) print 'Dimension of Samples', np.shape(samples) activations = findActivations(model_name, samples, which_layer, allFeats=False, bScalar=True) y_labels = plankton_utils.getY_label_int(which_data='train') activation_dict = separateActivationsByClass(activations, y_labels) calculateAverageActivations(activation_dict, which_layer) plotFeatures(activation_dict, which_layer)
def displayClassSamples(saveIndividualSamples=False): # from bc01 to cb01 samples = loadSamplePlanktons(30000, rotate=False) print 'samples shape', samples.shape #e.g. (30000,1,28,28) y_labels = plankton_utils.getY_label_int(which_data='train')[:30000] # construct list of indices for each label class_indices = [None]*121 for _index, y_label in enumerate(y_labels): #print "label %d _index = %d" % (y_label, _index) if class_indices[y_label] == None: class_indices[y_label] = [_index] else: class_indices[y_label].append(_index); #print class_indices[y_label] for i, this_class_indices in enumerate(class_indices): if len(this_class_indices) < 4: print 'Warning! Increase sample size! (Found less than 4 samples for class ', i return if saveIndividualSamples: ''' Saving sample image for each class ''' #print 'this_class_indices', this_class_indices shape = samples[this_class_indices[0]].shape #print 'shape of sample', shape #e.g. (1,28,28) for class_num, this_class_indices in enumerate(class_indices): tile4 = None for i in range(2): tile2 = None for j in range(2): if tile2 == None: tile2 = np.reshape(samples[this_class_indices[2*i+j]], (shape[1],shape[2])) else: tile2 = np.concatenate( (tile2, np.reshape(samples[this_class_indices[2*i+j]], (shape[1],shape[2])) ), axis=0) if tile4 == None: tile4 = tile2 else: tile4 = np.concatenate( (tile4, tile2), axis=1) print 'class_num', class_num ,'shape of tile4', tile4.shape fname = 'results/classSamples_grey/' + str(class_num) + ".png" if False: from PIL import Image newIm = 255.*(tile4)/2. + 127. im = Image.fromarray(newIm) im = im.convert('RGB') print 'Saving class num=', class_num im.save(fname) else: plt.imshow(newIm, cmap = cm.Greys_r) plt.axis('off') plt.show() plt.savefig(fname) bigbigmap = None for row in range(11): bigmap = None for col in range(11): class_index = 11*row+col print 'Class Index', class_index print "Indices with this class", class_indices[class_index][:4] class_samples = samples[class_indices[class_index][:4]] print "class_samples shape", class_samples.shape ''' for pairs in heap: this_sam = pairs.sam this_sams.append( this_sam.reshape([NUM_C,MAX_PIXEL,MAX_PIXEL]) ) this_Deconv.append( Net.DeConv( this_sam, kernel_index, which_layer=which_layer ).reshape([NUM_C,MAX_PIXEL,MAX_PIXEL]) ) ''' this_sams = np.transpose( class_samples, [ 1, 0, 2, 3 ]) this_sams = this_sams.reshape( [ NUM_C, 4, MAX_PIXEL*MAX_PIXEL ]) this_sams = tuple( [ this_sams[0] for i in xrange(3)] + [None] ) this_map = tile_raster_images( class_samples, img_shape = (MAX_PIXEL,MAX_PIXEL), tile_shape = (2,2), tile_spacing=(1, 1), scale_rows_to_unit_interval=True, output_pixel_vals=True) if bigmap == None: bigmap = this_map segment_line = 255*np.ones([bigmap.shape[0],1],dtype='uint8') print 'this_map dimension', this_map.shape print 'segment_line dimension', segment_line.shape else: bigmap = np.append(bigmap, segment_line, axis = 1) bigmap = np.append(bigmap, this_map, axis = 1) if bigbigmap == None: bigbigmap = bigmap else: bigbigmap = np.append(bigbigmap, 255*np.ones((1, bigmap.shape[1]), dtype='uint8'), axis=0) bigbigmap = np.append(bigbigmap, bigmap, axis=0) print 'shape of big big map', bigbigmap.shape plt.imshow(bigbigmap,cmap = cm.Greys_r) plt.xticks(np.arange(11)) plt.yticks(np.arange(11)) plt.show()