def image_similarity(self, request_iterator, context):
        """Find image similarity between two images
        """
        # Initialize ML library to compare similarity between images
        img_vec = Img2Vec()

        # Read image stream in chunks received from client end
        image_streams = read_image_bytes_on_server(request_iterator)

        vectors = []
        for image_bytes in image_streams:
            # Create PIL image object from the image stream
            pil_image = Image.open(image_bytes)
            # Convert PIL image object into vectors
            vectors.append(img_vec.get_vec(pil_image, tensor=True))
            # Close image bytes streaming
            image_bytes.close()

        assert len(
            vectors
        ) == 2  # Must be two vectors as we are comparing to images, one for each
        # Find out similarity between images
        similarity = img_vec.cosine_similarity(*vectors)
        # Send response to the client
        return config_pb2.Reply(similarity=similarity)
def add_to_dict(pic_rel_path):
    img2vec = Img2Vec()
    filename = os.fsdecode(pic_rel_path)

    print("Filename is: %s" % filename)
    img = Image.open(os.path.join('.', filename))
    vec = img2vec.get_vec(img)
    pics[filename] = vec
예제 #3
0
def main(args):

    img2vec = Img2Vec()
    file_list = list_files(args.input_dir, args.ext)
    # For each test image, we store the filename and vector as key, value in a dictionary
    pics_vec_list = []
    pics_name = []
    for i, im_file in enumerate(file_list):
        img = Image.open(os.path.join(args.input_dir, im_file))
        vec = img2vec.get_vec(img)
        pics_name.append(im_file)
        pics_vec_list.append(vec)

    print("Do KMeans")
    X = np.array(pics_vec_list)
    kmeans = KMeans(n_clusters=12, random_state=0).fit(X)
    labels = kmeans.labels_
    print(labels)
    cluster_centers = kmeans.cluster_centers_

    labels_unique = np.unique(labels)
    n_clusters_ = len(labels_unique)

    closest, _ = pairwise_distances_argmin_min(kmeans.cluster_centers_, X)
    print(closest)

    for c in range(len(labels_unique)):
        mkdir_if_nonexist(os.path.join(args.output_dir, 'class_' + str(c)))

    select_path = os.path.join(args.output_dir, 'class_select')
    mkdir_if_nonexist(select_path)

    print("number of estimated clusters : %d" % n_clusters_)

    for i in range(len(pics_name)):
        cluster_id = labels[i]
        output_fpath = os.path.join(
            os.path.join(args.output_dir, 'class_' + str(cluster_id)),
            pics_name[i])
        input_fpath = os.path.join(args.input_dir, pics_name[i])
        copyfile(input_fpath, output_fpath)

    for i in range(len(closest)):
        select_pic = pics_name[closest[i]]
        output_fpath = os.path.join(select_path, select_pic)
        input_fpath = os.path.join(args.input_dir, select_pic)
        copyfile(input_fpath, output_fpath)

    return 0
예제 #4
0
def get_style(im_path='pasha_style.jpg'):

    img2vec = Img2Vec(model='alexnet')
    a = load_obj('vecs')
    names = load_obj('names')

    a = np.array(a)
    start = time()
    q = img2vec.get_vec(Image.open('pasha_style.jpg'))
    score = np.sum(q * a, axis=1) / np.linalg.norm(a, axis=1)
    topk_idx = np.argsort(score)[::-1]
    for i in topk_idx:
        print('> %s\t%s' % (score[i], names[i]))
    end = time()
    print(end - start)
    return [names[i] for i in topk_idx]
def func_get_ImageVectors(path):
    veclist = []
    # Initialize Img2Vec with GPU
    img2vec = Img2Vec(cuda=False)
    print path
    # Read in an image
    for filename in glob.iglob(path + '/*.jpg'):
        # print filename
        img = Image.open(filename)
        # Get a vector from img2vec
        vec = img2vec.get_vec(img)
        veclist.append(vec)
    #   print vec.shape
    #   print len(veclist)
    #
    # print "Above are vec details===>"
    # convert the veclist to numpy array
    data = np.asarray(veclist)
    # print "data----shape"
    # print data.shape

    return data
예제 #6
0
import sys
import os
sys.path.append("..")  # Adds higher directory to python modules path.
from img_to_vec import Img2Vec
from PIL import Image
from sklearn.metrics.pairwise import cosine_similarity

input_path = './test_images'

img2vec = Img2Vec(cuda=True, model='resnet-152')

# For each test image, we store the filename and vector as key, value in a dictionary
pics = {}
for file in os.listdir(input_path):
    filename = os.fsdecode(file)
    img = Image.open(os.path.join(input_path, filename))
    vec = img2vec.get_vec(img)
    pics[filename] = vec
    print(vec.shape)
pic_name = ""
while pic_name != "exit":
    pic_name = str(input("Which filename would you like similarities for?\n"))

    try:
        sims = {}
        for key in list(pics.keys()):
            if key == pic_name:
                continue

            sims[key] = cosine_similarity(pics[pic_name].reshape((1, -1)),
                                          pics[key].reshape((1, -1)))[0][0]
예제 #7
0
#####################################################################################
#																					#
#	 This is an example script for extracting features of an image. For your own    #
#	 self portraits, you should get inspiration from this script. After extracting  #
#	 the feature vector, you can use it with your trained network.                  #
#																					#
#	 Note that, this requires torchvision, Pillow and NumPy packages.				#
#	 You are not forced to totally understand how the feature extractor works.      #
#	 You can just ignore the warnings given by the script.							#
#																					#
#####################################################################################
import sys

from PIL import Image
import numpy as np
from img_to_vec import Img2Vec

if __name__ == '__main__':
    if len(sys.argv) != 2:
        pritn("Give the path of the image as an argument.")
        sys.exit(0)
    image_path = sys.argv[1]
    fe = Img2Vec(
        cuda=False)  # change this if you use Cuda version of the PyTorch.
    img = Image.open(image_path)
    img = img.resize((224, 500))
    feats = fe.get_vec(img).reshape(1, -1)
    np.save(image_path + "_features.npy", feats)
    print(type(feats), feats.shape)
예제 #8
0
def load_img2vec():
    from img_to_vec import Img2Vec
    global img2vec
    img2vec = Img2Vec()
                              x='PC2',
                              y='PC1',
                              c=existing_df_2d.cluster.astype(np.float),
                              figsize=(16, 8))

    for i, country in enumerate(existing_df.index):
        axk.annotate(
            country,
            (existing_df_2d.iloc[i].PC2 + 2, existing_df_2d.iloc[i].PC1 + 2))

    plt.show()


if __name__ == '__main__':
    PATH = './test_scrape/puppies/'
    img2vec = Img2Vec(cuda=False)
    cos = nn.CosineSimilarity(dim=1, eps=1e-6)

    file = 'puppy_ON.csv'
    if os.path.isfile(Path(file)):
        os.remove(file)

    all_files = [PATH + file for file in os.listdir(PATH)]
    feature_vectors = compute_vectors(all_files)
    output_results(feature_vectors, file)

    existing_df = pd.read_csv(file, index_col=0)
    existing_df.index.names = ['file_names']
    existing_df.columns.names = ['r_num']

    existing_df, existing_df_2d = do_PCA(existing_df)
예제 #10
0
input_path = '../../../data/19Breast'
# input_path = '../../../data/test_images'

# chose a model
# model='resnet-18'
# img2vec = Img2Vec()

# model = 'alexnet'
# model = 'vgg19_bn'
model = 'vgg19_bn_attention'
model_zoo = [
    'resnet-18', 'alexnet', 'vgg19_bn', 'vgg_19bn_attention', 'squeezenet1_1',
    'inception_v3', 'Densenet121'
]
img2vec = Img2Vec(cuda=False, model=model, layer=2, layer_output_size=4096)

# For each test image, we store the filename and vector as key, value in a dictionary
pics = {}
for file in os.listdir(input_path):
    filename = os.fsdecode(file)
    img = Image.open(os.path.join(input_path, filename))
    vec = img2vec.get_vec(img)
    pics[filename] = vec

# output: top 10 most similar images for a specific image, and calculate all of the image in folder
log_file = open('./%s.txt' % model, 'w')
for pic_name in os.listdir(input_path):
    log_file.write('%s top 10 similar images:\n' % pic_name)
    # pic_name = ""
    # while pic_name != "exit":
예제 #11
0
def startup():
    img2vec = Img2Vec()
    male_list_of_clothing_info = []
    female_list_of_clothing_info = []

    # Check to see if we have already generated the feature vectors
    # of our database images for men's clothing
    male_vectors_generated = True
    if os.path.isfile("male_list_of_clothing_info.pkl") == False:
        male_vectors_generated = False

    # Check to see if we have already generated the feature vectors
    # of our database images for women's clothing
    female_vectors_generated = True
    if os.path.isfile("female_list_of_clothing_info.pkl") == False:
        female_vectors_generated = False

    # If we have not generated the feature vectors yet for male clothing,
    # we need to go through and create them and store them
    if male_vectors_generated == False:
        print("Calculating feature vectors of male clothing")

        male_json_path = "json/men"
        for file in tqdm(os.listdir(male_json_path)):
            if file == ".DS_Store":
                continue

            file_path = male_json_path + "/" + file
            with open(file_path) as f:
                data = json.load(f)

                male_list_of_clothing_info = []
                for i in tqdm(data):
                    response = requests.get(i["images"])
                    img = Image.open(BytesIO(response.content))
                    feature_vec = img2vec.get_vec(img)
                    i["feature_vec"] = feature_vec
                    male_list_of_clothing_info.append(i)

        output = open('male_list_of_clothing_info.pkl', 'wb')
        pickle.dump(male_list_of_clothing_info, output)
        output.close()

    else:
        pkl_file = open('male_list_of_clothing_info.pkl', 'rb')
        male_list_of_clothing_info = pickle.load(pkl_file)
        pkl_file.close()

    # If we have not generated the feature vectors yet for female clothing,
    # we need to go through and create them and store them
    if female_vectors_generated == False:
        print("Calculating feature vectors of female clothing")

        female_json_path = "json/women"
        for file in tqdm(os.listdir(female_json_path)):
            if file == ".DS_Store":
                continue

            file_path = female_json_path + "/" + file
            with open(file_path) as f:
                data = json.load(f)

                female_list_of_clothing_info = []
                for i in tqdm(data):
                    response = requests.get(i["images"])
                    img = Image.open(BytesIO(response.content))
                    feature_vec = img2vec.get_vec(img)
                    i["feature_vec"] = feature_vec
                    female_list_of_clothing_info.append(i)

        output = open('female_list_of_clothing_info.pkl', 'wb')
        pickle.dump(female_list_of_clothing_info, output)
        output.close()

    else:
        pkl_file = open('female_list_of_clothing_info.pkl', 'rb')
        female_list_of_clothing_info = pickle.load(pkl_file)
        pkl_file.close()

    return male_list_of_clothing_info, female_list_of_clothing_info
예제 #12
0
def load_img2vec():
    from img_to_vec import Img2Vec
    global img2vec
    img2vec = Img2Vec(model='alexnet')
예제 #13
0
import numpy as np
import math
import sys
import argparse
import os
import random
import pickle
import sklearn.preprocessing
import sklearn.linear_model
import sklearn.svm
from img_to_vec import Img2Vec
from PIL import Image

# Initialize img2vec. This triggers downloading the neural net model if not present.
img2vec = Img2Vec(cuda=False, model='resnet-18')


def image_make_rgb(img):
    # https://stackoverflow.com/questions/9166400/convert-rgba-png-to-rgb-with-pil
    # img must be loaded first.
    flatimg = Image.new("RGB", img.size, (255, 255, 255))
    splitimg = img.split()
    if len(splitimg) > 3:
        flatimg.paste(img, mask=img.split()[3])
    else:
        flatimg.paste(img)
    return flatimg


def get_vec_from_file(fpath):
예제 #14
0
import sys
import pickle
import scipy
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from pathlib import Path
from gluoncv import model_zoo, data, utils

config = {}
with open('config.json', 'r') as fp:
    config = json.load(fp)

sys.path.append(os.path.join(config['path'], 'src'))
from img_to_vec import Img2Vec
img2vec = Img2Vec(model = config['arch_scene'])

def img_channels(img):
    # Reshape for 3-channels
    if len(np.array(img).shape) != 3:
        img = np.stack((img,)*3, axis=-1)
        img = Image.fromarray(np.uint8(img))
    elif np.array(img).shape[2] > 3:
        img = img.convert('RGB')
        img = np.asarray(img, dtype=np.float32)
        img = img[:, :, :3]
        img = Image.fromarray(np.uint8(img))

    return img

root = os.path.join(config['path'], 'data', 'SUN397')
예제 #15
0
from shutil import copyfile

sys.path.insert(1, os.getcwd())

import system

from img_to_vec import Img2Vec
from PIL import Image
import numpy as np
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA

input_path = "./tmp/new"
files = os.listdir(input_path)

img2vec = Img2Vec()
vec_length = 512  # Using resnet-18 as default

samples = len(files)  # Amount of samples to take from input path
k_value = 6  # How many clusters

# Matrix to hold the image vectors
vec_mat = np.zeros((samples, vec_length))
# If samples is < the number of files in the folder, we sample them randomly
sample_indices = np.random.choice(range(0, len(files)),
                                  size=samples,
                                  replace=False)

print('Reading images...')
for index, i in enumerate(sample_indices):
    file = files[i]
예제 #16
0
from torch.nn import functional as F
from torchvision import transforms as trn
from torch.autograd import Variable as V
import torchvision.transforms as transforms

config = {}
with open('config.json', 'r') as fp:
    config = json.load(fp)

sys.path.append(os.path.join(config['path'], 'src'))
from img_to_vec import Img2Vec

input_path = os.path.join(config['path'], 'debug', 'test_images')

arch = 'resnet-18-ImageNet'
img2vec = Img2Vec(model=arch)

# For each test image, we store the filename and vector as key, value in a dictionary
pics = {}
for file in os.listdir(input_path):
    filename = os.fsdecode(file)
    img = Image.open(os.path.join(input_path, filename))
    vec = img2vec.get_vec(img)
    pics[filename] = vec

pic_name = 'catdog.jpg'  # The image that will be compared with other ones

####################################### SIMILARITY #######################################

sims = {}
for key in list(pics.keys()):
def main(args):
    # Create model directory
    if not os.path.exists(args.model_path):
        os.makedirs(args.model_path)

    transform = transforms.Compose([
        # transforms.ColorJitter(contrast = 0.3,saturation = 0.3),
        # transforms.RandomChoice([transforms.RandomHorizontalFlip(),transforms.RandomVerticalFlip()]),
        transforms.RandomAffine(0,translate = (0.1,0.1)),
        transforms.ToTensor(), 
        transforms.Normalize((0.8, 0.7, 0.8), 
                            (1, 1, 1))
        ])
    
    # Load vocabulary wrapper.
    with open(args.vocab_path, 'rb') as f:
        vocab = pickle.load(f)
    
    # data_loader = get_loader(args.image_dir, args.caption_path, vocab, 
    #                          transform, args.batch_size,
    #                          shuffle=True, num_workers=args.num_workers) 
    sasr_data_loader = SASR_Data_Loader(vocab,transform)
    sasr_data_loader.load_data(args.data_file,args.init_flag)
    frogger_data_loader = sasr_data_loader.data_loader(args.batch_size, 
                             transform,
                             shuffle=True, num_workers=args.num_workers) 
    # Build the models
    encoder = EncoderCNN(args.embed_size)
    decoder = DecoderRNN(args.embed_size, args.hidden_size, 
                         len(vocab), args.num_layers)
    
    if torch.cuda.is_available():
        encoder.cuda()
        decoder.cuda()

    # Loss and Optimizer
    criterion = nn.CrossEntropyLoss()
    params = list(decoder.parameters()) + list(encoder.linear.parameters()) + list(encoder.bn.parameters()) + list(encoder.resnet.parameters())
    optimizer = torch.optim.Adam(params, lr=args.learning_rate)
    stransform = transforms.ToPILImage()

    img2vec = Img2Vec()
    total_step = len(frogger_data_loader)
    for epoch in range(args.num_epochs):
        for i,(images,captions,lengths) in enumerate(frogger_data_loader):
            # image1 = images[0].squeeze()
            # # print(image1.size())
            # # c = stransform(image1)
            # # vec = img2vec.get_vec(c,True)
            # # # print(vec)
            # # c.save('save_image1.png')
            # # image2 = images[1].squeeze()
            # # print(image2.size())
            # # c = stransform(image2)
            # # # vec = img2vec.get_vec(c)
            # # # print(vec)
            # # c.save('save_image2.png')
            images = to_var(images, volatile=True)
            # images = images.to(device)
            if (list(images.size())[0]!=1):
                captions = to_var(captions)
                
                # print(images[0])
                targets = pack_padded_sequence(captions, lengths, batch_first=True)[0]
                decoder.zero_grad()
                encoder.zero_grad()
                # print(images) 
                features = encoder(images)
                outputs = decoder(features, captions, lengths)
                loss = criterion(outputs, targets)
                loss.backward()
                optimizer.step()

                # Print log info
                if i % args.log_step == 0:
                    print('Epoch [%d/%d], Step [%d/%d], Loss: %.4f, Perplexity: %5.4f'
                          %(epoch, args.num_epochs, i, total_step, 
                            loss.data[0], np.exp(loss.data[0]))) 
                    
                # Save the models
                if (i+1) % args.save_step == 0:
                    torch.save(decoder.state_dict(), 
                               os.path.join(args.model_path, 
                                            'decoder-%d-%d.pkl' %(epoch+1, i+1)))
                    torch.save(encoder.state_dict(), 
                               os.path.join(args.model_path, 
                                        'encoder-%d-%d.pkl' %(epoch+1, i+1)))
예제 #18
0
#	 self portraits, you should get inspiration from this script. After extracting  #
#	 the feature vector, you can use it with your trained network.                  #
#																					#
#	 Note that, this requires torchvision, Pillow and NumPy packages.				#
#	 You are not forced to totally understand how the feature extractor works.      #
#	 You can just ignore the warnings given by the script.							#
#																					#
#####################################################################################

from PIL import Image
from img_to_vec import Img2Vec
import numpy as np

if __name__ == '__main__':
    fe = Img2Vec(
        cuda=True
    )  # change this if you cannot use Cuda version of the PyTorch.

    img = Image.open('../ismail.jpg')
    img = img.resize((224, 224))
    feats = fe.get_vec(img).reshape(1, -1)

    with open('../ismail.npy', 'wb') as file:
        np.save(file, feats)

    img = Image.open('../ismail_sunglasses.jpg')
    img = img.resize((224, 224))
    feats = fe.get_vec(img).reshape(1, -1)

    with open('../ismail_sunglasses.npy', 'wb') as file:
        np.save(file, feats)
예제 #19
0
import glob
import sys
import numpy as np
from img_to_vec import Img2Vec
from PIL import Image

img2vec = Img2Vec(cuda=True)
version = 1


def ExtractImageFeature(path):
    img = Image.open(path)
    return img2vec.get_vec(img)


def SaveDict(data):
    import pickle
    with open('image_feature_dictionary.pkl', 'wb') as f:
        print("saving...")
        pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
        print("saved!")


if __name__ == "__main__":

    # Where we will search for images
    path_to_images = "data/icons_v{}/".format(version)  #sys.argv[1]

    # Dictionary to store results
    result = {}
예제 #20
0
 def __init__(self):
     self.logger = logging.getLogger(self.__class__.__name__)
     self.logger.setLevel(logging.DEBUG)
     self.img2vec = Img2Vec()
예제 #21
0
import sys
import os
from img_to_vec import Img2Vec
from PIL import Image
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
from time import time
from saveload import save_obj
input_path = './Styles'

img2vec = Img2Vec(model='alexnet')
a = []
names = []
# For each test image, we store the filename and vector as key, value in a dictionary
pics = {}
for file in os.listdir(input_path):
    filename = os.fsdecode(file)
    img = Image.open(os.path.join(input_path, filename))
    vec = img2vec.get_vec(img)
    pics[filename] = vec
    a.append(vec)
    names.append(file)
save_obj(pics, 'pics_vocab')
save_obj(a, 'vecs')
save_obj(names, 'names')
예제 #22
0
import pickle
import cv2
from img_to_vec import Img2Vec
from PIL import Image

path_to_train = './data/SUN/training_set'
path_to_test = './data/SUN/testing_set'

categories = {'bathroom': 0, 'bedroom': 1, 'classroom': 2, 'computer_room': 3, 'conference_room': 4,
'corridor': 5, 'dining_area': 6, 'dining_room': 7, 'discussion_area': 8, 'furniture_store': 9,
 'home_office': 10, 'kitchen': 11, 'lab': 12, 'lecture_theatre': 13, 'library': 14, 'living_room': 15, 'office': 16, 'rest_space': 17, 'study_space': 18}

total_classes=19
total_num=[0]*total_classes

img2vec = Img2Vec(cuda = True, model = 'resnet_places')
train_features = []
train_labels = []
test_features = []
test_labels = []
import types
iter = 0
for label in os.listdir(path_to_train):
    folder = os.path.join(path_to_train,label)
    if label not in categories:
        categories[label] = categories['others']
    for file in os.listdir(folder):
        path = os.path.join(folder,file)
        img = Image.open(path)
        vec = img2vec.get_vec(img)
        if vec is not None: