<jupyter_start><jupyter_text>File indexing  Create file indexing<jupyter_code># data_df = pd.DataFrame(columns=['index', 'path'])
# i = 0
# for file in sorted(os.listdir(f"/content/drive/MyDrive/CS336/dataset")):
#     data_df.loc[i] = [i, f"/content/drive/MyDrive/CS336/dataset/{file}"]
#     i += 1
# data_df<jupyter_output><empty_output><jupyter_text>Load index<jupyter_code>data_df = pd.read_csv("./index.csv", index_col=0)
data_df.head()<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>tf.keras.backend.clear_session()
input_shape = (224, 224, 3)
base_model = ResNet101(include_top=False, weights='imagenet', input_shape=input_shape)
model = Sequential([
                # input_layers,
                base_model,
                MaxPooling2D(strides=(7, 7)),
                Flatten()
])

# model.build((None, 224, 224, 3))
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 resnet101 (Functional)      (None, 7, 7, 2048)        42658176  
                                                                 
 max_pooling2d (MaxPooling2D  (None, 1, 1, 2048)       0         
 )                                                               
                                                                 
 flatten (Flatten)           (None, 2048)              0         
                                                                 
=================================================================
Total params: 42,658,176
Trainable params: 42,552,832
Non-trainable params: 105,344
_________________________________________________________________<jupyter_text>Save feature vectors<jupyter_code># features = []
# for i in notebook.tqdm_notebook(range(len(data_df))):
#     img = cv2.imread(data_df.iloc[i][1])
#     img = cv2.resize(img, input_shape[: 2])
#     img = tf.expand_dims(tf.convert_to_tensor(img), axis=0)
#     feature = model.predict(img, batch_size=None).squeeze()
#     features.append(feature)
# with open("./feature_vectors/ResNet101_mac_features.npy", 'wb') as f:
#     np.save(f, features)<jupyter_output><empty_output><jupyter_text>Load features<jupyter_code>with open('./feature_vectors/ResNet101_mac_features.npy', 'rb') as f:
    features = np.load(f)
features.shape<jupyter_output><empty_output><jupyter_text>Essential functions<jupyter_code>def Sort_Tuple(tup): 
    tup.sort(key = lambda x: x[1]) 
    return tup 
def compute_ap(rel, ranked_list):
    ap = 0.0
    precision = 1.0
    intersect_size = 0
    precision_list = []
    j = 0
    for i in range(len(ranked_list)):
        if ranked_list[i] in rel:
            intersect_size += 1
            precision = intersect_size / (j + 1.0)
            precision_list.append(precision)
        j += 1
    ap = np.array(precision_list).mean()
    return ap
def predict(IMGPATH, kmeans, features):
  query_img = cv2.imread(IMGPATH)
  query_img = cv2.resize(query_img, input_shape[: 2])
  query_img = tf.expand_dims(tf.convert_to_tensor(query_img), axis=0)

  query_features = model.predict(query_img, batch_size=None)

  label_query = kmeans.predict(query_features.astype(np.float32))

  rank_list_features = []
  rank_list_index = []

  for index,label in enumerate(kmeans.labels_,0):
      if label == label_query:
          rank_list_features.append(features[index])
          rank_list_index.append(index)

  sim = []

  for v in zip(rank_list_features, rank_list_index):
      cos_sim = np.dot(query_features, v[0]) / (np.linalg.norm(query_features) * np.linalg.norm(v[0]))
      sim.append((cos_sim,v[1])) 

  relevant_ans = Sort_Tuple(sim)

  return relevant_ans
def plot_result(IMGPATH, kmeans, features):
  relevant_ans = predict(IMGPATH, kmeans, features)
  for ans in relevant_ans[:len(relevant_ans) // 2]:
      img = cv2.imread(f"{data_df.iloc[ans[1]][1]}")
      plt.figure(figsize=(8, 8))
      plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
      plt.axis('off')
      plt.show()
def compute_mAP(DATAPATH, kmeans, features):
  ap =[] 
  for q in sorted(os.listdir(f"{DATAPATH}/query")): 
    IMGPATH = f"/content/drive/MyDrive/CS336/query/{q}"
    relevant_ans = predict(IMGPATH, kmeans, features)
    ranked_list = []
    for ans in relevant_ans:
      ranked_list.append(f"{data_df.iloc[ans[1]][1]}".split('/')[-1].split('.')[0])

    gt = []
    grd = f"{q[: q.rindex('_')]}.txt"
    with open(f'/content/drive/MyDrive/CS336/gt/{grd}') as gt_f:
        gt = [line.strip() for line in gt_f.readlines()]

    q_ap = compute_ap(gt, ranked_list)
    ap.append(q_ap)
  return np.array(ap).mean()
list_mAP = []
for i in notebook.tqdm_notebook(range(50,550, 50)):
  kmeans = KMeans(n_clusters=i, random_state=0).fit(features)
  list_mAP.append(compute_mAP(DATAPATH, kmeans, features))
np.around(np.array(list_mAP), decimals=3)
plt.figure(figsize=(12,8))
plt.rcParams.update({'font.size': 16})
plt.plot(range(50,550,50), list_mAP, marker='o')
plt.ylim([0.10, 0.5])
plt.xlabel('Cluster')
plt.ylabel('mAP')
plt.plot()

plot_result("/content/drive/MyDrive/CS336/query/all_souls_1_query.jpg", kmeans)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16125954198473283, 'fraction_numerical': 0.031106870229007634, 'mean_word_length': 3.267915309446254, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '558405', 'license': 'MIT', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1790, 'n_words': 289, 'path': 'notebooks/IR_ResNet101_MAC_Kmeans.ipynb', 'repo_name': 'trnhattan/CS336.M11.KHCL', 'size': '5240'}