-4

Can we resize an image from 64x64 to 256x256 without affecting the resolution is that a way to add zero on new row and column in the new resized output I m working on vgg and I get an error while adding my 64x64 input image because vggface is a pertrained model that include an input size of 224

code:

from keras.models import Model, Sequential
from keras.layers import Input, Convolution2D, ZeroPadding2D, MaxPooling2D, Flatten, Dense, Dropout, Activation
from PIL import Image
import numpy as np
from keras.preprocessing.image import load_img, save_img, img_to_array
from keras.applications.imagenet_utils import preprocess_input
from keras.preprocessing import image
import matplotlib

matplotlib.use('TkAgg')

import matplotlib.pyplot as plt

# from sup5 import X_test, Y_test
from sklearn.metrics import roc_curve, auc


from keras.models import Model, Sequential
from keras.layers import Input, Convolution2D, ZeroPadding2D, MaxPooling2D, Flatten, Dense, Dropout, Activation
from PIL import Image
import numpy as np
from keras.preprocessing.image import load_img, save_img, img_to_array
from keras.applications.imagenet_utils import preprocess_input
from keras.preprocessing import image
import matplotlib.pyplot as plt

# from sup5 import X_test, Y_test
from sklearn.metrics import roc_curve, auc
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np

model = VGG16(weights='imagenet', include_top=False)




from keras.models import model_from_json




vgg_face_descriptor = Model(inputs=model.layers[0].input
                            , outputs=model.layers[-2].output)
# import  pandas as pd
# test_x_predictions = deep.predict(X_test)
# mse = np.mean(np.power(X_test - test_x_predictions, 2), axis=1)
# error_df = pd.DataFrame({'Reconstruction_error': mse,
#                         'True_class': Y_test})
# error_df.describe()
from PIL import Image


def preprocess_image(image_path):
    img = load_img(image_path, target_size=(224, 224))

    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)

    img = preprocess_input(img)
    return img


def findCosineSimilarity(source_representation, test_representation):
    a = np.matmul(np.transpose(source_representation), test_representation)
    b = np.sum(np.multiply(source_representation, source_representation))
    c = np.sum(np.multiply(test_representation, test_representation))
    return 1 - (a / (np.sqrt(b) * np.sqrt(c)))


def findEuclideanDistance(source_representation, test_representation):
    euclidean_distance = source_representation - test_representation
    euclidean_distance = np.sum(np.multiply(euclidean_distance, euclidean_distance))
    euclidean_distance = np.sqrt(euclidean_distance)
    return euclidean_distance


vgg_face_descriptor = Model(inputs=model.layers[0].input, outputs=model.layers[-2].output)

# for encod epsilon = 0.004
epsilon = 0.16
# epsilon = 0.095
retFalse,ret_val, euclidean_distance = verifyFace(str(i)+"test.jpg", str(j)+"train.jpg", epsilon)
  verifyFace1(str(i) + "testencod.jpg", str(j) + "trainencod.jpg")

Error : ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (512,14,14)->(512,newaxis,newaxis) (14,14,512)->(14,newaxis,newaxis) and requested shape (14,512)

SamBn04
  • 151
  • 1
  • 7
  • 9
    "resize [...] without increasing the size" What!? – Klaus D. Jun 03 '19 at 03:36
  • Your input has to be compatible with the images used to train the model. If it isn't the fit will be bad. That you means you need to understand those images. You may have to use some image package to properly scale the image. `numpy` reshape/resize functions won't do the job for you. – hpaulj Jun 03 '19 at 05:35
  • Question is oxymoron. – Deshwal Jan 05 '20 at 14:16

3 Answers3

5

I'm not sure what you mean, here is my solution for you. First method, if i understand clearly what you mean, for adding pad with zero value you need to use numpy.pad for each layer of image.

I use this image for take example, its shape is 158x84x3

I use this image for take example, its shape is 158x84x3

import numpy as np
import cv2
from matplotlib import pyplot as mlt
image = cv2.imread('zero.png')
shape = image.shape
add_x = int((256-shape[0])/2)
add_y = int((256-shape[1])/2)
temp_img = np.zeros((256,256,3),dtype = int)
for i in range(3):
    temp_img[:,:,i] = np.pad(image[:,:,i],((add_x,add_x),(add_y,add_y)),'constant', constant_values = (0))
mlt.imshow(temp_img)

By this code i can add padding into picture and have the result like this.

enter image description here

Now its shape is 256x256x3 like you want. Or another method for you is use Image of Pillow library. By using that, you can resize the picture without losing too much information with very simple code.

from PIL import Image
image = Image.fromarray(image)
img = image.resize((256, 256), Image.BILINEAR) 
mlt.imshow(img)

That code will give you this solution

enter image description here

Hope my answer can help you solve the problem!

CuCaRot
  • 1,208
  • 7
  • 23
  • Thank you for you answer my solution is to configure the vgg module to get a input size equal to 64x64x3 I will try to solve it @Francesco have the solution if it does not work I will try your method thank you again – SamBn04 Jun 04 '19 at 02:52
  • @SamBn04 if you use Keras, I recommend you use preprocessing function of Keras, which use by `from keras.applications.vgg16 import preprocess_input` , it will reshape your image to the right size of model `image = preprocess_input(image)` – CuCaRot Jun 04 '19 at 16:27
1

I think the best way to solve your problem is not resizing the image but rather to load the model specifying the input shape of your images. Assuming you are using keras:

model = VGG16(weights=..., include_top=False, input_shape=(64,64,3))

Include top has to be set to false in order to change the input shape, which means you will need to do some sort of training yourself. If you need include_top to be True resizing the input image is the best way to proceed, but a network trained on 224x224 images is probably not going to perform great with upscaled 64x64 images.

Francesco
  • 91
  • 4
  • thank you that's exactly what Im looking for but when I try my code I got this error TypeError: ('Keyword argument not understood:', 'include_top'). the code is posted in my question – SamBn04 Jun 04 '19 at 02:39
  • any ideas about Keras version – SamBn04 Jun 04 '19 at 04:44
  • You don't have to re write the architecture by hand. You might want to look at https://keras.io/applications/ and https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html – Francesco Jun 04 '19 at 09:39
  • I would try both input_shape=3x64x64 and 3x224x224. You may get better performance with 3x224x224 since the model was trained with that. – user650654 Nov 30 '21 at 01:44
-1

I think you mean resize (resolution) without increasing the size (amount of data) and as far as I'm aware, the answer would be no, because making the resolution bigger literally would mean a higher pixel count. You could resize the resolution without increasing the file size too much though, there are plenty of programs, websites and utilities for lightweight photo resizing, maybe you could implement the use of a service like such into your code?

Alex
  • 31
  • 1
  • 1
  • 3