if EC2: # Model Path CCLF_PATH = '/home/ec2-user/VizioMetrics/Model/Classifier/nClass_5_2015-03-09_CPR' DMTLER_PATH = '/home/ec2-user/VizioMetrics/Model/Dismantler/dismantler_matsplit_matsvm_ceil_latest' CPSD_PATH = '/home/ec2-user/VizioMetrics/Model/Dismantler/composite_detector_firelanemap' # Data Path KEYPATH = '/home/ec2-user/VizioMetrics/keys.txt' HOST = 'escience.washington.edu.viziometrics' else: # Model Path CCLF_PATH = '/Users/sephon/Desktop/Research/VizioMetrics/Model/Classifier/nClass_5_2015-03-09_CPR' DMTLER_PATH = '/Users/sephon/Desktop/Research/VizioMetrics/Model/Dismantler/dismantler_matsplit_matsvm_ceil_latest' CPSD_PATH = '/Users/sephon/Desktop/Research/VizioMetrics/Model/Dismantler/composite_detector_firelanemap' # Data Path KEYPATH = '/Users/sephon/Desktop/Research/VizioMetrics/keys.txt' HOST = 'escience.washington.edu.viziometrics' ## Global Object OPT_CCLF = Option(isClassify=True) OPT_DMTLER = Option_Dismantler(isTrain=False) OPT_CPSD = Option_CompositeDetector(isTrain=False) ## Boxes FD = FeatureDescriptor(CCLF_PATH) CCLF = SVMClassifier(OPT_CCLF, clfPath=CCLF_PATH) CIL = CloudImageLoader(OPT_CCLF, keyPath=KEYPATH, host=HOST) DMTLER = Dismantler(OPT_DMTLER, auxClfPath=DMTLER_PATH) CPSD = CompositeImageDetector(OPT_CPSD, modelPath=CPSD_PATH) print CPSD.Classifier.classifier.best_estimator_
def classifyLocalImagesWithDismantler(self, corpusPath = None): Opt_dmtler = Option_Dismantler(isTrain = False) Dmtler = Dismantler(Opt_dmtler) if corpusPath is None: corpusPath = self.Opt.classifyCorpusPath if self.Classifier is not None: print 'Start classifying images with dismantler in local disk...' startTime = time.time() nImageAll = 0 header = ['file_path', 'number_subimages', 'sub_class_name', 'sub_probability', 'segmentation', 'subimage_height', 'subimage_width'] csvSavingPath = self.Opt.resultPath csvFilename = 'class_result' Common.saveCSV(csvSavingPath, csvFilename, header = header, mode = 'wb', consoleOut = False) for dirPath, dirNames, fileNames in os.walk(corpusPath): for f in fileNames: fname, suffix = Common.getFileNameAndSuffix(f) if suffix in self.Opt.validImageFormat: filename = os.path.join(dirPath, f) # Load image img = ImageLoader.loadImageByPath(filename) # Dismantle image nodeList = Dmtler.dismantle(img) # Get number of sub-images if len(nodeList) > 0: numSubImages = len(nodeList) else: numSubImages = 1 # Remove surrounding empty space nodeList = Dmtler.updateImageToEffectiveAreaFromNodeList(img, nodeList, Opt_dmtler.thresholds) # Load all sub-images if len(nodeList) > 0: imData, imDims, dimSum = ImageLoader.loadSubImagesByNodeList(img, nodeList, self.Opt.finalDim, self.Opt.preserveAspectRatio) else: imData, imDims, dimSum = ImageLoader.loadImagesByList([filename], self.Opt.finalDim, self.Opt.preserveAspectRatio) # Extracting Features X = self.FeatureDescriptor.extractFeatures(imData, 1) # Classify y_pred, y_proba = self.Classifier.predict(X) sub_classes = '' sub_probs = '' sub_height = '' sub_width = '' segmentations = '' for i in range(0, len(nodeList)): if i != 0: sub_classes += ',' sub_probs += ',' sub_height += ',' sub_width += ',' segmentations += ',' sub_classes += str(y_pred[i]) sub_height += str(imDims[i][0]) sub_width += str(imDims[i][1]) sub_prob = '' for j in range(0, len(y_proba[i])): if j != 0: sub_prob += ':' sub_prob += str(y_proba[i][j]) sub_probs += sub_prob node = nodeList[i] segmentation = str(node.info['start'][0]) + ':' + \ str(node.info['start'][1]) + ':' + \ str(node.info['end'][0]) + ':' + \ str(node.info['end'][1]) segmentations += segmentation result = zip([filename], [numSubImages], [sub_classes], [sub_probs], [segmentations], [sub_height], [sub_width]) print result Common.saveCSV(csvSavingPath, csvFilename, result, mode = 'ab', consoleOut = False) nImageAll += 1 if np.mod(nImageAll, 100) == 0: print '%d images have been classified.' % nImageAll costTime = time.time() - startTime print 'All %d images were classified and saved in %s within %d sec.' % (nImageAll, os.path.join(csvSavingPath, csvFilename), costTime) else: print 'Classifier not loaded'
# Main program to train model import sys sys.path.append("..") from Dismantler.Dismantler import * from Dismantler.TrainCompositeDetector import * from Options import * from Models import * from DataManager import * from Dictionary import * Opt_Dmtler = Option_Dismantler(isTrain=False) Dmtler = Dismantler(Opt_Dmtler) Opt_CID = Option_CompositeDetector(isTrain=True) CID = CompositeImageDetector(Opt_CID) def getFeatureByFireLaneMapFromFileList(fileList, showStatus=True): data = np.zeros([len(fileList), 2 + CID.division[1] * CID.division[0]]) for (i, filename) in enumerate(fileList): img = cv.imread(filename) if len(img.shape) == 3: img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) first_vertical, fire_lane_map_vertical, count_standalone_first_vertical = Dmtler.split( img, 0) first_horizontal, fire_lane_map_horizontal, count_standalone_first_horizontal = Dmtler.split( img, 1) map = fire_lane_map_vertical + fire_lane_map_horizontal
def classifyCloudImagesWithDismantler(self, keyPath = None, host = None): Opt_dmtler = Option_Dismantler(isTrain = False) Dmtler = Dismantler(Opt_dmtler) if self.Classifier is not None: try: cImageLoader = CloudImageLoader(self.Opt, keyPath = keyPath, host = host) bucketList = cImageLoader.getBucketList() except: print 'Unable to connect cloud server' print 'Start classifying images on cloud server...' startTime = time.time() nImageAll = 0 header = ['file_path', 'number_subimages', 'sub_class_name', 'sub_probability', 'segmentation', 'subimage_height', 'subimage_width'] csvSavingPath = self.Opt.resultPath csvFilename = 'class_result' Common.saveCSV(csvSavingPath, csvFilename, header = header, mode = 'wb', consoleOut = False) for key in bucketList: isValidImage, suffix = cImageLoader.isKeyValidImageFormat(key) if isValidImage: # Get image from s3 img = CloudImageLoader.keyToValidImage(key) # Dismantle the image nodeList = Dmtler.dismantle(img) # Get number of sub-images if len(nodeList) > 0: numSubImages = len(nodeList) else: numSubImages = 1 # Remove surrounding empty space nodeList = Dmtler.updateImageToEffectiveAreaFromNodeList(img, nodeList, Opt_dmtler.thresholds) # Load all sub-images if len(nodeList) > 0: imData, imDims, dimSum = ImageLoader.loadSubImagesByNodeList(img, nodeList, self.Opt.finalDim, self.Opt.preserveAspectRatio) else: imData, imDim = ImageLoader.preImageProcessing(img, self.Opt.finalDim, self.Opt.preserveAspectRatio) # Extracting Features X = self.FeatureDescriptor.extractFeatures(imData, 1) # Classify y_pred, y_proba = self.Classifier.predict(X) sub_classes = '' sub_probs = '' sub_height = '' sub_width = '' segmentations = '' for i in range(0, len(nodeList)): if i != 0: sub_classes += ',' sub_probs += ',' sub_height += ',' sub_width += ',' segmentations += ',' sub_classes += str(y_pred[i]) sub_height += str(imDims[i][0]) sub_width += str(imDims[i][1]) sub_prob = '' for j in range(0, len(y_proba[i])): if j != 0: sub_prob += ':' sub_prob += str(y_proba[i][j]) sub_probs += sub_prob node = nodeList[i] segmentation = str(node.info['start'][0]) + ':' + \ str(node.info['start'][1]) + ':' + \ str(node.info['end'][0]) + ':' + \ str(node.info['end'][1]) segmentations += segmentation result = zip([key.name], [numSubImages], [sub_classes], [sub_probs], [segmentations], [sub_height], [sub_width]) Common.saveCSV(csvSavingPath, csvFilename, result, mode = 'ab', consoleOut = False) nImageAll += 1 if np.mod(nImageAll, 100) == 0: print '%d images have been classified' % nImageAll costTime = time.time() - startTime print 'All %d images were classified and saved in %s within %d sec.' % (nImageAll, os.path.join(csvSavingPath, csvFilename), costTime) else: print 'Classifier not loaded'
from DataManager import * from Models import * import sys sys.path.append("..") from Dismantler.Dismantler import * from Dismantler.Options_Dismantler import * ## Global Object Opt = Option(isClassify=True) FD = FeatureDescriptor(Opt.dicPath) Clf = SVMClassifier(Opt, clfPath=Opt.svmModelPath) cIL = CloudImageLoader(Opt) Opt_dmtler = Option_Dismantler(isTrain=False) Opt_CD = Option_CompositeDetector(isTrain=False) Dmtler = Dismantler(Opt_dmtler) CDetector = CompositeImageDetector(Opt_CD) def listener(name, q, outPath, outFilename): print '%s Listener set up in %s' % (name, mp.current_process().name) startTime = time.time() outFilePath = os.path.join(outPath, outFilename) + '.csv' count = 0 outcsv = open(outFilePath, 'ab') writer = csv.writer(outcsv, dialect='excel') while True: content = q.get() if content is not None: count += 1 # Stop