def resize_mean(inFile, outFile, imSz): mn = mp.read_mean(inFile) dType = mn.dtype ch, rows, cols = mn.shape mn = mn.transpose((1,2,0)) mn = scm.imresize(mn, (imSz, imSz)).transpose((2,0,1)).reshape((1,ch,imSz,imSz)) write_proto(mn.astype(dType), outFile)
def load_mean(self): self.mu_ = None if self.param_.mean_file is not None: print('READING MEAN FROM %s', self.param_.mean_file) if self.param_.mean_file[-3:] == 'pkl': meanDat = pickle.load(open(self.param_.mean_file, 'r')) self.mu_ = meanDat['mu'].astype(np.float32).transpose( (2, 0, 1)) else: #Mean is assumbed to be in BGR format self.mu_ = mp.read_mean(self.param_.mean_file) self.mu_ = self.mu_.astype(np.float32) if self.param_.mean_type == '3val': self.mu_ = np.mean(self.mu_, axis=(1, 2)).reshape(1, 3, 1, 1) self.mu_ = np.concatenate((self.mu_, self.mu_), axis=1) elif self.param_.mean_type == 'img': ch, h, w = self.mu_.shape assert (h >= self.param_.crop_size and w >= self.param_.crop_size) y1 = int(h / 2 - (self.param_.crop_size / 2)) x1 = int(w / 2 - (self.param_.crop_size / 2)) y2 = int(y1 + self.param_.crop_size) x2 = int(x1 + self.param_.crop_size) self.mu_ = self.mu_[:, y1:y2, x1:x2] self.mu_ = self.mu_.reshape((1, ) + self.mu_.shape) else: raise Exception('Mean type %s not recognized' % self.param_.mean_type)
def resize_mean(inFile, outFile, imSz): mn = mp.read_mean(inFile) dType = mn.dtype ch, rows, cols = mn.shape mn = mn.transpose((1, 2, 0)) mn = scm.imresize(mn, (imSz, imSz)).transpose((2, 0, 1)).reshape( (1, ch, imSz, imSz)) write_proto(mn.astype(dType), outFile)
def mean2graymean(inFile, outFile): assert not os.path.exists(outFile), '%s already exists' % outFile mn = mp.read_mean(inFile) dType = mn.dtype ch = mn.shape[0] assert ch==3 mn = rgb2gray(mn.transpose((1,2,0))).reshape((1,1,mn.shape[1],mn.shape[2])) print "New mean shape: ", mn.shape, dType write_proto(mn.astype(dType), outFile)
def siamese_mean2mean(inFile, outFile): assert not os.path.exists(outFile), '%s already exists' % outFile mn = mp.read_mean(inFile) ch = mn.shape[0] assert np.mod(ch,2)==0 ch = ch / 2 print "New number of channels: %d" % ch newMn = mn[0:ch].reshape(1,ch,mn.shape[1],mn.shape[2]) write_proto(newMn.astype(mn.dtype), outFile)
def mean2siamese_mean(inFile, outFile, isGray=False): mn = mp.read_mean(inFile) if isGray: mn = mn.reshape((1,mn.shape[0],mn.shape[1])) mn = np.concatenate((mn, mn)) dType = mn.dtype mn = mn.reshape((1, mn.shape[0], mn.shape[1], mn.shape[2])) print "New mean shape: ", mn.shape, dType write_proto(mn, outFile)
def siamese_mean2mean(inFile, outFile): assert not os.path.exists(outFile), '%s already exists' % outFile mn = mp.read_mean(inFile) ch = mn.shape[0] assert np.mod(ch, 2) == 0 ch = ch / 2 print "New number of channels: %d" % ch newMn = mn[0:ch].reshape(1, ch, mn.shape[1], mn.shape[2]) write_proto(newMn.astype(mn.dtype), outFile)
def mean2siamese_mean(inFile, outFile, isGray=False): mn = mp.read_mean(inFile) if isGray: mn = mn.reshape((1, mn.shape[0], mn.shape[1])) mn = np.concatenate((mn, mn)) dType = mn.dtype mn = mn.reshape((1, mn.shape[0], mn.shape[1], mn.shape[2])) print "New mean shape: ", mn.shape, dType write_proto(mn, outFile)
def mean2graymean(inFile, outFile): assert not os.path.exists(outFile), '%s already exists' % outFile mn = mp.read_mean(inFile) dType = mn.dtype ch = mn.shape[0] assert ch == 3 mn = rgb2gray(mn.transpose((1, 2, 0))).reshape( (1, 1, mn.shape[1], mn.shape[2])) print "New mean shape: ", mn.shape, dType write_proto(mn.astype(dType), outFile)
def load_mean(self): self.mu_ = None if len(self.param_.mean_file) > 0: #Mean is assumbed to be in BGR format self.mu_ = mp.read_mean(self.param_.mean_file) self.mu_ = self.mu_.astype(np.float32) ch, h, w = self.mu_.shape assert (h >= self.param_.crop_size and w >= self.param_.crop_size) y1 = int(h/2 - (self.param_.crop_size/2)) x1 = int(w/2 - (self.param_.crop_size/2)) y2 = int(y1 + self.param_.crop_size) x2 = int(x1 + self.param_.crop_size) self.mu_ = self.mu_[:,y1:y2,x1:x2]
def load_mean(self): self.mu_ = None if 'binaryproto' in self.param_.mean_file: print('##### MEAN FILE FOUND ######') #Mean is assumbed to be in BGR format self.mu_ = mp.read_mean(self.param_.mean_file) self.mu_ = self.mu_.astype(np.float32) ch, h, w = self.mu_.shape assert (h >= self.param_.im_size and w >= self.param_.im_size) y1 = int(h / 2 - (self.param_.im_size / 2)) x1 = int(w / 2 - (self.param_.im_size / 2)) y2 = int(y1 + self.param_.im_size) x2 = int(x1 + self.param_.im_size) self.mu_ = self.mu_[:, y1:y2, x1:x2]
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 = mp.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 vis_results_pose(prms, cPrms, modelIter): #Initialize the network exp = se.setup_experiment(prms, cPrms) modelFile = exp.get_snapshot_name(numIter=modelIter) defFile = exp.expFile_.def_ net = caffe.Net(defFile, modelFile, caffe.TEST) bSz = exp.get_layer_property('window_data', 'batch_size', phase='TEST') crpSz = int( exp.get_layer_property('window_data', 'crop_size', phase='TEST')) muFile = exp.get_layer_property('window_data', 'mean_file')[1:-1] mn = mp.read_mean(muFile).transpose(1, 2, 0) st = int((227 - 101) / 2.0) print mn.shape mn = mn[st:st + 101, st:st + 101, :] mn = mn[:, :, [2, 1, 0]] plt.ion() count = 0 numBatches = 5 fig = plt.figure() for nb in range(numBatches): #Generate results on test set data = net.forward(['data', 'data_p', 'pose_fc', 'pose_label']) for b in range(int(bSz)): predLbl = data['pose_fc'][b].squeeze() predEuler = ru.quat2euler(predLbl) gtLbl = data['pose_label'][b].squeeze() gtEuler = ru.quat2euler(gtLbl) tStr = 'GT- roll: %.2f, yaw: %.2f, pitch: %.2f\n'\ + 'PD- roll: %.2f, yaw: %.2f, pitch: %.2f' #pdb.set_trace() tStr = tStr % (gtEuler + predEuler) im1 = data['data'][b].transpose(1, 2, 0).squeeze() im2 = data['data_p'][b].transpose(1, 2, 0).squeeze() im1 = np.maximum(0, np.minimum(255, im1[:, :, [2, 1, 0]] + mn)) im2 = np.maximum(0, np.minimum(255, im2[:, :, [2, 1, 0]] + mn)) #pdb.set_trace() vu.plot_pairs(im1, im2, fig, figTitle=tStr) count += 1 imName = exp.paths.testImVis % count plt.savefig(imName)