Exemple #1
0
 def find_corresp_aexpansion(D, initLabels, lmb,
                             maxV):
     from maxflow.fastmin import aexpansion_grid
     V = np.fromfunction(lambda i, j: lmb * np.minimum(abs(i - j), maxV), (D.shape[-1], D.shape[-1]))
     # V = lmb * np.vectorize(lambda x: 1. if x==0 else 0.)(np.identity(D.shape[-1]))
     # V = np.fromfunction(lambda i, j: lmb * np.minimum((i - j)**2, maxV), (D.shape[-1], D.shape[-1]))
     return aexpansion_grid(D, V, max_cycles=None, labels=initLabels)
Exemple #2
0
 def get_content_feature_map(Fc, KM, k, l=0.1):
     V = np.ones((k, )) * l - np.identity(k) * l
     D = cosine_distances(
         Fc.reshape((vgg_feature_h * vgg_feature_w, vgg_feature_c)),
         KM.cluster_centers_).reshape(
             (vgg_feature_h, vgg_feature_w, -1)).astype(np.float64)
     content_labels = aexpansion_grid(D, V, max_cycles=None, labels=None)
     return content_labels
def solve(img, disp_min, disp_max, smoothness):
    """img must be an array of np.float_"""
    D = ssd_volume(img, img, np.arange(disp_min, disp_max), 5)
    num_labels = D.shape[-1]
    X,Y = np.mgrid[:num_labels, :num_labels]
    V = smoothness*np.float_(np.abs(X-Y))
    sol = fastmin.aexpansion_grid(D, V, 3)
    return sol
Exemple #4
0
    def graphCutOptimaze(self):
        #	初始化D
        print("[VQRCode] GCO: Initializing D Matrix...")
        D = np.zeros((self.Length, self.Length, 512))
        for l in range(512):
            for r in range(self.Length):
                for c in range(self.Length):
                    #对第r行c列的patch赋予标签l的unary cost
                    ##可靠性 ER
                    ER = 0.0
                    if self.centerColorMap[r][c] == VQRPatch.getCenterColor(l):
                        ER = VQRPatch.getReliability(l)
                    ER = 1.0 - ER
                    ER = ER * exp(-self.weightsMap[r][c])
                    ##相似性 EG
                    #
                    startX = c * 3
                    startY = r * 3
                    binStr = ""
                    for y in range(3):
                        for x in range(3):
                            if self.Vimg.HalftoneImg.getpixel(
                                (startX + x, startY + y)) == 0:
                                #black
                                binStr = binStr + '1'
                            else:
                                #white
                                binStr = binStr + '0'
                    sn = int(binStr, 2)
                    EG = VQRCode.pSSIMMap[sn][l]
                    ##惩罚性
                    EC = 0
                    if self.centerColorMap[r][c] != VQRPatch.getCenterColor(l):
                        EC = 1
                    EC = EC * self.Beta

                    D[r][c][l] = self.Lamda * ER + EG + EC
                    print("ER=%s   EG=%s   EC=%s   D=%s" %
                          (ER, EG, EC, D[r][c][l]))
        #	初始化V
        print("[VQRCode] GCO: Initializing V Matrix...")
        V = np.zeros((512, 512))
        for r in range(512):
            for c in range(512):
                V[r][c] = self.Wsmooth * VQRCode.pSSIMMap[r][c]
        #	solve
        print("[VQRCode] Running Graph Cut for D=(%sx%sx%s) V=(%sx%s)" %
              (D.shape[0], D.shape[1], D.shape[2], V.shape[0], V.shape[1]))
        res = fastmin.aexpansion_grid(D, V)
        print(res.tolist())
        return res.tolist()
        pass
Exemple #5
0
	def graphCutOptimaze(self):
		#	初始化D
		print("[VQRCode] GCO: Initializing D Matrix...")
		D= np.zeros((self.Length, self.Length, 512))
		for l in range(512):
			for r in range(self.Length):
				for c in range(self.Length):
					#对第r行c列的patch赋予标签l的unary cost
					##可靠性 ER
					ER=0.0
					if self.centerColorMap[r][c]==VQRPatch.getCenterColor(l):
						ER=VQRPatch.getReliability(l)
					ER=1.0-ER
					ER=ER*exp(-self.weightsMap[r][c])
					##相似性 EG
					#
					startX=c*3
					startY=r*3
					binStr=""
					for y in range(3):
						for x in range(3):
							if self.Vimg.HalftoneImg.getpixel((startX+x,startY+y))==0:
								#black
								binStr=binStr+'1'
							else:
								#white
								binStr=binStr+'0'
					sn=int(binStr,2)
					EG=VQRCode.pSSIMMap[sn][l]
					##惩罚性
					EC=0
					if self.centerColorMap[r][c]!=VQRPatch.getCenterColor(l):
						EC=1
					EC=EC*self.Beta
					
					D[r][c][l]=self.Lamda*ER+EG+EC
					print("ER=%s   EG=%s   EC=%s   D=%s" %(ER,EG,EC,D[r][c][l]) )
		#	初始化V
		print("[VQRCode] GCO: Initializing V Matrix...")
		V=np.zeros((512,512))
		for r in range(512):
			for c in range(512):
				V[r][c]=self.Wsmooth * VQRCode.pSSIMMap[r][c]
		#	solve
		print("[VQRCode] Running Graph Cut for D=(%sx%sx%s) V=(%sx%s)" %(D.shape[0],D.shape[1],D.shape[2],V.shape[0],V.shape[1]))
		res =fastmin.aexpansion_grid(D,V)
		print(res.tolist())
		return res.tolist()
		pass
def question_4(I, rho=0.8):
    labels = np.unique(I)

    # 1) Compute Unary cost
    # define unary costs
    u_same = -np.log(rho)
    u_diff = -np.log((1 - rho) / 2)

    # assign unary costs
    D = np.zeros(I.shape + labels.shape)
    for y in range(I.shape[0]):
        for x in range(I.shape[1]):
            for lbl in range(labels.shape[0]):
                if I[y, x] == labels[lbl]:
                    D[y, x, lbl] = u_same
                else:
                    D[y, x, lbl] = u_diff

    # 2) Compute Pairwise cost metric
    # define pairwise cost
    def pairwise_cost(i, j):
        def kronecker_delta(i, j):
            return 1 if i == j else 0

        return 1 - kronecker_delta(i, j)

    # assign pairwise costs
    V = np.zeros((labels.shape[0], labels.shape[0]))
    for i in range(labels.shape[0]):
        for j in range(labels.shape[0]):
            V[i, j] = pairwise_cost(i, j)

    # 3) Alpha expansion
    alpha_exp = aexpansion_grid(D, V).astype(np.uint8)

    # 4) Assign values to labels
    for lbl in range(labels.shape[0]):
        alpha_exp[alpha_exp == lbl] = labels[lbl]
    Denoised_I = alpha_exp

    cv2.imshow('Original Img', I), \
    cv2.imshow('Denoised Img', Denoised_I), cv2.waitKey(0), cv2.destroyAllWindows()
        a = random.random()
        if (0 < a < 0.33333):
            labels[value] = 0
            
        elif(0.33333 <= a < 0.66666):
            labels[value] = 1
            
        elif(a >= 0.66666):
            labels[value] = 2
    
    # initial labels
    initialConditionsImage = (np.resize(labels, (xPixels, yPixels))).astype('uint8')*colourNumInt;
    
    
    # perform fast approximate energy minimisation
    alphaExpansion = fastmin.aexpansion_grid(D, V, maxCycles, labels)
    alphaBetaSwap = fastmin.abswap_grid(D, V, maxCycles, labels)
    
    
    # Get the segment labels and convert to a format for image display
#     aeSegResultImage = (np.resize(alphaExpansion, (xPixels, yPixels))).astype('uint8')*colourNumInt
    
    aeSegResultImage = (np.resize(alphaExpansion.astype('uint8'), (xPixels, yPixels)))*colourNumInt
    
    print "Set of result image values::\n", set(np.resize(aeSegResultImage, numPixels))
    
    
    # normalise class labels for conversion to greyscale
    cv2.imshow("scenelabel3 initial labels", initialConditionsImage)
    cv2.imshow("scenelabel3 alpha expansion result", aeSegResultImage)
#     cv2.imshow("scenelabel3 alpha beta swap result", abSegResultImage)
        a = random.random()
        if (0 < a < 0.33333):
            labels[value] = 0

        elif (0.33333 <= a < 0.66666):
            labels[value] = 1

        elif (a >= 0.66666):
            labels[value] = 2

    # initial labels
    initialConditionsImage = (np.resize(
        labels, (xPixels, yPixels))).astype('uint8') * colourNumInt

    # perform fast approximate energy minimisation
    alphaExpansion = fastmin.aexpansion_grid(D, V, maxCycles, labels)
    alphaBetaSwap = fastmin.abswap_grid(D, V, maxCycles, labels)

    # Get the segment labels and convert to a format for image display
    #     aeSegResultImage = (np.resize(alphaExpansion, (xPixels, yPixels))).astype('uint8')*colourNumInt

    aeSegResultImage = (np.resize(alphaExpansion.astype('uint8'),
                                  (xPixels, yPixels))) * colourNumInt

    print "Set of result image values::\n", set(
        np.resize(aeSegResultImage, numPixels))

    # normalise class labels for conversion to greyscale
    cv2.imshow("scenelabel3 initial labels", initialConditionsImage)
    cv2.imshow("scenelabel3 alpha expansion result", aeSegResultImage)
    #     cv2.imshow("scenelabel3 alpha beta swap result", abSegResultImage)
Exemple #9
0
def style_transfer(content=None,
                   content_dir=None,
                   content_size=512,
                   style=None,
                   style_dir=None,
                   style_size=512,
                   crop=None,
                   alpha=1.0,
                   output_dir='output',
                   save_ext='jpg',
                   gpu=0,
                   vgg_weights='models/vgg19_weights_normalized.h5',
                   decoder_weights='models/ckp-MST-paper',
                   patch_size=3,
                   n_clusters_s=3,
                   graphPara_smooth=0.1,
                   graphPara_max_cycles=3,
                   data_format='channels_first'):
    assert bool(content) != bool(
        content_dir), 'Either content or content_dir should be given'
    assert bool(style) != bool(
        style_dir), 'Either style or style_dir should be given'

    if not os.path.exists(output_dir):
        print('Creating output dir at', output_dir)
        os.makedirs(output_dir)

    # Assume that it is either an h5 file or a name of a TensorFlow checkpoint
    decoder_in_h5 = decoder_weights.endswith('.h5')

    if content:
        content_batch = [content]
    else:
        content_batch = extract_image_names_recursive(content_dir)

    if style:
        style_batch = [style]
    else:
        style_batch = extract_image_names_recursive(style_dir)

    print('Number of content images:', len(content_batch))
    print('Number of style images:', len(style_batch))
    total_output_batch = len(content_batch) * len(style_batch)
    print('Total number of output:', total_output_batch)

    image, content, style, target, encoder, decoder = _build_graph(
        vgg_weights,
        decoder_weights if decoder_in_h5 else None,
        alpha,
        patch_size,
        data_format=data_format)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    start_time = time.time()
    with tf.Session() as sess:
        if decoder_in_h5:
            sess.run(tf.global_variables_initializer())
        else:
            saver = tf.train.Saver()
            saver.restore(sess, decoder_weights)

        for content_path, style_path in product(content_batch, style_batch):
            content_name = get_filename(content_path)
            content_image = load_image(content_path, content_size, crop)

            style_name = get_filename(style_path)
            style_image = load_image(style_path, style_size, crop)

            style_image = prepare_image(style_image)
            content_image = prepare_image(content_image)
            style_feature = sess.run(
                encoder, feed_dict={image: style_image[np.newaxis, :]})
            content_feature = sess.run(
                encoder, feed_dict={image: content_image[np.newaxis, :]})

            # style_feature and content_feature information
            Bc, Cc, Hc, Wc = content_feature.shape
            Bs, Cs, Hs, Ws = style_feature.shape
            c_feat_rec_HWxC = np.zeros((Hc * Wc, Cc))

            # reshape content feature
            c_feat_HWxC = BxCxHxW_to_HWxC(content_feature)

            # cluster style feature
            s_feat_HWxC = BxCxHxW_to_HWxC(style_feature)
            s_cluster_centers, s_cluster_labels = cluster_feature(
                s_feat_HWxC, n_clusters_s)

            # construct D
            graphPara_D = np.double(
                1 - cosine_similarity(c_feat_HWxC, s_cluster_centers))
            # construct V
            X, Y = np.mgrid[:n_clusters_s, :n_clusters_s]
            graphPara_V = graphPara_smooth * np.float_(np.abs(X - Y))
            # graph cut
            graphPara_sol = fastmin.aexpansion_grid(graphPara_D, graphPara_V,
                                                    graphPara_max_cycles)
            # ST
            for label_idx in range(n_clusters_s):
                print("#%d cluster:" % label_idx)
                # select content feature
                c_subset_index = np.argwhere(
                    graphPara_sol == label_idx).flatten()
                c_subset_sample = c_feat_HWxC[c_subset_index, :]
                c_subset_sample = HWxC_to_BxCxHWxW0(c_subset_sample)
                print("c_subset_sample:", c_subset_sample.shape)
                # select cooresponding style feature
                s_subset_index = np.argwhere(
                    s_cluster_labels == label_idx).flatten()
                s_subset_sample = s_feat_HWxC[s_subset_index, :]
                s_subset_sample = HWxC_to_BxCxHWxW0(s_subset_sample)
                print("s_subset_sample:", s_subset_sample.shape)
                # feature transfer
                t_subset_sample = sess.run(target,
                                           feed_dict={
                                               content: c_subset_sample,
                                               style: s_subset_sample
                                           })

                # target feature subset
                t_subset_sample = BxCxHxW_to_HWxC(t_subset_sample)
                c_feat_rec_HWxC[c_subset_index, :] = t_subset_sample
            # reshape to target feature
            target_feature = HWxC_to_BxCxHxW(c_feat_rec_HWxC, Hc, Wc, Cc)

            # obtain output
            output = sess.run(decoder,
                              feed_dict={
                                  content: content_feature,
                                  target: target_feature
                              })

            filename = '%s_stylized_%s.%s' % (content_name, style_name,
                                              save_ext)
            filename = os.path.join(output_dir, filename)
            save_image(filename, output[0], data_format=data_format)
            print('Output image saved at', filename)
        end_time = time.time()
        print('Total outputs=' + str(total_output_batch) + ', total time=' +
              str(end_time - start_time) + ', average time=' +
              str((end_time - start_time) / total_output_batch))
Exemple #10
0
nodes = g.add_nodes(2)
g.add_edge(nodes[0], nodes[1], 1, 2)
g.add_tedge(nodes[0], 2, 5)
g.add_tedge(nodes[1], 9, 4)

flow = g.maxflow()
print("max flow: {}, node0: {}, node1: {}".format(flow, g.get_segment(nodes[0]), g.get_segment(nodes[1])))
'''
'''img = imread("D:/s5/lena/a2.png")

g = maxflow.Graph[int]()
nodeids = g.add_grid_nodes(img.shape)
g.add_grid_edges(nodeids, 50)
g.add_grid_tedges(nodeids, img, 255 - img)

g.maxflow()
sgs = g.get_grid_segments(nodeids)
print(sgs)

img2 = np.int_(np.logical_not(sgs))
print(img2)
ppl.imshow(img2)
ppl.show()
'''

D = np.asarray([[[5, 1, 5], [5, 5, 1]], [[5, 1, 5], [5, 1, 5]]])

V = np.asarray([[1, 1, 1], [1, 1, 1], [1, 1, 1]])

print(aexpansion_grid(D, V))
Exemple #11
0
                for vv in range(-maximum_movement, maximum_movement + 1):
                    print(vv)
                    for uuuu in range(-maximum_movement, maximum_movement + 1):
                        for vvvv in range(-maximum_movement,
                                          maximum_movement + 1):
                            smoothness_term[(uu + maximum_movement) *
                                            (2 * maximum_movement + 1) + vv +
                                            maximum_movement,
                                            (uuuu + maximum_movement) *
                                            (2 * maximum_movement + 1) + vvvv +
                                            maximum_movement] = (
                                                ((uu - uuuu) * (2**level))**2 +
                                                ((vv - vvvv) * (2**level))**2)

            alpha = 100.0
            labels = aexpansion_grid(data_term,
                                     smoothness_term * alpha)  #  [H, W]
            for iiii in range(labels.shape[0]):
                accumulated_motion_vectors[iiii, 0] += (
                    labels[iiii] //
                    (2 * maximum_movement + 1) - maximum_movement) * (2**level)
                accumulated_motion_vectors[iiii, 1] += (
                    labels[iiii] %
                    (2 * maximum_movement + 1) - maximum_movement) * (2**level)
            print(accumulated_motion_vectors)
        print(accumulated_motion_vectors)
        np.savetxt("motion_vector.csv",
                   accumulated_motion_vectors,
                   delimiter=",")

        loss = 0.0
        for iiii in range(len(large_mask_chain)):