با سلام
بر اساس آموزش جناب دکتر رضوی(تشکر فراروان) اقدام به پیاده سازی شبکه کانولوشن کردم ولی با خطای:
Input 0 is incompatible with layer conv2d_2: expected ndim=4, found ndim=2
مواجه هستم. ممنون میشم کد زیر را نگاهی بندازید.
ممنون و قدردان زحمات شما.
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout ,Flatten, Activation
from keras.layers import Conv2D, MaxPooling2D
# %matplotlib inline
image_size = 32
num_channels = 3
num_classes = 10
epochs =2
# epochs =20
data_augmentation =True
num_features = image_size * image_size * num_channels
#num_train = 49000
# Load CIRAR10 Dataset
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print('Training set shap: {} '.format(X_train.shape))
print('Train data shape: {}'.format(X_train.shape[0]))
print('Test data shape: {}'.format(X_test.shape[0]))
#Data Visualization
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
samples_per_class = 4
plt.figure(figsize=(16,8))
for cls, name in enumerate(class_names):
idxs = np.flatnonzero(y_train == cls)
idxs = np.random.choice(idxs, samples_per_class, replace=False)
for i, idx in enumerate(idxs):
plt.subplot(samples_per_class, num_classes, i * num_classes + cls + 1)
plt.imshow(X_train[idx], interpolation='spline16')
plt.axis('off')
if i == 0:
plt.title(class_names[cls])
# Convert 4D arrays to 2D arrays
X_train = X_train.reshape([-1, num_features])
X_test = X_test.reshape([-1, num_features])
print('Train data shape: {}'.format(X_train.shape))
print('Test data shape: {}'.format(X_test.shape))
#Data Preprocessing
# convert pixel range from [0, 255] to [0., 1.]
X_train = X_train.astype('float32')
x_test = X_test.astype('float32')
mu = np.mean(X_train, axis=0)
X_train -= mu
x_test -= mu
#same scales for features
X_train /= 255.0
x_test /= 255.0
#One-Hot encodding
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
print(y_train.shape)
print(y_test.shape)
#def create_cnn():
model = Sequential()
# Conv Block 1
model.add(Conv2D(64, (3, 3), padding='same', input_shape=X_train.shape[1:], activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# Conv Block 2
model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# Conv Block 3
model.add(Conv2D(256, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# Classifier
model.add(Flatten())
model.add(Dense(128, activation='relu'))
#model.add(Dropout(0.25))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
# return model
#model = create_cnn()
#print model
model.summary()