Exemplo n.º 1
0
import pickle
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
import matplotlib.pyplot as plt
import numpy as np
from keras.utils import np_utils
from sklearn.model_selection import train_test_split
import dev_constants as dev
import load_pickle_database as pdb
dir = dev.MY_PROJECT_PATH + '\\ConvolutionalNeuralNets\\'

path = dir + 'singlechar_database.p'

images, features = pdb.load_images_labels(path)
# convert all chars to integers
labels = np.array([ord(i) for i in features])
labels = labels - 65
y = np_utils.to_categorical(labels)
print(y)
print(y.shape)
#categorize the features

## process images
## convert X to flaots
# images = images.astype('float32');
# images = images/255.0;
X = images
# X = X[:,:,:,0:2];
X = X[:, :, :, 0]
X = np.reshape(X, (50000, 60, 40, 1))
Exemplo n.º 2
0
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA

import load_pickle_database

[dataset_read, label_read] = load_pickle_database.load_images_labels(
    "/Users/yiliu/Box Sync/Stanford/Classes/2017-2018 Fall/CS 229 Machine Learning/Project/CAPTCHA Database/Simple single letter dataset/50000_single_letter.p"
)
print(dataset_read.shape)
print(label_read.shape)

nTrainPrcntg = 1
nTrain = int(nTrainPrcntg * dataset_read.shape[0])
trainData0 = dataset_read[0:nTrain, :]
trainLabel = label_read[0:nTrain, ]
testData0 = dataset_read[nTrain:, :]
testLabel = label_read[nTrain:, ]

#Perform PCA
PCAcontrol = 1
if PCAcontrol == 1:
    pca = PCA()
    #     pca = PCA(n_components = 300)
    trainData = pca.fit_transform(trainData0)
else:
    trainData = trainData0
    testData = testData0

#K-means clustering
Exemplo n.º 3
0
## kmeans clustering of images
from sklearn.cluster import KMeans
import load_pickle_database as ldp
import numpy as np
import dev_constants as dev
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split;
from sklearn.decomposition import PCA

kmeans = KMeans(n_clusters=26, random_state=0, verbose=1);

## import a dataset;
filepath = dev.MY_PROJECT_PATH+'\\ConvolutionalNeuralNets\\singlechar_database.p';
images,labels = ldp.load_images_labels(filepath);
print(images.shape)
num_samples = 10000;
## generate a representative sample
samples = np.random.randint(0,50000,num_samples);
labels = np.array([ord(i) for i in labels]);
labels = labels - 65;
labels = labels[samples];
X = images[samples,:,:,1];
print(X.shape)
X = np.reshape(X, (num_samples, 2400));
pca = PCA(n_components=2)
X_red = pca.fit_transform(X);
print(X_red.shape)
plt.figure()
plt.scatter(X_red[:,0], X_red[:,1], c = labels );
plt.show();
Exemplo n.º 4
0
## import a saved model and then test
from keras.models import load_model
import dev_constants as dev
import load_pickle_database as ldp
from keras.utils import np_utils
from sklearn.model_selection import train_test_split
import numpy as np

## load a prediction/generalization set
file = dev.MY_PROJECT_PATH + '\\SingleCharOverlay\\overlaychar_database.p'
file = dev.MY_PROJECT_PATH + '\\NeuralNetTesting\\singlechar_test_database.p'

images, features = ldp.load_images_labels(file)
y = features
X = images

X = X[:, :, :, 0]
X = np.reshape(X, (1580, 60, 40, 1))

#y = y[:,1]; #switch this between 0 an d1
labels = np.array([ord(i) for i in y])
labels = labels - 65
y = np_utils.to_categorical(labels)

## load model
model = dev.MY_PROJECT_PATH + '\\NeuralNetTesting\\single_char_CNN.h5'
single_model = load_model(model)

print(single_model.evaluate(X, y))