با درود واحترام؛
من یک برنامه ساده رو با پایگاه داده تصاویر خودم میخواستم در محیط Jupyter و با استفاده از کراس بنویسم.برنامه زیر:
import keras
import keras.utils
from keras import utils as np_utils
from keras.models import Sequential
model = Sequential()
#
from keras.layers import Dense, Activation
#model.add(Dense(units=64, input_dim=100))
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Generate dummy data
import numpy as np
import matplotlib.pyplot as plt
try:
from scipy import misc
except ImportError:
!pip install scipy
from scipy import misc
training_size = 300
img_size = 20*20*3
training_data = np.empty(shape=(training_size,20,20,3))
import glob
i = 0
for filename in glob.glob('D:/Minutia/PrincipleWrinkleMinutia/*.jpg'):
image = misc.imread(filename)
training_data[i] = image
i+=1
#labels = np.random.randint(10, size=(1000, 1))
# #Convert labels to categorical one-hot encoding
#one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)
a= [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
from sklearn.preprocessing import OneHotEncoder
a = np.asarray(a)
one_hot_labels = OneHotEncoder(sparse=False).fit_transform(a.reshape(-1, 1))
model.fit(training_data, one_hot_labels, epochs=10, batch_size=32)
لینک کد
وخطای برنامه :
ValueError Traceback (most recent call last)
<ipython-input-9-3b925296aca6> in <module>()
49
50 # Train the model, iterating on the data in batches of 32 samples
---> 51 model.fit(training_data, one_hot_labels, epochs=10, batch_size=32)
52
53 #model.fit(x_train, y_train, epochs=5, batch_size=32)
C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
865 class_weight=class_weight,
866 sample_weight=sample_weight,
--> 867 initial_epoch=initial_epoch)
868
869 def evaluate(self, x, y, batch_size=32, verbose=1,
C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1520 class_weight=class_weight,
1521 check_batch_axis=False,
-> 1522 batch_size=batch_size)
1523 # Prepare validation data.
1524 do_validation = False
C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
1376 self._feed_input_shapes,
1377 check_batch_axis=False,
-> 1378 exception_prefix='input')
1379 y = _standardize_input_data(y, self._feed_output_names,
1380 output_shapes,
C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
130 ' to have ' + str(len(shapes[i])) +
131 ' dimensions, but got array with shape ' +
--> 132 str(array.shape))
133 for j, (dim, ref_dim) in enumerate(zip(array.shape, shapes[i])):
134 if not j and not check_batch_axis:
ValueError: Error when checking input: expected dense_5_input to have 2 dimensions, but got array with shape (300, 20, 20, 3)
لینک خطا
ValueError: Error when checking input: expected dense_5_input to have 2 dimensions, but got array with shape (300, 20, 20, 3)
هست، درصورت امکان راهنمایی بفرمایید.