<jupyter_start><jupyter_text>Bag of words<jupyter_code>from sklearn.feature_extraction.text import CountVectorizer

training_size = 10000

training_descriptions = []
for i in range(training_size):
    if i % 1000 == 0:
        print(i)
    training_descriptions.append(parse_description(i))
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(training_descriptions)
X = X.toarray()
print( len(vectorizer.get_feature_names()) )
print( vectorizer.get_feature_names()[0:50] )<jupyter_output>4923
['abdomen', 'abil', 'abll', 'aboard', 'abook', 'abou', 'abraham', 'abreast', 'abund', 'abyss', 'acacia', 'accent', 'access', 'accessori', 'accid', 'account', 'ace', 'acket', 'aclock', 'acm', 'acorn', 'across', 'act', 'action', 'activ', 'actor', 'ad', 'add', 'addit', 'address', 'adjac', 'adjust', 'adolesc', 'adult', 'adventur', 'advertis', 'aer', 'aeroplan', 'afar', 'affair', 'affect', 'afghan', 'afield', 'africa', 'afro', 'afterburn', 'afternoon', 'agaist', 'age', 'agenc']<jupyter_text>MLP<jupyter_code>from sklearn.neural_network import MLPClassifier
from sklearn.neural_network import MLPRegressor

def mlp(X, y, test):
    clf = MLPRegressor(solver='sgd', alpha=1e-5, max_iter=2000,
                    hidden_layer_sizes=(2048, 1024, 1024), random_state=1)
    clf.fit(X, y)
    return clf.predict(test)<jupyter_output><empty_output><jupyter_text>Get top 20<jupyter_code>import heapq
import math

def cosine_similarity(target, candid):
    prod = np.dot(target, candid)
    len1 = math.sqrt(np.dot(target, target))
    len2 = math.sqrt(np.dot(candid, candid))
    return prod / (len1 * len2)
    
def closest_20_neighbors(target, candid):
    dist2index = {}
    idx = 0
    
    pq = []
    for c in candid:
        # d = np.linalg.norm(target - c)
        d = -cosine_similarity(target, c)
        
        dist2index[d] = idx
        pq.append(d)
        idx += 1

    res = []
    for d in heapq.nsmallest(50, pq):
        res.append(dist2index[d])
    return res<jupyter_output><empty_output><jupyter_text>Cross validation<jupyter_code>from random import shuffle
label = [i for i in range(10000)]
shuffle(label)

y = features_resnet1000intermediate_train

# wordvec_train = np.zeros((8000, len(vectorizer.get_feature_names())))
wordvec_train = np.zeros((8000, pca_components))
feature_train = np.zeros((8000, 2048))
# wordvec_test = np.zeros((2000, len(vectorizer.get_feature_names())))
wordvec_test = np.zeros((2000, pca_components))
feature_test = np.zeros((2000, 2048))

y_label = []

for i in range(8000):
    wordvec_train[i] = X_pca[label[i]]
    feature_train[i] = y[label[i]]
for i in range(2000):
    wordvec_test[i] = X_pca[label[i + 8000]]
    feature_test[i] = y[label[i + 8000]]
    y_label.append(label[i])
feature_predict = mlp(wordvec_train, feature_train, wordvec_test)
print('predict finished')
idx = 0
score_sum = 0
for y_p in feature_predict:
    top_20 = closest_20_neighbors(y_p, feature_test)
    j = 1
    for i in top_20:
        if i == idx:
            # MAP@20
            score_sum += float(21 - j) / 20.0
            break
        j += 1
    idx += 1
    if idx % 100 == 0:
        print(idx)
score = score_sum / 2000.0
print(score)
print(feature_predict[0])
print(y.shape)
print(X.shape)<jupyter_output>(10000, 2048)
(10000, 4923)<jupyter_text>Reduce dimension<jupyter_code>import tensorflow as tf


from sklearn.decomposition import PCA

pca_components = 1024
pca = PCA(n_components=pca_components)
X_pca = pca.fit_transform(X)
print(X_pca[0])<jupyter_output>[ 0.60180886  0.14567559 -1.49503461 ...  0.00944589 -0.02258241
 -0.00586142]<jupyter_text>Training with full set<jupyter_code>clf = MLPRegressor(solver='sgd', alpha=1e-5, max_iter=2000,
                hidden_layer_sizes=(4096, 4096, 2048), random_state=1)
clf.fit(y, X_pca)
print("finished.")
wordvec_predict = clf.predict(feature_test)

idx = 0
score_sum = 0
tops = []
for x_p in wordvec_test:
    top_20 = closest_20_neighbors(x_p, wordvec_predict)
    tops.append(top_20)
    j = 1
    for i in top_20:
        if i == idx:
            # MAP@20
            score_sum += float(21 - j) / 20.0
            break
        j += 1
    idx += 1
    if idx % 100 == 0:
        print(idx)
    
score = score_sum / 2000.0
print(score)
for i in range(10):
    print(tops[i][:10])
word2index = {}

idx = 0
for w in vectorizer.get_feature_names():
    word2index[w] = idx
    idx += 1

print(len(word2index))
descriptions_test_dir = 'data/descriptions_test/'
features_test_dir = 'data/features_test/'
images_test_dir = 'data/images_test/'
tags_test_dir = 'data/tags_test/'

table = str.maketrans('', '', string.punctuation)
stop_words = set(stopwords.words('english'))
stemmer = SnowballStemmer("english")
lemmatizer = WordNetLemmatizer()

def parse_test_description(i):
    descriptions_test_file = open(descriptions_test_dir + str(i) + '.txt', 'r') 
    lines = descriptions_test_file.read()
    # print(lines)
    
    nouns = []
    for word,pos in nltk.pos_tag(nltk.word_tokenize(lines)):
        if (pos == 'NN' or pos == 'NNP' or pos == 'NNS' or pos == 'NNPS'):
            nouns.append(word)
    
    words = [w.lower() for w in nouns]
    words = [w for w in words if not w in stop_words] # strip stopword
    words = [stemmer.stem(w) for w in words] # stem
    
    return words
    
test_size = 2000
X_real_test = np.zeros((test_size, len(vectorizer.get_feature_names())))

for i in range(test_size):
    words = parse_test_description(i)
    for w in words:
        if w in word2index:
            X_real_test[i][word2index[w]] += 1    

X_real_test_pca = pca.transform(X_real_test)

feature_real_test = np.zeros((2000, 2048)) # resnet feature

features_test_dir = 'data/features_test/'
filename = 'features_resnet1000intermediate_test.csv'

with open(features_test_dir + filename) as f:
    reader = csv.reader(f)
    for line in reader:
        index = int(line[0][12:].split('.')[0])
        feature_real_test[index] = line[1:]

feature_real_test.astype(float)

wordvec_real_test_predict = clf.predict(feature_real_test)
idx = 0
res = []
for x_p in X_real_test_pca:
    top_20 = closest_20_neighbors(x_p, wordvec_real_test_predict)
    res.append(top_20)
    idx += 1
    if idx % 100 == 0:
        print(idx)
# Using tags

def parse_tag(i):
    tag_test_file = open(tags_test_dir + str(i) + '.txt', 'r') 
    lines = tag_test_file.read()
    if lines == '':
        return ['?']
    tags_pre = lines.rstrip().split('\n')
    tags_pre = [w.split(':')[1] for w in tags_pre]
    
    tags = []
    for w in tags_pre:
        for ww in w.split(' '):
            tags.append(ww)
    tags = [stemmer.stem(w) for w in tags] # stem
    return tags

test_tags = []
for i in range(test_size):
    test_tags.append(parse_tag(i))

print(test_tags[598])

for i in range(test_size):
    rank = 1
    scores = []
    for candid in res[i]:
        # compute tags score for each candidate
        s = 0
        tags = test_tags[candid]
        for tag in tags:
            if tag in word2index:
                s -= X_real_test[i][word2index[tag]] * 10
            else:
                s += 10
        s += rank
        scores.append(s)
        rank += 1
        
    top_idx = sorted(range(20), key=lambda i: scores[i])
    new_res = []
    for idx in top_idx:
        new_res.append(res[i][idx])
    res[i] = new_res<jupyter_output>['airplan', 'traffic', 'light']<jupyter_text>Output submission<jupyter_code>output_filename = 'submission_feature2wordvec_nouns_without_tags_origin_top50_consine.csv'
with open(output_filename, 'w') as f:
    writer = csv.writer(f)
    writer.writerow(["Descritpion_ID","Top_20_Image_IDs"])
    for i in range(test_size):
        row = []
        row.append(str(i) + '.txt')
        candids = []
        for candid in res[i]:
            candids.append(str(candid) + '.jpg')
        row.append(' '.join(candids))
        writer.writerow(row)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup