text_plain_output_1.png
from tensorflow.keras.applications.resnet50 import ResNet50

from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions

from tensorflow.keras.applications.vgg16 import VGG16

from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions

from tensorflow.keras.applications.vgg19 import preprocess_input, decode_predictions

from tensorflow.keras.layers import GlobalMaxPooling2D

from tensorflow.keras.preprocessing import image

import cv2

import matplotlib.pyplot as plt

import numpy as np

import numpy as np # linear algebra

import os

import os

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sn

import tensorflow as tf

import numpy as np
import pandas as pd
import os

DATASET_PATH = '../input/fashion-product-images-small/myntradataset'

DATASET_PATH = '../input/fashion-product-images-small/myntradataset'
df = pd.read_csv(DATASET_PATH + '/styles.csv', nrows=5000, error_bad_lines=False)

df['image'] = df.apply(lambda row: str(row['id']) + '.jpg', axis=1).reset_index(drop=True)

def plot_figures(figures,nrows = 1,ncols = 1,figsize = (8,8)):
    fig,axeslist = plt.subplots(ncols = ncols,nrows = nrows,figsize = figsize)
    for index,title in enumerate(figures):
        axeslist.ravel()[index].imshow(cv2.cvtColor(figures[title], cv2.COLOR_BGR2RGB))
        axeslist.ravel()[index].set_title(title)
        axeslist.ravel()[index].set_axis_off()
        
    plt.tight_layout()
    
def img_path(img):
    DATASET_PATH = '../input/fashion-product-images-small/myntradataset'
    return DATASET_PATH+"/images/"+img

def load_image(img, resized_fac = 0.1):
    img     = cv2.imread(img_path(img))
    w, h, _ = img.shape
    resized = cv2.resize(img, (int(h*resized_fac), int(w*resized_fac)), interpolation = cv2.INTER_AREA)
    return resized   

import tensorflow as tf
import keras
from keras import Model
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg19 import VGG19
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions
from tensorflow.keras.applications.vgg19 import preprocess_input, decode_predictions
from tensorflow.keras.layers import GlobalMaxPooling2D
tf.__version__

img_width, img_height, _ = (224, 224, 3)
model_1 = ResNet50(weights='imagenet', include_top=False, input_shape=(img_width, img_height, 3))
model_1.trainable = False
model_1 = tf.keras.models.Sequential([model_1, GlobalMaxPooling2D()])
model_1.summary()

def get_embedding(model, img_name):
    img = image.load_img(img_path(img_name), target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    return model.predict(x).reshape(-1)

emb = get_embedding(model_1, df.iloc[0].image)
emb.shape

a = [list(i) for i in complete_indices]
sub_cat = [df['subCategory'].iloc[a[index]].to_list() for index in range(df.shape[0])]
mast_cat = [df['masterCategory'].iloc[a[index]].to_list() for index in range(df.shape[0])]
resnet_50 = pd.DataFrame()
resnet_50['id'] = df['id']
resnet_50['recommended_index'] = [list(i) for i in complete_indices]
resnet_50['recommended_scores'] = complete_scores
resnet_50['masterCategory'] = df['masterCategory']
resnet_50['subCategory'] = df['subCategory']
resnet_50['Recommended_master_category'] = mast_cat
resnet_50['Recommended_sub_category'] = sub_cat
resnet_50['mean_recommended_score'] = mean_sim_scores

def getmatch_score(resnet_50, col, col1):
    match_list = []
    for index in range(resnet_50.shape[0]):
        try:
            actual_match = resnet_50[col].iloc[index]
            predicted_match = resnet_50[col1].iloc[index]
            count = 0
            for item in predicted_match:
                if item == actual_match:
                    count += 1
            match_list.append(count / 5 * 100)
        except Exception as ex:
    return match_list
resnet_50['Match_Master_Score'] = getmatch_score(resnet_50, 'masterCategory', 'Recommended_master_category')
resnet_50['Match_Sub_Score'] = getmatch_score(resnet_50, 'subCategory', 'Recommended_sub_category')

img_width, img_height, _ = (224, 224, 3)
model_2 = VGG16(weights='imagenet', include_top=False, input_shape=(img_width, img_height, 3))
model_2.trainable = False
model_2 = tf.keras.models.Sequential([model_2, GlobalMaxPooling2D()])
model_2.summary()

vgg_emb = get_embedding(model_2, df.iloc[0].image)
vgg_emb.shape

a = [list(i) for i in complete_indices_vgg]
sub_cat = [df['subCategory'].iloc[a[index]].to_list() for index in range(df.shape[0])]
mast_cat = [df['masterCategory'].iloc[a[index]].to_list() for index in range(df.shape[0])]
vgg_16 = pd.DataFrame()
vgg_16['id'] = df['id']
vgg_16['recommended_index'] = [list(i) for i in complete_indices_vgg]
vgg_16['recommended_scores'] = complete_scores_vgg
vgg_16['masterCategory'] = df['masterCategory']
vgg_16['subCategory'] = df['subCategory']
vgg_16['Recommended_master_category'] = mast_cat
vgg_16['Recommended_sub_category'] = sub_cat
vgg_16['mean_recommended_score'] = mean_sim_scores_vgg

def getmatch_score(data, col, col1):
    match_list = []
    for index in range(data.shape[0]):
        try:
            actual_match = data[col].iloc[index]
            predicted_match = data[col1].iloc[index]
            count = 0
            for item in predicted_match:
                if item == actual_match:
                    count += 1
            match_list.append(count / 5 * 100)
        except Exception as ex:
    return match_list
vgg_16['Match_Master_Score'] = getmatch_score(vgg_16, 'masterCategory', 'Recommended_master_category')
vgg_16['Match_Sub_Score'] = getmatch_score(vgg_16, 'subCategory', 'Recommended_sub_category')

sn.distplot(vgg_16['mean_recommended_score'])