This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def centroid_embedding(outfit_embedding_list): | |
number_of_outfits = outfit_embedding_list.shape[0] | |
length_of_embedding = outfit_embedding_list.shape[1] | |
centroid = [] | |
for i in range(length_of_embedding): | |
centroid.append(np.sum(outfit_embedding_list[:, i])/number_of_outfits) | |
return centroid | |
def generate_image_mix(list_1, list_2, number_of_items=12, ratio=0.5): | |
list_1_num_items = int(number_of_items * ratio) |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def centroid_embedding(outfit_embedding_list): | |
number_of_outfits = outfit_embedding_list.shape[0] | |
length_of_embedding = outfit_embedding_list.shape[1] | |
centroid = [] | |
for i in range(length_of_embedding): | |
centroid.append(np.sum(outfit_embedding_list[:, i])/number_of_outfits) | |
return centroid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for images similar to centroid | |
def get_similar_images_annoy_centroid(annoy_tree, vector_value, number_of_items=12): | |
start = time.time() | |
similar_img_ids = annoy_tree.get_nns_by_vector(vector_value, number_of_items+1) | |
end = time.time() | |
print(f'{(end - start) * 1000} ms') | |
# ignore first item as it is always target image | |
return data_df_ouput.iloc[similar_img_ids[1:]] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_image_list = ImageList.from_df(df=data_df, path=root_path, cols='images').split_by_idxs( | |
(data_df[data_df['dataset']=='train'].index), | |
(data_df[data_df['dataset']=='val'].index)).label_from_df(cols='category') | |
data = train_image_list.transform(get_transforms(), size=224).databunch(bs=128).normalize(imagenet_stats) | |
data.add_test(ImageList.from_df(df=data_df[data_df['dataset'] == 'test'], path=root_path, cols='images')) | |
data.show_batch(rows=3, figsize=(8,8)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using Spotify's Annoy | |
def get_similar_images_annoy(annoy_tree, img_index, number_of_items=12): | |
start = time.time() | |
img_id, img_label = data_df_ouput.iloc[img_index, [0, 1]] | |
similar_img_ids = annoy_tree.get_nns_by_item(img_index, number_of_items+1) | |
end = time.time() | |
print(f'{(end - start) * 1000} ms') | |
# ignore first item as it is always target image | |
return img_id, img_label, data_df_ouput.iloc[similar_img_ids[1:]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load the trained model | |
def load_learner(data, pretrained_model, model_metrics, model_path): | |
learner = cnn_learner(data, pretrained_model, metrics=model_metrics) | |
learner.model = torch.nn.DataParallel(learner.model) | |
learner = learner.load(model_path) | |
return learner | |
pretrained_model = models.resnet18 | |
model_metrics = [accuracy, partial(top_k_accuracy, k=1), partial(top_k_accuracy, k=5)] | |
model_path = "/content/gdrive/My Drive/resnet18-fashion" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def train_model(data, pretrained_model, model_metrics): | |
learner = cnn_learner(data, pretrained_model, metrics=model_metrics) | |
learner.model = torch.nn.DataParallel(learner.model) | |
learner.lr_find() | |
learner.recorder.plot(suggestion=True) | |
return learner | |
pretrained_model = models.resnet18 | |
model_metrics = [accuracy, partial(top_k_accuracy, k=1), partial(top_k_accuracy, k=5)] | |
learner = train_model(data, pretrained_model, model_metrics) |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
NewerOlder