Skip to content

Instantly share code, notes, and snippets.

View thiakx's full-sized avatar

Thia Kai Xin thiakx

View GitHub Profile
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)
@thiakx
thiakx / modeling-temporal-fashion-recommender-across-season.ipynb
Last active June 13, 2022 17:21
Modeling Temporal Fashion Recommender across Season
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@thiakx
thiakx / building-a-personalized-real-time-fashion-collection-recommender.ipynb
Last active June 13, 2022 17:23
Building a Personalized Real-Time Fashion Collection Recommender.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
# 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:]]
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))
# 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:]]
# 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"
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)
@thiakx
thiakx / covid-19-case-study-with-cnn.ipynb
Last active March 31, 2020 01:43
COVID-19 Case Study with CNN.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.