First of all, I tried to look at other similar questions, but it has not fixed my issues.
I have a project which required me to use Data Augmentation to increase the samples and Transfer Learning with VGG16 to improve the accuracy. I found this Github link online https://github.com/sachinruk/deepschool.io/blob/master/DL-Keras_Tensorflow/Lesson%2013%20-%20Transfer%20Learning%20-%20Solutions.ipynb and I try to apply to my project, but the error is List index out of range. I already loaded the images in data --> train or test --> benign or malign. My image's shape is (360,560,3). I am using Tensorflow 2.1.0. Here is my code.
import random
import numpy as np
from numpy.random import randint as rdi
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from plotly import __version__
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
cf.go_offline()
import plotly.graph_objects as go
from plotly.offline import *
import plotly.offline as pyo
import chart_studio.plotly as py
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import tensorflow as tf
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPool2D, Flatten, MaxPooling2D, Activation, Reshape, BatchNormalization
from sklearn.metrics import mean_squared_error, mean_absolute_error, explained_variance_score
from tensorflow.keras.models import load_model
from tensorflow.keras.callbacks import EarlyStopping, TensorBoard
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.datasets import cifar10
import os
from matplotlib.image import imread
from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import warnings
from tensorflow.keras.preprocessing import image
from mpl_toolkits.mplot3d import Axes3D
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.layers import GaussianNoise
from keras import applications
import cv2
from os.path import isfile, isdir, getsize
from tqdm import tqdm
from urllib.request import urlretrieve
import zipfile
import tarfile
import pickle
import glob
import shutil
from os.path import isfile, isdir, getsize
from os import mkdir, makedirs, remove, listdir
# Try to download bottelneck file for later use
class DLProgress(tqdm):
last_block = 0
def hook(self, block_num=1, block_size=1, total_size=None):
self.total = total_size
self.update((block_num - self.last_block) * block_size)
self.last_block = block_num
class DLProgress(tqdm):
last_block = 0
def hook(self, block_num=1, block_size=1, total_size=None):
self.total = total_size
self.update((block_num - self.last_block) * block_size)
self.last_block = block_num
if not isfile('bottleneck_features_train.npy'):
with DLProgress(unit='B', unit_scale=True, miniters=1, desc='Bottleneck features') as pbar:
urlretrieve(
'https://www.dropbox.com/s/a38gpvdcryw0kfc/bottleneck.zip?dl=1',
'bottleneck.zip',
pbar.hook)
with zipfile.ZipFile('bottleneck.zip') as f:
f.extractall('./')
files = listdir('bottleneck 2/')
for f in files:
shutil.move('bottleneck 2/'+f,'./')
shutil.rmtree('bottleneck 2/')
remove('bottleneck.zip')
# Import the Ultrasound dataset
data_dir = 'C:\Spring 2020\Machine Learning and Computer Vision\data'
os.listdir(data_dir)
test_path = data_dir+'\\test\\'
train_path = data_dir+'\\train\\'
os.listdir(test_path)
os.listdir(train_path)
os.listdir(train_path+'\\malign')[0]
para_cell = train_path+'\\malign'+'\\53.jpg'
para_img= imread(para_cell)
para_img.shape
plt.imshow(para_img)
image_shape = (360,560,3)
# Generate some new images
datagen = ImageDataGenerator(rescale=1.0/255)
# Create model
model = applications.VGG16(include_top = False, input_shape = image_shape)
model = applications.VGG16(include_top = False, weights = 'imagenet')
model.summary()
with open('bottleneck_features_train.npy','rb') as f:
bottleneck_features_train = pickle.load(f)
bottleneck_features_train.shape
batch_size = 128
generator = datagen.flow_from_directory(
train_path,
target_size=image_shape[:2],
batch_size=batch_size,
class_mode=None,
shuffle=False)
batch_size = 128
valid_generator = datagen.flow_from_directory(
test_path,
target_size=image_shape[:2],
batch_size=batch_size,
class_mode=None,
shuffle=False)
# Use bottleneck file
with open('bottleneck_features_train.npy','rb') as f:
bottleneck_features_train = pickle.load(f)
# Apply model
model = Sequential()
model.add(Flatten(input_shape=bottleneck_features_train.shape[1:]))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy', metrics=['accuracy'])
model.summary()
batch_size = 128
# Set up the flow of training data
generator = datagen.flow_from_directory(
train_path,
target_size=image_shape[:2],
batch_size=batch_size,
class_mode=None,
shuffle=False)
generator.class_indices
# Set up the early stop to avoid training so many epochs, but won't increase the accuracy
early_stop = EarlyStopping(monitor='val_loss',patience=5)
warnings.filterwarnings('ignore')
# I think the code stucks here
results = model.fit(bottleneck_features_train, epochs=15, batch_size = batch_size)
# Save to vgg16
model.save('vgg16.h5')
losses = pd.DataFrame(model.history.history)
losses[['loss','val_loss']].plot()
model.metrics_names
model.evaluate_generator(test_image_gen)
#https://datascience.stackexchange.com/questions/13894/how-to-get-predictions-with-predict-generator-on-streaming-test-data-in-keras
pred_probabilities = model.predict_generator(test_image_gen)
#preds_num = pred_probabilities
test_image_gen.classes
predictions = pred_probabilities > 0.5
# Numpy can treat this as True/False for us
predictions
check = classification_report(test_image_gen.classes,predictions)
print(classification_report(test_image_gen.classes,predictions))
print(confusion_matrix(test_image_gen.classes,predictions))
para_cell
my_image = image.load_img(para_cell,target_size=image_shape)
my_image
type(my_image)
my_image = image.img_to_array(my_image)
type(my_image)
my_image.shape
my_image = np.expand_dims(my_image, axis=0)
my_image.shape
para_cell = test_path+'\\benign'+'\\40.jpg'
my_image = image.load_img(para_cell,target_size=image_shape)
my_image
type(my_image)
my_image = image.img_to_array(my_image)
type(my_image)
my_image.shape
my_image = np.expand_dims(my_image, axis=0)
my_image.shape
model.predict(my_image)
The error is:
runfile('C:/Spring 2020/Machine Learning and Computer Vision/hopefully.py', wdir='C:/Spring 2020/Machine Learning and Computer Vision')
Model: "vgg16"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_16 (InputLayer) (None, None, None, 3) 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, None, None, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, None, None, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, None, None, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, None, None, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, None, None, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, None, None, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, None, None, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, None, None, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, None, None, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, None, None, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, None, None, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, None, None, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, None, None, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, None, None, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, None, None, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, None, None, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, None, None, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, None, None, 512) 0
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
_________________________________________________________________
Found 2198 images belonging to 2 classes.
Found 800 images belonging to 2 classes.
Model: "sequential_7"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_7 (Flatten) (None, 4608) 0
_________________________________________________________________
dense_14 (Dense) (None, 256) 1179904
_________________________________________________________________
dropout_7 (Dropout) (None, 256) 0
_________________________________________________________________
dense_15 (Dense) (None, 1) 257
=================================================================
Total params: 1,180,161
Trainable params: 1,180,161
Non-trainable params: 0
_________________________________________________________________
Found 2198 images belonging to 2 classes.
Train on 19872 samples
Epoch 1/15
128/19872 [..............................] - ETA: 2sTraceback (most recent call last):
File "C:\Spring 2020\Machine Learning and Computer Vision\hopefully.py", line 188, in <module>
results = model.fit(bottleneck_features_train, epochs=15, batch_size = batch_size)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 819, in fit
use_multiprocessing=use_multiprocessing)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 342, in fit
total_epochs=epochs)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 128, in run_one_epoch
batch_outs = execution_function(iterator)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 98, in execution_function
distributed_function(input_fn))
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 568, in __call__
result = self._call(*args, **kwds)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 615, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 497, in _initialize
*args, **kwds))
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 2389, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 2703, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 2593, in _create_graph_function
capture_by_value=self._capture_by_value),
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 978, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 439, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 85, in distributed_function
per_replica_function, args=args)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py", line 763, in experimental_run_v2
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py", line 1819, in call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py", line 2164, in _call_for_each_replica
return fn(*args, **kwargs)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\autograph\impl\api.py", line 292, in wrapper
return func(*args, **kwargs)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 433, in train_on_batch
output_loss_metrics=model._output_loss_metrics)
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_eager.py", line 312, in train_on_batch
output_loss_metrics=output_loss_metrics))
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_eager.py", line 253, in _process_single_batch
training=training))
File "C:\Users\binhd\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_eager.py", line 167, in _model_loss
per_sample_losses = loss_fn.call(targets[i], outs[i])
IndexError: list index out of range
Like I mentioned above, I re-used some codes from online Github to apply to my previous codes. The codes I re-used are (In just copy and paste the codes above, so you might not need to look at this part. It is just for info):
# Try to download bottelneck file for later use
class DLProgress(tqdm):
last_block = 0
def hook(self, block_num=1, block_size=1, total_size=None):
self.total = total_size
self.update((block_num - self.last_block) * block_size)
self.last_block = block_num
class DLProgress(tqdm):
last_block = 0
def hook(self, block_num=1, block_size=1, total_size=None):
self.total = total_size
self.update((block_num - self.last_block) * block_size)
self.last_block = block_num
if not isfile('bottleneck_features_train.npy'):
with DLProgress(unit='B', unit_scale=True, miniters=1, desc='Bottleneck features') as pbar:
urlretrieve(
'https://www.dropbox.com/s/a38gpvdcryw0kfc/bottleneck.zip?dl=1',
'bottleneck.zip',
pbar.hook)
with zipfile.ZipFile('bottleneck.zip') as f:
f.extractall('./')
files = listdir('bottleneck 2/')
for f in files:
shutil.move('bottleneck 2/'+f,'./')
shutil.rmtree('bottleneck 2/')
remove('bottleneck.zip')
And (In just copy and paste the codes above, so you might not need to look at this part. It is just for info):
# Generate some new images
datagen = ImageDataGenerator(rescale=1.0/255)
# Create model
model = applications.VGG16(include_top = False, input_shape = image_shape)
model = applications.VGG16(include_top = False, weights = 'imagenet')
model.summary()
with open('bottleneck_features_train.npy','rb') as f:
bottleneck_features_train = pickle.load(f)
bottleneck_features_train.shape
batch_size = 128
generator = datagen.flow_from_directory(
train_path,
target_size=image_shape[:2],
batch_size=batch_size,
class_mode=None,
shuffle=False)
batch_size = 128
valid_generator = datagen.flow_from_directory(
test_path,
target_size=image_shape[:2],
batch_size=batch_size,
class_mode=None,
shuffle=False)
# Use bottleneck file
with open('bottleneck_features_train.npy','rb') as f:
bottleneck_features_train = pickle.load(f)
# Apply model
model = Sequential()
model.add(Flatten(input_shape=bottleneck_features_train.shape[1:]))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy', metrics=['accuracy'])
model.summary()
batch_size = 128
# Set up the flow of training data
generator = datagen.flow_from_directory(
train_path,
target_size=image_shape[:2],
batch_size=batch_size,
class_mode=None,
shuffle=False)
generator.class_indices
# Set up the early stop to avoid training so many epochs, but won't increase the accuracy
early_stop = EarlyStopping(monitor='val_loss',patience=5)
warnings.filterwarnings('ignore')
# I think the code stucks here
results = model.fit(bottleneck_features_train, epochs=15, batch_size = batch_size)
How can I fix this issue?
I was able to recreate your issue using the below code. The error occurs when you don't pass the target variable or label in model.fit() function.
Note - You can download the dataset I have used in the code from here.
Error Code -
# MLP for Pima Indians Dataset saved to single file
import numpy as np
from numpy import loadtxt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# load pima indians dataset
dataset = np.loadtxt("/content/pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# define model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Model Summary
model.summary()
# Fit the model
model.fit(X, epochs=150, batch_size=10, verbose=0)
# evaluate the model
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
Output -
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 12) 108
_________________________________________________________________
dense_1 (Dense) (None, 8) 104
_________________________________________________________________
dense_2 (Dense) (None, 1) 9
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-1-b9b0f557b27e> in <module>()
25
26 # Fit the model
---> 27 model.fit(X, epochs=150, batch_size=10, verbose=0)
28
29 # evaluate the model
1 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_arrays.py in fit_loop(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
185 indices_for_conversion_to_dense = []
186 for i in range(len(feed)):
--> 187 if issparse is not None and issparse(ins[i]) and not K.is_sparse(feed[i]):
188 indices_for_conversion_to_dense.append(i)
189
IndexError: list index out of range
Solution - Pass the target variable or label in model.fit along with training features.
Fixed Code -
# MLP for Pima Indians Dataset saved to single file
import numpy as np
from numpy import loadtxt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# load pima indians dataset
dataset = np.loadtxt("/content/pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# define model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Model Summary
model.summary()
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10, verbose=0)
# evaluate the model
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
Output -
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_3 (Dense) (None, 12) 108
_________________________________________________________________
dense_4 (Dense) (None, 8) 104
_________________________________________________________________
dense_5 (Dense) (None, 1) 9
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________
acc: 70.96%
Related
I've got this network thats using TF Hub's Elmo layer for a classification task. Oddly it starts the training but fails during the process with the error:
Unsupported object type int
import tensorflow_hub as hub
import tensorflow as tf
elmo = hub.Module("https://tfhub.dev/google/elmo/3", trainable=True)
from tensorflow.keras.layers import Input, Lambda, Bidirectional, Dense, Dropout, Flatten, LSTM
from tensorflow.keras.models import Model
def ELMoEmbedding(input_text):
return elmo(tf.reshape(tf.cast(input_text, tf.string), [-1]), signature="default", as_dict=True)["elmo"]
def build_model():
input_layer = Input(shape=(1,), dtype="string", name="Input_layer")
embedding_layer = Lambda(ELMoEmbedding, output_shape=(1024, ), name="Elmo_Embedding")(input_layer)
BiLSTM = Bidirectional(LSTM(128, return_sequences= False, recurrent_dropout=0.2, dropout=0.2), name="BiLSTM")(embedding_layer)
Dense_layer_1 = Dense(64, activation='relu')(BiLSTM)
Dropout_layer_1 = Dropout(0.5)(Dense_layer_1)
Dense_layer_2 = Dense(32, activation='relu')(Dropout_layer_1)
Dropout_layer_2 = Dropout(0.5)(Dense_layer_2)
output_layer = Dense(1, activation='sigmoid')(Dropout_layer_2)
model = Model(inputs=[input_layer], outputs=output_layer, name="BiLSTM with ELMo Embeddings")
model.summary()
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
return model
elmo_BiDirectional_model = build_model()
import numpy as np
import io
import re
from tensorflow import keras
i = 0
max_cells = 51
x_data = np.zeros((max_cells, 1), dtype='object')
y_data = np.zeros((max_cells, 1), dtype='float32')
with io.open('./data/names-sample.txt', encoding='utf-8') as f:
content = f.readlines()
for line in content:
line = re.sub("[\n]", " ", line)
x_data[i] = line
y_data[i] = .1 #testing!
i = i+1
with tf.Session() as session:
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
model_elmo = elmo_BiDirectional_model.fit(x_data, y_data, epochs=100, batch_size=5)
train_prediction = elmo_BiDirectional_model.predict(x_data)
Full error:
INFO:tensorflow:Saver not created because there are no variables in the graph to restore
INFO:tensorflow:Saver not created because there are no variables in the graph to restore
Model: "BiLSTM with ELMo Embeddings"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
Input_layer (InputLayer) [(None, 1)] 0
_________________________________________________________________
Elmo_Embedding (Lambda) (None, None, 1024) 0
_________________________________________________________________
BiLSTM (Bidirectional) (None, 256) 1180672
_________________________________________________________________
dense_43 (Dense) (None, 64) 16448
_________________________________________________________________
dropout_28 (Dropout) (None, 64) 0
_________________________________________________________________
dense_44 (Dense) (None, 32) 2080
_________________________________________________________________
dropout_29 (Dropout) (None, 32) 0
_________________________________________________________________
dense_45 (Dense) (None, 1) 33
=================================================================
Total params: 1,199,233
Trainable params: 1,199,233
Non-trainable params: 0
_________________________________________________________________
Train on 51 samples
Epoch 1/100
30/51 [================>.............] - ETA: 2s - loss: 0.5324 - acc: 0.0000e+00 Traceback (most recent call last):
File "C:\temp\Simon\TestElmo2.py", line 52, in <module>
model_elmo = elmo_BiDirectional_model.fit(x_data, y_data, epochs=100, batch_size=5)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 727, in fit
use_multiprocessing=use_multiprocessing)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_arrays.py", line 675, in fit
steps_name='steps_per_epoch')
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_arrays.py", line 394, in model_iteration
batch_outs = f(ins_batch)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3476, in __call__
run_metadata=self.run_metadata)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\client\session.py", line 1472, in __call__
run_metadata_ptr)
InternalError: Unsupported object type int
This turned out to be a data issue. I had an empty line in the dataset!
Please help me run the code. This doesn't work. I have been trying hard to solve this for days. The model_final.fit() also doesn't work. Why is this showing the same error for the method. This is almost the entire code.
EDIT: The train_generator and validation_generator added.
img_width, img_height = 400, 400
train_data_dir = "data/train"
validation_data_dir = "data/validation"
nb_train_samples = 4125
nb_validation_samples = 466
batch_size = 16,
epochs = 5
output_num_classes = 2
K.set_image_data_format('channels_last')
model = applications.VGG19(weights="imagenet", include_top=False, input_shape=(img_width, img_height, 3))
model.summary()
x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation="relu")(x)
predictions = Dense(output_num_classes, activation="softmax")(x)
model_final = Model(inputs=model.input, outputs=predictions)
model_final.compile(loss="categorical_crossentropy",
optimizer=optimizers.SGD(learning_rate=0.0001, momentum=0.9),
metrics=['accuracy'])
train_datagen = ImageDataGenerator(rescale=1./255,
zoom_range=0.3,
width_shift_range=0.3,
height_shift_range=0.3,
rotation_range=30,
horizontal_flip=True,
fill_mode='nearest')
train_generator = train_datagen.flow_from_directory(train_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical')
validation_datagen = ImageDataGenerator(rescale=1./255,
zoom_range=0.3,
width_shift_range=0.3,
height_shift_range=0.3,
rotation_range=30,
horizontal_flip=True,
fill_mode='nearest')
validation_generator = validation_datagen.flow_from_directory(validation_data_dir,
target_size=(img_height, img_width),
batch_size = batch_size,
class_mode='categorical')
The code above is before fitting.
model_final.fit_generator(train_generator,
steps_per_epoch=nb_train_samples,
epochs = epochs,
validation_data = validation_generator,
validation_steps = nb_validation_samples)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:1844: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
warnings.warn('`Model.fit_generator` is deprecated and '
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-87-b195f52ce8fb> in <module>()
3 epochs = epochs,
4 validation_data = validation_generator,
----> 5 validation_steps = nb_validation_samples)
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/iterator.py in __len__(self)
66
67 def __len__(self):
---> 68 return (self.n + self.batch_size - 1) // self.batch_size # round up
69
70 def on_epoch_end(self):
TypeError: unsupported operand type(s) for +: 'int' and 'tuple' ```
I was able to replicate your issue with sample code as shown below
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
import os
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential
from google.colab import drive
drive.mount('/content/drive')
train_dir = '/content/drive/My Drive/Dogs_Vs_Cats/train'
test_dir = '/content/drive/My Drive/Dogs_Vs_Cats/test'
img_width, img_height = 224, 224
input_shape = (img_width, img_height, 3)
epochs = 8
batch_size = 32,
train_datagen = ImageDataGenerator(
rescale = 1. /255,
horizontal_flip = True)
test_datagen = ImageDataGenerator(
rescale = 1. /255)
train_data = train_datagen.flow_from_directory(
train_dir,
target_size = (img_width, img_height),
batch_size = batch_size,
class_mode = 'binary')
test_data = test_datagen.flow_from_directory(
test_dir,
target_size = (img_width, img_height),
class_mode = 'binary')
Conv_Base = ResNet50(include_top = False, weights = 'imagenet', input_shape = input_shape)
model = Sequential()
model.add(Conv_Base)
model.add(Flatten())
model.add(Dense(units = 256, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 1, activation = 'sigmoid'))
model.summary()
model.compile(loss = 'binary_crossentropy',
optimizer = 'Adam',
metrics = [tf.keras.metrics.Precision(), tf.keras.metrics.Recall()])
model.fit(
train_data,
steps_per_epoch = 10,
epochs = 2,
validation_data = test_data,
verbose = 1,
validation_steps = 32)
Output:
Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
Found 2000 images belonging to 2 classes.
Found 1018 images belonging to 2 classes.
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
resnet50 (Functional) (None, 7, 7, 2048) 23587712
_________________________________________________________________
flatten (Flatten) (None, 100352) 0
_________________________________________________________________
dense (Dense) (None, 256) 25690368
_________________________________________________________________
dropout (Dropout) (None, 256) 0
_________________________________________________________________
dense_1 (Dense) (None, 1) 257
=================================================================
Total params: 49,278,337
Trainable params: 49,225,217
Non-trainable params: 53,120
_________________________________________________________________
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-5a5644117355> in <module>()
60 validation_data = test_data,
61 verbose = 1,
---> 62 validation_steps = 32)
3 frames
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/iterator.py in __len__(self)
66
67 def __len__(self):
---> 68 return (self.n + self.batch_size - 1) // self.batch_size # round up
69
70 def on_epoch_end(self):
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
Solution to fix:
As rightly suggested by #Dr. Snoopy, this issue was due to comma right after batch_size definition. It converts the integer into a tuple and which is not supported.
Changing batch_size = 32, to batch_size = 32 has resolved the issue.
Working code as shown below
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
import os
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential
from google.colab import drive
drive.mount('/content/drive')
train_dir = '/content/drive/My Drive/Dogs_Vs_Cats/train'
test_dir = '/content/drive/My Drive/Dogs_Vs_Cats/test'
img_width, img_height = 224, 224
input_shape = (img_width, img_height, 3)
epochs = 8
batch_size = 32 # removed comma
train_datagen = ImageDataGenerator(
rescale = 1. /255,
horizontal_flip = True)
test_datagen = ImageDataGenerator(
rescale = 1. /255)
train_data = train_datagen.flow_from_directory(
train_dir,
target_size = (img_width, img_height),
batch_size = batch_size,
class_mode = 'binary')
test_data = test_datagen.flow_from_directory(
test_dir,
target_size = (img_width, img_height),
class_mode = 'binary')
Conv_Base = ResNet50(include_top = False, weights = 'imagenet', input_shape = input_shape)
model = Sequential()
model.add(Conv_Base)
model.add(Flatten())
model.add(Dense(units = 256, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 1, activation = 'sigmoid'))
model.summary()
model.compile(loss = 'binary_crossentropy',
optimizer = 'Adam',
metrics = [tf.keras.metrics.Precision(), tf.keras.metrics.Recall()])
model.fit(
train_data,
steps_per_epoch = 10,
epochs = 2,
validation_data = test_data,
verbose = 1,
validation_steps = 32)
Output:
Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
Found 2000 images belonging to 2 classes.
Found 1018 images belonging to 2 classes.
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
resnet50 (Functional) (None, 7, 7, 2048) 23587712
_________________________________________________________________
flatten_1 (Flatten) (None, 100352) 0
_________________________________________________________________
dense_2 (Dense) (None, 256) 25690368
_________________________________________________________________
dropout_1 (Dropout) (None, 256) 0
_________________________________________________________________
dense_3 (Dense) (None, 1) 257
=================================================================
Total params: 49,278,337
Trainable params: 49,225,217
Non-trainable params: 53,120
_________________________________________________________________
Epoch 1/2
10/10 [==============================] - 107s 10s/step - loss: 9.5302 - precision_1: 0.6096 - recall_1: 0.6485 - val_loss: 14405.0479 - val_precision_1: 0.0000e+00 - val_recall_1: 0.0000e+00
Epoch 2/2
10/10 [==============================] - 68s 7s/step - loss: 2.2295 - precision_1: 0.8191 - recall_1: 0.8799 - val_loss: 3977.1418 - val_precision_1: 0.4912 - val_recall_1: 1.0000
<tensorflow.python.keras.callbacks.History at 0x7fbba005cc50>
I have the following code, I need to remove some layers of the model and perform prediction. But currently I am retrieving error.
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
from keras.models import Model
from tensorflow.python.keras.optimizers import SGD
base_model = ResNet50(include_top=False, weights='imagenet')
model= Model(inputs=base_model.input, outputs=base_model .layers[-2].output)
#model = Model(inputs=base_model.input, outputs=predictions)
#Compiling the model
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics =
['accuracy'])
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
#decode the results into a list of tuples (class, description, probability)
#(one such list for each sample in the batch)
print('Predicted:', decode_predictions(preds, top=3)[0])
error
File "C:/Users/learn/remove_layer.py", line 9, in <module>
model= Model(inputs=base_model.input, outputs=base_model .layers[-2].output)
AttributeError: 'Tensor' object has no attribute '_keras_shape'
Due to my beginner's knowledge in Keras what I understood is the shape issue. Since its a resnet model, if I remove a layer from one merge to another merge layer, because merge layer doesn't have dimension issues, how can I accomplish this?
You actually need to visualize what you have done, so lets do little summary for last layers of ResNet50 Model:
base_model.summary()
conv5_block3_2_relu (Activation (None, None, None, 5 0 conv5_block3_2_bn[0][0]
__________________________________________________________________________________________________
conv5_block3_3_conv (Conv2D) (None, None, None, 2 1050624 conv5_block3_2_relu[0][0]
__________________________________________________________________________________________________
conv5_block3_3_bn (BatchNormali (None, None, None, 2 8192 conv5_block3_3_conv[0][0]
__________________________________________________________________________________________________
conv5_block3_add (Add) (None, None, None, 2 0 conv5_block2_out[0][0]
conv5_block3_3_bn[0][0]
__________________________________________________________________________________________________
conv5_block3_out (Activation) (None, None, None, 2 0 conv5_block3_add[0][0]
==================================================================================================
Total params: 23,587,712
Trainable params: 23,534,592
Non-trainable params: 53,120
_____________________________
And now your model after removing last layer
model.summary()
conv5_block3_2_relu (Activation (None, None, None, 5 0 conv5_block3_2_bn[0][0]
__________________________________________________________________________________________________
conv5_block3_3_conv (Conv2D) (None, None, None, 2 1050624 conv5_block3_2_relu[0][0]
__________________________________________________________________________________________________
conv5_block3_3_bn (BatchNormali (None, None, None, 2 8192 conv5_block3_3_conv[0][0]
__________________________________________________________________________________________________
conv5_block3_add (Add) (None, None, None, 2 0 conv5_block2_out[0][0]
conv5_block3_3_bn[0][0]
==================================================================================================
Total params: 23,587,712
Trainable params: 23,534,592
Non-trainable params: 53,120
Reset50 in keras output is all the feature map after the last Conv2D blocks it doesn't care about the classfication part of your model, what you actualy did is that you just removed the last activation layer after the last addition block
So you need check more which block layer you wanna remove and add flatten and fully connected layer for the classfication part
Also as mentioned by Dr.Snoopy, dont mix imports between keras and tensorflow.keras
# this part
from tensorflow.keras.models import Model
Trying to do binary image classification using VGG16 for transfer learning. This is my code for training:
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Activation, Dense
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
import os
cwd = os.getcwd()
vgg16_model = tf.keras.applications.VGG16(
include_top=True,
weights="imagenet",
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
classifier_activation="softmax",
)
vgg16_model._layers.pop()
model = Sequential()
for layer in vgg16_model.layers:
model.add(layer)
for layer in model.layers:
layer.trainable = False
model.add(Dense(4096, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.summary()
model.compile(optimizer=Adam(lr=.0001), loss='binary_crossentropy', metrics=['accuracy'])
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
cwd + '/images/dd_notadd/train',
target_size=(224, 224),
batch_size=32,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
cwd + '/images/dd_notadd/validation',
target_size=(224, 224),
batch_size=32,
class_mode='binary')
model.fit(
train_generator,
steps_per_epoch=10,
epochs=20,
validation_data=validation_generator,
validation_steps=10)
model.save("ddornot.h5")
Then this is my code for inference:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 7 11:12:16 2021
#author: lennartkonst
"""
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Activation, Dense
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
import os
import numpy as np
import cv2
cwd = os.getcwd()
vgg16_model = tf.keras.applications.VGG16(
include_top=True,
weights="imagenet",
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
classifier_activation="softmax",
)
vgg16_model._layers.pop()
model = Sequential()
for layer in vgg16_model.layers:
model.add(layer)
for layer in model.layers:
layer.trainable = False
model.add(Dense(4096, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.summary()
model.compile(optimizer=Adam(lr=.0001), loss='binary_crossentropy', metrics=['accuracy'])
model.load_weights("ddornot.h5")
testimagefilename = cwd + '/images/dd_notadd/testimage/testimage.jpeg'
img = cv2.imread(testimagefilename)
img = cv2.resize(img,(224,224))
img = np.expand_dims(img, axis=0)
print(img.shape)
img_class = model.predict(img, batch_size=1)
I get the following output on this inference script:
Model: "sequential_29"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, 7, 7, 512) 0
_________________________________________________________________
flatten (Flatten) (None, 25088) 0
_________________________________________________________________
fc1 (Dense) (None, 4096) 102764544
_________________________________________________________________
fc2 (Dense) (None, 4096) 16781312
_________________________________________________________________
dense_58 (Dense) (None, 4096) 16781312
_________________________________________________________________
dense_59 (Dense) (None, 2) 8194
=================================================================
Total params: 151,050,050
Trainable params: 16,789,506
Non-trainable params: 134,260,544
_________________________________________________________________
(1, 224, 224, 3)
WARNING:tensorflow:7 out of the last 7 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7fb1487b9280> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating #tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your #tf.function outside of the loop. For (2), #tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.
Traceback (most recent call last):
File "/Users/lennartkonst/Documents/Twitterbots/Pythonbots/DreamDestinationsBot/ML/ddornotinference.py", line 60, in <module>
img_class = model.predict(img, batch_size=1)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 130, in _method_wrapper
return method(self, *args, **kwargs)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1614, in predict
all_outputs = nest.map_structure_up_to(batch_outputs, concat, outputs)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/nest.py", line 1135, in map_structure_up_to
return map_structure_with_tuple_paths_up_to(
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/nest.py", line 1234, in map_structure_with_tuple_paths_up_to
results = [func(*args, **kwargs) for args in zip(flat_path_list,
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/nest.py", line 1234, in <listcomp>
results = [func(*args, **kwargs) for args in zip(flat_path_list,
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/nest.py", line 1137, in <lambda>
lambda _, *values: func(*values), # Discards the path arg.
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 2673, in concat
return array_ops.concat(tensors, axis=axis)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
return target(*args, **kwargs)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py", line 1654, in concat
return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1207, in concat_v2
_ops.raise_from_not_ok_status(e, name)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 6843, in raise_from_not_ok_status
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
InvalidArgumentError: Value for attr 'N' of 1 must be at least minimum 2
; NodeDef: {{node ConcatV2}}; Op<name=ConcatV2; signature=values:N*T, axis:Tidx -> output:T; attr=N:int,min=2; attr=T:type; attr=Tidx:type,default=DT_INT32,allowed=[DT_INT32, DT_INT64]> [Op:ConcatV2] name: concat
I don't fully understand what the problem is, I tried several different shapes but I figure the input shape should be just 1,224,224,3.
Any help would be greatly appreciated.
Your generators specify class_mode as binary and you specify the loss as 'binary_crossentropy'. Therefore your dense layer at the top of the model should have a single neuron and the activation='sigmoid. Why do you bother to recreate the whole model for inference since you saved the entire model just load it with
model= keras.models.load_model("ddornot.h5")
You are reading in your test image using cv2. In that case if you have a color image it is in BGR format. Your model was trained on RGB images so you have to convert it with
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
Note in your code for vgg16 you should have set
image_shape=(224,224,3)
Also you can try this for your model
base_model=tf.keras.applications.VGG19( include_top=False, input_shape=(224,224,3), pooling='max', weights='imagenet' )
for layer in base_model.layers:
layer.trainable=False
x=base_model.output
x=tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001 )(x)
x= tf.keras.layers.Dense(1024, activation='relu)(x)
x= tf.keras.layers.Dropout(.2)(x)
predictions=tf.keras.layers.Deense(1, activation='sigmoid)(x)
model=Model(inputs=base_model.input, outputs=predictions)
model.compile(optimizer=Adam(lr=.0001), loss='binary_crossentropy', metrics=['accuracy'])
I finally found a workaround the error. It's still not preferable off course so a good answer where I can just use model.predict() would be awesome.
import tensorflow as tf
from tensorflow import keras
# from keras.models import Sequential
# from keras.layers import Activation, Dense
# from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
import os
# import numpy as np
# import cv2
cwd = os.getcwd()
model= keras.models.load_model("ddornot.h5")
model.summary()
validation_datagen = ImageDataGenerator(rescale=1/255)
# Flow validation images in batches of 19 using valid_datagen generator
validation_generator = validation_datagen.flow_from_directory(
cwd + '/images/dd_notadd/testimage', # This is the source directory for validation images
classes = ['dd', 'notadd'],
target_size=(224, 224), # All images will be resized to 200x200
batch_size=1,
# Use binary labels
class_mode='binary',
shuffle=False)
STEP_SIZE_TEST=validation_generator.n//validation_generator.batch_size
validation_generator.reset()
preds = model.predict(validation_generator,
verbose=1)
print(preds)
# testimagefilename = cwd + '/images/dd_notadd/testimage/testimage.jpeg'
# img = cv2.imread(testimagefilename)
# img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# img = cv2.resize(img,(224,224))
# img = np.expand_dims(img, axis=0)
# print(img.shape)
# img_class = model.predict(img)
I've trained a model with pre-trained word embeddings like this:
embedding_matrix = np.zeros((vocab_size, 100))
for word, i in text_tokenizer.word_index.items():
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
embedding_layer = Embedding(vocab_size,
100,
embeddings_initializer=Constant(embedding_matrix),
input_length=50,
trainable=False)
With the architecture looking like this:
sequence_input = Input(shape=(50,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
text_cnn = Conv1D(filters=5, kernel_size=5, padding='same', activation='relu')(embedded_sequences)
text_lstm = LSTM(500, return_sequences=True)(embedded_sequences)
char_in = Input(shape=(50, 18, ))
char_cnn = Conv1D(filters=5, kernel_size=5, padding='same', activation='relu')(char_in)
char_cnn = GaussianNoise(0.40)(char_cnn)
char_lstm = LSTM(500, return_sequences=True)(char_in)
merged = concatenate([char_lstm, text_lstm])
merged_d1 = Dense(800, activation='relu')(merged)
merged_d1 = Dropout(0.5)(merged_d1)
text_class = Dense(len(y_unique), activation='softmax')(merged_d1)
model = Model([sequence_input,char_in], text_class)
When I go to convert the model to json, I get this error:
ValueError: can only convert an array of size 1 to a Python scalar
Similarly, if I use the model.save() function, it seems to save correctly, but when I go to load it, I get Type Error: Expected Float32.
My question is: is there something I am missing when trying to serialize this model? Do I need some sort of Lambda layer or something of the sorts?
Any help would be greatly appreciated!
You can use the weights argument in Embedding layer to provide initial weights.
embedding_layer = Embedding(vocab_size,
100,
weights=[embedding_matrix],
input_length=50,
trainable=False)
The weights should remain non-trainable after model saving/loading:
model.save('1.h5')
m = load_model('1.h5')
m.summary()
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_3 (InputLayer) (None, 50) 0
__________________________________________________________________________________________________
input_4 (InputLayer) (None, 50, 18) 0
__________________________________________________________________________________________________
embedding_1 (Embedding) (None, 50, 100) 1000000 input_3[0][0]
__________________________________________________________________________________________________
lstm_4 (LSTM) (None, 50, 500) 1038000 input_4[0][0]
__________________________________________________________________________________________________
lstm_3 (LSTM) (None, 50, 500) 1202000 embedding_1[0][0]
__________________________________________________________________________________________________
concatenate_2 (Concatenate) (None, 50, 1000) 0 lstm_4[0][0]
lstm_3[0][0]
__________________________________________________________________________________________________
dense_2 (Dense) (None, 50, 800) 800800 concatenate_2[0][0]
__________________________________________________________________________________________________
dropout_2 (Dropout) (None, 50, 800) 0 dense_2[0][0]
__________________________________________________________________________________________________
dense_3 (Dense) (None, 50, 15) 12015 dropout_2[0][0]
==================================================================================================
Total params: 4,052,815
Trainable params: 3,052,815
Non-trainable params: 1,000,000
__________________________________________________________________________________________________
I hope you are saving the model after compiling. Like:
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
To save model, you can do:
from keras.models import load_model
model.save('model.h5')
model = load_model('model_detect1.h5')
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
To load model,
from keras.models import model_from_json
json_file = open('model.json', 'r')
model_json = json_file.read()
model = model_from_json(model_json)
model.load_weights("model.h5")
I tried multiple methods . The problem is when we work in the embedding layer, then pickle doesnt work, and is not able to save the data.
SO what you can do , when you have some layers like these:-
## Creating model
embedding_vector_features=100
model=Sequential()
model.add(Embedding(voc_size,embedding_vector_features,input_length=sent_length))
model.add(LSTM(100))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
print(model.summary())
then, u can use
h5 extension to d=save file, and then convert that to json, model converetd to model2 here
from tensorflow.keras.models import load_model
model.save('model.h5')
model = load_model('model.h5')
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
and this to load data:-
from tensorflow.keras.models import model_from_json
json_file = open('model.json', 'r')
model_json = json_file.read()
model2 = model_from_json(model_json)
model2.load_weights("model.h5")