I have 2 versions of models built using keras that seem to build well, but when it comes to compiling and fitting, I receive the same error for both of them. I'm not sure what the problem is.
def build_cnn():
model = models.Sequential([
layers.Conv2D(32, (3, 3), input_shape=(32, 32, 3), padding='same', name='conv1'),
layers.MaxPooling2D((2, 2), name='maxpooling1'),
layers.Conv2D(64, (3, 3), activation='relu', name='conv2'),
layers.MaxPooling2D((2, 2), name = 'maxpooling2'),
layers.Conv2D(64, (3, 3), activation='relu', name = 'conv3'),
layers.Flatten(name = 'flatten'),
layers.Dense(64, activation='relu', name='dense1'),
layers.Dense(10, name='dense2')
], name='CNN')
return model
def build_cnn2():
model = models.Sequential([
tf.keras.layers.Input(shape=(32,32,3)),
tf.keras.layers.Conv2D(32, (3,3), padding='same', name='conv2d'),
tf.keras.layers.MaxPool2D(pool_size=(2,2), name='maxpooling'),
tf.keras.layers.Flatten(name='flatten'),
tf.keras.layers.Dense(10, activation='softmax', name='dense'),
], name='conv2d')
return model
def train(model):
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['acc'])
return model.fit(x_train, y_train,
epochs=5,
validation_split=0.1)
model2 = build_cnn()
log2 = train(model2)
I receive this order for both model2 = build_cnn() and model2 = build_cnn2():
ValueError: Input 0 of layer CNN is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (None, 30)
According to the error, it seems like you didn't preprocess your data properly - as it says, it expects 4D - (None, h, w, c) but yours somehow (None, 30).
Also, one of your model's last activation is None, another is set as softmax but yet you set the loss function binary_crossentropy rather than CategoricalCrossentropy.
Here is a possible solution for you (by resolving your above issue).
def build_cnn():
model = Sequential([
layers.Conv2D(32, (3, 3), input_shape=(32, 32, 3),
padding='same', name='conv1'),
layers.MaxPooling2D((2, 2), name='maxpooling1'),
layers.Conv2D(64, (3, 3), activation='relu', name='conv2'),
layers.MaxPooling2D((2, 2), name = 'maxpooling2'),
layers.Conv2D(64, (3, 3), activation='relu', name = 'conv3'),
layers.Flatten(name = 'flatten'),
layers.Dense(64, activation='relu', name='dense1'),
layers.Dense(10, activation='softmax', name='dense2')
], name='CNN')
return model
def train(model, x_train, y_train):
model.compile(optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=['acc'])
return model.fit(x_train, y_train,
epochs=5, verbose=2,
validation_split=0.1)
model2 = build_cnn()
I use mnist for demonstration. Its shape is (28 x 28) but as your model takes (32, 32, 3) - the mnist needs to be preprocessed. Hopefully, you can adapt to your case.
DataSet
(x_train, y_train), (_, _) = tf.keras.datasets.mnist.load_data()
print(x_train.shape, y_train.shape)
# expand new axis, channel axis
x_train = np.expand_dims(x_train, axis=-1)
print(x_train.shape)
# need 3 channel (instead of 1)
x_train = np.repeat(x_train, 3, axis=-1)
print(x_train.shape)
# it's always better to normalize
x_train = x_train.astype('float32') / 255
print(x_train.shape)
# resize the input shape , i.e. old shape: 28, new shape: 32
x_train = tf.image.resize(x_train, [32,32]) # if we want to resize
print(x_train.shape)
# train set / target
y_train = tf.keras.utils.to_categorical(y_train , num_classes=10)
print(y_train.shape)
(60000, 28, 28) (60000,)
(60000, 28, 28, 1)
(60000, 28, 28, 3)
(60000, 28, 28, 3)
(60000, 32, 32, 3)
(60000, 10)
Now, you can train your model.
log2 = train(model2, x_train , y_train)
Epoch 1/5
4ms/step - loss: 0.2792 - acc: 0.9133 - val_loss: 0.0676 - val_acc: 0.9815
Epoch 2/5
4ms/step - loss: 0.0454 - acc: 0.9864 - val_loss: 0.0400 - val_acc: 0.9883
Epoch 3/5
4ms/step - loss: 0.0336 - acc: 0.9892 - val_loss: 0.0415 - val_acc: 0.9900
Epoch 4/5
4ms/step - loss: 0.0235 - acc: 0.9926 - val_loss: 0.0359 - val_acc: 0.9907
Epoch 5/5
4ms/step - loss: 0.0163 - acc: 0.9948 - val_loss: 0.0295 - val_acc: 0.9918
Related
I'm trying to build a Siamese Neural Network to analyze the MNIST dataset, however when trying to fit the model to the dataset I encounter this problem according to which I have training data and labels shapes' mismatch. I tried changing the loss function as well as tried to squeeze the labels array, and neither of "solutions" worked.
Here are the train and labels arrays' shapes:
pairTrain shape: (120000, 2, 28, 28, 1)
labelTrain shape: (120000, 1)
Here's my model:
def build_model(input_shape, embedDim=48):
inputs = Input(input_shape)
x = Conv2D(64, (2, 2), padding="same", activation="relu", input_shape=input_shape)(inputs)
x = MaxPooling2D()(x)
x = Dropout(0.3)(x)
x = Conv2D(32, (2, 2), padding="same", activation="relu")(x)
x = MaxPooling2D()(x)
x = Dropout(0.3)(x)
x = Conv2D(16, (2, 2), padding="same", activation="relu")(x)
x = MaxPooling2D()(x)
x = Dropout(0.3)(x)
outputs = Flatten()(x)
outputs = Dense(embedDim)(outputs)
model = Model(inputs, outputs)
return model
And finally here's the code that generates the error itself:
imgA = Input(shape=(28, 28, 1))
imgB = Input(shape=(28, 28, 1))
featA = build_model((28, 28, 1))(imgA)
featB = build_model((28, 28, 1))(imgB)
distance = Lambda(euclidean_distance)([featA, featB])
output = Dense(1, activation="sigmoid")(distance)
model = Model([imgA, imgB], output)
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
history = model.fit(
[pairTrain[:, 0], pairTrain[:, 1]], labelTrain,
validation_data=[[pairTest[:, 0], pairTest[:, 1]], labelTest],
batch_size=64,
epochs=10
)
model.save("output/siamese_model")
Please help me to resolve the problem.
I was not able to reproduce the error using the below code. I suspect that your labels shape is different than the one you reported or it does not contain strictly binary data (0s and 1s) only.
Also, you should use tf.keras.losses.BinaryCrossentropy instead of tf.keras.losses.CategoricalCrossentropy as your labels should be binary with the sigmoid activation in the last layer.
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Input, Flatten, Dense, Lambda
from tensorflow.keras.models import Model
import tensorflow as tf
def build_model(input_shape, embedDim=48):
inputs = Input(input_shape)
x = Conv2D(64, (2, 2), padding="same", activation="relu", input_shape=input_shape)(inputs)
x = MaxPooling2D()(x)
x = Dropout(0.3)(x)
x = Conv2D(32, (2, 2), padding="same", activation="relu")(x)
x = MaxPooling2D()(x)
x = Dropout(0.3)(x)
x = Conv2D(16, (2, 2), padding="same", activation="relu")(x)
x = MaxPooling2D()(x)
x = Dropout(0.3)(x)
outputs = Flatten()(x)
outputs = Dense(embedDim)(outputs)
model = Model(inputs, outputs)
return model
imgA = Input(shape=(28, 28, 1))
imgB = Input(shape=(28, 28, 1))
featA = build_model((28, 28, 1))(imgA)
featB = build_model((28, 28, 1))(imgB)
distance = Lambda(lambda x: x[0]-x[1])([featA, featB])
output = Dense(1, activation="sigmoid")(distance)
model = Model([imgA, imgB], output)
pairTrain = tf.random.uniform((10, 2, 28, 28, 1))
labelTrain = tf.random.uniform(shape=(10, 1), minval=0, maxval=2, dtype=tf.int32)
pairTest = tf.random.uniform((10, 2, 28, 28, 1))
labelTest = tf.random.uniform(shape=(10, 1), minval=0, maxval=2, dtype=tf.int32)
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
history = model.fit(
[pairTrain[:, 0], pairTrain[:, 1]], labelTrain,
validation_data=[[pairTest[:, 0], pairTest[:, 1]], labelTest],
batch_size=64,
epochs=10
)
model.save("output/siamese_model")
Epoch 1/10
1/1 [==============================] - 2s 2s/step - loss: 0.7061 - accuracy: 0.5000 - val_loss: 0.6862 - val_accuracy: 0.7000
Epoch 2/10
1/1 [==============================] - 0s 80ms/step - loss: 0.7882 - accuracy: 0.4000 - val_loss: 0.6751 - val_accuracy: 0.6000
Epoch 3/10
1/1 [==============================] - 0s 81ms/step - loss: 0.6358 - accuracy: 0.5000 - val_loss: 0.6755 - val_accuracy: 0.6000
Epoch 4/10
1/1 [==============================] - 0s 79ms/step - loss: 0.7027 - accuracy: 0.5000 - val_loss: 0.6759 - val_accuracy: 0.6000
Epoch 5/10
1/1 [==============================] - 0s 82ms/step - loss: 0.6970 - accuracy: 0.4000 - val_loss: 0.6752 - val_accuracy: 0.6000
Epoch 6/10
1/1 [==============================] - 0s 83ms/step - loss: 0.7564 - accuracy: 0.4000 - val_loss: 0.6779 - val_accuracy: 0.6000
Epoch 7/10
1/1 [==============================] - 0s 73ms/step - loss: 0.7123 - accuracy: 0.6000 - val_loss: 0.6818 - val_accuracy: 0.6000
I want to train a Sequential Neural Net (NN) with Tensorflow.
import tensorflow as tf
bidding_nn = tf.keras.Sequential([
tf.keras.layers.Dense(
units=128, activation='elu', kernel_initializer='he_uniform'),
tf.keras.layers.Dense(
units=128, activation='elu', kernel_initializer='he_uniform'),
tf.keras.layers.Dense(
units=9, activation='softmax', kernel_initializer='he_uniform'),
])
opt = tf.keras.optimizers.Adam(lr=0.02, decay=0.01)
bidding_nn.compile(optimizer=opt,
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=["accuracy"])
bidding_nn.fit(train_dataset, epochs=10)
It should tell me probabilities for the 9 categories I have.
So as input for the NN, I have 8 numpy arrays of lengths 32 (one-hot encoded) and as output 1 numpy array of lengths 9 (one-hot encoded).
(Pdb) train_dataset
<TensorSliceDataset shapes: ((8, 32), (9,)), types: (tf.float64, tf.float64)>
However, at bidding_nn.fit(train_dataset, epochs=10) I get the error message
ValueError: Shapes (9, 1) and (8, 9) are incompatible
Your first problem is that the dataset you have created is only spitting out a single training instance. try:
print(train_dataset)
# <TensorSliceDataset shapes: ((8, 32), (9,)), types: (tf.float32, tf.float32)>
Your network is expecting an input of ((batch_size, 8, 32), (batch_size, 9)). To fix this add .batch() to the end of your dataset.
train_dataset = train_dataset.batch(32)
print(train_dataset)
# <BatchDataset shapes: ((None, 8, 32), (None, 9)),
# types: (tf.float32, tf.float32)>
This should fix your original problem. Your next problem will be that you are putting 3D input into a network but your need 2D output. You need a Flatten() layer (or some other dimensional reduction layer) before you create your logits.
import tensorflow as tf
# fake X
X = tf.one_hot(tf.random.uniform(
[100, 8], minval=0, maxval=33, dtype=tf.int64), 32)
print(X.shape)
# (100, 8, 32)
# fake y
y = tf.one_hot(tf.random.uniform(
[100, ], minval=0, maxval=10, dtype=tf.int64), 9)
print(y.shape)
# (100, 9)
# network layers
bidding_nn = tf.keras.Sequential([
tf.keras.layers.Dense(
units=128, activation='elu', kernel_initializer='he_uniform'),
tf.keras.layers.Dense(
units=128, activation='elu', kernel_initializer='he_uniform'),
tf.keras.layers.Flatten(), # <= add this
tf.keras.layers.Dense(
units=9, activation='softmax', kernel_initializer='he_uniform'),
])
# compile model
bidding_nn.compile(optimizer=tf.keras.optimizers.Adam(lr=0.02, decay=0.01),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=["accuracy"])
# create dataset
train_dataset = tf.data.Dataset.from_tensor_slices((X, y)).batch(32)
# train
bidding_nn.fit(train_dataset, epochs=10)
# Epoch 1/10
# 4/4 [========================] - 1s 3ms/step - loss: 2.2432 - accuracy: 0.0738
# Epoch 2/10
# 4/4 [========================] - 0s 2ms/step - loss: 0.1858 - accuracy: 0.8672
# ...
output should be (Batch_size,num_output)
change your dataset generator to expand the dimension then append each label together
labels = []
for i in one_hot:
lbs.append(np.expand_dims(i,0))
labels = np.array(lbs)
print(labels.shape) # (8,9)
I am trying to train the mnist dataset on ResNet50 using the Keras library.
The shape of mnist is (28, 28, 1) however resnet50 required the shape to be (32, 32, 3)
How can I convert the mnist dataset to the required shape?
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)
x_train = x_train/255.0
x_test = x_test/255.0
from keras.utils import to_categorical
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = models.Sequential()
# model.add(InputLayer(input_shape=(28, 28)))
# model.add(Reshape(target_shape=(32, 32, 3)))
# model.add(Conv2D())
model.add(conv_base)
model.add(Flatten())
model.add(BatchNormalization())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization())
model.add(Dense(10, activation='softmax'))
model.compile(optimizer=optimizers.RMSprop(lr=2e-5), loss='binary_crossentropy', metrics=['acc'])
history = model.fit(x_train, y_train, epochs=5, batch_size=20, validation_data=(x_test, y_test))
ValueError: Input 0 is incompatible with layer sequential_10: expected shape=(None, 32, 32, 3), found shape=(20, 28, 28, 1)
You need to resize the MNIST data set. Note that minimum size actually depends on the ImageNet model. For example: Xception requires at least 72, where ResNet is asking for 32. Apart from that, the MNIST is a grayscale image, but it may conflict if you're using the pretrained weight of these models. So, good and safe side is to resize and convert grayscale to RGB.
Full working code for you.
Data Set
We will resize MNIST from 28 to 32. Also, make 3 channels instead of keeping 1.
import tensorflow as tf
import numpy as np
(x_train, y_train), (_, _) = tf.keras.datasets.mnist.load_data()
# expand new axis, channel axis
x_train = np.expand_dims(x_train, axis=-1)
# [optional]: we may need 3 channel (instead of 1)
x_train = np.repeat(x_train, 3, axis=-1)
# it's always better to normalize
x_train = x_train.astype('float32') / 255
# resize the input shape , i.e. old shape: 28, new shape: 32
x_train = tf.image.resize(x_train, [32,32]) # if we want to resize
# one hot
y_train = tf.keras.utils.to_categorical(y_train , num_classes=10)
print(x_train.shape, y_train.shape)
(60000, 32, 32, 3) (60000, 10)
ResNet 50
input = tf.keras.Input(shape=(32,32,3))
efnet = tf.keras.applications.ResNet50(weights='imagenet',
include_top = False,
input_tensor = input)
# Now that we apply global max pooling.
gap = tf.keras.layers.GlobalMaxPooling2D()(efnet.output)
# Finally, we add a classification layer.
output = tf.keras.layers.Dense(10, activation='softmax', use_bias=True)(gap)
# bind all
func_model = tf.keras.Model(efnet.input, output)
Train
func_model.compile(
loss = tf.keras.losses.CategoricalCrossentropy(),
metrics = tf.keras.metrics.CategoricalAccuracy(),
optimizer = tf.keras.optimizers.Adam())
# fit
func_model.fit(x_train, y_train, batch_size=128, epochs=5, verbose = 2)
Epoch 1/5
469/469 - 56s - loss: 0.1184 - categorical_accuracy: 0.9690
Epoch 2/5
469/469 - 21s - loss: 0.0648 - categorical_accuracy: 0.9844
Epoch 3/5
469/469 - 21s - loss: 0.0503 - categorical_accuracy: 0.9867
Epoch 4/5
469/469 - 21s - loss: 0.0416 - categorical_accuracy: 0.9888
Epoch 5/5
469/469 - 21s - loss: 0.1556 - categorical_accuracy: 0.9697
<tensorflow.python.keras.callbacks.History at 0x7f316005a3d0>
I am new to deep learning and have been trying to convert the Keras sequential API to the functional API running on the CIFAR10 image dataset but have been having some difficulty. I've converted the model which looks the same except for the input layer yet the sequential has an average accuracy of around ~70% and my functional has an average accuracy of around ~10%. I would really appreciate some help with regards to figuring out what is going wrong. Here is my functional code:
import tensorflow as tf
from tensorflow import keras
from keras import datasets, layers, models
from keras.models import Model, Input, Sequential
import matplotlib.pyplot as plt
Download and prepare:
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0
input_shape = train_images[0,:,:,:].shape
Create model:
input = layers.Input(shape=input_shape)
x = layers.Conv2D(32, (3, 3), activation='relu',padding='valid')(input)
x = layers.MaxPooling2D((2,2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu')(x)
x = layers.MaxPooling2D((2,2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu')(x)
x = layers.Flatten()(x)
x = layers.Dense(64, activation='relu')(x)
x = layers.Dense(10)(x)
model = Model(input, x, name='Functional')
Compile and train:
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
Here is a link to the original sequential CNN which is a google collaboratory notebook. I would really appreciate any help in trying to understand and fix what is going wrong. Thank you in advance.
There seems to be some issues with SparseCategoricalCrossentropy loss.
Check this: https://github.com/tensorflow/tensorflow/issues/38632
The following model gives good accuracy:
import tensorflow as tf
from tensorflow import keras
from keras import datasets, layers, models
from keras.models import Model, Input, Sequential
import matplotlib.pyplot as plt
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0
train_labels, test_labels = tf.keras.utils.to_categorical(train_labels, 10) , tf.keras.utils.to_categorical(test_labels, 10)
input_shape = train_images[0,:,:,:].shape
input = layers.Input(shape=input_shape)
x = layers.Conv2D(32, (3, 3), activation='relu',padding='valid')(input)
x = layers.MaxPooling2D((2,2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu')(x)
x = layers.MaxPooling2D((2,2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu')(x)
x = layers.Flatten()(x)
x = layers.Dense(64, activation='relu')(x)
x = layers.Dense(10, activation='softmax')(x)
model = Model(input, x, name='Functional')
model.summary()
model.compile(optimizer='adam',
loss=loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
conv2d_16 (Conv2D) (None, 30, 30, 32) 896
_________________________________________________________________
max_pooling2d_11 (MaxPooling (None, 15, 15, 32) 0
_________________________________________________________________
conv2d_17 (Conv2D) (None, 13, 13, 64) 18496
_________________________________________________________________
max_pooling2d_12 (MaxPooling (None, 6, 6, 64) 0
_________________________________________________________________
conv2d_18 (Conv2D) (None, 4, 4, 64) 36928
_________________________________________________________________
flatten_6 (Flatten) (None, 1024) 0
_________________________________________________________________
dense_11 (Dense) (None, 64) 65600
_________________________________________________________________
dense_12 (Dense) (None, 10) 650
=================================================================
Total params: 122,570
Trainable params: 122,570
Non-trainable params: 0
_________________________________________________________________
Train on 50000 samples, validate on 10000 samples
Epoch 1/10
50000/50000 [==============================] - 15s 305us/step - loss: 1.4870 - accuracy: 0.4600 - val_loss: 1.2874 - val_accuracy: 0.5488
Epoch 2/10
50000/50000 [==============================] - 15s 301us/step - loss: 1.1365 - accuracy: 0.5989 - val_loss: 1.0789 - val_accuracy: 0.6191
Epoch 3/10
50000/50000 [==============================] - 15s 301us/step - loss: 0.9869 - accuracy: 0.6547 - val_loss: 0.9506 - val_accuracy: 0.6700
Epoch 4/10
50000/50000 [==============================] - 15s 301us/step - loss: 0.8896 - accuracy: 0.6907 - val_loss: 0.9509 - val_accuracy: 0.6695
Epoch 5/10
50000/50000 [==============================] - 16s 311us/step - loss: 0.8135 - accuracy: 0.7151 - val_loss: 0.8688 - val_accuracy: 0.7046
Epoch 6/10
50000/50000 [==============================] - 15s 303us/step - loss: 0.7566 - accuracy: 0.7351 - val_loss: 0.8411 - val_accuracy: 0.7141
I have to identify a retinal disease with CNN. I have 1400 images, 700 each class. My classes are (0 - no PDR) and (1 - PDR). I'm trying to make a model to identify if an input retina have the disease in level 4 or not.
I'm making a follow manipulation with my images and rezise all to 256x256:
ImageCV[index] = cv2.addWeighted(ImageCV[index],4, cv2.GaussianBlur(ImageCV[index],(0,0), 256/30), -4, 128)
And it made the follow with my imgs:
https://imgur.com/X1p9G1c
Then, when I train my model I got a very high accuracy (like 99....) but when I try to predict some test images, it fails.. for example, I've putted 10 PDR examples in test folder and tries to predict them (all must be 1).. this is the result:
[[0.]]
[[0.]]
[[1.]]
[[0.]]
[[0.]]
[[0.]]
[[1.]]
[[0.]]
[[0.]]
[[0.]]
This is my model:
visible = Input(shape=(256,256,3))
conv1 = Conv2D(16, kernel_size=(3,3), activation='relu', strides=(1, 1))(visible)
conv2 = Conv2D(16, kernel_size=(3,3), activation='relu', strides=(1, 1))(conv1)
bat1 = BatchNormalization()(conv2)
conv3 = ZeroPadding2D(padding=(1, 1))(bat1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(32, kernel_size=(3,3), activation='relu', padding='valid', kernel_regularizer=regularizers.l2(0.01))(pool1)
conv5 = Conv2D(32, kernel_size=(3,3), activation='relu', padding='valid', kernel_regularizer=regularizers.l2(0.01))(conv4)
bat2 = BatchNormalization()(conv5)
pool2 = MaxPooling2D(pool_size=(1, 1))(bat2)
conv6 = Conv2D(64, kernel_size=(3,3), activation='relu',strides=(1, 1), padding='valid')(pool2)
conv7 = Conv2D(64, kernel_size=(3,3), activation='relu',strides=(1, 1), padding='valid')(conv6)
bat3 = BatchNormalization()(conv7)
conv7 = ZeroPadding2D(padding=(1, 1))(bat3)
pool3 = MaxPooling2D(pool_size=(1, 1))(conv7)
conv8 = Conv2D(128, kernel_size=(3,3), activation='relu', padding='valid', kernel_regularizer=regularizers.l2(0.01))(pool3)
conv9 = Conv2D(128, kernel_size=(2,2), activation='relu', strides=(1, 1), padding='valid')(conv8)
bat4 = BatchNormalization()(conv9)
pool4 = MaxPooling2D(pool_size=(1, 1))(bat4)
flat = Flatten()(pool4)
output = Dense(1, activation='sigmoid')(flat)
model = Model(inputs=visible, outputs=output)
opt = optimizers.adam(lr=0.001, decay=0.0)
model.compile(optimizer= opt, loss='binary_crossentropy', metrics=['accuracy'])
data, labels = ReadImages(TRAIN_DIR)
test, lt = ReadImages(TEST_DIR)
data = np.array(data)
labels = np.array(labels)
test = np.array(test)
lt = np.array(lt)
np.random.permutation(len(data))
np.random.permutation(len(labels))
np.random.permutation(len(test))
np.random.permutation(len(lt))
model.fit(data, labels, epochs=7, validation_data = (test,lt))
model.save('model.h5')
And this is predict.py
model = load_model('model.h5')
for filename in os.listdir(r'v/'):
if filename.endswith(".jpg") or filename.endswith(".ppm") or filename.endswith(".jpeg"):
ImageCV = cv2.resize(cv2.imread(os.path.join(TEST_DIR) + filename), (256,256))
ImageCV = cv2.addWeighted(ImageCV,4, cv2.GaussianBlur(ImageCV,(0,0), 256/30), -4, 128)
cv2.imshow('image', ImageCV)
cv2.waitKey(0)
cv2.destroyAllWindows()
ImageCV = ImageCV.reshape(-1,256,256,3)
print(model.predict(ImageCV))
What do I could to do to improve my predictions at all?
I strongly appreciate your help
UPDATE
Well, I tried to do all of were said in answers but still'nt working...
this is my code now:
visible = Input(shape=(256,256,3))
conv1 = Conv2D(16, kernel_size=(3,3), activation='relu', strides=(1, 1))(visible)
conv2 = Conv2D(32, kernel_size=(3,3), activation='relu', strides=(1, 1))(conv1)
bat1 = BatchNormalization()(conv2)
conv3 = ZeroPadding2D(padding=(1, 1))(bat1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv3)
drop1 = Dropout(0.30)(pool1)
conv4 = Conv2D(32, kernel_size=(3,3), activation='relu', padding='valid', kernel_regularizer=regularizers.l2(0.01))(drop1)
conv5 = Conv2D(64, kernel_size=(3,3), activation='relu', padding='valid', kernel_regularizer=regularizers.l2(0.01))(conv4)
bat2 = BatchNormalization()(conv5)
pool2 = MaxPooling2D(pool_size=(1, 1))(bat2)
drop1 = Dropout(0.30)(pool2)
conv6 = Conv2D(128, kernel_size=(3,3), activation='relu', padding='valid', kernel_regularizer=regularizers.l2(0.01))(pool2)
conv7 = Conv2D(128, kernel_size=(2,2), activation='relu', strides=(1, 1), padding='valid')(conv6)
bat3 = BatchNormalization()(conv7)
pool3 = MaxPooling2D(pool_size=(1, 1))(bat3)
drop1 = Dropout(0.30)(pool3)
flat = Flatten()(pool3)
drop4 = Dropout(0.50)(flat)
output = Dense(1, activation='sigmoid')(drop4)
model = Model(inputs=visible, outputs=output)
opt = optimizers.adam(lr=0.001, decay=0.0)
model.compile(optimizer= opt, loss='binary_crossentropy', metrics=['accuracy'])
data, labels = ReadImages(TRAIN_DIR)
test, lt = ReadImages(TEST_DIR)
data = np.array(data)
labels = np.array(labels)
perm = np.random.permutation(len(data))
data = data[perm]
labels = labels[perm]
#model.fit(data, labels, epochs=8, validation_data = (np.array(test), np.array(lt)))
aug = ImageDataGenerator(rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15,
horizontal_flip=True)
# train the network
model.fit_generator(aug.flow(data, labels, batch_size=32),
validation_data=(np.array(test), np.array(lt)), steps_per_epoch=len(data) // 32,
epochs=7)
And this is the return:
Epoch 1/7
43/43 [==============================] - 1004s 23s/step - loss: 1.8090 - acc: 0.9724 - val_loss: 1.7871 - val_acc: 0.9861
Epoch 2/7
43/43 [==============================] - 1003s 23s/step - loss: 1.8449 - acc: 0.9801 - val_loss: 1.4828 - val_acc: 1.0000
Epoch 3/7
43/43 [==============================] - 1092s 25s/step - loss: 1.5704 - acc: 0.9920 - val_loss: 1.3985 - val_acc: 1.0000
Epoch 4/7
43/43 [==============================] - 1062s 25s/step - loss: 1.5219 - acc: 0.9898 - val_loss: 1.3167 - val_acc: 1.0000
Epoch 5/7
43/43 [==============================] - 990s 23s/step - loss: 2.5744 - acc: 0.9222 - val_loss: 2.9347 - val_acc: 0.9028
Epoch 6/7
43/43 [==============================] - 983s 23s/step - loss: 1.6053 - acc: 0.9840 - val_loss: 1.3299 - val_acc: 1.0000
Epoch 7/7
43/43 [==============================] - 974s 23s/step - loss: 1.6180 - acc: 0.9801 - val_loss: 1.5181 - val_acc: 0.9861
I'd have added dropouts, reduce the model layers, put data augmentation, and doesn't work at all (all the predictions returns 0)...
Please anyone can help in it.
It seems you having issues with overfitting. I have kind of a personal dilemma in here whether this is off-topic or not because the approach one can give it's somewhat opinion-based, but here I go:
First off, if you need to regularize an overfitting network, you want to use dropout starting at 0.25 and checking if that improves the model. Data augmentation is a must when dealing with overfitting, together with batch normalization (which you are applying).
If this still not solves your overfitting problems, then you should try working on your network architecture to tweak it in order to make it generalize better. Did you check a simple of the inputs being used for train and test?
TLDR: Try dropout and data augmentation, if it doesn't work and your data is correct, you might have to work on improving the architecture to make a better generalizing model.
EDIT: The consensus on approaching this kind of models is to have it overfit at first, with a decent accuracy and then work towards generalizing it without losing accuracy if possible.
Your model is very big for your training set size, leading to overfitting. Use less layers
You're overfitting i.e. have a high variance problem. Try adding some soft dropout (0.2-0.3) at the end of your convolution blocks. You can also add a couple of Dense layers with decreasing unit numbers before the output layer and put more dropout layers in between those (0.5+).
You should also implement more data augmentation such as rotations, flips, random noise, random brightness, etc. Check out Keras' documentation for the ImageDataGenerator class.