{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Please upload your pre-trained **\"tag_list_word2vec_64.npz\"** file to run this notebook.","metadata":{}},{"cell_type":"code","source":"import sys\nimport keras\nimport numpy as np\n\nfrom keras import models, layers, Model\n\ndata_folder  = \"/kaggle/input/109-2-uta-dl-app-hw2\"\nsys.path.append(data_folder) # For importing utiliy.py\nfrom utility import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_FEAT_FILE = data_folder + '/harrison_features.npz'\nMODEL_NAME = 'im_hashtag_model.h5'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Your uploaded htag2vec file location is \"/kaggle/input\" + your_dataset_name + \"/\" + filename\n","metadata":{}},{"cell_type":"code","source":"!ls /kaggle/input/  # You should be able to find your data here","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedding_size = 64\nTAG2VEC_FILE = '/kaggle/input/' + 'tag2vec-64' + '/' + 'tag_list_word2vec_' + str(embedding_size) + '.npz'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load Hashtag-to-Vector file\nfrom packaging import version\nt2v_data = None\nif version.parse(np.__version__) > version.parse(\"1.16.2\"):\n    t2v_data = np.load(TAG2VEC_FILE, allow_pickle=True) # word_count, dictionary, reverse_dictionary, word_embeddings\nelse:\n    t2v_data = np.load(TAG2VEC_FILE) # word_count, dictionary, reverse_dictionary, word_embeddings\n    \ntag_dictionary = t2v_data['dictionary'].tolist()\ntag_reverse_dict = t2v_data['reverse_dictionary'].tolist()\ntag_embeddings = t2v_data['word_embeddings']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feat_data = np.load(IMAGE_FEAT_FILE)\nimnet_fc_feats = feat_data['imagenet_fc_layers']\nplace_fc_feats = feat_data['places365_fc_layers']\nimage_list = feat_data['image_list']\ntag_strings = feat_data['hashtag_list']\ntrain_indices = feat_data['train_indices']\ntest_indices = feat_data['test_indices']\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get HARRISON labels\nlabels = img_paths_to_labels(image_list)\n\n# Get word vector of each image\nimage_t2v = []\nfor label in labels:\n    if label in tag_dictionary:\n        tagID = tag_dictionary[label]\n        image_t2v.append(tag_embeddings[tagID])\n    else:\n        print(label, \"doesn't exist!\")\n\nimage_t2v_mat = np.array(image_t2v)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split data into train/test set\ntrain_x = imnet_fc_feats[train_indices, :]\ntest_x = imnet_fc_feats[test_indices, :]\ntrain_y = image_t2v_mat[train_indices, :]\ntest_y = image_t2v_mat[test_indices, :]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Your Neural Network model\n#-------------\nmodel = models.Sequential()\nmodel.add(layers.Dense(128, activation='linear', input_shape=(2048,)))\nmodel.add(layers.Dense(embedding_size))\n#-------------\nmodel.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])\n\nhistory = model.fit(train_x, train_y, epochs=20, batch_size=512, verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(MODEL_NAME)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert HARRISON tag ground-truth string into wordID\ntag_list, miss_tags = tag_string_to_w2v_id(tag_strings, tag_dictionary)\ntest_tag_list = []\nfor tid in test_indices:\n    test_tag_list.append(tag_list[tid])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### Evaluate DeViSE Model ####\nmodel.evaluate(test_x, test_y)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_y = model.predict(test_x)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Don't change the code \"KNN = 10\"! Or your result won't be counted.","metadata":{}},{"cell_type":"code","source":"# Calculate K-Nearest Neighbor of predicted y (tag-vector)\nKNN = 10\nknn_list = []\nfor pred in pred_y:\n    knn_list.append(get_knn(pred, tag_embeddings, KNN))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"recall_rate, recall_list = cal_recall_rate(knn_list, test_tag_list)\nprint('The recall rate of our model is', recall_rate)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels = []\nfor tid in test_indices:\n    #test_tag_list.append(tag_list[tid])\n    test_labels.append(labels[tid])\n    \nrecall_rates, _ = cal_class_recall(test_labels, recall_list, test_tag_list)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\n\nplt.figure(figsize=(15,40))\nsns.set(style=\"whitegrid\")\nax = sns.barplot(x=list(recall_rates.values()), y=list(recall_rates.keys()))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# cal_hamming_loss.ipynb","metadata":{}},{"cell_type":"code","source":"loss, result_list = cal_hamming_loss(knn_list, test_tag_list)\nprint('Hamming loss (lower is better): %s' % loss)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_kaggle_hamming_loss_csv('predict_4_kaggle.csv', result_list)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nlocfile = \"./predict_4_kaggle.csv\"\nloczip = \"./predict_4_kaggle.zip\"\nzip = zipfile.ZipFile(loczip, \"w\", zipfile.ZIP_DEFLATED)\nzip.write(locfile)\nzip.close()\n# I zip it because that Kaggle can only download file less than 2 MB from HTML. \n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import HTML\ndef create_download_link(title = \"Download zip file\", filename = \"predict_4_kaggle.zip\"):  \n    html = '<a href={filename}>{title}</a>'\n    html = html.format(title=title,filename=filename)\n    return HTML(html)\n\n# create a link to download the dataframe which was saved with .to_csv method\ncreate_download_link(filename='predict_4_kaggle.zip')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}