介绍
假设你要搜索与任何图片相似的图像。想象一下在网上搜索与我们用手机拍摄的图像相似的图像。在本文中,我们将开发和比较两种不同的方式,这些方式可以使用深度学习算法解决在数千张图像(最相似的图像)之间进行查询的问题。
我们将比较两种不同的方法:
自动编码器
图像特征提取
数据
我们将使用Flipkart图像数据集来解决这个问题。因此,我们将从这种庞大的印度电子商务产品中找到相似的图片。
从数据上的可用URL下载图像后,我们获得了18322张不同产品的图像。
可以在此Github存储库中找到用于下载图像和开发两种方法的代码。
Github存储库:https://github.com/luchonaveiro/image-search-engine
自动编码器
让我们尝试通过使用自动编码器模型获得相似的图像。这种类型的模型包括三个主要部分:
编码器
潜在空间
解码器
该模型背后的思想是重构我们为算法提供的输入,因此输入和输出大小相同。因此,如果我们可以输入,则可以将图像的尺寸减小到非常小的向量,而该向量就是潜在空间。如果模型是健壮的,我们可以将图像的所有复杂性降低到较小的尺寸。
为了训练自动编码器,我们将使用Tensorflow 2.0库中的Keras模块。
下载图像后,我们可以定义训练和验证集。在这里,我们将使用ImageDataGenerator API。
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array, array_to_img
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.layers import Flatten, Conv2D, Conv2DTranspose, LeakyReLU, BatchNormalization, Input, Dense, Reshape, Activation
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint
import tensorflow.keras.backend as K
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm_notebook as tqdm
import pickle
import pandas as pd
# Load images
img_height = 256
img_width = 256
channels = 3
batch_size = 16
train_datagen = ImageDataGenerator(rescale=1./255,
validation_split=0.2)
training_set = train_datagen.flow_from_directory(
'./flipkart/images',
target_size = (img_height, img_width),
batch_size = batch_size,
class_mode = 'input',
subset = &