I have a features extraction algorithm that works well to extract features from images. I want to develop an ANN to classify those images based on those features. I have extracted features in a csv file as columns and rows. I know we can use CNN to extract and classify images but my scerio is only the second half (features classification). Thanks
1 Answers
If you have already extracted a feature vector $X$ from your images. Then indeed you can use an artificial neural network (ANN) to classify these given you have labelled instances.
You can do this in python using multiple different libraries such as scikit-learn, Keras, or tensorflow quite easily. Scikit-learn is the easiest to implement but the least flexible. Keras allows for more complex ANNs, and tensorflow allows for every kind of custom structure you can imagine.
Scikit-learn
This is the easiest way to implement a single-layer ANN.
from sklearn.neural_network import MLPClassifier
X = [[0., 0.], [1., 1.]]
y = [0, 1]
clf = MLPClassifier(solver='adam', alpha=1e-5,
hidden_layer_sizes=(32,), random_state=1)
clf.fit(X, y)
clf.score(X, y)
Keras
First we build a model we want to use. This is an example of a simple single-layered neural network. $k$ is the number of features per instance and $num_classes$ is the number of output classes you are expecting.
from __future__ import print_function
import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten
from keras.models import model_from_json
from keras import backend as K
input_shape = (k,)
model = Sequential()
model.add(Dense(32, activation='tanh',
input_shape=input_shape))
model.add(Dense(num_classes, activation='linear'))
model.compile(loss=keras.losses.mean_squared_error,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
You can see the summary of the model by using
model.summary()
Then we can train this model using
batch_size = 128
epochs = 10
model.fit(x_train, y_train_binary,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test_binary))
You will need to tune all the hyper-parameters in this code sample to better suit your data.
- 8,746
- 28
- 45
-
1Thanks for your great answer. You have directed me to the right direction. Now, I'm exploring https://keras.io/examples/structured_data/structured_data_classification_from_scratch/ to find a suitable solution – DevLoverUmar Jun 18 '20 at 05:34