예제 #1
0
파일: 200702.py 프로젝트: wanglaotou/CCYC
def vis_square(data,padsize=1,padval=0):
    data-=data.min()
    data/=data.max()

    n = int(np.ceil(np.sqrt(data.shape[0])))
    print("The n is %.2f"%(n))
    print("The n is %.2f"%(data.ndim))
    padding = ((0,n**2-data.shape[0]),(0,padsize),(0,padsize)) + ((0,0),)*(data.ndim - 3)
    data = np.pad(data,padding,mode='constant',constant_values=(padval,padval))
    data = data.reshape((n,n) + data.shape[1:]).transpose((0,2,1,3)+ tuple(range(4,data.ndim+1)))
    data = data.reshape((n*data.shape[1],n*data.shape[3]) + data.shape[4:])
예제 #2
0
def readMSTARFile(filename):
    # raw_input('Enter the mstar file to read: ')

    # print filename

    f = open(filename, 'rb')

    a = b''

    phoenix_header = []

    tmp = 'PhoenixHeaderVer'
    while tmp.encode() not in a:
        a = f.readline()

    a = f.readline()

    tmp = 'EndofPhoenixHeader'
    while tmp.encode() not in a:
        phoenix_header.append(a)
        a = f.readline()

    data = np.fromfile(f, dtype='>f4')

    # print data.shape

    # magdata = data[:128*128]
    # phasedata = data[128*128:]

    # if you want to print an image
    # imdata = magdata*255

    # imdata = imdata.astype('uint8')

    targetSerNum = '-'

    for line in phoenix_header:
        # print line
        if ('TargetType').encode() in line:
            targetType = line.strip().split(b'=')[1].strip()
        elif ('TargetSerNum').encode() in line:
            targetSerNum = line.strip().split(b'=')[1].strip()
        elif ('NumberOfColumns').encode() in line:
            cols = int(line.strip().split(b'=')[1].strip())
        elif ('NumberOfRows').encode() in line:
            rows = int(line.strip().split(b'=')[1].strip())

    label = targetType  # + '_' + targetSerNum

    roffset = (rows - 128) // 2
    coffset = (cols - 128) // 2
    data = data[:rows * cols]
    data = data.reshape((rows, cols))
    data = data[roffset:(128 + roffset), coffset:(128 + coffset)]

    # plt.imshow(data)
    # plt.show()

    return data.astype('float32'), label, targetSerNum
예제 #3
0
파일: hsv2.py 프로젝트: yaxinshen/test
def vis_square(data):
    data = (data - data.min()) / (data.max() - data.min())

    n = int(np.ceil(np.sqrt(data.shape[0])))
    padding = (((0, n**2 - data.shape[0]), (0, 1),
                (0, 1))  # add some space between filters
               + ((0, 0), ) * (data.ndim - 3)
               )  # don't pad the last dimension (if there is one)
    data = np.pad(data, padding, mode='constant',
                  constant_values=1)  # pad with ones (white)

    data = data.reshape((n, n) + data.shape[1:]).transpose(
        (0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
    data = data.reshape((n * data.shape[1], n * data.shape[3]) +
                        data.shape[4:])

    plt.imshow(data)
    plt.axis('off')
    plt.show()
예제 #4
0
def patch_and_Gnormalize(img, patch_size, verbose=True):
    """1. Extract patches from an image
    2. Cast to a 2D matrix 
    3. normalize the patches like normal r.v.s
    
    Make sure that dtype(img) is float, so that normalization makes sense!
    
    Parameters
	----------
	img: 3D array of float, shape (M, N, C)
		Input data, M rows, N columns, C colour channels
		
	patch_size: 2D tuple, shape (patch_width, patch_height)
		Usually (10, 10)
		
	verbose: controls verbosity. True by default.
		
	Returns
	-------
	patches: 2D array of float, shape (M2, N2)
		M2 ~= M*N, N2 = patch_width * patch_height * C
		M2 will likely be less than M * N because of how extract_patches_2d works
    """
    t0 = time.time()
    if verbose:
        print('image shape = %s' % (img.shape,))
    
	#1. Extract patches
    patches = extract_patches_2d(img, patch_size)
    #2. Cast into 2D
    patches = patches.reshape(patches.shape[0], -1)
    #3. GNormalize
    
    t1 = time.time()
    patch_size = (10,10)
    data = extract_patches_2d(img,patch_size)
    dt = time.time() - t1
    if verbose:
        print('Extracted patches. Current shape = %s' % (data.shape,))
    
    t1 = time.time()
    data = data.reshape(data.shape[0],-1)
    if verbose:
        print('Reshaped patches. Current shape = %s' % (data.shape,))


    data -= np.mean(data, axis = 0)
    data /= np.std(data, axis = 0)
    data[np.isnan(data)] = 0.0
    
    dt = time.time() - t0
    if verbose:
        print('Total time elapsed = %.3fs.' % (time.time() - t0))
        
    return patches
예제 #5
0
def patch_and_Gnormalize(img, patch_size, verbose=True):
    """1. Extract patches from an image
    2. Cast to a 2D matrix 
    3. normalize the patches like normal r.v.s
    
    Make sure that dtype(img) is float, so that normalization makes sense!
    
    Parameters
	----------
	img: 3D array of float, shape (M, N, C)
		Input data, M rows, N columns, C colour channels
		
	patch_size: 2D tuple, shape (patch_width, patch_height)
		Usually (10, 10)
		
	verbose: controls verbosity. True by default.
		
	Returns
	-------
	patches: 2D array of float, shape (M2, N2)
		M2 ~= M*N, N2 = patch_width * patch_height * C
		M2 will likely be less than M * N because of how extract_patches_2d works
    """
    t0 = time.time()
    if verbose:
        print('image shape = %s' % (img.shape, ))

#1. Extract patches
    patches = extract_patches_2d(img, patch_size)
    #2. Cast into 2D
    patches = patches.reshape(patches.shape[0], -1)
    #3. GNormalize

    t1 = time.time()
    patch_size = (10, 10)
    data = extract_patches_2d(img, patch_size)
    dt = time.time() - t1
    if verbose:
        print('Extracted patches. Current shape = %s' % (data.shape, ))

    t1 = time.time()
    data = data.reshape(data.shape[0], -1)
    if verbose:
        print('Reshaped patches. Current shape = %s' % (data.shape, ))

    data -= np.mean(data, axis=0)
    data /= np.std(data, axis=0)
    data[np.isnan(data)] = 0.0

    dt = time.time() - t0
    if verbose:
        print('Total time elapsed = %.3fs.' % (time.time() - t0))

    return patches
예제 #6
0
def loadOutputs():
    global outputsData, trainButton
    outputsPath = askopenfilename() 
    
    archivo = matlabIo.loadmat(outputsPath, mdict=None)
    t       = archivo['allTarge']
    #t       = archivo['Target']
    data    = np.uint8(t)        
    data    = data.reshape(-1)
    
    outputsData = data
    trainButton.config(state=NORMAL)
    print "Target data loaded!" + str(outputsData.shape)
def radial_profile(data, center):
    y, x = np.indices((data.shape[0], data.shape[1]))
    r = np.sqrt((x - center[0])**2 + (y - center[1])**2)
    r = r.astype(np.int)
    r = r.ravel()
    data = data.reshape(-1, data.shape[-1])
    lis = [[] for i in range(r.max())]
    for i in range(len(lis)):
        for j in range(len(r)):
            if r[j] == i:

                lis[i].append(data[j])
            else:
                pass
    return lis
예제 #8
0
    def GetControlledBatch(self,
                           my_angle,
                           my_x,
                           my_y,
                           delta_angle,
                           my_speed,
                           verbose=False):

        start_y, start_x = self.GetControlledTrajectory(
            my_angle=my_angle,
            my_x=my_x,
            my_y=my_y,
            delta_angle=delta_angle,
            my_speed=my_speed)

        # minibatch data
        #     data_downsample = np.zeros((self.batch_size_, self.seq_length_, self.image_size_downsample_, self.image_size_downsample_), dtype=np.float32)
        data = np.zeros((self.batch_size_, self.seq_length_, self.image_size_,
                         self.image_size_),
                        dtype=np.float32)
        data_label = np.zeros(self.batch_size_, dtype=np.float32)
        data_label_onehot = np.zeros(
            (self.batch_size_, self.num_input_labels_), dtype=np.float32)

        for j in range(self.batch_size_):
            # get random digit from dataset
            ind = self.indices_[j]
            digit_image = self.data_[ind, :, :]
            digit_image = rescale(digit_image, 1.0 / 2.0, anti_aliasing=False)

            # generate video
            for i in range(self.seq_length_):
                top = start_y[i, j * self.num_digits_]
                left = start_x[i, j * self.num_digits_]
                bottom = top + self.digit_size_
                right = left + self.digit_size_
                data[j, i, top:bottom, left:right] = self.Overlap(
                    data[j, i, top:bottom, left:right], digit_image)

            data_label_onehot[j,
                              np.where(
                                  self.input_labels_ == self.labels_[ind])] = 1
            data_label[j] = self.labels_[ind]

        return data.reshape(self.batch_size_,
                            -1), data_label, data_label_onehot
예제 #9
0
def loadInputs():
    global inputData, boxSize
    inputsPath = askopenfilename() 
    
    archivo = matlabIo.loadmat(inputsPath, mdict=None)
    image   = archivo['allBlobs']
    #image   = archivo['Data']
    data    = np.uint8(image)
    boxSize = len(data[0][0])
    data    = data.reshape(-1,1,boxSize,boxSize)
                
    inputData = data / np.float32(256)
    
    
    
    boxSizeIndicator.config(state=NORMAL)
    boxSizeIndicator.delete(1.0, END)   
    boxSizeIndicator.insert(END, "Box Size: " + str(boxSize))
    boxSizeIndicator.config(state=DISABLED)
    
    print "Input data loaded!"
예제 #10
0
def show_data(npy_data):
    epoch_size = 300
    width = 28
    length = 28
    depth = 1
    input_data = np.load(npy_data)

    num = input_data[0:epoch_size].shape[0]  # only 1 ~ 1000 to show
    print(input_data[0:epoch_size, 28**2:28**2 + 1])
    image = tf.placeholder(tf.float32, [epoch_size, width, length, depth],
                           name="image")
    tf.summary.image('source_data', image, max_outputs=epoch_size)  # 2

    merged = tf.summary.merge_all()
    writer = tf.summary.FileWriter(current_path + "showDataRow")  # 3
    with tf.Session() as sess:
        for i in range(num // epoch_size):
            data = input_data[i * epoch_size:(i + 1) * epoch_size,
                              0:width * length]
            data = data.reshape(epoch_size, width, length, depth)
            summary = sess.run(merged, feed_dict={image: data})
            writer.add_summary(summary, i)
    writer.close()
예제 #11
0
파일: piro.py 프로젝트: sy9976/piro_proj1
def plt_to_array(fig):
  data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
  data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
  print data
  plt.imshow(data)
  plt.show()
예제 #12
0
파일: mnist.py 프로젝트: zwdnet/mnist
def drawData(data):
    plt.figure()
    plt.imshow(data.reshape(28, 28))
    plt.savefig("./output/num.png")
    plt.close()
예제 #13
0
def load_mnist_images(filename):
    with gzip.open(filename, 'rb') as f:
        data = np.frombuffer(f.read(), np.uint8, offset=16)
        data = data.reshape(-1,784)
        return data
예제 #14
0
    def show(self):
        """Show the data graphically in a new pop-up window."""

        # prevent the following error:
        # '_tkinter.TclError: no display name and no $DISPLAY environment
        #    variable'
        # import matplotlib
        # matplotlib.use('GTK3Agg', warn=False)

        import matplotlib
        matplotlib.use('Agg')
        import matplotlib.pyplot as plt

        pointlist = self.get_pointlist()
        if 'pen_down' in pointlist[0][0]:
            assert len(pointlist) > 1, \
                "Lenght of pointlist was %i. Got: %s" % (len(pointlist),
                                                         pointlist)
            # Create a new pointlist that models pen-down strokes and pen
            # up strokes
            new_pointlist = []
            last_pendown_state = None
            stroke = []
            for point in pointlist[0]:
                if last_pendown_state is None:
                    last_pendown_state = point['pen_down']
                if point['pen_down'] != last_pendown_state:
                    new_pointlist.append(stroke)
                    last_pendown_state = point['pen_down']
                    stroke = []
                else:
                    stroke.append(point)
            new_pointlist.append(stroke)  # add the last stroke
            pointlist = new_pointlist

        _, ax = plt.subplots()
        ax.set_title("Raw data id: %s, "
                     "Formula_id: %s" %
                     (str(self.raw_data_id), str(self.formula_id)))

        colors = _get_colors(self.segmentation)
        for symbols, color_1 in zip(self.segmentation, colors):

            # print "Symbol: {}".format(self.inv_mapping[tuple(symbols)])
            fig = plt.figure()
            ax = fig.add_subplot(111)
            for stroke_index in symbols:
                stroke = pointlist[stroke_index]
                xs, ys = [], []
                for p in stroke:
                    xs.append(p['x'])
                    ys.append(p['y'])
                    ax.plot(xs, ys, color="#000000")
            # if "pen_down" in stroke[0] and stroke[0]["pen_down"] is False:
            #     ax.plot(xs, ys, '-x', color=color)
            # else:
            #     ax.plot(xs, ys, '-x', color=color)
            #  print xs,ys

            # Make a random plot...

            # If we haven't already shown or saved the plot, then we need to
            # draw the figure first...
            plt.gca().invert_yaxis()
            ax.set_aspect('equal')
            plt.axis('off')
            fig.canvas.draw()
            # fig.savefig("test_fig.png")
            np.set_printoptions(threshold=np.nan)
            # Now we can save it to a numpy array.
            #   non_grey = []
            data = np.fromstring(fig.canvas.tostring_rgb(),
                                 dtype=np.uint8,
                                 sep='')
            # print data
            # print fig.canvas.get_width_height()
            # print data.shape
            data = data.reshape(fig.canvas.get_width_height()[::-1] + (3, ))
            data = np.dot(data[..., :3], [0.299, 0.587, 0.114])

            print data.shape
            data_dict = defaultdict(int)
            new_data = np.zeros(data.shape)
            for row in range(len(data)):
                for col in range(len(data[0])):
                    point = data[row][col]
                    #  if point != 191:
                    #       new_data[row][col] = 0
                    if point != 191:
                        new_data[row][col] = 1
                        data_dict[(row, col)] = 1

        #  print list(itertools.chain.from_iterable(new_data))

        #  plt.imshow(new_data, cmap=plt.get_cmap('gray'))
        #  plt.savefig("current_{}".format(symbols))
            break
예제 #15
0
plt.hist(labels, 32)
plt.show()
plt.figure(figsize=(10,10))
legos=[300, 2250, 3650, 4000]
for i in range(len(legos)):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(images[i], cmap=plt.cm.binary)
    plt.xlabel(lego_classes[labels[i]])
plt.show()
# scale the raw pixel intensities to the range [0, 1]
data = np.array(data, dtype="float") / 255.0
print(data.shape)
data64 = data.reshape(data.shape[0],200,200,1)
data64 = np.array(data64)
#Encode labels
en = LabelEncoder()
labels = en.fit_transform(labels)
labels = np.array(labels)


# partition the data into training and testing splits using 75% of
# the data for training and the remaining 25% for testing
(trainX, testX, trainY, testY) = train_test_split(data64,
	labels, test_size=0.25, random_state=42)#Only Used the dataset marked 'Train' my pc has low amounts of RAM



# Define model architecture
예제 #16
0
def plot_traces_v2(strokes):
    """Show the data graphically in a new pop-up window."""

    # prevent the following error:
    # '_tkinter.TclError: no display name and no $DISPLAY environment
    #    variable'
    # import matplotlib
    # matplotlib.use('GTK3Agg', warn=False)

    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    import numpy as np

    plt.rcParams['axes.facecolor'] = 'red'
    x, y = [], []

    fig = plt.figure()
    ax = fig.add_subplot(111)
    for stroke_index in range(len(strokes)):
        stroke = strokes[stroke_index]
        xs, ys = [], []
        for p in stroke:
            xs.append(p['x'])
            ys.append(p['y'])
            ax.plot(xs, ys, color="#000000")

    # If we haven't already shown or saved the plot, then we need to
    # draw the figure first...
    plt.gca().invert_yaxis()
    ax.set_aspect('equal')
    plt.axis('off')

    fig.canvas.draw()
    # fig.savefig("results/"+symbol_str+".png",facecolor=ax.get_facecolor())
    #  np.set_printoptions(threshold=np.nan)
    # Now we can save it to a numpy array.
    #   non_grey = []
    data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
    # print data
    # print fig.canvas.get_width_height()
    # print data.shape
    data = data.reshape(fig.canvas.get_width_height()[::-1] + (3, ))

    #  data = np.dot(data[..., :3], [0.299, 0.587, 0.114])
    # print data.shape
    '''
    data_dict = defaultdict(int)
    new_data = np.zeros(data.shape)
    flipped_data = np.zeros(data.shape)
   # print data
    for row in range(len(data)):
        for col in range(len(data[0])):
            point = data[row][col]
            if point != 255:
                flipped_data[row][col] = 0
            else:
                flipped_data[row][col] = 1

            if point != 255:
                new_data[row][col] = 1
                data_dict[(row, col)] = 1

    '''

    #  blurred = gaussian_filter(new_data, sigma=7)
    # im = Image.fromarray(blurred * 255)
    # im.show()
    # print new_data.shape
    # im = Image.fromarray(new_data*255)
    # im.show()
    # new_data = new_data[:, 100:-100]
    #  flipped_data = flipped_data[:,100:-100]
    #  plt.savefig("results/"+symbol_str)
    image = color.rgb2gray(data)

    fd, hog_image = hog(image,
                        orientations=8,
                        pixels_per_cell=(32, 32),
                        cells_per_block=(1, 1),
                        visualise=True)

    # Rescale histogram for better display
    hog_image_rescaled = exposure.rescale_intensity(hog_image,
                                                    in_range=(0, 0.02))
    hog_np_array = np.asarray(hog_image_rescaled)
    return hog_np_array
예제 #17
0
    def get_training_example(self):
        """Show the data graphically in a new pop-up window."""

        # prevent the following error:
        # '_tkinter.TclError: no display name and no $DISPLAY environment
        #    variable'
        # import matplotlib
        # matplotlib.use('GTK3Agg', warn=False)

        import matplotlib
        matplotlib.use('Agg')
        import matplotlib.pyplot as plt
        plt.rcParams['axes.facecolor'] = 'red'
        from scipy.ndimage.filters import gaussian_filter
        x, y = [], []

        pointlist = self.get_pointlist()
        if 'pen_down' in pointlist[0][0]:
            assert len(pointlist) > 1, \
                "Lenght of pointlist was %i. Got: %s" % (len(pointlist),
                                                         pointlist)
            # Create a new pointlist that models pen-down strokes and pen
            # up strokes
            new_pointlist = []
            last_pendown_state = None
            stroke = []
            for point in pointlist[0]:
                if last_pendown_state is None:
                    last_pendown_state = point['pen_down']
                if point['pen_down'] != last_pendown_state:
                    new_pointlist.append(stroke)
                    last_pendown_state = point['pen_down']
                    stroke = []
                else:
                    stroke.append(point)
            new_pointlist.append(stroke)  # add the last stroke
            pointlist = new_pointlist

        _, ax = plt.subplots()
        ax.set_title("Raw data id: %s, "
                     "Formula_id: %s" %
                     (str(self.raw_data_id), str(self.formula_id)))

        colors = _get_colors(self.segmentation)
        fig = plt.figure()
        for symbols, color_1 in zip(self.segmentation, colors):
            symbol_str = self.inv_mapping[tuple(sorted(symbols))]
            plt.clf()
            ax = fig.add_subplot(111)
            #  fig.set_size_inches([124.0 / 192, 93.0 / 192])
            #  fig.set_size_inches([62.0 / 192, 62.0 / 192])
            for stroke_index in symbols:
                stroke = pointlist[stroke_index]
                xs, ys = [], []
                for p in stroke:
                    xs.append(p['x'])
                    ys.append(p['y'])
                    ax.plot(xs, ys, color="#000000")

            # If we haven't already shown or saved the plot, then we need to
            # draw the figure first...
            plt.gca().invert_yaxis()
            ax.set_aspect('equal')
            plt.axis('off')

            fig.canvas.draw()
            # fig.savefig("results/"+symbol_str+".png",facecolor=ax.get_facecolor())
            #  np.set_printoptions(threshold=np.nan)
            # Now we can save it to a numpy array.
            #   non_grey = []
            data = np.fromstring(fig.canvas.tostring_rgb(),
                                 dtype=np.uint8,
                                 sep='')
            # print data
            # print fig.canvas.get_width_height()
            # print data.shape
            data = data.reshape(fig.canvas.get_width_height()[::-1] + (3, ))

            #  data = np.dot(data[..., :3], [0.299, 0.587, 0.114])
            # print data.shape
            '''
            data_dict = defaultdict(int)
            new_data = np.zeros(data.shape)
            flipped_data = np.zeros(data.shape)
           # print data
            for row in range(len(data)):
                for col in range(len(data[0])):
                    point = data[row][col]
                    if point != 255:
                        flipped_data[row][col] = 0
                    else:
                        flipped_data[row][col] = 1

                    if point != 255:
                        new_data[row][col] = 1
                        data_dict[(row, col)] = 1

            '''

            #  blurred = gaussian_filter(new_data, sigma=7)
            # im = Image.fromarray(blurred * 255)
            # im.show()
            # print new_data.shape
            # im = Image.fromarray(new_data*255)
            # im.show()
            # new_data = new_data[:, 100:-100]
            #  flipped_data = flipped_data[:,100:-100]
            #  plt.savefig("results/"+symbol_str)
            image = color.rgb2gray(data)

            fd, hog_image = hog(image,
                                orientations=8,
                                pixels_per_cell=(32, 32),
                                cells_per_block=(1, 1),
                                visualise=True)

            # Rescale histogram for better display
            hog_image_rescaled = exposure.rescale_intensity(hog_image,
                                                            in_range=(0, 0.02))
            hog_np_array = np.asarray(hog_image_rescaled)
            #  plt.imshow(hog_image_rescaled, cmap=plt.cm.gray)
            #  plt.show()
            #  plt.savefig("hog_image_{}.png".format(symbol_str))
            x.append(hog_np_array)
            y.append(symbol_str)

        return x, y
예제 #18
0
    def get_training_example_v4(self, dir):
        """Show the data graphically in a new pop-up window."""

        # prevent the following error:
        # '_tkinter.TclError: no display name and no $DISPLAY environment
        #    variable'
        # import matplotlib
        # matplotlib.use('GTK3Agg', warn=False)

        import matplotlib
        matplotlib.use('Agg')
        import matplotlib.pyplot as plt
        from scipy.ndimage.filters import gaussian_filter
        x, y = [], []

        pointlist = self.get_pointlist()
        if 'pen_down' in pointlist[0][0]:
            assert len(pointlist) > 1, \
                "Lenght of pointlist was %i. Got: %s" % (len(pointlist),
                                                         pointlist)
            # Create a new pointlist that models pen-down strokes and pen
            # up strokes
            new_pointlist = []
            last_pendown_state = None
            stroke = []
            for point in pointlist[0]:
                if last_pendown_state is None:
                    last_pendown_state = point['pen_down']
                if point['pen_down'] != last_pendown_state:
                    new_pointlist.append(stroke)
                    last_pendown_state = point['pen_down']
                    stroke = []
                else:
                    stroke.append(point)
            new_pointlist.append(stroke)  # add the last stroke
            pointlist = new_pointlist

        _, ax = plt.subplots()
        ax.set_title("Raw data id: %s, "
                     "Formula_id: %s" %
                     (str(self.raw_data_id), str(self.formula_id)))

        colors = _get_colors(self.segmentation)
        fig = plt.figure()
        for symbols, color_1 in zip(self.segmentation, colors):
            symbol_str = self.inv_mapping[tuple(sorted(symbols))]
            plt.clf()
            ax = fig.add_subplot(111)
            #  fig.set_size_inches([62.0 / 192, 62.0 / 192])
            for stroke_index in symbols:
                stroke = pointlist[stroke_index]
                xs, ys = [], []
                for p in stroke:
                    xs.append(p['x'])
                    ys.append(p['y'])
                    ax.plot(xs, ys, color="#000000")

            # If we haven't already shown or saved the plot, then we need to
            # draw the figure first...
            plt.gca().invert_yaxis()
            ax.set_aspect('equal')
            plt.axis('off')

            fig.canvas.draw()
            # fig.savefig("results/"+symbol_str+".png",facecolor=ax.get_facecolor())
            #  np.set_printoptions(threshold=np.nan)
            # Now we can save it to a numpy array.
            #   non_grey = []
            data = np.fromstring(fig.canvas.tostring_rgb(),
                                 dtype=np.uint8,
                                 sep='')
            plt.clf()
            # print data
            # print fig.canvas.get_width_height()
            # print data.shape
            data = data.reshape(fig.canvas.get_width_height()[::-1] + (3, ))
            image = color.rgb2gray(data)
            fd, hog_image = hog(image,
                                orientations=8,
                                pixels_per_cell=(32, 32),
                                cells_per_block=(1, 1),
                                visualise=True)

            # Rescale histogram for better display
            hog_image_rescaled = exposure.rescale_intensity(hog_image,
                                                            in_range=(0, 0.02))
            hog_np_array = np.asarray(hog_image_rescaled)
            print symbol_str
            if symbol_str == '/':
                symbol_str = 'forward_slash'

            plt.imshow(hog_np_array, cmap='gray')
            filename = dir + "_" + self.filename + symbol_str + '.png'
            plt.savefig(filename, format='png', facecolor=ax.get_facecolor())
            #print hog_np_array.shape
            x.append(filename)
            y.append(symbol_str)
예제 #19
0
# downsample for higher speed
face = face[::4, ::4] + face[1::4, ::4] + face[::4, 1::4] + face[1::4, 1::4]
face /= 4.0
height, width = face.shape

# Distort the right half of the image
print('Distorting image...')
distorted = face.copy()
distorted[:, width // 2:] += 0.075 * np.random.randn(height, width // 2)

# Extract all reference patches from the left half of the image
print('Extracting reference patches...')
t0 = time()
patch_size = (7, 7)
data = extract_patches_2d(distorted[:, :width // 2], patch_size)
data = data.reshape(data.shape[0], -1)
data -= np.mean(data, axis=0)
data /= np.std(data, axis=0)
print('done in %.2fs.' % (time() - t0))

# #############################################################################
# Learn the dictionary from reference patches

print('Learning the dictionary...')
t0 = time()
dico = MiniBatchDictionaryLearning(n_components=100, alpha=1, n_iter=500)
V = dico.fit(data).components_
dt = time() - t0
print('done in %.2fs.' % dt)

plt.figure(figsize=(4.2, 4))
예제 #20
0
import os
import numpy as np
from skimage import io, data, measure, color, filters, morphology, segmentation
from PIL import Image
import matplotlib.pyplot as plt

data_num = 10000
fig_w = 45

data = np.fromfile("mnist_test_data0", dtype=np.uint8)
label = np.fromfile("mnist_test_label", dtype=np.uint8)

print(data.shape)
print(label.shape)

data = data.reshape(data_num, fig_w, fig_w)

print("After reshape:", data.shape)
imagenum = data.shape[0]

resfile = np.zeros([data_num, fig_w, fig_w], dtype=np.uint8)
for i in range(imagenum):
    print(i)
    img = data[i]
    m, n = img.shape

    bw = np.zeros([m, n])
    for k in range(m):
        for j in range(n):
            if img[k][j] > 0:
                bw[k][j] = 1
#china  = imageio.imread('custom_image.jpg')
from sklearn.datasets import load_sample_image
### loading image sample from avaliable dataset
Image = load_sample_image('flower.jpg')

### used to get the pixel value of image
print(Image.shape)

### Original picture
#io.imshow(Image)
#plt.show()

### Normaliazing the original image
data = Image / 255.0
###  converting  3D matrix into 2D
data = data.reshape(640 * 427, 3)

import warnings
warnings.simplefilter('ignore')
### import clustering from scikit
from sklearn.cluster import MiniBatchKMeans
### can be compressed further by incrasing the number of the cluster
kmean = MiniBatchKMeans(8)
### fitting the data into Kmean
kmean.fit(data)
#### clustering the datapoints
new_colors = kmean.cluster_centers_[kmean.predict(data)]
#### converting back to  3D matrix
Image_recoloyred = new_colors.reshape(Image.shape)

### plotting the output
예제 #22
0
data = mnist.data[::30]
target = mnist.target[::30]

model = Isomap(n_components=2)
proj = model.fit_transform(data)
plt.scatter(proj[:, 0], proj[:, 1])

# %%
data = mnist.data[mnist.target == 1][::4]

fig, ax = plt.subplots(figsize=(10, 10))
model = Isomap(n_neighbors=5, n_components=2, eigen_solver="dense")
plot_components(
    data,
    model,
    images=data.reshape((-1, 28, 28)),
    ax=ax,
    thumb_frac=0.05,
    cmap="gray_r",
)

# %%
X, y_true = make_blobs(n_samples=300,
                       centers=4,
                       cluster_std=0.6,
                       random_state=0)
plt.scatter(X[:, 0], X[:, 1], s=50)

# %%
kmeans = KMeans(n_clusters=4)
kmeans.fit(X)