Change input size of vggface pretained model - tensorflow

It been a week that Im trying to change the input size of the pertrained Vgg face model every time I change something it got me an error how can I change the input size from 224x224x3 to 64x64x3 is there a way to change it directly without training again the model just using the pertrained weights directly
model.add(ZeroPadding2D((1, 1),include_top=False,input_shape=(64, 64, 3)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Convolution2D(4096, (7, 7), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(4096, (1, 1), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(2622, (1, 1)))
model.add(Flatten())
model.add(Activation('softmax'))
from keras.models import model_from_json
deep= model.load_weights('/Users/macbookpro/PycharmProjects/untitled/venv/vgg_face_weights.h5')
code:
vggface
When I try to call include_top i got this error:
TypeError: ('Keyword argument not understood:', 'include_top')
When I change directly the input size i got this error:
ValueError: Negative dimension size caused by subtracting 7 from 2 for 'conv2d_14/convolution' (op: 'Conv2D') with input shapes: [?,2,2,512], [7,7,512,4096].

First of all, remove the include_top=False.
Your problem is that this architecture is too deep for a 64x64 input. In particular, this line:
model.add(Convolution2D(4096, (7, 7), activation='relu'))
is trying to perform a 7x7 convolution on an input of size 2x2 which is impossible.
A possible solution is to remove the Convolutions after the last MaxPooling layer and use some Dense layer instead.

Related

ValueError: Shapes (None, 1) and (None, 5) are incompatible in keras

model = Sequential()
model.add(Conv2D(128, (3, 3), activation='relu', input_shape=(64, 64, 3), padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(5, activation = 'softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=[tf.keras.metrics.Recall()])
This code works fine for metrics=['accuracy']), but it shows ValueError: Shapes (None, 1) and (None, 5) are incompatible for metrics=[tf.keras.metrics.Recall()])
Please help me. Thanks in advance.
Recall makes sense only for binary classification. Your final layer has 5 nodes, which essentially means you have 5 classes. You should change recall to another metric. Documentation should help you choose an appropriate metric for your model. Categorical accuracy should be good enough to get started.

How to give an image as input and get another image as output in keras tensorflow

I have zigsaw puzzle images and I have the corresponding pairs.I want to give the image as input to the model and find the corresponding pair of it.I have made the below model which achieves a bad accuracy of 30% while in the training.But when I pass the test images array it predicts an array having all nan values.Should I change my loss function? Please check the code below the image
in_shape=(32,256,256,3)
model1=models.Sequential(
[
resize_and_rescale,
layers.Conv2D(32,(3,3),activation="relu",input_shape=in_shape,padding='same'),
layers.Dropout(0.1),
layers.Conv2D(32,(3,3),activation="relu",input_shape=in_shape,padding='same'),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, kernel_size = (3,3), activation='relu',padding='same'),
layers.Dropout(0.1),
layers.Conv2D(64,(3,3),activation="relu",input_shape=in_shape,padding='same'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(128, kernel_size = (3,3), activation='relu',padding='same'),
layers.Dropout(0.1),
layers.Conv2D(128,(3,3),activation="relu",input_shape=in_shape,padding='same'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(256, kernel_size = (3,3), activation='relu',padding='same'),
layers.MaxPooling2D((2, 2)),
layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same'),
layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Dropout(0.2),
layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same'),
layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Dropout(0.2),
layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same'),
layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Dropout(0.2),
layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same'),
layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Dropout(0.2),
layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Conv2D(3, (1, 1), activation='sigmoid')
]
)
model1.build(input_shape=in_shape)
model1.compile(
optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False),
metrics=['accuracy']
)
If you're predicting pixel values [0, 255], then you'll want to change your last layer to:
layers.Conv2D(3, (1, 1), activation='linear')
A sigmoid activation function will try to force your outputs to a range of [0, 1], whereas a linear activation will allow for regression to pixel values of the range [0, 255], assuming that's what you want.

Tensor Tensor("flatten/Reshape:0", shape=(?, 2622), dtype=float32) is not an element of this graph

Hello StackOverFlow Team:
I built a model based on (Vgg_Face_Model) with weights loaded (vgg_face_weights.h5).
Note that I use tensorflow-gpu = 2.1.0 , and keras=2.3.1 , with Anaconda 3 create it as interpreter and used with pycharm
But the code shows an error in the part :
input_descriptor = [model.predict(face), img]
The code is:
def face_recognizer(face, db_descriptors):
# face = cv2.imread(img)
# face = cv2.resize(face, (IMG_Size, IMG_Size))
t0 = time.perf_counter()
face = np.array(face).reshape(-1, IMG_Size, IMG_Size, 3)
###### here error #################################
input_descriptor = [model.predict(face), img]
###################################################
K_nn_result = K_nn_Classifier(input_descriptor[0], db_descriptors, 5)
input_result = Knn_Distance_Score(K_nn_result)
if input_result[0] <= 10:
identity = 'stranger'
else:
identity = input_result[1]
# print('Done in',time.perf_counter()-t0)
return input_result, identity
def PrepareModels(self):
global mpFaceDetection, FaceDetector, model
mpFaceDetection = mp.solutions.face_detection
FaceDetector = mpFaceDetection.FaceDetection()
model = loadModel()
Model is:
import os
from pathlib import Path
# from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.models import Model, Sequential, load_model
from tensorflow.keras.layers import Input, Convolution2D, ZeroPadding2D, MaxPooling2D, Flatten, Dense, Dropout, \
Activation
import gdown
# ---------------------------------------
def Vgg_Face_Model():
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(224, 224, 3)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Convolution2D(4096, (7, 7), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(4096, (1, 1), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(2622, (1, 1)))
model.add(Flatten())
model.add(Activation('softmax'))
return model
def loadModel():
model = Vgg_Face_Model()
# -----------------------------------
home = str(Path.home())
if os.path.isfile(home + '/.deepface/weights/vgg_face_weights.h5') != True:
print("vgg_face_weights.h5 will be downloaded...")
url = 'https://drive.google.com/uc?id=1CPSeum3HpopfomUEK1gybeuIVoeJT_Eo'
output = home + '/.deepface/weights/vgg_face_weights.h5'
gdown.download(url, output, quiet=False)
# -----------------------------------
model.load_weights(home + '/.deepface/weights/vgg_face_weights.h5')
# -----------------------------------
# TO-DO: why?
vgg_model_descriptor = Model(inputs=model.layers[0].input, outputs=model.layers[-2].output)
return vgg_model_descriptor
# model = loadModel()
output:
Tensor Tensor("flatten/Reshape:0", shape=(?, 2622), dtype=float32) is not an element of this graph.'
from tensorflow.python.keras.backend import set_session
sess = tf.Session()
#This is a global session and graph
graph = tf.get_default_graph()
set_session(sess)
#now where you are calling the model
global sess
global graph
with graph.as_default():
set_session(sess)
input_descriptor = [model.predict(face), img]

Unable to add fourth convolutional layer

I'm pretty new to maching learning and when I was looking at a tutorial for a convolutional neural network I wanted to experiment on my own on how to increase accuracy. However, when I tried to add another convolutional and pooling layer to my model it displayed an error message. This is before I added the layer:
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(62))
And this is after:
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(62))
This is the error message it gave me:
ValueError: Negative dimension size caused by subtracting 3 from 1 for '{{node conv2d_36/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](max_pooling2d_26/MaxPool, conv2d_36/Conv2D/ReadVariableOp)' with input shapes: [?,1,1,64], [3,3,64,64]. site:stackoverflow.com
This is because you reduce the dimensionality too much inside your network. use padding='same' in your convolutional layer to avoid this dimensionality error
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', padding='same',
input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(62))
model.summary()

Conv neural network to tell standard 52-card deck apart

I'm using the below keras model to train a neural network to tell 52 game cards 23456789TJQA each with Club, Diamond, Heart and Spade apart.
The model is working quite well but occasionally has problems telling Club and Diamond apart, as they are the most similar (and the difference is quite granular). I was wondering if anybody has some suggestions in what way I can improve the below model?
I've tried different things, like converting everything to black and white, grayscale, smoothing, augmentation etc, but nothing seems to solve that problem.
The pictures are all 15x50 pixels, with 1 channel, so the input shape is (15,50,1)
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape, activation='relu', padding='same'))
model.add(Dropout(0.2))
model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(Dropout(0.2))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dropout(0.2))
model.add(Dense(1024, activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))