i try to feed my model with dataset values but i get a shape error. Can somebody help with it and explain how it works with datasets? Many thanks
import pandas as pd
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Model
dataset = tf.data.experimental.make_csv_dataset(
"mydata/*.csv",
batch_size=64,
field_delim=",",
num_epochs=1,
select_columns=['X1', 'X2', 'X3', 'Y'],
label_name='Y',
shuffle=False,
prefetch_buffer_size=64)
dataset = dataset.map(lambda x, y: (tf.concat(
[tf.expand_dims(x['X1'], axis=-1), tf.expand_dims(x['X2'], axis=-1), tf.expand_dims(x['X3'], axis=-1)], axis=-1), y))
inputs = Input(shape=(None, 3))
hidden1 = SimpleRNN(3, activation='relu', input_shape=(3, 1))(inputs)
outputs = Dense(units=1, activation='linear')(hidden1) # hidden layer 1
model_rnn = Model(hidden1, outputs)
model_rnn.compile(loss='mean_absolute_error', optimizer="adam", metrics=['mean_squared_error'])
history = model_rnn.fit(dataset, epochs=2, shuffle=False)
Related
I want to predict outputs using two inputs using LSTM. my code is as follow:
from keras.models import Sequential
from keras.layers import Dense, LSTM
from numpy import array
from numpy.random import uniform
from numpy import hstack
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import pandas as pd
from sklearn.preprocessing import StandardScaler
import seaborn as sns
dataset_train = pd.read_csv('E:/Research/Summer22/Disaggregation/hourly1.csv')
cols = dataset_train[['TotalE','Outdoor_temp_F','electric.appliances..kWhs.','hvac']]
#Normalization
scaler = StandardScaler()
scaler = scaler.fit(cols)
df_for_training_scaled = scaler.transform(cols)
df_for_training_scaled= pd.DataFrame(data=df_for_training_scaled)
x = array(df_for_training_scaled.iloc[:, [0,1]] )
x = x.reshape(x.shape[0],x.shape[1])
x=x.astype(float)
y= array(df_for_training_scaled.iloc[:, [2,3]] )
y=y.astype(float)
print(y.shape[1])
out_dim = y.shape[1]
in_dim = x.shape[0]
plt.plot(y)
plt.show()
xtrain, xtest, ytrain, ytest=train_test_split(x, y, test_size=0.15)
model = Sequential()
model.add(LSTM(64, input_shape=in_dim, activation="relu"))
model.add(Dense(out_dim))
model.compile(loss="mse", optimizer="adam")
model.summary()
History=model.fit(xtrain, ytrain, validation_split=0.7, epochs=10, batch_size=16, verbose=10)
When I try to build the model and save it to history I face an error:
Input 0 of layer "sequential_85" is incompatible with the layer: expected shape=(None, 2750, 2), found shape=(None, 2)
I know there is a problem with my input/output shape but I don't know how to fix it to predict ['electric.appliances..kWhs.','hvac'] using ['TotalE','Outdoor_temp_F']
For the sake of reproducibility, the training and validations datasets I am using are shared here
The validation_dataset.csv is the ground truth of training_dataset.csv.
What I am doing below is feeding the datasets into a simple CNN layer that extracts the useful features of the images and feed that as 1D into the LSTM network for classification.
from keras.models import Sequential
from keras.layers import Dense, Flatten, Activation
from keras.layers.convolutional import Conv1D
from keras.layers import LSTM
from keras.layers.convolutional import MaxPooling1D
from keras.layers import TimeDistributed
from keras.layers import Dropout
from keras import optimizers
from keras.callbacks import EarlyStopping
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
from confusion_matrix import plot_confusion_matrix
import scikitplot as skplt
from numpy import genfromtxt
train_set = genfromtxt('data/train/training_dataset.csv', delimiter=',')
validation_set = genfromtxt('data/validation/validation_dataset.csv', delimiter=',')
train_set = train_set[..., None]
validation_set = validation_set[..., None]
X_train, X_test, y_train, y_test = train_test_split(train_set, validation_set, test_size=0.30, random_state=0)
batch_size=16
epochs=5
# Create the model
model = Sequential()
model.add(Conv1D(filters=5, kernel_size=3, activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=2))
model.add(LSTM(50, return_sequences=True))
model.add(Dropout(0.5))
model.add(LSTM(10))
model.add(Dense(1,kernel_initializer='random_normal'))
model.add(Activation('relu'))
adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)
sgd = optimizers.SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=adam, loss='mean_squared_error', metrics=['mae', 'mape', 'mean_squared_error', 'acc'])
model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs)
print(model.summary())
# Evaluate the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
skplt.metrics.plot_confusion_matrix(y_test, scores, x_tick_rotation=50, title=' ', normalize=True)
Finally, I want to plot the confusion matrix of the model using
skplt.metrics.plot_confusion_matrix(y_test, scores, x_tick_rotation=50, title=' ', normalize=True)
However, it is raising an error ValueError: Found input variables with inconsistent numbers of samples: [5394, 5].
How can we fix this error?
The second argument to skplt.metrics.plot_confusion_matrix must be the predicted labels (see https://scikit-plot.readthedocs.io/en/stable/metrics.html). But, you pass scores, which does not contain the predicted labels.
The fix would be to do:
y_pred = model.predict(X_test)
skplt.metrics.plot_confusion_matrix(y_test,
y_pred,
x_tick_rotation=50,
title=' ',
normalize=True)
I was working on SVM few days ago and when i tried to plot confusion matrix the following lines of code worked for me.
predicted=model.predict(X_test) #predicted output
cm=metrics.confusion_matrix(y_test, predicted)
df_cm = pd.DataFrame(cm, range(2), range(2))
sns.set(font_scale=1.4)
sns.heatmap(df_cm, annot=True, annot_kws={"size": 16})
plt.title('CONFUSION MATRIX ',fontdict={'fontsize': 14, 'fontweight': 'bold'})
plt.show()
I have been trying to implement a transfer learning model using the Xception model and fine-tune it. But when I tried to train the model in the last section of the code it is showing the following error - AttributeError: 'numpy.ndarray' object has no attribute '_in_multi_worker_mode'.Can someone help me solve this error or any code to train the model.My code is given below.
# Install TensorFlow
try:
# %tensorflow_version only exists in Colab.
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as to
print(tf.__version__)
from tensorflow import keras
tf.random.set_seed(42)
import numpy as np
np.random.seed(42)
# Pandas and Numpy for data structures and util functions
import numpy as np
import pandas as pd
from numpy.random import rand
pd.options.display.max_colwidth = 600
# Scikit Imports
from sklearn.model_selection import train_test_split
# Matplot Imports
import matplotlib.pyplot as plt
params = {'legend.fontsize': 'x-large',
'figure.figsize': (15, 5),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
import glob
import PIL
from PIL import Image
plt.rcParams.update(params)
%matplotlib inline
# pandas display data frames as tables
from IPython.display import display, HTML
import warnings
warnings.filterwarnings('ignore')
import sys
import os
from tensorflow.keras import utils as np_utils
from tensorflow.keras.utils import multi_gpu_model
from tensorflow.keras.utils import Sequence
from tensorflow.keras.models import Model
from tensorflow.keras import layers
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras import regularizers
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import GlobalAveragePooling2D
from tensorflow.keras.layers import Dense, Dropout, Activation,
BatchNormalization, Flatten
from tensorflow.keras.models import Sequential,load_model
from tensorflow.python.keras.utils.data_utils import Sequence
from tensorflow.keras.preprocessing.image import ImageDataGenerator,
img_to_array, load_img
from tensorflow.keras.preprocessing.image import NumpyArrayIterator
from keras.applications import Xception
from tensorflow.keras.preprocessing import image
from tensorflow.keras import backend as K
imgFiles = glob.glob("dataset/*/*.jpg")
for items in imgFiles[:8]:
print(items)
X = []
y = []
for fName in imgFiles:
X_i = Image.open(fName)
X_i = X_i.resize((299,299))
X_i = np.array(X_i) / 255.0
X.append(X_i)
label = fName.split("/")
y_i = label[-2]
y.append(y_i)
print(set(y))
from sklearn.preprocessing import LabelEncoder
lEncoder = LabelEncoder()
y = lEncoder.fit_transform(y)
print(set(y))
print(lEncoder.classes_)
X = np.array(X)
y = np.array(y)
print(X.shape)
print(y.shape)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
stratify=y,
random_state=42)#splitting train and test
images to 70% and 30% reps.
print("X_train_shape: {}".format(X_train.shape))
print("X_test_shape: {}".format(X_test.shape))
mu = X_train.mean()
std = X_train.std()
X_train_std = (X_train-mu)/std
X_test_std = (X_test-mu)/std
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train,
test_size=0.15, stratify=y_train,
random_state=42)#splitting 15% of
train images for validation
print("X_val_shape: {}".format(X_val.shape))
# hyper parameters for model
nb_classes = 6 # number of classes
based_model_last_block_layer_number = 126
img_width, img_height = 299, 299
num_channels= 3
batch_size = 32
nb_epoch = 15 # number of iteration the algorithm gets trained.
transformation_ratio = .05 # how aggressive will be the data
augmentation/transformation
#data augmentation
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip='true',
vertical_flip='true')
train_generator = train_datagen.flow(X,y, shuffle=False,
batch_size=batch_size, seed=1)
validation_datagen = ImageDataGenerator(rescale=1. / 255)
val_generator = train_datagen.flow(X,y, shuffle=False,
batch_size=batch_size, seed=1)
# Pre-Trained CNN Model using imagenet dataset for pre-trained weights
# Transfer Learning!!
# Importing Xception pre trained model on ImageNet
base_model = keras.applications.xception.Xception(include_top=False,
weights='imagenet',
input_shape=(img_width, img_height, num_channels))
# first: train only the top layers (which were randomly initialized)
# i.e. freeze all layers of the based model that is already pre-trained.
for layer in base_model.layers:
layer.trainable = False
# Top Model Block which is to be stacked over xception model
out = base_model.output
out = GlobalAveragePooling2D()(out)
out = Dense(1024, activation='relu')(out)
out = Dense(512, activation='relu')(out)
total_classes = y.shape[0]
predictions = Dense(total_classes, activation='softmax')(out)
model = keras.models.Model(inputs=base_model.input, outputs=predictions)
model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=
['accuracy'])
model.summary()
callbacks_list = [keras.callbacks.ModelCheckpoint("bestTL.h5",
save_best_only=True)]
# Train the model
batch_size = batch_size
train_steps_per_epoch = X_train.shape[0] // batch_size
val_steps_per_epoch = X_val.shape[0] // batch_size
#training cnn up to 15 epoch
history = Model.fit(X_train_std,y_train,
steps_per_epoch=train_steps_per_epoch,
validation_data=val_generator,
validation_steps=val_steps_per_epoch,
callbacks=callback_list
epochs=15,
verbose=1)
How about using only tensorflow.keras.xxxx or keras.xxxx instead of using both of them at one time?
I have to perform a simple FSGM attack to a convolutional neural network. The code for the CNN works correctly, and the model is saved without a problem, but when i try to perform the attack an error is shown.
HERE'S THE CODE FOR THE CNN
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D
import matplotlib.pyplot as plt
from keras.datasets import mnist
from keras.utils import to_categorical
import json
import tensorflow as tf
#Using TensorFlow backend.
#download mnist data and split into train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
#plot the first image in the dataset
plt.imshow(X_train[0])
#check image shape
X_train[0].shape
#reshape data to fit model
X_train = X_train.reshape(60000,28,28,1)
X_test = X_test.reshape(10000,28,28,1)
#one-hot encode target column
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
y_train[0]
#create model
model = Sequential()
#add model layers
model.add(Conv2D(32, kernel_size=(5,5), activation='relu', input_shape= (28,28,1)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, kernel_size=(5,5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
#compile model using accuracy as a measure of model performance
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics= ['accuracy'])
#train model
model.fit(X_train, y_train,validation_data=(X_test, y_test), epochs=5)
json.dump({'model':model.to_json()},open("model.json", "w"))
model.save_weights("model_weights.h5")
THEN I TRY TO PERFORM THE ATTACK WITH THE FOLLOWING CODE:
import json
import foolbox
import keras
import numpy as np
from keras import backend
from keras.models import load_model
from keras.datasets import mnist
from keras.utils import np_utils
from foolbox.attacks import FGSM
from foolbox.criteria import Misclassification
from foolbox.distances import MeanSquaredDistance
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
import numpy as np
import tensorflow as tf
from keras.models import model_from_json
import os
############## Loading the model and preprocessing #####################
backend.set_learning_phase(False)
model = tf.keras.models.model_from_json(json.load(open("model.json"))["model"],custom_objects={})
model.load_weights("model_weights.h5")
fmodel = foolbox.models.KerasModel(model, bounds=(0,1))
_,(images, labels) = mnist.load_data()
images = images.reshape(10000,28,28)
images= images.astype('float32')
images /= 255
######################### Attacking the model ##########################
attack=foolbox.attacks.FGSM(fmodel, criterion=Misclassification())
adversarial=attack(images[12],labels[12]) # for single image
adversarial_all=attack(images,labels) # for all the images
adversarial =adversarial.reshape(1,28,28,1) #reshaping it for model prediction
model_predictions = model.predict(adversarial)
print(model_predictions)
########################## Visualization ################################
images=images.reshape(10000,28,28)
adversarial =adversarial.reshape(28,28)
plt.figure()
plt.subplot(1,3,1)
plt.title('Original')
plt.imshow(images[12])
plt.axis('off')
plt.subplot(1, 3, 2)
plt.title('Adversarial')
plt.imshow(adversarial)
plt.axis('off')
plt.subplot(1, 3, 3)
plt.title('Difference')
difference = adversarial - images[124]
plt.imshow(difference / abs(difference).max() * 0.2 + 0.5)
plt.axis('off')
plt.show()
this error is shown when the adversarial examples are generated:
c_api.TF_GetCode(self.status.status))
InvalidArgumentError: Matrix size-incompatible: In[0]: [1,639232], In[1]: [1024,10]
[[{{node dense_4_5/MatMul}}]]
[[{{node dense_4_5/BiasAdd}}]]
What could it be?
here is my solution.
First of all modify the model code as follows
import tensorflow as tf
import json
# download mnist data and split into train and test sets
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
# reshape data to fit model
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
X_train, X_test = X_train/255, X_test/255
# one-hot encode target column
y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)
# create model
model = tf.keras.models.Sequential()
# add model layers
model.add(tf.keras.layers.Conv2D(32, kernel_size=(5, 5),
activation='relu', input_shape=(28, 28, 1)))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Conv2D(64, kernel_size=(5, 5), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(10, activation='softmax'))
# compile model using accuracy as a measure of model performance
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
# train model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=5)
json.dump({'model': model.to_json()}, open("model.json", "w"))
model.save_weights("model_weights.h5")
You just forgot to divide each pixel by the maximum value of RGB (255)
As for the attacker code
import json
import foolbox
from foolbox.attacks import FGSM
from foolbox.criteria import Misclassification
import numpy as np
import tensorflow as tf
############## Loading the model and preprocessing #####################
tf.enable_eager_execution()
tf.keras.backend.set_learning_phase(False)
model = tf.keras.models.model_from_json(
json.load(open("model.json"))["model"], custom_objects={})
model.load_weights("model_weights.h5")
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
_, (images, labels) = tf.keras.datasets.mnist.load_data()
images = images.reshape(images.shape[0], 28, 28, 1)
images = images/255
images = images.astype(np.float32)
fmodel = foolbox.models.TensorFlowEagerModel(model, bounds=(0, 1))
######################### Attacking the model ##########################
attack = foolbox.attacks.FGSM(fmodel, criterion=Misclassification())
adversarial = np.array([attack(images[0], label=labels[0])])
model_predictions = model.predict(adversarial)
print('real label: {}, label prediction; {}'.format(
labels[0], np.argmax(model_predictions)))
I used TensorFlowEagerModel instead of KerasModel for simplicty. The error you were encountering was due to the fact that model.predict expects a 4d matrix while you were passing a 3d matrix, so I just wrapped up the attack to the image example into a numpy array to make it 4d.
Hope it helps
I'm trying to train a baseline ANN model for a binary classification with Keras (tensorflow backend) and Jupyter notebooks.
The code is the following:
array=df6.values
X= array[:,0:384]
Y = array[:,385]
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
seed = 7
np.random.seed(seed)
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
def create_baseline():
model = Sequential()
model.add(Dense(60, input_dim=60, kernel_initializer='normal', activation='relu'))
model.add(Dense(10, kernel_initializer='normal', activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
estimator = KerasClassifier(build_fn=create_baseline, epochs=100, batch_size=5, verbose=0)
kfold = StratifiedKFold(n_splits=2, shuffle=True, random_state=seed)
results = cross_val_score(estimator, X, encoded_Y, cv=kfold)
print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
Finally the error is the following:
ValueError: Error when checking input: expected dense_5_input to have shape (None, 60) but got array with shape (8, 384)
Also my dataset has 18 rows and 385 columns
I would like to know how to reshape correctly for a correct estimation of results. Thank you so much!
input_dim = 384
This argument refers to the shape of your input, which is X.