Reputation: 38255
I am creating image feature vector using resnet50 in PyTorch. Each feature vector is of length 2048. When I want to write it to a txt file, I have to convert it to str which I did in the code below. Problem is, only a few numbers from the vector of length 2048 is saved in the txt file. How can I fix this?
Additionally, each of my filenames (images) have a label associated between 1 to 9 (9 classes).
The code below is a modification from this repo: https://github.com/christiansafka/img2vec
import numpy as np
import sys
import os
sys.path.append("..") # Adds higher directory to python modules path.
from img_to_vec import Img2Vec
from PIL import Image
from sklearn.metrics.pairwise import cosine_similarity
import glob
input_path = "my image folder**"
img2vec = Img2Vec()
vector_fh = open('resnet50_feature_vectors.txt', 'w+')
# For each test image, we store the filename and vector as key, value in a dictionary
pics = {}
filenames = glob.glob(input_path + "/*.*")
for filename in filenames:
print(filename)
img = Image.open(filename)
nd_arr = img2vec.get_vec(img)
#str_arr = nd_arr.tostring()
str_arr = np.array2string(nd_arr, formatter={'float_kind':lambda x: "%.2f" % x})
vector_fh.write(str_arr+"\n")
Here is what I am receiving as results:
$ head resnet50_feature_vectors.txt
[0.22 1.54 0.40 ... 0.15 0.56 0.22]
[0.57 1.34 1.78 ... 0.26 1.19 1.30]
[0.01 2.81 0.15 ... 0.28 0.41 0.27]
[0.30 0.80 0.15 ... 0.02 0.08 0.03]
[0.10 1.39 0.60 ... 0.13 0.25 0.04]
[0.62 0.71 0.72 ... 0.36 0.15 0.51]
[0.43 0.44 0.52 ... 0.40 0.29 0.33]
[0.07 1.14 0.40 ... 0.09 0.08 0.10]
[0.13 1.45 0.96 ... 0.19 0.03 0.11]
[0.06 1.84 0.19 ... 0.11 0.11 0.03]
How should I fix the way feature vectors are saved in the txt file?
I am trying to follow the tutorial here in which there is a .txt file containing each feature vector and one txt file containing the label for each feature vector.
I am speaking about the Python part of the tutorial for MNIST dataset https://lvdmaaten.github.io/tsne/code/tsne_python.zip
Upvotes: 0
Views: 1032