ValueError: Error when checking input: expected input_44 to have 4 dimensions, but got array with shape (0, 1) - python-3.x

I am new to CNNs, keras and tf. I am trying to build SqueezNet for keras 2.0 as shown here https://github.com/DT42/squeezenet_demo. It didnĀ“t work for me, so I just tried to make only one layer network and again failed. Below is the code for one layer.
As input I use white-black images 90*90 with 2 classes.
I am a bit lost cause I have tried and read many different things and not sure how to fix this.
import h5py
from keras.models import Model
from keras.layers import Input, Activation, Concatenate
from keras.layers import Flatten, Dropout, Dense
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import AveragePooling2D
from keras import backend as K
import numpy as np
import cv2
import glob
from sklearn.model_selection import train_test_split
class_names = {
"class_A": 0,
"class_B": 1
}
#all imgs in one file
X = list()
y = list()
for img_folder in ["class_A", "class_B"]:
for img in glob.glob("path" + img_folder + "*.jpg"):
input_img = cv2.imread(img)
X.append(input_img)
y.append(class_names[img_folder])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
input_img = Input(shape=(90,90,1))
b = Dense(32)(input_img)
model = Model(inputs=input_img, outputs=b)
print(model.summary())
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=["accuracy"])
model.fit(X_train, y_train,
epochs=20, batch_size=32)

Related

ValueError: Could not interpret optimizer identifier: []

Hi i'm working on Image classification using XGBoost and VGG16 imagenet as feature extractor here's my code
I tried to implement feature extractor on image dataset from kaggle CK+48 with tensorflow 2.11.0. I'm facing this error,
ValueError: Could not interpret optimizer identifier: []
have tried alot to solve it, will appreciate the help
import numpy as np
import matplotlib.pyplot as plt
import glob
import cv2
import keras
from tensorflow.keras import Model
#from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Activatation
from keras.models import Model, Sequential
from keras.models import load_model
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.layers import BatchNormalization
import os
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, BatchNormalization
from keras.callbacks import TensorBoard
from keras.callbacks import ModelCheckpoint
#from keras.optimizers import adam
import seaborn as sns
from keras.applications.vgg16 import VGG16
# Read input images and assign labels based on folder names
print(os.listdir("C:/Users/Tanzeel ur Rehman/Desktop/CK+48"))
SIZE = 256 #Resize
#Capture training data and labels into respective lists
train_images = []
train_labels = []
for directory_path in glob.glob("C:/Users/Tanzeel ur Rehman/Desktop/CK+48/train"):
label = directory_path.split("\\")[-1]
print(label)
for img_path in glob.glob(os.path.join(directory_path, "*.jpg")):
print(img_path)
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
img = cv2.resize(img, (SIZE, SIZE))
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
train_images.append(img)
train_labels.append(label)
#Convert lists to arrays
train_images = np.array(train_images)
train_labels = np.array(train_labels)
# Capture test/validation data and labels into respective lists
test_images = []
test_labels = []
for directory_path in glob.glob("C:/Users/Tanzeel ur Rehman/Desktop/CK+48/test"):
fruit_label = directory_path.split("\\")[-1]
for img_path in glob.glob(os.path.join(directory_path, "*.jpg")):
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
img = cv2.resize(img, (SIZE, SIZE))
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
test_images.append(img)
test_labels.append(fruit_label)
#Convert lists to arrays
test_images = np.array(test_images)
test_labels = np.array(test_labels)
#Encode labels from text to integers.
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(test_labels)
test_labels_encoded = le.transform(test_labels)
le.fit(train_labels)
train_labels_encoded = le.transform(train_labels)
#Split data into test and train datasets (already split but assigning to meaningful convention)
x_train, y_train, x_test, y_test = train_images, train_labels_encoded, test_images, test_labels_encoded
###################################################################
# Normalize pixel values to between 0 and 1
x_train, x_test = x_train / 255.0, x_test / 255.0
#One hot encode y values for neural network.
#from keras.utils import to_categorical
#y_train_one_hot = to_categorical(y_train)
#y_test_one_hot = to_categorical(y_test)
#############################
#Load model wothout classifier/fully connected layers
VGG_model = VGG16(weights='imagenet', include_top=False, input_shape=(SIZE, SIZE, 3))
#Make loaded layers as non-trainable. This is important as we want to work with pre-trained weights
for layer in VGG_model.layers:
layer.trainable = True
VGG_model.summary() #Trainable parameters will be 0
#Now, let us use features from convolutional network for RF
feature_extractor=VGG_model.predict(x_train)
The error is in the last line of the code, when I try to use features from convolutional network for random Forests on training dataset of images.

How can I get the history of the KerasRegressor?

I want to get KerasRegressor history but all the time I get (...) object has no attribute 'History'
'''
# Regression Example With Boston Dataset: Standardized and Wider
import numpy as np
from pandas import read_csv
from keras.models import Sequential
from keras.layers import Dense
#from keras.wrappers.scikit_learn import KerasRegressor
from scikeras.wrappers import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
import keras.backend as K
# load dataset
dataframe = read_csv("Data 1398-2.csv")
dataset = dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,0:10]
Y = dataset[:,10]
############
from sklearn import preprocessing
from sklearn.metrics import r2_score
min_max_scaler = preprocessing.MinMaxScaler()
X_scale = min_max_scaler.fit_transform(X)
from sklearn.model_selection import train_test_split
X_train, X_val_and_test, Y_train, Y_val_and_test = train_test_split(X_scale, Y, test_size=0.25)
X_val, X_test, Y_val, Y_test = train_test_split(X_val_and_test, Y_val_and_test, test_size=0.55)
##################
# define wider model
def wider_model():
# create model
model = Sequential()
model.add(Dense(40, input_dim=10, kernel_initializer='normal', activation='relu'))
model.add(Dense(20, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
# Compile model
model.compile(loss='mean_squared_error',metrics=['mae'], optimizer='adam')
#history = model.fit(X, Y, epochs=10, batch_size=len(X), verbose=1)
return model
# evaluate model with standardized dataset
from keras.callbacks import History
estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp',KerasRegressor(model=wider_model, epochs=100, batch_size=2, verbose=0) ))
pipeline = Pipeline(estimators)
kfold = KFold(n_splits=5)
results = cross_val_score(pipeline, X_train, Y_train, cv=kfold)
print("Wider: %.2f (%.2f) MSE" % (results.mean(), results.std()))
import matplotlib.pyplot as plt
#plt.plot(history.history['loss'])
#plt.plot(history.history['val_loss'])
#plt.title('Model loss')
#plt.ylabel('Loss')
#plt.xlabel('Epoch')
#plt.legend(['Train', 'Val'], loc='upper right')
#plt.show()
'''
Model is at index 1 in your case, but you can also find it. Now to get history object:
pipeline.steps[1][1].model.history.history
If you are sure that Keras Model is always the last estimator, you can also use:
pipeline._final_estimator.model.history.history

Module object is not callable (kerastuner)

I am trying to optimize my keras NN using kerastuner but for some reason its giving me a 'module' object is not callable error on the tuner=randomsearch line.
import pandas as pd
import numpy as np
from sklearn.preprocessing import scale
from keras.models import Sequential
from keras.layers import Dense
from sklearn.metrics import mean_squared_error
from matplotlib import pyplot as plt
from keras_tuner.tuners import randomsearch
from keras_tuner.engine import hyperparameters
import time
LOG_DIR = f"{int(time.time())}"
def build_model(hp):
model = Sequential()
model.add(Dense(hp.Int("input_units", min_value=1, max_value=105, step=1), input_dim=X_train.shape[1], activation='relu'))
for i in range(hp.Int("n_layers", 1, 5)):
model.add(Dense(hp.Int(f"conv_{i}_units", min_value=1, max_value=105, step=1), activation='relu'))
model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])
return model
tuner = randomsearch(build_model, objective = "val_accuracy", max_trials = 1, executions_per_trial = 1, directory = LOG_DIR)
tuner.search(x=X_train, y=y_train, epochs=1, batch_size=146, validation_data=(X_test, y_test))
I figured it out I imported the wrong randomsearch and hyperparameters.

How To Do Model Predict Using Distributed Dask With a Pre-Trained Keras Model?

I am loading my pre-trained keras model and then trying to parallelize a large number of input data using dask? Unfortunately, I'm running into some issues with this relating to how I'm creating my dask array. Any guidance would be greatly appreciated!
Setup:
First I cloned from this repo https://github.com/sanchit2843/dlworkshop.git
Reproducible Code Example:
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.model_selection import train_test_split
from keras.models import load_model
import keras
from keras.models import Sequential
from keras.layers import Dense
from dask.distributed import Client
import warnings
import dask.array as DaskArray
warnings.filterwarnings('ignore')
dataset = pd.read_csv('data/train.csv')
X = dataset.drop(['price_range'], axis=1).values
y = dataset[['price_range']].values
# scale data
sc = StandardScaler()
X = sc.fit_transform(X)
ohe = OneHotEncoder()
y = ohe.fit_transform(y).toarray()
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2)
# Neural network
model = Sequential()
model.add(Dense(16, input_dim=20, activation="relu"))
model.add(Dense(12, activation="relu"))
model.add(Dense(4, activation="softmax"))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=100, batch_size=64)
# Use dask
client = Client()
def load_and_predict(input_data_chunk):
def contrastive_loss(y_true, y_pred):
margin = 1
square_pred = K.square(y_pred)
margin_square = K.square(K.maximum(margin - y_pred, 0))
return K.mean(y_true * square_pred + (1 - y_true) * margin_square)
mlflow.set_tracking_uri('<uri>')
mlflow.set_experiment('clean_parties_ml')
runs = mlflow.search_runs()
artifact_uri = runs.loc[runs['start_time'].idxmax()]['artifact_uri']
model = mlflow.keras.load_model(artifact_uri + '/model', custom_objects={'contrastive_loss': contrastive_loss})
y_pred = model.predict(input_data_chunk)
return y_pred
da_input_data = da.from_array(X_test, chunks=(100, None))
prediction_results = da_input_data.map_blocks(load_and_predict, dtype=X_test.dtype).compute()
The Error I'm receiving:
AttributeError: '_thread._local' object has no attribute 'value'
Keras/Tensorflow don't play nicely with other threaded systems. There is an ongoing issue on this topic here: https://github.com/dask/dask-examples/issues/35

Keras RNN - ValueError when checking Input

I recently got a Nvidia Card and wanted to try LSTM-Models with the new GPU-Support. Sadly I do not know much about LSTMs. And I build this little model to test it:
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import Normalizer, StandardScaler
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.visible_device_list = "0"
set_session(tf.Session(config=config))
data = pd.read_excel("./data/google_data.xlsx", header=0)
X = data.drop("Close", axis=1)
y = data["Close"]
model = Sequential()
model.add(LSTM(units=10, activation='sigmoid', input_shape=(4,1),return_sequences=True))
model.add(Dropout(0.4))
model.add(Dense(10, activation="sigmoid"))
model.add(Dense(1, activation="sigmoid"))
model.compile(optimizer='adam', loss='mean_squared_error')
print(model.summary())
normalizer = StandardScaler()
normalizer.fit(X)
X = normalizer.fit_transform(X)
X_train, y_train, X_test, y_test = train_test_split(X, y, test_size=0.20, shuffle=False)
model.fit(X, y, batch_size=64, epochs=40, verbose=1, validation_data=(X_test, y_test))
I always get an ValueError, I have tried the Inputshape from the Keras Docs w ith (batch_size,timesteps,features) but I still get the same ValueError.
I guess it is probably a quite dumb problem, but a newbie like me could need some help. Thanks!

Resources