I would classify images with HBA-SVM but i have errors like this. My code on below.
x_train = []
y_train = []
x_test = []
y_test = []
for feature, label in train:
x_train.append(feature)
y_train.append(label)
for feature, label in test:
x_test.append(feature)
y_test.append(label)
Then normalize the data
x_train = np.array(x_train) / 255
x_test = np.array(x_test) / 255
Then, resize the data
# resize data
x_train = x_train.reshape(-1, img_size, img_size, 1) #img_size = 150
y_train = np.array(y_train)
x_test = x_test.reshape(-1, img_size, img_size, 1)
y_test = np.array(y_test)
then, aument the data using ImageDataGenerator
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range = 30, # randomly rotate images in the range (degrees, 0 to 180)
zoom_range = 0.2, # Randomly zoom image
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip = True, # randomly flip images
vertical_flip=False) # randomly flip images
datagen.fit(x_train)
Then, set hyperparameter of SVM
param_grid = {
'C': [0.1, 1, 10, 100, 1000],
'gamma': [1, 0.1, 0.01, 0.001, 0.0001],
'kernel': ['rbf', 'poly', 'linear', 'sigmoid']
}
clf = SVC(random_state=42)
algorithm = HybridBatAlgorithm()
algorithm.set_parameters(NP=50, Ts=5, Mr=0.25)
After that, i would search the best hyperparameter
nia_search = NatureInspiredSearchCV(
clf,
param_grid,
algorithm=algorithm,
population_size=50,
max_n_gen=100,
max_stagnating_gen=20,
runs=3,
)
nia_search.fit(x_train, y_train)
But, my code resulted the error (on pict) like this ..
FitFailedWarning)
/usr/local/lib/python3.7/dist-packages/sklearn/model_selection/_validation.py:619: FitFailedWarning: Estimator fit failed. The score on this train-test partition for these parameters will be set to nan. Details:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/sklearn/model_selection/_validation.py", line 598, in _fit_and_score
estimator.fit(X_train, y_train, **fit_params)
File "/usr/local/lib/python3.7/dist-packages/sklearn/svm/_base.py", line 171, in fit
accept_large_sparse=False)
File "/usr/local/lib/python3.7/dist-packages/sklearn/base.py", line 433, in _validate_data
X, y = check_X_y(X, y, **check_params)
File "/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py", line 63, in inner_f
return f(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py", line 878, in check_X_y
estimator=estimator)
File "/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py", line 63, in inner_f
return f(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py", line 717, in check_array
% (array.ndim, estimator_name))
ValueError: Found array with dim 4. Estimator expected <= 2.