def setup_prototypical_network(netName='vgg', layerName='pool4'): ''' Sets up a network in a configuration in which I commonly use it. ''' modelFile, meanFile = get_model_mean_file(netName) defFile = get_layer_def_files(netName, layerName=layerName) meanDat = mpio.read_mean(meanFile) net = MyNet(defFile, modelFile) net.set_preprocess(ipName='data', meanDat=meanDat, imageDims=(256,256,3)) return net
def setup_prototypical_network(netName='vgg', layerName='pool4'): ''' Sets up a network in a configuration in which I commonly use it. ''' modelFile, meanFile = get_model_mean_file(netName) defFile = get_layer_def_files(netName, layerName=layerName) meanDat = mpio.read_mean(meanFile) net = MyNet(defFile, modelFile) net.set_preprocess(ipName='data', meanDat=meanDat, imageDims=(256, 256, 3)) return net
def vis_generic_window_data(protoDef, numLabels, layerName='window_data', phase='TEST', maxVis=100): ''' layerName: The name of the generic_window_data layer numLabels: The number of labels. ''' #Just write the data part of the file. if not isinstance(protoDef, mpu.ProtoDef): protoDef = mpu.ProtoDef(protoDef) protoDef.del_all_layers_above(layerName) randInt = np.random.randint(1e+10) outProto = os.path.join(TMP_DATA_DIR, 'gn_window_%d.prototxt' % randInt) protoDef.write(outProto) #Extract the name of the data and the label blobs. dataName = protoDef.get_layer_property(layerName, 'top', propNum=0)[1:-1] labelName = protoDef.get_layer_property(layerName, 'top', propNum=1)[1:-1] crpSize = int(protoDef.get_layer_property(layerName, ['crop_size'])) mnFile = protoDef.get_layer_property(layerName, ['mean_file'])[1:-1] mnDat = mpio.read_mean(mnFile) ch, nr, nc = mnDat.shape xMn = int((nr - crpSize) / 2) mnDat = mnDat[:, xMn:xMn + crpSize, xMn:xMn + crpSize] print mnDat.shape #Create a network if phase == 'TRAIN': net = caffe.Net(outProto, caffe.TRAIN) else: net = caffe.Net(outProto, caffe.TEST) lblStr = ''.join('lb-%d: %s, ' % (i, '%.2f') for i in range(numLabels)) figDt = plt.figure() plt.ion() for i in range(maxVis): allDat = net.forward([dataName, labelName]) imData = allDat[dataName] + mnDat lblDat = allDat[labelName] batchSz = imData.shape[0] for b in range(batchSz): #Plot network data. im1 = imData[b, 0:3].transpose((1, 2, 0)) im2 = imData[b, 3:6].transpose((1, 2, 0)) im1 = im1[:, :, [2, 1, 0]] im2 = im2[:, :, [2, 1, 0]] lb = lblDat[b].squeeze() lbStr = lblStr % tuple(lb) plot_pairs(im1, im2, figDt, lbStr) raw_input()
def set_preprocess(self, ipName='data',chSwap=(2,1,0), meanDat=None, imageDims=None, isBlobFormat=False, rawScale=None, cropDims=None, noTransform=False, numCh=3): ''' isBlobFormat: if the images are already coming in blobFormat or not. ipName : the blob for which the pre-processing parameters need to be set. meanDat : the mean which needs to subtracted imageDims : the size of the images as H * W * K where K is the number of channels cropDims : the size to which the image needs to be cropped. if None - then it is automatically determined this behavior is undesirable for some deploy prototxts noTransform: if no transform needs to be applied numCh : number of channels ''' if chSwap is not None: assert len(chSwap) == numCh, 'Number of channels mismatch (%d, %d)'\ %(len(chSwap), numCh) if noTransform: self.transformer[ipName] = None return self.transformer[ipName] = caffe.io.Transformer({ipName: self.net.blobs[ipName].data.shape}) #Note blobFormat will be so used that finally the image will need to be flipped. self.transformer[ipName].set_transpose(ipName, (2,0,1)) if isBlobFormat: assert chSwap is None, 'With Blob format chSwap should be none' if chSwap is not None: #Required for eg RGB to BGR conversion. print (ipName, chSwap) self.transformer[ipName].set_channel_swap(ipName, chSwap) if rawScale is not None: self.transformer[ipName].set_raw_scale(ipName, rawScale) #Crop Dimensions ipDims = np.array(self.net.blobs[ipName].data.shape) if cropDims is not None: assert len(cropDims)==2, 'Length of cropDims needs to be corrected' self.cropDims = np.array(cropDims) else: self.cropDims = ipDims[2:] self.isBlobFormat = isBlobFormat if imageDims is None: imageDims = np.array([ipDims[2], ipDims[3], ipDims[1]]) else: assert len(imageDims)==3 imageDims = np.array([imageDims[0], imageDims[1], imageDims[2]]) self.imageDims = imageDims self.get_crop_dims() #Mean Subtraction if not meanDat is None: isTuple = False if isinstance(meanDat, string_types): meanDat = mpio.read_mean(meanDat) elif type(meanDat) == tuple: meanDat = np.array(meanDat).reshape(numCh,1,1) meanDat = meanDat * (np.ones((numCh, self.crop[2] - self.crop[0],\ self.crop[3]-self.crop[1])).astype(np.float32)) isTuple = True _,h,w = meanDat.shape assert self.imageDims[0]<=h and self.imageDims[1]<=w,\ 'imageDims must match mean Image size, (h,w), (imH, imW): (%d, %d), (%d,%d)'\ % (h,w,self.imageDims[0],self.imageDims[1]) if not isTuple: meanDat = meanDat[:, self.crop[0]:self.crop[2], self.crop[1]:self.crop[3]] self.transformer[ipName].set_mean(ipName, meanDat)
def set_preprocess(self, ipName='data', chSwap=(2, 1, 0), meanDat=None, imageDims=None, isBlobFormat=False, rawScale=None, cropDims=None, noTransform=False, numCh=3): ''' isBlobFormat: if the images are already coming in blobFormat or not. ipName : the blob for which the pre-processing parameters need to be set. meanDat : the mean which needs to subtracted imageDims : the size of the images as H * W * K where K is the number of channels cropDims : the size to which the image needs to be cropped. if None - then it is automatically determined this behavior is undesirable for some deploy prototxts noTransform: if no transform needs to be applied numCh : number of channels ''' if chSwap is not None: assert len(chSwap) == numCh, 'Number of channels mismatch (%d, %d)'\ %(len(chSwap), numCh) if noTransform: self.transformer[ipName] = None return self.transformer[ipName] = caffe.io.Transformer( {ipName: self.net.blobs[ipName].data.shape}) #Note blobFormat will be so used that finally the image will need to be flipped. self.transformer[ipName].set_transpose(ipName, (2, 0, 1)) if isBlobFormat: assert chSwap is None, 'With Blob format chSwap should be none' if chSwap is not None: #Required for eg RGB to BGR conversion. print(ipName, chSwap) self.transformer[ipName].set_channel_swap(ipName, chSwap) if rawScale is not None: self.transformer[ipName].set_raw_scale(ipName, rawScale) #Crop Dimensions ipDims = np.array(self.net.blobs[ipName].data.shape) if cropDims is not None: assert len( cropDims) == 2, 'Length of cropDims needs to be corrected' self.cropDims = np.array(cropDims) else: self.cropDims = ipDims[2:] self.isBlobFormat = isBlobFormat if imageDims is None: imageDims = np.array([ipDims[2], ipDims[3], ipDims[1]]) else: assert len(imageDims) == 3 imageDims = np.array([imageDims[0], imageDims[1], imageDims[2]]) self.imageDims = imageDims self.get_crop_dims() #Mean Subtraction if not meanDat is None: isTuple = False if isinstance(meanDat, string_types): meanDat = mpio.read_mean(meanDat) elif type(meanDat) == tuple: meanDat = np.array(meanDat).reshape(numCh, 1, 1) meanDat = meanDat * (np.ones((numCh, self.crop[2] - self.crop[0],\ self.crop[3]-self.crop[1])).astype(np.float32)) isTuple = True _, h, w = meanDat.shape assert self.imageDims[0]<=h and self.imageDims[1]<=w,\ 'imageDims must match mean Image size, (h,w), (imH, imW): (%d, %d), (%d,%d)'\ % (h,w,self.imageDims[0],self.imageDims[1]) if not isTuple: meanDat = meanDat[:, self.crop[0]:self.crop[2], self.crop[1]:self.crop[3]] self.transformer[ipName].set_mean(ipName, meanDat)