Build the feature matrix and label vector: - python

I have a dataset “Digit” . The dataset includes 1797 small images (8x8 pixels), each one includes a hand-written digit (0-9). Each image is considered as a data sample with pixels as features. Thus, to build the feature table you have to convert each 8x8 image into a row of the feature matrix with 64 feature columns for 64 pixels. How to build a feature matrix and label vector for it ???

You can follow the scikit-learn tutorial on supervised learning, where they are using the Digit dataset
http://scikit-learn.org/stable/tutorial/basic/tutorial.html#loading-an-example-dataset
with more detail here. If you load the dataset as in the example, you can simple reshape the images:
from sklearn import datasets
digits = datasets.load_digits()
# To apply a classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))
This makes data a 2D matrix, with n_samples rows and as many columns as needed to fit the flattened image.

If you're using numpy and cv2 you can do the following:
import numpy as np
import cv2
fname = "image1.jpg"
image = cv2.imread(fname) # (8, 8, 1)
feature = image.reshape(64) # (64,)
to read a bunch of images and load into a 'feature matrix' (a numpy array) you can do the following:
N = 10 # number of images
data = np.zeros((N, 64))
for index in range(N):
# get the current image and convert to feature, as above
data[index] = np.copy(feature)
Each row of your data matrix is now one example (a 64 dim list of features).
Does this help?
The label vector can just be a 1D numpy array, i.e. labels = np.zeros(N)
EDIT:
There are a number of ways to read images:
(1) img = cv2.imread(filename)
(2) using matplotlib:
import matplotlib.image as mpimg
img = mpimg.imread(filename)
(3) using PIL (or PILLOW):
from PIL import Image
img = Image.open(filename)
It pays to check the shape of the image after it has been read, so that you know it is in the correct channel, width, height order that is appropriate for your application.

Related

How to convert data array to show easy to understand results

I tried to make a algorithm using Teachable Machine to receive a picture and see if it fall under one of two categories of pictures (e.g dogs or humans), but after I exported the code that was given I couldn't make sense of how I could make the results that were given via array to turn into something that anyone can understand. So far it only shows a list of two numbers (e.g [[0.00058185 0.99941814]] the first number being dogs and the second one humans) I wanted to make it to show which one of the two numbers means dog and human and the percentage of both or to make it to only shows which one is the most probable to be.
Here's the code:
import tensorflow.keras
from PIL import Image, ImageOps
import numpy as np
from decimal import Decimal
# Disable scientific notation for clarity
np.set_printoptions(suppress=True)
# Load the model
model = tensorflow.keras.models.load_model('keras_model.h5')
# Create the array of the right shape to feed into the keras model
# The 'length' or number of images you can put into the array is
# determined by the first position in the shape tuple, in this case 1.
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
# Replace this with the path to your image
image = Image.open('test_photo.jpg')
#resize the image to a 224x224 with the same strategy as in TM2:
#resizing the image to be at least 224x224 and then cropping from the center
size = (224, 224)
image = ImageOps.fit(image, size, Image.ANTIALIAS)
#turn the image into a numpy array
image_array = np.asarray(image)
# display the resized image
image.show()
# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
# Load the image into the array
data[0] = normalized_image_array
# run the inference
prediction = model.predict(data)
print(prediction)
input('Press ENTER to exit')
Using argmax and max does what you want:
"Prediction is {} with {}% probability".format(["dog", "human"][np.argmax(prediction)], round(np.max(prediction)*100,2))
'Prediction is human with 99.94% probability'

Show image from fetched data using openCV

I've been using datasets from sklearn. And I want to show image from 'MNIST original' using openCV.imshow
Here is part of my code
dataset = datasets.fetch_mldata('MNIST original')
features = np.array(dataset.data, 'int16')
labels = np.array(dataset.target, 'int')
list_hog_fd = []
deskewed_images = []
for img in features:
cv2.imshow("digit", img)
deskewed_images.append(deskew(img))
"digit" window appears but it is definitely not an digit image. How can I access real image from dataset?
Shape
MNIST image datasets generally are distributed and used as a 1D vector of 784 values.
However, in order to show it as image, you need to convert it to a 2D matrix with 28*28 values.
Simply using img = img.reshape(28,28) might work in your case.

How do I format a dataset for training in Python?

How do I format a dataset for training in Python?
I have 3000 grayscale BMP images of some handwritten digits (just like MNIST). Now I want to train my model with this dataset (I am using the Keras library) and I am using a convolution neural network for training.
I am using this code to convert one of the images into array
`import numpy
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
img = Image.open(open('CMATERdb 3.3.1\Ad02599.bmp')).convert("L")
print(img.format, img.size, img.mode)
img = numpy.asarray(img) / 255.
imgplot = plt.imshow(img)`
and the result from the code was
None (32, 32) L
image from the 3000 image want to convert into dataset
Any help how I can convert all images and put them in the same MNIST datast format that will be highly appreciated.
You can use any library that loads image files into arrays, such as Pillow.
Read Pillow's documentation to learn how to load an image file into an array.
Then, you should usually scale the array into values between 0 and 1. Usually, you just divide the image array by 255 (because they are RGB values between 0 and 255).
Be sure to end up with an array shaped like this: (3000, width, heigth, channels), where channels is usually 3 (Red, green, blue).

loading an image from cifar-10 dataset

I am using cifar-10 dataset for my training my classifier. I have downloaded the dataset and tried to display am image from the dataset. I have used the following code:
from six.moves import cPickle as pickle
from PIL import Image
import numpy as np
f = open('/home/jayanth/udacity/cifar-10-batches-py/data_batch_1', 'rb')
tupled_data= pickle.load(f, encoding='bytes')
f.close()
img = tupled_data[b'data']
single_img = np.array(img[5])
single_img_reshaped = single_img.reshape(32,32,3)
plt.imshow(single_img_reshaped)
the description of data is as follows:
Each array stores a 32x32 colour image. The first 1024 entries contain the red channel values, the next 1024 the green, and the final 1024 the blue. The image is stored in row-major order, so that the first 32 entries of the array are the red channel values of the first row of the image.
Is my implementation correct?
the above code gave me the following image:
I used
single_img_reshaped = np.transpose(np.reshape(single_img,(3, 32,32)), (1,2,0))
to get the correct format in my program.
Since Python uses the default C-like indexing order (row-major order), it can be forced to work in column-major order:
import numpy as np
import matplotlib.pyplot as plt
# I assume you have loaded your data into x_train (see some tutorial)
data = x_train[0, :] # get a row data
data = np.reshape(data, (32,32,3), order='F' ) # Fortran-like indexing order
plt.imshow(data)
single_img_reshaped = single_img.reshape(3,32,32).transpose([1, 2, 0])

Visualize MNIST dataset using OpenCV or Matplotlib/Pyplot

i have MNIST dataset and i am trying to visualise it using pyplot. The dataset is in cvs format where each row is one image of 784 pixels. i want to visualise it in pyplot or opencv in the 28*28 image format. I am trying directly using :
plt.imshow(X[2:],cmap =plt.cm.gray_r, interpolation = "nearest")
but i its not working? any ideas on how should i approach this.
Assuming you have a CSV file with this format, which is a format the MNIST dataset is available in
label, pixel_1_1, pixel_1_2, ...
Here's how you can visulize it in Python with Matplotlib and then OpenCV
Matplotlib / Pyplot
import numpy as np
import csv
import matplotlib.pyplot as plt
with open('mnist_test_10.csv', 'r') as csv_file:
for data in csv.reader(csv_file):
# The first column is the label
label = data[0]
# The rest of columns are pixels
pixels = data[1:]
# Make those columns into a array of 8-bits pixels
# This array will be of 1D with length 784
# The pixel intensity values are integers from 0 to 255
pixels = np.array(pixels, dtype='uint8')
# Reshape the array into 28 x 28 array (2-dimensional array)
pixels = pixels.reshape((28, 28))
# Plot
plt.title('Label is {label}'.format(label=label))
plt.imshow(pixels, cmap='gray')
plt.show()
break # This stops the loop, I just want to see one
OpenCV
You can take the pixels numpy array from above which is of dtype='uint8' (unsigned 8-bits integer) and shape 28 x 28 , and plot with cv2.imshow()
title = 'Label is {label}'.format(label=label)
cv2.imshow(title, pixels)
cv2.waitKey(0)
cv2.destroyAllWindows()
Importing necessary packages
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
Reading mnist train dataset ( which is csv formatted ) as a pandas dataframe
s = pd.read_csv("mnist_train.csv")
Converting the pandas dataframe to a numpy matrix
data = np.matrix(s)
The first column contains the label, so store it in a separate array
output = data[:, 0]
And delete the first column from the data matrix
data = np.delete(data, 0, 1)
The first row represents the first image, it is 28X28 image (stored as 784 pixels)
img = data[0].reshape(28,28)
# And displaying the image
plt.imshow(img, cmap="gray")
For all like me who want a quick and dirty solution, simply to get a rough idea what a given input is about, in-console and without fancy libraries:
def print_greyscale(pixels, width=28, height=28):
def get_single_greyscale(pixel):
val = 232 + round(pixel * 23)
return '\x1b[48;5;{}m \x1b[0m'.format(int(val))
for l in range(height):
line_pixels = pixels[l * width:(l+1) * width]
print(''.join(get_single_greyscale(p) for p in line_pixels))
(expects the input to be shaped like [784] and with float values from 0 to 1. If either is not the case, you can easily convert (e.g. pixels = pixels.reshape((784,)) or pixels \= 255)
The output is a bit distorted but you get the idea.

Categories

Resources