Error with keras model: expected dense_53 to have 2 dimensions, but got array with shape (1, 1, 28, 28) - python

I have a model and although I have a flatten layer it keeps giving me the error:
ValueError: Error when checking target: expected dense_53 to have 2 dimensions, but got array with shape (1, 1, 28, 28)
Additionaly, if I remove the dense and flatten layers it gives me the same error for the maxPooling and then for the convolutional. Here is my entire code:
import os
import numpy as np
from keras.preprocessing import image
from keras import backend as K
from keras.callbacks import EarlyStopping
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D
from keras.optimizers import Adam
from keras.layers.pooling import MaxPooling2D
from keras.utils import to_categorical
PATH = os.getcwd()
train_path = PATH+'/Downloads/KerasStuff/train_data/rice/'
train_batch = os.listdir(train_path)
x_train = np.empty((0,32,32,3))
# if data are in form of images
for sample in train_batch:
img_path = train_path+sample
if img_path == '/Users/23athreyad/Downloads/KerasStuff/train_data/rice/.DS_Store':
print("INVALID")
else:
x = image.load_img(img_path)
np.append(x_train,x)
test_path = PATH+'/Downloads/KerasStuff/test_data/rice/'
test_batch = os.listdir(test_path)
x_test = []
for sample in test_batch:
img_path = test_path+sample
x = image.load_img(img_path)
# preprocessing if required
x_test.append(x)
# Create the model
K.set_image_dim_ordering('tf')
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size = (2,2),data_format="channels_last"))
print(model.output_shape)
model.add(Dropout(0.25))
model.add(Flatten())
print(model.output_shape)
model.add(Dense(7200,activation = 'relu'))
# Compile the model
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=0.0001, decay=1e-6),
metrics=['accuracy'])
model.fit(x_train,y_train,batch_size = 10, epochs = 20,callbacks=[EarlyStopping(min_delta=0.001, patience=3)])
Can someone help me with this? I am new to Keras and have looked this up but not found any solutions.

Related

How to transfer weights to Conv2DTranspose layer in TensorFlow?

from tensorflow import keras
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Dense
from keras.layers import Flatten
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.utils import to_categorical
from scipy import signal
(trainX, trainY) , (testX, testY) = keras.datasets.mnist.load_data()
trainX = trainX.reshape((trainX.shape[0], 28, 28, 1))
testX = testX.reshape((testX.shape[0], 28, 28, 1))
# one hot encode target values
trainY = to_categorical(trainY)
testY = to_categorical(testY)
model=Sequential()
model.add(Conv2D(32, (3, 3),activation='relu',input_shape=(28, 28, 1)))
model.add(MaxPooling2D((1, 1)))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history=model.fit(trainX,trainY,epochs=3)
history=model.evaluate(testX,testY)
After this, I get the weights of the conv layer
for layer in model.layers:
if "con" in layer.name:
filterForThis,bias=layer.get_weights()
Now the aim is to transfer the weights from Conv to new model
from keras.layers import Conv2DTranspose
model2 = Sequential()
model2.add(Conv2DTranspose(32,kernel_size=(3, 3), padding='same',activation='relu',input_shape=(28, 28, 1)))
model2.summary()
model2.layers[0].set_weights([filterForThis,bias])
Here I am getting the error
Layer conv2d_transpose_15 weight shape (3, 3, 32, 1) is not compatible with provided weight shape (3, 3, 1, 32)
Here I know reshaping would work, but how to do it the right way?
I think numpy.swapaxes should do it:
print(filterForThis.shape) # prints (3, 3, 1, 32)
filter2 = np.swapaxes(filterForThis, axis1=2, axis2=3)
print(filter2.shape) # prints (3, 3, 32, 1)
model2.layers[0].set_weights([filter2,bias])

Trying to train a convolutional neural network using an image dataset saved as a numpy array in pickle

Trying to train a convolutional neural network using tensorflow and keras on python 3 in an anaconda environment. I think it's to do with how the dataset was saved in pickle previously but not completely sure
This is the error I keep getting:
ValueError: Failed to find data adapter that can handle input: <class 'numpy.ndarray'>, (<class 'list'> containing values of types {"<class 'int'>"})
This is my code:
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
import pickle
pickle_in = open("X.pickle","rb")
X = pickle.load(pickle_in)
pickle_in = open("Y.pickle","rb")
y = pickle.load(pickle_in)
X = X/255.0
model = Sequential()
model.add(Conv2D(256, (3, 3), input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X, y, batch_size=32, epochs=3, validation_split=0.3)
This code and question has been asked on StackExchange before:
https://datascience.stackexchange.com/questions/60035/keras-error-failed-to-find-data-adapter-that-can-handle-input-while-trying-to
edit:
To fix your problem, you can try to change how you import the data and make sure it's a valid numpy array.
At the top, import numpy as np and then change where you load the data and define X and y to: X = np.asarray(pickle.load(open("X.pickle", "rb"))) and then: y = np.asarray(pickle.load(open("y.pickle", "rb")))
I am also facing the same issue however it works after importing NumPy.
import numpy as np
pickle_in = open("X.pickle","rb")
X = np.asarray(pickle.load(pickle_in))
pickle_in = open("y.pickle","rb")
y = np.asarray(pickle.load(pickle_in))

Keras - copy Conv2D layer

I want to make a copy of a Conv2D layer.
I tried this:
Edit: I've changed the example code to a mcve
Edit2: I've changed the code based on fuglede's answer
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
from keras.datasets import mnist
from keras.utils import to_categorical
import matplotlib.pyplot as plt
import numpy as np
import random
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(60000, 28, 28, 1)
X_test = X_test.reshape(10000, 28, 28, 1)
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential()
model.add(Conv2D(random.randint(32, 64), kernel_size=random.randint(1, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(Conv2D(32, kernel_size=3, activation='relu'))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
other_model = Sequential()
layer = model.layers[1]
other_model.add(Conv2D(random.randint(32, 64), kernel_size=random.randint(1, 3), activation='relu', input_shape=(28, 28, 1)))
copy_layer = Conv2D(layer.filters, kernel_size=layer.kernel_size, activation='relu')
other_model.add(copy_layer)
copy_layer.set_weights(layer.get_weights())
But I'm getting this error:
ValueError: Layer weight shape (3L, 3L, 61L, 32L) not compatible with provided weight shape (3L, 3L, 40L, 32L)
Edit: The purpose of this is, I'm using a genetic algorithm to evolve/"train" a set a neural networks, and this a part of the crossover step.
This happens because the layer is only initialized once it's added to a model. If you swap the two last lines of your example, it should work as expected.

Store images into multiply array and use it to train model

I get this error when train the model:
ValueError: Error when checking target:
expected dropout_5 to have shape (33,) but got array with shape (1,).
I want to store my images into 33 array from folder using path. I have categories the images into different folder which were 1,2,3,4,5...
I have use this code to do it but i dont know how to store it into different array. Can someone help me.
datadir = 'C:/Users/user/Desktop/RESIZE' #path of the folder
categories = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y']
img_rows, img_cols = 100, 100
training_data = []
for category in categories:
path = os.path.join(datadir,category)
class_num = categories.index(category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array,(img_rows,img_cols))
training_data.append([new_array,class_num])
random.shuffle(training_data)
X = []
y = []
for features, label in training_data:
X.append(features)
y.append(label)
X = np.array(X).reshape(-1,img_rows,img_cols,1)
X = X.astype("float32")
pickle_out = open("X.pickle","wb")
pickle.dump(X,pickle_out)
pickle_out.close()
pickle_out = open("y.pickle","wb")
pickle.dump(y,pickle_out)
pickle_out.close()
After I save the file, then I use this code to train model and I want get 33 output layer but it only can work when my output layer(Dense) set 1.
I got this error:
ValueError: Error when checking target:
expected dropout_5 to have shape (33,) but got array with shape (1,)
Here was my training code.
import tensorflow as tf
from keras import optimizers
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers import Activation
import cv2
import os
import numpy as np
import pickle
from sklearn.utils import shuffle
X = pickle.load(open("X.pickle","rb"))
y = pickle.load(open("y.pickle","rb"))
X = X/255.0
model = Sequential()
model.add(Conv2D(32,(3,3), input_shape = X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(64,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(128,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.4))
model.add(Dense(128))
model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(33, activation='softmax'))
model.add(Dropout(0.4))
model.compile(loss = "binary_crossentropy", optimizer = "adam", metrics = ["accuracy"])
model.fit(X, y, batch_size = 2, epochs = 1, validation_split = 0.2)
You need to change your y as one hot encoded data to do the training.
Try this for y,
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
# integer encode
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(y)
print(label_encoder.classes_) # This is your classes.
print(integer_encoded.shape())
# binary encode
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
print(onehot_encoded.shape())
And one more thing, if you want to classify for 33 class than change your loss to categorical_crossentropy.

CNN for feature array in python

I have an array data which has 5000 row(images) and 58 columns(features). And the array is CSV format. How can i classify with CNN?
I was trying this following code, but i got error. what is wrong?
import keras
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution1D, MaxPooling2D
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
data = pd.read_csv('/home/mahfuz/Documents/AllFeatures.csv')
data.head()
data.iloc[3,1:].values
df_x = data.iloc[:,1:].values
y = data.iloc[:,1].values
df_x = np.array(df_x)
df_y = np.array(y)
df_y
df_y.shape
df_x.shape
x_train, x_test, y_train, y_test = train_test_split(df_x,df_y,test_size=0.2,random_state=4)
model = Sequential()
model.add(Convolution1D(32, (3, 3), input_shape= ( 32, 32, 3)))
model.add(MaxPooling1D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(100))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',optimizer = 'adadelta', metrices = ['accuracy'])
model.fit(x_train,y_train,validation_data=(x_test,y_test))
model.evaluate(x_test,y_test)
Can I ask you what is the error?
First, Your data shape in the model is (32,32,3) on Conv1D, check it.
Your images shape is (32, 32, 3) or you have data with shape (5000, 58) ?

Categories

Resources