示例#1
0
def displayData(X, example_width):

    example_width = round(math.sqrt(X.shape[1]))

    m = X.shape[0]
    n = X.shape[1]
    example_height = int((n / example_width))

    # Compute number of items to display
    display_rows = math.floor(math.sqrt(m))
    display_cols = math.ceil(m / display_rows)

    # Between images padding
    pad = 1

    # Setup blank display
    w1 = pad + display_rows * (example_height + pad)
    h1 = int(pad + display_cols * (example_width + pad))
    display_array = -np.ones(shape=(w1, h1))

    #display_array[0:32,0:32]=X[0, :].reshape( example_height, example_width,order='F') #/ max_val;
    #display_array[0:32,32:64]=X[1, :].reshape( example_height, example_width,order='F')

    # Copy each example into a patch on the display array
    curr_ex = 0
    for j in range(1, display_rows + 1):
        for i in range(1, display_cols + 1):
            max_val = max(abs(X[curr_ex, :]))
            row0 = pad + (j - 1) * (example_height + pad)
            row1 = pad + (j - 1) * (example_height + pad) + example_height
            col0 = pad + (i - 1) * (example_width + pad)
            col1 = pad + (i - 1) * (example_width + pad) + example_width
            display_array[row0:row1, col0:col1] = X[curr_ex, :].reshape(
                example_height, example_width, order='F') / max_val

            curr_ex = curr_ex + 1

    # Display Image
    h = io.imshow(display_array, cmap='gray')
    io.show()

    return h, display_array
labels = np.array(labels)

path_converted = 'D:\\documents\\Downloads\\nyu_depth'
if not os.path.isdir(path_converted):
    os.makedirs(path_converted)
for i in range(len(labels)):
    depth = labels[i]
    depth_ = np.empty([480, 640, 3])
    depth_[:, :, 0] = depth[:, :].T
    depth_[:, :, 1] = depth[:, :].T
    depth_[:, :, 2] = depth[:, :].T
    depth_ = depth_ / 4.0
    #depth_1 = Image.fromarray(np.uint8(depth_))
    iconpath = 'D:\\documents\\Downloads\\nyu_depth\\' + str(i) + '.mat'
    io.savemat(iconpath, {'depth_': depth_})
"""
import skimage.io as io
import numpy as np
import h5py

# data path
path_to_depth = 'D:\\documents\\Downloads\\nyu_depth_v2_labeled.mat'

# read mat file
f = h5py.File(path_to_depth)

# read 0-th image. original format is [3 x 640 x 480], uint8
img = f['images'][0]

# reshape
img_ = np.empty([480, 640, 3])
# calculate the test error
prediction_test = net(Variable(ramandata_test).unsqueeze(1))
prediction2_test = torch.max(F.softmax(prediction_test), 1)[1]
error1_test = np.absolute(ramanlabel_test.numpy().squeeze() -
                          prediction2_test.data.numpy().squeeze())
error1_test = error1_test[np.nonzero(error1_test)]
print('\nNumber of test error : ', np.size(error1_test), ' / ',
      num1_test + num2_test + num3_test)

# generate classification map
RamanLine = np.zeros((400 * 400, 60))
RamanStack = io.imread('raman_1.tif')
cnt = -1
for h1 in range(400):
    for h2 in range(400):
        cnt = cnt + 1
        RamanLine[cnt, :] = RamanStack[:, h1, h2]
ramandata = (torch.from_numpy(RamanLine)).type(torch.FloatTensor)
prediction = net(Variable(ramandata).unsqueeze(1))
RGB1 = F.softmax(prediction)
RGB1 = 255 * RGB1.data.numpy()
RGB1 = np.uint8(RGB1)
RGBdisp = np.uint8(np.zeros((400, 400, 3)))
cnt = -1
for h1 in range(400):
    for h2 in range(400):
        cnt = cnt + 1
        RGBdisp[h1, h2, :] = RGB1[cnt, :]
io.imshow(RGBdisp)
RGBsave = Image.fromarray(RGBdisp)
RGBsave.save('conv_classification_1.tif')
示例#4
0
def ex7_Kmeans():  # main function
    #################-1-
    print('Finding closest centroids.')
    mat = scipy.io.loadmat('ex7data2.mat')
    X = mat['X']

    K = 3
    # 3 Centroids
    initial_centroids = np.array([[3, 3], [6, 2], [8, 5]])

    # Find the closest centroids for the examples using the initial_centroids
    idx = findClosestCentroids(X, initial_centroids)
    print('Closest centroids for the first 3 examples:', idx[0:3])
    print('(the closest centroids should be 1, 3, 2 respectively)')

    #################-2-
    print('Computing centroids means.')

    #  Compute means based on the closest centroids found in the previous part.
    centroids = computeCentroids(X, idx, K)

    print('Centroids computed after initial finding of closest centroids:',
          centroids)
    print('(the centroids should be')
    print('   [ 2.428301 3.157924 ]\n')
    print('   [ 5.813503 2.633656 ]\n')
    print('   [ 7.119387 3.616684 ]\n\n')

    ################-3-
    print('Running K-Means clustering on example dataset.')

    K = 3
    max_iters = 10
    initial_centroids = np.array([[3, 3], [6, 2], [8, 5]])
    [centroids, idx] = runkMeans(X, initial_centroids, max_iters, True)

    print('K-Means Done.')

    ###############-4-
    A = io.imread('bird_small.png')
    A = A / 255
    img_size = A.shape
    X = np.reshape(A, (img_size[0] * img_size[1], 3))
    K = 16
    max_iters = 10
    initial_centroids = kMeansInitCentroids(X, K)
    [centroids, idx] = runkMeans(X, initial_centroids, max_iters, False)
    io.imshow(A)

    ##############-5-

    print('Applying K-Means to compress an image.')
    #print('centroids:',centroids)
    idx1 = findClosestCentroids(X, centroids)
    #print('idx1:',idx1[0:10])
    #print('idx1 sh:',idx1.shape)

    X_recovered = [
    ]  #np.zeros(shape=(idx1.shape[0]))  # 16384 e 3 olması lazım
    for i in range(0, idx1.shape[0]):  # 16384
        X_recovered.append(centroids[int(idx1[i][0])])

    X_recovered = np.reshape(X_recovered, (img_size[0], img_size[1], 3))

    # Display the original image
    io.imshow(A)  #imagesc(A);
    #io.title("Original")
    io.show()

    # Display compressed image side by side
    #io.title('Compressed, with',K,'colors.')
    io.imshow(X_recovered)  #imagesc(X_recovered)
    io.show()

    ###############-6- out of homework. Doing same job by python class
    print('Image compression by K-means (sklearn) python class..')
    kmeans = KMeans(init="random",
                    n_clusters=K,
                    n_init=10,
                    max_iter=max_iters,
                    random_state=42)

    kmeans.fit(X)  # X : pixels of image.

    print('kmeans.inertia_:', kmeans.inertia_)
    print(
        'kmeans.cluster_centers_:',
        kmeans.cluster_centers_)  # this is same with centroids variable on up.
    print('kmeans.n_iter_:', kmeans.n_iter_)
    print('kmeans.labels_[:5]', kmeans.labels_[:10])
    print('kmeans.labels_:', kmeans.labels_.shape)

    y_kmeans = kmeans.predict(X)  # y_kmeans is same with idx1 variable on up.

    print('y_kmeans:', y_kmeans.shape)
    print('y_kmeans 0-10:', y_kmeans[0:10])

    X_recovered2 = []
    for i in range(0, y_kmeans.shape[0]):  # 16384
        X_recovered2.append(kmeans.cluster_centers_[int(y_kmeans[i])])

    X_recovered2 = np.reshape(X_recovered2, (img_size[0], img_size[1], 3))
    io.imshow(X_recovered2)
    io.show()
示例#5
0
plt.title('kmeans_labels')
plt.show()

# ploted the points along with the centroid coordinates of each cluster to see how the centroid positions effects clustering
print "\nCentroid position "
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap='rainbow')
plt.scatter(kmeans.cluster_centers_[:, 0],
            kmeans.cluster_centers_[:, 1],
            color='black')
plt.title('kmeans_cluster_centers')
plt.show()
print "\nDone with Apply K-Means classifier on ex6data1.mat\n"

print('\nRunning K-Means clustering on pixels from an image.\n\n')
image = io.imread('bird_small.png')
io.imshow(image)
plt.title('original_bird_small_image')
io.show()

rows = image.shape[0]
cols = image.shape[1]
image = image.reshape(image.shape[0] * image.shape[1], 3)

#  kmeans algorithms with with 16 colors and max iter 10
kmeans = KMeans(n_clusters=128, n_init=10, max_iter=10)
kmeans.fit(image)

clusters = np.asarray(kmeans.cluster_centers_, dtype=np.uint8)
labels = np.asarray(kmeans.labels_, dtype=np.uint8)
labels = labels.reshape(rows, cols)
import matplotlib.image as mpimg
from skimage import data, io
from matplotlib import pyplot as plt

for i in range(300):
    plt.imshow(Image.open(img_paths[i]))
    gt_file = h5py.File(
        img_paths[i].replace('.jpg', '.h5').replace('images', 'ground_truth'),
        'r')
    groundtruth = np.asarray(gt_file['density'])
    plt.imshow(groundtruth, cmap=CM.jet)
    tup = (np.sum(groundtruth))
    #print(np.sum(groundtruth))
    cv2.putText(img, 'Count = ' + str(tup), (10, 400),
                cv2.FONT_HERSHEY_SIMPLEX, 1.25, (255, 255, 0), 2, cv2.LINE_AA)
    io.imshow(img_paths[i])
    plt.show()
    print(np.sum(groundtruth))
"""The above outputs show the estimated numbers of people in the crowd

WE NEXT DO THE SAME PROCESS FOR PART B OF THE SHANGHAI DATASET
"""

#for partB

path_sets = [part_B_train, part_B_test]
img_paths = []
for path in path_sets:
    for img_path in glob.glob(os.path.join(path, '*.jpg')):
        img_paths.append(img_path)