Logits and labels must be broadcastable error? - python

I am very new to deep learning. I am training on anime illustration images and I am receiving the error: logits and labels must be broadcastable: logits_size=[214,2] labels_size=[214,173]
I am sure there are other errors in my code, but I am unsure where to look. I ran model.summary() and noticed
Total params: 12,219,618
Trainable params: 7,080,962
Non-trainable params: 5,138,656
I could really appreciate any help. Thanks.
import tensorflow as tf
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Activation, Dropout, Flatten, Dense
from tensorflow.keras import backend as K
from tensorflow.keras import metrics, optimizers
import matplotlib.pyplot as plt
train_datagen = ImageDataGenerator(
rescale=1. / 255,
rotation_range = 30,
zoom_range = 0.2,
width_shift_range=0.1,
height_shift_range=0.1,
validation_split = 0.15)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size = (75,75),
batch_size = 214,
class_mode = 'categorical',
subset='training')
#validation_generator = test_datagen.flow_from_directory(
# validation_dir,
# target_size = (75,75),
# batch_size = 37,
# class_mode = 'categorical',
# subset = 'validation')
test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=(75,75),
batch_size = 32,
class_mode = 'categorical')
# Inspect batch
sample_training_images, _ = next(train_generator)
from tensorflow.keras.applications.inception_v3 import InceptionV3
def model_output_for_TL (pre_trained_model, last_output):
x = Flatten()(last_output)
# Dense hidden layer
x = Dense(1024, activation='relu')(x)
x = Dropout(0.5)(x)
# Output neuron.
x = Dense(2, activation='softmax')(x)
model = Model(pre_trained_model.input, x)
return model
pre_trained_model = InceptionV3(input_shape = (75, 75, 3),
include_top = False,
classes=173,
weights = 'imagenet')
for layer in pre_trained_model.layers:
layer.trainable = False
last_layer = pre_trained_model.get_layer('mixed5')
last_output = last_layer.output
model_TL = model_output_for_TL(pre_trained_model, last_output)
model_TL.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
history_TL = model_TL.fit(
train_generator,
steps_per_epoch=10,
epochs=60,
verbose=2)
#validation_data = validation_generator)
tf.keras.models.save_model(model_TL,'my_model.hdf5')

Related

How do I use tf.keras.preprocessing.image_dataset_from_directory() to create a dataset with a certain shape?

I have a dataset of around 3500 images, divided into 3 folders, that I loaded into Google Collab from my google drive, and I'm trying to make them into an ML algorithm using keras and tensorflow with the following code:
train = tf.keras.preprocessing.image_dataset_from_directory(
path,
labels = "inferred",
label_mode = "categorical",
color_mode = "rgb",
batch_size = 32,
image_size = (140,140),
seed = 1234,
subset = "training",
validation_split = 0.2
)
shape = tf.TensorShape([None,140,140,3])
print(shape)
valid = tf.keras.preprocessing.image_dataset_from_directory(
path,
labels = "inferred",
label_mode = "categorical",
color_mode = "rgb",
batch_size = 32,
image_size = (140,140),
seed = 1234,
subset = "validation",
validation_split = 0.2
)
print(train)
print(valid)
print(tf.keras.utils.image_dataset_from_directory(path, labels='inferred'))
from keras.models import Sequential
from keras.layers import Dense
from tensorflow import keras
#from tensorflow.keras import layers
model = Sequential()
model.add(Dense(256, activation = "softmax", input_shape = (140,140,3)))
model.add(Dense(64, activation = "softmax"))
model.add(Dense(32, activation = "softmax"))
#model.add(Dense(3, activation = "softmax"))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
print(model.summary())
keras.utils.plot_model(model, "my_first_model_with_shape_info.png", show_shapes=True)
#print(tf.keras.utils.plot_model(model))
model.fit(train, validation_data = valid, epochs = 50, batch_size = 32)
However when I run the code I get this error:
ValueError: Shapes (None, 3) and (None, 140, 140, 32) are incompatible
I tried fixing this by adding the (None,140,140,3) shape to the "train" variable but I'm not sure how to do that, so does anyone know how to make the shape of my "train" and "valid" variables compatible with the model I made? Thank you.
For reference this is the train variable:
train = tf.keras.preprocessing.image_dataset_from_directory(
path,
labels = "inferred",
label_mode = "categorical",
color_mode = "rgb",
batch_size = 32,
image_size = (140,140),
seed = 1234,
subset = "training",
validation_split = 0.2
)
when I print "train" out however, I get this
<BatchDataset element_spec=(TensorSpec(shape=(None, 140, 140, 3), dtype=tf.float32, name=None), TensorSpec(shape=(None, 3), dtype=tf.float32, name=None))>
So can someone also explain what a BatchDataset element is, and how do I edit its shape in the first place? thanks.
No neurons in the last layer should be same as the number of classes you want to classify (it should be 3 if you are trying to classify 3 types of flowers not 32) . Added a few convolution layers and pooling layers to improve the performance too.
import tensorflow as tf
from tensorflow.keras import layers
import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
path = pathlib.Path(data_dir)
train = tf.keras.preprocessing.image_dataset_from_directory(
path,
labels = "inferred",
label_mode = "categorical",
color_mode = "rgb",
batch_size = 32,
image_size = (140,140),
seed = 1234,
subset = "training",
validation_split = 0.2
)
shape = tf.TensorShape([None,140,140,3])
print(shape)
valid = tf.keras.preprocessing.image_dataset_from_directory(
path,
labels = "inferred",
label_mode = "categorical",
color_mode = "rgb",
batch_size = 32,
image_size = (140,140),
seed = 1234,
subset = "validation",
validation_split = 0.2
)
classnames = train.class_names
print(classnames)
print(train)
print(valid)
num_classes = len(classnames)
print(tf.keras.utils.image_dataset_from_directory(path, labels='inferred'))
from keras.models import Sequential
from keras.layers import Dense
from tensorflow import keras
#from tensorflow.keras import layers
model = Sequential([
layers.Rescaling(1./255, input_shape=(140,140, 3)),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes) // in your case layers.Dense(3)
])
model.compile(loss = tf.keras.losses.CategoricalCrossentropy(),optimizer = 'adam', metrics = ['accuracy'])
print(model.summary())
keras.utils.plot_model(model, "my_first_model_with_shape_info.png", show_shapes=True)
#print(tf.keras.utils.plot_model(model))
model.fit(train, validation_data = valid, epochs = 50, batch_size = 32)

Poor Validation Acc and High Validation Loss for resnet50

After trying out VGG16 and having really good results, I was trying to train a ResNet50 Model from Imagenet. First I set all layers to trainable because I have a large Dataset and did the same with VGG16, but my results were quite bad.
Then I tried to set the layers to not trainable and see if it gets better, but the results were still bad.
My original images are of size 384x384 but I resized them to 224x224. Is that the issue? Or did I do something wrong while implementing it?
from keras import Input, Model
from keras.applications import ResNet50
from keras.layers import AveragePooling2D, Flatten, Dense, Dropout
from keras.optimizers import Adam
from keras_preprocessing.image import ImageDataGenerator
class example:
def __init__(self):
# define the names of the classes
self.CLASSES = ["nok", "ok"]
# initialize the initial learning rate, batch size, and number of
# epochs to train for
self.INIT_LR = 1e-4
self.BS = 32
self.NUM_EPOCHS = 32
def build_model(self, train_path):
train_data_path = train_path
train_datagen = ImageDataGenerator(rescale=1. / 255, validation_split=0.25)
train_generator = train_datagen.flow_from_directory(
train_data_path,
target_size=(224,224),
color_mode="rgb",
batch_size=self.BS,
class_mode='categorical',
subset='training')
validation_generator = train_datagen.flow_from_directory(
train_data_path,
target_size=(224, 224),
color_mode="rgb",
batch_size=self.BS,
class_mode='categorical',
subset='validation')
# load the ResNet-50 network, ensuring the head FC layer sets are left off
baseModel = ResNet50(weights="imagenet", include_top=False,
input_tensor = Input(shape=(224, 224, 3)))
# construct the head of the model that will be placed on top of the the base model
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(256, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(len(self.CLASSES), activation="softmax")(headModel)
# place the head FC model on top of the base model (this will become the actual model we will train)
model = Model(inputs=baseModel.input, outputs=headModel)
for layer in baseModel.layers:
layer.trainable = True
# compile the model
opt = Adam(lr=self.INIT_LR)#, decay=self.INIT_LR / self.NUM_EPOCHS)
model.compile(loss="binary_crossentropy", optimizer=opt,
metrics=["accuracy"])
from keras.callbacks import ModelCheckpoint, EarlyStopping
import matplotlib.pyplot as plt
checkpoint = ModelCheckpoint('resnetModel.h5', monitor='val_accuracy', verbose=1, save_best_only=True,
save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='val_accuracy', min_delta=0, patience=6, verbose=1, mode='auto')
hist = model.fit_generator(steps_per_epoch=self.BS, generator=train_generator,
validation_data=validation_generator, validation_steps=32, epochs=self.NUM_EPOCHS,
callbacks=[checkpoint, early])
plt.plot(hist.history['accuracy'])
plt.plot(hist.history['val_accuracy'])
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title("model accuracy")
plt.ylabel("Accuracy")
plt.xlabel("Epoch")
plt.legend(["Accuracy", "Validation Accuracy", "loss", "Validation Loss"])
plt.show()
plt.figure(1)
import tensorflow as tf
if __name__ == '__main__':
x = example()
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.compat.v1.Session(config=config)
x.build_model("C:/Users/but/Desktop/dataScratch/Train")
I have 2 Classes which contain images of integrated circuits with defect and non defect images. My Batch Size is 32, Epoches is 32, LR is 1e-4.
Here are example images:
This is a defect image
This is an ok image

model.predict() function always returns 1 for binary image classification model

I was working on a binary image classification deep learning model using transfer learning in Google colab.
!wget https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5 -O /tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5
local_weights_file = '/tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'
pre_trained_model = InceptionV3(input_shape = (300, 300, 3),
include_top = False,
weights = None)
pre_trained_model.load_weights(local_weights_file)
for layer in pre_trained_model.layers:
layer.trainable = False
last_layer = pre_trained_model.get_layer('mixed7')
last_output = last_layer.output
x = layers.Flatten()(last_output)
x = layers.Dense(512, activation='relu')(x)
x = layers.Dropout(0.2)(x)
x = layers.Dense(1, activation='sigmoid')(x)
model = Model(pre_trained_model.input, x)
from tensorflow.keras.optimizers import RMSprop
model.compile(optimizer=RMSprop(lr=0.0001),
loss='binary_crossentropy',
metrics=['accuracy'])
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1/255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode="nearest")
validation_datagen = ImageDataGenerator(rescale=1/255)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(300, 300),
batch_size=100,
class_mode='binary')
validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(300, 300),
batch_size=100,
class_mode='binary')
history = model.fit(
train_generator,
steps_per_epoch=20,
epochs=30,
verbose=1,
validation_data=validation_generator,
validation_steps=10,
callbacks=[callbacks])
import numpy as np
from google.colab import files
from tensorflow.keras.preprocessing import image
uploaded=files.upload()
for fn in uploaded.keys():
path='/content/' + fn
img=image.load_img(path, target_size=(300, 300))
x=image.img_to_array(img)
x=np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images, batch_size=10)
print(classes)
Even though after training the model and obtaining a quite good accuracy on training and validation data, the model is always predicting 1 for any new image. I have tried changing the batch size, epochs, learning rate, etc. But, no luck.
Can anyone explain what's the problem here?

How to give 2 different dataset to 2 different cnn

Hi everyone, i'm the new on keras and i'm in trouble. I've found to how to combine to cnn model but i cant give datasets to models. is the anyone who can help me ?
Here is my code :
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 16 08:20:24 2019
#author: TECHFEA
"""
from keras import applications
from keras.layers import GlobalAveragePooling2D, Dense,Flatten,Conv2D,MaxPooling2D,Add,Input
from keras.layers import Concatenate
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import log_loss
from keras.models import Model
from keras.optimizers import SGD
from sklearn.metrics import roc_curve, auc
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
import matplotlib.pyplot as plt
from keras.models import load_model
from scipy import interp
from itertools import cycle
from glob import glob
from keras.optimizers import Adam
train_path ="C:/Users/Monster/Desktop/furkan_ecevit/Datasets/fer_orj/train/"
validation_path ="C:/Users/Monster/Desktop/furkan_ecevit/Datasets/fer_orj/validation/"
train_path2="C:/Users/Monster/Desktop/furkan_ecevit/Datasets/fer_lbp/train_lbp/"
validation_path2="C:/Users/Monster/Desktop/furkan_ecevit/Datasets/fer_lbp/validation_lbp/"
className = glob(train_path + "*/")
numberOfClass = len(className)
batch_size=32
train_datagen = ImageDataGenerator(rescale= 1./255,
vertical_flip=False,
horizontal_flip=True)
validation_datagen = ImageDataGenerator(rescale = 1./255)
train_generator = train_datagen.flow_from_directory(train_path, target_size =(72,72),
batch_size = batch_size,
color_mode = "rgb",
class_mode = "categorical")
validation_generator = validation_datagen.flow_from_directory(validation_path, target_size =(72,72),
batch_size = 10,
color_mode = "rgb",
class_mode = "categorical")
train_generator2 = train_datagen.flow_from_directory(train_path2, target_size =(72,72),
batch_size = batch_size,
color_mode = "rgb",
class_mode = "categorical")
validation_generator2 = validation_datagen.flow_from_directory(validation_path2, target_size =(72,72),
batch_size = 10,
color_mode = "rgb",
class_mode = "categorical")
base_model1 = applications.VGG16(weights='imagenet', include_top=False, input_shape=(72,72,3))
base_model1.summary()
x1=base_model1.output
x1=Flatten()(x1)
x1=Dense(100,activation='relu')(x1)
model1 = Model(inputs=base_model1.input, outputs=x1)
model1.summary()
input_shallow = Input(shape = (72,72,3))
conv1 = Conv2D(16, (3,3), activation = 'relu', padding = "same")(input_shallow)
pool1 = MaxPooling2D(pool_size = (2,2), strides = 2)(conv1)
conv2 = Conv2D(32, (3,3), activation = 'relu', padding = "same")(pool1)
pool2 = MaxPooling2D(pool_size = (2,2), strides = 2)(conv2)
flat1=Flatten()(pool2)
dense_1=Dense(100,activation='relu')(flat1)
model2=Model(inputs=input_shallow,outputs=dense_1)
model2.summary()
mergedOut = Add()([model1.output,model2.output])
out=Dense(2048, activation='relu')(mergedOut)
out = Dense(7, activation='softmax', name='predictions')(out)
model = Model(inputs=[model1.input,model2.input], outputs=out)
model.summary()
opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)
model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=["accuracy"])
hist = model.fit_generator(
generator=(train_generator,train_generator2),
steps_per_epoch = 10,
epochs=16,
validation_data =(validation_generator,validation_generator2),
validation_steps = 2,
shuffle=True)
Here is the what i want to do with image :
Here is the what i got error :
'DirectoryIterator' object has no attribute 'ndim'

Keras model.predict always 0

I am using keras applications for transfer learning with resnet 50 and inception v3 but when predicting always get [[ 0.]]
The below code is for a binary classification problem. I have also tried vgg19 and vgg16 but they work fine, its just resnet and inception. The dataset is a 50/50 split. And I am only changing the model = applications.resnet50.ResNet50 line of code for each model.
below is the code:
from keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(monitor='val_loss', patience=2)
img_width, img_height = 256, 256
train_data_dir = xxx
validation_data_dir = xxx
nb_train_samples = 14000
nb_validation_samples = 6000
batch_size = 16
epochs = 50
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
model = applications.resnet50.ResNet50(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, 3))
from keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(monitor='val_loss', patience=2)
img_width, img_height = 256, 256
train_data_dir = xxx
validation_data_dir = xxx
nb_train_samples = 14000
nb_validation_samples = 6000
batch_size = 16
epochs = 50
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
model = applications.resnet50.ResNet50(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, 3))
#Freeze the layers which you don't want to train. Here I am freezing the first 5 layers.
for layer in model.layers[:5]:
layer.trainable = False
#Adding custom Layers
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(1, activation="sigmoid")(x)
# creating the final model
model_final = Model(input = model.input, output = predictions)
# compile the model
model_final.compile(loss = "binary_crossentropy", optimizer = optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"])
# Initiate the train and test generators with data Augumentation
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
# Save the model according to the conditions
#checkpoint = ModelCheckpoint("vgg16_1.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
#early = EarlyStopping(monitor='val_acc', min_delta=0, patience=10, verbose=1, mode='auto')
model_final.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size,
callbacks=[early_stopping])
from keras.models import load_model
import numpy as np
from keras.preprocessing.image import img_to_array, load_img
#test_model = load_model('vgg16_1.h5')
img = load_img('testn7.jpg',False,target_size=(img_width,img_height))
x = img_to_array(img)
x = np.expand_dims(x, axis=0)
#preds = model_final.predict_classes(x)
prob = model_final.predict(x, verbose=0)
#print(preds)
print(prob)
Note That model_final.evaluate_generator(validation_generator, nb_validation_samples) provides an expected accuracy like 80% its just predict that is always 0.
Just find it strange that vgg19 and vgg16 work fine but not resnet50 and inception. Do these models require something else to work?
Any insight would be great.
Thanks in advance.
I was running into similar problem. You are scaling all the RGB values from 0-255 to 0-1 during training.
Thse same should be done at the time of prediction.
Try
x = img_to_array(img)
x = x/255

Categories

Resources