예제 #1
0
def equalize_hist(image: torch.Tensor,
                  nbins: int = 256,
                  mask: Optional[torch.Tensor] = None) -> torch.Tensor:
    """Return image after histogram equalization.

    Parameters
    ----------
    image : torch.Tensor
        Input image
    nbins : int, default 256
        Number of bins for image histogram. Note: this argument is
        ignored for integer images, for which each integer is its own
        bin.
    mask : [type], default None
        Pytorch Tensor of same shape as `image`. Only points at which mask == True
        are used for the equalization, which is applied to the whole image.

    Returns
    -------
    torch.Tensor
        Image tensor after histogram equalization

    References
    ----------
    .. [1] http://www.janeriksolem.net/histogram-equalization-with-python-and.html
    .. [2] https://en.wikipedia.org/wiki/Histogram_equalization
    """
    if mask is not None:
        cdf, bin_centers = cumulative_distribution(image[mask], nbins)
    else:
        cdf, bin_centers = cumulative_distribution(image, nbins)
    out = interp(image.flatten(), bin_centers, cdf)
    return out.reshape(image.shape)
예제 #2
0
파일: vae.py 프로젝트: starstorms9/shape
def interpolateDesigns(anchor_vects, labels, index1, index2, divs=10) :
    mids_string = ' {} , {} '.format(labels[index1].numpy().decode(), labels[index2].numpy().decode())
    print(mids_string)
    interp_vects = ut.interp(anchor_vects[index1].numpy(), anchor_vects[index2].numpy(), divs)
    
    v = model.sample(interp_vects)
    v = v.numpy()[:,:,:,:,0]
    for i, sample in enumerate(v) :    
        ut.plotVox(sample, step=1, threshold=0.5, limits=cf_limits, show_axes=False)
예제 #3
0
    def interpolate_value(samples, timestamp, key):
        epoch = utils.EPOCH

        x = list((s['timestamp'] - epoch).total_seconds() for s in samples)
        y = list(s[key] for s in samples)

        x0 = (timestamp - epoch).total_seconds()
        y0 = utils.interp(x, y, x0)
        return y0
예제 #4
0
def generate_interpolations(config, generator, fix_z=False, fix_y=False, num_per_sheet=16, num_midpoints=8,
                            device='cuda'):
    """

    Args:
        config: 配置
        generator: 生成器模型
        fix_z: 插值的两侧的样本的 z 相同
        fix_y: 插值的两侧的样本的类别相同
        num_per_sheet: 有几个插值
        num_midpoints: 每个插值有多少中间点
        device:

    Returns:

    """
    target_dir = f"./samples/{config['experiment_name']}/_interpolation/"
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)
    if fix_z:
        zs = torch.randn(num_per_sheet, 1, config["dim_z"], device=device)
        zs = zs.repeat(1, num_midpoints + 2, 1).view(-1, config["dim_z"])
    else:
        zs = interp(torch.randn(num_per_sheet, 1, config["dim_z"], device=device),
                    torch.randn(num_per_sheet, 1, config["dim_z"], device=device),
                    num_midpoints).view(-1, config["dim_z"])
    if fix_y:
        ys = sample_labels(config["n_classes"], num_per_sheet).view(num_per_sheet, 1, -1)
        ys = ys.repeat(1, num_midpoints + 2, 1).view(-1, 1)
    else:
        ys = interp(sample_labels(config["n_classes"], num_per_sheet).view(num_per_sheet, 1, -1),
                    sample_labels(config["n_classes"], num_per_sheet).view(num_per_sheet, 1, -1),
                    num_midpoints).view(-1, 1)
    with torch.no_grad():
        generated_samples = generator(zs, ys).data.cpu()
        timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%S')
        fix_info = '' + ('N' if not fix_z and not fix_y else '') + ('Z' if fix_z else '') + (
            'Y' if fix_y else '') + ' fixed'
        image_filename = f"{target_dir}/{fix_info}-{timestamp}.jpg"
        torchvision.utils.save_image(generated_samples, image_filename, nrow=num_midpoints + 2, normalize=True)
예제 #5
0
import numpy as np
import matplotlib.pyplot as plt

import kernels
import utils

if __name__ == '__main__':

    np.random.seed(123)
    x = np.linspace(-5., 5, 20, endpoint=True)

    cov = kernels.se_kernel(x, x, sigma=1., length=2.)
    y = np.random.multivariate_normal(np.zeros(x.shape[0]), cov, size=10)

    fig, ax = plt.subplots()
    ax.set_title('Ten samples from a GP prior with zero mean')
    xdraw = np.linspace(-5, 5, 100, endpoint=True)
    ax.plot(xdraw.reshape(-1, 1), utils.interp(x, y.T, xdraw))
    plt.tight_layout()
    plt.show()
예제 #6
0
    if getattr(args, 'restore', None) is not None:
        solver.restore("params/%s" % args.restore)
    # finetune
    else:
        solver.net.copy_from("params/%s" % args.init_weights)
        # div3 branch
        for name in solver.net.params.keys():
            if 'div3' in name:
                print 'copy params from %s to %s.' % (name[:name.rfind('_div3')], name)
                for i in range(len(solver.net.params[name])):
                    solver.net.params[name][i].data[...] = solver.net.params[name[:name.rfind('_div3')]][i].data
            if '_att' in name:
                print name
                print 'copy params from %s to %s.' % (name[:name.rfind('_att')], name)
                for i in range(len(solver.net.params[name])):
                    solver.net.params[name][i].data[...] = solver.net.params[name[:name.rfind('_att')]][i].data
    
    # surgeries (for upsample layer)
    interp_layers = [layer for layer in solver.net.params.keys() if 'up' in layer]
    utils.interp(solver.net, interp_layers)

    # debug
    if args.debug:
        embed()
    
    # start training
    solver.step(args.step)



예제 #7
0
파일: vae.py 프로젝트: starstorms9/shape
    journey_mids.append(mids[start_index])
    
    for i in range(journey_length) :
        n_dists, close_ids = vec_tree.query(start_vect, k = vects_sample, distance_upper_bound=max_dist)
        if len(shape2vec) in close_ids :
            n_dists, close_ids = vec_tree.query(start_vect, k = vects_sample, distance_upper_bound=max_dist*3)    
        close_ids = list(close_ids)  #[:1000]
        
        for index in sorted(close_ids, reverse=True):
            if index in visited_indices:
                close_ids.remove(index)
        
        next_index = random.choice(close_ids)
        next_vect = vecs[next_index]
        visited_indices.append(next_index)
        interp_vects = ut.interp(next_vect, start_vect, divs = interp_points)
        journey_vecs.extend(interp_vects)
        start_vect = next_vect
        journey_mids.append(mids[next_index])
        
    journey_voxs = np.zeros(( len(journey_vecs), cf_vox_size, cf_vox_size, cf_vox_size))
    for i, vect in enumerate(journey_vecs) :
        journey_voxs[i,...] = model.decode(vect[None,...], apply_sigmoid=True)[0,...,0]
    
    for i, vox in enumerate(journey_voxs) :
        ut.plotVox(vox, step=plot_step, limits = cf_limits, title='', show_axes=False)

#%% Start by randomly searching for some object indices to start from
showRandIndices(100)

#%% Remember good starting indices for various categories
예제 #8
0
    return args


if __name__ == '__main__':
    args = parse_args()

    caffe.set_mode_gpu()
    caffe.set_device(int(args.gpu_id))
    setproctitle.setproctitle(args.model)

    net = caffe.Net('models/' + args.model + ".test.prototxt",
                    'params/' + args.init_weights, caffe.TEST)

    # surgeries
    interp_layers = [layer for layer in net.params.keys() if 'up' in layer]
    utils.interp(net, interp_layers)

    if os.path.exists("configs/%s.json" % args.model):
        load_config("configs/%s.json" % args.model)
    else:
        print "Specified config does not exists, use the default config..."

    time.sleep(2)

    timer = Timer()

    config.ANNOTATION_TYPE = args.dataset
    config.IMAGE_SET = "val2014"
    from spiders.coco_ssm_spider import COCOSSMDemoSpider
    spider = COCOSSMDemoSpider()
    spider.dataset.sort(key=lambda item: int(item.image_path[-10:-4]))
예제 #9
0
if __name__ == '__main__':
    args = parse_args()

    # caffe setup
    caffe.set_mode_gpu()
    caffe.set_device(int(args.gpu_id))

    net = caffe.Net(
            'models/' + args.model + '.test.prototxt',
            'params/' + args.init_weights,
            caffe.TEST)
    
    # surgeries
    interp_layers = [layer for layer in net.params.keys() if 'up' in layer]
    interp(net, interp_layers)

    # load config
    if os.path.exists("configs/%s.json" % args.model):
        load_config("configs/%s.json" % args.model)
    else:
        print "Specified config does not exists, use the default config..."
        
    # image pre-processing
    img = load_image(args.input_image)
    img_org = img.copy()[:,:,::-1]
    origin_height = img.shape[0]
    origin_width = img.shape[1]

    if img.shape[0] > img.shape[1]:
        scale = 1.0 * config.TEST_SCALE / img.shape[0]
 def update(val):
     np.random.seed(123)
     y = sample_prior(x, ssigma.val, slength.val)
     l.set_ydata(utils.interp(x, y, xdraw))
     fig.canvas.draw_idle()
    return np.random.multivariate_normal(np.zeros(x.shape[0]), cov)


if __name__ == '__main__':

    fig, ax = plt.subplots()
    ax.set_title('Interactive GP Prior')
    plt.subplots_adjust(bottom=0.25)

    axsigma = plt.axes([0.2, 0.1, 0.65, 0.03], facecolor='green')
    axlength = plt.axes([0.2, 0.15, 0.65, 0.03], facecolor='green')
    ssigma = Slider(axsigma, 'Sigma', 0.01, 3., valinit=1)
    slength = Slider(axlength, 'Length', 0.1, 3.0, valinit=1)

    np.random.seed(123)
    x = np.linspace(-5., 5, 20, endpoint=True)
    xdraw = np.linspace(-5, 5, 100, endpoint=True)
    y = sample_prior(x, 1., 1.)
    l, = ax.plot(xdraw, utils.interp(x, y, xdraw))

    def update(val):
        np.random.seed(123)
        y = sample_prior(x, ssigma.val, slength.val)
        l.set_ydata(utils.interp(x, y, xdraw))
        fig.canvas.draw_idle()

    ssigma.on_changed(update)
    slength.on_changed(update)

    plt.show()
예제 #12
0
def calcoloFeatures(lista_float):
	nb = 6
	fr = 200/60.0
	db = 1
	dp = 6.5
	alfa = 0

	f_or = nb/2  * fr * (1 - db/dp * np.cos(alfa))
	f_ir = nb/2  * fr * (1 + db/dp * np.cos(alfa))
	f_b  = dp/db * fr * (1 - (db/dp * np.cos(alfa))**2)

	lista_float = np.array(lista_float, np.float32)
	timeLine = np.linspace(0, len(lista_float), len(lista_float))


	# EMD #
	emd = EMD()
	emd.FIXE_H = 3
	emd.nbsym = 2
	emd.splineKind = 'cubic'

	fcamp=1/len(lista_float)
	timeLine = t = np.linspace(0, len(lista_float), len(lista_float))
	VibrationNump = np.array(lista_float, np.float32)
	IMF, EXT, ITER, imfNo = emd.emd(VibrationNump, timeLine, -1)

	c = np.floor(np.sqrt(imfNo + 3))
	r = np.ceil((imfNo + 2) / c)

	def butter_lowpass(cutoff, fs, order=5):
		nyq = 0.5*fs
		normal_cutoff = cutoff / nyq
		b, a = butter(order, normal_cutoff, btype='low', analog=False)
		return b,a

	def butter_lowpass_filter(data, cutoff, fs, order=5):
		b, a = butter_lowpass(cutoff, fs, order=order)
		y = lfilter(b,a,data)
		return y

	mhsf_for = []
	mhsf_fir = []
	mhsf_fb  = []


	for num in range(imfNo):
		H, Amp, phase = hilb(IMF[num], unwrap=True)
		freq = np.diff(phase)/(2 * np.pi) * fcamp
		f, hf = mhs(Amp, freq)

		mhsf = interp(f, hf) #interpolante

		# ABBIAMO CALCOLATO L'MHS PER TUTTI GLI IMF, ORA BISOGNA TROVARE IL VALORE MASSIMO
		# DI TUTTI GLI MHS NELLE 3 FREQUENZE (f_or, f_ir, f_b)
		mhsf_for.append(mhsf(f_or))
		mhsf_fir.append(mhsf(f_ir))
		mhsf_fb.append(mhsf(f_b))

		##trovare il maggiore tra gli mhsf_for, mhsf_fir, mhsf_fb

		#passabasso
		fs = 50 # Hz, sample rate (un valore ogni 0.5s)
		order= 6 ######### ??????????????

		#FOURIER
		DanneggiatoFFT=fft(lista_float)
		PS=np.abs(DanneggiatoFFT)**2
		AREA=np.trapz(PS)
		LP_for=butter_lowpass_filter(lista_float,f_or,fs,order)
		FORFFT=fft(LP_for)
		PSFOR=np.abs(FORFFT)**2
		FOR_AREA=np.trapz(PSFOR)
		FOR_FEAT=FOR_AREA/AREA
		LP_fir=butter_lowpass_filter(lista_float,f_ir,fs,order)
		FIRFFT=fft(LP_fir)
		PSFIR=np.abs(FIRFFT)**2
		FIR_AREA=np.trapz(PSFIR)
		FIR_FEAT=FIR_AREA/AREA
		LP_fb=butter_lowpass_filter(lista_float,f_b,fs,order)
		FBFFT=fft(LP_fb)
		PSFB=np.abs(FBFFT)**2
		FB_AREA=np.trapz(PSFB)
		FB_FEAT=FB_AREA/AREA
		#PLOTTING#
		#plt.subplot(r, c, num + 3)
		#plt.plot(timeLine, IMF[num], 'g')
		#plt.title("IMF no " + str(num))


	max_for = max(mhsf_for)
	max_fir = max(mhsf_fir)
	max_fb  = max(mhsf_fb)
	# print(FOR_FEAT)
	# print(FIR_FEAT)
	# print(FB_FEAT)
	# print(max_for)
	# print(max_fir)
	# print(max_fb)
	return FOR_FEAT,FIR_FEAT,FB_FEAT,max_for,max_fir,max_fb