Related
New Python developer here. I looked at other similar posts here but i'm not able to get it right. Would appreciate any help.
print('X_train:', X_train.shape)
print('y_train:', y_train1.shape)
print('X_test:', X_train.shape)
print('y_test:', y_train1.shape)
X_train: (42000, 32, 32)
y_train: (42000,)
X_test: (42000, 32, 32)
y_test: (42000,)
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
def featuremodel() :
model = Sequential()
model.add(Conv2D(32, kernel_size=4, activation='relu', input_shape=(X_train.shape[0],32,64)))
model.add(MaxPooling2D(pool_size=3))
model.add(Conv2D(64, kernel_size=4, activation='relu'))
model.add(Flatten())
model.add(Dense(len(y_train[0]), activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['acc'])
model.summary()
model.fit(X_train, y_train, epochs = 10, validation_data = (X_test,y_test))
return model
ValueError: Input 0 of layer sequential_7 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (None, 1024)
The input shape you have specified should be changed. Your input has 42000 samples and each one has (32,32) shape. You should not pass number of samples (42000) to input layer, and you should add a channel dimension. So the input shape should be (32,32,1).
The modified code should be like this:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
# test data
X_train = tf.random.uniform((42000,32,32))
y_train1 = tf.random.uniform((42000,))
X_train = tf.expand_dims(X_train, axis=3) #add channel axis (42000,32,32) => (42000,32,32,1)
model = Sequential()
model.add(Conv2D(32, kernel_size=4, activation='relu', input_shape=(32,32,1))) #change input shape
model.add(MaxPooling2D(pool_size=3))
model.add(Conv2D(64, kernel_size=4, activation='relu'))
model.add(Flatten())
#last layer should have output like your y data. in this case it should be 1, since you have 1 y for each sample
model.add(Dense(1, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['acc'])
model.summary()
model.fit(X_train, y_train1, epochs = 10) #, validation_data = (X_test,y_test))
I read Keras documentation, but had not found any explanation on the following error
Code:
import numpy as np
import pandas as pd
from tensorflow.keras import layers
from keras.optimizers import SGD
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.layers.core import Dense, Activation, Dropout, Reshape, Flatten
from keras.utils.np_utils import to_categorical
data = pd.read_excel('oildata.xlsx')
firstBranch = Sequential()
#firstBranch.add(Reshape((1,28,28), input_shape=(,)))
firstBranch.add(LSTM(64, input_shape=(10, 1100)))
#firstBranch.add(MaxPooling2D((2, 2), strides=(2, 2)))
firstBranch.add(Flatten())
secondBranch = Sequential()
secondBranch.add(BatchNormalization(name = 'batch_norm_0', input_shape = (1000, 10, 1, 1)))
secondBranch.add(ConvLSTM2D(name ='conv_lstm_1',
filters = 64, kernel_size = (10, 1),
padding = 'same',
return_sequences = False))
secondBranch.add(Dropout(0.10, name = 'dropout_1'))
secondBranch.add(BatchNormalization(name = 'batch_norm_1'))
# model.add(ConvLSTM2D(name ='conv_lstm_2',
# filters = 64, kernel_size = (5, 1),
# padding='same',
# return_sequences = False))
# model.add(Dropout(0.20, name = 'dropout_2'))
# model.add(BatchNormalization(name = 'batch_norm_2'))
secondBranch.add(Flatten())
secondBranch.add(RepeatVector(1000))
secondBranch.add(Reshape((1000, 10, 1, 64)))
# model.add(ConvLSTM2D(name ='conv_lstm_3',
# filters = 64, kernel_size = (10, 1),
# padding='same',
# return_sequences = True))
# model.add(Dropout(0.20, name = 'dropout_3'))
# model.add(BatchNormalization(name = 'batch_norm_3'))
secondBranch.add(ConvLSTM2D(name ='conv_lstm_4',
filters = 64, kernel_size = (5, 1),
padding='same',
return_sequences = True))
secondBranch.add(TimeDistributed(Dense(units=1, name = 'dense_1', activation = 'relu')))
secondBranch.add(Dense(units=1, name = 'dense_2'))
secondBranch.add(Flatten())
thirdBranch = Sequential()
thirdBranch.add(Reshape((1,28,28), input_shape=(784,)))
thirdBranch.add(Dense(10, activation='relu'))
thirdBranch.add(Flatten())
fourthBranch = Sequential()
#fourthBranch.add(Reshape((1,28,28), input_shape=(784,)))
fourthBranch.add(Dense(10, activation='relu'))
fourthBranch.add(Flatten())
#merged = Concatenate([firstBranch, secondBranch, thirdBranch,fourthBranch], mode='concat')
merged = Concatenate([firstBranch,secondBranch,thirdBranch,fourthBranch])
model = Sequential()
model.add(merged)
model.add(Dense(28*3, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(28, activation='relu'))
model.add(Dense(19))
model.add(Activation("softmax"))
sgd = SGD(lr=0.5, momentum=0.8, decay=0.1, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
model.fit([X,X,X,X],X, batch_size=100, verbose=2)
yPred = model.predict([X,X,X,X],X)
Error:
TypeError Traceback (most recent call last)
<ipython-input-385-11a86cc54884> in <module>
88 sgd = SGD(lr=0.5, momentum=0.0, decay=0.0, nesterov=False)
89 model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
---> 90 model.fit([X,X,X,X],X, batch_size=100, verbose=2)
91
92 yPred = model.predict([X,X,X,X],X)
...........................................
TypeError: list indices must be integers or slices, not ListWrapper"
What does it mean ListWrapper? Data is turned into frames and had to fit the model.
I'm training a model to produce image masks. This error keeps popping up, and I can not determine the cause. Help would be appreciated.
Error statement:
File "--\Users\-----\Anaconda3\lib\site-packages\keras\initializers.py", line 209, in __call__
scale /= max(1., float(fan_in + fan_out) / 2)
TypeError: only size-1 arrays can be converted to Python scalars
Researching online, this error occurs when normal lists are used with numpy functions, but in my case, the arrays used are numpy arrays. Below, I've attached the code.
import cv2
import glob
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist
from keras import backend as K
K.set_image_dim_ordering('tf')
np.random.seed(123) # for reproducibility
image_list = []
test_list = []
for filename in glob.glob("image/*.jpg*"):
im = cv2.imread(filename)
im_r = cv2.resize(im,(200, 200), interpolation = cv2.INTER_AREA)
image_list.append(im_r)
for filename in glob.glob("test/*.png*"):
im = cv2.imread(filename)
im_r = cv2.resize(im,(200, 200), interpolation = cv2.INTER_AREA)
im_r = np.ravel(im_r)
test_list.append(im_r)
x_data = np.array(image_list)
y_data = np.array(test_list)
x_data = x_data.astype("float32")
y_data = y_data.astype("float32")
x_data /= 255
y_data /= 255
X_train = x_data
Y_train = y_data
model = Sequential()
model.add(Convolution2D(32, 5, 5, activation='relu', input_shape=(200, 200, 3)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Convolution2D(32, 5, 5, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(Y_train[0], activation='sigmoid'))
print('hello')
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
loss = acc = 0
while acc < 0.9999:
model.fit(X_train, Y_train, batch_size=32, nb_epoch=10, verbose=1)
loss, acc = model.evaluate(X_train, Y_train, verbose=1)
model.save("model_state_no_mapping")
The problem is in the last layer of your model.
Change the last layer from
model.add(Dense(Y_train[0], activation='sigmoid'))
to
model.add(Dense(Y_train.shape[0], activation='sigmoid'))
Also, in newer versions of Keras it is recommended to use Conv2D layer instead of old Convolution2D.
I am doing a binary classification problem, my model architecture is as follow
def CNN_model(height, width, depth):
input_shape = (height, width, depth)
model = Sequential()
# Block 1
model.add(Conv2D(64, kernel_size=(3, 3), strides=1, activation='relu', input_shape=input_shape, padding='VALID'))
model.add(Conv2D(64, kernel_size=(3, 3), strides=1, activation='relu', padding='VALID'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Block 2
model.add(Conv2D(128, kernel_size=(3, 3), strides=1, activation='relu', padding='VALID'))
model.add(Conv2D(128, kernel_size=(3, 3), strides=1, activation='relu', padding='VALID'))
model.add(AveragePooling2D(pool_size=(19, 19)))
# set of FC => RELU layers
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.binary_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
return model
I need for each image on a test set, I get a 128-D feature vector collected from FC layer use for SVM classification. More detail, from model.add(Dense(128)). Can you please show me how to solve this problem? Thank you!
Here the simplest way is to remove the Dense layer.
I will answer with a counter example with similar layers but different input_shape:
from keras.layers import *
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
import numpy as np
from scipy.misc import imsave
import numpy as np
from keras.layers import *
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.layers import Dropout, Flatten, Dense
from keras.applications import ResNet50
from keras.models import Model, Sequential
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
import matplotlib.pyplot as plt
from keras.applications.resnet50 import preprocess_input
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3), input_shape=(530, 700, 3), padding='VALID'))
model.add(Conv2D(64, kernel_size=(3, 3), padding='VALID'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Block 2
model.add(Conv2D(128, kernel_size=(3, 3), strides=1, activation='relu', padding='VALID'))
model.add(Conv2D(128, kernel_size=(3, 3), strides=1, activation='relu', padding='VALID'))
model.add(AveragePooling2D(pool_size=(19, 19)))
# set of FC => RELU layers
model.add(Flatten())
#getting the summary of the model (architecture)
model.summary()
img_path = '/home/sb0709/Desktop/dqn/DQN/data/data/2016_11_01-2017_11_01.png'
img = image.load_img(img_path, target_size=(530, 700))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
vgg_feature = model.predict(img_data)
#print the shape of the output (so from your architecture is clear will be (1, 128))
#print shape
print(vgg_feature.shape)
#print the numpy array output flatten layer
print(vgg_feature.shape)
Here is the output model architecture with all layers:
Also here is listed the feature vector:
Image used in the example:
Second method is for when using Functional Api instead of Sequencial() to use How can I obtain the output of an intermediate layer?
from keras import backend as K
# with a Sequential model
get_6rd_layer_output = K.function([model.layers[0].input],
[model.layers[6].output])
layer_output = get_6rd_layer_output([x])[0]
#print shape
print(layer_output.shape)
#print the numpy array output flatten layer
print(layer_output.shape)
One more useful step is the visualization of the features, I bet a lot of people want to see what see the computer and will illustrate only the "Flatten" layer output(better said the network):
def visualize_stock(img_data):
plt.figure(1, figsize=(25, 25))
stock = np.squeeze(img_data, axis=0)
print(stock.shape)
plt.imshow(stock)
and the magic:
visualize_stock(img_data)
Note: changed from input_shape=(530, 700, 3) from input_shape=(84, 84, 3) for better visualization for the public.
P.S: Decided to post so anyone who has this type of question to benefit (struggled with same type of questions recently).
I have a 2d array of log-scaled mel-spectrograms of sound samples for 5 different categories.
For training I have used convolutional and dense neural network in Keras. Here the code:
model = Sequential()
model.add(Conv1D(80, 8, activation='relu', padding='same',input_shape=(60,108)))
model.add(MaxPooling1D(2,padding='same',strides=None))
model.add(Flatten())
initializer=initializers.TruncatedNormal()
model.add(Dense(200, activation='relu', kernel_initializer=initializer,bias_initializer=initializer))
model.add(BatchNormalization())
model.add(Dropout(0.8))
model.add(Dense(50, activation='relu', kernel_initializer=initializer,bias_initializer=initializer))
model.add(Dropout(0.8))
model.add(Dense(5, activation='softmax', kernel_initializer=initializer,bias_initializer=initializer))
model.compile(loss='categorical_crossentropy',
optimizer='adam',lr=0.01,
metrics=['accuracy'])
What kind of autoencoder can I apply to this type of data input? What model? Any suggestion or also code example would be helpful. :)
Since I don’t have answers to my question about the nature of the data, I will assume that we have set of 2 dimensional data with the shape like (NSamples, 68, 108). Also, I assume that answer on my suggestion to use Convolutional2D instead Convolutional1D is yes
Here is sample of models for convolutional auto encoder, model, which can use a trained auto encoder and how to use weights from an auto encoder for the final model:
from keras.layers.core import Dense, Dropout, Flatten, Reshape
from keras.layers import Conv1D, Conv2D, Deconv2D, MaxPooling1D, MaxPooling2D, UpSampling2D, Conv2DTranspose, Flatten, BatchNormalization, Dropout
from keras.callbacks import ModelCheckpoint
import keras.models as models
import keras.initializers as initializers
from sklearn.model_selection import train_test_split
ae = models.Sequential()
#model.add(Conv1D(80, 8, activation='relu', padding='same',input_shape=(60,108)))
#encoder
c = Conv2D(80, 3, activation='relu', padding='same',input_shape=(60, 108, 1))
ae.add(c)
ae.add(MaxPooling2D(pool_size=(2, 2), padding='same', strides=None))
ae.add(Flatten())
initializer=initializers.TruncatedNormal()
d1 = Dense(200, activation='relu', kernel_initializer=initializer,bias_initializer=initializer)
ae.add(d1)
ae.add(BatchNormalization())
ae.add(Dropout(0.8))
d2 = Dense(50, activation='relu', kernel_initializer=initializer,bias_initializer=initializer)
ae.add(d2)
ae.add(Dropout(0.8))
#decodser
ae.add(Dense(d2.input_shape[1], activation='sigmoid'))
ae.add(Dense(d1.input_shape[1], activation='sigmoid'))
ae.add(Reshape((30, 54, 80)))
ae.add(UpSampling2D((2,2)))
ae.add(Deconv2D(filters= c.filters, kernel_size= c.kernel_size, strides=c.strides, activation=c.activation, padding=c.padding, ))
ae.add(Deconv2D(filters= 1, kernel_size= c.kernel_size, strides=c.strides, activation=c.activation, padding=c.padding, ))
ae.compile(loss='binary_crossentropy',
optimizer='adam',lr=0.001,
metrics=['accuracy'])
ae.summary()
#now train your convolutional autoencoder to reconstruct your input data
#reshape your data to (NSamples, 60, 108, 1)
#Then train your autoencoder. it can be something like that:
#X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=43)
#pre_mcp = ModelCheckpoint("CAE.hdf5", monitor='val_accuracy', verbose=2, save_best_only=True, mode='max')
#pre_history = ae.fit(X_train, X_train, epochs=100, validation_data=(X_val, X_val), batch_size=22, verbose=2, callbacks=[pre_mcp])
#model
model = models.Sequential()
#model.add(Conv1D(80, 8, activation='relu', padding='same',input_shape=(60,108)))
model.add(Conv2D(80, 3, activation='relu', padding='same',input_shape=(60, 108, 1)))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same',strides=None))
model.add(Flatten())
initializer=initializers.TruncatedNormal()
model.add(Dense(200, activation='relu', kernel_initializer=initializer,bias_initializer=initializer))
model.add(BatchNormalization())
model.add(Dropout(0.8))
model.add(Dense(50, activation='relu', kernel_initializer=initializer,bias_initializer=initializer))
model.add(Dropout(0.8))
model.add(Dense(5, activation='softmax', kernel_initializer=initializer,bias_initializer=initializer))
model.compile(loss='categorical_crossentropy',
optimizer='adam',lr=0.001,
metrics=['accuracy'])
#Set weights
model.layers[0].set_weights(ae.layers[0].get_weights())
model.layers[3].set_weights(ae.layers[3].get_weights())
model.layers[4].set_weights(ae.layers[4].get_weights())
model.layers[6].set_weights(ae.layers[6].get_weights())
model.summary()
#Now you can train your model with pre-trained weights from autoencoder
A model like this was useful for me with MNIST dataset and improved accuracy of model with initial weights from auto encoder in comparison with model initialized with random weights
However, I would recommend using of several convolutional/deconvolutional layers, probably 3 or more, since from my experience convolutional auto encoders with 3 and more convolutional layers are more efficient than with 1 convolutional layer. In fact, with one convolutional layer I can’t even see any accuracy improvements sometimes
Update:
I checked auto encoder with data provided by Emanuela, also I checked it with different auto encoders architectures without any success
My hypothesis about that is that the data doesn’t contain any significant features, which can be distinguished by auto encoder or even CAE
However, it looks like my assumption about 2 dimensional nature of the data was confirmed by reaching of almost 99.99% validation accuracy:
Nevertheless, in the same time, 97.31% accuracy of training data can indicate potential issues with dataset, so it looks like a good idea to revise it
In addition, I would suggest using ensembles of networks. You could train, for example 10 networks with different validation data and assign a category for items by the most voted categories
Here is my code:
from keras.layers.core import Dense, Dropout, Flatten
from keras.layers import Conv2D, BatchNormalization
from keras.callbacks import ModelCheckpoint
from keras.optimizers import Adam
from sklearn.model_selection import train_test_split
import keras.models as models
import keras.initializers as initializers
import msgpack
import numpy as np
with open('SoundDataX.msg', "rb") as fx,open('SoundDataY.msg', "rb") as fy:
dataX=msgpack.load(fx)
dataY=msgpack.load(fy)
num_samples = len(dataX)
x = np.empty((num_samples, 60, 108, 1), dtype = np.float32)
y = np.empty((num_samples, 4), dtype = np.float32)
for i in range(0, num_samples):
x[i] = np.asanyarray(dataX[i]).reshape(60, 108, 1)
y[i] = np.asanyarray(dataY[i])
X_train, X_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=43)
#model
model = models.Sequential()
model.add(Conv2D(128, 3, activation='relu', padding='same',input_shape=(60, 108, 1)))
model.add(Conv2D(128, 5, activation='relu', padding='same',input_shape=(60, 108, 1)))
model.add(Conv2D(128, 7, activation='relu', padding='same',input_shape=(60, 108, 1)))
model.add(Flatten())
initializer=initializers.TruncatedNormal()
model.add(Dense(200, activation='relu', kernel_initializer=initializer,bias_initializer=initializer))
model.add(BatchNormalization())
model.add(Dropout(0.8))
model.add(Dense(50, activation='relu', kernel_initializer=initializer,bias_initializer=initializer))
model.add(Dropout(0.8))
model.add(Dense(4, activation='softmax', kernel_initializer=initializer,bias_initializer=initializer))
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=0.0001),
metrics=['accuracy'])
model.summary()
filepath="weights-{epoch:02d}-{val_acc:.7f}-{acc:.7f}.hdf5"
mcp = ModelCheckpoint(filepath, monitor='val_acc', verbose=2, save_best_only=True, mode='max')
history = model.fit(X_train, y_train, epochs=100, validation_data=(X_val, y_val), batch_size=64, verbose=2, callbacks=[mcp])