def debug_whole_image_classification(self, img, normMean=None, norm_std=None): start_time = time.clock() row_range = 1 img = normalizeImage(img) imSize = np.shape(img) membraneProbabilities = np.zeros(np.shape(img)) patchSize = self.patchSize data_shared = shared_single_dataset(np.zeros((imSize[0]*row_range,patchSize**2)), borrow=True) classify = theano.function( [], self.debug_x, givens={x: data_shared} ) for row in xrange(0,33,row_range): if row%100 == 0: print row data = generate_patch_data_rows(img, rowOffset=row, rowRange=row_range, patchSize=patchSize, imSize=imSize, data_mean=normMean, data_std=norm_std) data_shared.set_value(np.float32(data)) result = classify() #membraneProbabilities[row,:] = result[:,1] membraneProbabilities = result end_time = time.clock() total_time = (end_time - start_time) print "Image classification took %f seconds" % (total_time) return np.array(membraneProbabilities)
def classify_image(self, img, normMean=None, norm_std=None): start_time = time.clock() row_range = 1 img = normalizeImage(img) imSize = np.shape(img) membraneProbabilities = np.zeros(np.shape(img)) patchSize = np.int(np.sqrt(self.hiddenLayers[0].W.eval().shape[0])) data_shared = shared_single_dataset(np.zeros((imSize[0]*row_range,patchSize**2)), borrow=True) classify = theano.function( [], self.p_y_given_x, givens={x: data_shared} ) for row in xrange(0,1024,row_range): if row%100 == 0: print row data = generate_patch_data_rows(img, rowOffset=row, rowRange=row_range, patchSize=patchSize, imSize=imSize, data_mean=normMean, data_std=norm_std) data_shared.set_value(np.float32(data)) result = classify() membraneProbabilities[row,:] = result[:,1] end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) return np.array(membraneProbabilities)
def predict_image(self, x, img, normMean=None, norm_std=None): start_time = time.clock() row_range = 1 img = normalizeImage(img) imSize = np.shape(img) membraneProbabilities = np.zeros(1024*1024, dtype=int ) patchSize = np.int(np.sqrt(self.hiddenLayers[0].W.eval().shape[0])) data_shared = shared_single_dataset(np.zeros((imSize[0]*row_range,patchSize**2)), borrow=True) classify = theano.function( [], self.logRegressionLayer.y_pred, givens={x: data_shared} ) for row in xrange(0,1024,row_range): if row%100 == 0: print row data = generate_patch_data_rows(img, rowOffset=row, rowRange=row_range, patchSize=patchSize, imSize=imSize, data_mean=normMean, data_std=norm_std) data_shared.set_value(np.float32(data)) membraneProbabilities[row*1024:row*1024+row_range*1024] = classify() end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) return np.array(membraneProbabilities)
def classify_image(self, img, normMean=None, norm_std=None): data_type = T.matrix('data') start_time = time.clock() row_range = 1 img = normalizeImage(img) imSize = np.shape(img) membraneProbabilities = np.zeros(np.shape(img)) patchSize = np.int(np.sqrt(self.hiddenLayers[0].W.eval().shape[0])) def applyNetwork(data): for hl in self.hiddenLayers: data = hl.get_output(data) return self.get_p_y_given_x(data) data_shared = theano.shared(np.float32(np.zeros((imSize[0]*row_range,patchSize**2)))) #classify = theano.function(inputs=[data_type], outputs=applyNetwork(data_type)) classify = theano.function(inputs=[], outputs=applyNetwork(data_type), givens={data_type: data_shared}, on_unused_input='ignore') for row in xrange(0,1024,row_range): if row%100 == 0: print row data = generate_patch_data_rows(img, rowOffset=row, rowRange=row_range, patchSize=patchSize, imSize=imSize, data_mean=normMean, data_std=norm_std) data_shared.set_value(np.float32(data)) result = classify() #classify(data) membraneProbabilities[row,:] = result[:,1] end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) return np.array(membraneProbabilities)
def classifyImage_aniso_MLP_iae(imageName, classifier, iae_layer=None, data_mean=None, data_std=None, doThresh=False, iae=None, yz=True): start_time = time.clock() img = mahotas.imread(imageName) #img = np.float32(np.array(img)) #img = img - img.min() #img = img / img.max() img = normalizeImage(img) if not yz: img = np.rot90(img,3) imSize = np.shape(img) print imSize membraneProbabilities = [] patchSize = np.int(np.sqrt(classifier.n_visible)) data_type = T.matrix('data') get_first_layer_code = classifier.get_layer_output_function(data=data_type, layerNumber=0) if not iae == None: applyIAE = iae.buildClassification_function(data=data_type) classify_from_code = classifier.classify_from_code(code=data_type, layerNumber=1) #for row in xrange(0,np.shape(img)[0],5): for row in xrange(0,170,5): if row%100 == 0: print row data, middlePixel = generate_patch_data_rows(image=img, data_mean=data_mean, data_std=data_std, rowOffset=row, rowRange=1, patchSize=patchSize, imSize=imSize) intermediate_code = get_first_layer_code(data) if not iae==None: corrected_code = applyIAE(intermediate_code) result = classify_from_code(corrected_code) else: result = classify_from_code(intermediate_code) result = result[:,1] if doThresh: result[middlePixel>0.7]=0.0 membraneProbabilities.append(result) print np.shape(membraneProbabilities) end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) membraneProbabilities = np.array(membraneProbabilities) if not yz: membraneProbabilities = np.rot90(membraneProbabilities,1) return membraneProbabilities
def classifyImage_aniso(imageName, network_layerList, yz=True): def applyNetwork(data): for da in network_layerList[:-1]: data = da.get_hidden_values(data) p_y_given_x = network_layerList[-1].get_p_y_given_x(data) return p_y_given_x start_time = time.clock() img = mahotas.imread(imageName) #img = np.float32(np.array(img)) #img = img - img.min() #img = img / img.max() img = normalizeImage(img) if not yz: img = np.rot90(img, 1) imSize = np.shape(img) print imSize membraneProbabilities = [] patchSize = np.int(np.sqrt(network_layerList[0].n_visible)) data_type = T.matrix('data') classifiedData = applyNetwork(data_type) classify = theano.function(inputs=[data_type], outputs=classifiedData) for row in xrange(0, np.shape(img)[0], 5): if row % 100 == 0: print row data = generate_patch_data_rows(img, rowOffset=row, rowRange=1, patchSize=patchSize, imSize=imSize) result = classify(data) membraneProbabilities.append(result[:, 1]) print np.shape(membraneProbabilities) end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) membraneProbabilities = np.array(membraneProbabilities) if not yz: membraneProbabilities = np.rot90(membraneProbabilities, 3) return membraneProbabilities
def classifyImage_aniso(imageName, network_layerList, yz=True): def applyNetwork(data): for da in network_layerList[:-1]: data = da.get_hidden_values(data) p_y_given_x = network_layerList[-1].get_p_y_given_x(data) return p_y_given_x start_time = time.clock() img = mahotas.imread(imageName) #img = np.float32(np.array(img)) #img = img - img.min() #img = img / img.max() img = normalizeImage(img) if not yz: img = np.rot90(img,1) imSize = np.shape(img) print imSize membraneProbabilities = [] patchSize = np.int(np.sqrt(network_layerList[0].n_visible)) data_type = T.matrix('data') classifiedData = applyNetwork(data_type) classify = theano.function(inputs=[data_type], outputs=classifiedData) for row in xrange(0,np.shape(img)[0],5): if row%100 == 0: print row data = generate_patch_data_rows(img, rowOffset=row, rowRange=1, patchSize=patchSize, imSize=imSize) result = classify(data) membraneProbabilities.append(result[:,1]) print np.shape(membraneProbabilities) end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) membraneProbabilities = np.array(membraneProbabilities) if not yz: membraneProbabilities = np.rot90(membraneProbabilities,3) return membraneProbabilities
def classifyImage_MLP(imageName, classifier, data_mean=None, data_std=None, doThresh=False): start_time = time.clock() row_range = 1 img = mahotas.imread(imageName) img = normalizeImage(img) if doThresh: mask = img >= 0.7 imSize = np.shape(img) membraneProbabilities = [] patchSize = np.int(np.sqrt(classifier.n_visible)) data_type = T.matrix('data') classify = classifier.buildClassification_function(data_type) for row in xrange(0, 1024, row_range): if row % 100 == 0: print row data, middlePixel = generate_patch_data_rows(img, data_mean, data_std, rowOffset=row, rowRange=row_range, patchSize=patchSize, imSize=imSize) result = classify(data) result = result[:, 1] if doThresh: result[middlePixel > 0.7] = 0.0 membraneProbabilities.append(result) end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) if doThresh: membraneProbabilities = np.array(membraneProbabilities) membraneProbabilities[mask > 0] = np.min(membraneProbabilities) return np.array(membraneProbabilities)
def classifyImage_MLP_iae(imageName, classifier, iae): start_time = time.clock() row_range = 1 img = mahotas.imread(imageName) #img = np.float32(img) #img = img - img.min() #img = img / img.max() img = normalizeImage(img) imSize = np.shape(img) membraneProbabilities = [] patchSize = np.int(np.sqrt(classifier.n_visible)) data_type = T.matrix('data') get_first_layer_code = classifier.get_layer_output_function(data=data_type, layerNumber=0) applyIAE = iae.buildClassification_function(data=data_type) classify_from_code = classifier.classify_from_code(code=data_type, layerNumber=1) for row in xrange(0, 1024, row_range): if row % 100 == 0: print row data = generate_patch_data_rows(img, rowOffset=row, rowRange=row_range, patchSize=patchSize, imSize=imSize) intermediate_code = get_first_layer_code(data) corrected_code = applyIAE(intermediate_code) result = classify_from_code(corrected_code) membraneProbabilities.append(result[:, 1]) end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) return np.array(membraneProbabilities)
def classifyImage(imageName, network_layerList): def applyNetwork(data): for da in network_layerList[:-1]: data = da.get_hidden_values(data) p_y_given_x = network_layerList[-1].get_p_y_given_x(data) return p_y_given_x start_time = time.clock() row_range = 1 img = mahotas.imread(imageName) img = normalizeImage(img) imSize = np.shape(img) membraneProbabilities = [] patchSize = np.int(np.sqrt(network_layerList[0].n_visible)) data_type = T.matrix('data') classifiedData = applyNetwork(data_type) classify = theano.function(inputs=[data_type], outputs=classifiedData) for row in xrange(0, 1024, row_range): if row % 100 == 0: print row data = generate_patch_data_rows(img, rowOffset=row, rowRange=row_range, patchSize=patchSize, imSize=imSize) result = classify(data) membraneProbabilities.append(result[:, 1]) end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) return np.array(membraneProbabilities)
def classifyImage_MLP(imageName, classifier, data_mean=None, data_std=None, doThresh=False): start_time = time.clock() row_range = 1 img = mahotas.imread(imageName) img = normalizeImage(img) if doThresh: mask = img >= 0.7 imSize = np.shape(img) membraneProbabilities = [] patchSize = np.int(np.sqrt(classifier.n_visible)) data_type = T.matrix('data') classify = classifier.buildClassification_function(data_type) for row in xrange(0,1024,row_range): if row%100 == 0: print row data, middlePixel = generate_patch_data_rows(img, data_mean, data_std, rowOffset=row, rowRange=row_range, patchSize=patchSize, imSize=imSize) result = classify(data) result = result[:,1] if doThresh: result[middlePixel>0.7]=0.0 membraneProbabilities.append(result) end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) if doThresh: membraneProbabilities = np.array(membraneProbabilities) membraneProbabilities[mask>0] = np.min(membraneProbabilities) return np.array(membraneProbabilities)
def classifyImage(imageName, network_layerList): def applyNetwork(data): for da in network_layerList[:-1]: data = da.get_hidden_values(data) p_y_given_x = network_layerList[-1].get_p_y_given_x(data) return p_y_given_x start_time = time.clock() row_range = 1 img = mahotas.imread(imageName) img = normalizeImage(img) imSize = np.shape(img) membraneProbabilities = [] patchSize = np.int(np.sqrt(network_layerList[0].n_visible)) data_type = T.matrix('data') classifiedData = applyNetwork(data_type) classify = theano.function(inputs=[data_type], outputs=classifiedData) for row in xrange(0,1024,row_range): if row%100 == 0: print row data = generate_patch_data_rows(img, rowOffset=row, rowRange=row_range, patchSize=patchSize, imSize=imSize) result = classify(data) membraneProbabilities.append(result[:,1]) end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) return np.array(membraneProbabilities)
def classifyImage_MLP_iae(imageName, classifier, iae): start_time = time.clock() row_range = 1 img = mahotas.imread(imageName) #img = np.float32(img) #img = img - img.min() #img = img / img.max() img = normalizeImage(img) imSize = np.shape(img) membraneProbabilities = [] patchSize = np.int(np.sqrt(classifier.n_visible)) data_type = T.matrix('data') get_first_layer_code = classifier.get_layer_output_function(data=data_type, layerNumber=0) applyIAE = iae.buildClassification_function(data=data_type) classify_from_code = classifier.classify_from_code(code=data_type, layerNumber=1) for row in xrange(0,1024,row_range): if row%100 == 0: print row data = generate_patch_data_rows(img, rowOffset=row, rowRange=row_range, patchSize=patchSize, imSize=imSize) intermediate_code = get_first_layer_code(data) corrected_code = applyIAE(intermediate_code) result = classify_from_code(corrected_code) membraneProbabilities.append(result[:,1]) end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) return np.array(membraneProbabilities)
return output.reshape((2, output_shape[2], output_shape[3])) if __name__ == '__main__': rng = numpy.random.RandomState(929292) import mahotas import matplotlib.pyplot as plt #CPU #image = mahotas.imread('ac3_input_0141.tif') image = mahotas.imread('test-input-1.tif') imageSize = 1024 image = image[0:imageSize, 0:imageSize] start_time = time.clock() image = normalizeImage(image) - 0.5 #GPU image_shared = theano.shared( np.float64(image)) #theano.shared(np.float64(image)) image_shared = image_shared.reshape((1, 1, imageSize, imageSize)) x = T.matrix('x') classifier = CNN(input=x, batch_size=imageSize, patchSize=65, rng=rng, nkerns=[48, 48, 48], kernelSizes=[5, 5, 5], hiddenSizes=[200],
def classifyImage_aniso_MLP_iae(imageName, classifier, iae_layer=None, data_mean=None, data_std=None, doThresh=False, iae=None, yz=True): start_time = time.clock() img = mahotas.imread(imageName) #img = np.float32(np.array(img)) #img = img - img.min() #img = img / img.max() img = normalizeImage(img) if not yz: img = np.rot90(img, 3) imSize = np.shape(img) print imSize membraneProbabilities = [] patchSize = np.int(np.sqrt(classifier.n_visible)) data_type = T.matrix('data') get_first_layer_code = classifier.get_layer_output_function(data=data_type, layerNumber=0) if not iae == None: applyIAE = iae.buildClassification_function(data=data_type) classify_from_code = classifier.classify_from_code(code=data_type, layerNumber=1) #for row in xrange(0,np.shape(img)[0],5): for row in xrange(0, 170, 5): if row % 100 == 0: print row data, middlePixel = generate_patch_data_rows(image=img, data_mean=data_mean, data_std=data_std, rowOffset=row, rowRange=1, patchSize=patchSize, imSize=imSize) intermediate_code = get_first_layer_code(data) if not iae == None: corrected_code = applyIAE(intermediate_code) result = classify_from_code(corrected_code) else: result = classify_from_code(intermediate_code) result = result[:, 1] if doThresh: result[middlePixel > 0.7] = 0.0 membraneProbabilities.append(result) print np.shape(membraneProbabilities) end_time = time.clock() total_time = (end_time - start_time) print >> sys.stderr, ('Running time: ' + '%.2fm' % (total_time / 60.)) membraneProbabilities = np.array(membraneProbabilities) if not yz: membraneProbabilities = np.rot90(membraneProbabilities, 1) return membraneProbabilities