我们可以在不增加尺寸的情况下将图像从64x64调整为256x256的大小吗

时间:2019-06-03 03:26:58

标签: python image numpy keras

我们可以在不增加尺寸的情况下将图像从64x64调整为256x256并影响分辨率吗,是一种在新调整大小的输出中对新行和列添加零的方法,我正在vgg上工作,添加我时出现错误输入图片为64x64,因为vggface是经过训练的模型,其输入大小为224

代码:

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")
  

错误:    ValueError:操作数不能与一起广播   重新映射的形状[原始->重新映射]:   (512,14,14)->(512,newaxis,newaxis)(14,14,512)->(14,newaxis,newaxis)   并要求形状(14,512)

3 个答案:

答案 0 :(得分:2)

我不确定您的意思,这是我为您提供的解决方案。 第一种方法,如果我清楚理解您的意思,则要添加零值的pad,则需要对图像的每一层使用numpy.pad。

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

我以这张图片为例,其形状为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)

通过此代码,我可以在图片中添加填充,并得到这样的结果。

enter image description here

现在,您可以根据需要将其形状设为256x256x3。 或者,另一种适合您的方法是使用Image of Pillow库。通过使用它,您可以通过非常简单的代码来调整图片的大小而不会丢失太多信息。

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

该代码将为您提供此解决方案

enter image description here

希望我的回答可以帮助您解决问题!

答案 1 :(得分:0)

我认为解决问题的最佳方法不是调整图像大小,而是加载指定图像输入形状的模型。 假设您正在使用keras:

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

必须将include top设置为false才能更改输入形状,这意味着您需要自己进行某种培训。 如果需要include_top为True,则调整输入图像的大小是最好的方法,但是在224x224图像上训练的网络可能无法在放大的64x64图像上表现出色。

答案 2 :(得分:-1)

我认为您的意思是调整大小(分辨率)而不增加大小(数据量) 据我所知,答案是否定的,因为从字面上扩大分辨率将意味着更高的像素数。您可以在不增加文件大小的情况下调整分辨率的大小,但是有许多程序,网站和实用程序可用于轻量级照片的大小调整,也许您可​​以在代码中实现类似服务的使用?

相关问题