def show_cmc(model, X, indices, samples): print "Computing cmc ..." result = cachem.load('cmc') if result is not None: return result import theano import theano.tensor as T from reid.models.layers import CompLayer from reid.utils import cmc x = T.matrix() y, thr = model.get_output(x) thr.append(y) comp = CompLayer() y, thr = comp.get_output(thr) f = theano.function(inputs=[x], outputs=y) train_pids = samples[3] gX, gY, pX, pY = [], [], [], [] for i, (pid, vid) in enumerate(indices): if pid in train_pids: continue if vid == 0: gX.append(i) gY.append(pid) else: pX.append(i) pY.append(pid) def compute_distance(i, j): y = f(numpy.hstack( (X[gX[i]:gX[i] + 1, :], X[pX[j]:pX[j] + 1, :]))).ravel() return -y[-1] return cmc.count_lazy(compute_distance, gY, pY, 100, 1)
def show_cmc(model, X, indices, samples): print "Computing cmc ..." result = cachem.load('cmc') if result is not None: return result import theano import theano.tensor as T from reid.models.layers import CompLayer from reid.utils import cmc x = T.matrix() y, thr = model.get_output(x) thr.append(y) comp = CompLayer() y, thr = comp.get_output(thr) f = theano.function(inputs=[x], outputs=y) train_pids = samples[3] gX, gY, pX, pY = [], [], [], [] for i, (pid, vid) in enumerate(indices): if pid in train_pids: continue if vid == 0: gX.append(i) gY.append(pid) else: pX.append(i) pY.append(pid) def compute_distance(i, j): y = f(numpy.hstack((X[gX[i]:gX[i]+1, :], X[pX[j]:pX[j]+1, :]))).ravel() return -y[-1] return cmc.count_lazy(compute_distance, gY, pY, 100, 1)
def compute_result(model, batch_dir, images, samples): """Compute output for dataset Args: model: Deep model dataset: Dataset X = X1_X2, Y = A1_A2_(0/1) images: [img] samples: (train, vaid, test) each is [(i, j, 0/1)] Returns: (train, valid, test) where each is (img_list_1, img_list_2 output_matrix, target_matrix) """ print "Computing result ..." result = cachem.load('result') if result is not None: return result import glob import cPickle import theano import theano.tensor as T from reid.models.layers import CompLayer x = T.matrix() y, thr = model.get_output(x) thr.append(y) comp = CompLayer() y, thr = comp.get_output(thr) f = theano.function(inputs=[x], outputs=y) def compute_output(X): outputs = [f(X[i:i + 1, :]).ravel() for i in xrange(X.shape[0])] return numpy.asarray(outputs) train_files = glob.glob(os.path.join(batch_dir, 'train_*.pkl')) valid_files = glob.glob(os.path.join(batch_dir, 'valid_*.pkl')) test_files = glob.glob(os.path.join(batch_dir, 'test_*.pkl')) train_files.sort() valid_files.sort() test_files.sort() # Setup parameters n_train_batches = len(train_files) n_valid_batches = len(valid_files) n_test_batches = len(test_files) for i in xrange(n_train_batches): with open(train_files[i], 'rb') as fid: X, T = cPickle.load(fid) Y = compute_output(X) if i == 0: cum_T = T cum_Y = Y else: cum_T = numpy.vstack((cum_T, T)) cum_Y = numpy.vstack((cum_Y, Y)) train = ([images[i] for i, __, __ in samples[0]], [images[j] for __, j, __ in samples[0]], cum_Y, cum_T) for i in xrange(n_valid_batches): with open(valid_files[i], 'rb') as fid: X, T = cPickle.load(fid) Y = compute_output(X) if i == 0: cum_T = T cum_Y = Y else: cum_T = numpy.vstack((cum_T, T)) cum_Y = numpy.vstack((cum_Y, Y)) valid = ([images[i] for i, __, __ in samples[1]], [images[j] for __, j, __ in samples[1]], cum_Y, cum_T) for i in xrange(n_test_batches): with open(test_files[i], 'rb') as fid: X, T = cPickle.load(fid) Y = compute_output(X) if i == 0: cum_T = T cum_Y = Y else: cum_T = numpy.vstack((cum_T, T)) cum_Y = numpy.vstack((cum_Y, Y)) test = ([images[i] for i, __, __ in samples[2]], [images[j] for __, j, __ in samples[2]], cum_Y, cum_T) return (train, valid, test)
def compute_result(model, batch_dir, images, samples): """Compute output for dataset Args: model: Deep model dataset: Dataset X = X1_X2, Y = A1_A2_(0/1) images: [img] samples: (train, vaid, test) each is [(i, j, 0/1)] Returns: (train, valid, test) where each is (img_list_1, img_list_2 output_matrix, target_matrix) """ print "Computing result ..." result = cachem.load('result') if result is not None: return result import glob import cPickle import theano import theano.tensor as T from reid.models.layers import CompLayer x = T.matrix() y, thr = model.get_output(x) thr.append(y) comp = CompLayer() y, thr = comp.get_output(thr) f = theano.function(inputs=[x], outputs=y) def compute_output(X): outputs = [f(X[i:i+1, :]).ravel() for i in xrange(X.shape[0])] return numpy.asarray(outputs) train_files = glob.glob(os.path.join(batch_dir, 'train_*.pkl')) valid_files = glob.glob(os.path.join(batch_dir, 'valid_*.pkl')) test_files = glob.glob(os.path.join(batch_dir, 'test_*.pkl')) train_files.sort() valid_files.sort() test_files.sort() # Setup parameters n_train_batches = len(train_files) n_valid_batches = len(valid_files) n_test_batches = len(test_files) for i in xrange(n_train_batches): with open(train_files[i], 'rb') as fid: X, T = cPickle.load(fid) Y = compute_output(X) if i == 0: cum_T = T cum_Y = Y else: cum_T = numpy.vstack((cum_T, T)) cum_Y = numpy.vstack((cum_Y, Y)) train = ([images[i] for i, __, __ in samples[0]], [images[j] for __, j, __ in samples[0]], cum_Y, cum_T) for i in xrange(n_valid_batches): with open(valid_files[i], 'rb') as fid: X, T = cPickle.load(fid) Y = compute_output(X) if i == 0: cum_T = T cum_Y = Y else: cum_T = numpy.vstack((cum_T, T)) cum_Y = numpy.vstack((cum_Y, Y)) valid = ([images[i] for i, __, __ in samples[1]], [images[j] for __, j, __ in samples[1]], cum_Y, cum_T) for i in xrange(n_test_batches): with open(test_files[i], 'rb') as fid: X, T = cPickle.load(fid) Y = compute_output(X) if i == 0: cum_T = T cum_Y = Y else: cum_T = numpy.vstack((cum_T, T)) cum_Y = numpy.vstack((cum_Y, Y)) test = ([images[i] for i, __, __ in samples[2]], [images[j] for __, j, __ in samples[2]], cum_Y, cum_T) return (train, valid, test)